clang-tools 23.0.0git
ClangTidy.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file This file implements a clang-tidy tool.
10///
11/// This tool uses the Clang Tooling infrastructure, see
12/// https://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
13/// for details on setting it up with LLVM source tree.
14///
15//===----------------------------------------------------------------------===//
16
17#include "ClangTidy.h"
18#include "ClangTidyCheck.h"
20#include "ClangTidyModule.h"
21#include "ClangTidyProfiling.h"
23#include "clang-tidy-config.h"
24#include "clang/AST/ASTConsumer.h"
25#include "clang/ASTMatchers/ASTMatchFinder.h"
26#include "clang/Basic/DiagnosticFrontend.h"
27#include "clang/Format/Format.h"
28#include "clang/Frontend/ASTConsumers.h"
29#include "clang/Frontend/CompilerInstance.h"
30#include "clang/Frontend/MultiplexConsumer.h"
31#include "clang/Frontend/TextDiagnosticPrinter.h"
32#include "clang/Lex/Preprocessor.h"
33#include "clang/Lex/PreprocessorOptions.h"
34#include "clang/Rewrite/Frontend/FixItRewriter.h"
35#include "clang/Tooling/Core/Diagnostic.h"
36#include "clang/Tooling/DiagnosticsYaml.h" // IWYU pragma: keep
37#include "clang/Tooling/Refactoring.h"
38#include "clang/Tooling/Tooling.h"
39#include "llvm/Support/Process.h"
40#include <utility>
41
42#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
43#include "clang/Analysis/PathDiagnostic.h"
44#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
45#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
46
47using namespace clang::ast_matchers;
48using namespace clang::driver;
49using namespace clang::tooling;
50using namespace llvm;
51
52LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
53
54namespace clang::tidy {
55
56#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
57namespace custom {
58void (*RegisterCustomChecks)(const ClangTidyOptions &O,
59 ClangTidyCheckFactories &Factories) = nullptr;
60} // namespace custom
61#endif
62
63namespace {
64#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
65#define ANALYZER_CHECK_NAME_PREFIX "clang-analyzer-"
66static constexpr StringRef AnalyzerCheckNamePrefix = ANALYZER_CHECK_NAME_PREFIX;
67
68class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
69public:
70 AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
71
72 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
73 FilesMade *FilesMade) override {
74 for (const ento::PathDiagnostic *PD : Diags) {
75 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
76 CheckName += PD->getCheckerName();
77 Context.diag(CheckName, PD->getLocation().asLocation(),
78 PD->getShortDescription())
79 << PD->path.back()->getRanges();
80
81 for (const auto &DiagPiece :
82 PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
83 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
84 DiagPiece->getString(), DiagnosticIDs::Note)
85 << DiagPiece->getRanges();
86 }
87 }
88 }
89
90 StringRef getName() const override { return "ClangTidyDiags"; }
91 bool supportsLogicalOpControlFlow() const override { return true; }
92 bool supportsCrossFileDiagnostics() const override { return true; }
93
94private:
95 ClangTidyContext &Context;
96};
97#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
98
99class ErrorReporter {
100public:
101 ErrorReporter(ClangTidyContext &Context, FixBehaviour ApplyFixes,
102 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS)
103 : Files(FileSystemOptions(), std::move(BaseFS)),
104 DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), DiagOpts)),
105 Diags(DiagnosticIDs::create(), DiagOpts, DiagPrinter),
106 SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes) {
107 DiagOpts.ShowColors = Context.getOptions().UseColor.value_or(
108 llvm::sys::Process::StandardOutHasColors());
109 DiagPrinter->BeginSourceFile(LangOpts);
110 if (DiagOpts.ShowColors && !llvm::sys::Process::StandardOutIsDisplayed())
111 llvm::sys::Process::UseANSIEscapeCodes(true);
112 }
113
114 SourceManager &getSourceManager() { return SourceMgr; }
115
116 void reportDiagnostic(const ClangTidyError &Error) {
117 const tooling::DiagnosticMessage &Message = Error.Message;
118 const SourceLocation Loc =
119 getLocation(Message.FilePath, Message.FileOffset);
120 // Contains a pair for each attempted fix: location and whether the fix was
121 // applied successfully.
122 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
123 {
124 auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
125 std::string Name = Error.DiagnosticName;
126 if (!Error.EnabledDiagnosticAliases.empty())
127 Name += "," + llvm::join(Error.EnabledDiagnosticAliases, ",");
128 if (Error.IsWarningAsError) {
129 Name += ",-warnings-as-errors";
130 Level = DiagnosticsEngine::Error;
131 WarningsAsErrors++;
132 }
133 auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
134 << Message.Message << Name;
135 for (const FileByteRange &FBR : Error.Message.Ranges)
136 Diag << getRange(FBR);
137 // FIXME: explore options to support interactive fix selection.
138 const llvm::StringMap<Replacements> *ChosenFix = nullptr;
139 if (ApplyFixes != FB_NoFix &&
140 (ChosenFix = getFixIt(Error, ApplyFixes == FB_FixNotes))) {
141 for (const auto &FileAndReplacements : *ChosenFix) {
142 for (const auto &Repl : FileAndReplacements.second) {
143 ++TotalFixes;
144 bool CanBeApplied = false;
145 if (!Repl.isApplicable())
146 continue;
147 SourceLocation FixLoc;
148 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
149 Files.makeAbsolutePath(FixAbsoluteFilePath);
150 tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
151 Repl.getLength(), Repl.getReplacementText());
152 auto &Entry = FileReplacements[R.getFilePath()];
153 Replacements &Replacements = Entry.Replaces;
154 llvm::Error Err = Replacements.add(R);
155 if (Err) {
156 // FIXME: Implement better conflict handling.
157 llvm::errs() << "Trying to resolve conflict: "
158 << llvm::toString(std::move(Err)) << "\n";
159 const unsigned NewOffset =
160 Replacements.getShiftedCodePosition(R.getOffset());
161 const unsigned NewLength = Replacements.getShiftedCodePosition(
162 R.getOffset() + R.getLength()) -
163 NewOffset;
164 if (NewLength == R.getLength()) {
165 R = Replacement(R.getFilePath(), NewOffset, NewLength,
166 R.getReplacementText());
167 Replacements = Replacements.merge(tooling::Replacements(R));
168 CanBeApplied = true;
169 ++AppliedFixes;
170 } else {
171 llvm::errs()
172 << "Can't resolve conflict, skipping the replacement.\n";
173 }
174 } else {
175 CanBeApplied = true;
176 ++AppliedFixes;
177 }
178 FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
179 FixLocations.emplace_back(FixLoc, CanBeApplied);
180 Entry.BuildDir = Error.BuildDirectory;
181 }
182 }
183 }
184 reportFix(Diag, Error.Message.Fix);
185 }
186 for (auto Fix : FixLocations) {
187 Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
188 : diag::note_fixit_failed);
189 }
190 for (const auto &Note : Error.Notes)
191 reportNote(Note);
192 }
193
194 void finish() {
195 if (TotalFixes > 0) {
196 auto &VFS = Files.getVirtualFileSystem();
197 auto OriginalCWD = VFS.getCurrentWorkingDirectory();
198 bool AnyNotWritten = false;
199
200 for (const auto &FileAndReplacements : FileReplacements) {
201 Rewriter Rewrite(SourceMgr, LangOpts);
202 const StringRef File = FileAndReplacements.first();
203 VFS.setCurrentWorkingDirectory(FileAndReplacements.second.BuildDir);
204 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
205 SourceMgr.getFileManager().getBufferForFile(File);
206 if (!Buffer) {
207 llvm::errs() << "Can't get buffer for file " << File << ": "
208 << Buffer.getError().message() << "\n";
209 // FIXME: Maybe don't apply fixes for other files as well.
210 continue;
211 }
212 const StringRef Code = Buffer.get()->getBuffer();
213 auto Style = format::getStyle(
214 Context.getOptionsForFile(File).FormatStyle.value_or("none"), File,
215 "none");
216 if (!Style) {
217 llvm::errs() << llvm::toString(Style.takeError()) << "\n";
218 continue;
219 }
220 llvm::Expected<tooling::Replacements> Replacements =
221 format::cleanupAroundReplacements(
222 Code, FileAndReplacements.second.Replaces, *Style);
223 if (!Replacements) {
224 llvm::errs() << llvm::toString(Replacements.takeError()) << "\n";
225 continue;
226 }
227 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
228 format::formatReplacements(Code, *Replacements, *Style)) {
229 Replacements = std::move(FormattedReplacements);
230 if (!Replacements)
231 llvm_unreachable("!Replacements");
232 } else {
233 llvm::errs() << llvm::toString(FormattedReplacements.takeError())
234 << ". Skipping formatting.\n";
235 }
236 if (!tooling::applyAllReplacements(Replacements.get(), Rewrite))
237 llvm::errs() << "Can't apply replacements for file " << File << "\n";
238 AnyNotWritten |= Rewrite.overwriteChangedFiles();
239 }
240
241 if (AnyNotWritten) {
242 llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
243 } else {
244 llvm::errs() << "clang-tidy applied " << AppliedFixes << " of "
245 << TotalFixes << " suggested fixes.\n";
246 }
247
248 if (OriginalCWD)
249 VFS.setCurrentWorkingDirectory(*OriginalCWD);
250 }
251 }
252
253 unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
254
255private:
256 SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
257 if (FilePath.empty())
258 return {};
259
260 auto File = SourceMgr.getFileManager().getOptionalFileRef(FilePath);
261 if (!File)
262 return {};
263
264 const FileID ID = SourceMgr.getOrCreateFileID(*File, SrcMgr::C_User);
265 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
266 }
267
268 void reportFix(const DiagnosticBuilder &Diag,
269 const llvm::StringMap<Replacements> &Fix) {
270 for (const auto &FileAndReplacements : Fix) {
271 for (const auto &Repl : FileAndReplacements.second) {
272 if (!Repl.isApplicable())
273 continue;
274 FileByteRange FBR;
275 FBR.FilePath = Repl.getFilePath().str();
276 FBR.FileOffset = Repl.getOffset();
277 FBR.Length = Repl.getLength();
278
279 Diag << FixItHint::CreateReplacement(getRange(FBR),
280 Repl.getReplacementText());
281 }
282 }
283 }
284
285 void reportNote(const tooling::DiagnosticMessage &Message) {
286 const SourceLocation Loc =
287 getLocation(Message.FilePath, Message.FileOffset);
288 auto Diag =
289 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
290 << Message.Message;
291 for (const FileByteRange &FBR : Message.Ranges)
292 Diag << getRange(FBR);
293 reportFix(Diag, Message.Fix);
294 }
295
296 CharSourceRange getRange(const FileByteRange &Range) {
297 SmallString<128> AbsoluteFilePath{Range.FilePath};
298 Files.makeAbsolutePath(AbsoluteFilePath);
299 const SourceLocation BeginLoc =
300 getLocation(AbsoluteFilePath, Range.FileOffset);
301 const SourceLocation EndLoc = BeginLoc.getLocWithOffset(Range.Length);
302 // Retrieve the source range for applicable highlights and fixes. Macro
303 // definition on the command line have locations in a virtual buffer and
304 // don't have valid file paths and are therefore not applicable.
305 return CharSourceRange::getCharRange(BeginLoc, EndLoc);
306 }
307
308 struct ReplacementsWithBuildDir {
309 StringRef BuildDir;
310 Replacements Replaces;
311 };
312
313 FileManager Files;
314 LangOptions LangOpts; // FIXME: use langopts from each original file
315 DiagnosticOptions DiagOpts;
316 DiagnosticConsumer *DiagPrinter;
317 DiagnosticsEngine Diags;
318 SourceManager SourceMgr;
319 llvm::StringMap<ReplacementsWithBuildDir> FileReplacements;
320 ClangTidyContext &Context;
321 FixBehaviour ApplyFixes;
322 unsigned TotalFixes = 0U;
323 unsigned AppliedFixes = 0U;
324 unsigned WarningsAsErrors = 0U;
325};
326
327class ClangTidyASTConsumer : public MultiplexConsumer {
328public:
329 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
330 std::unique_ptr<ClangTidyProfiling> Profiling,
331 std::unique_ptr<ast_matchers::MatchFinder> Finder,
332 std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
333 : MultiplexConsumer(std::move(Consumers)),
334 Profiling(std::move(Profiling)), Finder(std::move(Finder)),
335 Checks(std::move(Checks)) {}
336
337private:
338 // Destructor order matters! Profiling must be destructed last.
339 // Or at least after Finder.
340 std::unique_ptr<ClangTidyProfiling> Profiling;
341 std::unique_ptr<ast_matchers::MatchFinder> Finder;
342 std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
343 void anchor() override {}
344};
345
346} // namespace
347
349 ClangTidyContext &Context,
350 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
351 : Context(Context), OverlayFS(std::move(OverlayFS)),
352 CheckFactories(new ClangTidyCheckFactories) {
353#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
354 if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
355 custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
356#endif
357 for (const ClangTidyModuleRegistry::entry E :
358 ClangTidyModuleRegistry::entries()) {
359 std::unique_ptr<ClangTidyModule> Module = E.instantiate();
360 Module->addCheckFactories(*CheckFactories);
361 }
362}
363
364#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
365static void
366setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
367 clang::AnalyzerOptions &AnalyzerOptions) {
368 for (const auto &Opt : Opts.CheckOptions) {
369 StringRef OptName(Opt.getKey());
370 if (!OptName.consume_front(AnalyzerCheckNamePrefix))
371 continue;
372 // Analyzer options are always local options so we can ignore priority.
373 AnalyzerOptions.Config[OptName] = Opt.getValue().Value;
374 }
375}
376
377using CheckersList = std::vector<std::pair<std::string, bool>>;
378
379static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
380 bool IncludeExperimental) {
381 CheckersList List;
382
383 const auto &RegisteredCheckers =
384 AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
385 const bool AnalyzerChecksEnabled =
386 llvm::any_of(RegisteredCheckers, [&](StringRef CheckName) -> bool {
387 return Context.isCheckEnabled(
388 (AnalyzerCheckNamePrefix + CheckName).str());
389 });
390
391 if (!AnalyzerChecksEnabled)
392 return List;
393
394 // List all static analyzer checkers that our filter enables.
395 //
396 // Always add all core checkers if any other static analyzer check is enabled.
397 // This is currently necessary, as other path sensitive checks rely on the
398 // core checkers.
399 for (const StringRef CheckName : RegisteredCheckers) {
400 const std::string ClangTidyCheckName(
401 (AnalyzerCheckNamePrefix + CheckName).str());
402
403 if (CheckName.starts_with("core") ||
404 Context.isCheckEnabled(ClangTidyCheckName)) {
405 List.emplace_back(std::string(CheckName), true);
406 }
407 }
408 return List;
409}
410#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
411
412std::unique_ptr<clang::ASTConsumer>
414 clang::CompilerInstance &Compiler, StringRef File) {
415 // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
416 // modify Compiler.
417 SourceManager *SM = &Compiler.getSourceManager();
418 Context.setSourceManager(SM);
419 Context.setCurrentFile(File);
420 Context.setASTContext(&Compiler.getASTContext());
421
422 auto WorkingDir = Compiler.getSourceManager()
423 .getFileManager()
424 .getVirtualFileSystem()
425 .getCurrentWorkingDirectory();
426 if (WorkingDir)
427 Context.setCurrentBuildDirectory(WorkingDir.get());
428#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
429 if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
430 custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
431#endif
432 std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
433 CheckFactories->createChecksForLanguage(&Context);
434
435 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
436
437 std::unique_ptr<ClangTidyProfiling> Profiling;
438 if (Context.getEnableProfiling()) {
439 Profiling =
440 std::make_unique<ClangTidyProfiling>(Context.getProfileStorageParams());
441 FinderOptions.CheckProfiling.emplace(Profiling->Records);
442 }
443
444 // Avoid processing system headers, unless the user explicitly requests it
445 if (!Context.getOptions().SystemHeaders.value_or(false))
446 FinderOptions.IgnoreSystemHeaders = true;
447
448 std::unique_ptr<ast_matchers::MatchFinder> Finder(
449 new ast_matchers::MatchFinder(std::move(FinderOptions)));
450
451 Preprocessor *PP = &Compiler.getPreprocessor();
452 Preprocessor *ModuleExpanderPP = PP;
453
454 if (Context.canEnableModuleHeadersParsing() &&
455 Context.getLangOpts().Modules && OverlayFS != nullptr) {
456 auto ModuleExpander = std::make_unique<ExpandModularHeadersPPCallbacks>(
457 &Compiler, *OverlayFS);
458 ModuleExpanderPP = ModuleExpander->getPreprocessor();
459 PP->addPPCallbacks(std::move(ModuleExpander));
460 }
461
462 for (auto &Check : Checks) {
463 Check->registerMatchers(&*Finder);
464 Check->registerPPCallbacks(*SM, PP, ModuleExpanderPP);
465 }
466
467 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
468 if (!Checks.empty())
469 Consumers.push_back(Finder->newASTConsumer());
470
471#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
472 AnalyzerOptions &AnalyzerOptions = Compiler.getAnalyzerOpts();
473 AnalyzerOptions.CheckersAndPackages = getAnalyzerCheckersAndPackages(
474 Context, Context.canEnableAnalyzerAlphaCheckers());
475 if (!AnalyzerOptions.CheckersAndPackages.empty()) {
476 setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
477 AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
478 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
479 ento::CreateAnalysisConsumer(Compiler);
480 AnalysisConsumer->AddDiagnosticConsumer(
481 std::make_unique<AnalyzerDiagnosticConsumer>(Context));
482 Consumers.push_back(std::move(AnalysisConsumer));
483 }
484#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
485 return std::make_unique<ClangTidyASTConsumer>(
486 std::move(Consumers), std::move(Profiling), std::move(Finder),
487 std::move(Checks));
488}
489
491 std::vector<std::string> CheckNames;
492 for (const auto &CheckFactory : *CheckFactories)
493 if (Context.isCheckEnabled(CheckFactory.getKey()))
494 CheckNames.emplace_back(CheckFactory.getKey());
495
496#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
497 for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
498 Context, Context.canEnableAnalyzerAlphaCheckers()))
499 CheckNames.emplace_back(
500 (AnalyzerCheckNamePrefix + AnalyzerCheck.first).str());
501#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
502
503 llvm::sort(CheckNames);
504 return CheckNames;
505}
506
509 const std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
510 CheckFactories->createChecks(&Context);
511 for (const auto &Check : Checks)
512 Check->storeOptions(Options);
513 return Options;
514}
515
516std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
520 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
521 Options),
523 ClangTidyASTConsumerFactory Factory(Context);
524 return Factory.getCheckNames();
525}
526
528 const std::vector<std::string> &EnabledChecks) {
529 ClangTidyOptions::OptionMap FilteredOptions;
530 for (const auto &[OptionName, Value] : Options.CheckOptions) {
531 const size_t CheckNameEndPos = OptionName.find('.');
532 if (CheckNameEndPos == StringRef::npos)
533 continue;
534 const StringRef CheckName = OptionName.substr(0, CheckNameEndPos);
535 if (llvm::binary_search(EnabledChecks, CheckName))
536 FilteredOptions[OptionName] = Value;
537 }
538 Options.CheckOptions = std::move(FilteredOptions);
539}
540
546 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
547 Options),
549 ClangTidyDiagnosticConsumer DiagConsumer(Context);
550 auto DiagOpts = std::make_unique<DiagnosticOptions>();
551 DiagnosticsEngine DE(llvm::makeIntrusiveRefCnt<DiagnosticIDs>(), *DiagOpts,
552 &DiagConsumer, /*ShouldOwnClient=*/false);
553 Context.setDiagnosticsEngine(std::move(DiagOpts), &DE);
554 ClangTidyASTConsumerFactory Factory(Context);
555 return Factory.getCheckOptions();
556}
557
558std::vector<ClangTidyError>
560 const CompilationDatabase &Compilations,
561 ArrayRef<std::string> InputFiles,
562 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
563 bool ApplyAnyFix, bool EnableCheckProfile,
564 llvm::StringRef StoreCheckProfile, bool Quiet) {
565 ClangTool Tool(Compilations, InputFiles,
566 std::make_shared<PCHContainerOperations>(), BaseFS);
567
568 // Add extra arguments passed by the clang-tidy command-line.
569 const ArgumentsAdjuster PerFileExtraArgumentsInserter =
570 [&Context](const CommandLineArguments &Args, StringRef Filename) {
571 ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
572 CommandLineArguments AdjustedArgs = Args;
573 if (Opts.ExtraArgsBefore) {
574 auto I = AdjustedArgs.begin();
575 if (I != AdjustedArgs.end() && !StringRef(*I).starts_with("-"))
576 ++I; // Skip compiler binary name, if it is there.
577 AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
578 Opts.ExtraArgsBefore->end());
579 }
580 if (Opts.ExtraArgs)
581 AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
582 Opts.ExtraArgs->end());
583 return AdjustedArgs;
584 };
585
586 // Remove unwanted arguments passed to the compiler
587 const ArgumentsAdjuster PerFileArgumentRemover =
588 [&Context](const CommandLineArguments &Args, StringRef Filename) {
589 ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
590 CommandLineArguments AdjustedArgs = Args;
591
592 if (Opts.RemovedArgs) {
593 for (const StringRef ArgToRemove : *Opts.RemovedArgs) {
594 AdjustedArgs.erase(std::remove(AdjustedArgs.begin(),
595 AdjustedArgs.end(), ArgToRemove),
596 AdjustedArgs.end());
597 }
598 }
599
600 return AdjustedArgs;
601 };
602
603 Tool.appendArgumentsAdjuster(PerFileArgumentRemover);
604 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
605 Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
608
609 ClangTidyDiagnosticConsumer DiagConsumer(Context, nullptr, true, ApplyAnyFix);
610 auto DiagOpts = std::make_unique<DiagnosticOptions>();
611 DiagnosticsEngine DE(DiagnosticIDs::create(), *DiagOpts, &DiagConsumer,
612 /*ShouldOwnClient=*/false);
613 Context.setDiagnosticsEngine(std::move(DiagOpts), &DE);
614 Tool.setDiagnosticConsumer(&DiagConsumer);
615
616 class ActionFactory : public FrontendActionFactory {
617 public:
618 ActionFactory(ClangTidyContext &Context,
619 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
620 bool Quiet)
621 : ConsumerFactory(Context, std::move(BaseFS)), Quiet(Quiet) {}
622 std::unique_ptr<FrontendAction> create() override {
623 return std::make_unique<Action>(&ConsumerFactory);
624 }
625
626 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
627 FileManager *Files,
628 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
629 DiagnosticConsumer *DiagConsumer) override {
630 // Explicitly ask to define __clang_analyzer__ macro.
631 Invocation->getPreprocessorOpts().SetUpStaticAnalyzer = true;
632 if (Quiet)
633 Invocation->getDiagnosticOpts().ShowCarets = false;
634 return FrontendActionFactory::runInvocation(
635 Invocation, Files, PCHContainerOps, DiagConsumer);
636 }
637
638 private:
639 class Action : public ASTFrontendAction {
640 public:
641 Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
642 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
643 StringRef File) override {
644 return Factory->createASTConsumer(Compiler, File);
645 }
646
647 private:
649 };
650
651 ClangTidyASTConsumerFactory ConsumerFactory;
652 bool Quiet;
653 };
654
655 ActionFactory Factory(Context, std::move(BaseFS), Quiet);
656 Tool.run(&Factory);
657 return DiagConsumer.take();
658}
659
660void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
662 unsigned &WarningsAsErrorsCount,
663 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
664 ErrorReporter Reporter(Context, Fix, std::move(BaseFS));
665 llvm::vfs::FileSystem &FileSystem =
666 Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
667 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
668 if (!InitialWorkingDir)
669 llvm::report_fatal_error("Cannot get current working path.");
670
671 for (const ClangTidyError &Error : Errors) {
672 if (!Error.BuildDirectory.empty()) {
673 // By default, the working directory of file system is the current
674 // clang-tidy running directory.
675 //
676 // Change the directory to the one used during the analysis.
677 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
678 }
679 Reporter.reportDiagnostic(Error);
680 // Return to the initial directory to correctly resolve next Error.
681 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
682 }
683 Reporter.finish();
684 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
685}
686
687void exportReplacements(const llvm::StringRef MainFilePath,
688 const std::vector<ClangTidyError> &Errors,
689 raw_ostream &OS) {
690 TranslationUnitDiagnostics TUD;
691 TUD.MainSourceFile = std::string(MainFilePath);
692 for (const auto &Error : Errors) {
693 tooling::Diagnostic Diag = Error;
694 if (Error.IsWarningAsError)
695 Diag.DiagLevel = tooling::Diagnostic::Error;
696 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
697 }
698
699 yaml::Output YAML(OS);
700 YAML << TUD;
701}
702
705 ChecksAndOptions Result;
706 ClangTidyOptions Opts;
707 Opts.Checks = "*";
709 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(), Opts),
711 ClangTidyCheckFactories Factories;
712#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
714 custom::RegisterCustomChecks(Context.getOptions(), Factories);
715#endif
716 for (const ClangTidyModuleRegistry::entry &Module :
717 ClangTidyModuleRegistry::entries()) {
718 Module.instantiate()->addCheckFactories(Factories);
719 }
720
721 for (const auto &Factory : Factories)
722 Result.Checks.insert(Factory.getKey());
723
724#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
725 SmallString<64> Buffer(AnalyzerCheckNamePrefix);
726 const size_t DefSize = Buffer.size();
727 for (const auto &AnalyzerCheck : AnalyzerOptions::getRegisteredCheckers(
729 Buffer.truncate(DefSize);
730 Buffer.append(AnalyzerCheck);
731 Result.Checks.insert(Buffer);
732 }
733
734 static constexpr StringRef OptionNames[] = {
735#define GET_CHECKER_OPTIONS
736#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT, \
737 RELEASE, HIDDEN) \
738 ANALYZER_CHECK_NAME_PREFIX CHECKER ":" OPTION_NAME,
739
740#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
741#undef CHECKER_OPTION
742#undef GET_CHECKER_OPTIONS
743 };
744
745 Result.Options.insert_range(OptionNames);
746#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
747
748 Context.setOptionsCollector(&Result.Options);
749 for (const auto &Factory : Factories)
750 Factory.getValue()(Factory.getKey(), &Context);
751
752 return Result;
753}
754} // namespace clang::tidy
static cl::opt< bool > UseColor("use-color", cl::desc(R"(Use colors in detailed AST output. If not set, colors will be used if the terminal connected to standard output supports colors.)"), cl::init(false), cl::cat(ClangQueryCategory))
static cl::opt< bool > EnableCheckProfile("enable-check-profile", desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > Fix("fix", desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > ExperimentalCustomChecks("experimental-custom-checks", desc(R"( Enable experimental clang-query based custom checks. see https://clang.llvm.org/extra/clang-tidy/QueryBasedCustomChecks.html. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
static cl::opt< std::string > Checks("checks", desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
static cl::opt< bool > Quiet("quiet", desc(R"( Run clang-tidy in quiet mode. This suppresses printing statistics about ignored warnings and warnings treated as errors if the respective options are specified. )"), cl::init(false), cl::cat(ClangTidyCategory))
static cl::opt< std::string > StoreCheckProfile("store-check-profile", desc(R"( By default reports are printed in tabulated format to stderr. When this option is passed, these per-TU profiles are instead stored as JSON. )"), cl::value_desc("prefix"), cl::cat(ClangTidyCategory))
std::unique_ptr< clang::ASTConsumer > createASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
ClangTidyASTConsumerFactory(ClangTidyContext &Context, IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > OverlayFS=nullptr)
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
A collection of ClangTidyCheckFactory instances.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void setOptionsCollector(llvm::StringSet<> *Collector)
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
void setDiagnosticsEngine(std::unique_ptr< DiagnosticOptions > DiagOpts, DiagnosticsEngine *DiagEngine)
Sets the DiagnosticsEngine that diag() will emit diagnostics to.
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError.
@ Error
An error message.
Definition Protocol.h:734
void(* RegisterCustomChecks)(const ClangTidyOptions &O, ClangTidyCheckFactories &Factories)
const llvm::StringMap< tooling::Replacements > * getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix)
Gets the Fix attached to Diagnostic.
ChecksAndOptions getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
FixBehaviour
Controls what kind of fixes clang-tidy is allowed to apply.
Definition ClangTidy.h:103
@ FB_NoFix
Don't try to apply any fix.
Definition ClangTidy.h:105
@ FB_FixNotes
Apply fixes found in notes.
Definition ClangTidy.h:109
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Fills the list of check names that are enabled when the provided filters are applied.
llvm::Registry< ClangTidyModule > ClangTidyModuleRegistry
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers, bool ExperimentalCustomChecks)
Returns the effective check-specific options.
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, FixBehaviour Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
void filterCheckOptions(ClangTidyOptions &Options, const std::vector< std::string > &EnabledChecks)
Filters CheckOptions in Options to only include options specified in the EnabledChecks which is a sor...
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
std::vector< ClangTidyError > runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< llvm::vfs::OverlayFileSystem > BaseFS, bool ApplyAnyFix, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile, bool Quiet)
Some operations such as code completion produce a set of candidates.
Definition Generators.h:145
llvm::StringMap< ClangTidyValue > OptionMap
A detected error complete with information to display diagnostic and automatic fix.
Contains options for clang-tidy.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::StringMap< ClangTidyValue > OptionMap
std::optional< std::string > Checks
Checks filter.
std::optional< ArgList > RemovedArgs
Remove command line arguments sent to the compiler matching this.
std::optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
std::optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.