clang-tools  6.0.0
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 "clang/AST/ASTConsumer.h"
22 #include "clang/AST/ASTContext.h"
23 #include "clang/AST/Decl.h"
24 #include "clang/ASTMatchers/ASTMatchFinder.h"
25 #include "clang/Format/Format.h"
26 #include "clang/Frontend/ASTConsumers.h"
27 #include "clang/Frontend/CompilerInstance.h"
28 #include "clang/Frontend/FrontendActions.h"
29 #include "clang/Frontend/FrontendDiagnostic.h"
30 #include "clang/Frontend/MultiplexConsumer.h"
31 #include "clang/Frontend/TextDiagnosticPrinter.h"
32 #include "clang/Lex/PPCallbacks.h"
33 #include "clang/Lex/Preprocessor.h"
34 #include "clang/Rewrite/Frontend/FixItRewriter.h"
35 #include "clang/Rewrite/Frontend/FrontendActions.h"
36 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
37 #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
38 #include "clang/Tooling/DiagnosticsYaml.h"
39 #include "clang/Tooling/Refactoring.h"
40 #include "clang/Tooling/ReplacementsYaml.h"
41 #include "clang/Tooling/Tooling.h"
42 #include "llvm/Support/Process.h"
43 #include "llvm/Support/Signals.h"
44 #include <algorithm>
45 #include <utility>
46 
47 using namespace clang::ast_matchers;
48 using namespace clang::driver;
49 using namespace clang::tooling;
50 using namespace llvm;
51 
52 LLVM_INSTANTIATE_REGISTRY(clang::tidy::ClangTidyModuleRegistry)
53 
54 namespace clang {
55 namespace tidy {
56 
57 namespace {
58 static const char *AnalyzerCheckNamePrefix = "clang-analyzer-";
59 
60 class AnalyzerDiagnosticConsumer : public ento::PathDiagnosticConsumer {
61 public:
62  AnalyzerDiagnosticConsumer(ClangTidyContext &Context) : Context(Context) {}
63 
64  void FlushDiagnosticsImpl(std::vector<const ento::PathDiagnostic *> &Diags,
65  FilesMade *filesMade) override {
66  for (const ento::PathDiagnostic *PD : Diags) {
67  SmallString<64> CheckName(AnalyzerCheckNamePrefix);
68  CheckName += PD->getCheckName();
69  Context.diag(CheckName, PD->getLocation().asLocation(),
70  PD->getShortDescription())
71  << PD->path.back()->getRanges();
72 
73  for (const auto &DiagPiece :
74  PD->path.flatten(/*ShouldFlattenMacros=*/true)) {
75  Context.diag(CheckName, DiagPiece->getLocation().asLocation(),
76  DiagPiece->getString(), DiagnosticIDs::Note)
77  << DiagPiece->getRanges();
78  }
79  }
80  }
81 
82  StringRef getName() const override { return "ClangTidyDiags"; }
83  bool supportsLogicalOpControlFlow() const override { return true; }
84  bool supportsCrossFileDiagnostics() const override { return true; }
85 
86 private:
87  ClangTidyContext &Context;
88 };
89 
90 class ErrorReporter {
91 public:
92  ErrorReporter(ClangTidyContext &Context, bool ApplyFixes)
93  : Files(FileSystemOptions()), DiagOpts(new DiagnosticOptions()),
94  DiagPrinter(new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts)),
95  Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts,
96  DiagPrinter),
97  SourceMgr(Diags, Files), Context(Context), ApplyFixes(ApplyFixes),
98  TotalFixes(0), AppliedFixes(0), WarningsAsErrors(0) {
99  DiagOpts->ShowColors = llvm::sys::Process::StandardOutHasColors();
100  DiagPrinter->BeginSourceFile(LangOpts);
101  }
102 
103  SourceManager &getSourceManager() { return SourceMgr; }
104 
105  void reportDiagnostic(const ClangTidyError &Error) {
106  const tooling::DiagnosticMessage &Message = Error.Message;
107  SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
108  // Contains a pair for each attempted fix: location and whether the fix was
109  // applied successfully.
110  SmallVector<std::pair<SourceLocation, bool>, 4> FixLocations;
111  {
112  auto Level = static_cast<DiagnosticsEngine::Level>(Error.DiagLevel);
113  std::string Name = Error.DiagnosticName;
114  if (Error.IsWarningAsError) {
115  Name += ",-warnings-as-errors";
116  Level = DiagnosticsEngine::Error;
118  }
119  auto Diag = Diags.Report(Loc, Diags.getCustomDiagID(Level, "%0 [%1]"))
120  << Message.Message << Name;
121  for (const auto &FileAndReplacements : Error.Fix) {
122  for (const auto &Repl : FileAndReplacements.second) {
123  // Retrieve the source range for applicable fixes. Macro definitions
124  // on the command line have locations in a virtual buffer and don't
125  // have valid file paths and are therefore not applicable.
126  SourceRange Range;
127  SourceLocation FixLoc;
128  ++TotalFixes;
129  bool CanBeApplied = false;
130  if (Repl.isApplicable()) {
131  SmallString<128> FixAbsoluteFilePath = Repl.getFilePath();
132  Files.makeAbsolutePath(FixAbsoluteFilePath);
133  if (ApplyFixes) {
134  tooling::Replacement R(FixAbsoluteFilePath, Repl.getOffset(),
135  Repl.getLength(),
136  Repl.getReplacementText());
137  Replacements &Replacements = FileReplacements[R.getFilePath()];
138  llvm::Error Err = Replacements.add(R);
139  if (Err) {
140  // FIXME: Implement better conflict handling.
141  llvm::errs() << "Trying to resolve conflict: "
142  << llvm::toString(std::move(Err)) << "\n";
143  unsigned NewOffset =
144  Replacements.getShiftedCodePosition(R.getOffset());
145  unsigned NewLength = Replacements.getShiftedCodePosition(
146  R.getOffset() + R.getLength()) -
147  NewOffset;
148  if (NewLength == R.getLength()) {
149  R = Replacement(R.getFilePath(), NewOffset, NewLength,
150  R.getReplacementText());
151  Replacements = Replacements.merge(tooling::Replacements(R));
152  CanBeApplied = true;
153  ++AppliedFixes;
154  } else {
155  llvm::errs()
156  << "Can't resolve conflict, skipping the replacement.\n";
157  }
158 
159  } else {
160  CanBeApplied = true;
161  ++AppliedFixes;
162  }
163  }
164  FixLoc = getLocation(FixAbsoluteFilePath, Repl.getOffset());
165  SourceLocation FixEndLoc =
166  FixLoc.getLocWithOffset(Repl.getLength());
167  Range = SourceRange(FixLoc, FixEndLoc);
168  Diag << FixItHint::CreateReplacement(Range,
169  Repl.getReplacementText());
170  }
171 
172  if (ApplyFixes)
173  FixLocations.push_back(std::make_pair(FixLoc, CanBeApplied));
174  }
175  }
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  const FileEntry *File = SourceMgr.getFileManager().getFile(FilePath);
242  FileID ID = SourceMgr.getOrCreateFileID(File, SrcMgr::C_User);
243  return SourceMgr.getLocForStartOfFile(ID).getLocWithOffset(Offset);
244  }
245 
246  void reportNote(const tooling::DiagnosticMessage &Message) {
247  SourceLocation Loc = getLocation(Message.FilePath, Message.FileOffset);
248  Diags.Report(Loc, Diags.getCustomDiagID(DiagnosticsEngine::Note, "%0"))
249  << Message.Message;
250  }
251 
252  FileManager Files;
253  LangOptions LangOpts; // FIXME: use langopts from each original file
254  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts;
255  DiagnosticConsumer *DiagPrinter;
256  DiagnosticsEngine Diags;
257  SourceManager SourceMgr;
258  llvm::StringMap<Replacements> FileReplacements;
259  ClangTidyContext &Context;
260  bool ApplyFixes;
261  unsigned TotalFixes;
262  unsigned AppliedFixes;
263  unsigned WarningsAsErrors;
264 };
265 
266 class ClangTidyASTConsumer : public MultiplexConsumer {
267 public:
268  ClangTidyASTConsumer(std::vector<std::unique_ptr<ASTConsumer>> Consumers,
269  std::unique_ptr<ast_matchers::MatchFinder> Finder,
270  std::vector<std::unique_ptr<ClangTidyCheck>> Checks)
271  : MultiplexConsumer(std::move(Consumers)), Finder(std::move(Finder)),
272  Checks(std::move(Checks)) {}
273 
274 private:
275  std::unique_ptr<ast_matchers::MatchFinder> Finder;
276  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
277 };
278 
279 } // namespace
280 
281 ClangTidyASTConsumerFactory::ClangTidyASTConsumerFactory(
282  ClangTidyContext &Context)
283  : Context(Context), CheckFactories(new ClangTidyCheckFactories) {
284  for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
285  E = ClangTidyModuleRegistry::end();
286  I != E; ++I) {
287  std::unique_ptr<ClangTidyModule> Module(I->instantiate());
288  Module->addCheckFactories(*CheckFactories);
289  }
290 }
291 
293  AnalyzerOptionsRef AnalyzerOptions) {
294  StringRef AnalyzerPrefix(AnalyzerCheckNamePrefix);
295  for (const auto &Opt : Opts.CheckOptions) {
296  StringRef OptName(Opt.first);
297  if (!OptName.startswith(AnalyzerPrefix))
298  continue;
299  AnalyzerOptions->Config[OptName.substr(AnalyzerPrefix.size())] = Opt.second;
300  }
301 }
302 
303 typedef std::vector<std::pair<std::string, bool>> CheckersList;
304 
305 static CheckersList getCheckersControlList(ClangTidyContext &Context) {
306  CheckersList List;
307 
308  const auto &RegisteredCheckers =
309  AnalyzerOptions::getRegisteredCheckers(/*IncludeExperimental=*/false);
310  bool AnalyzerChecksEnabled = false;
311  for (StringRef CheckName : RegisteredCheckers) {
312  std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
313  AnalyzerChecksEnabled |= Context.isCheckEnabled(ClangTidyCheckName);
314  }
315 
316  if (!AnalyzerChecksEnabled)
317  return List;
318 
319  // List all static analyzer checkers that our filter enables.
320  //
321  // Always add all core checkers if any other static analyzer check is enabled.
322  // This is currently necessary, as other path sensitive checks rely on the
323  // core checkers.
324  for (StringRef CheckName : RegisteredCheckers) {
325  std::string ClangTidyCheckName((AnalyzerCheckNamePrefix + CheckName).str());
326 
327  if (CheckName.startswith("core") ||
328  Context.isCheckEnabled(ClangTidyCheckName)) {
329  List.emplace_back(CheckName, true);
330  }
331  }
332  return List;
333 }
334 
335 std::unique_ptr<clang::ASTConsumer>
337  clang::CompilerInstance &Compiler, StringRef File) {
338  // FIXME: Move this to a separate method, so that CreateASTConsumer doesn't
339  // modify Compiler.
340  Context.setSourceManager(&Compiler.getSourceManager());
341  Context.setCurrentFile(File);
342  Context.setASTContext(&Compiler.getASTContext());
343 
344  auto WorkingDir = Compiler.getSourceManager()
345  .getFileManager()
346  .getVirtualFileSystem()
347  ->getCurrentWorkingDirectory();
348  if (WorkingDir)
349  Context.setCurrentBuildDirectory(WorkingDir.get());
350 
351  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
352  CheckFactories->createChecks(&Context, Checks);
353 
354  ast_matchers::MatchFinder::MatchFinderOptions FinderOptions;
355  if (auto *P = Context.getCheckProfileData())
356  FinderOptions.CheckProfiling.emplace(P->Records);
357 
358  std::unique_ptr<ast_matchers::MatchFinder> Finder(
359  new ast_matchers::MatchFinder(std::move(FinderOptions)));
360 
361  for (auto &Check : Checks) {
362  Check->registerMatchers(&*Finder);
363  Check->registerPPCallbacks(Compiler);
364  }
365 
366  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
367  if (!Checks.empty())
368  Consumers.push_back(Finder->newASTConsumer());
369 
370  AnalyzerOptionsRef AnalyzerOptions = Compiler.getAnalyzerOpts();
371  // FIXME: Remove this option once clang's cfg-temporary-dtors option defaults
372  // to true.
373  AnalyzerOptions->Config["cfg-temporary-dtors"] =
374  Context.getOptions().AnalyzeTemporaryDtors ? "true" : "false";
375 
376  AnalyzerOptions->CheckersControlList = getCheckersControlList(Context);
377  if (!AnalyzerOptions->CheckersControlList.empty()) {
378  setStaticAnalyzerCheckerOpts(Context.getOptions(), AnalyzerOptions);
379  AnalyzerOptions->AnalysisStoreOpt = RegionStoreModel;
380  AnalyzerOptions->AnalysisDiagOpt = PD_NONE;
381  AnalyzerOptions->AnalyzeNestedBlocks = true;
382  AnalyzerOptions->eagerlyAssumeBinOpBifurcation = true;
383  std::unique_ptr<ento::AnalysisASTConsumer> AnalysisConsumer =
384  ento::CreateAnalysisConsumer(Compiler);
385  AnalysisConsumer->AddDiagnosticConsumer(
386  new AnalyzerDiagnosticConsumer(Context));
387  Consumers.push_back(std::move(AnalysisConsumer));
388  }
389  return llvm::make_unique<ClangTidyASTConsumer>(
390  std::move(Consumers), std::move(Finder), std::move(Checks));
391 }
392 
393 std::vector<std::string> ClangTidyASTConsumerFactory::getCheckNames() {
394  std::vector<std::string> CheckNames;
395  for (const auto &CheckFactory : *CheckFactories) {
396  if (Context.isCheckEnabled(CheckFactory.first))
397  CheckNames.push_back(CheckFactory.first);
398  }
399 
400  for (const auto &AnalyzerCheck : getCheckersControlList(Context))
401  CheckNames.push_back(AnalyzerCheckNamePrefix + AnalyzerCheck.first);
402 
403  std::sort(CheckNames.begin(), CheckNames.end());
404  return CheckNames;
405 }
406 
409  std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
410  CheckFactories->createChecks(&Context, Checks);
411  for (const auto &Check : Checks)
412  Check->storeOptions(Options);
413  return Options;
414 }
415 
416 DiagnosticBuilder ClangTidyCheck::diag(SourceLocation Loc, StringRef Message,
417  DiagnosticIDs::Level Level) {
418  return Context->diag(CheckName, Loc, Message, Level);
419 }
420 
421 void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
422  Context->setSourceManager(Result.SourceManager);
423  check(Result);
424 }
425 
426 OptionsView::OptionsView(StringRef CheckName,
427  const ClangTidyOptions::OptionMap &CheckOptions)
428  : NamePrefix(CheckName.str() + "."), CheckOptions(CheckOptions) {}
429 
430 std::string OptionsView::get(StringRef LocalName, StringRef Default) const {
431  const auto &Iter = CheckOptions.find(NamePrefix + LocalName.str());
432  if (Iter != CheckOptions.end())
433  return Iter->second;
434  return Default;
435 }
436 
437 std::string OptionsView::getLocalOrGlobal(StringRef LocalName,
438  StringRef Default) const {
439  auto Iter = CheckOptions.find(NamePrefix + LocalName.str());
440  if (Iter != CheckOptions.end())
441  return Iter->second;
442  // Fallback to global setting, if present.
443  Iter = CheckOptions.find(LocalName.str());
444  if (Iter != CheckOptions.end())
445  return Iter->second;
446  return Default;
447 }
448 
450  StringRef LocalName, StringRef Value) const {
451  Options[NamePrefix + LocalName.str()] = Value;
452 }
453 
455  StringRef LocalName, int64_t Value) const {
456  store(Options, LocalName, llvm::itostr(Value));
457 }
458 
459 std::vector<std::string> getCheckNames(const ClangTidyOptions &Options) {
461  llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
462  Options));
463  ClangTidyASTConsumerFactory Factory(Context);
464  return Factory.getCheckNames();
465 }
466 
469  llvm::make_unique<DefaultOptionsProvider>(ClangTidyGlobalOptions(),
470  Options));
471  ClangTidyASTConsumerFactory Factory(Context);
472  return Factory.getCheckOptions();
473 }
474 
476  const CompilationDatabase &Compilations,
477  ArrayRef<std::string> InputFiles, ProfileData *Profile) {
478  ClangTool Tool(Compilations, InputFiles);
479 
480  // Add extra arguments passed by the clang-tidy command-line.
481  ArgumentsAdjuster PerFileExtraArgumentsInserter =
482  [&Context](const CommandLineArguments &Args, StringRef Filename) {
484  CommandLineArguments AdjustedArgs = Args;
485  if (Opts.ExtraArgsBefore) {
486  auto I = AdjustedArgs.begin();
487  if (I != AdjustedArgs.end() && !StringRef(*I).startswith("-"))
488  ++I; // Skip compiler binary name, if it is there.
489  AdjustedArgs.insert(I, Opts.ExtraArgsBefore->begin(),
490  Opts.ExtraArgsBefore->end());
491  }
492  if (Opts.ExtraArgs)
493  AdjustedArgs.insert(AdjustedArgs.end(), Opts.ExtraArgs->begin(),
494  Opts.ExtraArgs->end());
495  return AdjustedArgs;
496  };
497 
498  // Remove plugins arguments.
499  ArgumentsAdjuster PluginArgumentsRemover =
500  [](const CommandLineArguments &Args, StringRef Filename) {
501  CommandLineArguments AdjustedArgs;
502  for (size_t I = 0, E = Args.size(); I < E; ++I) {
503  if (I + 4 < Args.size() && Args[I] == "-Xclang" &&
504  (Args[I + 1] == "-load" || Args[I + 1] == "-add-plugin" ||
505  StringRef(Args[I + 1]).startswith("-plugin-arg-")) &&
506  Args[I + 2] == "-Xclang") {
507  I += 3;
508  } else
509  AdjustedArgs.push_back(Args[I]);
510  }
511  return AdjustedArgs;
512  };
513 
514  Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
515  Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
516  if (Profile)
517  Context.setCheckProfileData(Profile);
518 
519  ClangTidyDiagnosticConsumer DiagConsumer(Context);
520 
521  Tool.setDiagnosticConsumer(&DiagConsumer);
522 
523  class ActionFactory : public FrontendActionFactory {
524  public:
525  ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {}
526  FrontendAction *create() override { return new Action(&ConsumerFactory); }
527 
528  private:
529  class Action : public ASTFrontendAction {
530  public:
531  Action(ClangTidyASTConsumerFactory *Factory) : Factory(Factory) {}
532  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
533  StringRef File) override {
534  return Factory->CreateASTConsumer(Compiler, File);
535  }
536 
537  private:
539  };
540 
541  ClangTidyASTConsumerFactory ConsumerFactory;
542  };
543 
544  ActionFactory Factory(Context);
545  Tool.run(&Factory);
546 }
547 
548 void handleErrors(ClangTidyContext &Context, bool Fix,
549  unsigned &WarningsAsErrorsCount) {
550  ErrorReporter Reporter(Context, Fix);
551  vfs::FileSystem &FileSystem =
552  *Reporter.getSourceManager().getFileManager().getVirtualFileSystem();
553  auto InitialWorkingDir = FileSystem.getCurrentWorkingDirectory();
554  if (!InitialWorkingDir)
555  llvm::report_fatal_error("Cannot get current working path.");
556 
557  for (const ClangTidyError &Error : Context.getErrors()) {
558  if (!Error.BuildDirectory.empty()) {
559  // By default, the working directory of file system is the current
560  // clang-tidy running directory.
561  //
562  // Change the directory to the one used during the analysis.
563  FileSystem.setCurrentWorkingDirectory(Error.BuildDirectory);
564  }
565  Reporter.reportDiagnostic(Error);
566  // Return to the initial directory to correctly resolve next Error.
567  FileSystem.setCurrentWorkingDirectory(InitialWorkingDir.get());
568  }
569  Reporter.Finish();
570  WarningsAsErrorsCount += Reporter.getWarningsAsErrorsCount();
571 }
572 
573 void exportReplacements(const llvm::StringRef MainFilePath,
574  const std::vector<ClangTidyError> &Errors,
575  raw_ostream &OS) {
576  TranslationUnitDiagnostics TUD;
577  TUD.MainSourceFile = MainFilePath;
578  for (const auto &Error : Errors) {
579  tooling::Diagnostic Diag = Error;
580  TUD.Diagnostics.insert(TUD.Diagnostics.end(), Diag);
581  }
582 
583  yaml::Output YAML(OS);
584  YAML << TUD;
585 }
586 
587 } // namespace tidy
588 } // 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:393
std::string getLocalOrGlobal(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:437
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:449
std::string get(StringRef LocalName, StringRef Default) const
Read a named option from the Context.
Definition: ClangTidy.cpp:430
StringHandle Name
ClangTidyOptions::OptionMap getCheckOptions()
Get the union of options from all checks.
Definition: ClangTidy.cpp:407
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check is enabled for the CurrentFile.
HeaderHandle File
static llvm::StringRef toString(SpecialMemberFunctionsCheck::SpecialMemberFunctionKind K)
ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options)
Returns the effective check-specific options.
Definition: ClangTidy.cpp:467
Contains options for clang-tidy.
void runClangTidy(clang::tidy::ClangTidyContext &Context, const CompilationDatabase &Compilations, ArrayRef< std::string > InputFiles, ProfileData *Profile)
Definition: ClangTidy.cpp:475
A collection of ClangTidyCheckFactory instances.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::Optional< ArgList > ExtraArgsBefore
Add extra compilation arguments to the start of the list.
std::string Filename
Filename as a string.
void setCheckProfileData(ProfileData *Profile)
Set the output struct for profile data.
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
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...
std::map< std::string, std::string > OptionMap
std::vector< std::string > getCheckNames(const ClangTidyOptions &Options)
Fills the list of check names that are enabled when the provided filters are applied.
Definition: ClangTidy.cpp:459
std::vector< std::pair< std::string, bool > > CheckersList
Definition: ClangTidy.cpp:303
void handleErrors(ClangTidyContext &Context, bool Fix, unsigned &WarningsAsErrorsCount)
Displays the found Errors to the users.
Definition: ClangTidy.cpp:548
static void setStaticAnalyzerCheckerOpts(const ClangTidyOptions &Opts, AnalyzerOptionsRef AnalyzerOptions)
Definition: ClangTidy.cpp:292
ArrayRef< ClangTidyError > getErrors() const
Returns all collected errors.
OptionsView(StringRef CheckName, const ClangTidyOptions::OptionMap &CheckOptions)
Initializes the instance using CheckName + "." as a prefix.
Definition: ClangTidy.cpp:426
void exportReplacements(const llvm::StringRef MainFilePath, const std::vector< ClangTidyError > &Errors, raw_ostream &OS)
Definition: ClangTidy.cpp:573
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:336
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))
bool applyAllReplacements(const std::vector< tooling::Replacement > &Replaces, Rewriter &Rewrite)
Container for clang-tidy profiling data.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:416
static CheckersList getCheckersControlList(ClangTidyContext &Context)
Definition: ClangTidy.cpp:305