22 #include "clang/AST/ASTConsumer.h" 23 #include "clang/AST/ASTContext.h" 24 #include "clang/AST/Decl.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/FrontendActions.h" 30 #include "clang/Frontend/FrontendDiagnostic.h" 31 #include "clang/Frontend/MultiplexConsumer.h" 32 #include "clang/Frontend/TextDiagnosticPrinter.h" 33 #include "clang/Lex/PPCallbacks.h" 34 #include "clang/Lex/Preprocessor.h" 35 #include "clang/Rewrite/Frontend/FixItRewriter.h" 36 #include "clang/Rewrite/Frontend/FrontendActions.h" 37 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 38 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" 39 #include "clang/Tooling/DiagnosticsYaml.h" 40 #include "clang/Tooling/Refactoring.h" 41 #include "clang/Tooling/ReplacementsYaml.h" 42 #include "clang/Tooling/Tooling.h" 43 #include "llvm/Support/Process.h" 44 #include "llvm/Support/Signals.h" 59 static const char *AnalyzerCheckNamePrefix =
"clang-analyzer-";
61 class AnalyzerDiagnosticConsumer :
public ento::PathDiagnosticConsumer {
63 AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
65 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
66 FilesMade *filesMade)
override {
67 for (
const ento::PathDiagnostic *PD : Diags) {
68 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
69 CheckName += PD->getCheckName();
70 Context.diag(CheckName, PD->getLocation().asLocation(),
71 PD->getShortDescription())
72 << PD->path.back()->getRanges();
74 for (
const auto &DiagPiece :
75 PD->path.flatten(
true)) {
76 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
77 DiagPiece->getString(), DiagnosticIDs::Note)
78 << DiagPiece->getRanges();
83 StringRef getName()
const override {
return "ClangTidyDiags"; }
84 bool supportsLogicalOpControlFlow()
const override {
return true; }
85 bool supportsCrossFileDiagnostics()
const override {
return true; }
88 ClangTidyContext &Context;
93 ErrorReporter(ClangTidyContext &Context,
bool ApplyFixes,
94 llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS)
95 : Files(FileSystemOptions(), BaseFS), DiagOpts(new DiagnosticOptions()),
96 DiagPrinter(new TextDiagnosticPrinter(
llvm::outs(), &*DiagOpts)),
97 Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
99 SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
101 DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
102 DiagPrinter->BeginSourceFile(LangOpts);
105 SourceManager &getSourceManager() {
return SourceMgr; }
107 void reportDiagnostic(
const ClangTidyError &Error) {
108 const tooling::DiagnosticMessage &
Message = Error.Message;
109 SourceLocation
Loc = getLocation(Message.FilePath, Message.FileOffset);
112 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
114 auto Level =
static_cast<DiagnosticsEngine::Level
>(Error.DiagLevel);
115 std::string
Name = Error.DiagnosticName;
116 if (Error.IsWarningAsError) {
117 Name +=
",-warnings-as-errors";
118 Level = DiagnosticsEngine::Error;
121 auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level,
"%0 [%1]"))
122 << Message.Message << Name;
123 for (
const auto &FileAndReplacements : Error.Fix) {
124 for (
const auto &Repl : FileAndReplacements.second) {
129 SourceLocation FixLoc;
131 bool CanBeApplied =
false;
132 if (Repl.isApplicable()) {
133 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
134 Files.makeAbsolutePath(FixAbsoluteFilePath);
136 tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
138 Repl.getReplacementText());
139 Replacements &Replacements = FileReplacements[R.getFilePath()];
140 llvm::Error Err = Replacements.add(R);
143 llvm::errs() <<
"Trying to resolve conflict: " 146 Replacements.getShiftedCodePosition(R.getOffset());
147 unsigned NewLength = Replacements.getShiftedCodePosition(
148 R.getOffset() + R.getLength()) -
150 if (NewLength == R.getLength()) {
151 R = Replacement(R.getFilePath(), NewOffset, NewLength,
152 R.getReplacementText());
153 Replacements = Replacements.merge(tooling::Replacements(R));
158 <<
"Can't resolve conflict, skipping the replacement.\n";
166 FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
167 SourceLocation FixEndLoc =
168 FixLoc.getLocWithOffset(Repl.getLength());
169 Range = SourceRange(FixLoc, FixEndLoc);
170 Diag << FixItHint::CreateReplacement(Range,
171 Repl.getReplacementText());
175 FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
179 for (
auto Fix : FixLocations) {
180 Diags.Report(
Fix.first,
Fix.second ? diag::note_fixit_applied
181 : diag::note_fixit_failed);
183 for (
const auto &Note : Error.Notes)
188 if (ApplyFixes && TotalFixes > 0) {
189 Rewriter Rewrite(SourceMgr, LangOpts);
190 for (
const auto &FileAndReplacements : FileReplacements) {
191 StringRef
File = FileAndReplacements.first();
192 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
193 SourceMgr.getFileManager().getBufferForFile(File);
195 llvm::errs() <<
"Can't get buffer for file " << File <<
": " 196 << Buffer.getError().message() <<
"\n";
200 StringRef Code = Buffer.get()->getBuffer();
201 auto Style = format::getStyle(
202 *Context.getOptionsForFile(File).FormatStyle, File,
"none");
207 llvm::Expected<tooling::Replacements> Replacements =
208 format::cleanupAroundReplacements(Code, FileAndReplacements.second,
214 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
215 format::formatReplacements(Code, *Replacements, *Style)) {
216 Replacements = std::move(FormattedReplacements);
218 llvm_unreachable(
"!Replacements");
221 <<
". Skipping formatting.\n";
223 if (!tooling::applyAllReplacements(Replacements.get(), Rewrite)) {
224 llvm::errs() <<
"Can't apply replacements for file " << File <<
"\n";
227 if (Rewrite.overwriteChangedFiles()) {
228 llvm::errs() <<
"clang-tidy failed to apply suggested fixes.\n";
230 llvm::errs() <<
"clang-tidy applied " << AppliedFixes <<
" of " 231 << TotalFixes <<
" suggested fixes.\n";
239 SourceLocation getLocation(StringRef FilePath,
unsigned Offset) {
240 if (FilePath.empty())
241 return SourceLocation();
243 const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
244 FileID ID = SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
245 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
248 void reportNote(
const tooling::DiagnosticMessage &Message) {
249 SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
250 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note,
"%0"))
255 LangOptions LangOpts;
256 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
258 DiagnosticsEngine Diags;
259 SourceManager SourceMgr;
260 llvm::StringMap<Replacements> FileReplacements;
261 ClangTidyContext &Context;
264 unsigned AppliedFixes;
270 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
271 std::unique_ptr<ClangTidyProfiling> Profiling,
272 std::unique_ptr<ast_matchers::MatchFinder> Finder,
273 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks)
275 Profiling(std::move(Profiling)), Finder(std::move(Finder)),
281 std::unique_ptr<ClangTidyProfiling> Profiling;
282 std::unique_ptr<ast_matchers::MatchFinder> Finder;
283 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
288 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
291 for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
292 E = ClangTidyModuleRegistry::end();
294 std::unique_ptr<ClangTidyModule> Module(I->instantiate());
295 Module->addCheckFactories(*CheckFactories);
300 AnalyzerOptionsRef AnalyzerOptions) {
301 StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
303 StringRef OptName(Opt.first);
304 if (!OptName.startswith(AnalyzerPrefix))
306 AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
313 bool IncludeExperimental) {
316 const auto &RegisteredCheckers =
317 AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
318 bool AnalyzerChecksEnabled =
false;
319 for (StringRef CheckName : RegisteredCheckers) {
320 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
321 AnalyzerChecksEnabled |= Context.
isCheckEnabled(ClangTidyCheckName);
324 if (!AnalyzerChecksEnabled)
332 for (StringRef CheckName : RegisteredCheckers) {
333 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
335 if (CheckName.startswith(
"core") ||
337 List.emplace_back(CheckName,
true);
343 std::unique_ptr<clang::ASTConsumer>
345 clang::CompilerInstance &Compiler, StringRef File) {
348 Context.setSourceManager(&Compiler.getSourceManager());
349 Context.setCurrentFile(File);
350 Context.setASTContext(&Compiler.getASTContext());
352 auto WorkingDir = Compiler.getSourceManager()
354 .getVirtualFileSystem()
355 ->getCurrentWorkingDirectory();
357 Context.setCurrentBuildDirectory(WorkingDir.get());
359 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
360 CheckFactories->createChecks(&Context, Checks);
362 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
364 std::unique_ptr<ClangTidyProfiling> Profiling;
365 if (Context.getEnableProfiling()) {
366 Profiling = llvm::make_unique<ClangTidyProfiling>(
367 Context.getProfileStorageParams());
368 FinderOptions.CheckProfiling.emplace(Profiling->Records);
371 std::unique_ptr<ast_matchers::MatchFinder> Finder(
372 new ast_matchers::MatchFinder(std::move(FinderOptions)));
374 for (
auto &Check : Checks) {
375 Check->registerMatchers(&*Finder);
376 Check->registerPPCallbacks(Compiler);
379 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
381 Consumers.push_back(Finder->newASTConsumer());
383 AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
384 AnalyzerOptions->CheckersControlList =
386 if (!AnalyzerOptions->CheckersControlList.empty()) {
388 AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
389 AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
390 AnalyzerOptions->AnalyzeNestedBlocks =
true;
391 AnalyzerOptions->eagerlyAssumeBinOpBifurcation =
true;
392 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
393 ento::CreateAnalysisConsumer(Compiler);
394 AnalysisConsumer->AddDiagnosticConsumer(
395 new AnalyzerDiagnosticConsumer(Context));
396 Consumers.push_back(std::move(AnalysisConsumer));
398 return llvm::make_unique<ClangTidyASTConsumer>(
399 std::move(Consumers), std::move(Profiling), std::move(Finder),
404 std::vector<std::string> CheckNames;
405 for (
const auto &CheckFactory : *CheckFactories) {
406 if (Context.isCheckEnabled(CheckFactory.first))
407 CheckNames.push_back(CheckFactory.first);
411 Context, Context.canEnableAnalyzerAlphaCheckers()))
412 CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
414 std::sort(CheckNames.begin(), CheckNames.end());
420 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
421 CheckFactories->createChecks(&Context, Checks);
422 for (
const auto &Check : Checks)
423 Check->storeOptions(Options);
428 DiagnosticIDs::Level Level) {
429 return Context->diag(CheckName, Loc, Message, Level);
432 void ClangTidyCheck::run(
const ast_matchers::MatchFinder::MatchResult &Result) {
433 Context->setSourceManager(Result.SourceManager);
439 : NamePrefix(CheckName.str() +
"."), CheckOptions(CheckOptions) {}
442 const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
443 if (Iter != CheckOptions.end())
449 StringRef Default)
const {
450 auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
451 if (Iter != CheckOptions.end())
454 Iter = CheckOptions.find(LocalName.str());
455 if (Iter != CheckOptions.end())
461 StringRef LocalName, StringRef Value)
const {
462 Options[NamePrefix + LocalName.str()] = Value;
466 StringRef LocalName, int64_t Value)
const {
467 store(Options, LocalName, llvm::itostr(Value));
470 std::vector<std::string>
476 AllowEnablingAnalyzerAlphaCheckers);
487 AllowEnablingAnalyzerAlphaCheckers);
493 const CompilationDatabase &Compilations,
494 ArrayRef<std::string> InputFiles,
495 llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
497 ClangTool Tool(Compilations, InputFiles,
498 std::make_shared<PCHContainerOperations>(), BaseFS);
501 ArgumentsAdjuster PerFileExtraArgumentsInserter =
502 [&Context](
const CommandLineArguments &Args, StringRef
Filename) {
504 CommandLineArguments AdjustedArgs = Args;
506 auto I = AdjustedArgs.begin();
507 if (I != AdjustedArgs.end() && !StringRef(*I).startswith(
"-"))
513 AdjustedArgs.insert(AdjustedArgs.end(), Opts.
ExtraArgs->begin(),
519 ArgumentsAdjuster PluginArgumentsRemover =
520 [](
const CommandLineArguments &Args, StringRef
Filename) {
521 CommandLineArguments AdjustedArgs;
522 for (
size_t I = 0, E = Args.size(); I < E; ++I) {
523 if (I + 4 < Args.size() && Args[I] ==
"-Xclang" &&
524 (Args[I + 1] ==
"-load" || Args[I + 1] ==
"-add-plugin" ||
525 StringRef(Args[I + 1]).startswith(
"-plugin-arg-")) &&
526 Args[I + 2] ==
"-Xclang") {
529 AdjustedArgs.push_back(Args[I]);
534 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
535 Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
541 Tool.setDiagnosticConsumer(&DiagConsumer);
543 class ActionFactory :
public FrontendActionFactory {
546 FrontendAction *
create()
override {
return new Action(&ConsumerFactory); }
548 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
550 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
555 Invocation->getFrontendOpts().ProgramAction = frontend::RunAnalysis;
556 return FrontendActionFactory::runInvocation(
557 Invocation, Files, PCHContainerOps, DiagConsumer);
561 class Action :
public ASTFrontendAction {
564 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
565 StringRef File)
override {
576 ActionFactory Factory(Context);
581 unsigned &WarningsAsErrorsCount,
582 llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS) {
583 ErrorReporter Reporter(Context, Fix, BaseFS);
584 vfs::FileSystem &FileSystem =
585 *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
586 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
587 if (!InitialWorkingDir)
588 llvm::report_fatal_error(
"Cannot get current working path.");
591 if (!Error.BuildDirectory.empty()) {
596 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
598 Reporter.reportDiagnostic(Error);
600 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
603 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
607 const std::vector<ClangTidyError> &Errors,
609 TranslationUnitDiagnostics TUD;
610 TUD.MainSourceFile = MainFilePath;
611 for (
const auto &Error : Errors) {
612 tooling::Diagnostic Diag = Error;
613 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
616 yaml::Output
YAML(OS);
SourceLocation Loc
'#' location in the include directive
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Some operations such as code completion produce a set of candidates.
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
static CheckersList getCheckersControlList(ClangTidyContext &Context, bool IncludeExperimental)
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
static cl::opt< std::string > StoreCheckProfile("store-check-profile", cl::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))
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
static const StringRef Message
Contains options for clang-tidy.
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Returns the effective check-specific options.
A collection of ClangTidyCheckFactory instances.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
void handleErrors(ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
std::string Filename
Filename as a string.
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.
std::unique_ptr< Iterator > create(PostingListRef Documents)
Returns a document iterator over given PostingList.
llvm::unique_function< void()> Action
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
static cl::opt< std::string > WarningsAsErrors("warnings-as-errors", cl::desc(R"(
Upgrades warnings to errors. Same format as
'-checks'.
This option's value is appended to the value of
the 'WarningsAsErrors' option in .clang-tidy
file, if any.
)"), cl::init(""), cl::cat(ClangTidyCategory))
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError...
void runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< vfs::FileSystem > BaseFS, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile)
std::map< std::string, std::string > OptionMap
std::vector< std::pair< std::string, bool > > CheckersList
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
static cl::opt< bool > EnableCheckProfile("enable-check-profile", cl::desc(R"(
Enable per-check timing profiles, and print a
report to stderr.
)"), cl::init(false), cl::cat(ClangTidyCategory))
static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts, AnalyzerOptionsRef AnalyzerOptions)
ArrayRef< ClangTidyError > getErrors() const
Returns all collected errors.
static GeneratorRegistry::Add< YAMLGenerator > YAML(YAMLGenerator::Format, "Generator for YAML output.")
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Fills the list of check names that are enabled when the provided filters are applied.
CharSourceRange Range
SourceRange for the file name.
A detected error complete with information to display diagnostic and automatic fix.
static cl::opt< std::string > Checks("checks", cl::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))
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Registry< ClangTidyModule > ClangTidyModuleRegistry
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
static cl::opt< bool > Fix("fix", cl::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))
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.