clang-tools  10.0.0git
ClangTidy.cpp
Go to the documentation of this file.
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"
20 #include "ClangTidyProfiling.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:
70  AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
71 
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 
90  StringRef getName() const override { return "ClangTidyDiags"; }
91  bool supportsLogicalOpControlFlow() const override { return true; }
92  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:
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 
113  SourceManager &getSourceManager() { return SourceMgr; }
114 
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;
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 
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 
234  unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
235 
236 private:
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 
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 
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:
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 
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
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 
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>
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 
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 
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>
472  std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
473  Options),
474  AllowEnablingAnalyzerAlphaCheckers);
475  ClangTidyASTConsumerFactory Factory(Context);
476  return Factory.getCheckNames();
477 }
478 
483  std::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
484  Options),
485  AllowEnablingAnalyzerAlphaCheckers);
486  ClangTidyASTConsumerFactory Factory(Context);
487  return Factory.getCheckOptions();
488 }
489 
490 std::vector<ClangTidyError>
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) {
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 
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:
558  };
559 
560  ClangTidyASTConsumerFactory ConsumerFactory;
561  };
562 
563  ActionFactory Factory(Context, BaseFS);
564  Tool.run(&Factory);
565  return DiagConsumer.take();
566 }
567 
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 
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
SourceLocation Loc
&#39;#&#39; location in the include directive
std::vector< std::string > getCheckNames()
Get the list of enabled checks.
Definition: ClangTidy.cpp:442
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
std::string Code
Some operations such as code completion produce a set of candidates.
bool canEnableAnalyzerAlphaCheckers() const
If the experimental alpha checkers from the static analyzer can be enabled.
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
Definition: ClangTidy.cpp:459
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
static cl::opt< std::string > StoreCheckProfile("store-check-profile", cl::desc(R"( By default reports are printed in tabulated format to stderr. When this option is passed, these per-TU profiles are instead stored as JSON. )"), cl::value_desc("prefix"), cl::cat(ClangTidyCategory))
constexpr llvm::StringLiteral Message
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Contains options for clang-tidy.
A collection of ClangTidyCheckFactory instances.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
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)
Definition: ClangTidy.cpp:491
void handleErrors(llvm::ArrayRef< ClangTidyError > Errors, ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS)
Displays the found Errors to the users.
Definition: ClangTidy.cpp:568
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
std::string Filename
Filename as a string.
static cl::opt< bool > AllowEnablingAnalyzerAlphaCheckers("allow-enabling-analyzer-alpha-checkers", cl::init(false), cl::Hidden, cl::cat(ClangTidyCategory))
This option allows enabling the experimental alpha checkers from the static analyzer.
llvm::unique_function< void()> Action
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
const LangOptions & getLangOpts() const
Gets the language options from the AST context.
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
static cl::opt< std::string > WarningsAsErrors("warnings-as-errors", cl::desc(R"( Upgrades warnings to errors. Same format as '-checks'. This option's value is appended to the value of the 'WarningsAsErrors' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError...
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void setProfileStoragePrefix(StringRef ProfilePrefix)
Control storage of profile date.
static cl::opt< bool > EnableCheckProfile("enable-check-profile", cl::desc(R"( Enable per-check timing profiles, and print a report to stderr. )"), cl::init(false), cl::cat(ClangTidyCategory))
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
size_t Offset
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void setDiagnosticsEngine(DiagnosticsEngine *DiagEngine)
Sets the DiagnosticsEngine that diag() will emit diagnostics to.
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Definition: ClangTidy.cpp:595
CharSourceRange Range
SourceRange for the file name.
A detected error complete with information to display diagnostic and automatic fix.
static cl::opt< std::string > Checks("checks", cl::desc(R"( Comma-separated list of globs with optional '-' prefix. Globs are processed in order of appearance in the list. Globs without '-' prefix add checks with matching names to the set, globs with the '-' prefix remove checks with matching names from the set of enabled checks. This option's value is appended to the value of the 'Checks' option in .clang-tidy file, if any. )"), cl::init(""), cl::cat(ClangTidyCategory))
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.
Definition: ClangTidy.cpp:370
const Expr * E
IgnoreDiagnostics DiagConsumer
static cl::opt< bool > Fix("fix", cl::desc(R"( Apply suggested fixes. Without -fix-errors clang-tidy will bail out if any compilation errors were found. )"), cl::init(false), cl::cat(ClangTidyCategory))
void setCurrentBuildDirectory(StringRef BuildDirectory)
Should be called when starting to process new translation unit.
void setEnableProfiling(bool Profile)
Control profile collection in clang-tidy.
llvm::StringMap< std::string > Files