Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 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" |
Stephen Kelly | a3c4206 | 2018-10-01 20:24:22 +0000 | [diff] [blame] | 12 | #include "clang/Config/config.h" |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 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. |
| 22 | class 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 | |
| 34 | public: |
| 35 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler, |
| 36 | StringRef File) override { |
| 37 | // Insert the current diagnostics engine. |
Sam McCall | 3c1f490 | 2018-11-08 17:42:16 +0000 | [diff] [blame^] | 38 | Context->setDiagnosticsEngine(&Compiler.getDiagnostics()); |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 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"); |
| 78 | |
| 79 | namespace clang { |
| 80 | namespace tidy { |
| 81 | |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 82 | // This anchor is used to force the linker to link the AbseilModule. |
| 83 | extern volatile int AbseilModuleAnchorSource; |
| 84 | static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination = |
| 85 | AbseilModuleAnchorSource; |
| 86 | |
| 87 | // This anchor is used to force the linker to link the AndroidModule. |
| 88 | extern volatile int AndroidModuleAnchorSource; |
| 89 | static int LLVM_ATTRIBUTE_UNUSED AndroidModuleAnchorDestination = |
| 90 | AndroidModuleAnchorSource; |
| 91 | |
| 92 | // This anchor is used to force the linker to link the BoostModule. |
| 93 | extern volatile int BoostModuleAnchorSource; |
| 94 | static int LLVM_ATTRIBUTE_UNUSED BoostModuleAnchorDestination = |
| 95 | BoostModuleAnchorSource; |
| 96 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 97 | // This anchor is used to force the linker to link the CERTModule. |
| 98 | extern volatile int CERTModuleAnchorSource; |
| 99 | static int LLVM_ATTRIBUTE_UNUSED CERTModuleAnchorDestination = |
| 100 | CERTModuleAnchorSource; |
| 101 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 102 | // This anchor is used to force the linker to link the CppCoreGuidelinesModule. |
| 103 | extern volatile int CppCoreGuidelinesModuleAnchorSource; |
| 104 | static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination = |
| 105 | CppCoreGuidelinesModuleAnchorSource; |
| 106 | |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 107 | // This anchor is used to force the linker to link the FuchsiaModule. |
| 108 | extern volatile int FuchsiaModuleAnchorSource; |
| 109 | static int LLVM_ATTRIBUTE_UNUSED FuchsiaModuleAnchorDestination = |
| 110 | FuchsiaModuleAnchorSource; |
| 111 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 112 | // This anchor is used to force the linker to link the GoogleModule. |
| 113 | extern volatile int GoogleModuleAnchorSource; |
| 114 | static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination = |
| 115 | GoogleModuleAnchorSource; |
| 116 | |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 117 | // This anchor is used to force the linker to link the HICPPModule. |
| 118 | extern volatile int HICPPModuleAnchorSource; |
| 119 | static int LLVM_ATTRIBUTE_UNUSED HICPPModuleAnchorDestination = |
| 120 | HICPPModuleAnchorSource; |
| 121 | |
| 122 | // This anchor is used to force the linker to link the LLVMModule. |
| 123 | extern volatile int LLVMModuleAnchorSource; |
| 124 | static int LLVM_ATTRIBUTE_UNUSED LLVMModuleAnchorDestination = |
| 125 | LLVMModuleAnchorSource; |
| 126 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 127 | // This anchor is used to force the linker to link the MiscModule. |
| 128 | extern volatile int MiscModuleAnchorSource; |
| 129 | static int LLVM_ATTRIBUTE_UNUSED MiscModuleAnchorDestination = |
| 130 | MiscModuleAnchorSource; |
| 131 | |
| 132 | // This anchor is used to force the linker to link the ModernizeModule. |
| 133 | extern volatile int ModernizeModuleAnchorSource; |
| 134 | static int LLVM_ATTRIBUTE_UNUSED ModernizeModuleAnchorDestination = |
| 135 | ModernizeModuleAnchorSource; |
| 136 | |
Stephen Kelly | a3c4206 | 2018-10-01 20:24:22 +0000 | [diff] [blame] | 137 | #if CLANG_ENABLE_STATIC_ANALYZER |
Piotr Padlewski | 28da400 | 2016-12-16 09:14:47 +0000 | [diff] [blame] | 138 | // This anchor is used to force the linker to link the MPIModule. |
| 139 | extern volatile int MPIModuleAnchorSource; |
| 140 | static int LLVM_ATTRIBUTE_UNUSED MPIModuleAnchorDestination = |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 141 | MPIModuleAnchorSource; |
Stephen Kelly | a3c4206 | 2018-10-01 20:24:22 +0000 | [diff] [blame] | 142 | #endif |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 143 | |
| 144 | // This anchor is used to force the linker to link the ObjCModule. |
| 145 | extern volatile int ObjCModuleAnchorSource; |
| 146 | static int LLVM_ATTRIBUTE_UNUSED ObjCModuleAnchorDestination = |
| 147 | ObjCModuleAnchorSource; |
Piotr Padlewski | 28da400 | 2016-12-16 09:14:47 +0000 | [diff] [blame] | 148 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 149 | // This anchor is used to force the linker to link the PerformanceModule. |
| 150 | extern volatile int PerformanceModuleAnchorSource; |
| 151 | static int LLVM_ATTRIBUTE_UNUSED PerformanceModuleAnchorDestination = |
| 152 | PerformanceModuleAnchorSource; |
| 153 | |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 154 | // This anchor is used to force the linker to link the PortabilityModule. |
| 155 | extern volatile int PortabilityModuleAnchorSource; |
| 156 | static int LLVM_ATTRIBUTE_UNUSED PortabilityModuleAnchorDestination = |
| 157 | PortabilityModuleAnchorSource; |
| 158 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 159 | // This anchor is used to force the linker to link the ReadabilityModule. |
| 160 | extern volatile int ReadabilityModuleAnchorSource; |
| 161 | static int LLVM_ATTRIBUTE_UNUSED ReadabilityModuleAnchorDestination = |
| 162 | ReadabilityModuleAnchorSource; |
| 163 | |
Jonas Toth | 0b8fdd2 | 2018-07-31 15:23:49 +0000 | [diff] [blame] | 164 | // This anchor is used to force the linker to link the ZirconModule. |
| 165 | extern volatile int ZirconModuleAnchorSource; |
| 166 | static int LLVM_ATTRIBUTE_UNUSED ZirconModuleAnchorDestination = |
| 167 | ZirconModuleAnchorSource; |
Haojian Wu | abcd64c | 2017-10-26 08:23:20 +0000 | [diff] [blame] | 168 | |
Benjamin Kramer | 8f5eb56 | 2016-03-03 08:58:12 +0000 | [diff] [blame] | 169 | } // namespace tidy |
| 170 | } // namespace clang |