clang-tools 6.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 "../ClangTidyModule.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Frontend/FrontendPluginRegistry.h"
14 #include "clang/Frontend/MultiplexConsumer.h"
15
16 namespace clang {
17 namespace tidy {
18
19 /// The core clang tidy plugin action. This just provides the AST consumer and
20 /// command line flag parsing for using clang-tidy as a clang plugin.
21 class ClangTidyPluginAction : public PluginASTAction {
22 /// Wrapper to grant the context the same lifetime as the action. We use
23 /// MultiplexConsumer to avoid writing out all the forwarding methods.
24 class WrapConsumer : public MultiplexConsumer {
25 std::unique_ptr<ClangTidyContext> Context;
26
27 public:
28 WrapConsumer(std::unique_ptr<ClangTidyContext> Context,
29 std::vector<std::unique_ptr<ASTConsumer>> Consumer)
30 : MultiplexConsumer(std::move(Consumer)), Context(std::move(Context)) {}
31 };
32
33 public:
34 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
35 StringRef File) override {
36 // Insert the current diagnostics engine.
37 Context->setDiagnosticsEngine(&Compiler.getDiagnostics());
38
39 // Create the AST consumer.
40 ClangTidyASTConsumerFactory Factory(*Context);
41 std::vector<std::unique_ptr<ASTConsumer>> Vec;
42 Vec.push_back(Factory.CreateASTConsumer(Compiler, File));
43
44 return llvm::make_unique<WrapConsumer>(std::move(Context), std::move(Vec));
45 }
46
47 bool ParseArgs(const CompilerInstance &,
48 const std::vector<std::string> &Args) override {
49 ClangTidyGlobalOptions GlobalOptions;
50 ClangTidyOptions DefaultOptions;
51 ClangTidyOptions OverrideOptions;
52
53 // Parse the extra command line args.
54 // FIXME: This is very limited at the moment.
55 for (StringRef Arg : Args)
56 if (Arg.startswith("-checks="))
57 OverrideOptions.Checks = Arg.substr(strlen("-checks="));
58
59 auto Options = llvm::make_unique<FileOptionsProvider>(
60 GlobalOptions, DefaultOptions, OverrideOptions);
61 Context = llvm::make_unique<ClangTidyContext>(std::move(Options));
62 return true;
63 }
64
65 private:
66 std::unique_ptr<ClangTidyContext> Context;
67 };
68 } // namespace tidy
69 } // namespace clang
70
71 // This anchor is used to force the linker to link in the generated object file
72 // and thus register the clang-tidy plugin.
73 volatile int ClangTidyPluginAnchorSource = 0;
74
75 static clang::FrontendPluginRegistry::Add<clang::tidy::ClangTidyPluginAction>
76 X("clang-tidy", "clang-tidy");
77
78 namespace clang {
79 namespace tidy {
80
81 // This anchor is used to force the linker to link the CERTModule.
82 extern volatile int CERTModuleAnchorSource;
83 static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
84 CERTModuleAnchorSource;
85
86 // This anchor is used to force the linker to link the LLVMModule.
87 extern volatile int LLVMModuleAnchorSource;
88 static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
89 LLVMModuleAnchorSource;
90
91 // This anchor is used to force the linker to link the CppCoreGuidelinesModule.
92 extern volatile int CppCoreGuidelinesModuleAnchorSource;
93 static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
94 CppCoreGuidelinesModuleAnchorSource;
95
96 // This anchor is used to force the linker to link the GoogleModule.
97 extern volatile int GoogleModuleAnchorSource;
98 static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
99 GoogleModuleAnchorSource;
100
101 // This anchor is used to force the linker to link the MiscModule.
102 extern volatile int MiscModuleAnchorSource;
103 static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
104 MiscModuleAnchorSource;
105
106 // This anchor is used to force the linker to link the ModernizeModule.
107 extern volatile int ModernizeModuleAnchorSource;
108 static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
109 ModernizeModuleAnchorSource;
110
111 // This anchor is used to force the linker to link the MPIModule.
112 extern volatile int MPIModuleAnchorSource;
113 static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
114 MPIModuleAnchorSource;
115
116 // This anchor is used to force the linker to link the PerformanceModule.
117 extern volatile int PerformanceModuleAnchorSource;
118 static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
119 PerformanceModuleAnchorSource;
120
121 // This anchor is used to force the linker to link the ReadabilityModule.
122 extern volatile int ReadabilityModuleAnchorSource;
123 static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
124 ReadabilityModuleAnchorSource;
125
126 // This anchor is used to force the linker to link the ObjCModule.
127 extern volatile int ObjCModuleAnchorSource;
128 static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination =
129 ObjCModuleAnchorSource;
130
131 } // namespace tidy
132 } // namespace clang
clang::tidy::ClangTidyOptions::Checks
llvm::Optional< std::string > Checks
Checks filter.
Definition: ClangTidyOptions.h:63
clang::tidy::GoogleModuleAnchorSource
volatile int GoogleModuleAnchorSource
Definition: GoogleTidyModule.cpp:102
clang::tidy::ReadabilityModuleAnchorSource
volatile int ReadabilityModuleAnchorSource
Definition: ReadabilityTidyModule.cpp:107
clang::tidy::ClangTidyPluginAction::ParseArgs
bool ParseArgs(const CompilerInstance &, const std::vector< std::string > &Args) override
Definition: ClangTidyPlugin.cpp:47
clang::tidy::ClangTidyASTConsumerFactory
Definition: ClangTidy.h:192
File
HeaderHandle File
Definition: PreprocessorTracker.cpp:526
clang::tidy::LLVMModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:88
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:49
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:41
clang::tidy::LLVMModuleAnchorSource
volatile int LLVMModuleAnchorSource
Definition: LLVMTidyModule.cpp:41
clang::tidy::PerformanceModuleAnchorSource
volatile int PerformanceModuleAnchorSource
Definition: PerformanceTidyModule.cpp:68
ClangTidyPluginAnchorSource
volatile int ClangTidyPluginAnchorSource
Definition: ClangTidyPlugin.cpp:73
clang::tidy::CppCoreGuidelinesModuleAnchorSource
volatile int CppCoreGuidelinesModuleAnchorSource
Definition: CppCoreGuidelinesTidyModule.cpp:79
clang::tidy::ObjCModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:128
clang::tidy::ReadabilityModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:123
clang::tidy::MPIModuleAnchorSource
volatile int MPIModuleAnchorSource
Definition: MPITidyModule.cpp:36
PluginASTAction
clang::tidy::CERTModuleAnchorSource
volatile int CERTModuleAnchorSource
Definition: CERTTidyModule.cpp:87
clang::tidy::X
static ClangTidyModuleRegistry::Add< bugprone::BugproneModule > X("bugprone-module", "Adds checks for bugprone code constructs.")
clang::tidy::GoogleModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:98
clang::tidy::ObjCModuleAnchorSource
volatile int ObjCModuleAnchorSource
Definition: ObjCTidyModule.cpp:47
clang::tidy::MiscModuleAnchorSource
volatile int MiscModuleAnchorSource
Definition: MiscTidyModule.cpp:117
clang::tidy::PerformanceModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:118
clang::tidy::MiscModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:103
clang::tidy::CppCoreGuidelinesModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:93
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ModernizeModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:108
clang::tidy::ClangTidyASTConsumerFactory::CreateASTConsumer
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
clang::tidy::ModernizeModuleAnchorSource
volatile int ModernizeModuleAnchorSource
Definition: ModernizeTidyModule.cpp:110
clang::tidy::MPIModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:113
clang::tidy::CERTModuleAnchorDestination
static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination
Definition: ClangTidyPlugin.cpp:83
clang::tidy::ClangTidyPluginAction
The core clang tidy plugin action.
Definition: ClangTidyPlugin.cpp:21
MultiplexConsumer
clang::tidy::ClangTidyPluginAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &Compiler, StringRef File) override
Definition: ClangTidyPlugin.cpp:34