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