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/Format/Format.h"
26 #include "clang/Frontend/ASTConsumers.h"
27 #include "clang/Frontend/CompilerInstance.h"
28 #include "clang/Frontend/FrontendActions.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/Rewrite/Frontend/FixItRewriter.h"
35 #include "clang/Rewrite/Frontend/FrontendActions.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
37 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.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 "llvm/Support/Signals.h"
47 using namespace clang::ast_matchers;
48 using namespace clang::driver;
49 using namespace clang::tooling;
58 static const char *AnalyzerCheckNamePrefix =
"clang-analyzer-";
60 class AnalyzerDiagnosticConsumer :
public ento::PathDiagnosticConsumer {
62 AnalyzerDiagnosticConsumer(ClangTidyContext &
Context) : Context(Context) {}
64 void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &
Diags,
65 FilesMade *filesMade)
override {
66 for (
const ento::PathDiagnostic *PD : Diags) {
67 SmallString<64> CheckName(AnalyzerCheckNamePrefix);
68 CheckName += PD->getCheckName();
69 Context.diag(CheckName, PD->getLocation().asLocation(),
70 PD->getShortDescription())
71 << PD->path.back()->getRanges();
73 for (
const auto &DiagPiece :
74 PD->path.flatten(
true)) {
75 Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
76 DiagPiece->getString(), DiagnosticIDs::Note)
77 << DiagPiece->getRanges();
82 StringRef getName()
const override {
return "ClangTidyDiags"; }
83 bool supportsLogicalOpControlFlow()
const override {
return true; }
84 bool supportsCrossFileDiagnostics()
const override {
return true; }
93 :
Files(FileSystemOptions()),
DiagOpts(
new DiagnosticOptions()),
95 Diags(IntrusiveRefCntPtr<DiagnosticIDs>(
new DiagnosticIDs), &*
DiagOpts,
99 DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
103 SourceManager &getSourceManager() {
return SourceMgr; }
105 void reportDiagnostic(
const ClangTidyError &Error) {
106 const tooling::DiagnosticMessage &
Message = Error.Message;
107 SourceLocation
Loc = getLocation(Message.FilePath, Message.FileOffset);
110 SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
112 auto Level =
static_cast<DiagnosticsEngine::Level
>(Error.DiagLevel);
113 std::string
Name = Error.DiagnosticName;
114 if (Error.IsWarningAsError) {
115 Name +=
",-warnings-as-errors";
116 Level = DiagnosticsEngine::Error;
119 auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level,
"%0 [%1]"))
120 << Message.Message << Name;
121 for (
const auto &FileAndReplacements : Error.Fix) {
122 for (
const auto &Repl : FileAndReplacements.second) {
127 SourceLocation FixLoc;
129 bool CanBeApplied =
false;
130 if (Repl.isApplicable()) {
131 SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
132 Files.makeAbsolutePath(FixAbsoluteFilePath);
134 tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
136 Repl.getReplacementText());
138 llvm::Error Err = Replacements.add(R);
141 llvm::errs() <<
"Trying to resolve conflict: "
144 Replacements.getShiftedCodePosition(R.getOffset());
145 unsigned NewLength = Replacements.getShiftedCodePosition(
146 R.getOffset() + R.getLength()) -
148 if (NewLength == R.getLength()) {
149 R = Replacement(R.getFilePath(), NewOffset, NewLength,
150 R.getReplacementText());
151 Replacements = Replacements.merge(tooling::Replacements(R));
156 <<
"Can't resolve conflict, skipping the replacement.\n";
164 FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
165 SourceLocation FixEndLoc =
166 FixLoc.getLocWithOffset(Repl.getLength());
167 Range = SourceRange(FixLoc, FixEndLoc);
168 Diag << FixItHint::CreateReplacement(Range,
169 Repl.getReplacementText());
173 FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
177 for (
auto Fix : FixLocations) {
178 Diags.Report(
Fix.first,
Fix.second ? diag::note_fixit_applied
179 : diag::note_fixit_failed);
181 for (
const auto &Note : Error.Notes)
189 StringRef
File = FileAndReplacements.first();
190 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
191 SourceMgr.getFileManager().getBufferForFile(File);
193 llvm::errs() <<
"Can't get buffer for file " << File <<
": "
194 << Buffer.getError().message() <<
"\n";
198 StringRef Code = Buffer.get()->getBuffer();
199 auto Style = format::getStyle(
200 *Context.getOptionsForFile(File).FormatStyle,
File,
"none");
205 llvm::Expected<tooling::Replacements> Replacements =
206 format::cleanupAroundReplacements(Code, FileAndReplacements.second,
212 if (llvm::Expected<tooling::Replacements> FormattedReplacements =
213 format::formatReplacements(Code, *Replacements, *Style)) {
214 Replacements = std::move(FormattedReplacements);
216 llvm_unreachable(
"!Replacements");
219 <<
". Skipping formatting.\n";
222 llvm::errs() <<
"Can't apply replacements for file " << File <<
"\n";
225 if (Rewrite.overwriteChangedFiles()) {
226 llvm::errs() <<
"clang-tidy failed to apply suggested fixes.\n";
228 llvm::errs() <<
"clang-tidy applied " <<
AppliedFixes <<
" of "
237 SourceLocation getLocation(StringRef FilePath,
unsigned Offset) {
238 if (FilePath.empty())
239 return SourceLocation();
241 const FileEntry *File =
SourceMgr.getFileManager().getFile(FilePath);
242 FileID ID =
SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
243 return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
246 void reportNote(
const tooling::DiagnosticMessage &Message) {
247 SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
248 Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note,
"%0"))
268 ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
269 std::unique_ptr<ast_matchers::MatchFinder>
Finder,
270 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks)
275 std::unique_ptr<ast_matchers::MatchFinder>
Finder;
276 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
281 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
284 for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
285 E = ClangTidyModuleRegistry::end();
287 std::unique_ptr<ClangTidyModule> Module(I->instantiate());
288 Module->addCheckFactories(*CheckFactories);
293 AnalyzerOptionsRef AnalyzerOptions) {
294 StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
296 StringRef OptName(Opt.first);
297 if (!OptName.startswith(AnalyzerPrefix))
299 AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
308 const auto &RegisteredCheckers =
309 AnalyzerOptions::getRegisteredCheckers(
false);
310 bool AnalyzerChecksEnabled =
false;
311 for (StringRef CheckName : RegisteredCheckers) {
312 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
313 AnalyzerChecksEnabled |= Context.
isCheckEnabled(ClangTidyCheckName);
316 if (!AnalyzerChecksEnabled)
324 for (StringRef CheckName : RegisteredCheckers) {
325 std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
327 if (CheckName.startswith(
"core") ||
329 List.emplace_back(CheckName,
true);
335 std::unique_ptr<clang::ASTConsumer>
337 clang::CompilerInstance &Compiler, StringRef File) {
340 Context.setSourceManager(&Compiler.getSourceManager());
341 Context.setCurrentFile(File);
342 Context.setASTContext(&Compiler.getASTContext());
344 auto WorkingDir = Compiler.getSourceManager()
346 .getVirtualFileSystem()
347 ->getCurrentWorkingDirectory();
349 Context.setCurrentBuildDirectory(WorkingDir.get());
351 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
352 CheckFactories->createChecks(&Context, Checks);
354 ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
355 if (
auto *P = Context.getCheckProfileData())
356 FinderOptions.CheckProfiling.emplace(P->Records);
358 std::unique_ptr<ast_matchers::MatchFinder>
Finder(
359 new ast_matchers::MatchFinder(std::move(FinderOptions)));
361 for (
auto &
Check : Checks) {
362 Check->registerMatchers(&*Finder);
363 Check->registerPPCallbacks(Compiler);
366 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
368 Consumers.push_back(Finder->newASTConsumer());
370 AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
373 AnalyzerOptions->Config[
"cfg-temporary-dtors"] =
374 Context.getOptions().AnalyzeTemporaryDtors ?
"true" :
"false";
377 if (!AnalyzerOptions->CheckersControlList.empty()) {
379 AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
380 AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
381 AnalyzerOptions->AnalyzeNestedBlocks =
true;
382 AnalyzerOptions->eagerlyAssumeBinOpBifurcation =
true;
383 std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
384 ento::CreateAnalysisConsumer(Compiler);
385 AnalysisConsumer->AddDiagnosticConsumer(
386 new AnalyzerDiagnosticConsumer(Context));
387 Consumers.push_back(std::move(AnalysisConsumer));
389 return llvm::make_unique<ClangTidyASTConsumer>(
390 std::move(Consumers), std::move(Finder), std::move(Checks));
394 std::vector<std::string> CheckNames;
395 for (
const auto &CheckFactory : *CheckFactories) {
396 if (Context.isCheckEnabled(CheckFactory.first))
397 CheckNames.push_back(CheckFactory.first);
401 CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
403 std::sort(CheckNames.begin(), CheckNames.end());
409 std::vector<std::unique_ptr<ClangTidyCheck>>
Checks;
410 CheckFactories->createChecks(&Context, Checks);
411 for (
const auto &
Check : Checks)
412 Check->storeOptions(Options);
417 DiagnosticIDs::Level Level) {
418 return Context->diag(CheckName, Loc, Message, Level);
421 void ClangTidyCheck::run(
const ast_matchers::MatchFinder::MatchResult &Result) {
422 Context->setSourceManager(Result.SourceManager);
428 : NamePrefix(CheckName.str() +
"."), CheckOptions(CheckOptions) {}
431 const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
432 if (Iter != CheckOptions.end())
438 StringRef Default)
const {
439 auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
440 if (Iter != CheckOptions.end())
443 Iter = CheckOptions.find(LocalName.str());
444 if (Iter != CheckOptions.end())
450 StringRef LocalName, StringRef Value)
const {
451 Options[NamePrefix + LocalName.str()] = Value;
455 StringRef LocalName, int64_t Value)
const {
456 store(Options, LocalName, llvm::itostr(Value));
476 const CompilationDatabase &Compilations,
477 ArrayRef<std::string> InputFiles,
ProfileData *Profile) {
478 ClangTool Tool(Compilations, InputFiles);
481 ArgumentsAdjuster PerFileExtraArgumentsInserter =
484 CommandLineArguments AdjustedArgs = Args;
486 auto I = AdjustedArgs.begin();
487 if (I != AdjustedArgs.end() && !StringRef(*I).startswith(
"-"))
493 AdjustedArgs.insert(AdjustedArgs.end(), Opts.
ExtraArgs->begin(),
499 ArgumentsAdjuster PluginArgumentsRemover =
500 [](
const CommandLineArguments &Args, StringRef
Filename) {
501 CommandLineArguments AdjustedArgs;
502 for (
size_t I = 0, E = Args.size(); I < E; ++I) {
503 if (I + 4 < Args.size() && Args[I] ==
"-Xclang" &&
504 (Args[I + 1] ==
"-load" || Args[I + 1] ==
"-add-plugin" ||
505 StringRef(Args[I + 1]).startswith(
"-plugin-arg-")) &&
506 Args[I + 2] ==
"-Xclang") {
509 AdjustedArgs.push_back(Args[I]);
514 Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
515 Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
521 Tool.setDiagnosticConsumer(&DiagConsumer);
526 FrontendAction *create()
override {
return new Action(&ConsumerFactory); }
529 class Action :
public ASTFrontendAction {
532 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
533 StringRef File)
override {
544 ActionFactory Factory(Context);
549 unsigned &WarningsAsErrorsCount) {
550 ErrorReporter Reporter(Context, Fix);
551 vfs::FileSystem &FileSystem =
552 *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
553 auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
554 if (!InitialWorkingDir)
555 llvm::report_fatal_error(
"Cannot get current working path.");
558 if (!Error.BuildDirectory.empty()) {
563 FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
565 Reporter.reportDiagnostic(Error);
567 FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
570 WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
574 const std::vector<ClangTidyError> &Errors,
576 TranslationUnitDiagnostics TUD;
577 TUD.MainSourceFile = MainFilePath;
578 for (
const auto &Error : Errors) {
579 tooling::Diagnostic Diag = Error;
580 TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
583 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.
llvm::StringMap< Replacements > FileReplacements
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
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options)
Returns the effective check-specific options.
static const StringRef Message
Contains options for clang-tidy.
void runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, ProfileData *Profile)
A collection of ClangTidyCheckFactory instances.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
ArrayRef< ClangTidyError > getErrors() const
Returns all collected errors.
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.
std::string Filename
Filename as a string.
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.
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.
std::vector< std::pair< std::string, bool > > CheckersList
void handleErrors(ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount)
Displays the found Errors to the users.
static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts, AnalyzerOptionsRef AnalyzerOptions)
unsigned WarningsAsErrors
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
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.
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))
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
bool applyAllReplacements(const std::vector< tooling::Replacement > &Replaces, Rewriter &Rewrite)
virtual void check(const ast_matchers::MatchFinder::MatchResult &Result)
ClangTidyChecks that register ASTMatchers should do the actual work in here.
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.
static CheckersList getCheckersControlList(ClangTidyContext &Context)
DiagnosticConsumer * DiagPrinter