clang-tools  14.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/Lex/Lexer.h"
30 #include "clang/Tooling/Core/Diagnostic.h"
31 #include "clang/Tooling/Core/Replacement.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/ADT/StringMap.h"
35 #include "llvm/Support/FormatVariadic.h"
36 #include "llvm/Support/Regex.h"
37 #include <tuple>
38 #include <utility>
39 #include <vector>
40 using namespace clang;
41 using namespace tidy;
42 
43 namespace {
44 class ClangTidyDiagnosticRenderer : public DiagnosticRenderer {
45 public:
46  ClangTidyDiagnosticRenderer(const LangOptions &LangOpts,
47  DiagnosticOptions *DiagOpts,
48  ClangTidyError &Error)
49  : DiagnosticRenderer(LangOpts, DiagOpts), Error(Error) {}
50 
51 protected:
52  void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
53  DiagnosticsEngine::Level Level, StringRef Message,
54  ArrayRef<CharSourceRange> Ranges,
55  DiagOrStoredDiag Info) override {
56  // Remove check name from the message.
57  // FIXME: Remove this once there's a better way to pass check names than
58  // appending the check name to the message in ClangTidyContext::diag and
59  // using getCustomDiagID.
60  std::string CheckNameInMessage = " [" + Error.DiagnosticName + "]";
61  if (Message.endswith(CheckNameInMessage))
62  Message = Message.substr(0, Message.size() - CheckNameInMessage.size());
63 
64  auto TidyMessage =
65  Loc.isValid()
66  ? tooling::DiagnosticMessage(Message, Loc.getManager(), Loc)
67  : tooling::DiagnosticMessage(Message);
68 
69  // Make sure that if a TokenRange is receieved from the check it is unfurled
70  // into a real CharRange for the diagnostic printer later.
71  // Whatever we store here gets decoupled from the current SourceManager, so
72  // we **have to** know the exact position and length of the highlight.
73  auto ToCharRange = [this, &Loc](const CharSourceRange &SourceRange) {
74  if (SourceRange.isCharRange())
75  return SourceRange;
76  assert(SourceRange.isTokenRange());
77  SourceLocation End = Lexer::getLocForEndOfToken(
78  SourceRange.getEnd(), 0, Loc.getManager(), LangOpts);
79  return CharSourceRange::getCharRange(SourceRange.getBegin(), End);
80  };
81 
82  if (Level == DiagnosticsEngine::Note) {
83  Error.Notes.push_back(TidyMessage);
84  for (const CharSourceRange &SourceRange : Ranges)
85  Error.Notes.back().Ranges.emplace_back(Loc.getManager(),
86  ToCharRange(SourceRange));
87  return;
88  }
89  assert(Error.Message.Message.empty() && "Overwriting a diagnostic message");
90  Error.Message = TidyMessage;
91  for (const CharSourceRange &SourceRange : Ranges)
92  Error.Message.Ranges.emplace_back(Loc.getManager(),
93  ToCharRange(SourceRange));
94  }
95 
96  void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
97  DiagnosticsEngine::Level Level,
98  ArrayRef<CharSourceRange> Ranges) override {}
99 
100  void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
101  SmallVectorImpl<CharSourceRange> &Ranges,
102  ArrayRef<FixItHint> Hints) override {
103  assert(Loc.isValid());
104  tooling::DiagnosticMessage *DiagWithFix =
105  Level == DiagnosticsEngine::Note ? &Error.Notes.back() : &Error.Message;
106 
107  for (const auto &FixIt : Hints) {
108  CharSourceRange Range = FixIt.RemoveRange;
109  assert(Range.getBegin().isValid() && Range.getEnd().isValid() &&
110  "Invalid range in the fix-it hint.");
111  assert(Range.getBegin().isFileID() && Range.getEnd().isFileID() &&
112  "Only file locations supported in fix-it hints.");
113 
114  tooling::Replacement Replacement(Loc.getManager(), Range,
115  FixIt.CodeToInsert);
116  llvm::Error Err =
117  DiagWithFix->Fix[Replacement.getFilePath()].add(Replacement);
118  // FIXME: better error handling (at least, don't let other replacements be
119  // applied).
120  if (Err) {
121  llvm::errs() << "Fix conflicts with existing fix! "
122  << llvm::toString(std::move(Err)) << "\n";
123  assert(false && "Fix conflicts with existing fix!");
124  }
125  }
126  }
127 
128  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override {}
129 
130  void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
131  StringRef ModuleName) override {}
132 
133  void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
134  StringRef ModuleName) override {}
135 
136  void endDiagnostic(DiagOrStoredDiag D,
137  DiagnosticsEngine::Level Level) override {
138  assert(!Error.Message.Message.empty() && "Message has not been set");
139  }
140 
141 private:
143 };
144 } // end anonymous namespace
145 
146 ClangTidyError::ClangTidyError(StringRef CheckName,
147  ClangTidyError::Level DiagLevel,
148  StringRef BuildDirectory, bool IsWarningAsError)
149  : tooling::Diagnostic(CheckName, DiagLevel, BuildDirectory),
150  IsWarningAsError(IsWarningAsError) {}
151 
153 public:
154  CachedGlobList(StringRef Globs) : Globs(Globs) {}
155 
156  bool contains(StringRef S) {
157  switch (auto &Result = Cache[S]) {
158  case Yes:
159  return true;
160  case No:
161  return false;
162  case None:
163  Result = Globs.contains(S) ? Yes : No;
164  return Result == Yes;
165  }
166  llvm_unreachable("invalid enum");
167  }
168 
169 private:
170  GlobList Globs;
171  enum Tristate { None, Yes, No };
172  llvm::StringMap<Tristate> Cache;
173 };
174 
176  std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
178  : DiagEngine(nullptr), OptionsProvider(std::move(OptionsProvider)),
179  Profile(false),
181  // Before the first translation unit we can get errors related to command-line
182  // parsing, use empty string for the file name in this case.
183  setCurrentFile("");
184 }
185 
187 
188 DiagnosticBuilder ClangTidyContext::diag(
189  StringRef CheckName, SourceLocation Loc, StringRef Description,
190  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
191  assert(Loc.isValid());
192  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
193  Level, (Description + " [" + CheckName + "]").str());
194  CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
195  return DiagEngine->Report(Loc, ID);
196 }
197 
198 DiagnosticBuilder ClangTidyContext::diag(
199  StringRef CheckName, StringRef Description,
200  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
201  unsigned ID = DiagEngine->getDiagnosticIDs()->getCustomDiagID(
202  Level, (Description + " [" + CheckName + "]").str());
203  CheckNamesByDiagnosticID.try_emplace(ID, CheckName);
204  return DiagEngine->Report(ID);
205 }
206 
207 DiagnosticBuilder ClangTidyContext::diag(const ClangTidyError &Error) {
208  SourceManager &SM = DiagEngine->getSourceManager();
209  llvm::ErrorOr<const FileEntry *> File =
210  SM.getFileManager().getFile(Error.Message.FilePath);
211  FileID ID = SM.getOrCreateFileID(*File, SrcMgr::C_User);
212  SourceLocation FileStartLoc = SM.getLocForStartOfFile(ID);
213  SourceLocation Loc = FileStartLoc.getLocWithOffset(Error.Message.FileOffset);
214  return diag(Error.DiagnosticName, Loc, Error.Message.Message,
215  static_cast<DiagnosticIDs::Level>(Error.DiagLevel));
216 }
217 
219  StringRef Message,
220  DiagnosticIDs::Level Level /* = DiagnosticIDs::Warning*/) {
221  return diag("clang-tidy-config", Message, Level);
222 }
223 
225  DiagEngine->setSourceManager(SourceMgr);
226 }
227 
228 void ClangTidyContext::setCurrentFile(StringRef File) {
229  CurrentFile = std::string(File);
230  CurrentOptions = getOptionsForFile(CurrentFile);
231  CheckFilter = std::make_unique<CachedGlobList>(*getOptions().Checks);
232  WarningAsErrorFilter =
233  std::make_unique<CachedGlobList>(*getOptions().WarningsAsErrors);
234 }
235 
236 void ClangTidyContext::setASTContext(ASTContext *Context) {
237  DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
238  LangOpts = Context->getLangOpts();
239 }
240 
242  return OptionsProvider->getGlobalOptions();
243 }
244 
246  return CurrentOptions;
247 }
248 
250  // Merge options on top of getDefaults() as a safeguard against options with
251  // unset values.
253  OptionsProvider->getOptions(File), 0);
254 }
255 
256 void ClangTidyContext::setEnableProfiling(bool P) { Profile = P; }
257 
259  ProfilePrefix = std::string(Prefix);
260 }
261 
262 llvm::Optional<ClangTidyProfiling::StorageParams>
264  if (ProfilePrefix.empty())
265  return llvm::None;
266 
267  return ClangTidyProfiling::StorageParams(ProfilePrefix, CurrentFile);
268 }
269 
270 bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const {
271  assert(CheckFilter != nullptr);
272  return CheckFilter->contains(CheckName);
273 }
274 
275 bool ClangTidyContext::treatAsError(StringRef CheckName) const {
276  assert(WarningAsErrorFilter != nullptr);
277  return WarningAsErrorFilter->contains(CheckName);
278 }
279 
280 std::string ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
281  std::string ClangWarningOption = std::string(
282  DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(DiagnosticID));
283  if (!ClangWarningOption.empty())
284  return "clang-diagnostic-" + ClangWarningOption;
285  llvm::DenseMap<unsigned, std::string>::const_iterator I =
286  CheckNamesByDiagnosticID.find(DiagnosticID);
287  if (I != CheckNamesByDiagnosticID.end())
288  return I->second;
289  return "";
290 }
291 
293  ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine,
294  bool RemoveIncompatibleErrors, bool GetFixesFromNotes)
295  : Context(Ctx), ExternalDiagEngine(ExternalDiagEngine),
296  RemoveIncompatibleErrors(RemoveIncompatibleErrors),
297  GetFixesFromNotes(GetFixesFromNotes), LastErrorRelatesToUserCode(false),
298  LastErrorPassesLineFilter(false), LastErrorWasIgnored(false) {}
299 
300 void ClangTidyDiagnosticConsumer::finalizeLastError() {
301  if (!Errors.empty()) {
302  ClangTidyError &Error = Errors.back();
303  if (Error.DiagnosticName == "clang-tidy-config") {
304  // Never ignore these.
305  } else if (!Context.isCheckEnabled(Error.DiagnosticName) &&
306  Error.DiagLevel != ClangTidyError::Error) {
307  ++Context.Stats.ErrorsIgnoredCheckFilter;
308  Errors.pop_back();
309  } else if (!LastErrorRelatesToUserCode) {
310  ++Context.Stats.ErrorsIgnoredNonUserCode;
311  Errors.pop_back();
312  } else if (!LastErrorPassesLineFilter) {
313  ++Context.Stats.ErrorsIgnoredLineFilter;
314  Errors.pop_back();
315  } else {
316  ++Context.Stats.ErrorsDisplayed;
317  }
318  }
319  LastErrorRelatesToUserCode = false;
320  LastErrorPassesLineFilter = false;
321 }
322 
323 static bool isNOLINTFound(StringRef NolintDirectiveText, StringRef CheckName,
324  StringRef Line, size_t *FoundNolintIndex = nullptr,
325  StringRef *FoundNolintChecksStr = nullptr) {
326  if (FoundNolintIndex)
327  *FoundNolintIndex = StringRef::npos;
328  if (FoundNolintChecksStr)
329  *FoundNolintChecksStr = StringRef();
330 
331  size_t NolintIndex = Line.find(NolintDirectiveText);
332  if (NolintIndex == StringRef::npos)
333  return false;
334 
335  size_t BracketIndex = NolintIndex + NolintDirectiveText.size();
336  if (BracketIndex < Line.size() && isalnum(Line[BracketIndex])) {
337  // Reject this search result, otherwise it will cause false positives when
338  // NOLINT is found as a substring of NOLINT(NEXTLINE/BEGIN/END).
339  return false;
340  }
341 
342  // Check if specific checks are specified in brackets.
343  if (BracketIndex < Line.size() && Line[BracketIndex] == '(') {
344  ++BracketIndex;
345  const size_t BracketEndIndex = Line.find(')', BracketIndex);
346  if (BracketEndIndex != StringRef::npos) {
347  StringRef ChecksStr =
348  Line.substr(BracketIndex, BracketEndIndex - BracketIndex);
349  if (FoundNolintChecksStr)
350  *FoundNolintChecksStr = ChecksStr;
351  // Allow specifying a few checks with a glob expression, ignoring
352  // negative globs (which would effectively disable the suppression).
353  GlobList Globs(ChecksStr, /*KeepNegativeGlobs=*/false);
354  if (!Globs.contains(CheckName))
355  return false;
356  }
357  }
358 
359  if (FoundNolintIndex)
360  *FoundNolintIndex = NolintIndex;
361 
362  return true;
363 }
364 
365 static llvm::Optional<StringRef> getBuffer(const SourceManager &SM, FileID File,
366  bool AllowIO) {
367  return AllowIO ? SM.getBufferDataOrNone(File)
368  : SM.getBufferDataIfLoaded(File);
369 }
370 
372  const SourceManager &SM,
373  SourceLocation Loc,
374  bool IsNolintBegin) {
375  ClangTidyError Error("clang-tidy-nolint", ClangTidyError::Error,
376  Context.getCurrentBuildDirectory(), false);
377  StringRef Message =
378  IsNolintBegin
379  ? "unmatched 'NOLINTBEGIN' comment without a subsequent 'NOLINTEND' "
380  "comment"
381  : "unmatched 'NOLINTEND' comment without a previous 'NOLINTBEGIN' "
382  "comment";
383  Error.Message = tooling::DiagnosticMessage(Message, SM, Loc);
384  return Error;
385 }
386 
387 static Optional<ClangTidyError> tallyNolintBegins(
388  const ClangTidyContext &Context, const SourceManager &SM,
389  StringRef CheckName, SmallVector<StringRef> Lines, SourceLocation LinesLoc,
390  SmallVector<std::pair<SourceLocation, StringRef>> &NolintBegins) {
391  // Keep a running total of how many NOLINT(BEGIN...END) blocks are active, as
392  // well as the bracket expression (if any) that was used in the NOLINT
393  // expression.
394  size_t NolintIndex;
395  StringRef NolintChecksStr;
396  for (const auto &Line : Lines) {
397  if (isNOLINTFound("NOLINTBEGIN", CheckName, Line, &NolintIndex,
398  &NolintChecksStr)) {
399  // Check if a new block is being started.
400  NolintBegins.emplace_back(std::make_pair(
401  LinesLoc.getLocWithOffset(NolintIndex), NolintChecksStr));
402  } else if (isNOLINTFound("NOLINTEND", CheckName, Line, &NolintIndex,
403  &NolintChecksStr)) {
404  // Check if the previous block is being closed.
405  if (!NolintBegins.empty() &&
406  NolintBegins.back().second == NolintChecksStr) {
407  NolintBegins.pop_back();
408  } else {
409  // Trying to close a nonexistent block. Return a diagnostic about this
410  // misuse that can be displayed along with the original clang-tidy check
411  // that the user was attempting to suppress.
412  return createNolintError(Context, SM,
413  LinesLoc.getLocWithOffset(NolintIndex), false);
414  }
415  }
416  // Advance source location to the next line.
417  LinesLoc = LinesLoc.getLocWithOffset(Line.size() + sizeof('\n'));
418  }
419  return None; // All NOLINT(BEGIN/END) use has been consistent so far.
420 }
421 
422 static bool
424  SmallVectorImpl<ClangTidyError> &SuppressionErrors,
425  const SourceManager &SM, SourceLocation Loc,
426  StringRef CheckName, StringRef TextBeforeDiag,
427  StringRef TextAfterDiag) {
428  Loc = SM.getExpansionRange(Loc).getBegin();
429  SourceLocation FileStartLoc = SM.getLocForStartOfFile(SM.getFileID(Loc));
430  SmallVector<std::pair<SourceLocation, StringRef>> NolintBegins;
431 
432  // Check if there's an open NOLINT(BEGIN...END) block on the previous lines.
433  SmallVector<StringRef> PrevLines;
434  TextBeforeDiag.split(PrevLines, '\n');
435  auto Error = tallyNolintBegins(Context, SM, CheckName, PrevLines,
436  FileStartLoc, NolintBegins);
437  if (Error) {
438  SuppressionErrors.emplace_back(Error.getValue());
439  }
440  bool WithinNolintBegin = !NolintBegins.empty();
441 
442  // Check that every block is terminated correctly on the following lines.
443  SmallVector<StringRef> FollowingLines;
444  TextAfterDiag.split(FollowingLines, '\n');
445  Error = tallyNolintBegins(Context, SM, CheckName, FollowingLines, Loc,
446  NolintBegins);
447  if (Error) {
448  SuppressionErrors.emplace_back(Error.getValue());
449  }
450 
451  // The following blocks were never closed. Return diagnostics for each
452  // instance that can be displayed along with the original clang-tidy check
453  // that the user was attempting to suppress.
454  for (const auto &NolintBegin : NolintBegins) {
455  SuppressionErrors.emplace_back(
456  createNolintError(Context, SM, NolintBegin.first, true));
457  }
458 
459  return WithinNolintBegin && SuppressionErrors.empty();
460 }
461 
462 static bool
464  SmallVectorImpl<ClangTidyError> &SuppressionErrors,
465  bool AllowIO, const SourceManager &SM,
466  SourceLocation Loc, StringRef CheckName) {
467  // Get source code for this location.
468  FileID File;
469  unsigned Offset;
470  std::tie(File, Offset) = SM.getDecomposedSpellingLoc(Loc);
471  Optional<StringRef> Buffer = getBuffer(SM, File, AllowIO);
472  if (!Buffer)
473  return false;
474 
475  // Check if there's a NOLINT on this line.
476  StringRef TextAfterDiag = Buffer->substr(Offset);
477  StringRef RestOfThisLine, FollowingLines;
478  std::tie(RestOfThisLine, FollowingLines) = TextAfterDiag.split('\n');
479  if (isNOLINTFound("NOLINT", CheckName, RestOfThisLine))
480  return true;
481 
482  // Check if there's a NOLINTNEXTLINE on the previous line.
483  StringRef TextBeforeDiag = Buffer->substr(0, Offset);
484  size_t LastNewLinePos = TextBeforeDiag.rfind('\n');
485  StringRef PrevLines = (LastNewLinePos == StringRef::npos)
486  ? StringRef()
487  : TextBeforeDiag.slice(0, LastNewLinePos);
488  LastNewLinePos = PrevLines.rfind('\n');
489  StringRef PrevLine = (LastNewLinePos == StringRef::npos)
490  ? PrevLines
491  : PrevLines.substr(LastNewLinePos + 1);
492  if (isNOLINTFound("NOLINTNEXTLINE", CheckName, PrevLine))
493  return true;
494 
495  // Check if this line is within a NOLINT(BEGIN...END) block.
496  return lineIsWithinNolintBegin(Context, SuppressionErrors, SM, Loc, CheckName,
497  TextBeforeDiag, TextAfterDiag);
498 }
499 
501  const Diagnostic &Info, const ClangTidyContext &Context,
502  SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO) {
503  const SourceManager &SM = Info.getSourceManager();
504  SourceLocation Loc = Info.getLocation();
505  std::string CheckName = Context.getCheckName(Info.getID());
506  while (true) {
507  if (lineIsMarkedWithNOLINT(Context, SuppressionErrors, AllowIO, SM, Loc,
508  CheckName))
509  return true;
510  if (!Loc.isMacroID())
511  return false;
512  Loc = SM.getImmediateExpansionRange(Loc).getBegin();
513  }
514  return false;
515 }
516 
517 namespace clang {
518 namespace tidy {
519 
520 bool shouldSuppressDiagnostic(DiagnosticsEngine::Level DiagLevel,
521  const Diagnostic &Info, ClangTidyContext &Context,
522  bool AllowIO) {
523  SmallVector<ClangTidyError, 1> Unused;
524  bool ShouldSuppress =
525  shouldSuppressDiagnostic(DiagLevel, Info, Context, Unused, AllowIO);
526  assert(Unused.empty());
527  return ShouldSuppress;
528 }
529 
531  DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info,
532  ClangTidyContext &Context,
533  SmallVectorImpl<ClangTidyError> &SuppressionErrors, bool AllowIO) {
534  return Info.getLocation().isValid() &&
535  DiagLevel != DiagnosticsEngine::Error &&
536  DiagLevel != DiagnosticsEngine::Fatal &&
537  lineIsMarkedWithNOLINTinMacro(Info, Context, SuppressionErrors,
538  AllowIO);
539 }
540 
541 const llvm::StringMap<tooling::Replacements> *
542 getFixIt(const tooling::Diagnostic &Diagnostic, bool GetFixFromNotes) {
543  if (!Diagnostic.Message.Fix.empty())
544  return &Diagnostic.Message.Fix;
545  if (!GetFixFromNotes)
546  return nullptr;
547  const llvm::StringMap<tooling::Replacements> *Result = nullptr;
548  for (const auto &Note : Diagnostic.Notes) {
549  if (!Note.Fix.empty()) {
550  if (Result)
551  // We have 2 different fixes in notes, bail out.
552  return nullptr;
553  Result = &Note.Fix;
554  }
555  }
556  return Result;
557 }
558 
559 } // namespace tidy
560 } // namespace clang
561 
563  DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
564  if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
565  return;
566 
567  SmallVector<ClangTidyError, 1> SuppressionErrors;
568  if (shouldSuppressDiagnostic(DiagLevel, Info, Context, SuppressionErrors)) {
569  ++Context.Stats.ErrorsIgnoredNOLINT;
570  // Ignored a warning, should ignore related notes as well
571  LastErrorWasIgnored = true;
572  return;
573  }
574 
575  LastErrorWasIgnored = false;
576  // Count warnings/errors.
577  DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info);
578 
579  if (DiagLevel == DiagnosticsEngine::Note) {
580  assert(!Errors.empty() &&
581  "A diagnostic note can only be appended to a message.");
582  } else {
583  finalizeLastError();
584  std::string CheckName = Context.getCheckName(Info.getID());
585  if (CheckName.empty()) {
586  // This is a compiler diagnostic without a warning option. Assign check
587  // name based on its level.
588  switch (DiagLevel) {
590  case DiagnosticsEngine::Fatal:
591  CheckName = "clang-diagnostic-error";
592  break;
594  CheckName = "clang-diagnostic-warning";
595  break;
596  case DiagnosticsEngine::Remark:
597  CheckName = "clang-diagnostic-remark";
598  break;
599  default:
600  CheckName = "clang-diagnostic-unknown";
601  break;
602  }
603  }
604 
605  ClangTidyError::Level Level = ClangTidyError::Warning;
606  if (DiagLevel == DiagnosticsEngine::Error ||
607  DiagLevel == DiagnosticsEngine::Fatal) {
608  // Force reporting of Clang errors regardless of filters and non-user
609  // code.
610  Level = ClangTidyError::Error;
611  LastErrorRelatesToUserCode = true;
612  LastErrorPassesLineFilter = true;
613  } else if (DiagLevel == DiagnosticsEngine::Remark) {
614  Level = ClangTidyError::Remark;
615  }
616 
617  bool IsWarningAsError = DiagLevel == DiagnosticsEngine::Warning &&
618  Context.treatAsError(CheckName);
619  Errors.emplace_back(CheckName, Level, Context.getCurrentBuildDirectory(),
620  IsWarningAsError);
621  }
622 
623  if (ExternalDiagEngine) {
624  // If there is an external diagnostics engine, like in the
625  // ClangTidyPluginAction case, forward the diagnostics to it.
626  forwardDiagnostic(Info);
627  } else {
628  ClangTidyDiagnosticRenderer Converter(
629  Context.getLangOpts(), &Context.DiagEngine->getDiagnosticOptions(),
630  Errors.back());
631  SmallString<100> Message;
632  Info.FormatDiagnostic(Message);
633  FullSourceLoc Loc;
634  if (Info.getLocation().isValid() && Info.hasSourceManager())
635  Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
636  Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
637  Info.getFixItHints());
638  }
639 
640  if (Info.hasSourceManager())
641  checkFilters(Info.getLocation(), Info.getSourceManager());
642 
643  Context.DiagEngine->Clear();
644  for (const auto &Error : SuppressionErrors)
645  Context.diag(Error);
646 }
647 
648 bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
649  unsigned LineNumber) const {
650  if (Context.getGlobalOptions().LineFilter.empty())
651  return true;
652  for (const FileFilter &Filter : Context.getGlobalOptions().LineFilter) {
653  if (FileName.endswith(Filter.Name)) {
654  if (Filter.LineRanges.empty())
655  return true;
656  for (const FileFilter::LineRange &Range : Filter.LineRanges) {
657  if (Range.first <= LineNumber && LineNumber <= Range.second)
658  return true;
659  }
660  return false;
661  }
662  }
663  return false;
664 }
665 
666 void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
667  // Acquire a diagnostic ID also in the external diagnostics engine.
668  auto DiagLevelAndFormatString =
669  Context.getDiagLevelAndFormatString(Info.getID(), Info.getLocation());
670  unsigned ExternalID = ExternalDiagEngine->getDiagnosticIDs()->getCustomDiagID(
671  DiagLevelAndFormatString.first, DiagLevelAndFormatString.second);
672 
673  // Forward the details.
674  auto Builder = ExternalDiagEngine->Report(Info.getLocation(), ExternalID);
675  for (auto Hint : Info.getFixItHints())
676  Builder << Hint;
677  for (auto Range : Info.getRanges())
678  Builder << Range;
679  for (unsigned Index = 0; Index < Info.getNumArgs(); ++Index) {
680  DiagnosticsEngine::ArgumentKind Kind = Info.getArgKind(Index);
681  switch (Kind) {
682  case clang::DiagnosticsEngine::ak_std_string:
683  Builder << Info.getArgStdStr(Index);
684  break;
685  case clang::DiagnosticsEngine::ak_c_string:
686  Builder << Info.getArgCStr(Index);
687  break;
688  case clang::DiagnosticsEngine::ak_sint:
689  Builder << Info.getArgSInt(Index);
690  break;
691  case clang::DiagnosticsEngine::ak_uint:
692  Builder << Info.getArgUInt(Index);
693  break;
694  case clang::DiagnosticsEngine::ak_tokenkind:
695  Builder << static_cast<tok::TokenKind>(Info.getRawArg(Index));
696  break;
697  case clang::DiagnosticsEngine::ak_identifierinfo:
698  Builder << Info.getArgIdentifier(Index);
699  break;
700  case clang::DiagnosticsEngine::ak_qual:
701  Builder << Qualifiers::fromOpaqueValue(Info.getRawArg(Index));
702  break;
703  case clang::DiagnosticsEngine::ak_qualtype:
704  Builder << QualType::getFromOpaquePtr((void *)Info.getRawArg(Index));
705  break;
706  case clang::DiagnosticsEngine::ak_declarationname:
707  Builder << DeclarationName::getFromOpaqueInteger(Info.getRawArg(Index));
708  break;
709  case clang::DiagnosticsEngine::ak_nameddecl:
710  Builder << reinterpret_cast<const NamedDecl *>(Info.getRawArg(Index));
711  break;
712  case clang::DiagnosticsEngine::ak_nestednamespec:
713  Builder << reinterpret_cast<NestedNameSpecifier *>(Info.getRawArg(Index));
714  break;
715  case clang::DiagnosticsEngine::ak_declcontext:
716  Builder << reinterpret_cast<DeclContext *>(Info.getRawArg(Index));
717  break;
718  case clang::DiagnosticsEngine::ak_qualtype_pair:
719  assert(false); // This one is not passed around.
720  break;
721  case clang::DiagnosticsEngine::ak_attr:
722  Builder << reinterpret_cast<Attr *>(Info.getRawArg(Index));
723  break;
724  case clang::DiagnosticsEngine::ak_addrspace:
725  Builder << static_cast<LangAS>(Info.getRawArg(Index));
726  break;
727  }
728  }
729 }
730 
731 void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
732  const SourceManager &Sources) {
733  // Invalid location may mean a diagnostic in a command line, don't skip these.
734  if (!Location.isValid()) {
735  LastErrorRelatesToUserCode = true;
736  LastErrorPassesLineFilter = true;
737  return;
738  }
739 
740  if (!*Context.getOptions().SystemHeaders &&
741  Sources.isInSystemHeader(Location))
742  return;
743 
744  // FIXME: We start with a conservative approach here, but the actual type of
745  // location needed depends on the check (in particular, where this check wants
746  // to apply fixes).
747  FileID FID = Sources.getDecomposedExpansionLoc(Location).first;
748  const FileEntry *File = Sources.getFileEntryForID(FID);
749 
750  // -DMACRO definitions on the command line have locations in a virtual buffer
751  // that doesn't have a FileEntry. Don't skip these as well.
752  if (!File) {
753  LastErrorRelatesToUserCode = true;
754  LastErrorPassesLineFilter = true;
755  return;
756  }
757 
758  StringRef FileName(File->getName());
759  LastErrorRelatesToUserCode = LastErrorRelatesToUserCode ||
760  Sources.isInMainFile(Location) ||
761  getHeaderFilter()->match(FileName);
762 
763  unsigned LineNumber = Sources.getExpansionLineNumber(Location);
764  LastErrorPassesLineFilter =
765  LastErrorPassesLineFilter || passesLineFilter(FileName, LineNumber);
766 }
767 
768 llvm::Regex *ClangTidyDiagnosticConsumer::getHeaderFilter() {
769  if (!HeaderFilter)
770  HeaderFilter =
771  std::make_unique<llvm::Regex>(*Context.getOptions().HeaderFilterRegex);
772  return HeaderFilter.get();
773 }
774 
775 void ClangTidyDiagnosticConsumer::removeIncompatibleErrors() {
776  // Each error is modelled as the set of intervals in which it applies
777  // replacements. To detect overlapping replacements, we use a sweep line
778  // algorithm over these sets of intervals.
779  // An event here consists of the opening or closing of an interval. During the
780  // process, we maintain a counter with the amount of open intervals. If we
781  // find an endpoint of an interval and this counter is different from 0, it
782  // means that this interval overlaps with another one, so we set it as
783  // inapplicable.
784  struct Event {
785  // An event can be either the begin or the end of an interval.
786  enum EventType {
787  ET_Begin = 1,
788  ET_Insert = 0,
789  ET_End = -1,
790  };
791 
792  Event(unsigned Begin, unsigned End, EventType Type, unsigned ErrorId,
793  unsigned ErrorSize)
794  : Type(Type), ErrorId(ErrorId) {
795  // The events are going to be sorted by their position. In case of draw:
796  //
797  // * If an interval ends at the same position at which other interval
798  // begins, this is not an overlapping, so we want to remove the ending
799  // interval before adding the starting one: end events have higher
800  // priority than begin events.
801  //
802  // * If we have several begin points at the same position, we will mark as
803  // inapplicable the ones that we process later, so the first one has to
804  // be the one with the latest end point, because this one will contain
805  // all the other intervals. For the same reason, if we have several end
806  // points in the same position, the last one has to be the one with the
807  // earliest begin point. In both cases, we sort non-increasingly by the
808  // position of the complementary.
809  //
810  // * In case of two equal intervals, the one whose error is bigger can
811  // potentially contain the other one, so we want to process its begin
812  // points before and its end points later.
813  //
814  // * Finally, if we have two equal intervals whose errors have the same
815  // size, none of them will be strictly contained inside the other.
816  // Sorting by ErrorId will guarantee that the begin point of the first
817  // one will be processed before, disallowing the second one, and the
818  // end point of the first one will also be processed before,
819  // disallowing the first one.
820  switch (Type) {
821  case ET_Begin:
822  Priority = std::make_tuple(Begin, Type, -End, -ErrorSize, ErrorId);
823  break;
824  case ET_Insert:
825  Priority = std::make_tuple(Begin, Type, -End, ErrorSize, ErrorId);
826  break;
827  case ET_End:
828  Priority = std::make_tuple(End, Type, -Begin, ErrorSize, ErrorId);
829  break;
830  }
831  }
832 
833  bool operator<(const Event &Other) const {
834  return Priority < Other.Priority;
835  }
836 
837  // Determines if this event is the begin or the end of an interval.
838  EventType Type;
839  // The index of the error to which the interval that generated this event
840  // belongs.
841  unsigned ErrorId;
842  // The events will be sorted based on this field.
843  std::tuple<unsigned, EventType, int, int, unsigned> Priority;
844  };
845 
846  removeDuplicatedDiagnosticsOfAliasCheckers();
847 
848  // Compute error sizes.
849  std::vector<int> Sizes;
850  std::vector<
851  std::pair<ClangTidyError *, llvm::StringMap<tooling::Replacements> *>>
852  ErrorFixes;
853  for (auto &Error : Errors) {
854  if (const auto *Fix = getFixIt(Error, GetFixesFromNotes))
855  ErrorFixes.emplace_back(
856  &Error, const_cast<llvm::StringMap<tooling::Replacements> *>(Fix));
857  }
858  for (const auto &ErrorAndFix : ErrorFixes) {
859  int Size = 0;
860  for (const auto &FileAndReplaces : *ErrorAndFix.second) {
861  for (const auto &Replace : FileAndReplaces.second)
862  Size += Replace.getLength();
863  }
864  Sizes.push_back(Size);
865  }
866 
867  // Build events from error intervals.
868  llvm::StringMap<std::vector<Event>> FileEvents;
869  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
870  for (const auto &FileAndReplace : *ErrorFixes[I].second) {
871  for (const auto &Replace : FileAndReplace.second) {
872  unsigned Begin = Replace.getOffset();
873  unsigned End = Begin + Replace.getLength();
874  auto &Events = FileEvents[Replace.getFilePath()];
875  if (Begin == End) {
876  Events.emplace_back(Begin, End, Event::ET_Insert, I, Sizes[I]);
877  } else {
878  Events.emplace_back(Begin, End, Event::ET_Begin, I, Sizes[I]);
879  Events.emplace_back(Begin, End, Event::ET_End, I, Sizes[I]);
880  }
881  }
882  }
883  }
884 
885  std::vector<bool> Apply(ErrorFixes.size(), true);
886  for (auto &FileAndEvents : FileEvents) {
887  std::vector<Event> &Events = FileAndEvents.second;
888  // Sweep.
889  llvm::sort(Events);
890  int OpenIntervals = 0;
891  for (const auto &Event : Events) {
892  switch (Event.Type) {
893  case Event::ET_Begin:
894  if (OpenIntervals++ != 0)
895  Apply[Event.ErrorId] = false;
896  break;
897  case Event::ET_Insert:
898  if (OpenIntervals != 0)
899  Apply[Event.ErrorId] = false;
900  break;
901  case Event::ET_End:
902  if (--OpenIntervals != 0)
903  Apply[Event.ErrorId] = false;
904  break;
905  }
906  }
907  assert(OpenIntervals == 0 && "Amount of begin/end points doesn't match");
908  }
909 
910  for (unsigned I = 0; I < ErrorFixes.size(); ++I) {
911  if (!Apply[I]) {
912  ErrorFixes[I].second->clear();
913  ErrorFixes[I].first->Notes.emplace_back(
914  "this fix will not be applied because it overlaps with another fix");
915  }
916  }
917 }
918 
919 namespace {
920 struct LessClangTidyError {
921  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
922  const tooling::DiagnosticMessage &M1 = LHS.Message;
923  const tooling::DiagnosticMessage &M2 = RHS.Message;
924 
925  return std::tie(M1.FilePath, M1.FileOffset, LHS.DiagnosticName,
926  M1.Message) <
927  std::tie(M2.FilePath, M2.FileOffset, RHS.DiagnosticName, M2.Message);
928  }
929 };
930 struct EqualClangTidyError {
931  bool operator()(const ClangTidyError &LHS, const ClangTidyError &RHS) const {
932  LessClangTidyError Less;
933  return !Less(LHS, RHS) && !Less(RHS, LHS);
934  }
935 };
936 } // end anonymous namespace
937 
938 std::vector<ClangTidyError> ClangTidyDiagnosticConsumer::take() {
939  finalizeLastError();
940 
941  llvm::stable_sort(Errors, LessClangTidyError());
942  Errors.erase(std::unique(Errors.begin(), Errors.end(), EqualClangTidyError()),
943  Errors.end());
944  if (RemoveIncompatibleErrors)
945  removeIncompatibleErrors();
946  return std::move(Errors);
947 }
948 
949 namespace {
950 struct LessClangTidyErrorWithoutDiagnosticName {
951  bool operator()(const ClangTidyError *LHS, const ClangTidyError *RHS) const {
952  const tooling::DiagnosticMessage &M1 = LHS->Message;
953  const tooling::DiagnosticMessage &M2 = RHS->Message;
954 
955  return std::tie(M1.FilePath, M1.FileOffset, M1.Message) <
956  std::tie(M2.FilePath, M2.FileOffset, M2.Message);
957  }
958 };
959 } // end anonymous namespace
960 
961 void ClangTidyDiagnosticConsumer::removeDuplicatedDiagnosticsOfAliasCheckers() {
962  using UniqueErrorSet =
963  std::set<ClangTidyError *, LessClangTidyErrorWithoutDiagnosticName>;
964  UniqueErrorSet UniqueErrors;
965 
966  auto IT = Errors.begin();
967  while (IT != Errors.end()) {
968  ClangTidyError &Error = *IT;
969  std::pair<UniqueErrorSet::iterator, bool> Inserted =
970  UniqueErrors.insert(&Error);
971 
972  // Unique error, we keep it and move along.
973  if (Inserted.second) {
974  ++IT;
975  } else {
976  ClangTidyError &ExistingError = **Inserted.first;
977  const llvm::StringMap<tooling::Replacements> &CandidateFix =
978  Error.Message.Fix;
979  const llvm::StringMap<tooling::Replacements> &ExistingFix =
980  (*Inserted.first)->Message.Fix;
981 
982  if (CandidateFix != ExistingFix) {
983 
984  // In case of a conflict, don't suggest any fix-it.
985  ExistingError.Message.Fix.clear();
986  ExistingError.Notes.emplace_back(
987  llvm::formatv("cannot apply fix-it because an alias checker has "
988  "suggested a different fix-it; please remove one of "
989  "the checkers ('{0}', '{1}') or "
990  "ensure they are both configured the same",
991  ExistingError.DiagnosticName, Error.DiagnosticName)
992  .str());
993  }
994 
995  if (Error.IsWarningAsError)
996  ExistingError.IsWarningAsError = true;
997 
998  // Since it is the same error, we should take it as alias and remove it.
999  ExistingError.EnabledDiagnosticAliases.emplace_back(Error.DiagnosticName);
1000  IT = Errors.erase(IT);
1001  }
1002  }
1003 }
clang::tidy::ClangTidyProfiling::StorageParams
Definition: ClangTidyProfiling.h:27
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
ClangTidyDiagnosticConsumer.h
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx, DiagnosticsEngine *ExternalDiagEngine=nullptr, bool RemoveIncompatibleErrors=true, bool GetFixesFromNotes=false)
Definition: ClangTidyDiagnosticConsumer.cpp:292
Hints
std::vector< FixItHint > Hints
Definition: RedundantStrcatCallsCheck.cpp:46
Apply
std::vector< llvm::unique_function< void(const Params &, Config &) const > > Apply
Definition: ConfigCompile.cpp:79
clang::tidy::ClangTidyContext::setASTContext
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:236
clang::tidy::ClangTidyDiagnosticConsumer::take
std::vector< ClangTidyError > take()
Definition: ClangTidyDiagnosticConsumer.cpp:938
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::tidy::ClangTidyStats::ErrorsIgnoredCheckFilter
unsigned ErrorsIgnoredCheckFilter
Definition: ClangTidyDiagnosticConsumer.h:56
clang::tidy::ClangTidyContext::getOptionsForFile
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
Definition: ClangTidyDiagnosticConsumer.cpp:249
Location
Definition: Modularize.cpp:382
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))
getBuffer
static llvm::Optional< StringRef > getBuffer(const SourceManager &SM, FileID File, bool AllowIO)
Definition: ClangTidyDiagnosticConsumer.cpp:365
clang::tidy::shouldSuppressDiagnostic
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...
Definition: ClangTidyDiagnosticConsumer.cpp:520
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:280
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:460
clang::tidy::FileFilter
Contains a list of line ranges in a single file.
Definition: ClangTidyOptions.h:29
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:102
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:224
Error
constexpr static llvm::SourceMgr::DiagKind Error
Definition: ConfigCompile.cpp:510
Offset
size_t Offset
Definition: CodeComplete.cpp:1147
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:40
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:101
clang::tidy::ClangTidyGlobalOptions::LineFilter
std::vector< FileFilter > LineFilter
Output warnings from certain line ranges of certain files only.
Definition: ClangTidyOptions.h:45
clang::tidy::GlobList
Read-only set of strings represented as a list of positive and negative globs.
Definition: GlobList.h:25
clang::tidy::ClangTidyStats::ErrorsIgnoredNOLINT
unsigned ErrorsIgnoredNOLINT
Definition: ClangTidyDiagnosticConsumer.h:57
clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Definition: ClangTidyDiagnosticConsumer.cpp:562
clang::tidy::ClangTidyError::IsWarningAsError
bool IsWarningAsError
Definition: ClangTidyDiagnosticConsumer.h:44
Line
int Line
Definition: PreprocessorTracker.cpp:514
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::tidy::ClangTidyContext::CachedGlobList::contains
bool contains(StringRef S)
Definition: ClangTidyDiagnosticConsumer.cpp:156
clang::tidy::ClangTidyContext::setCurrentFile
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:228
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
if
if(CLANGD_ENABLE_REMOTE) generate_protos(RemoteIndexProto "Index.proto") generate_protos(MonitoringServiceProto "MonitoringService.proto" GRPC) generate_protos(RemoteIndexServiceProto "Service.proto" DEPENDS "Index.proto" GRPC) target_link_libraries(RemoteIndexServiceProto PRIVATE RemoteIndexProto MonitoringServiceProto) include_directories($
Definition: clangd/index/remote/CMakeLists.txt:1
Description
const char * Description
Definition: Dexp.cpp:361
clang::tidy::ClangTidyContext::getGlobalOptions
const ClangTidyGlobalOptions & getGlobalOptions() const
Returns global options.
Definition: ClangTidyDiagnosticConsumer.cpp:241
clang::tidy::ClangTidyContext::getLangOpts
const LangOptions & getLangOpts() const
Gets the language options from the AST context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::ClangTidyContext::setEnableProfiling
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
Definition: ClangTidyDiagnosticConsumer.cpp:256
lineIsWithinNolintBegin
static bool lineIsWithinNolintBegin(const ClangTidyContext &Context, SmallVectorImpl< ClangTidyError > &SuppressionErrors, const SourceManager &SM, SourceLocation Loc, StringRef CheckName, StringRef TextBeforeDiag, StringRef TextAfterDiag)
Definition: ClangTidyDiagnosticConsumer.cpp:423
GlobList.h
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::tidy::readability::Unused
@ Unused
Definition: MakeMemberFunctionConstCheck.cpp:52
isNOLINTFound
static bool isNOLINTFound(StringRef NolintDirectiveText, StringRef CheckName, StringRef Line, size_t *FoundNolintIndex=nullptr, StringRef *FoundNolintChecksStr=nullptr)
Definition: ClangTidyDiagnosticConsumer.cpp:323
lineIsMarkedWithNOLINT
static bool lineIsMarkedWithNOLINT(const ClangTidyContext &Context, SmallVectorImpl< ClangTidyError > &SuppressionErrors, bool AllowIO, const SourceManager &SM, SourceLocation Loc, StringRef CheckName)
Definition: ClangTidyDiagnosticConsumer.cpp:463
clang::tidy::ClangTidyStats::ErrorsIgnoredLineFilter
unsigned ErrorsIgnoredLineFilter
Definition: ClangTidyDiagnosticConsumer.h:59
clang::tidy::ClangTidyStats::ErrorsIgnoredNonUserCode
unsigned ErrorsIgnoredNonUserCode
Definition: ClangTidyDiagnosticConsumer.h:58
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:42
clang::tidy::ClangTidyContext::getOptions
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:245
clang::tidy::ClangTidyStats::ErrorsDisplayed
unsigned ErrorsDisplayed
Definition: ClangTidyDiagnosticConsumer.h:55
Index
const SymbolIndex * Index
Definition: Dexp.cpp:99
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:270
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::ClangTidyContext::getDiagLevelAndFormatString
DiagLevelAndFormatString getDiagLevelAndFormatString(unsigned DiagnosticID, SourceLocation Loc)
Definition: ClangTidyDiagnosticConsumer.h:181
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::ClangTidyContext::CachedGlobList
Definition: ClangTidyDiagnosticConsumer.cpp:152
clang::tidy::ClangTidyContext::ClangTidyContext
ClangTidyContext(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider, bool AllowEnablingAnalyzerAlphaCheckers=false)
Initializes ClangTidyContext instance.
Definition: ClangTidyDiagnosticConsumer.cpp:175
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:170
clang::tidy::FileFilter::LineRange
std::pair< unsigned, unsigned > LineRange
LineRange is a pair<start, end> (inclusive).
Definition: ClangTidyOptions.h:34
clang::clangd::operator<
bool operator<(const Ref &L, const Ref &R)
Definition: Ref.h:97
clang::tidy::cppcoreguidelines::toString
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Definition: SpecialMemberFunctionsCheck.cpp:55
createNolintError
static ClangTidyError createNolintError(const ClangTidyContext &Context, const SourceManager &SM, SourceLocation Loc, bool IsNolintBegin)
Definition: ClangTidyDiagnosticConsumer.cpp:371
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:127
lineIsMarkedWithNOLINTinMacro
static bool lineIsMarkedWithNOLINTinMacro(const Diagnostic &Info, const ClangTidyContext &Context, SmallVectorImpl< ClangTidyError > &SuppressionErrors, bool AllowIO)
Definition: ClangTidyDiagnosticConsumer.cpp:500
clang::tidy::getFixIt
const llvm::StringMap< tooling::Replacements > * getFixIt(const tooling::Diagnostic &Diagnostic, bool GetFixFromNotes)
Gets the Fix attached to Diagnostic.
Definition: ClangTidyDiagnosticConsumer.cpp:542
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:188
ClangTidyOptions.h
clang::clangd::CompletionItemKind::Event
@ Event
clang::tidy::ClangTidyContext::CachedGlobList::CachedGlobList
CachedGlobList(StringRef Globs)
Definition: ClangTidyDiagnosticConsumer.cpp:154
Warning
constexpr static llvm::SourceMgr::DiagKind Warning
Definition: ConfigCompile.cpp:511
clang::tidy::ClangTidyContext::getProfileStorageParams
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
Definition: ClangTidyDiagnosticConsumer.cpp:263
Lines
unsigned Lines
Definition: FunctionSizeCheck.cpp:113
clang::tidy::ClangTidyError::ClangTidyError
ClangTidyError(StringRef CheckName, Level DiagLevel, StringRef BuildDirectory, bool IsWarningAsError)
Definition: ClangTidyDiagnosticConsumer.cpp:146
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:275
clang::tidy::ClangTidyOptions::merge
LLVM_NODISCARD 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:166
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:109
clang::clangd::MessageType::Error
@ Error
An error message.
clang::tidy::ClangTidyError::EnabledDiagnosticAliases
std::vector< std::string > EnabledDiagnosticAliases
Definition: ClangTidyDiagnosticConsumer.h:45
tallyNolintBegins
static Optional< ClangTidyError > tallyNolintBegins(const ClangTidyContext &Context, const SourceManager &SM, StringRef CheckName, SmallVector< StringRef > Lines, SourceLocation LinesLoc, SmallVector< std::pair< SourceLocation, StringRef >> &NolintBegins)
Definition: ClangTidyDiagnosticConsumer.cpp:387
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:218
clang::tidy::ClangTidyContext::setProfileStoragePrefix
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
Definition: ClangTidyDiagnosticConsumer.cpp:258
clang::tidy::GlobList::contains
bool contains(StringRef S) const
Returns true if the pattern matches S.
Definition: GlobList.cpp:56