LibClang AST缺少宏扩展导致的节点?

时间:2019-02-14 13:57:34

标签: c clang abstract-syntax-tree libclang

我正在尝试使用LibClang获得完整的AST。使用以下代码作为工作示例:

foo.c

#define FOO 13u
#define BAR ((1u << FOO) - 1u)

int foo(unsigned int x) {
  return (x > BAR);
}

使用递归访问者,遍历AST并输出以下内容:

foo.out

macro definition <"foo.c", 8, 15> FOO '' 
macro definition <"foo.c", 24, 46> BAR '' 
macro expansion <"foo.c", 88, 91> BAR '' 
FunctionDecl <"foo.c", 48, 95> foo 'int (unsigned int)' 
  ParmDecl <"foo.c", 56, 70> x 'unsigned int' 
  CompoundStmt <"foo.c", 72, 95>  '' 
    ReturnStmt <"foo.c", 76, 92>  '' 
      ParenExpr <"foo.c", 83, 92>  'int' 
        BinaryOperator <"foo.c", 84, 91>  'int' '>' 
          UnexposedExpr <"foo.c", 84, 85> x 'unsigned int' 
            DeclRefExpr <"foo.c", 84, 85> x 'unsigned int'

在生成的AST遍历中缺少>二进制运算符的第二个操作数。如果我对foo.c进行了充分的预处理,则可以获得预期的AST。

bar.c




int foo(unsigned int x) {
  return (x > ((1u << 13u) - 1u));
}

bar.out

FunctionDecl <"bar.c", 3, 65> foo 'int (unsigned int)' 
  ParmDecl <"bar.c", 11, 25> x 'unsigned int' 
  CompoundStmt <"bar.c", 27, 65>  '' 
    ReturnStmt <"bar.c", 31, 62>  '' 
      ParenExpr <"bar.c", 38, 62>  'int' 
        BinaryOperator <"bar.c", 39, 61>  'int' '>' 
          UnexposedExpr <"bar.c", 39, 40> x 'unsigned int' 
            DeclRefExpr <"bar.c", 39, 40> x 'unsigned int' 
          ParenExpr <"bar.c", 43, 61>  'unsigned int' 
            BinaryOperator <"bar.c", 44, 60>  'unsigned int' '-' 
              ParenExpr <"bar.c", 44, 55>  'unsigned int' 
                BinaryOperator <"bar.c", 45, 54>  'unsigned int' '<<' 
                  IntegerLiteral <"bar.c", 45, 47>  'unsigned int' 1u
                  IntegerLiteral <"bar.c", 51, 54>  'unsigned int' 13u
              IntegerLiteral <"bar.c", 58, 60>  'unsigned int' 1u

但是,如果我修改foo.c使得二进制运算符对两个操作数都具有宏扩展,我将获得以下输出:

baz.c

#define FOO 13u
#define BAR ((1u << FOO) - 1u)

int foo(unsigned int x) {
  return (FOO > BAR);
}

baz.out

macro definition <"baz.c", 8, 15> FOO '' 
macro definition <"baz.c", 24, 46> BAR '' 
macro expansion <"baz.c", 84, 87> FOO '' 
macro expansion <"baz.c", 90, 93> BAR '' 
FunctionDecl <"baz.c", 48, 97> foo 'int (unsigned int)' 
  ParmDecl <"baz.c", 56, 70> x 'unsigned int' 
  CompoundStmt <"baz.c", 72, 97>  '' 
    ReturnStmt <"baz.c", 76, 94>  '' 
      ParenExpr <"baz.c", 83, 94>  'int'

现在操作数运算符都没有从结果AST遍历中丢失。

由于-fsyntax-only的隐式clang_parseTranslationUnit(...)参数,预处理必须在某个时候完成,但我看不到AST遍历中的宏扩展子树。

问题:宏扩展的AST子树在哪里?或者更确切地说,为什么clang_visitChildren(...)不访问这些子树?

0 个答案:

没有答案