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