Developer knowledge network · moderated exchange

UnreliableCode 커뮤니티

개발자 연구, 리버스 엔지니어링 및 코딩 커뮤니티

Knowledge index살다
4Categories
919Threads
2.8K게시물
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.