Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

Analysis

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

llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
担当者: 139
参加日: Jul 2018
投稿: 21
ありがとう: 15
1 か月前 · 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
担当者: 119
参加日: Apr 2021
投稿: 10
ありがとう: 53
1 か月前 · 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
担当者: 120
参加日: Feb 2020
投稿: 12
ありがとう: 75
1 か月前 · Jul 5, 2026 9:01 PM
#3

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