ClangTidyDiagnosticConsumer.cpp 30 KB

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