clang-tools 22.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"
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.push_back(std::make_pair(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, File, "none");
215 if (!Style) {
216 llvm::errs() << llvm::toString(Style.takeError()) << "\n";
217 continue;
218 }
219 llvm::Expected<tooling::Replacements> Replacements =
220 format::cleanupAroundReplacements(
221 Code, FileAndReplacements.second.Replaces, *Style);
222 if (!Replacements) {
223 llvm::errs() << llvm::toString(Replacements.takeError()) << "\n";
224 continue;
225 }
226 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
227 format::formatReplacements(Code, *Replacements, *Style)) {
228 Replacements = std::move(FormattedReplacements);
229 if (!Replacements)
230 llvm_unreachable("!Replacements");
231 } else {
232 llvm::errs() << llvm::toString(FormattedReplacements.takeError())
233 << ". Skipping formatting.\n";
234 }
235 if (!tooling::applyAllReplacements(Replacements.get(), Rewrite))
236 llvm::errs() << "Can't apply replacements for file " << File << "\n";
237 AnyNotWritten |= Rewrite.overwriteChangedFiles();
238 }
239
240 if (AnyNotWritten) {
241 llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
242 } else {
243 llvm::errs() << "clang-tidy applied " << AppliedFixes << " of "
244 << TotalFixes << " suggested fixes.\n";
245 }
246
247 if (OriginalCWD)
248 VFS.setCurrentWorkingDirectory(*OriginalCWD);
249 }
250 }
251
252 unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
253
254private:
255 SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
256 if (FilePath.empty())
257 return {};
258
259 auto File = SourceMgr.getFileManager().getOptionalFileRef(FilePath);
260 if (!File)
261 return {};
262
263 const FileID ID = SourceMgr.getOrCreateFileID(*File, SrcMgr::C_User);
264 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
265 }
266
267 void reportFix(const DiagnosticBuilder &Diag,
268 const llvm::StringMap<Replacements> &Fix) {
269 for (const auto &FileAndReplacements : Fix) {
270 for (const auto &Repl : FileAndReplacements.second) {
271 if (!Repl.isApplicable())
272 continue;
273 FileByteRange FBR;
274 FBR.FilePath = Repl.getFilePath().str();
275 FBR.FileOffset = Repl.getOffset();
276 FBR.Length = Repl.getLength();
277
278 Diag << FixItHint::CreateReplacement(getRange(FBR),
279 Repl.getReplacementText());
280 }
281 }
282 }
283
284 void reportNote(const tooling::DiagnosticMessage &Message) {
285 const SourceLocation Loc =
286 getLocation(Message.FilePath, Message.FileOffset);
287 auto Diag =
288 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
289 << Message.Message;
290 for (const FileByteRange &FBR : Message.Ranges)
291 Diag << getRange(FBR);
292 reportFix(Diag, Message.Fix);
293 }
294
295 CharSourceRange getRange(const FileByteRange &Range) {
296 SmallString<128> AbsoluteFilePath{Range.FilePath};
297 Files.makeAbsolutePath(AbsoluteFilePath);
298 const SourceLocation BeginLoc =
299 getLocation(AbsoluteFilePath, Range.FileOffset);
300 const SourceLocation EndLoc = BeginLoc.getLocWithOffset(Range.Length);
301 // Retrieve the source range for applicable highlights and fixes. Macro
302 // definition on the command line have locations in a virtual buffer and
303 // don't have valid file paths and are therefore not applicable.
304 return CharSourceRange::getCharRange(BeginLoc, EndLoc);
305 }
306
307 struct ReplacementsWithBuildDir {
308 StringRef BuildDir;
309 Replacements Replaces;
310 };
311
312 FileManager Files;
313 LangOptions LangOpts; // FIXME: use langopts from each original file
314 DiagnosticOptions DiagOpts;
315 DiagnosticConsumer *DiagPrinter;
316 DiagnosticsEngine Diags;
317 SourceManager SourceMgr;
318 llvm::StringMap<ReplacementsWithBuildDir> FileReplacements;
319 ClangTidyContext &Context;
320 FixBehaviour ApplyFixes;
321 unsigned TotalFixes = 0U;
322 unsigned AppliedFixes = 0U;
323 unsigned WarningsAsErrors = 0U;
324};
325
326class ClangTidyASTConsumer : public MultiplexConsumer {
327public:
328 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
329 std::unique_ptr<ClangTidyProfiling> Profiling,
330 std::unique_ptr<ast_matchers::MatchFinder> Finder,
331 std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
332 : MultiplexConsumer(std::move(Consumers)),
333 Profiling(std::move(Profiling)), Finder(std::move(Finder)),
334 Checks(std::move(Checks)) {}
335
336private:
337 // Destructor order matters! Profiling must be destructed last.
338 // Or at least after Finder.
339 std::unique_ptr<ClangTidyProfiling> Profiling;
340 std::unique_ptr<ast_matchers::MatchFinder> Finder;
341 std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
342 void anchor() override {};
343};
344
345} // namespace
346
348 ClangTidyContext &Context,
349 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
350 : Context(Context), OverlayFS(std::move(OverlayFS)),
351 CheckFactories(new ClangTidyCheckFactories) {
352#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
353 if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
354 custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
355#endif
356 for (const ClangTidyModuleRegistry::entry E :
357 ClangTidyModuleRegistry::entries()) {
358 std::unique_ptr<ClangTidyModule> Module = E.instantiate();
359 Module->addCheckFactories(*CheckFactories);
360 }
361}
362
363#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
364static void
365setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
366 clang::AnalyzerOptions &AnalyzerOptions) {
367 for (const auto &Opt : Opts.CheckOptions) {
368 StringRef OptName(Opt.getKey());
369 if (!OptName.consume_front(AnalyzerCheckNamePrefix))
370 continue;
371 // Analyzer options are always local options so we can ignore priority.
372 AnalyzerOptions.Config[OptName] = Opt.getValue().Value;
373 }
374}
375
376using CheckersList = std::vector<std::pair<std::string, bool>>;
377
378static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
379 bool IncludeExperimental) {
380 CheckersList List;
381
382 const auto &RegisteredCheckers =
383 AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
384 const bool AnalyzerChecksEnabled =
385 llvm::any_of(RegisteredCheckers, [&](StringRef CheckName) -> bool {
386 return Context.isCheckEnabled(
387 (AnalyzerCheckNamePrefix + CheckName).str());
388 });
389
390 if (!AnalyzerChecksEnabled)
391 return List;
392
393 // List all static analyzer checkers that our filter enables.
394 //
395 // Always add all core checkers if any other static analyzer check is enabled.
396 // This is currently necessary, as other path sensitive checks rely on the
397 // core checkers.
398 for (const StringRef CheckName : RegisteredCheckers) {
399 const std::string ClangTidyCheckName(
400 (AnalyzerCheckNamePrefix + CheckName).str());
401
402 if (CheckName.starts_with("core") ||
403 Context.isCheckEnabled(ClangTidyCheckName)) {
404 List.emplace_back(std::string(CheckName), true);
405 }
406 }
407 return List;
408}
409#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
410
411std::unique_ptr<clang::ASTConsumer>
413 clang::CompilerInstance &Compiler, StringRef File) {
414 // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
415 // modify Compiler.
416 SourceManager *SM = &Compiler.getSourceManager();
417 Context.setSourceManager(SM);
418 Context.setCurrentFile(File);
419 Context.setASTContext(&Compiler.getASTContext());
420
421 auto WorkingDir = Compiler.getSourceManager()
422 .getFileManager()
423 .getVirtualFileSystem()
424 .getCurrentWorkingDirectory();
425 if (WorkingDir)
426 Context.setCurrentBuildDirectory(WorkingDir.get());
427#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
428 if (Context.canExperimentalCustomChecks() && custom::RegisterCustomChecks)
429 custom::RegisterCustomChecks(Context.getOptions(), *CheckFactories);
430#endif
431 std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
432 CheckFactories->createChecksForLanguage(&Context);
433
434 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
435
436 std::unique_ptr<ClangTidyProfiling> Profiling;
437 if (Context.getEnableProfiling()) {
438 Profiling =
439 std::make_unique<ClangTidyProfiling>(Context.getProfileStorageParams());
440 FinderOptions.CheckProfiling.emplace(Profiling->Records);
441 }
442
443 // Avoid processing system headers, unless the user explicitly requests it
444 if (!Context.getOptions().SystemHeaders.value_or(false))
445 FinderOptions.IgnoreSystemHeaders = true;
446
447 std::unique_ptr<ast_matchers::MatchFinder> Finder(
448 new ast_matchers::MatchFinder(std::move(FinderOptions)));
449
450 Preprocessor *PP = &Compiler.getPreprocessor();
451 Preprocessor *ModuleExpanderPP = PP;
452
453 if (Context.canEnableModuleHeadersParsing() &&
454 Context.getLangOpts().Modules && OverlayFS != nullptr) {
455 auto ModuleExpander = std::make_unique<ExpandModularHeadersPPCallbacks>(
456 &Compiler, *OverlayFS);
457 ModuleExpanderPP = ModuleExpander->getPreprocessor();
458 PP->addPPCallbacks(std::move(ModuleExpander));
459 }
460
461 for (auto &Check : Checks) {
462 Check->registerMatchers(&*Finder);
463 Check->registerPPCallbacks(*SM, PP, ModuleExpanderPP);
464 }
465
466 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
467 if (!Checks.empty())
468 Consumers.push_back(Finder->newASTConsumer());
469
470#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
471 AnalyzerOptions &AnalyzerOptions = Compiler.getAnalyzerOpts();
472 AnalyzerOptions.CheckersAndPackages = getAnalyzerCheckersAndPackages(
473 Context, Context.canEnableAnalyzerAlphaCheckers());
474 if (!AnalyzerOptions.CheckersAndPackages.empty()) {
475 setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
476 AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
477 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
478 ento::CreateAnalysisConsumer(Compiler);
479 AnalysisConsumer->AddDiagnosticConsumer(
480 std::make_unique<AnalyzerDiagnosticConsumer>(Context));
481 Consumers.push_back(std::move(AnalysisConsumer));
482 }
483#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
484 return std::make_unique<ClangTidyASTConsumer>(
485 std::move(Consumers), std::move(Profiling), std::move(Finder),
486 std::move(Checks));
487}
488
490 std::vector<std::string> CheckNames;
491 for (const auto &CheckFactory : *CheckFactories)
492 if (Context.isCheckEnabled(CheckFactory.getKey()))
493 CheckNames.emplace_back(CheckFactory.getKey());
494
495#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
496 for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
497 Context, Context.canEnableAnalyzerAlphaCheckers()))
498 CheckNames.emplace_back(
499 (AnalyzerCheckNamePrefix + AnalyzerCheck.first).str());
500#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
501
502 llvm::sort(CheckNames);
503 return CheckNames;
504}
505
508 const std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
509 CheckFactories->createChecks(&Context);
510 for (const auto &Check : Checks)
511 Check->storeOptions(Options);
512 return Options;
513}
514
515std::vector<std::string> getCheckNames(const ClangTidyOptions &Options,
519 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
520 Options),
522 ClangTidyASTConsumerFactory Factory(Context);
523 return Factory.getCheckNames();
524}
525
527 const std::vector<std::string> &EnabledChecks) {
528 ClangTidyOptions::OptionMap FilteredOptions;
529 for (const auto &[OptionName, Value] : Options.CheckOptions) {
530 const size_t CheckNameEndPos = OptionName.find('.');
531 if (CheckNameEndPos == StringRef::npos)
532 continue;
533 const StringRef CheckName = OptionName.substr(0, CheckNameEndPos);
534 if (llvm::binary_search(EnabledChecks, CheckName))
535 FilteredOptions[OptionName] = Value;
536 }
537 Options.CheckOptions = std::move(FilteredOptions);
538}
539
545 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
546 Options),
548 ClangTidyDiagnosticConsumer DiagConsumer(Context);
549 auto DiagOpts = std::make_unique<DiagnosticOptions>();
550 DiagnosticsEngine DE(llvm::makeIntrusiveRefCnt<DiagnosticIDs>(), *DiagOpts,
551 &DiagConsumer, /*ShouldOwnClient=*/false);
552 Context.setDiagnosticsEngine(std::move(DiagOpts), &DE);
553 ClangTidyASTConsumerFactory Factory(Context);
554 return Factory.getCheckOptions();
555}
556
557std::vector<ClangTidyError>
559 const CompilationDatabase &Compilations,
560 ArrayRef<std::string> InputFiles,
561 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
562 bool ApplyAnyFix, bool EnableCheckProfile,
563 llvm::StringRef StoreCheckProfile, bool Quiet) {
564 ClangTool Tool(Compilations, InputFiles,
565 std::make_shared<PCHContainerOperations>(), BaseFS);
566
567 // Add extra arguments passed by the clang-tidy command-line.
568 const ArgumentsAdjuster PerFileExtraArgumentsInserter =
569 [&Context](const CommandLineArguments &Args, StringRef Filename) {
570 ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
571 CommandLineArguments AdjustedArgs = Args;
572 if (Opts.ExtraArgsBefore) {
573 auto I = AdjustedArgs.begin();
574 if (I != AdjustedArgs.end() && !StringRef(*I).starts_with("-"))
575 ++I; // Skip compiler binary name, if it is there.
576 AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
577 Opts.ExtraArgsBefore->end());
578 }
579 if (Opts.ExtraArgs)
580 AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
581 Opts.ExtraArgs->end());
582 return AdjustedArgs;
583 };
584
585 // Remove unwanted arguments passed to the compiler
586 const ArgumentsAdjuster PerFileArgumentRemover =
587 [&Context](const CommandLineArguments &Args, StringRef Filename) {
588 ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
589 CommandLineArguments AdjustedArgs = Args;
590
591 if (Opts.RemovedArgs) {
592 for (const StringRef ArgToRemove : *Opts.RemovedArgs) {
593 AdjustedArgs.erase(std::remove(AdjustedArgs.begin(),
594 AdjustedArgs.end(), ArgToRemove),
595 AdjustedArgs.end());
596 }
597 }
598
599 return AdjustedArgs;
600 };
601
602 Tool.appendArgumentsAdjuster(PerFileArgumentRemover);
603 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
604 Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
607
608 ClangTidyDiagnosticConsumer DiagConsumer(Context, nullptr, true, ApplyAnyFix);
609 auto DiagOpts = std::make_unique<DiagnosticOptions>();
610 DiagnosticsEngine DE(DiagnosticIDs::create(), *DiagOpts, &DiagConsumer,
611 /*ShouldOwnClient=*/false);
612 Context.setDiagnosticsEngine(std::move(DiagOpts), &DE);
613 Tool.setDiagnosticConsumer(&DiagConsumer);
614
615 class ActionFactory : public FrontendActionFactory {
616 public:
617 ActionFactory(ClangTidyContext &Context,
618 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
619 bool Quiet)
620 : ConsumerFactory(Context, std::move(BaseFS)), Quiet(Quiet) {}
621 std::unique_ptr<FrontendAction> create() override {
622 return std::make_unique<Action>(&ConsumerFactory);
623 }
624
625 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
626 FileManager *Files,
627 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
628 DiagnosticConsumer *DiagConsumer) override {
629 // Explicitly ask to define __clang_analyzer__ macro.
630 Invocation->getPreprocessorOpts().SetUpStaticAnalyzer = true;
631 if (Quiet)
632 Invocation->getDiagnosticOpts().ShowCarets = false;
633 return FrontendActionFactory::runInvocation(
634 Invocation, Files, PCHContainerOps, DiagConsumer);
635 }
636
637 private:
638 class Action : public ASTFrontendAction {
639 public:
640 Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
641 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
642 StringRef File) override {
643 return Factory->createASTConsumer(Compiler, File);
644 }
645
646 private:
648 };
649
650 ClangTidyASTConsumerFactory ConsumerFactory;
651 bool Quiet;
652 };
653
654 ActionFactory Factory(Context, std::move(BaseFS), Quiet);
655 Tool.run(&Factory);
656 return DiagConsumer.take();
657}
658
659void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
661 unsigned &WarningsAsErrorsCount,
662 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
663 ErrorReporter Reporter(Context, Fix, std::move(BaseFS));
664 llvm::vfs::FileSystem &FileSystem =
665 Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
666 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
667 if (!InitialWorkingDir)
668 llvm::report_fatal_error("Cannot get current working path.");
669
670 for (const ClangTidyError &Error : Errors) {
671 if (!Error.BuildDirectory.empty()) {
672 // By default, the working directory of file system is the current
673 // clang-tidy running directory.
674 //
675 // Change the directory to the one used during the analysis.
676 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
677 }
678 Reporter.reportDiagnostic(Error);
679 // Return to the initial directory to correctly resolve next Error.
680 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
681 }
682 Reporter.finish();
683 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
684}
685
686void exportReplacements(const llvm::StringRef MainFilePath,
687 const std::vector<ClangTidyError> &Errors,
688 raw_ostream &OS) {
689 TranslationUnitDiagnostics TUD;
690 TUD.MainSourceFile = std::string(MainFilePath);
691 for (const auto &Error : Errors) {
692 tooling::Diagnostic Diag = Error;
693 if (Error.IsWarningAsError)
694 Diag.DiagLevel = tooling::Diagnostic::Error;
695 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
696 }
697
698 yaml::Output YAML(OS);
699 YAML << TUD;
700}
701
704 ChecksAndOptions Result;
705 ClangTidyOptions Opts;
706 Opts.Checks = "*";
708 std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(), Opts),
710 ClangTidyCheckFactories Factories;
711#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS
713 custom::RegisterCustomChecks(Context.getOptions(), Factories);
714#endif
715 for (const ClangTidyModuleRegistry::entry &Module :
716 ClangTidyModuleRegistry::entries()) {
717 Module.instantiate()->addCheckFactories(Factories);
718 }
719
720 for (const auto &Factory : Factories)
721 Result.Checks.insert(Factory.getKey());
722
723#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
724 SmallString<64> Buffer(AnalyzerCheckNamePrefix);
725 const size_t DefSize = Buffer.size();
726 for (const auto &AnalyzerCheck : AnalyzerOptions::getRegisteredCheckers(
728 Buffer.truncate(DefSize);
729 Buffer.append(AnalyzerCheck);
730 Result.Checks.insert(Buffer);
731 }
732
733 static constexpr StringRef OptionNames[] = {
734#define GET_CHECKER_OPTIONS
735#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT, \
736 RELEASE, HIDDEN) \
737 ANALYZER_CHECK_NAME_PREFIX CHECKER ":" OPTION_NAME,
738
739#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
740#undef CHECKER_OPTION
741#undef GET_CHECKER_OPTIONS
742 };
743
744 Result.Options.insert_range(OptionNames);
745#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
746
747 Context.setOptionsCollector(&Result.Options);
748 for (const auto &Factory : Factories)
749 Factory.getValue()(Factory.getKey(), &Context);
750
751 return Result;
752}
753} // 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.