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