Developer knowledge network ยท moderated exchange

Onbetrouwbare Code-gemeenschap

Ontwikkelaarsonderzoek, reverse engineering en coderingsgemeenschap

Knowledge indexLive
4Categories
919Threads
2.8KBerichten
Analysis

Clang AST Matchers: Building Custom Static Analysis Linter Rules for Codebases [StackOverflow Architecture Guide]

llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
Vertegenwoordiger: 139
Datum van deelname: Jul 2018
Berichten: 21
Bedankt: 15
1 maanden geleden ยท Jul 5, 2026 12:58 AM
#1

How to write automated AST inspection rules in Clang tooling:

To detect calls to malloc without null checks across millions of lines of code:

CPP
StatementMatcher MallocMatcher = callExpr(
    callee(functionDecl(hasName("malloc"))),
    unless(hasAncestor(ifStmt()))
).bind("unverifiedMalloc");

class MallocCallback : public MatchFinder::MatchCallback {
    void run(const MatchFinder::MatchResult& result) override {
        const auto* call = result.Nodes.getNodeAs<CallExpr>("unverifiedMalloc");
        Diagnostics.Report(call->getBeginLoc(), DiagnosticID);
    }
};
sanitizer_sam
UB Hunter
MEMBER
Vertegenwoordiger: 119
Datum van deelname: Apr 2021
Berichten: 10
Bedankt: 53
1 maanden geleden ยท Jul 5, 2026 3:03 AM
#2

Clang AST matchers are the exact mechanism behind clang-tidy checks and automated refactoring tools.

roslyn_source_gen
Roslyn Compiler Dev
MEMBER
Vertegenwoordiger: 120
Datum van deelname: Feb 2020
Berichten: 12
Bedankt: 75
1 maanden geleden ยท Jul 5, 2026 9:01 PM
#3

Infinitely more reliable than regex grep because it operates on the semantic Abstract Syntax Tree.