Skip to content

Commit 9623372

Browse files
committed
Demonstrator of a real plugin to clang-tidy
* Adding a toy plugin clang-tidy module which is compiled into a shared lib * This plugin can be activated using LD_PRELOAD * This enables (in principle) a complete decoupling of our check developments to the clang-tidy binary
1 parent cfbe47f commit 9623372

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ add_subdirectory(aliceO2)
4646
# the specific reporting tools code
4747
add_subdirectory(reporting)
4848

49+
# plugin
50+
add_subdirectory(plugin)
4951

5052
# for testing
5153
add_subdirectory(test)

plugin/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(LLVM_LINK_COMPONENTS support)
2+
3+
add_clang_library(clangTidyPluginModule
4+
SHARED
5+
6+
FooCheck.cpp
7+
PluginTidyModule.cpp
8+
9+
LINK_LIBS
10+
clangAST
11+
clangASTMatchers
12+
clangBasic
13+
clangLex
14+
clangTidy
15+
clangTidyUtils
16+
clangTooling
17+
)

plugin/FooCheck.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- FooCheck.cpp - clang-tidy-----------------------------------------===//
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 "FooCheck.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang {
17+
namespace tidy {
18+
namespace plugin {
19+
20+
void FooCheck::registerMatchers(MatchFinder *Finder) {
21+
// FIXME: Add matchers.
22+
Finder->addMatcher(functionDecl().bind("x"), this);
23+
}
24+
25+
void FooCheck::check(const MatchFinder::MatchResult &Result) {
26+
// FIXME: Add callback implementation.
27+
const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
28+
diag(MatchedDecl->getLocation(), "you should not use functions")
29+
<< MatchedDecl;
30+
}
31+
32+
} // namespace plugin
33+
} // namespace tidy
34+
} // namespace clang

plugin/FooCheck.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- FooCheck.h - clang-tidy---------------------------------*- C++ -*-===//
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+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PLUGIN_FOO_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PLUGIN_FOO_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace plugin {
18+
19+
/// FIXME: Write a short description.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/plugin-Foo.html
23+
class FooCheck : public ClangTidyCheck {
24+
public:
25+
FooCheck(StringRef Name, ClangTidyContext *Context)
26+
: ClangTidyCheck(Name, Context) {}
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
};
30+
31+
} // namespace plugin
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PLUGIN_FOO_H

plugin/PluginTidyModule.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===--- AliceO2TidyModule.cpp - clang-tidy ----------------------------------===//
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"
12+
#include "../ClangTidyModuleRegistry.h"
13+
#include "FooCheck.h"
14+
#include <iostream>
15+
16+
namespace clang {
17+
namespace tidy {
18+
namespace plugin {
19+
20+
class PluginModule : public ClangTidyModule {
21+
public:
22+
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
23+
std::cerr << "side effect \n";
24+
CheckFactories.registerCheck<FooCheck>(
25+
"plugin-Foo");
26+
}
27+
};
28+
29+
} // namespace plugin
30+
31+
// Register the AliceO2TidyModule using this statically initialized variable.
32+
static ClangTidyModuleRegistry::Add<plugin::PluginModule>
33+
X("aliceO2-module", "Adds AliceO2 specific checks");
34+
35+
// This anchor is used to force the linker to link in the generated object file
36+
// and thus register the AliceO2Module.
37+
volatile int PluginModuleAnchorSource = 0;
38+
39+
} // namespace tidy
40+
} // namespace clang
41+
42+
43+
// A function to execute upon load of shared library
44+
//__attribute__((constructor))
45+
static int huhuhuhuhu() {
46+
static clang::tidy::plugin::PluginModule module;
47+
return clang::tidy::PluginModuleAnchorSource;
48+
}

0 commit comments

Comments
 (0)