clang-tools  8.0.0svn
ClangTidy.cpp
Go to the documentation of this file.
1 //===--- tools/extra/clang-tidy/ClangTidy.cpp - Clang tidy tool -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file This file implements a clang-tidy tool.
11 ///
12 /// This tool uses the Clang Tooling infrastructure, see
13 /// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
14 /// for details on setting it up with LLVM source tree.
15 ///
16 //===----------------------------------------------------------------------===//
17 
18 #include "ClangTidy.h"
21 #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/Rewrite/Frontend/FixItRewriter.h"
37 #include "clang/Rewrite/Frontend/FrontendActions.h"
38 #if CLANG_ENABLE_STATIC_ANALYZER
39 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
40 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
41 #endif // CLANG_ENABLE_STATIC_ANALYZER
42 #include "clang/Tooling/DiagnosticsYaml.h"
43 #include "clang/Tooling/Refactoring.h"
44 #include "clang/Tooling/ReplacementsYaml.h"
45 #include "clang/Tooling/Tooling.h"
46 #include "llvm/Support/Process.h"
47 #include "llvm/Support/Signals.h"
48 #include <algorithm>
49 #include <utility>
50 
51 using namespace clang::ast_matchers;
52 using namespace clang::driver;
53 using namespace clang::tooling;
54 using namespace llvm;
55 
56 LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
57 
58 namespace clang {
59 namespace tidy {
60 
61 namespace {
62 #if CLANG_ENABLE_STATIC_ANALYZER
63 static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
64 
65 class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
66 public:
67  AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
68 
69  void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
70  FilesMade *filesMade) override {
71  for (const ento::PathDiagnostic *PD : Diags) {
72  SmallString<64> CheckName(AnalyzerCheckNamePrefix);
73  CheckName += PD->getCheckName();
74  Context.diag(CheckName, PD->getLocation().asLocation(),
75  PD->getShortDescription())
76  << PD->path.back()->getRanges();
77 
78  for (const auto &DiagPiece :
79  PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
80  Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
81  DiagPiece->getString(), DiagnosticIDs::Note)
82  << DiagPiece->getRanges();
83  }
84  }
85  }
86 
87  StringRef getName() const override { return "ClangTidyDiags"; }
88  bool supportsLogicalOpControlFlow() const override { return true; }
89  bool supportsCrossFileDiagnostics() const override { return true; }
90 
91 private:
92  ClangTidyContext &Context;
93 };
94 #endif // CLANG_ENABLE_STATIC_ANALYZER
95 
96 class ErrorReporter {
97 public:
98  ErrorReporter(ClangTidyContext &Context, bool ApplyFixes,
99  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS)
100  : Files(FileSystemOptions(), BaseFS), DiagOpts(new DiagnosticOptions()),
101  DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
102  Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
103  DiagPrinter),
104  SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
105  TotalFixes(0), AppliedFixes(0), WarningsAsErrors(0) {
106  DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
107  DiagPrinter->BeginSourceFile(LangOpts);
108  }
109 
110  SourceManager &getSourceManager() { return SourceMgr; }
111 
112  void reportDiagnostic(const ClangTidyError &Error) {
113  const tooling::DiagnosticMessage &Message = Error.Message;
114  SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
115  // Contains a pair for each attempted fix: location and whether the fix was
116  // applied successfully.
117  SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
118  {
119  auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
120  std::string Name = Error.DiagnosticName;
121  if (Error.IsWarningAsError) {
122  Name += ",-warnings-as-errors";
123  Level = DiagnosticsEngine::Error;
125  }
126  auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
127  << Message.Message << Name;
128  for (const auto &FileAndReplacements : Error.Fix) {
129  for (const auto &Repl : FileAndReplacements.second) {
130  SourceLocation FixLoc;
131  ++TotalFixes;
132  bool CanBeApplied = false;
133  if (Repl.isApplicable()) {
134  SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
135  Files.makeAbsolutePath(FixAbsoluteFilePath);
136  if (ApplyFixes) {
137  tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
138  Repl.getLength(),
139  Repl.getReplacementText());
140  Replacements &Replacements = FileReplacements[R.getFilePath()];
141  llvm::Error Err = Replacements.add(R);
142  if (Err) {
143  // FIXME: Implement better conflict handling.
144  llvm::errs() << "Trying to resolve conflict: "
145  << llvm::toString(std::move(Err)) << "\n";
146  unsigned NewOffset =
147  Replacements.getShiftedCodePosition(R.getOffset());
148  unsigned NewLength = Replacements.getShiftedCodePosition(
149  R.getOffset() + R.getLength()) -
150  NewOffset;
151  if (NewLength == R.getLength()) {
152  R = Replacement(R.getFilePath(), NewOffset, NewLength,
153  R.getReplacementText());
154  Replacements = Replacements.merge(tooling::Replacements(R));
155  CanBeApplied = true;
156  ++AppliedFixes;
157  } else {
158  llvm::errs()
159  << "Can't resolve conflict, skipping the replacement.\n";
160  }
161 
162  } else {
163  CanBeApplied = true;
164  ++AppliedFixes;
165  }
166  }
167  FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
168  SourceLocation FixEndLoc =
169  FixLoc.getLocWithOffset(Repl.getLength());
170  // Retrieve the source range for applicable fixes. Macro definitions
171  // on the command line have locations in a virtual buffer and don't
172  // have valid file paths and are therefore not applicable.
173  CharSourceRange Range =
174  CharSourceRange::getCharRange(SourceRange(FixLoc, FixEndLoc));
175  Diag << FixItHint::CreateReplacement(Range,
176  Repl.getReplacementText());
177  }
178 
179  if (ApplyFixes)
180  FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
181  }
182  }
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 
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 
241  unsigned getWarningsAsErrorsCount() const { return WarningsAsErrors; }
242 
243 private:
244  SourceLocation getLocation(StringRef FilePath, unsigned Offset) {
245  if (FilePath.empty())
246  return SourceLocation();
247 
248  const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
249  FileID ID = SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
250  return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
251  }
252 
253  void reportNote(const tooling::DiagnosticMessage &Message) {
254  SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
255  Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
256  << Message.Message;
257  }
258 
259  FileManager Files;
260  LangOptions LangOpts; // FIXME: use langopts from each original file
261  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
262  DiagnosticConsumer *DiagPrinter;
263  DiagnosticsEngine Diags;
264  SourceManager SourceMgr;
265  llvm::StringMap<Replacements> FileReplacements;
266  ClangTidyContext &Context;
267  bool ApplyFixes;
268  unsigned TotalFixes;
269  unsigned AppliedFixes;
270  unsigned WarningsAsErrors;
271 };
272 
273 class ClangTidyASTConsumer : public MultiplexConsumer {
274 public:
275  ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
276  std::unique_ptr<ClangTidyProfiling> Profiling,
277  std::unique_ptr<ast_matchers::MatchFinder> Finder,
278  std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
279  : MultiplexConsumer(std::move(Consumers)),
280  Profiling(std::move(Profiling)), Finder(std::move(Finder)),
281  Checks(std::move(Checks)) {}
282 
283 private:
284  // Destructor order matters! Profiling must be destructed last.
285  // Or at least after Finder.
286  std::unique_ptr<ClangTidyProfiling> Profiling;
287  std::unique_ptr<ast_matchers::MatchFinder> Finder;
288  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
289 };
290 
291 } // namespace
292 
293 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
294  ClangTidyContext &Context)
295  : Context(Context), CheckFactories(new ClangTidyCheckFactories) {
296  for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
297  E = ClangTidyModuleRegistry::end();
298  I != E; ++I) {
299  std::unique_ptr<ClangTidyModule> Module(I->instantiate());
300  Module->addCheckFactories(*CheckFactories);
301  }
302 }
303 
304 #if CLANG_ENABLE_STATIC_ANALYZER
305 static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts,
306  AnalyzerOptionsRef AnalyzerOptions) {
307  StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
308  for (const auto &Opt : Opts.CheckOptions) {
309  StringRef OptName(Opt.first);
310  if (!OptName.startswith(AnalyzerPrefix))
311  continue;
312  AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
313  }
314 }
315 
316 typedef std::vector<std::pair<std::string, bool>> CheckersList;
317 
318 static CheckersList getCheckersControlList(ClangTidyContext &Context,
319  bool IncludeExperimental) {
320  CheckersList List;
321 
322  const auto &RegisteredCheckers =
323  AnalyzerOptions::getRegisteredCheckers(IncludeExperimental);
324  bool AnalyzerChecksEnabled = false;
325  for (StringRef CheckName : RegisteredCheckers) {
326  std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
327  AnalyzerChecksEnabled |= Context.isCheckEnabled(ClangTidyCheckName);
328  }
329 
330  if (!AnalyzerChecksEnabled)
331  return List;
332 
333  // List all static analyzer checkers that our filter enables.
334  //
335  // Always add all core checkers if any other static analyzer check is enabled.
336  // This is currently necessary, as other path sensitive checks rely on the
337  // core checkers.
338  for (StringRef CheckName : RegisteredCheckers) {
339  std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
340 
341  if (CheckName.startswith("core") ||
342  Context.isCheckEnabled(ClangTidyCheckName)) {
343  List.emplace_back(CheckName, true);
344  }
345  }
346  return List;
347 }
348 #endif // CLANG_ENABLE_STATIC_ANALYZER
349 
350 std::unique_ptr<clang::ASTConsumer>
352  clang::CompilerInstance &Compiler, StringRef File) {
353  // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
354  // modify Compiler.
355  Context.setSourceManager(&Compiler.getSourceManager());
356  Context.setCurrentFile(File);
357  Context.setASTContext(&Compiler.getASTContext());
358 
359  auto WorkingDir = Compiler.getSourceManager()
360  .getFileManager()
361  .getVirtualFileSystem()
362  ->getCurrentWorkingDirectory();
363  if (WorkingDir)
364  Context.setCurrentBuildDirectory(WorkingDir.get());
365 
366  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
367  CheckFactories->createChecks(&Context, Checks);
368 
369  ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
370 
371  std::unique_ptr<ClangTidyProfiling> Profiling;
372  if (Context.getEnableProfiling()) {
373  Profiling = llvm::make_unique<ClangTidyProfiling>(
374  Context.getProfileStorageParams());
375  FinderOptions.CheckProfiling.emplace(Profiling->Records);
376  }
377 
378  std::unique_ptr<ast_matchers::MatchFinder> Finder(
379  new ast_matchers::MatchFinder(std::move(FinderOptions)));
380 
381  for (auto &Check : Checks) {
382  Check->registerMatchers(&*Finder);
383  Check->registerPPCallbacks(Compiler);
384  }
385 
386  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
387  if (!Checks.empty())
388  Consumers.push_back(Finder->newASTConsumer());
389 
390 #if CLANG_ENABLE_STATIC_ANALYZER
391  AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
392  AnalyzerOptions->CheckersControlList =
393  getCheckersControlList(Context, Context.canEnableAnalyzerAlphaCheckers());
394  if (!AnalyzerOptions->CheckersControlList.empty()) {
395  setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
396  AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
397  AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
398  AnalyzerOptions->AnalyzeNestedBlocks = true;
399  AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
400  std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
401  ento::CreateAnalysisConsumer(Compiler);
402  AnalysisConsumer->AddDiagnosticConsumer(
403  new AnalyzerDiagnosticConsumer(Context));
404  Consumers.push_back(std::move(AnalysisConsumer));
405  }
406 #endif // CLANG_ENABLE_STATIC_ANALYZER
407  return llvm::make_unique<ClangTidyASTConsumer>(
408  std::move(Consumers), std::move(Profiling), std::move(Finder),
409  std::move(Checks));
410 }
411 
412 std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
413  std::vector<std::string> CheckNames;
414  for (const auto &CheckFactory : *CheckFactories) {
415  if (Context.isCheckEnabled(CheckFactory.first))
416  CheckNames.push_back(CheckFactory.first);
417  }
418 
419 #if CLANG_ENABLE_STATIC_ANALYZER
420  for (const auto &AnalyzerCheck : getCheckersControlList(
421  Context, Context.canEnableAnalyzerAlphaCheckers()))
422  CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
423 #endif // CLANG_ENABLE_STATIC_ANALYZER
424 
425  std::sort(CheckNames.begin(), CheckNames.end());
426  return CheckNames;
427 }
428 
431  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
432  CheckFactories->createChecks(&Context, Checks);
433  for (const auto &Check : Checks)
434  Check->storeOptions(Options);
435  return Options;
436 }
437 
438 DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
439  DiagnosticIDs::Level Level) {
440  return Context->diag(CheckName, Loc, Message, Level);
441 }
442 
443 void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
444  // For historical reasons, checks don't implement the MatchFinder run()
445  // callback directly. We keep the run()/check() distinction to avoid interface
446  // churn, and to allow us to add cross-cutting logic in the future.
447  check(Result);
448 }
449 
450 OptionsView::OptionsView(StringRef CheckName,
451  const ClangTidyOptions::OptionMap &CheckOptions)
452  : NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
453 
454 std::string OptionsView::get(StringRef LocalName, StringRef Default) const {
455  const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
456  if (Iter != CheckOptions.end())
457  return Iter->second;
458  return Default;
459 }
460 
461 std::string OptionsView::getLocalOrGlobal(StringRef LocalName,
462  StringRef Default) const {
463  auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
464  if (Iter != CheckOptions.end())
465  return Iter->second;
466  // Fallback to global setting, if present.
467  Iter = CheckOptions.find(LocalName.str());
468  if (Iter != CheckOptions.end())
469  return Iter->second;
470  return Default;
471 }
472 
474  StringRef LocalName, StringRef Value) const {
475  Options[NamePrefix + LocalName.str()] = Value;
476 }
477 
479  StringRef LocalName, int64_t Value) const {
480  store(Options, LocalName, llvm::itostr(Value));
481 }
482 
483 std::vector<std::string>
487  llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
488  Options),
489  AllowEnablingAnalyzerAlphaCheckers);
490  ClangTidyASTConsumerFactory Factory(Context);
491  return Factory.getCheckNames();
492 }
493 
498  llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
499  Options),
500  AllowEnablingAnalyzerAlphaCheckers);
501  ClangTidyASTConsumerFactory Factory(Context);
502  return Factory.getCheckOptions();
503 }
504 
505 std::vector<ClangTidyError>
507  const CompilationDatabase &Compilations,
508  ArrayRef<std::string> InputFiles,
509  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
510  bool EnableCheckProfile, llvm::StringRef StoreCheckProfile) {
511  ClangTool Tool(Compilations, InputFiles,
512  std::make_shared<PCHContainerOperations>(), BaseFS);
513 
514  // Add extra arguments passed by the clang-tidy command-line.
515  ArgumentsAdjuster PerFileExtraArgumentsInserter =
516  [&Context](const CommandLineArguments &Args, StringRef Filename) {
518  CommandLineArguments AdjustedArgs = Args;
519  if (Opts.ExtraArgsBefore) {
520  auto I = AdjustedArgs.begin();
521  if (I != AdjustedArgs.end() && !StringRef(*I).startswith("-"))
522  ++I; // Skip compiler binary name, if it is there.
523  AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
524  Opts.ExtraArgsBefore->end());
525  }
526  if (Opts.ExtraArgs)
527  AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
528  Opts.ExtraArgs->end());
529  return AdjustedArgs;
530  };
531 
532  // Remove plugins arguments.
533  ArgumentsAdjuster PluginArgumentsRemover =
534  [](const CommandLineArguments &Args, StringRef Filename) {
535  CommandLineArguments AdjustedArgs;
536  for (size_t I = 0, E = Args.size(); I < E; ++I) {
537  if (I + 4 < Args.size() && Args[I] == "-Xclang" &&
538  (Args[I + 1] == "-load" || Args[I + 1] == "-add-plugin" ||
539  StringRef(Args[I + 1]).startswith("-plugin-arg-")) &&
540  Args[I + 2] == "-Xclang") {
541  I += 3;
542  } else
543  AdjustedArgs.push_back(Args[I]);
544  }
545  return AdjustedArgs;
546  };
547 
548  Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
549  Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
550  Context.setEnableProfiling(EnableCheckProfile);
551  Context.setProfileStoragePrefix(StoreCheckProfile);
552 
553  ClangTidyDiagnosticConsumer DiagConsumer(Context);
554  DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions(),
555  &DiagConsumer, /*ShouldOwnClient=*/false);
556  Context.setDiagnosticsEngine(&DE);
557  Tool.setDiagnosticConsumer(&DiagConsumer);
558 
559  class ActionFactory : public FrontendActionFactory {
560  public:
561  ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {}
562  FrontendAction *create() override { return new Action(&ConsumerFactory); }
563 
564  bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
565  FileManager *Files,
566  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
567  DiagnosticConsumer *DiagConsumer) override {
568  // Explicitly set ProgramAction to RunAnalysis to make the preprocessor
569  // define __clang_analyzer__ macro. The frontend analyzer action will not
570  // be called here.
571  Invocation->getFrontendOpts().ProgramAction = frontend::RunAnalysis;
572  return FrontendActionFactory::runInvocation(
573  Invocation, Files, PCHContainerOps, DiagConsumer);
574  }
575 
576  private:
577  class Action : public ASTFrontendAction {
578  public:
579  Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
580  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
581  StringRef File) override {
582  return Factory->CreateASTConsumer(Compiler, File);
583  }
584 
585  private:
587  };
588 
589  ClangTidyASTConsumerFactory ConsumerFactory;
590  };
591 
592  ActionFactory Factory(Context);
593  Tool.run(&Factory);
594  return DiagConsumer.take();
595 }
596 
597 void handleErrors(llvm::ArrayRef<ClangTidyError> Errors,
598  ClangTidyContext &Context, bool Fix,
599  unsigned &WarningsAsErrorsCount,
600  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) {
601  ErrorReporter Reporter(Context, Fix, BaseFS);
602  llvm::vfs::FileSystem &FileSystem =
603  *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
604  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
605  if (!InitialWorkingDir)
606  llvm::report_fatal_error("Cannot get current working path.");
607 
608  for (const ClangTidyError &Error : Errors) {
609  if (!Error.BuildDirectory.empty()) {
610  // By default, the working directory of file system is the current
611  // clang-tidy running directory.
612  //
613  // Change the directory to the one used during the analysis.
614  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
615  }
616  Reporter.reportDiagnostic(Error);
617  // Return to the initial directory to correctly resolve next Error.
618  FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
619  }
620  Reporter.Finish();
621  WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
622 }
623 
624 void exportReplacements(const llvm::StringRef MainFilePath,
625  const std::vector<ClangTidyError> &Errors,
626  raw_ostream &OS) {
627  TranslationUnitDiagnostics TUD;
628  TUD.MainSourceFile = MainFilePath;
629  for (const auto &Error : Errors) {
630  tooling::Diagnostic Diag = Error;
631  TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
632  }
633 
634  yaml::Output YAML(OS);
635  YAML << TUD;
636 }
637 
638 } // namespace tidy
639 } // 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:412
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:461
llvm::Optional< ArgList > ExtraArgs
Add extra compilation arguments to the end of the list.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:473
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.
Optional< Expected< tooling::AtomicChanges > > Result
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:454
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
Definition: ClangTidy.cpp:429
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))
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
Contains options for clang-tidy.
std::vector< ClangTidyError > runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS, bool EnableCheckProfile, llvm::StringRef StoreCheckProfile)
Definition: ClangTidy.cpp:506
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Returns the effective check-specific options.
Definition: ClangTidy.cpp:495
A collection of ClangTidyCheckFactory instances.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
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:597
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.
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
llvm::Optional< ClangTidyProfiling::StorageParams > getProfileStorageParams() const
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.
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Definition: ClangTidy.cpp:450
===– 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:624
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options, bool AllowEnablingAnalyzerAlphaCheckers)
Fills the list of check names that are enabled when the provided filters are applied.
Definition: ClangTidy.cpp:484
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:351
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438
unique_function< void()> Action