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