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