21 #include "clang/AST/ASTConsumer.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Decl.h"
24 #include "clang/ASTMatchers/ASTMatchFinder.h"
25 #include "clang/Frontend/ASTConsumers.h"
26 #include "clang/Frontend/CompilerInstance.h"
27 #include "clang/Frontend/FrontendActions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
29 #include "clang/Frontend/MultiplexConsumer.h"
30 #include "clang/Frontend/TextDiagnosticPrinter.h"
31 #include "clang/Lex/PPCallbacks.h"
32 #include "clang/Lex/Preprocessor.h"
33 #include "clang/Rewrite/Frontend/FixItRewriter.h"
34 #include "clang/Rewrite/Frontend/FrontendActions.h"
35 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
36 #include "clang/Tooling/Refactoring.h"
37 #include "clang/Tooling/ReplacementsYaml.h"
38 #include "clang/Tooling/Tooling.h"
39 #include "llvm/Support/Process.h"
40 #include "llvm/Support/Signals.h"
44 using namespace clang::ast_matchers;
45 using namespace clang::driver;
46 using namespace clang::tooling;
49 template class llvm::Registry<clang::tidy::ClangTidyModule>;
55 static const char *AnalyzerCheckNamePrefix =
"clang-analyzer-";
57 static const StringRef StaticAnalyzerChecks[] = {
59 #define CHECKER(FULLNAME, CLASS, DESCFILE, HELPTEXT, GROUPINDEX, HIDDEN) \
61 #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
66 class AnalyzerDiagnosticConsumer :
public ento::PathDiagnosticConsumer {
68 AnalyzerDiagnosticConsumer(ClangTidyContext &
Context) : Context(Context) {}
70 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &
Diags,
71 FilesMade *filesMade)
override {
72 for (
const ento::PathDiagnostic *PD : Diags) {
73 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
74 CheckName += PD->getCheckName();
75 Context.diag(CheckName, PD->getLocation().asLocation(),
76 PD->getShortDescription())
77 << PD->path.back()->getRanges();
79 for (
const auto &DiagPiece :
80 PD->path.flatten(
true)) {
81 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
82 DiagPiece->getString(), DiagnosticIDs::Note)
83 << DiagPiece->getRanges();
88 StringRef getName()
const override {
return "ClangTidyDiags"; }
89 bool supportsLogicalOpControlFlow()
const override {
return true; }
90 bool supportsCrossFileDiagnostics()
const override {
return true; }
99 :
Files(FileSystemOptions()),
DiagOpts(
new DiagnosticOptions()),
101 Diags(IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs), &*
DiagOpts,
106 DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
110 SourceManager &getSourceManager() {
return SourceMgr; }
112 void reportDiagnostic(
const ClangTidyError &Error) {
113 const ClangTidyMessage &Message = Error.Message;
114 SourceLocation
Loc = getLocation(Message.FilePath, Message.FileOffset);
117 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
119 auto Level =
static_cast<DiagnosticsEngine::Level
>(Error.DiagLevel);
120 std::string
Name = Error.CheckName;
121 if (Error.IsWarningAsError) {
122 Name +=
",-warnings-as-errors";
123 Level = DiagnosticsEngine::Error;
126 auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level,
"%0 [%1]"))
127 << Message.Message << Name;
128 for (
const tooling::Replacement &
Fix : Error.Fix) {
133 SourceLocation FixLoc;
134 if (Fix.isApplicable()) {
135 SmallString<128> FixAbsoluteFilePath = Fix.getFilePath();
136 Files.makeAbsolutePath(FixAbsoluteFilePath);
137 FixLoc = getLocation(FixAbsoluteFilePath, Fix.getOffset());
138 SourceLocation FixEndLoc = FixLoc.getLocWithOffset(Fix.getLength());
139 Range = SourceRange(FixLoc, FixEndLoc);
140 Diag << FixItHint::CreateReplacement(Range, Fix.getReplacementText());
145 bool Success = Fix.isApplicable() && Fix.apply(
Rewrite);
148 FixLocations.push_back(std::make_pair(FixLoc, Success));
152 for (
auto Fix : FixLocations) {
153 Diags.Report(Fix.first, Fix.second ? diag::note_fixit_applied
154 : diag::note_fixit_failed);
156 for (
const ClangTidyMessage &Note : Error.Notes)
163 llvm::errs() <<
"clang-tidy applied " <<
AppliedFixes <<
" of "
165 Rewrite.overwriteChangedFiles();
172 SourceLocation getLocation(StringRef FilePath,
unsigned Offset) {
173 if (FilePath.empty())
174 return SourceLocation();
176 const FileEntry *
File =
SourceMgr.getFileManager().getFile(FilePath);
177 FileID ID =
SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
178 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
181 void reportNote(
const ClangTidyMessage &Message) {
182 SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
183 DiagnosticBuilder Diag =
184 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note,
"%0"))
203 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
204 std::unique_ptr<ast_matchers::MatchFinder>
Finder,
205 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks)
210 std::unique_ptr<ast_matchers::MatchFinder>
Finder;
211 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
216 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
219 for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
220 E = ClangTidyModuleRegistry::end();
222 std::unique_ptr<ClangTidyModule> Module(I->instantiate());
223 Module->addCheckFactories(*CheckFactories);
228 AnalyzerOptionsRef AnalyzerOptions) {
229 StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
231 StringRef OptName(Opt.first);
232 if (!OptName.startswith(AnalyzerPrefix))
234 AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
238 std::unique_ptr<clang::ASTConsumer>
240 clang::CompilerInstance &Compiler, StringRef File) {
247 auto WorkingDir = Compiler.getSourceManager()
249 .getVirtualFileSystem()
250 ->getCurrentWorkingDirectory();
254 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
255 CheckFactories->createChecks(&Context, Checks);
257 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
259 FinderOptions.CheckProfiling.emplace(P->Records);
261 std::unique_ptr<ast_matchers::MatchFinder>
Finder(
262 new ast_matchers::MatchFinder(std::move(FinderOptions)));
264 for (
auto &
Check : Checks) {
265 Check->registerMatchers(&*Finder);
266 Check->registerPPCallbacks(Compiler);
269 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
271 Consumers.push_back(Finder->newASTConsumer());
273 AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
276 AnalyzerOptions->Config[
"cfg-temporary-dtors"] =
280 AnalyzerOptions->CheckersControlList = getCheckersControlList(Filter);
281 if (!AnalyzerOptions->CheckersControlList.empty()) {
283 AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
284 AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
285 AnalyzerOptions->AnalyzeNestedBlocks =
true;
286 AnalyzerOptions->eagerlyAssumeBinOpBifurcation =
true;
287 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
288 ento::CreateAnalysisConsumer(Compiler);
289 AnalysisConsumer->AddDiagnosticConsumer(
290 new AnalyzerDiagnosticConsumer(Context));
291 Consumers.push_back(std::move(AnalysisConsumer));
293 return llvm::make_unique<ClangTidyASTConsumer>(
294 std::move(Consumers), std::move(Finder), std::move(Checks));
298 std::vector<std::string> CheckNames;
300 for (
const auto &CheckFactory : *CheckFactories) {
301 if (Filter.
contains(CheckFactory.first))
302 CheckNames.push_back(CheckFactory.first);
305 for (
const auto &AnalyzerCheck : getCheckersControlList(Filter))
306 CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
308 std::sort(CheckNames.begin(), CheckNames.end());
314 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
315 CheckFactories->createChecks(&Context, Checks);
316 for (
const auto &
Check : Checks)
317 Check->storeOptions(Options);
321 ClangTidyASTConsumerFactory::CheckersList
322 ClangTidyASTConsumerFactory::getCheckersControlList(
GlobList &Filter) {
325 bool AnalyzerChecksEnabled =
false;
326 for (StringRef CheckName : StaticAnalyzerChecks) {
327 std::string Checker((AnalyzerCheckNamePrefix + CheckName).str());
328 AnalyzerChecksEnabled =
329 AnalyzerChecksEnabled ||
330 (!CheckName.startswith(
"debug") && Filter.
contains(Checker));
333 if (AnalyzerChecksEnabled) {
341 for (StringRef CheckName : StaticAnalyzerChecks) {
342 std::string Checker((AnalyzerCheckNamePrefix + CheckName).str());
344 if (CheckName.startswith(
"core") ||
345 (!CheckName.startswith(
"debug") && Filter.
contains(Checker)))
346 List.push_back(std::make_pair(CheckName,
true));
353 DiagnosticIDs::Level Level) {
354 return Context->
diag(CheckName, Loc, Message, Level);
357 void ClangTidyCheck::run(
const ast_matchers::MatchFinder::MatchResult &
Result) {
364 : NamePrefix(CheckName.str() +
"."), CheckOptions(CheckOptions) {}
367 const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
368 if (Iter != CheckOptions.end())
374 StringRef Default)
const {
375 auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
376 if (Iter != CheckOptions.end())
379 Iter = CheckOptions.find(LocalName.str());
380 if (Iter != CheckOptions.end())
386 StringRef LocalName, StringRef Value)
const {
387 Options[NamePrefix + LocalName.str()] = Value;
391 StringRef LocalName, int64_t Value)
const {
392 store(Options, LocalName, llvm::itostr(Value));
412 runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
413 const tooling::CompilationDatabase &Compilations,
414 ArrayRef<std::string> InputFiles,
415 std::vector<ClangTidyError> *Errors,
ProfileData *Profile) {
416 ClangTool Tool(Compilations, InputFiles);
420 ArgumentsAdjuster PerFileExtraArgumentsInserter =
423 CommandLineArguments AdjustedArgs;
426 AdjustedArgs.insert(AdjustedArgs.begin(), Args.begin(), Args.end());
428 AdjustedArgs.insert(AdjustedArgs.end(), Opts.
ExtraArgs->begin(),
434 ArgumentsAdjuster PluginArgumentsRemover =
436 CommandLineArguments AdjustedArgs;
437 for (
size_t I = 0, E = Args.size(); I < E; ++I) {
438 if (I + 4 < Args.size() && Args[I] ==
"-Xclang" &&
439 (Args[I + 1] ==
"-load" || Args[I + 1] ==
"-add-plugin" ||
440 StringRef(Args[I + 1]).startswith(
"-plugin-arg-")) &&
441 Args[I + 2] ==
"-Xclang") {
444 AdjustedArgs.push_back(Args[I]);
449 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
450 Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
456 Tool.setDiagnosticConsumer(&DiagConsumer);
461 FrontendAction *create()
override {
return new Action(&ConsumerFactory); }
464 class Action :
public ASTFrontendAction {
467 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
468 StringRef File)
override {
479 ActionFactory Factory(Context);
486 unsigned &WarningsAsErrorsCount) {
487 ErrorReporter Reporter(Fix);
488 vfs::FileSystem &FileSystem =
489 *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
490 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
491 if (!InitialWorkingDir)
492 llvm::report_fatal_error(
"Cannot get current working path.");
495 if (!Error.BuildDirectory.empty()) {
500 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
502 Reporter.reportDiagnostic(Error);
504 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
507 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
512 tooling::TranslationUnitReplacements TUR;
514 TUR.Replacements.insert(TUR.Replacements.end(), Error.Fix.begin(),
517 yaml::Output YAML(OS);
SourceLocation Loc
'#' location in the include directive
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
Read-only set of strings represented as a list of positive and negative globs.
std::unique_ptr< ast_matchers::MatchFinder > Finder
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
std::vector< std::unique_ptr< ClangTidyCheck > > Checks
bool contains(StringRef S)
Returns true if the pattern matches S.
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options)
Returns the effective check-specific options.
Contains options for clang-tidy.
ProfileData * getCheckProfileData() const
A collection of ClangTidyCheckFactory instances.
const std::vector< ClangTidyError > & getErrors() const
Returns all collected errors.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
void exportReplacements(const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Serializes replacements into YAML and writes them to the specified output stream. ...
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
std::string Filename
Filename as a string.
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
void setCheckProfileData(ProfileData *Profile)
Set the output struct for profile data.
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError...
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.
std::map< std::string, std::string > OptionMap
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options)
Fills the list of check names that are enabled when the provided filters are applied.
ClangTidyStats runClangTidy(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider, const tooling::CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, std::vector< ClangTidyError > *Errors, ProfileData *Profile)
Run a set of clang-tidy checks on a set of files.
static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts, AnalyzerOptionsRef AnalyzerOptions)
unsigned WarningsAsErrors
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
llvm::Optional< bool > AnalyzeTemporaryDtors
Turns on temporary destructor-based analysis.
const ClangTidyStats & getStats() const
Returns ClangTidyStats containing issued and ignored diagnostic counters.
CharSourceRange Range
SourceRange for the file name.
A detected error complete with information to display diagnostic and automatic fix.
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File)
Returns an ASTConsumer that runs the specified clang-tidy checks.
void handleErrors(const std::vector< ClangTidyError > &Errors, bool Fix, unsigned &WarningsAsErrorsCount)
Displays the found Errors to the users.
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 setCurrentBuildDirectory(StringRef BuildDirectory)
Should be called when starting to process new translation unit.
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result)
ClangTidyChecks that register ASTMatchers should do the actual work in here.
GlobList & getChecksFilter()
Returns check filter for the CurrentFile.
Container for clang-tidy profiling data.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticConsumer * DiagPrinter