llvm-project
684 строки · 26.4 Кб
1//===--- tools/extra/clang-tidy/ClangTidy.cpp - Clang tidy tool -----------===//
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/// http://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"
19#include "ClangTidyDiagnosticConsumer.h"
20#include "ClangTidyModuleRegistry.h"
21#include "ClangTidyProfiling.h"
22#include "ExpandModularHeadersPPCallbacks.h"
23#include "clang-tidy-config.h"
24#include "clang/AST/ASTConsumer.h"
25#include "clang/ASTMatchers/ASTMatchFinder.h"
26#include "clang/Format/Format.h"
27#include "clang/Frontend/ASTConsumers.h"
28#include "clang/Frontend/CompilerInstance.h"
29#include "clang/Frontend/FrontendDiagnostic.h"
30#include "clang/Frontend/MultiplexConsumer.h"
31#include "clang/Frontend/TextDiagnosticPrinter.h"
32#include "clang/Lex/PPCallbacks.h"
33#include "clang/Lex/Preprocessor.h"
34#include "clang/Lex/PreprocessorOptions.h"
35#include "clang/Rewrite/Frontend/FixItRewriter.h"
36#include "clang/Rewrite/Frontend/FrontendActions.h"
37#include "clang/Tooling/Core/Diagnostic.h"
38#include "clang/Tooling/DiagnosticsYaml.h"
39#include "clang/Tooling/Refactoring.h"
40#include "clang/Tooling/ReplacementsYaml.h"
41#include "clang/Tooling/Tooling.h"
42#include "llvm/Support/Process.h"
43#include <algorithm>
44#include <utility>
45
46#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
47#include "clang/Analysis/PathDiagnostic.h"
48#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
49#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
50
51using namespace clang::ast_matchers;
52using namespace clang::driver;
53using namespace clang::tooling;
54using namespace llvm;
55
56LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
57
58namespace clang::tidy {
59
60namespace {
61#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
62static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
63
64class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
65public:
66AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
67
68void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
69FilesMade *FilesMade) override {
70for (const ento::PathDiagnostic *PD : Diags) {
71SmallString<64> CheckName(AnalyzerCheckNamePrefix);
72CheckName += PD->getCheckerName();
73Context.diag(CheckName, PD->getLocation().asLocation(),
74PD->getShortDescription())
75<< PD->path.back()->getRanges();
76
77for (const auto &DiagPiece :
78PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
79Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
80DiagPiece->getString(), DiagnosticIDs::Note)
81<< DiagPiece->getRanges();
82}
83}
84}
85
86StringRef getName() const override { return "ClangTidyDiags"; }
87bool supportsLogicalOpControlFlow() const override { return true; }
88bool supportsCrossFileDiagnostics() const override { return true; }
89
90private:
91ClangTidyContext &Context;
92};
93#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
94
95class ErrorReporter {
96public:
97ErrorReporter(ClangTidyContext &Context, FixBehaviour ApplyFixes,
98llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS)
99: Files(FileSystemOptions(), std::move(BaseFS)),
100DiagOpts(new DiagnosticOptions()),
101DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
102Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
103DiagPrinter),
104SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes) {
105DiagOpts->ShowColors = Context.getOptions().UseColor.value_or(
106llvm::sys::Process::StandardOutHasColors());
107DiagPrinter->BeginSourceFile(LangOpts);
108if (DiagOpts->ShowColors && !llvm::sys::Process::StandardOutIsDisplayed()) {
109llvm::sys::Process::UseANSIEscapeCodes(true);
110}
111}
112
113SourceManager &getSourceManager() { return SourceMgr; }
114
115void reportDiagnostic(const ClangTidyError &Error) {
116const tooling::DiagnosticMessage &Message = Error.Message;
117SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
118// Contains a pair for each attempted fix: location and whether the fix was
119// applied successfully.
120SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
121{
122auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
123std::string Name = Error.DiagnosticName;
124if (!Error.EnabledDiagnosticAliases.empty())
125Name += "," + llvm::join(Error.EnabledDiagnosticAliases, ",");
126if (Error.IsWarningAsError) {
127Name += ",-warnings-as-errors";
128Level = DiagnosticsEngine::Error;
129WarningsAsErrors++;
130}
131auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
132<< Message.Message << Name;
133for (const FileByteRange &FBR : Error.Message.Ranges)
134Diag << getRange(FBR);
135// FIXME: explore options to support interactive fix selection.
136const llvm::StringMap<Replacements> *ChosenFix = nullptr;
137if (ApplyFixes != FB_NoFix &&
138(ChosenFix = getFixIt(Error, ApplyFixes == FB_FixNotes))) {
139for (const auto &FileAndReplacements : *ChosenFix) {
140for (const auto &Repl : FileAndReplacements.second) {
141++TotalFixes;
142bool CanBeApplied = false;
143if (!Repl.isApplicable())
144continue;
145SourceLocation FixLoc;
146SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
147Files.makeAbsolutePath(FixAbsoluteFilePath);
148tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
149Repl.getLength(), Repl.getReplacementText());
150auto &Entry = FileReplacements[R.getFilePath()];
151Replacements &Replacements = Entry.Replaces;
152llvm::Error Err = Replacements.add(R);
153if (Err) {
154// FIXME: Implement better conflict handling.
155llvm::errs() << "Trying to resolve conflict: "
156<< llvm::toString(std::move(Err)) << "\n";
157unsigned NewOffset =
158Replacements.getShiftedCodePosition(R.getOffset());
159unsigned NewLength = Replacements.getShiftedCodePosition(
160R.getOffset() + R.getLength()) -
161NewOffset;
162if (NewLength == R.getLength()) {
163R = Replacement(R.getFilePath(), NewOffset, NewLength,
164R.getReplacementText());
165Replacements = Replacements.merge(tooling::Replacements(R));
166CanBeApplied = true;
167++AppliedFixes;
168} else {
169llvm::errs()
170<< "Can't resolve conflict, skipping the replacement.\n";
171}
172} else {
173CanBeApplied = true;
174++AppliedFixes;
175}
176FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
177FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
178Entry.BuildDir = Error.BuildDirectory;
179}
180}
181}
182reportFix(Diag, Error.Message.Fix);
183}
184for (auto Fix : FixLocations) {
185Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
186: diag::note_fixit_failed);
187}
188for (const auto &Note : Error.Notes)
189reportNote(Note);
190}
191
192void finish() {
193if (TotalFixes > 0) {
194auto &VFS = Files.getVirtualFileSystem();
195auto OriginalCWD = VFS.getCurrentWorkingDirectory();
196bool AnyNotWritten = false;
197
198for (const auto &FileAndReplacements : FileReplacements) {
199Rewriter Rewrite(SourceMgr, LangOpts);
200StringRef File = FileAndReplacements.first();
201VFS.setCurrentWorkingDirectory(FileAndReplacements.second.BuildDir);
202llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
203SourceMgr.getFileManager().getBufferForFile(File);
204if (!Buffer) {
205llvm::errs() << "Can't get buffer for file " << File << ": "
206<< Buffer.getError().message() << "\n";
207// FIXME: Maybe don't apply fixes for other files as well.
208continue;
209}
210StringRef Code = Buffer.get()->getBuffer();
211auto Style = format::getStyle(
212*Context.getOptionsForFile(File).FormatStyle, File, "none");
213if (!Style) {
214llvm::errs() << llvm::toString(Style.takeError()) << "\n";
215continue;
216}
217llvm::Expected<tooling::Replacements> Replacements =
218format::cleanupAroundReplacements(
219Code, FileAndReplacements.second.Replaces, *Style);
220if (!Replacements) {
221llvm::errs() << llvm::toString(Replacements.takeError()) << "\n";
222continue;
223}
224if (llvm::Expected<tooling::Replacements> FormattedReplacements =
225format::formatReplacements(Code, *Replacements, *Style)) {
226Replacements = std::move(FormattedReplacements);
227if (!Replacements)
228llvm_unreachable("!Replacements");
229} else {
230llvm::errs() << llvm::toString(FormattedReplacements.takeError())
231<< ". Skipping formatting.\n";
232}
233if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) {
234llvm::errs() << "Can't apply replacements for file " << File << "\n";
235}
236AnyNotWritten |= Rewrite.overwriteChangedFiles();
237}
238
239if (AnyNotWritten) {
240llvm::errs() << "clang-tidy failed to apply suggested fixes.\n";
241} else {
242llvm::errs() << "clang-tidy applied " << AppliedFixes << " of "
243<< TotalFixes << " suggested fixes.\n";
244}
245
246if (OriginalCWD)
247VFS.setCurrentWorkingDirectory(*OriginalCWD);
248}
249}
250
251unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
252
253private:
254SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
255if (FilePath.empty())
256return {};
257
258auto File = SourceMgr.getFileManager().getOptionalFileRef(FilePath);
259if (!File)
260return {};
261
262FileID ID = SourceMgr.getOrCreateFileID(*File, SrcMgr::C_User);
263return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
264}
265
266void reportFix(const DiagnosticBuilder &Diag,
267const llvm::StringMap<Replacements> &Fix) {
268for (const auto &FileAndReplacements : Fix) {
269for (const auto &Repl : FileAndReplacements.second) {
270if (!Repl.isApplicable())
271continue;
272FileByteRange FBR;
273FBR.FilePath = Repl.getFilePath().str();
274FBR.FileOffset = Repl.getOffset();
275FBR.Length = Repl.getLength();
276
277Diag << FixItHint::CreateReplacement(getRange(FBR),
278Repl.getReplacementText());
279}
280}
281}
282
283void reportNote(const tooling::DiagnosticMessage &Message) {
284SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
285auto Diag =
286Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
287<< Message.Message;
288for (const FileByteRange &FBR : Message.Ranges)
289Diag << getRange(FBR);
290reportFix(Diag, Message.Fix);
291}
292
293CharSourceRange getRange(const FileByteRange &Range) {
294SmallString<128> AbsoluteFilePath{Range.FilePath};
295Files.makeAbsolutePath(AbsoluteFilePath);
296SourceLocation BeginLoc = getLocation(AbsoluteFilePath, Range.FileOffset);
297SourceLocation EndLoc = BeginLoc.getLocWithOffset(Range.Length);
298// Retrieve the source range for applicable highlights and fixes. Macro
299// definition on the command line have locations in a virtual buffer and
300// don't have valid file paths and are therefore not applicable.
301return CharSourceRange::getCharRange(BeginLoc, EndLoc);
302}
303
304struct ReplacementsWithBuildDir {
305StringRef BuildDir;
306Replacements Replaces;
307};
308
309FileManager Files;
310LangOptions LangOpts; // FIXME: use langopts from each original file
311IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
312DiagnosticConsumer *DiagPrinter;
313DiagnosticsEngine Diags;
314SourceManager SourceMgr;
315llvm::StringMap<ReplacementsWithBuildDir> FileReplacements;
316ClangTidyContext &Context;
317FixBehaviour ApplyFixes;
318unsigned TotalFixes = 0U;
319unsigned AppliedFixes = 0U;
320unsigned WarningsAsErrors = 0U;
321};
322
323class ClangTidyASTConsumer : public MultiplexConsumer {
324public:
325ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
326std::unique_ptr<ClangTidyProfiling> Profiling,
327std::unique_ptr<ast_matchers::MatchFinder> Finder,
328std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
329: MultiplexConsumer(std::move(Consumers)),
330Profiling(std::move(Profiling)), Finder(std::move(Finder)),
331Checks(std::move(Checks)) {}
332
333private:
334// Destructor order matters! Profiling must be destructed last.
335// Or at least after Finder.
336std::unique_ptr<ClangTidyProfiling> Profiling;
337std::unique_ptr<ast_matchers::MatchFinder> Finder;
338std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
339};
340
341} // namespace
342
343ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
344ClangTidyContext &Context,
345IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
346: Context(Context), OverlayFS(std::move(OverlayFS)),
347CheckFactories(new ClangTidyCheckFactories) {
348for (ClangTidyModuleRegistry::entry E : ClangTidyModuleRegistry::entries()) {
349std::unique_ptr<ClangTidyModule> Module = E.instantiate();
350Module->addCheckFactories(*CheckFactories);
351}
352}
353
354#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
355static void
356setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
357clang::AnalyzerOptions &AnalyzerOptions) {
358StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
359for (const auto &Opt : Opts.CheckOptions) {
360StringRef OptName(Opt.getKey());
361if (!OptName.consume_front(AnalyzerPrefix))
362continue;
363// Analyzer options are always local options so we can ignore priority.
364AnalyzerOptions.Config[OptName] = Opt.getValue().Value;
365}
366}
367
368using CheckersList = std::vector<std::pair<std::string, bool>>;
369
370static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
371bool IncludeExperimental) {
372CheckersList List;
373
374const auto &RegisteredCheckers =
375AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
376const bool AnalyzerChecksEnabled =
377llvm::any_of(RegisteredCheckers, [&](StringRef CheckName) -> bool {
378return Context.isCheckEnabled(
379(AnalyzerCheckNamePrefix + CheckName).str());
380});
381
382if (!AnalyzerChecksEnabled)
383return List;
384
385// List all static analyzer checkers that our filter enables.
386//
387// Always add all core checkers if any other static analyzer check is enabled.
388// This is currently necessary, as other path sensitive checks rely on the
389// core checkers.
390for (StringRef CheckName : RegisteredCheckers) {
391std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
392
393if (CheckName.starts_with("core") ||
394Context.isCheckEnabled(ClangTidyCheckName)) {
395List.emplace_back(std::string(CheckName), true);
396}
397}
398return List;
399}
400#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
401
402std::unique_ptr<clang::ASTConsumer>
403ClangTidyASTConsumerFactory::createASTConsumer(
404clang::CompilerInstance &Compiler, StringRef File) {
405// FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
406// modify Compiler.
407SourceManager *SM = &Compiler.getSourceManager();
408Context.setSourceManager(SM);
409Context.setCurrentFile(File);
410Context.setASTContext(&Compiler.getASTContext());
411
412auto WorkingDir = Compiler.getSourceManager()
413.getFileManager()
414.getVirtualFileSystem()
415.getCurrentWorkingDirectory();
416if (WorkingDir)
417Context.setCurrentBuildDirectory(WorkingDir.get());
418
419std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
420CheckFactories->createChecksForLanguage(&Context);
421
422ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
423
424std::unique_ptr<ClangTidyProfiling> Profiling;
425if (Context.getEnableProfiling()) {
426Profiling = std::make_unique<ClangTidyProfiling>(
427Context.getProfileStorageParams());
428FinderOptions.CheckProfiling.emplace(Profiling->Records);
429}
430
431std::unique_ptr<ast_matchers::MatchFinder> Finder(
432new ast_matchers::MatchFinder(std::move(FinderOptions)));
433
434Preprocessor *PP = &Compiler.getPreprocessor();
435Preprocessor *ModuleExpanderPP = PP;
436
437if (Context.canEnableModuleHeadersParsing() &&
438Context.getLangOpts().Modules && OverlayFS != nullptr) {
439auto ModuleExpander = std::make_unique<ExpandModularHeadersPPCallbacks>(
440&Compiler, OverlayFS);
441ModuleExpanderPP = ModuleExpander->getPreprocessor();
442PP->addPPCallbacks(std::move(ModuleExpander));
443}
444
445for (auto &Check : Checks) {
446Check->registerMatchers(&*Finder);
447Check->registerPPCallbacks(*SM, PP, ModuleExpanderPP);
448}
449
450std::vector<std::unique_ptr<ASTConsumer>> Consumers;
451if (!Checks.empty())
452Consumers.push_back(Finder->newASTConsumer());
453
454#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
455AnalyzerOptions &AnalyzerOptions = Compiler.getAnalyzerOpts();
456AnalyzerOptions.CheckersAndPackages = getAnalyzerCheckersAndPackages(
457Context, Context.canEnableAnalyzerAlphaCheckers());
458if (!AnalyzerOptions.CheckersAndPackages.empty()) {
459setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
460AnalyzerOptions.AnalysisDiagOpt = PD_NONE;
461AnalyzerOptions.eagerlyAssumeBinOpBifurcation = true;
462std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
463ento::CreateAnalysisConsumer(Compiler);
464AnalysisConsumer->AddDiagnosticConsumer(
465new AnalyzerDiagnosticConsumer(Context));
466Consumers.push_back(std::move(AnalysisConsumer));
467}
468#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
469return std::make_unique<ClangTidyASTConsumer>(
470std::move(Consumers), std::move(Profiling), std::move(Finder),
471std::move(Checks));
472}
473
474std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
475std::vector<std::string> CheckNames;
476for (const auto &CheckFactory : *CheckFactories) {
477if (Context.isCheckEnabled(CheckFactory.getKey()))
478CheckNames.emplace_back(CheckFactory.getKey());
479}
480
481#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
482for (const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
483Context, Context.canEnableAnalyzerAlphaCheckers()))
484CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
485#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
486
487llvm::sort(CheckNames);
488return CheckNames;
489}
490
491ClangTidyOptions::OptionMap ClangTidyASTConsumerFactory::getCheckOptions() {
492ClangTidyOptions::OptionMap Options;
493std::vector<std::unique_ptr<ClangTidyCheck>> Checks =
494CheckFactories->createChecks(&Context);
495for (const auto &Check : Checks)
496Check->storeOptions(Options);
497return Options;
498}
499
500std::vector<std::string>
501getCheckNames(const ClangTidyOptions &Options,
502bool AllowEnablingAnalyzerAlphaCheckers) {
503clang::tidy::ClangTidyContext Context(
504std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
505Options),
506AllowEnablingAnalyzerAlphaCheckers);
507ClangTidyASTConsumerFactory Factory(Context);
508return Factory.getCheckNames();
509}
510
511ClangTidyOptions::OptionMap
512getCheckOptions(const ClangTidyOptions &Options,
513bool AllowEnablingAnalyzerAlphaCheckers) {
514clang::tidy::ClangTidyContext Context(
515std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
516Options),
517AllowEnablingAnalyzerAlphaCheckers);
518ClangTidyDiagnosticConsumer DiagConsumer(Context);
519DiagnosticsEngine DE(llvm::makeIntrusiveRefCnt<DiagnosticIDs>(),
520llvm::makeIntrusiveRefCnt<DiagnosticOptions>(),
521&DiagConsumer, /*ShouldOwnClient=*/false);
522Context.setDiagnosticsEngine(&DE);
523ClangTidyASTConsumerFactory Factory(Context);
524return Factory.getCheckOptions();
525}
526
527std::vector<ClangTidyError>
528runClangTidy(clang::tidy::ClangTidyContext &Context,
529const CompilationDatabase &Compilations,
530ArrayRef<std::string> InputFiles,
531llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
532bool ApplyAnyFix, bool EnableCheckProfile,
533llvm::StringRef StoreCheckProfile) {
534ClangTool Tool(Compilations, InputFiles,
535std::make_shared<PCHContainerOperations>(), BaseFS);
536
537// Add extra arguments passed by the clang-tidy command-line.
538ArgumentsAdjuster PerFileExtraArgumentsInserter =
539[&Context](const CommandLineArguments &Args, StringRef Filename) {
540ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
541CommandLineArguments AdjustedArgs = Args;
542if (Opts.ExtraArgsBefore) {
543auto I = AdjustedArgs.begin();
544if (I != AdjustedArgs.end() && !StringRef(*I).starts_with("-"))
545++I; // Skip compiler binary name, if it is there.
546AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
547Opts.ExtraArgsBefore->end());
548}
549if (Opts.ExtraArgs)
550AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
551Opts.ExtraArgs->end());
552return AdjustedArgs;
553};
554
555Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
556Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
557Context.setEnableProfiling(EnableCheckProfile);
558Context.setProfileStoragePrefix(StoreCheckProfile);
559
560ClangTidyDiagnosticConsumer DiagConsumer(Context, nullptr, true, ApplyAnyFix);
561DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions(),
562&DiagConsumer, /*ShouldOwnClient=*/false);
563Context.setDiagnosticsEngine(&DE);
564Tool.setDiagnosticConsumer(&DiagConsumer);
565
566class ActionFactory : public FrontendActionFactory {
567public:
568ActionFactory(ClangTidyContext &Context,
569IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS)
570: ConsumerFactory(Context, std::move(BaseFS)) {}
571std::unique_ptr<FrontendAction> create() override {
572return std::make_unique<Action>(&ConsumerFactory);
573}
574
575bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
576FileManager *Files,
577std::shared_ptr<PCHContainerOperations> PCHContainerOps,
578DiagnosticConsumer *DiagConsumer) override {
579// Explicitly ask to define __clang_analyzer__ macro.
580Invocation->getPreprocessorOpts().SetUpStaticAnalyzer = true;
581return FrontendActionFactory::runInvocation(
582Invocation, Files, PCHContainerOps, DiagConsumer);
583}
584
585private:
586class Action : public ASTFrontendAction {
587public:
588Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
589std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
590StringRef File) override {
591return Factory->createASTConsumer(Compiler, File);
592}
593
594private:
595ClangTidyASTConsumerFactory *Factory;
596};
597
598ClangTidyASTConsumerFactory ConsumerFactory;
599};
600
601ActionFactory Factory(Context, std::move(BaseFS));
602Tool.run(&Factory);
603return DiagConsumer.take();
604}
605
606void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
607ClangTidyContext &Context, FixBehaviour Fix,
608unsigned &WarningsAsErrorsCount,
609llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
610ErrorReporter Reporter(Context, Fix, std::move(BaseFS));
611llvm::vfs::FileSystem &FileSystem =
612Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
613auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
614if (!InitialWorkingDir)
615llvm::report_fatal_error("Cannot get current working path.");
616
617for (const ClangTidyError &Error : Errors) {
618if (!Error.BuildDirectory.empty()) {
619// By default, the working directory of file system is the current
620// clang-tidy running directory.
621//
622// Change the directory to the one used during the analysis.
623FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
624}
625Reporter.reportDiagnostic(Error);
626// Return to the initial directory to correctly resolve next Error.
627FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
628}
629Reporter.finish();
630WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
631}
632
633void exportReplacements(const llvm::StringRef MainFilePath,
634const std::vector<ClangTidyError> &Errors,
635raw_ostream &OS) {
636TranslationUnitDiagnostics TUD;
637TUD.MainSourceFile = std::string(MainFilePath);
638for (const auto &Error : Errors) {
639tooling::Diagnostic Diag = Error;
640if (Error.IsWarningAsError)
641Diag.DiagLevel = tooling::Diagnostic::Error;
642TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
643}
644
645yaml::Output YAML(OS);
646YAML << TUD;
647}
648
649NamesAndOptions
650getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers) {
651NamesAndOptions Result;
652ClangTidyOptions Opts;
653Opts.Checks = "*";
654clang::tidy::ClangTidyContext Context(
655std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(), Opts),
656AllowEnablingAnalyzerAlphaCheckers);
657ClangTidyCheckFactories Factories;
658for (const ClangTidyModuleRegistry::entry &Module :
659ClangTidyModuleRegistry::entries()) {
660Module.instantiate()->addCheckFactories(Factories);
661}
662
663for (const auto &Factory : Factories)
664Result.Names.insert(Factory.getKey());
665
666#if CLANG_TIDY_ENABLE_STATIC_ANALYZER
667SmallString<64> Buffer(AnalyzerCheckNamePrefix);
668size_t DefSize = Buffer.size();
669for (const auto &AnalyzerCheck : AnalyzerOptions::getRegisteredCheckers(
670AllowEnablingAnalyzerAlphaCheckers)) {
671Buffer.truncate(DefSize);
672Buffer.append(AnalyzerCheck);
673Result.Names.insert(Buffer);
674}
675#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
676
677Context.setOptionsCollector(&Result.Options);
678for (const auto &Factory : Factories) {
679Factory.getValue()(Factory.getKey(), &Context);
680}
681
682return Result;
683}
684} // namespace clang::tidy
685