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