blob: dc5677221715b8dc1e5955005b00f1a6df846464 [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"
12#include "clang/Frontend/CompilerInstance.h"
13#include "clang/Frontend/FrontendPluginRegistry.h"
14#include "clang/Frontend/MultiplexConsumer.h"
15
16namespace clang {
17namespace 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.
21class 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
33public:
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
65private:
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.
73volatile int ClangTidyPluginAnchorSource = 0;
74
75static clang::FrontendPluginRegistry::Add<clang::tidy::ClangTidyPluginAction>
76 X("clang-tidy", "clang-tidy");
77
78namespace clang {
79namespace tidy {
80
81// This anchor is used to force the linker to link the CERTModule.
82extern volatile int CERTModuleAnchorSource;
83static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination =
84 CERTModuleAnchorSource;
85
86// This anchor is used to force the linker to link the LLVMModule.
87extern volatile int LLVMModuleAnchorSource;
88static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination =
89 LLVMModuleAnchorSource;
90
91// This anchor is used to force the linker to link the CppCoreGuidelinesModule.
92extern volatile int CppCoreGuidelinesModuleAnchorSource;
93static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination =
94 CppCoreGuidelinesModuleAnchorSource;
95
96// This anchor is used to force the linker to link the GoogleModule.
97extern volatile int GoogleModuleAnchorSource;
98static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
99 GoogleModuleAnchorSource;
100
101// This anchor is used to force the linker to link the MiscModule.
102extern volatile int MiscModuleAnchorSource;
103static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination =
104 MiscModuleAnchorSource;
105
106// This anchor is used to force the linker to link the ModernizeModule.
107extern volatile int ModernizeModuleAnchorSource;
108static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination =
109 ModernizeModuleAnchorSource;
110
Piotr Padlewski28da4002016-12-16 09:14:47 +0000111// This anchor is used to force the linker to link the MPIModule.
112extern volatile int MPIModuleAnchorSource;
113static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination =
114 MPIModuleAnchorSource;
115
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000116// This anchor is used to force the linker to link the PerformanceModule.
117extern volatile int PerformanceModuleAnchorSource;
118static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination =
119 PerformanceModuleAnchorSource;
120
Fangrui Songc0e768d2018-03-07 16:57:42 +0000121// This anchor is used to force the linker to link the PortabilityModule.
122extern volatile int PortabilityModuleAnchorSource;
123static int LLVM_ATTRIBUTE_UNUSED PortabilityModuleAnchorDestination =
124 PortabilityModuleAnchorSource;
125
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000126// This anchor is used to force the linker to link the ReadabilityModule.
127extern volatile int ReadabilityModuleAnchorSource;
128static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination =
129 ReadabilityModuleAnchorSource;
130
Haojian Wuabcd64c2017-10-26 08:23:20 +0000131// This anchor is used to force the linker to link the ObjCModule.
132extern volatile int ObjCModuleAnchorSource;
133static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination =
134 ObjCModuleAnchorSource;
135
Benjamin Kramer8f5eb562016-03-03 08:58:12 +0000136} // namespace tidy
137} // namespace clang