clang-tools  10.0.0git
ClangTidyDiagnosticConsumer.cpp
Go to the documentation of this file.
1 //===--- tools/extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp ----------=== //
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file This file implements ClangTidyDiagnosticConsumer, ClangTidyContext
10 /// and ClangTidyError classes.
11 ///
12 /// This tool uses the Clang Tooling infrastructure, see
13 /// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
14 /// for details on setting it up with LLVM source tree.
15 ///
16 //===----------------------------------------------------------------------===//
17 
19 #include "ClangTidyOptions.h"
20 #include "GlobList.h"
21 #include "clang/AST/ASTDiagnostic.h"
22 #include "clang/AST/Attr.h"
23 #include "clang/Basic/Diagnostic.h"
24 #include "clang/Basic/DiagnosticOptions.h"
25 #include "clang/Frontend/DiagnosticRenderer.h"
26 #include "clang/Tooling/Core/Diagnostic.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include <tuple>
30 #include <vector>
31 using namespace clang;
32 using namespace tidy;
33 
34 namespace {
35 class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
36 public:
37  ClangTidyDiagnosticRenderer(const LangOptions &LangOpts,
38  DiagnosticOptions *DiagOpts,
39  ClangTidyError &Error)
40  : DiagnosticRenderer(LangOpts, DiagOpts), Error(Error) {}
41 
42 protected:
43  void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
44  DiagnosticsEngine::Level Level, StringRef Message,
45  ArrayRef<CharSourceRange> Ranges,
46  DiagOrStoredDiag Info) override {
47  // Remove check name from the message.
48  // FIXME: Remove this once there's a better way to pass check names than
49  // appending the check name to the message in ClangTidyContext::diag and
50  // using getCustomDiagID.
51  std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]";
52  if (Message.endswith(CheckNameInMessage))
53  Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
54 
55  auto TidyMessage =
56  Loc.isValid()
57  ? tooling::DiagnosticMessage(Message, Loc.getManager(), Loc)
58  : tooling::DiagnosticMessage(Message);
59  if (Level == DiagnosticsEngine::Note) {
60  Error.Notes.push_back(TidyMessage);
61  return;
62  }
63  assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
64  Error.Message = TidyMessage;
65  }
66 
67  void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
68  DiagnosticsEngine::Level Level,
69  ArrayRef<CharSourceRange> Ranges) override {}
70 
71  void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
72  SmallVectorImpl<CharSourceRange> &Ranges,
73  ArrayRef<FixItHint> Hints) override {
74  assert(Loc.isValid());
75  tooling::DiagnosticMessage *DiagWithFix =
76  Level == DiagnosticsEngine::Note ? &Error.Notes.back() : &Error.Message;
77 
78  for (const auto &FixIt : Hints) {
79  CharSourceRange Range = FixIt.RemoveRange;
80  assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
81  "Invalid range in the fix-it hint.");
82  assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
83  "Only file locations supported in fix-it hints.");
84 
85  tooling::Replacement Replacement(Loc.getManager(), Range,
86  FixIt.CodeToInsert);
87  llvm::Error Err =
88  DiagWithFix->Fix[Replacement.getFilePath()].add(Replacement);
89  // FIXME: better error handling (at least, don't let other replacements be
90  // applied).
91  if (Err) {
92  llvm::errs() << "Fix conflicts with existing fix! "
93  << llvm::toString(std::move(Err)) << "\n";
94  assert(false && "Fix conflicts with existing fix!");
95  }
96  }
97  }
98 
99  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override {}
100 
101  void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
102  StringRef ModuleName) override {}
103 
104  void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
105  StringRef ModuleName) override {}
106 
107  void endDiagnostic(DiagOrStoredDiag D,
108  DiagnosticsEngine::Level Level) override {
109  assert(!Error.Message.Message.empty() && "Message has not been set");
110  }
111 
112 private:
114 };
115 } // end anonymous namespace
116 
117 ClangTidyError::ClangTidyError(StringRef CheckName,
118  ClangTidyError::Level DiagLevel,
119  StringRef BuildDirectory, bool IsWarningAsError)
120  : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
121  IsWarningAsError(IsWarningAsError) {}
122 
123 
125 public:
126  CachedGlobList(StringRef Globs) : Globs(Globs) {}
127 
128  bool contains(StringRef S) {
129  switch (auto &Result = Cache[S]) {
130  case Yes:
131  return true;
132  case No:
133  return false;
134  case None:
135  Result = Globs.contains(S) ? Yes : No;
136  return Result == Yes;
137  }
138  llvm_unreachable("invalid enum");
139  }
140 
141 private:
142  GlobList Globs;
143  enum Tristate { None, Yes, No };
144  llvm::StringMap<Tristate> Cache;
145 };
146 
148  std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
150  : DiagEngine(nullptr), OptionsProvider(std::move(OptionsProvider)),
151  Profile(false),
152  AllowEnablingAnalyzerAlphaCheckers(AllowEnablingAnalyzerAlphaCheckers) {
153  // Before the first translation unit we can get errors related to command-line
154  // parsing, use empty string for the file name in this case.
155  setCurrentFile("");
156 }
157 
159 
160 DiagnosticBuilder ClangTidyContext::diag(
161  StringRef CheckName, SourceLocation Loc, StringRef Description,
162  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
163  assert(Loc.isValid());
164  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
165  Level, (Description + " [" + CheckName + "]").str());
166  CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
167  return DiagEngine->Report(Loc, ID);
168 }
169 
170 void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
171  DiagEngine->setSourceManager(SourceMgr);
172 }
173 
174 void ClangTidyContext::setCurrentFile(StringRef File) {
175  CurrentFile = File;
176  CurrentOptions = getOptionsForFile(CurrentFile);
177  CheckFilter = std::make_unique<CachedGlobList>(*getOptions().Checks);
178  WarningAsErrorFilter =
179  std::make_unique<CachedGlobList>(*getOptions().WarningsAsErrors);
180 }
181 
182 void ClangTidyContext::setASTContext(ASTContext *Context) {
183  DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
184  LangOpts = Context->getLangOpts();
185 }
186 
188  return OptionsProvider->getGlobalOptions();
189 }
190 
192  return CurrentOptions;
193 }
194 
196  // Merge options on top of getDefaults() as a safeguard against options with
197  // unset values.
199  OptionsProvider->getOptions(File));
200 }
201 
202 void ClangTidyContext::setEnableProfiling(bool P) { Profile = P; }
203 
205  ProfilePrefix = Prefix;
206 }
207 
208 llvm::Optional<ClangTidyProfiling::StorageParams>
210  if (ProfilePrefix.empty())
211  return llvm::None;
212 
213  return ClangTidyProfiling::StorageParams(ProfilePrefix, CurrentFile);
214 }
215 
216 bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const {
217  assert(CheckFilter != nullptr);
218  return CheckFilter->contains(CheckName);
219 }
220 
221 bool ClangTidyContext::treatAsError(StringRef CheckName) const {
222  assert(WarningAsErrorFilter != nullptr);
223  return WarningAsErrorFilter->contains(CheckName);
224 }
225 
226 std::string ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
227  std::string ClangWarningOption =
228  DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(DiagnosticID);
229  if (!ClangWarningOption.empty())
230  return "clang-diagnostic-" + ClangWarningOption;
231  llvm::DenseMap<unsigned, std::string>::const_iterator I =
232  CheckNamesByDiagnosticID.find(DiagnosticID);
233  if (I != CheckNamesByDiagnosticID.end())
234  return I->second;
235  return "";
236 }
237 
239  ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine,
240  bool RemoveIncompatibleErrors)
241  : Context(Ctx), ExternalDiagEngine(ExternalDiagEngine),
242  RemoveIncompatibleErrors(RemoveIncompatibleErrors),
243  LastErrorRelatesToUserCode(false), LastErrorPassesLineFilter(false),
244  LastErrorWasIgnored(false) {}
245 
246 void ClangTidyDiagnosticConsumer::finalizeLastError() {
247  if (!Errors.empty()) {
248  ClangTidyError &Error = Errors.back();
249  if (!Context.isCheckEnabled(Error.DiagnosticName) &&
250  Error.DiagLevel != ClangTidyError::Error) {
251  ++Context.Stats.ErrorsIgnoredCheckFilter;
252  Errors.pop_back();
253  } else if (!LastErrorRelatesToUserCode) {
254  ++Context.Stats.ErrorsIgnoredNonUserCode;
255  Errors.pop_back();
256  } else if (!LastErrorPassesLineFilter) {
257  ++Context.Stats.ErrorsIgnoredLineFilter;
258  Errors.pop_back();
259  } else {
260  ++Context.Stats.ErrorsDisplayed;
261  }
262  }
263  LastErrorRelatesToUserCode = false;
264  LastErrorPassesLineFilter = false;
265 }
266 
267 static bool IsNOLINTFound(StringRef NolintDirectiveText, StringRef Line,
268  unsigned DiagID, const ClangTidyContext &Context) {
269  const size_t NolintIndex = Line.find(NolintDirectiveText);
270  if (NolintIndex == StringRef::npos)
271  return false;
272 
273  size_t BracketIndex = NolintIndex + NolintDirectiveText.size();
274  // Check if the specific checks are specified in brackets.
275  if (BracketIndex < Line.size() && Line[BracketIndex] == '(') {
276  ++BracketIndex;
277  const size_t BracketEndIndex = Line.find(')', BracketIndex);
278  if (BracketEndIndex != StringRef::npos) {
279  StringRef ChecksStr =
280  Line.substr(BracketIndex, BracketEndIndex - BracketIndex);
281  // Allow disabling all the checks with "*".
282  if (ChecksStr != "*") {
283  std::string CheckName = Context.getCheckName(DiagID);
284  // Allow specifying a few check names, delimited with comma.
285  SmallVector<StringRef, 1> Checks;
286  ChecksStr.split(Checks, ',', -1, false);
287  llvm::transform(Checks, Checks.begin(),
288  [](StringRef S) { return S.trim(); });
289  return llvm::find(Checks, CheckName) != Checks.end();
290  }
291  }
292  }
293  return true;
294 }
295 
296 static bool LineIsMarkedWithNOLINT(const SourceManager &SM, SourceLocation Loc,
297  unsigned DiagID,
298  const ClangTidyContext &Context) {
299  bool Invalid;
300  const char *CharacterData = SM.getCharacterData(Loc, &Invalid);
301  if (Invalid)
302  return false;
303 
304  // Check if there's a NOLINT on this line.
305  const char *P = CharacterData;
306  while (*P != '\0' && *P != '\r' && *P != '\n')
307  ++P;
308  StringRef RestOfLine(CharacterData, P - CharacterData + 1);
309  if (IsNOLINTFound("NOLINT", RestOfLine, DiagID, Context))
310  return true;
311 
312  // Check if there's a NOLINTNEXTLINE on the previous line.
313  const char *BufBegin =
314  SM.getCharacterData(SM.getLocForStartOfFile(SM.getFileID(Loc)), &Invalid);
315  if (Invalid || P == BufBegin)
316  return false;
317 
318  // Scan backwards over the current line.
319  P = CharacterData;
320  while (P != BufBegin && *P != '\n')
321  --P;
322 
323  // If we reached the begin of the file there is no line before it.
324  if (P == BufBegin)
325  return false;
326 
327  // Skip over the newline.
328  --P;
329  const char *LineEnd = P;
330 
331  // Now we're on the previous line. Skip to the beginning of it.
332  while (P != BufBegin && *P != '\n')
333  --P;
334 
335  RestOfLine = StringRef(P, LineEnd - P + 1);
336  if (IsNOLINTFound("NOLINTNEXTLINE", RestOfLine, DiagID, Context))
337  return true;
338 
339  return false;
340 }
341 
342 static bool LineIsMarkedWithNOLINTinMacro(const SourceManager &SM,
343  SourceLocation Loc, unsigned DiagID,
344  const ClangTidyContext &Context) {
345  while (true) {
346  if (LineIsMarkedWithNOLINT(SM, Loc, DiagID, Context))
347  return true;
348  if (!Loc.isMacroID())
349  return false;
350  Loc = SM.getImmediateExpansionRange(Loc).getBegin();
351  }
352  return false;
353 }
354 
355 namespace clang {
356 namespace tidy {
357 
358 bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel,
359  const Diagnostic &Info, ClangTidyContext &Context,
360  bool CheckMacroExpansion) {
361  return Info.getLocation().isValid() &&
362  DiagLevel != DiagnosticsEngine::Error &&
363  DiagLevel != DiagnosticsEngine::Fatal &&
364  (CheckMacroExpansion ? LineIsMarkedWithNOLINTinMacro
365  : LineIsMarkedWithNOLINT)(Info.getSourceManager(),
366  Info.getLocation(),
367  Info.getID(), Context);
368 }
369 
370 } // namespace tidy
371 } // namespace clang
372 
374  DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
375  if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
376  return;
377 
378  if (shouldSuppressDiagnostic(DiagLevel, Info, Context)) {
379  ++Context.Stats.ErrorsIgnoredNOLINT;
380  // Ignored a warning, should ignore related notes as well
381  LastErrorWasIgnored = true;
382  return;
383  }
384 
385  LastErrorWasIgnored = false;
386  // Count warnings/errors.
387  DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
388 
389  if (DiagLevel == DiagnosticsEngine::Note) {
390  assert(!Errors.empty() &&
391  "A diagnostic note can only be appended to a message.");
392  } else {
393  finalizeLastError();
394  std::string CheckName = Context.getCheckName(Info.getID());
395  if (CheckName.empty()) {
396  // This is a compiler diagnostic without a warning option. Assign check
397  // name based on its level.
398  switch (DiagLevel) {
399  case DiagnosticsEngine::Error:
400  case DiagnosticsEngine::Fatal:
401  CheckName = "clang-diagnostic-error";
402  break;
403  case DiagnosticsEngine::Warning:
404  CheckName = "clang-diagnostic-warning";
405  break;
406  default:
407  CheckName = "clang-diagnostic-unknown";
408  break;
409  }
410  }
411 
412  ClangTidyError::Level Level = ClangTidyError::Warning;
413  if (DiagLevel == DiagnosticsEngine::Error ||
414  DiagLevel == DiagnosticsEngine::Fatal) {
415  // Force reporting of Clang errors regardless of filters and non-user
416  // code.
417  Level = ClangTidyError::Error;
418  LastErrorRelatesToUserCode = true;
419  LastErrorPassesLineFilter = true;
420  }
421  bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning &&
422  Context.treatAsError(CheckName);
423  Errors.emplace_back(CheckName, Level, Context.getCurrentBuildDirectory(),
424  IsWarningAsError);
425  }
426 
427  if (ExternalDiagEngine) {
428  // If there is an external diagnostics engine, like in the
429  // ClangTidyPluginAction case, forward the diagnostics to it.
430  forwardDiagnostic(Info);
431  } else {
432  ClangTidyDiagnosticRenderer Converter(
433  Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(),
434  Errors.back());
435  SmallString<100> Message;
436  Info.FormatDiagnostic(Message);
437  FullSourceLoc Loc;
438  if (Info.getLocation().isValid() && Info.hasSourceManager())
439  Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
440  Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
441  Info.getFixItHints());
442  }
443 
444  if (Info.hasSourceManager())
445  checkFilters(Info.getLocation(), Info.getSourceManager());
446 }
447 
448 bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
449  unsigned LineNumber) const {
450  if (Context.getGlobalOptions().LineFilter.empty())
451  return true;
452  for (const FileFilter &Filter : Context.getGlobalOptions().LineFilter) {
453  if (FileName.endswith(Filter.Name)) {
454  if (Filter.LineRanges.empty())
455  return true;
456  for (const FileFilter::LineRange &Range : Filter.LineRanges) {
457  if (Range.first <= LineNumber && LineNumber <= Range.second)
458  return true;
459  }
460  return false;
461  }
462  }
463  return false;
464 }
465 
466 void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
467  // Acquire a diagnostic ID also in the external diagnostics engine.
468  auto DiagLevelAndFormatString =
469  Context.getDiagLevelAndFormatString(Info.getID(), Info.getLocation());
470  unsigned ExternalID = ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID(
471  DiagLevelAndFormatString.first, DiagLevelAndFormatString.second);
472 
473  // Forward the details.
474  auto Builder = ExternalDiagEngine->Report(Info.getLocation(), ExternalID);
475  for (auto Hint : Info.getFixItHints())
476  Builder << Hint;
477  for (auto Range : Info.getRanges())
478  Builder << Range;
479  for (unsigned Index = 0; Index < Info.getNumArgs(); ++Index) {
480  DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index);
481  switch (Kind) {
482  case clang::DiagnosticsEngine::ak_std_string:
483  Builder << Info.getArgStdStr(Index);
484  break;
485  case clang::DiagnosticsEngine::ak_c_string:
486  Builder << Info.getArgCStr(Index);
487  break;
488  case clang::DiagnosticsEngine::ak_sint:
489  Builder << Info.getArgSInt(Index);
490  break;
491  case clang::DiagnosticsEngine::ak_uint:
492  Builder << Info.getArgUInt(Index);
493  break;
494  case clang::DiagnosticsEngine::ak_tokenkind:
495  Builder << static_cast<tok::TokenKind>(Info.getRawArg(Index));
496  break;
497  case clang::DiagnosticsEngine::ak_identifierinfo:
498  Builder << Info.getArgIdentifier(Index);
499  break;
500  case clang::DiagnosticsEngine::ak_qual:
501  Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index));
502  break;
503  case clang::DiagnosticsEngine::ak_qualtype:
504  Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index));
505  break;
506  case clang::DiagnosticsEngine::ak_declarationname:
507  Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index));
508  break;
509  case clang::DiagnosticsEngine::ak_nameddecl:
510  Builder << reinterpret_cast<const NamedDecl *>(Info.getRawArg(Index));
511  break;
512  case clang::DiagnosticsEngine::ak_nestednamespec:
513  Builder << reinterpret_cast<NestedNameSpecifier *>(Info.getRawArg(Index));
514  break;
515  case clang::DiagnosticsEngine::ak_declcontext:
516  Builder << reinterpret_cast<DeclContext *>(Info.getRawArg(Index));
517  break;
518  case clang::DiagnosticsEngine::ak_qualtype_pair:
519  assert(false); // This one is not passed around.
520  break;
521  case clang::DiagnosticsEngine::ak_attr:
522  Builder << reinterpret_cast<Attr *>(Info.getRawArg(Index));
523  break;
524  case clang::DiagnosticsEngine::ak_addrspace:
525  Builder << static_cast<LangAS>(Info.getRawArg(Index));
526  break;
527  }
528  }
529 }
530 
531 void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
532  const SourceManager &Sources) {
533  // Invalid location may mean a diagnostic in a command line, don't skip these.
534  if (!Location.isValid()) {
535  LastErrorRelatesToUserCode = true;
536  LastErrorPassesLineFilter = true;
537  return;
538  }
539 
540  if (!*Context.getOptions().SystemHeaders &&
541  Sources.isInSystemHeader(Location))
542  return;
543 
544  // FIXME: We start with a conservative approach here, but the actual type of
545  // location needed depends on the check (in particular, where this check wants
546  // to apply fixes).
547  FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
548  const FileEntry *File = Sources.getFileEntryForID(FID);
549 
550  // -DMACRO definitions on the command line have locations in a virtual buffer
551  // that doesn't have a FileEntry. Don't skip these as well.
552  if (!File) {
553  LastErrorRelatesToUserCode = true;
554  LastErrorPassesLineFilter = true;
555  return;
556  }
557 
558  StringRef FileName(File->getName());
559  LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
560  Sources.isInMainFile(Location) ||
561  getHeaderFilter()->match(FileName);
562 
563  unsigned LineNumber = Sources.getExpansionLineNumber(Location);
564  LastErrorPassesLineFilter =
565  LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
566 }
567 
568 llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
569  if (!HeaderFilter)
570  HeaderFilter =
571  std::make_unique<llvm::Regex>(*Context.getOptions().HeaderFilterRegex);
572  return HeaderFilter.get();
573 }
574 
575 void ClangTidyDiagnosticConsumer::removeIncompatibleErrors() {
576  // Each error is modelled as the set of intervals in which it applies
577  // replacements. To detect overlapping replacements, we use a sweep line
578  // algorithm over these sets of intervals.
579  // An event here consists of the opening or closing of an interval. During the
580  // process, we maintain a counter with the amount of open intervals. If we
581  // find an endpoint of an interval and this counter is different from 0, it
582  // means that this interval overlaps with another one, so we set it as
583  // inapplicable.
584  struct Event {
585  // An event can be either the begin or the end of an interval.
586  enum EventType {
587  ET_Begin = 1,
588  ET_End = -1,
589  };
590 
591  Event(unsigned Begin, unsigned End, EventType Type, unsigned ErrorId,
592  unsigned ErrorSize)
593  : Type(Type), ErrorId(ErrorId) {
594  // The events are going to be sorted by their position. In case of draw:
595  //
596  // * If an interval ends at the same position at which other interval
597  // begins, this is not an overlapping, so we want to remove the ending
598  // interval before adding the starting one: end events have higher
599  // priority than begin events.
600  //
601  // * If we have several begin points at the same position, we will mark as
602  // inapplicable the ones that we process later, so the first one has to
603  // be the one with the latest end point, because this one will contain
604  // all the other intervals. For the same reason, if we have several end
605  // points in the same position, the last one has to be the one with the
606  // earliest begin point. In both cases, we sort non-increasingly by the
607  // position of the complementary.
608  //
609  // * In case of two equal intervals, the one whose error is bigger can
610  // potentially contain the other one, so we want to process its begin
611  // points before and its end points later.
612  //
613  // * Finally, if we have two equal intervals whose errors have the same
614  // size, none of them will be strictly contained inside the other.
615  // Sorting by ErrorId will guarantee that the begin point of the first
616  // one will be processed before, disallowing the second one, and the
617  // end point of the first one will also be processed before,
618  // disallowing the first one.
619  if (Type == ET_Begin)
620  Priority = std::make_tuple(Begin, Type, -End, -ErrorSize, ErrorId);
621  else
622  Priority = std::make_tuple(End, Type, -Begin, ErrorSize, ErrorId);
623  }
624 
625  bool operator<(const Event &Other) const {
626  return Priority < Other.Priority;
627  }
628 
629  // Determines if this event is the begin or the end of an interval.
630  EventType Type;
631  // The index of the error to which the interval that generated this event
632  // belongs.
633  unsigned ErrorId;
634  // The events will be sorted based on this field.
635  std::tuple<unsigned, EventType, int, int, unsigned> Priority;
636  };
637 
638  // Compute error sizes.
639  std::vector<int> Sizes;
640  std::vector<
641  std::pair<ClangTidyError *, llvm::StringMap<tooling::Replacements> *>>
642  ErrorFixes;
643  for (auto &Error : Errors) {
644  if (const auto *Fix = tooling::selectFirstFix(Error))
645  ErrorFixes.emplace_back(
646  &Error, const_cast<llvm::StringMap<tooling::Replacements> *>(Fix));
647  }
648  for (const auto &ErrorAndFix : ErrorFixes) {
649  int Size = 0;
650  for (const auto &FileAndReplaces : *ErrorAndFix.second) {
651  for (const auto &Replace : FileAndReplaces.second)
652  Size += Replace.getLength();
653  }
654  Sizes.push_back(Size);
655  }
656 
657  // Build events from error intervals.
658  std::map<std::string, std::vector<Event>> FileEvents;
659  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
660  for (const auto &FileAndReplace : *ErrorFixes[I].second) {
661  for (const auto &Replace : FileAndReplace.second) {
662  unsigned Begin = Replace.getOffset();
663  unsigned End = Begin + Replace.getLength();
664  const std::string &FilePath = Replace.getFilePath();
665  // FIXME: Handle empty intervals, such as those from insertions.
666  if (Begin == End)
667  continue;
668  auto &Events = FileEvents[FilePath];
669  Events.emplace_back(Begin, End, Event::ET_Begin, I, Sizes[I]);
670  Events.emplace_back(Begin, End, Event::ET_End, I, Sizes[I]);
671  }
672  }
673  }
674 
675  std::vector<bool> Apply(ErrorFixes.size(), true);
676  for (auto &FileAndEvents : FileEvents) {
677  std::vector<Event> &Events = FileAndEvents.second;
678  // Sweep.
679  std::sort(Events.begin(), Events.end());
680  int OpenIntervals = 0;
681  for (const auto &Event : Events) {
682  if (Event.Type == Event::ET_End)
683  --OpenIntervals;
684  // This has to be checked after removing the interval from the count if it
685  // is an end event, or before adding it if it is a begin event.
686  if (OpenIntervals != 0)
687  Apply[Event.ErrorId] = false;
688  if (Event.Type == Event::ET_Begin)
689  ++OpenIntervals;
690  }
691  assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
692  }
693 
694  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
695  if (!Apply[I]) {
696  ErrorFixes[I].second->clear();
697  ErrorFixes[I].first->Notes.emplace_back(
698  "this fix will not be applied because it overlaps with another fix");
699  }
700  }
701 }
702 
703 namespace {
704 struct LessClangTidyError {
705  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
706  const tooling::DiagnosticMessage &M1 = LHS.Message;
707  const tooling::DiagnosticMessage &M2 = RHS.Message;
708 
709  return
710  std::tie(M1.FilePath, M1.FileOffset, LHS.DiagnosticName, M1.Message) <
711  std::tie(M2.FilePath, M2.FileOffset, RHS.DiagnosticName, M2.Message);
712  }
713 };
714 struct EqualClangTidyError {
715  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
716  LessClangTidyError Less;
717  return !Less(LHS, RHS) && !Less(RHS, LHS);
718  }
719 };
720 } // end anonymous namespace
721 
722 std::vector<ClangTidyError> ClangTidyDiagnosticConsumer::take() {
723  finalizeLastError();
724 
725  std::sort(Errors.begin(), Errors.end(), LessClangTidyError());
726  Errors.erase(std::unique(Errors.begin(), Errors.end(), EqualClangTidyError()),
727  Errors.end());
728  if (RemoveIncompatibleErrors)
729  removeIncompatibleErrors();
730  return std::move(Errors);
731 }
llvm::Optional< std::string > Checks
Checks filter.
SourceLocation Loc
&#39;#&#39; location in the include directive
ClangTidyOptions mergeWith(const ClangTidyOptions &Other) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
const ClangTidyGlobalOptions & getGlobalOptions() const
Returns global options.
static bool LineIsMarkedWithNOLINTinMacro(const SourceManager &SM, SourceLocation Loc, unsigned DiagID, const ClangTidyContext &Context)
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
constexpr llvm::StringLiteral Message
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Contains options for clang-tidy.
BindArgumentKind Kind
Context Ctx
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:58
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine=nullptr, bool RemoveIncompatibleErrors=true)
ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory, bool IsWarningAsError)
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
PathRef FileName
llvm::Optional< std::string > WarningsAsErrors
WarningsAsErrors filter.
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
CodeCompletionBuilder Builder
Contains a list of line ranges in a single file.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info, ClangTidyContext &Context, bool CheckMacroExpansion)
Check whether a given diagnostic should be suppressed due to the presence of a "NOLINT" suppression c...
ClangTidyContext(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider, bool AllowEnablingAnalyzerAlphaCheckers=false)
Initializes ClangTidyContext instance.
static bool IsNOLINTFound(StringRef NolintDirectiveText, StringRef Line, unsigned DiagID, const ClangTidyContext &Context)
std::vector< FixItHint > Hints
CharSourceRange Range
SourceRange for the file name.
A detected error complete with information to display diagnostic and automatic fix.
std::string getCheckName(unsigned DiagnosticID) const
Returns the name of the clang-tidy check which produced this diagnostic ID.
static cl::opt< std::string > Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< FixItHint > FixIt
static ClangTidyOptions getDefaults()
These options are used for all settings that haven&#39;t been overridden by the OptionsProvider.
const char * Description
Definition: Dexp.cpp:257
static cl::opt< bool > Fix("fix", cl::desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
bool treatAsError(StringRef CheckName) const
Returns true if the check should be upgraded to error for the CurrentFile.
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
static bool LineIsMarkedWithNOLINT(const SourceManager &SM, SourceLocation Loc, unsigned DiagID, const ClangTidyContext &Context)
NodeType Type
const SymbolIndex * Index
Definition: Dexp.cpp:84