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