clang-tools  16.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 "NoLintDirectiveHandler.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/ASTDiagnostic.h"
24 #include "clang/AST/Attr.h"
25 #include "clang/Basic/Diagnostic.h"
26 #include "clang/Basic/DiagnosticOptions.h"
27 #include "clang/Basic/FileManager.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "clang/Frontend/DiagnosticRenderer.h"
30 #include "clang/Lex/Lexer.h"
31 #include "clang/Tooling/Core/Diagnostic.h"
32 #include "clang/Tooling/Core/Replacement.h"
33 #include "llvm/ADT/BitVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SmallString.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/Support/FormatVariadic.h"
38 #include "llvm/Support/Regex.h"
39 #include <tuple>
40 #include <utility>
41 #include <vector>
42 using namespace clang;
43 using namespace tidy;
44 
45 namespace {
46 class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
47 public:
48  ClangTidyDiagnosticRenderer(const LangOptions &LangOpts,
49  DiagnosticOptions *DiagOpts,
50  ClangTidyError &Error)
51  : DiagnosticRenderer(LangOpts, DiagOpts), Error(Error) {}
52 
53 protected:
54  void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
55  DiagnosticsEngine::Level Level, StringRef Message,
56  ArrayRef<CharSourceRange> Ranges,
57  DiagOrStoredDiag Info) override {
58  // Remove check name from the message.
59  // FIXME: Remove this once there's a better way to pass check names than
60  // appending the check name to the message in ClangTidyContext::diag and
61  // using getCustomDiagID.
62  std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]";
63  if (Message.endswith(CheckNameInMessage))
64  Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
65 
66  auto TidyMessage =
67  Loc.isValid()
68  ? tooling::DiagnosticMessage(Message, Loc.getManager(), Loc)
69  : tooling::DiagnosticMessage(Message);
70 
71  // Make sure that if a TokenRange is received from the check it is unfurled
72  // into a real CharRange for the diagnostic printer later.
73  // Whatever we store here gets decoupled from the current SourceManager, so
74  // we **have to** know the exact position and length of the highlight.
75  auto ToCharRange = [this, &Loc](const CharSourceRange &SourceRange) {
76  if (SourceRange.isCharRange())
77  return SourceRange;
78  assert(SourceRange.isTokenRange());
79  SourceLocation End = Lexer::getLocForEndOfToken(
80  SourceRange.getEnd(), 0, Loc.getManager(), LangOpts);
81  return CharSourceRange::getCharRange(SourceRange.getBegin(), End);
82  };
83 
84  // We are only interested in valid ranges.
85  auto ValidRanges =
86  llvm::make_filter_range(Ranges, [](const CharSourceRange &R) {
87  return R.getAsRange().isValid();
88  });
89 
90  if (Level == DiagnosticsEngine::Note) {
91  Error.Notes.push_back(TidyMessage);
92  for (const CharSourceRange &SourceRange : ValidRanges)
93  Error.Notes.back().Ranges.emplace_back(Loc.getManager(),
94  ToCharRange(SourceRange));
95  return;
96  }
97  assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
98  Error.Message = TidyMessage;
99  for (const CharSourceRange &SourceRange : ValidRanges)
100  Error.Message.Ranges.emplace_back(Loc.getManager(),
101  ToCharRange(SourceRange));
102  }
103 
104  void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
105  DiagnosticsEngine::Level Level,
106  ArrayRef<CharSourceRange> Ranges) override {}
107 
108  void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
109  SmallVectorImpl<CharSourceRange> &Ranges,
110  ArrayRef<FixItHint> Hints) override {
111  assert(Loc.isValid());
112  tooling::DiagnosticMessage *DiagWithFix =
113  Level == DiagnosticsEngine::Note ? &Error.Notes.back() : &Error.Message;
114 
115  for (const auto &FixIt : Hints) {
116  CharSourceRange Range = FixIt.RemoveRange;
117  assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
118  "Invalid range in the fix-it hint.");
119  assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
120  "Only file locations supported in fix-it hints.");
121 
122  tooling::Replacement Replacement(Loc.getManager(), Range,
123  FixIt.CodeToInsert);
124  llvm::Error Err =
125  DiagWithFix->Fix[Replacement.getFilePath()].add(Replacement);
126  // FIXME: better error handling (at least, don't let other replacements be
127  // applied).
128  if (Err) {
129  llvm::errs() << "Fix conflicts with existing fix! "
130  << llvm::toString(std::move(Err)) << "\n";
131  assert(false && "Fix conflicts with existing fix!");
132  }
133  }
134  }
135 
136  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override {}
137 
138  void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
139  StringRef ModuleName) override {}
140 
141  void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
142  StringRef ModuleName) override {}
143 
144  void endDiagnostic(DiagOrStoredDiag D,
145  DiagnosticsEngine::Level Level) override {
146  assert(!Error.Message.Message.empty() && "Message has not been set");
147  }
148 
149 private:
151 };
152 } // end anonymous namespace
153 
154 ClangTidyError::ClangTidyError(StringRef CheckName,
155  ClangTidyError::Level DiagLevel,
156  StringRef BuildDirectory, bool IsWarningAsError)
157  : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
158  IsWarningAsError(IsWarningAsError) {}
159 
161  std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
163  : DiagEngine(nullptr), OptionsProvider(std::move(OptionsProvider)),
164  Profile(false),
166  SelfContainedDiags(false) {
167  // Before the first translation unit we can get errors related to command-line
168  // parsing, use empty string for the file name in this case.
169  setCurrentFile("");
170 }
171 
173 
174 DiagnosticBuilder ClangTidyContext::diag(
175  StringRef CheckName, SourceLocation Loc, StringRef Description,
176  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
177  assert(Loc.isValid());
178  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
179  Level, (Description + " [" + CheckName + "]").str());
180  CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
181  return DiagEngine->Report(Loc, ID);
182 }
183 
184 DiagnosticBuilder ClangTidyContext::diag(
185  StringRef CheckName, StringRef Description,
186  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
187  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
188  Level, (Description + " [" + CheckName + "]").str());
189  CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
190  return DiagEngine->Report(ID);
191 }
192 
193 DiagnosticBuilder ClangTidyContext::diag(const tooling::Diagnostic &Error) {
194  SourceManager &SM = DiagEngine->getSourceManager();
195  llvm::ErrorOr<const FileEntry *> File =
196  SM.getFileManager().getFile(Error.Message.FilePath);
197  FileID ID = SM.getOrCreateFileID(*File, SrcMgr::C_User);
198  SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID);
199  SourceLocation Loc = FileStartLoc.getLocWithOffset(
200  static_cast<SourceLocation::IntTy>(Error.Message.FileOffset));
201  return diag(Error.DiagnosticName, Loc, Error.Message.Message,
202  static_cast<DiagnosticIDs::Level>(Error.DiagLevel));
203 }
204 
206  StringRef Message,
207  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
208  return diag("clang-tidy-config", Message, Level);
209 }
210 
212  DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info,
213  SmallVectorImpl<tooling::Diagnostic> &NoLintErrors, bool AllowIO,
214  bool EnableNoLintBlocks) {
215  std::string CheckName = getCheckName(Info.getID());
216  return NoLintHandler.shouldSuppress(DiagLevel, Info, CheckName, NoLintErrors,
217  AllowIO, EnableNoLintBlocks);
218 }
219 
221  DiagEngine->setSourceManager(SourceMgr);
222 }
223 
224 void ClangTidyContext::setCurrentFile(StringRef File) {
225  CurrentFile = std::string(File);
226  CurrentOptions = getOptionsForFile(CurrentFile);
227  CheckFilter = std::make_unique<CachedGlobList>(*getOptions().Checks);
228  WarningAsErrorFilter =
229  std::make_unique<CachedGlobList>(*getOptions().WarningsAsErrors);
230 }
231 
232 void ClangTidyContext::setASTContext(ASTContext *Context) {
233  DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
234  LangOpts = Context->getLangOpts();
235 }
236 
238  return OptionsProvider->getGlobalOptions();
239 }
240 
242  return CurrentOptions;
243 }
244 
246  // Merge options on top of getDefaults() as a safeguard against options with
247  // unset values.
249  OptionsProvider->getOptions(File), 0);
250 }
251 
252 void ClangTidyContext::setEnableProfiling(bool P) { Profile = P; }
253 
255  ProfilePrefix = std::string(Prefix);
256 }
257 
258 llvm::Optional<ClangTidyProfiling::StorageParams>
260  if (ProfilePrefix.empty())
261  return llvm::None;
262 
263  return ClangTidyProfiling::StorageParams(ProfilePrefix, CurrentFile);
264 }
265 
266 bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const {
267  assert(CheckFilter != nullptr);
268  return CheckFilter->contains(CheckName);
269 }
270 
271 bool ClangTidyContext::treatAsError(StringRef CheckName) const {
272  assert(WarningAsErrorFilter != nullptr);
273  return WarningAsErrorFilter->contains(CheckName);
274 }
275 
276 std::string ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
277  std::string ClangWarningOption = std::string(
278  DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(DiagnosticID));
279  if (!ClangWarningOption.empty())
280  return "clang-diagnostic-" + ClangWarningOption;
281  llvm::DenseMap<unsigned, std::string>::const_iterator I =
282  CheckNamesByDiagnosticID.find(DiagnosticID);
283  if (I != CheckNamesByDiagnosticID.end())
284  return I->second;
285  return "";
286 }
287 
289  ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine,
290  bool RemoveIncompatibleErrors, bool GetFixesFromNotes,
291  bool EnableNolintBlocks)
292  : Context(Ctx), ExternalDiagEngine(ExternalDiagEngine),
293  RemoveIncompatibleErrors(RemoveIncompatibleErrors),
294  GetFixesFromNotes(GetFixesFromNotes),
295  EnableNolintBlocks(EnableNolintBlocks), LastErrorRelatesToUserCode(false),
296  LastErrorPassesLineFilter(false), LastErrorWasIgnored(false) {}
297 
298 void ClangTidyDiagnosticConsumer::finalizeLastError() {
299  if (!Errors.empty()) {
300  ClangTidyError &Error = Errors.back();
301  if (Error.DiagnosticName == "clang-tidy-config") {
302  // Never ignore these.
303  } else if (!Context.isCheckEnabled(Error.DiagnosticName) &&
304  Error.DiagLevel != ClangTidyError::Error) {
305  ++Context.Stats.ErrorsIgnoredCheckFilter;
306  Errors.pop_back();
307  } else if (!LastErrorRelatesToUserCode) {
308  ++Context.Stats.ErrorsIgnoredNonUserCode;
309  Errors.pop_back();
310  } else if (!LastErrorPassesLineFilter) {
311  ++Context.Stats.ErrorsIgnoredLineFilter;
312  Errors.pop_back();
313  } else {
314  ++Context.Stats.ErrorsDisplayed;
315  }
316  }
317  LastErrorRelatesToUserCode = false;
318  LastErrorPassesLineFilter = false;
319 }
320 
321 namespace clang {
322 namespace tidy {
323 
324 const llvm::StringMap<tooling::Replacements> *
325 getFixIt(const tooling::Diagnostic &Diagnostic, bool GetFixFromNotes) {
326  if (!Diagnostic.Message.Fix.empty())
327  return &Diagnostic.Message.Fix;
328  if (!GetFixFromNotes)
329  return nullptr;
330  const llvm::StringMap<tooling::Replacements> *Result = nullptr;
331  for (const auto &Note : Diagnostic.Notes) {
332  if (!Note.Fix.empty()) {
333  if (Result)
334  // We have 2 different fixes in notes, bail out.
335  return nullptr;
336  Result = &Note.Fix;
337  }
338  }
339  return Result;
340 }
341 
342 } // namespace tidy
343 } // namespace clang
344 
346  DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
347  if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
348  return;
349 
350  SmallVector<tooling::Diagnostic, 1> SuppressionErrors;
351  if (Context.shouldSuppressDiagnostic(DiagLevel, Info, SuppressionErrors,
352  EnableNolintBlocks)) {
353  ++Context.Stats.ErrorsIgnoredNOLINT;
354  // Ignored a warning, should ignore related notes as well
355  LastErrorWasIgnored = true;
356  Context.DiagEngine->Clear();
357  for (const auto &Error : SuppressionErrors)
358  Context.diag(Error);
359  return;
360  }
361 
362  LastErrorWasIgnored = false;
363  // Count warnings/errors.
364  DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
365 
366  if (DiagLevel == DiagnosticsEngine::Note) {
367  assert(!Errors.empty() &&
368  "A diagnostic note can only be appended to a message.");
369  } else {
370  finalizeLastError();
371  std::string CheckName = Context.getCheckName(Info.getID());
372  if (CheckName.empty()) {
373  // This is a compiler diagnostic without a warning option. Assign check
374  // name based on its level.
375  switch (DiagLevel) {
377  case DiagnosticsEngine::Fatal:
378  CheckName = "clang-diagnostic-error";
379  break;
381  CheckName = "clang-diagnostic-warning";
382  break;
383  case DiagnosticsEngine::Remark:
384  CheckName = "clang-diagnostic-remark";
385  break;
386  default:
387  CheckName = "clang-diagnostic-unknown";
388  break;
389  }
390  }
391 
392  ClangTidyError::Level Level = ClangTidyError::Warning;
393  if (DiagLevel == DiagnosticsEngine::Error ||
394  DiagLevel == DiagnosticsEngine::Fatal) {
395  // Force reporting of Clang errors regardless of filters and non-user
396  // code.
397  Level = ClangTidyError::Error;
398  LastErrorRelatesToUserCode = true;
399  LastErrorPassesLineFilter = true;
400  } else if (DiagLevel == DiagnosticsEngine::Remark) {
401  Level = ClangTidyError::Remark;
402  }
403 
404  bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning &&
405  Context.treatAsError(CheckName);
406  if (IsWarningAsError)
407  Level = ClangTidyError::Error;
408  Errors.emplace_back(CheckName, Level, Context.getCurrentBuildDirectory(),
409  IsWarningAsError);
410  }
411 
412  if (ExternalDiagEngine) {
413  // If there is an external diagnostics engine, like in the
414  // ClangTidyPluginAction case, forward the diagnostics to it.
415  forwardDiagnostic(Info);
416  } else {
417  ClangTidyDiagnosticRenderer Converter(
418  Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(),
419  Errors.back());
420  SmallString<100> Message;
421  Info.FormatDiagnostic(Message);
422  FullSourceLoc Loc;
423  if (Info.getLocation().isValid() && Info.hasSourceManager())
424  Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
425  Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
426  Info.getFixItHints());
427  }
428 
429  if (Info.hasSourceManager())
430  checkFilters(Info.getLocation(), Info.getSourceManager());
431 
432  Context.DiagEngine->Clear();
433  for (const auto &Error : SuppressionErrors)
434  Context.diag(Error);
435 }
436 
437 bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
438  unsigned LineNumber) const {
439  if (Context.getGlobalOptions().LineFilter.empty())
440  return true;
441  for (const FileFilter &Filter : Context.getGlobalOptions().LineFilter) {
442  if (FileName.endswith(Filter.Name)) {
443  if (Filter.LineRanges.empty())
444  return true;
445  for (const FileFilter::LineRange &Range : Filter.LineRanges) {
446  if (Range.first <= LineNumber && LineNumber <= Range.second)
447  return true;
448  }
449  return false;
450  }
451  }
452  return false;
453 }
454 
455 void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
456  // Acquire a diagnostic ID also in the external diagnostics engine.
457  auto DiagLevelAndFormatString =
458  Context.getDiagLevelAndFormatString(Info.getID(), Info.getLocation());
459  unsigned ExternalID = ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID(
460  DiagLevelAndFormatString.first, DiagLevelAndFormatString.second);
461 
462  // Forward the details.
463  auto Builder = ExternalDiagEngine->Report(Info.getLocation(), ExternalID);
464  for (auto Hint : Info.getFixItHints())
465  Builder << Hint;
466  for (auto Range : Info.getRanges())
467  Builder << Range;
468  for (unsigned Index = 0; Index < Info.getNumArgs(); ++Index) {
469  DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index);
470  switch (Kind) {
471  case clang::DiagnosticsEngine::ak_std_string:
472  Builder << Info.getArgStdStr(Index);
473  break;
474  case clang::DiagnosticsEngine::ak_c_string:
475  Builder << Info.getArgCStr(Index);
476  break;
477  case clang::DiagnosticsEngine::ak_sint:
478  Builder << Info.getArgSInt(Index);
479  break;
480  case clang::DiagnosticsEngine::ak_uint:
481  Builder << Info.getArgUInt(Index);
482  break;
483  case clang::DiagnosticsEngine::ak_tokenkind:
484  Builder << static_cast<tok::TokenKind>(Info.getRawArg(Index));
485  break;
486  case clang::DiagnosticsEngine::ak_identifierinfo:
487  Builder << Info.getArgIdentifier(Index);
488  break;
489  case clang::DiagnosticsEngine::ak_qual:
490  Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index));
491  break;
492  case clang::DiagnosticsEngine::ak_qualtype:
493  Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index));
494  break;
495  case clang::DiagnosticsEngine::ak_declarationname:
496  Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index));
497  break;
498  case clang::DiagnosticsEngine::ak_nameddecl:
499  Builder << reinterpret_cast<const NamedDecl *>(Info.getRawArg(Index));
500  break;
501  case clang::DiagnosticsEngine::ak_nestednamespec:
502  Builder << reinterpret_cast<NestedNameSpecifier *>(Info.getRawArg(Index));
503  break;
504  case clang::DiagnosticsEngine::ak_declcontext:
505  Builder << reinterpret_cast<DeclContext *>(Info.getRawArg(Index));
506  break;
507  case clang::DiagnosticsEngine::ak_qualtype_pair:
508  assert(false); // This one is not passed around.
509  break;
510  case clang::DiagnosticsEngine::ak_attr:
511  Builder << reinterpret_cast<Attr *>(Info.getRawArg(Index));
512  break;
513  case clang::DiagnosticsEngine::ak_addrspace:
514  Builder << static_cast<LangAS>(Info.getRawArg(Index));
515  break;
516  }
517  }
518 }
519 
520 void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
521  const SourceManager &Sources) {
522  // Invalid location may mean a diagnostic in a command line, don't skip these.
523  if (!Location.isValid()) {
524  LastErrorRelatesToUserCode = true;
525  LastErrorPassesLineFilter = true;
526  return;
527  }
528 
529  if (!*Context.getOptions().SystemHeaders &&
530  (Sources.isInSystemHeader(Location) || Sources.isInSystemMacro(Location)))
531  return;
532 
533  // FIXME: We start with a conservative approach here, but the actual type of
534  // location needed depends on the check (in particular, where this check wants
535  // to apply fixes).
536  FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
537  const FileEntry *File = Sources.getFileEntryForID(FID);
538 
539  // -DMACRO definitions on the command line have locations in a virtual buffer
540  // that doesn't have a FileEntry. Don't skip these as well.
541  if (!File) {
542  LastErrorRelatesToUserCode = true;
543  LastErrorPassesLineFilter = true;
544  return;
545  }
546 
547  StringRef FileName(File->getName());
548  LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
549  Sources.isInMainFile(Location) ||
550  getHeaderFilter()->match(FileName);
551 
552  unsigned LineNumber = Sources.getExpansionLineNumber(Location);
553  LastErrorPassesLineFilter =
554  LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
555 }
556 
557 llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
558  if (!HeaderFilter)
559  HeaderFilter =
560  std::make_unique<llvm::Regex>(*Context.getOptions().HeaderFilterRegex);
561  return HeaderFilter.get();
562 }
563 
564 void ClangTidyDiagnosticConsumer::removeIncompatibleErrors() {
565  // Each error is modelled as the set of intervals in which it applies
566  // replacements. To detect overlapping replacements, we use a sweep line
567  // algorithm over these sets of intervals.
568  // An event here consists of the opening or closing of an interval. During the
569  // process, we maintain a counter with the amount of open intervals. If we
570  // find an endpoint of an interval and this counter is different from 0, it
571  // means that this interval overlaps with another one, so we set it as
572  // inapplicable.
573  struct Event {
574  // An event can be either the begin or the end of an interval.
575  enum EventType {
576  ET_Begin = 1,
577  ET_Insert = 0,
578  ET_End = -1,
579  };
580 
581  Event(unsigned Begin, unsigned End, EventType Type, unsigned ErrorId,
582  unsigned ErrorSize)
583  : Type(Type), ErrorId(ErrorId) {
584  // The events are going to be sorted by their position. In case of draw:
585  //
586  // * If an interval ends at the same position at which other interval
587  // begins, this is not an overlapping, so we want to remove the ending
588  // interval before adding the starting one: end events have higher
589  // priority than begin events.
590  //
591  // * If we have several begin points at the same position, we will mark as
592  // inapplicable the ones that we process later, so the first one has to
593  // be the one with the latest end point, because this one will contain
594  // all the other intervals. For the same reason, if we have several end
595  // points in the same position, the last one has to be the one with the
596  // earliest begin point. In both cases, we sort non-increasingly by the
597  // position of the complementary.
598  //
599  // * In case of two equal intervals, the one whose error is bigger can
600  // potentially contain the other one, so we want to process its begin
601  // points before and its end points later.
602  //
603  // * Finally, if we have two equal intervals whose errors have the same
604  // size, none of them will be strictly contained inside the other.
605  // Sorting by ErrorId will guarantee that the begin point of the first
606  // one will be processed before, disallowing the second one, and the
607  // end point of the first one will also be processed before,
608  // disallowing the first one.
609  switch (Type) {
610  case ET_Begin:
611  Priority = std::make_tuple(Begin, Type, -End, -ErrorSize, ErrorId);
612  break;
613  case ET_Insert:
614  Priority = std::make_tuple(Begin, Type, -End, ErrorSize, ErrorId);
615  break;
616  case ET_End:
617  Priority = std::make_tuple(End, Type, -Begin, ErrorSize, ErrorId);
618  break;
619  }
620  }
621 
622  bool operator<(const Event &Other) const {
623  return Priority < Other.Priority;
624  }
625 
626  // Determines if this event is the begin or the end of an interval.
627  EventType Type;
628  // The index of the error to which the interval that generated this event
629  // belongs.
630  unsigned ErrorId;
631  // The events will be sorted based on this field.
632  std::tuple<unsigned, EventType, int, int, unsigned> Priority;
633  };
634 
635  removeDuplicatedDiagnosticsOfAliasCheckers();
636 
637  // Compute error sizes.
638  std::vector<int> Sizes;
639  std::vector<
640  std::pair<ClangTidyError *, llvm::StringMap<tooling::Replacements> *>>
641  ErrorFixes;
642  for (auto &Error : Errors) {
643  if (const auto *Fix = getFixIt(Error, GetFixesFromNotes))
644  ErrorFixes.emplace_back(
645  &Error, const_cast<llvm::StringMap<tooling::Replacements> *>(Fix));
646  }
647  for (const auto &ErrorAndFix : ErrorFixes) {
648  int Size = 0;
649  for (const auto &FileAndReplaces : *ErrorAndFix.second) {
650  for (const auto &Replace : FileAndReplaces.second)
651  Size += Replace.getLength();
652  }
653  Sizes.push_back(Size);
654  }
655 
656  // Build events from error intervals.
657  llvm::StringMap<std::vector<Event>> FileEvents;
658  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
659  for (const auto &FileAndReplace : *ErrorFixes[I].second) {
660  for (const auto &Replace : FileAndReplace.second) {
661  unsigned Begin = Replace.getOffset();
662  unsigned End = Begin + Replace.getLength();
663  auto &Events = FileEvents[Replace.getFilePath()];
664  if (Begin == End) {
665  Events.emplace_back(Begin, End, Event::ET_Insert, I, Sizes[I]);
666  } else {
667  Events.emplace_back(Begin, End, Event::ET_Begin, I, Sizes[I]);
668  Events.emplace_back(Begin, End, Event::ET_End, I, Sizes[I]);
669  }
670  }
671  }
672  }
673 
674  llvm::BitVector Apply(ErrorFixes.size(), true);
675  for (auto &FileAndEvents : FileEvents) {
676  std::vector<Event> &Events = FileAndEvents.second;
677  // Sweep.
678  llvm::sort(Events);
679  int OpenIntervals = 0;
680  for (const auto &Event : Events) {
681  switch (Event.Type) {
682  case Event::ET_Begin:
683  if (OpenIntervals++ != 0)
684  Apply[Event.ErrorId] = false;
685  break;
686  case Event::ET_Insert:
687  if (OpenIntervals != 0)
688  Apply[Event.ErrorId] = false;
689  break;
690  case Event::ET_End:
691  if (--OpenIntervals != 0)
692  Apply[Event.ErrorId] = false;
693  break;
694  }
695  }
696  assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
697  }
698 
699  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
700  if (!Apply[I]) {
701  ErrorFixes[I].second->clear();
702  ErrorFixes[I].first->Notes.emplace_back(
703  "this fix will not be applied because it overlaps with another fix");
704  }
705  }
706 }
707 
708 namespace {
709 struct LessClangTidyError {
710  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
711  const tooling::DiagnosticMessage &M1 = LHS.Message;
712  const tooling::DiagnosticMessage &M2 = RHS.Message;
713 
714  return std::tie(M1.FilePath, M1.FileOffset, LHS.DiagnosticName,
715  M1.Message) <
716  std::tie(M2.FilePath, M2.FileOffset, RHS.DiagnosticName, M2.Message);
717  }
718 };
719 struct EqualClangTidyError {
720  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
721  LessClangTidyError Less;
722  return !Less(LHS, RHS) && !Less(RHS, LHS);
723  }
724 };
725 } // end anonymous namespace
726 
727 std::vector<ClangTidyError> ClangTidyDiagnosticConsumer::take() {
728  finalizeLastError();
729 
730  llvm::stable_sort(Errors, LessClangTidyError());
731  Errors.erase(std::unique(Errors.begin(), Errors.end(), EqualClangTidyError()),
732  Errors.end());
733  if (RemoveIncompatibleErrors)
734  removeIncompatibleErrors();
735  return std::move(Errors);
736 }
737 
738 namespace {
739 struct LessClangTidyErrorWithoutDiagnosticName {
740  bool operator()(const ClangTidyError *LHS, const ClangTidyError *RHS) const {
741  const tooling::DiagnosticMessage &M1 = LHS->Message;
742  const tooling::DiagnosticMessage &M2 = RHS->Message;
743 
744  return std::tie(M1.FilePath, M1.FileOffset, M1.Message) <
745  std::tie(M2.FilePath, M2.FileOffset, M2.Message);
746  }
747 };
748 } // end anonymous namespace
749 
750 void ClangTidyDiagnosticConsumer::removeDuplicatedDiagnosticsOfAliasCheckers() {
751  using UniqueErrorSet =
752  std::set<ClangTidyError *, LessClangTidyErrorWithoutDiagnosticName>;
753  UniqueErrorSet UniqueErrors;
754 
755  auto IT = Errors.begin();
756  while (IT != Errors.end()) {
757  ClangTidyError &Error = *IT;
758  std::pair<UniqueErrorSet::iterator, bool> Inserted =
759  UniqueErrors.insert(&Error);
760 
761  // Unique error, we keep it and move along.
762  if (Inserted.second) {
763  ++IT;
764  } else {
765  ClangTidyError &ExistingError = **Inserted.first;
766  const llvm::StringMap<tooling::Replacements> &CandidateFix =
767  Error.Message.Fix;
768  const llvm::StringMap<tooling::Replacements> &ExistingFix =
769  (*Inserted.first)->Message.Fix;
770 
771  if (CandidateFix != ExistingFix) {
772 
773  // In case of a conflict, don't suggest any fix-it.
774  ExistingError.Message.Fix.clear();
775  ExistingError.Notes.emplace_back(
776  llvm::formatv("cannot apply fix-it because an alias checker has "
777  "suggested a different fix-it; please remove one of "
778  "the checkers ('{0}', '{1}') or "
779  "ensure they are both configured the same",
780  ExistingError.DiagnosticName, Error.DiagnosticName)
781  .str());
782  }
783 
784  if (Error.IsWarningAsError)
785  ExistingError.IsWarningAsError = true;
786 
787  // Since it is the same error, we should take it as alias and remove it.
788  ExistingError.EnabledDiagnosticAliases.emplace_back(Error.DiagnosticName);
789  IT = Errors.erase(IT);
790  }
791  }
792 }
clang::tidy::ClangTidyProfiling::StorageParams
Definition: ClangTidyProfiling.h:27
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:39
ClangTidyDiagnosticConsumer.h
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
Hints
std::vector< FixItHint > Hints
Definition: RedundantStrcatCallsCheck.cpp:46
Apply
std::vector< llvm::unique_function< void(const Params &, Config &) const > > Apply
Definition: ConfigCompile.cpp:78
clang::tidy::ClangTidyContext::setASTContext
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:232
clang::tidy::ClangTidyDiagnosticConsumer::take
std::vector< ClangTidyError > take()
Definition: ClangTidyDiagnosticConsumer.cpp:727
Type
NodeType Type
Definition: HTMLGenerator.cpp:74
clang::tidy::ClangTidyStats::ErrorsIgnoredCheckFilter
unsigned ErrorsIgnoredCheckFilter
Definition: ClangTidyDiagnosticConsumer.h:47
clang::tidy::ClangTidyContext::getOptionsForFile
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
Definition: ClangTidyDiagnosticConsumer.cpp:245
Location
Definition: Modularize.cpp:381
Checks
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))
clang::tidy::ClangTidyContext::getCheckName
std::string getCheckName(unsigned DiagnosticID) const
Returns the name of the clang-tidy check which produced this diagnostic ID.
Definition: ClangTidyDiagnosticConsumer.cpp:276
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
Kind
BindArgumentKind Kind
Definition: AvoidBindCheck.cpp:59
Ctx
Context Ctx
Definition: TUScheduler.cpp:553
clang::tidy::FileFilter
Contains a list of line ranges in a single file.
Definition: ClangTidyOptions.h:29
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:101
Fix
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))
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
FixIt
llvm::Optional< FixItHint > FixIt
Definition: UppercaseLiteralSuffixCheck.cpp:63
clang::tidy::ClangTidyContext::setSourceManager
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
Definition: ClangTidyDiagnosticConsumer.cpp:220
clang::tidy::NoLintDirectiveHandler::shouldSuppress
bool shouldSuppress(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Diag, llvm::StringRef DiagName, llvm::SmallVectorImpl< tooling::Diagnostic > &NoLintErrors, bool AllowIO, bool EnableNoLintBlocks)
Definition: NoLintDirectiveHandler.cpp:406
Error
constexpr static llvm::SourceMgr::DiagKind Error
Definition: ConfigCompile.cpp:591
clang::tidy::ClangTidyOptions::HeaderFilterRegex
llvm::Optional< std::string > HeaderFilterRegex
Output warnings from headers matching this filter.
Definition: ClangTidyOptions.h:77
clang::tidy::ClangTidyOptions::SystemHeaders
llvm::Optional< bool > SystemHeaders
Output warnings from system headers matching HeaderFilterRegex.
Definition: ClangTidyOptions.h:80
AllowEnablingAnalyzerAlphaCheckers
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.
clang::tidy::ClangTidyError
A detected error complete with information to display diagnostic and automatic fix.
Definition: ClangTidyDiagnosticConsumer.h:36
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:100
clang::tidy::ClangTidyGlobalOptions::LineFilter
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
Definition: ClangTidyOptions.h:45
clang::tidy::ClangTidyStats::ErrorsIgnoredNOLINT
unsigned ErrorsIgnoredNOLINT
Definition: ClangTidyDiagnosticConsumer.h:48
clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Definition: ClangTidyDiagnosticConsumer.cpp:345
clang::tidy::ClangTidyError::IsWarningAsError
bool IsWarningAsError
Definition: ClangTidyDiagnosticConsumer.h:40
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::tidy::ClangTidyContext::setCurrentFile
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:224
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
Description
const char * Description
Definition: Dexp.cpp:361
clang::tidy::ClangTidyContext::getGlobalOptions
const ClangTidyGlobalOptions & getGlobalOptions() const
Returns global options.
Definition: ClangTidyDiagnosticConsumer.cpp:237
clang::tidy::ClangTidyContext::getLangOpts
const LangOptions & getLangOpts() const
Gets the language options from the AST context.
Definition: ClangTidyDiagnosticConsumer.h:135
clang::tidy::ClangTidyContext::setEnableProfiling
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
Definition: ClangTidyDiagnosticConsumer.cpp:252
GlobList.h
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::tidy::ClangTidyStats::ErrorsIgnoredLineFilter
unsigned ErrorsIgnoredLineFilter
Definition: ClangTidyDiagnosticConsumer.h:50
clang::tidy::ClangTidyStats::ErrorsIgnoredNonUserCode
unsigned ErrorsIgnoredNonUserCode
Definition: ClangTidyDiagnosticConsumer.h:49
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:42
clang::tidy::ClangTidyContext::getOptions
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:241
clang::tidy::ClangTidyStats::ErrorsDisplayed
unsigned ErrorsDisplayed
Definition: ClangTidyDiagnosticConsumer.h:46
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::tidy::modernize::operator<
static bool operator<(LiteralSize LHS, LiteralSize RHS)
Definition: IntegralLiteralExpressionMatcher.cpp:125
clang::tidy::ClangTidyContext::~ClangTidyContext
~ClangTidyContext()
clang::tidy::ClangTidyContext::isCheckEnabled
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:266
WarningsAsErrors
static cl::opt< std::string > WarningsAsErrors("warnings-as-errors", cl::desc(R"( Upgrades warnings to errors. Same format as '-checks'. This option's value is appended to the value of the 'WarningsAsErrors' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
clang::tidy::ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine=nullptr, bool RemoveIncompatibleErrors=true, bool GetFixesFromNotes=false, bool EnableNolintBlocks=true)
Definition: ClangTidyDiagnosticConsumer.cpp:288
clang::tidy::ClangTidyContext::getDiagLevelAndFormatString
DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID, SourceLocation Loc)
Definition: ClangTidyDiagnosticConsumer.h:196
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::ClangTidyContext::ClangTidyContext
ClangTidyContext(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider, bool AllowEnablingAnalyzerAlphaCheckers=false)
Initializes ClangTidyContext instance.
Definition: ClangTidyDiagnosticConsumer.cpp:160
clang::tidy::ClangTidyContext::shouldSuppressDiagnostic
bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info, SmallVectorImpl< tooling::Diagnostic > &NoLintErrors, bool AllowIO=true, bool EnableNoLintBlocks=true)
Check whether a given diagnostic should be suppressed due to the presence of a "NOLINT" suppression c...
Definition: ClangTidyDiagnosticConsumer.cpp:211
if
if(CLANG_PLUGIN_SUPPORT) export_executable_symbols_for_plugins(clang-tidy) endif() install(PROGRAMS clang-tidy-diff.py DESTINATION "$
Definition: clang-tidy/tool/CMakeLists.txt:59
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext::getCurrentBuildDirectory
const std::string & getCurrentBuildDirectory() const
Returns build directory of the current translation unit.
Definition: ClangTidyDiagnosticConsumer.h:181
clang::tidy::FileFilter::LineRange
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
Definition: ClangTidyOptions.h:34
LangOpts
const LangOptions * LangOpts
Definition: ExtractFunction.cpp:375
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:55
clang::tidy::getFixIt
const llvm::StringMap< tooling::Replacements > * getFixIt(const tooling::Diagnostic &Diagnostic, bool GetFixFromNotes)
Gets the Fix attached to Diagnostic.
Definition: ClangTidyDiagnosticConsumer.cpp:325
clang::tidy::ClangTidyContext::diag
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
Definition: ClangTidyDiagnosticConsumer.cpp:174
ClangTidyOptions.h
clang::clangd::CompletionItemKind::Event
@ Event
Warning
constexpr static llvm::SourceMgr::DiagKind Warning
Definition: ConfigCompile.cpp:592
clang::tidy::ClangTidyContext::getProfileStorageParams
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
Definition: ClangTidyDiagnosticConsumer.cpp:259
clang::tidy::modernize::isValid
static bool isValid(SourceRange Range)
Definition: MacroToEnumCheck.cpp:534
clang::tidy::ClangTidyError::ClangTidyError
ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory, bool IsWarningAsError)
Definition: ClangTidyDiagnosticConsumer.cpp:154
clang::tidy::ClangTidyContext::treatAsError
bool treatAsError(StringRef CheckName) const
Returns true if the check should be upgraded to error for the CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:271
clang::tidy::ClangTidyOptions::merge
ClangTidyOptions merge(const ClangTidyOptions &Other, unsigned Order) const
Creates a new ClangTidyOptions instance combined from all fields of this instance overridden by the f...
Definition: ClangTidyOptions.cpp:200
clang::tidy::ClangTidyOptions::getDefaults
static ClangTidyOptions getDefaults()
These options are used for all settings that haven't been overridden by the OptionsProvider.
Definition: ClangTidyOptions.cpp:143
clang::clangd::MessageType::Error
@ Error
An error message.
clang::tidy::ClangTidyError::EnabledDiagnosticAliases
std::vector< std::string > EnabledDiagnosticAliases
Definition: ClangTidyDiagnosticConsumer.h:41
clang::tidy::ClangTidyContext::configurationDiag
DiagnosticBuilder configurationDiag(StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors to do with reading the configuration using this method.
Definition: ClangTidyDiagnosticConsumer.cpp:205
clang::tidy::ClangTidyContext::setProfileStoragePrefix
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
Definition: ClangTidyDiagnosticConsumer.cpp:254
NoLintDirectiveHandler.h