blob: 22448939e830904b63fb7edf7be08f83f2fe58d5 [file] [log] [blame]
Benjamin Kramer8f5eb562016-03-03 08:58:12 +00001//===- 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"
Stephen Kellya3c42062018-10-01 20:24:22 +000012#include "clang/Config/config.h"
Benjamin Kramer8f5eb562016-03-03 08:58:12 +000013#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Frontend/FrontendPluginRegistry.h"
15#include "clang/Frontend/MultiplexConsumer.h"
16
17namespace clang {
18namespace 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.
22class ClangTidyPluginAction : public PluginASTAction {
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
34public:
35 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
36 StringRef File) override {
37 // Insert the current diagnostics engine.
Sam McCall3c1f4902018-11-08 17:42:16 +000038 Context->setDiagnosticsEngine(&Compiler.getDiagnostics());
Benjamin Kramer8f5eb562016-03-03 08:58:12 +000039
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
66private:
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.
74volatile int ClangTidyPluginAnchorSource = 0;
75
76static clang::FrontendPluginRegistry::Add<clang::tidy::ClangTidyPluginAction>
77 X("clang-tidy", "clang-tidy");
78
79namespace clang {
80namespace tidy {
81
Jonas Toth0b8fdd22018-07-31 15:23:49 +000082// This anchor is used to force the linker to link the AbseilModule.
83extern volatile int AbseilModuleAnchorSource;
84static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination =
85 AbseilModuleAnchorSource;
86
87// This anchor is used to force the linker to link the AndroidModule.
88extern volatile int AndroidModuleAnchorSource;
89static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination =
90 AndroidModuleAnchorSource;
91
92// This anchor is used to force the linker to link the BoostModule.
93extern volatile int BoostModuleAnchorSource;
94static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination =
95 BoostModuleAnchorSource;
96
Benjamin Kramer8f5eb562016-03-03 08:58:12 +000097// This anchor is used to force the linker to link the CERTModule.
98extern volatile int CERTModuleAnchorSource;
99static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
100 CERTModuleAnchorSource;
101
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000102// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
103extern volatile int CppCoreGuidelinesModuleAnchorSource;
104static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
105 CppCoreGuidelinesModuleAnchorSource;
106
Jonas Toth0b8fdd22018-07-31 15:23:49 +0000107// This anchor is used to force the linker to link the FuchsiaModule.
108extern volatile int FuchsiaModuleAnchorSource;
109static int LLVM_ATTRIBUTE_UNUSED FuchsiaModuleAnchorDestination =
110 FuchsiaModuleAnchorSource;
111
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000112// This anchor is used to force the linker to link the GoogleModule.
113extern volatile int GoogleModuleAnchorSource;
114static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
115 GoogleModuleAnchorSource;
116
Jonas Toth0b8fdd22018-07-31 15:23:49 +0000117// This anchor is used to force the linker to link the HICPPModule.
118extern volatile int HICPPModuleAnchorSource;
119static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination =
120 HICPPModuleAnchorSource;
121
122// This anchor is used to force the linker to link the LLVMModule.
123extern volatile int LLVMModuleAnchorSource;
124static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
125 LLVMModuleAnchorSource;
126
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000127// This anchor is used to force the linker to link the MiscModule.
128extern volatile int MiscModuleAnchorSource;
129static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
130 MiscModuleAnchorSource;
131
132// This anchor is used to force the linker to link the ModernizeModule.
133extern volatile int ModernizeModuleAnchorSource;
134static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
135 ModernizeModuleAnchorSource;
136
Stephen Kellya3c42062018-10-01 20:24:22 +0000137#if CLANG_ENABLE_STATIC_ANALYZER
Piotr Padlewski28da4002016-12-16 09:14:47 +0000138// This anchor is used to force the linker to link the MPIModule.
139extern volatile int MPIModuleAnchorSource;
140static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
Jonas Toth0b8fdd22018-07-31 15:23:49 +0000141 MPIModuleAnchorSource;
Stephen Kellya3c42062018-10-01 20:24:22 +0000142#endif
Jonas Toth0b8fdd22018-07-31 15:23:49 +0000143
144// This anchor is used to force the linker to link the ObjCModule.
145extern volatile int ObjCModuleAnchorSource;
146static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination =
147 ObjCModuleAnchorSource;
Piotr Padlewski28da4002016-12-16 09:14:47 +0000148
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000149// This anchor is used to force the linker to link the PerformanceModule.
150extern volatile int PerformanceModuleAnchorSource;
151static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
152 PerformanceModuleAnchorSource;
153
Fangrui Songc0e768d2018-03-07 16:57:42 +0000154// This anchor is used to force the linker to link the PortabilityModule.
155extern volatile int PortabilityModuleAnchorSource;
156static int LLVM_ATTRIBUTE_UNUSED PortabilityModuleAnchorDestination =
157 PortabilityModuleAnchorSource;
158
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000159// This anchor is used to force the linker to link the ReadabilityModule.
160extern volatile int ReadabilityModuleAnchorSource;
161static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
162 ReadabilityModuleAnchorSource;
163
Jonas Toth0b8fdd22018-07-31 15:23:49 +0000164// This anchor is used to force the linker to link the ZirconModule.
165extern volatile int ZirconModuleAnchorSource;
166static int LLVM_ATTRIBUTE_UNUSED ZirconModuleAnchorDestination =
167 ZirconModuleAnchorSource;
Haojian Wuabcd64c2017-10-26 08:23:20 +0000168
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000169} // namespace tidy
170} // namespace clang