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