flavorjones / flay-clang

Flay your C code!
MIT License
2 stars 0 forks source link

move away from ffi-clang #1

Open flavorjones opened 8 years ago

flavorjones commented 8 years ago

ffi-clang can't access any of the C++ API of libclang, and as a result we can't actually access all the information we need to build Sexps for the AST.

One example is the opcode for a BinaryOperator.

We need to come up with an alternative. Two ideas come to mind:

1. read the output from clang -Xclang -ast-dump -fsyntax-only foo.c, which looks like:

TranslationUnitDecl 0x15fe060 <<invalid sloc>>
|-TypedefDecl 0x15fe560 <<invalid sloc>> __int128_t '__int128'
|-TypedefDecl 0x15fe5c0 <<invalid sloc>> __uint128_t 'unsigned __int128'
|-TypedefDecl 0x15fe910 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'
`-FunctionDecl 0x15feab0 <test/fixtures/simple.c:1:1, line:6:1> simple_method 'int (int, int)'
  |-ParmVarDecl 0x15fe970 <line:1:19, col:23> foo 'int'
  |-ParmVarDecl 0x15fe9e0 <col:28, col:32> bar 'int'
  `-CompoundStmt 0x1645d40 <line:2:1, line:6:1>
    |-DeclStmt 0x15febe8 <line:3:3, col:16>
    | `-VarDecl 0x15feb70 <col:3, col:15> value 'int'
    |   `-IntegerLiteral 0x15febc8 <col:15> 'int' 0
    |-BinaryOperator 0x15fed18 <line:4:3, col:23> 'int' '='
    | |-DeclRefExpr 0x15fec00 <col:3> 'int' lvalue Var 0x15feb70 'value' 'int'
    | `-BinaryOperator 0x15fecf0 <col:11, col:23> 'int' '*'
    |   |-BinaryOperator 0x15feca8 <col:11, col:17> 'int' '*'
    |   | |-ImplicitCastExpr 0x15fec78 <col:11> 'int' <LValueToRValue>
    |   | | `-DeclRefExpr 0x15fec28 <col:11> 'int' lvalue ParmVar 0x15fe970 'foo' 'int'
    |   | `-ImplicitCastExpr 0x15fec90 <col:17> 'int' <LValueToRValue>
    |   |   `-DeclRefExpr 0x15fec50 <col:17> 'int' lvalue ParmVar 0x15fe9e0 'bar' 'int'
    |   `-IntegerLiteral 0x15fecd0 <col:23> 'int' 2
    `-ReturnStmt 0x1645d20 <line:5:3, col:10>
      `-ImplicitCastExpr 0x15fed68 <col:10> 'int' <LValueToRValue>
        `-DeclRefExpr 0x15fed40 <col:10> 'int' lvalue Var 0x15feb70 'value' 'int'

2. write a C++ extension that accesses libclang for only the bare minimum necessary to generate an AST.

Would love anyone's thoughts.

tenderlove commented 8 years ago

Blah. I thought there was a way to dump the AST as XML, but apparently that option was removed. Looks like there's a way to get graphviz representation, but that requires clang to be built in a special way. Seems like option 2 is the most portable way to go.

jtarchie commented 8 years ago
  1. Is this a well defined format? Looks like it could be easy to parse.
  2. Do you really want to write C++?