23 #include "clang/AST/ASTConsumer.h"
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/Decl.h"
26 #include "clang/ASTMatchers/ASTMatchFinder.h"
27 #include "clang/Config/config.h"
28 #include "clang/Format/Format.h"
29 #include "clang/Frontend/ASTConsumers.h"
30 #include "clang/Frontend/CompilerInstance.h"
31 #include "clang/Frontend/FrontendActions.h"
32 #include "clang/Frontend/FrontendDiagnostic.h"
33 #include "clang/Frontend/MultiplexConsumer.h"
34 #include "clang/Frontend/TextDiagnosticPrinter.h"
35 #include "clang/Lex/PPCallbacks.h"
36 #include "clang/Lex/Preprocessor.h"
37 #include "clang/Lex/PreprocessorOptions.h"
38 #include "clang/Rewrite/Frontend/FixItRewriter.h"
39 #include "clang/Rewrite/Frontend/FrontendActions.h"
40 #include "clang/Tooling/Core/Diagnostic.h"
41 #include "clang/Tooling/DiagnosticsYaml.h"
42 #include "clang/Tooling/Refactoring.h"
43 #include "clang/Tooling/ReplacementsYaml.h"
44 #include "clang/Tooling/Tooling.h"
45 #include "llvm/Support/Process.h"
46 #include "llvm/Support/Signals.h"
50 #if CLANG_ENABLE_STATIC_ANALYZER
51 #include "clang/Analysis/PathDiagnostic.h"
52 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
53 #endif // CLANG_ENABLE_STATIC_ANALYZER
56 using namespace clang::driver;
66 #if CLANG_ENABLE_STATIC_ANALYZER
67 static const char *AnalyzerCheckNamePrefix =
"clang-analyzer-";
69 class AnalyzerDiagnosticConsumer :
public ento::PathDiagnosticConsumer {
71 AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
73 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &
Diags,
74 FilesMade *filesMade)
override {
75 for (
const ento::PathDiagnostic *PD :
Diags) {
76 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
77 CheckName += PD->getCheckerName();
78 Context.diag(CheckName, PD->getLocation().asLocation(),
79 PD->getShortDescription())
80 << PD->path.back()->getRanges();
82 for (
const auto &DiagPiece :
83 PD->path.flatten(
true)) {
84 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
85 DiagPiece->getString(), DiagnosticIDs::Note)
86 << DiagPiece->getRanges();
91 StringRef getName()
const override {
return "ClangTidyDiags"; }
92 bool supportsLogicalOpControlFlow()
const override {
return true; }
93 bool supportsCrossFileDiagnostics()
const override {
return true; }
96 ClangTidyContext &Context;
98 #endif // CLANG_ENABLE_STATIC_ANALYZER
100 class ErrorReporter {
102 ErrorReporter(ClangTidyContext &Context,
bool ApplyFixes,
103 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS)
104 : Files(FileSystemOptions(), BaseFS), DiagOpts(new DiagnosticOptions()),
105 DiagPrinter(new TextDiagnosticPrinter(
llvm::outs(), &*DiagOpts)),
106 Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
108 SourceMgr(
Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
110 DiagOpts->ShowColors = Context.getOptions().UseColor.getValueOr(
111 llvm::sys::Process::StandardOutHasColors());
112 DiagPrinter->BeginSourceFile(LangOpts);
115 SourceManager &getSourceManager() {
return SourceMgr; }
117 void reportDiagnostic(
const ClangTidyError &Error) {
118 const tooling::DiagnosticMessage &
Message =
Error.Message;
122 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
124 auto Level = static_cast<DiagnosticsEngine::Level>(
Error.DiagLevel);
126 if (!
Error.EnabledDiagnosticAliases.empty())
128 if (
Error.IsWarningAsError) {
129 Name +=
",-warnings-as-errors";
133 auto Diag =
Diags.Report(
Loc,
Diags.getCustomDiagID(Level,
"%0 [%1]"))
136 const llvm::StringMap<Replacements> *ChosenFix = selectFirstFix(Error);
137 if (ApplyFixes && ChosenFix) {
138 for (
const auto &FileAndReplacements : *ChosenFix) {
139 for (
const auto &Repl : FileAndReplacements.second) {
141 bool CanBeApplied =
false;
142 if (!Repl.isApplicable())
144 SourceLocation FixLoc;
145 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
146 Files.makeAbsolutePath(FixAbsoluteFilePath);
147 tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
148 Repl.getLength(), Repl.getReplacementText());
149 Replacements &Replacements = FileReplacements[R.getFilePath()];
150 llvm::Error Err = Replacements.add(R);
153 llvm::errs() <<
"Trying to resolve conflict: "
156 Replacements.getShiftedCodePosition(R.getOffset());
157 unsigned NewLength = Replacements.getShiftedCodePosition(
158 R.getOffset() + R.getLength()) -
160 if (NewLength == R.getLength()) {
161 R = Replacement(R.getFilePath(), NewOffset, NewLength,
162 R.getReplacementText());
163 Replacements = Replacements.merge(tooling::Replacements(R));
168 <<
"Can't resolve conflict, skipping the replacement.\n";
174 FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
175 FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
179 reportFix(Diag,
Error.Message.Fix);
181 for (
auto Fix : FixLocations) {
182 Diags.Report(
Fix.first,
Fix.second ? diag::note_fixit_applied
183 : diag::note_fixit_failed);
185 for (
const auto &Note :
Error.Notes)
190 if (ApplyFixes && TotalFixes > 0) {
192 for (
const auto &FileAndReplacements : FileReplacements) {
193 StringRef File = FileAndReplacements.first();
194 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
195 SourceMgr.getFileManager().getBufferForFile(File);
197 llvm::errs() <<
"Can't get buffer for file " << File <<
": "
198 << Buffer.getError().message() <<
"\n";
202 StringRef
Code = Buffer.get()->getBuffer();
203 auto Style = format::getStyle(
204 *Context.getOptionsForFile(File).FormatStyle, File,
"none");
209 llvm::Expected<tooling::Replacements> Replacements =
210 format::cleanupAroundReplacements(
Code, FileAndReplacements.second,
216 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
217 format::formatReplacements(
Code, *Replacements, *Style)) {
218 Replacements = std::move(FormattedReplacements);
220 llvm_unreachable(
"!Replacements");
223 <<
". Skipping formatting.\n";
225 if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) {
226 llvm::errs() <<
"Can't apply replacements for file " << File <<
"\n";
229 if (Rewrite.overwriteChangedFiles()) {
230 llvm::errs() <<
"clang-tidy failed to apply suggested fixes.\n";
232 llvm::errs() <<
"clang-tidy applied " << AppliedFixes <<
" of "
233 << TotalFixes <<
" suggested fixes.\n";
241 SourceLocation getLocation(StringRef FilePath,
unsigned Offset) {
242 if (FilePath.empty())
243 return SourceLocation();
245 auto File =
SourceMgr.getFileManager().getFile(FilePath);
247 return SourceLocation();
249 FileID ID =
SourceMgr.getOrCreateFileID(*File, SrcMgr::C_User);
253 void reportFix(
const DiagnosticBuilder &Diag,
254 const llvm::StringMap<Replacements> &
Fix) {
255 for (
const auto &FileAndReplacements :
Fix) {
256 for (
const auto &Repl : FileAndReplacements.second) {
257 if (!Repl.isApplicable())
259 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
260 Files.makeAbsolutePath(FixAbsoluteFilePath);
261 SourceLocation FixLoc =
262 getLocation(FixAbsoluteFilePath, Repl.getOffset());
263 SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Repl.getLength());
267 CharSourceRange
Range =
268 CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
269 Diag << FixItHint::CreateReplacement(
Range, Repl.getReplacementText());
274 void reportNote(
const tooling::DiagnosticMessage &
Message) {
277 Diags.Report(
Loc,
Diags.getCustomDiagID(DiagnosticsEngine::Note,
"%0"))
283 LangOptions LangOpts;
284 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
286 DiagnosticsEngine
Diags;
288 llvm::StringMap<Replacements> FileReplacements;
289 ClangTidyContext &Context;
292 unsigned AppliedFixes;
298 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
299 std::unique_ptr<ClangTidyProfiling> Profiling,
300 std::unique_ptr<ast_matchers::MatchFinder> Finder,
301 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks)
303 Profiling(std::move(Profiling)), Finder(std::move(Finder)),
309 std::unique_ptr<ClangTidyProfiling> Profiling;
310 std::unique_ptr<ast_matchers::MatchFinder> Finder;
311 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
316 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
318 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
319 : Context(Context), OverlayFS(OverlayFS),
321 for (ClangTidyModuleRegistry::entry
E : ClangTidyModuleRegistry::entries()) {
322 std::unique_ptr<ClangTidyModule> Module =
E.instantiate();
323 Module->addCheckFactories(*CheckFactories);
327 #if CLANG_ENABLE_STATIC_ANALYZER
329 AnalyzerOptionsRef AnalyzerOptions) {
330 StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
331 for (
const auto &Opt : Opts.CheckOptions) {
332 StringRef OptName(Opt.first);
333 if (!OptName.startswith(AnalyzerPrefix))
336 AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] =
341 typedef std::vector<std::pair<std::string, bool>> CheckersList;
343 static CheckersList getAnalyzerCheckersAndPackages(ClangTidyContext &Context,
344 bool IncludeExperimental) {
347 const auto &RegisteredCheckers =
348 AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
349 bool AnalyzerChecksEnabled =
false;
350 for (StringRef CheckName : RegisteredCheckers) {
351 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
352 AnalyzerChecksEnabled |= Context.isCheckEnabled(ClangTidyCheckName);
355 if (!AnalyzerChecksEnabled)
363 for (StringRef CheckName : RegisteredCheckers) {
364 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
366 if (CheckName.startswith(
"core") ||
367 Context.isCheckEnabled(ClangTidyCheckName)) {
368 List.emplace_back(std::string(CheckName),
true);
373 #endif // CLANG_ENABLE_STATIC_ANALYZER
375 std::unique_ptr<clang::ASTConsumer>
377 clang::CompilerInstance &Compiler, StringRef File) {
380 SourceManager *SM = &Compiler.getSourceManager();
381 Context.setSourceManager(SM);
382 Context.setCurrentFile(File);
383 Context.setASTContext(&Compiler.getASTContext());
385 auto WorkingDir = Compiler.getSourceManager()
387 .getVirtualFileSystem()
388 .getCurrentWorkingDirectory();
390 Context.setCurrentBuildDirectory(WorkingDir.get());
392 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks =
393 CheckFactories->createChecks(&Context);
395 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
397 std::unique_ptr<ClangTidyProfiling> Profiling;
398 if (Context.getEnableProfiling()) {
399 Profiling = std::make_unique<ClangTidyProfiling>(
400 Context.getProfileStorageParams());
401 FinderOptions.CheckProfiling.emplace(Profiling->Records);
404 std::unique_ptr<ast_matchers::MatchFinder> Finder(
405 new ast_matchers::MatchFinder(std::move(FinderOptions)));
407 Preprocessor *
PP = &Compiler.getPreprocessor();
408 Preprocessor *ModuleExpanderPP =
PP;
410 if (Context.getLangOpts().Modules && OverlayFS !=
nullptr) {
411 auto ModuleExpander = std::make_unique<ExpandModularHeadersPPCallbacks>(
412 &Compiler, OverlayFS);
413 ModuleExpanderPP = ModuleExpander->getPreprocessor();
414 PP->addPPCallbacks(std::move(ModuleExpander));
417 for (
auto &Check :
Checks) {
418 if (!Check->isLanguageVersionSupported(Context.getLangOpts()))
420 Check->registerMatchers(&*Finder);
421 Check->registerPPCallbacks(*SM,
PP, ModuleExpanderPP);
424 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
426 Consumers.push_back(Finder->newASTConsumer());
428 #if CLANG_ENABLE_STATIC_ANALYZER
429 AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
430 AnalyzerOptions->CheckersAndPackages = getAnalyzerCheckersAndPackages(
431 Context, Context.canEnableAnalyzerAlphaCheckers());
432 if (!AnalyzerOptions->CheckersAndPackages.empty()) {
433 setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
434 AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
435 AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
436 AnalyzerOptions->AnalyzeNestedBlocks =
true;
437 AnalyzerOptions->eagerlyAssumeBinOpBifurcation =
true;
438 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
439 ento::CreateAnalysisConsumer(Compiler);
440 AnalysisConsumer->AddDiagnosticConsumer(
441 new AnalyzerDiagnosticConsumer(Context));
442 Consumers.push_back(std::move(AnalysisConsumer));
444 #endif // CLANG_ENABLE_STATIC_ANALYZER
445 return std::make_unique<ClangTidyASTConsumer>(
446 std::move(Consumers), std::move(Profiling), std::move(Finder),
451 std::vector<std::string> CheckNames;
452 for (
const auto &CheckFactory : *CheckFactories) {
453 if (Context.isCheckEnabled(CheckFactory.first))
454 CheckNames.push_back(CheckFactory.first);
457 #if CLANG_ENABLE_STATIC_ANALYZER
458 for (
const auto &AnalyzerCheck : getAnalyzerCheckersAndPackages(
459 Context, Context.canEnableAnalyzerAlphaCheckers()))
460 CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
461 #endif // CLANG_ENABLE_STATIC_ANALYZER
463 llvm::sort(CheckNames);
469 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks =
470 CheckFactories->createChecks(&Context);
471 for (
const auto &Check :
Checks)
472 Check->storeOptions(Options);
476 std::vector<std::string>
498 std::vector<ClangTidyError>
500 const CompilationDatabase &Compilations,
501 ArrayRef<std::string> InputFiles,
502 llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
504 ClangTool Tool(Compilations, InputFiles,
505 std::make_shared<PCHContainerOperations>(), BaseFS);
508 ArgumentsAdjuster PerFileExtraArgumentsInserter =
509 [&Context](
const CommandLineArguments &Args, StringRef
Filename) {
511 CommandLineArguments AdjustedArgs = Args;
512 if (Opts.ExtraArgsBefore) {
513 auto I = AdjustedArgs.begin();
514 if (I != AdjustedArgs.end() && !StringRef(*I).startswith(
"-"))
516 AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
517 Opts.ExtraArgsBefore->end());
520 AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
521 Opts.ExtraArgs->end());
525 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
526 Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
531 DiagnosticsEngine DE(
new DiagnosticIDs(),
new DiagnosticOptions(),
532 &DiagConsumer,
false);
533 Context.setDiagnosticsEngine(&DE);
534 Tool.setDiagnosticConsumer(&DiagConsumer);
536 class ActionFactory :
public FrontendActionFactory {
539 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS)
540 : ConsumerFactory(Context, BaseFS) {}
541 std::unique_ptr<FrontendAction> create()
override {
542 return std::make_unique<Action>(&ConsumerFactory);
545 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
547 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
550 Invocation->getPreprocessorOpts().SetUpStaticAnalyzer =
true;
551 return FrontendActionFactory::runInvocation(
552 Invocation, Files, PCHContainerOps, DiagConsumer);
556 class Action :
public ASTFrontendAction {
559 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
560 StringRef File)
override {
571 ActionFactory Factory(Context, BaseFS);
573 return DiagConsumer.
take();
578 unsigned &WarningsAsErrorsCount,
579 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
580 ErrorReporter Reporter(Context,
Fix, BaseFS);
581 llvm::vfs::FileSystem &FileSystem =
582 Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
583 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
584 if (!InitialWorkingDir)
585 llvm::report_fatal_error(
"Cannot get current working path.");
588 if (!Error.BuildDirectory.empty()) {
593 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
595 Reporter.reportDiagnostic(Error);
597 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
600 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
604 const std::vector<ClangTidyError> &Errors,
606 TranslationUnitDiagnostics TUD;
607 TUD.MainSourceFile = std::string(MainFilePath);
608 for (
const auto &Error : Errors) {
610 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);