clang-tools 22.0.0git
ClangTidyPlugin.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#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::tidy {
17namespace {
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 and diagnostics engine the same lifetime as
23 /// the action.
24 /// We use MultiplexConsumer to avoid writing out all the forwarding methods.
25 class WrapConsumer : public MultiplexConsumer {
26 std::unique_ptr<ClangTidyContext> Context;
27 std::unique_ptr<DiagnosticsEngine> DiagEngine;
28
29 public:
30 WrapConsumer(std::unique_ptr<ClangTidyContext> Context,
31 std::unique_ptr<DiagnosticsEngine> DiagEngine,
32 std::vector<std::unique_ptr<ASTConsumer>> Consumer)
33 : MultiplexConsumer(std::move(Consumer)), Context(std::move(Context)),
34 DiagEngine(std::move(DiagEngine)) {}
35 };
36
37public:
38 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
39 StringRef File) override {
40 // Create and set diagnostics engine
41 auto *DiagConsumer =
42 new ClangTidyDiagnosticConsumer(*Context, &Compiler.getDiagnostics());
43 auto DiagOpts = std::make_unique<DiagnosticOptions>();
44 auto DiagEngine = std::make_unique<DiagnosticsEngine>(
45 DiagnosticIDs::create(), *DiagOpts, DiagConsumer);
46 Context->setDiagnosticsEngine(std::move(DiagOpts), DiagEngine.get());
47
48 // Create the AST consumer.
49 ClangTidyASTConsumerFactory Factory(*Context);
50 std::vector<std::unique_ptr<ASTConsumer>> Vec;
51 Vec.push_back(Factory.createASTConsumer(Compiler, File));
52
53 return std::make_unique<WrapConsumer>(
54 std::move(Context), std::move(DiagEngine), std::move(Vec));
55 }
56
57 bool ParseArgs(const CompilerInstance &,
58 const std::vector<std::string> &Args) override {
59 const ClangTidyGlobalOptions GlobalOptions;
60 const ClangTidyOptions DefaultOptions;
61 ClangTidyOptions OverrideOptions;
62
63 // Parse the extra command line args.
64 // FIXME: This is very limited at the moment.
65 for (const StringRef Arg : Args)
66 if (Arg.starts_with("-checks="))
67 OverrideOptions.Checks = std::string(Arg.substr(strlen("-checks=")));
68
69 auto Options = std::make_unique<FileOptionsProvider>(
70 GlobalOptions, DefaultOptions, OverrideOptions);
71 Context = std::make_unique<ClangTidyContext>(std::move(Options));
72 return true;
73 }
74
75private:
76 std::unique_ptr<ClangTidyContext> Context;
77};
78
79} // namespace
80} // namespace clang::tidy
81
82// This anchor is used to force the linker to link in the generated object file
83// and thus register the clang-tidy plugin.
84// NOLINTNEXTLINE(misc-use-internal-linkage)
86
87static clang::FrontendPluginRegistry::Add<clang::tidy::ClangTidyPluginAction>
88 X("clang-tidy", "clang-tidy");
volatile int ClangTidyPluginAnchorSource
static clang::FrontendPluginRegistry::Add< clang::tidy::ClangTidyPluginAction > X("clang-tidy", "clang-tidy")
std::optional< std::string > Checks
Checks filter.