3.5 AST matchers
AST matchers [16] provide another approach for locating specific AST nodes. They can be particularly useful in linters when searching for improper pattern usage or in refactoring tools when identifying AST nodes for modification.
We will create a simple program to test AST matches. The program will identify a function definition with the name max
. We will use a slightly modified CMakeLists.txt
file from the previous examples to include the libraries required to support AST matches:
1 cmake_minimum_required(VERSION 3.16) 2 project("matchvisitor") 3 4 if ( NOT DEFINED ENV{LLVM_HOME}) 5 message(FATAL_ERROR "$LLVM_HOME is not defined") 6 else() 7 message(STATUS "$LLVM_HOME found: $ENV{LLVM_HOME}") 8 set(LLVM_HOME $ENV{LLVM_HOME} CACHE PATH "Root of LLVM installation") 9 set...