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

source code of clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp