Developer knowledge network · moderated exchange

UnreliableCode Topluluğu

Geliştirici Araştırması, Tersine Mühendislik ve Kodlama Topluluğu

Knowledge indexCanlı
4Categories
919Threads
2.8KGönderiler
Analysis

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

llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
Temsilci: 139
Katılım Tarihi: Jul 2018
Gönderiler: 21
Teşekkürler: 15
1 ay önce · 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
Temsilci: 119
Katılım Tarihi: Apr 2021
Gönderiler: 10
Teşekkürler: 53
1 ay önce · 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
Temsilci: 120
Katılım Tarihi: Feb 2020
Gönderiler: 12
Teşekkürler: 75
1 ay önce · Jul 5, 2026 9:01 PM
#3

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