Skip to content

Commit

Permalink
Implement support for Clang Rulesets
Browse files Browse the repository at this point in the history
  • Loading branch information
hach-que committed Jun 26, 2024
1 parent 76f74dc commit f004a11
Show file tree
Hide file tree
Showing 7 changed files with 949 additions and 41 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class MatchFinder {
/// Finds all matches in the given AST.
void matchAST(ASTContext &Context);

void matchDecl(clang::Decl *Decl, ASTContext &Context);

/// Registers a callback to notify the end of parsing.
///
/// The provided closure is called after parsing is done, before the AST is
Expand Down
11 changes: 11 additions & 0 deletions clang/include/clang/Basic/DiagnosticCommonKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ def note_mt_message : Note<"[rewriter] %0">;
def warn_arcmt_nsalloc_realloc : Warning<"[rewriter] call returns pointer to GC managed memory; it will become unmanaged in ARC">;
def err_arcmt_nsinvocation_ownership : Error<"NSInvocation's %0 is not safe to be used with an object with ownership other than __unsafe_unretained">;

// Clang rules
def err_clangrules_message : Error<"%0">;
def warn_clangrules_message : Warning<"%0">, InGroup<DiagGroup<"clangrules">>;
def note_clangrules_message : Note<"%0">;
def err_clangrules_rule_name_is_prefixed : Error<"Rule name '%0' must be unprefixed and not contain a '/' character.">;
def err_clangrules_ruleset_name_is_prefixed : Error<"Ruleset name '%0' must be unprefixed and not contain a '/' character.">;
def err_clangrules_ruleset_severity_is_notset : Error<"Ruleset '%0' severity must be a value other than 'NotSet'.">;
def err_clangrules_rule_name_conflict : Error<"Namespaced rule name '%0' is already declared in another .clang-rules file. Try using a different namespace to avoid conflicts.">;
def err_clangrules_rule_missing : Error<"Namespaced ruleset '%0' requested namespaced rule '%1' but it wasn't known at runtime. Make sure it is declared in either the same .clang-rules file, or in a .clang-rules file in a parent directory.">;
def err_clangrules_rule_matcher_parse_failure : Error<"Namespaced rule '%0' matcher failed to parse: %1">;

// API notes
def err_apinotes_message : Error<"%0">;
def warn_apinotes_message : Warning<"%0">, InGroup<DiagGroup<"apinotes">>;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/ASTMatchers/ASTMatchFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,12 @@ void MatchFinder::matchAST(ASTContext &Context) {
Visitor.onEndOfTranslationUnit();
}

void MatchFinder::matchDecl(clang::Decl *Decl, ASTContext &Context) {
internal::MatchASTVisitor Visitor(&Matchers, Options);
Visitor.set_active_ast_context(&Context);
Visitor.getDerived().TraverseDecl(Decl);
}

void MatchFinder::registerTestCallbackAfterParsing(
MatchFinder::ParsingDoneTestCallback *NewParsingDone) {
ParsingDone = NewParsingDone;
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_clang_library(clangFrontend
ASTUnit.cpp
ChainedDiagnosticConsumer.cpp
ChainedIncludesSource.cpp
ClangRulesets.cpp
CompilerInstance.cpp
CompilerInvocation.cpp
CreateInvocationFromCommandLine.cpp
Expand Down Expand Up @@ -57,4 +58,6 @@ add_clang_library(clangFrontend
clangParse
clangSema
clangSerialization
clangASTMatchers
clangDynamicASTMatchers
)
Loading

0 comments on commit f004a11

Please sign in to comment.