-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clang-tidy] support query based custom check #131804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 19 commits
a686695
421b26b
6fa8f7e
6ab7149
db6b315
dba8a8c
0df6f7e
5bb2c6c
64c45dc
b650594
b56c07c
5bbf53d
b43991f
b2cecb8
71eeaa1
8be412c
1367f0b
595e0a6
190ad52
5500699
4ce48a5
fd15b05
73dfc6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -54,6 +54,13 @@ extern volatile int CppCoreGuidelinesModuleAnchorSource; | |||||||||||||
static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination = | ||||||||||||||
CppCoreGuidelinesModuleAnchorSource; | ||||||||||||||
|
||||||||||||||
#if CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came across this code in llvm-project/clang-tools-extra/clangd/ParsedAST.cpp Lines 79 to 84 in 1f17bb1
|
||||||||||||||
// This anchor is used to force the linker to link the CustomModule. | ||||||||||||||
extern volatile int CustomModuleAnchorSource; | ||||||||||||||
static int LLVM_ATTRIBUTE_UNUSED CustomModuleAnchorDestination = | ||||||||||||||
CustomModuleAnchorSource; | ||||||||||||||
#endif | ||||||||||||||
|
||||||||||||||
// This anchor is used to force the linker to link the DarwinModule. | ||||||||||||||
extern volatile int DarwinModuleAnchorSource; | ||||||||||||||
static int LLVM_ATTRIBUTE_UNUSED DarwinModuleAnchorDestination = | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
if(CLANG_TIDY_ENABLE_QUERY_BASED_CUSTOM_CHECKS) | ||
set(LLVM_LINK_COMPONENTS | ||
support | ||
) | ||
|
||
add_clang_library(clangTidyCustomModule STATIC | ||
CustomTidyModule.cpp | ||
QueryCheck.cpp | ||
|
||
LINK_LIBS | ||
clangTidy | ||
clangTidyUtils | ||
|
||
DEPENDS | ||
ClangDriverOptions | ||
) | ||
|
||
clang_target_link_libraries(clangTidyCustomModule | ||
PRIVATE | ||
clangQuery | ||
) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "../ClangTidy.h" | ||
#include "../ClangTidyModule.h" | ||
#include "../ClangTidyModuleRegistry.h" | ||
#include "../ClangTidyOptions.h" | ||
#include "QueryCheck.h" | ||
#include "llvm/ADT/SmallSet.h" | ||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include <memory> | ||
|
||
namespace clang::tidy { | ||
namespace custom { | ||
|
||
class CustomModule : public ClangTidyModule { | ||
public: | ||
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {} | ||
}; | ||
|
||
// We need to register the checks more flexibly than builtin modules. The checks | ||
// will changed dynamically when switching to different source file. | ||
extern void registerCustomChecks(ClangTidyOptions const &Options, | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ClangTidyCheckFactories &Factories) { | ||
static llvm::SmallSet<llvm::SmallString<32>, 8> CustomCheckNames{}; | ||
if (!Options.CustomChecks.has_value() || Options.CustomChecks->empty()) | ||
return; | ||
for (llvm::SmallString<32> const &Name : CustomCheckNames) | ||
Factories.erase(Name); | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (const ClangTidyOptions::CustomCheckValue &V : | ||
Options.CustomChecks.value()) { | ||
llvm::SmallString<32> Name = llvm::StringRef{"custom-" + V.Name}; | ||
Factories.registerCheckFactory( | ||
// add custom- prefix to avoid conflicts with builtin checks | ||
Name, [&V](llvm::StringRef Name, ClangTidyContext *Context) { | ||
return std::make_unique<custom::QueryCheck>(Name, V, Context); | ||
}); | ||
CustomCheckNames.insert(std::move(Name)); | ||
} | ||
} | ||
|
||
} // namespace custom | ||
|
||
// Register the CustomTidyModule using this statically initialized variable. | ||
static ClangTidyModuleRegistry::Add<custom::CustomModule> | ||
X("custom-module", "Adds custom query lint checks."); | ||
|
||
// This anchor is used to force the linker to link in the generated object file | ||
// and thus register the AlteraModule. | ||
volatile int CustomModuleAnchorSource = 0; | ||
HerrCai0907 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} // namespace clang::tidy |
Uh oh!
There was an error while loading. Please reload this page.