xref: /llvm-project/clang-tools-extra/test/clang-tidy/CTTestTidyModule.cpp (revision 8b0d38be9ece414dbc7a91dbd8fa506b0e3287e7)
184f137a5SJameson Nash // REQUIRES: plugins
2*8b0d38beSAndrzej Warzyński // RUN: clang-tidy -checks='-*,mytest*' --list-checks -load %llvmshlibdir/CTTestTidyModule%pluginext | FileCheck --check-prefix=CHECK-LIST %s
384f137a5SJameson Nash // CHECK-LIST: Enabled checks:
484f137a5SJameson Nash // CHECK-LIST-NEXT:    mytest1
584f137a5SJameson Nash // CHECK-LIST-NEXT:    mytest2
684f137a5SJameson Nash // RUN: clang-tidy -checks='-*,mytest*,misc-definitions-in-headers' -load %llvmshlibdir/CTTestTidyModule%pluginext /dev/null -- -xc 2>&1 | FileCheck %s
784f137a5SJameson Nash // CHECK: 3 warnings generated.
884f137a5SJameson Nash // CHECK-NEXT: warning: mytest success [misc-definitions-in-headers,mytest1,mytest2]
984f137a5SJameson Nash 
1084f137a5SJameson Nash #include "clang-tidy/ClangTidy.h"
1184f137a5SJameson Nash #include "clang-tidy/ClangTidyCheck.h"
1284f137a5SJameson Nash #include "clang-tidy/ClangTidyModule.h"
1384f137a5SJameson Nash #include "clang-tidy/ClangTidyModuleRegistry.h"
1484f137a5SJameson Nash #include "clang/AST/ASTContext.h"
1584f137a5SJameson Nash #include "clang/ASTMatchers/ASTMatchFinder.h"
1684f137a5SJameson Nash 
1784f137a5SJameson Nash using namespace clang;
1884f137a5SJameson Nash using namespace clang::tidy;
1984f137a5SJameson Nash using namespace clang::ast_matchers;
2084f137a5SJameson Nash 
2184f137a5SJameson Nash namespace {
2284f137a5SJameson Nash class MyTestCheck : public ClangTidyCheck {
2384f137a5SJameson Nash 
2484f137a5SJameson Nash public:
MyTestCheck(StringRef Name,ClangTidyContext * Context)2584f137a5SJameson Nash   MyTestCheck(StringRef Name, ClangTidyContext *Context)
2684f137a5SJameson Nash       : ClangTidyCheck(Name, Context) {}
2784f137a5SJameson Nash 
registerMatchers(ast_matchers::MatchFinder * Finder)2884f137a5SJameson Nash   void registerMatchers(ast_matchers::MatchFinder *Finder) override {
2984f137a5SJameson Nash     Finder->addMatcher(translationUnitDecl().bind("tu"), this);
3084f137a5SJameson Nash   }
3184f137a5SJameson Nash 
check(const ast_matchers::MatchFinder::MatchResult & Result)3284f137a5SJameson Nash   void check(const ast_matchers::MatchFinder::MatchResult &Result) override {
3384f137a5SJameson Nash     auto S = Result.Nodes.getNodeAs<TranslationUnitDecl>("tu");
3484f137a5SJameson Nash     if (S)
3584f137a5SJameson Nash       diag("mytest success");
3684f137a5SJameson Nash   }
3784f137a5SJameson Nash 
3884f137a5SJameson Nash private:
3984f137a5SJameson Nash };
4084f137a5SJameson Nash 
4184f137a5SJameson Nash class CTTestModule : public ClangTidyModule {
4284f137a5SJameson Nash public:
addCheckFactories(ClangTidyCheckFactories & CheckFactories)4384f137a5SJameson Nash   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
4484f137a5SJameson Nash     CheckFactories.registerCheck<MyTestCheck>("mytest1");
4584f137a5SJameson Nash     CheckFactories.registerCheck<MyTestCheck>("mytest2");
4684f137a5SJameson Nash     // intentionally collide with an existing test name, overriding it
4784f137a5SJameson Nash     CheckFactories.registerCheck<MyTestCheck>("misc-definitions-in-headers");
4884f137a5SJameson Nash   }
4984f137a5SJameson Nash };
5084f137a5SJameson Nash } // namespace
5184f137a5SJameson Nash 
5284f137a5SJameson Nash namespace tidy1 {
5384f137a5SJameson Nash // Register the CTTestTidyModule using this statically initialized variable.
5484f137a5SJameson Nash static ClangTidyModuleRegistry::Add<::CTTestModule>
5584f137a5SJameson Nash     X("mytest-module", "Adds my checks.");
5684f137a5SJameson Nash } // namespace tidy1
5784f137a5SJameson Nash 
5884f137a5SJameson Nash namespace tidy2 {
5984f137a5SJameson Nash // intentionally collide with an existing test group name, merging with it
6084f137a5SJameson Nash static ClangTidyModuleRegistry::Add<::CTTestModule>
6184f137a5SJameson Nash     X("misc-module", "Adds miscellaneous lint checks.");
6284f137a5SJameson Nash } // namespace tidy2
6384f137a5SJameson Nash 
6484f137a5SJameson Nash // This anchor is used to force the linker to link in the generated object file
6584f137a5SJameson Nash // and thus register the CTTestModule.
6684f137a5SJameson Nash volatile int CTTestModuleAnchorSource = 0;
67