xref: /llvm-project/clang-tools-extra/test/clang-tidy/CTTestTidyModule.cpp (revision 8b0d38be9ece414dbc7a91dbd8fa506b0e3287e7)
1 // REQUIRES: plugins
2 // RUN: clang-tidy -checks='-*,mytest*' --list-checks -load %llvmshlibdir/CTTestTidyModule%pluginext | FileCheck --check-prefix=CHECK-LIST %s
3 // CHECK-LIST: Enabled checks:
4 // CHECK-LIST-NEXT:    mytest1
5 // CHECK-LIST-NEXT:    mytest2
6 // RUN: clang-tidy -checks='-*,mytest*,misc-definitions-in-headers' -load %llvmshlibdir/CTTestTidyModule%pluginext /dev/null -- -xc 2>&1 | FileCheck %s
7 // CHECK: 3 warnings generated.
8 // CHECK-NEXT: warning: mytest success [misc-definitions-in-headers,mytest1,mytest2]
9 
10 #include "clang-tidy/ClangTidy.h"
11 #include "clang-tidy/ClangTidyCheck.h"
12 #include "clang-tidy/ClangTidyModule.h"
13 #include "clang-tidy/ClangTidyModuleRegistry.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 
17 using namespace clang;
18 using namespace clang::tidy;
19 using namespace clang::ast_matchers;
20 
21 namespace {
22 class MyTestCheck : public ClangTidyCheck {
23 
24 public:
MyTestCheck(StringRef Name,ClangTidyContext * Context)25   MyTestCheck(StringRef Name, ClangTidyContext *Context)
26       : ClangTidyCheck(Name, Context) {}
27 
registerMatchers(ast_matchers::MatchFinder * Finder)28   void registerMatchers(ast_matchers::MatchFinder *Finder) override {
29     Finder->addMatcher(translationUnitDecl().bind("tu"), this);
30   }
31 
check(const ast_matchers::MatchFinder::MatchResult & Result)32   void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
33     auto S = Result.Nodes.getNodeAs<TranslationUnitDecl>("tu");
34     if (S)
35       diag("mytest success");
36   }
37 
38 private:
39 };
40 
41 class CTTestModule : public ClangTidyModule {
42 public:
addCheckFactories(ClangTidyCheckFactories & CheckFactories)43   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
44     CheckFactories.registerCheck<MyTestCheck>("mytest1");
45     CheckFactories.registerCheck<MyTestCheck>("mytest2");
46     // intentionally collide with an existing test name, overriding it
47     CheckFactories.registerCheck<MyTestCheck>("misc-definitions-in-headers");
48   }
49 };
50 } // namespace
51 
52 namespace tidy1 {
53 // Register the CTTestTidyModule using this statically initialized variable.
54 static ClangTidyModuleRegistry::Add<::CTTestModule>
55     X("mytest-module", "Adds my checks.");
56 } // namespace tidy1
57 
58 namespace tidy2 {
59 // intentionally collide with an existing test group name, merging with it
60 static ClangTidyModuleRegistry::Add<::CTTestModule>
61     X("misc-module", "Adds miscellaneous lint checks.");
62 } // namespace tidy2
63 
64 // This anchor is used to force the linker to link in the generated object file
65 // and thus register the CTTestModule.
66 volatile int CTTestModuleAnchorSource = 0;
67