clang-tools  8.0.0
ClangTidyPlugin.cpp
Go to the documentation of this file.
1 //===- ClangTidyPlugin.cpp - clang-tidy as a clang plugin -----------------===//
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 #include "../ClangTidy.h"
11 #include "../ClangTidyForceLinker.h"
12 #include "../ClangTidyModule.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Frontend/FrontendPluginRegistry.h"
15 #include "clang/Frontend/MultiplexConsumer.h"
16 
17 namespace clang {
18 namespace tidy {
19 
20 /// The core clang tidy plugin action. This just provides the AST consumer and
21 /// command line flag parsing for using clang-tidy as a clang plugin.
23  /// Wrapper to grant the context the same lifetime as the action. We use
24  /// MultiplexConsumer to avoid writing out all the forwarding methods.
25  class WrapConsumer : public MultiplexConsumer {
26  std::unique_ptr<ClangTidyContext> Context;
27 
28  public:
29  WrapConsumer(std::unique_ptr<ClangTidyContext> Context,
30  std::vector<std::unique_ptr<ASTConsumer>> Consumer)
31  : MultiplexConsumer(std::move(Consumer)), Context(std::move(Context)) {}
32  };
33 
34 public:
35  std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
36  StringRef File) override {
37  // Insert the current diagnostics engine.
38  Context->setDiagnosticsEngine(&Compiler.getDiagnostics());
39 
40  // Create the AST consumer.
41  ClangTidyASTConsumerFactory Factory(*Context);
42  std::vector<std::unique_ptr<ASTConsumer>> Vec;
43  Vec.push_back(Factory.CreateASTConsumer(Compiler, File));
44 
45  return llvm::make_unique<WrapConsumer>(std::move(Context), std::move(Vec));
46  }
47 
48  bool ParseArgs(const CompilerInstance &,
49  const std::vector<std::string> &Args) override {
50  ClangTidyGlobalOptions GlobalOptions;
51  ClangTidyOptions DefaultOptions;
52  ClangTidyOptions OverrideOptions;
53 
54  // Parse the extra command line args.
55  // FIXME: This is very limited at the moment.
56  for (StringRef Arg : Args)
57  if (Arg.startswith("-checks="))
58  OverrideOptions.Checks = Arg.substr(strlen("-checks="));
59 
60  auto Options = llvm::make_unique<FileOptionsProvider>(
61  GlobalOptions, DefaultOptions, OverrideOptions);
62  Context = llvm::make_unique<ClangTidyContext>(std::move(Options));
63  return true;
64  }
65 
66 private:
67  std::unique_ptr<ClangTidyContext> Context;
68 };
69 } // namespace tidy
70 } // namespace clang
71 
72 // This anchor is used to force the linker to link in the generated object file
73 // and thus register the clang-tidy plugin.
74 volatile int ClangTidyPluginAnchorSource = 0;
75 
76 static clang::FrontendPluginRegistry::Add<clang::tidy::ClangTidyPluginAction>
77  X("clang-tidy", "clang-tidy");
llvm::Optional< std::string > Checks
Checks filter.
bool ParseArgs(const CompilerInstance &, const std::vector< std::string > &Args) override
Contains options for clang-tidy.
volatile int ClangTidyPluginAnchorSource
static ClangTidyModuleRegistry::Add< bugprone::BugproneModule > X("bugprone-module", "Adds checks for bugprone code constructs.")
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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
The core clang tidy plugin action.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &Compiler, StringRef File) override