AST Matcher Rules

Feb 18, 2013

We are very excited to introduce abstract syntax tree (AST) matcher rule, as a new category of rules in addition to existing source code reader rules and AST visitor rules.

AST matcher to AST is like XPath to XML. Specific patterns in AST are described in simple, powerful, and concise representations. We can write rules with lightweight code and better readability.

For instance, in order to find an if statement, we can simply write our matcher like ifStmt().

AST matcher rule is implemented by AST visitor rule under the hook, but since AST matcher is on a higher abstraction layer, it is more descriptive compared to explicit AST traversal. Most AST matchers can be read very close to English sentences.

Using AST matchers has more restrictions than AST visitor, and it takes much longer time in analysis process, this could lower the performance of the tool. So, we always have to consider the trade-off, and choose wisely between AST visitor and AST matcher.

AST matcher opens a new gate for us. Especially for people who write their custom rules. For many cases, traversing AST and figuring out very low level AST nodes are too heavy to realize very simple task. Instead, by hiding these details but writing only AST matchers, people are called back with all AST nodes that match.

Thanks to handy Clang LibASTMatchers, writing AST matcher rules becomes quite easy and intuitive.

Read more discussions on rules internal documentation. We are also drafting a new section in writing custom rules page about writing AST matcher rules. Related Clang knowledge can be found here.

We are looking forward to seeing more rules are written by AST matcher from now on.