1// REQUIRES: plugins
2// RUN: clang-tidy -checks='-*,mytest*' --list-checks -load %llvmshlibdir/CTTestTidyModule%pluginext -load %llvmshlibdir/LLVMHello%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
17using namespace clang;
18using namespace clang::tidy;
19using namespace clang::ast_matchers;
20
21namespace {
22class MyTestCheck : public ClangTidyCheck {
23
24public:
25 MyTestCheck(StringRef Name, ClangTidyContext *Context)
26 : ClangTidyCheck(Name, Context) {}
27
28 void registerMatchers(ast_matchers::MatchFinder *Finder) override {
29 Finder->addMatcher(NodeMatch: translationUnitDecl().bind(ID: "tu"), Action: this);
30 }
31
32 void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
33 auto S = Result.Nodes.getNodeAs<TranslationUnitDecl>(ID: "tu");
34 if (S)
35 diag(Description: "mytest success");
36 }
37
38private:
39};
40
41class CTTestModule : public ClangTidyModule {
42public:
43 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
44 CheckFactories.registerCheck<MyTestCheck>(CheckName: "mytest1");
45 CheckFactories.registerCheck<MyTestCheck>(CheckName: "mytest2");
46 // intentionally collide with an existing test name, overriding it
47 CheckFactories.registerCheck<MyTestCheck>(CheckName: "misc-definitions-in-headers");
48 }
49};
50} // namespace
51
52namespace tidy1 {
53// Register the CTTestTidyModule using this statically initialized variable.
54static ClangTidyModuleRegistry::Add<::CTTestModule>
55 X("mytest-module", "Adds my checks.");
56} // namespace tidy1
57
58namespace tidy2 {
59// intentionally collide with an existing test group name, merging with it
60static 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.
66volatile int CTTestModuleAnchorSource = 0;
67

source code of clang-tools-extra/test/clang-tidy/CTTestTidyModule.cpp