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