Matching function argument that may not be valid

I am attempting to write a tool that will generate a table from tokens pulled from source code.

I have a static template function in a template class. I want to find the first argument for all calls to the template function.

The problem is that the token may or may not be defined, since they will be defined in a generated table.

If I use dump-ast it appears that the function call is omitted from the AST, so I don’t think that I can use AST matchers to find it.

Can anyone suggest how I might go about finding the desired function call?

I am attempting to write a tool that will generate a table from tokens pulled from source code.

I have a static template function in a template class. I want to find the first argument for all calls to the template function.

The problem is that the token may or may not be defined, since they will be defined in a generated table.

If I use dump-ast it appears that the function call is omitted from the AST, so I don’t think that I can use AST matchers to find it.

I somehow can’t believe that - all calls are in the AST.

I am attempting to write a tool that will generate a table from tokens
pulled from source code.

I have a static template function in a template class. I want to find
the first argument for all calls to the template function.

The problem is that the token may or may not be defined, since they will
be defined in a generated table.

If I use dump-ast it appears that the function call is omitted from the
AST, so I don't think that I can use AST matchers to find it.

I somehow can't believe that - all calls are in the AST.

Here is my example, using "clang version 3.7.0 (trunk 235602)". Note that
the call to Func is not present in the AST.

enum MyEnum
{
};
void Func(MyEnum v);
int main()
{
Func(SomeEnumValue);
return 0;
}

clang.exe -Xclang -ast-dump test.cpp
test.cpp:7:7: error: use of undeclared identifier 'SomeEnumValue'
        Func(SomeEnumValue);
             ^
TranslationUnitDecl 0x836140 <<invalid sloc>> <invalid sloc>

-CXXRecordDecl 0x836400 <<invalid sloc>> <invalid sloc> implicit class

type_inf
o

`-TypeVisibilityAttr 0x836470 <<invalid sloc>> Implicit Default
-TypedefDecl 0x8364b0 <<invalid sloc>> <invalid sloc> implicit size_t

'unsigned
int'

-TypedefDecl 0x836508 <<invalid sloc>> <invalid sloc> implicit

__builtin_va_lis
t 'char *'

-EnumDecl 0x836538 <test.cpp:1:1, line:3:1> line:1:6 referenced MyEnum

'int'

-FunctionDecl 0x836630 <line:4:1, col:19> col:6 Func 'void (enum MyEnum)'
`-ParmVarDecl 0x8365b8 <col:11, col:18> col:18 v 'enum MyEnum'

`-FunctionDecl 0x836718 <line:5:1, line:9:1> line:5:5 main 'int (void)'
  `-CompoundStmt 0x836880 <line:6:1, line:9:1>
    `-ReturnStmt 0x836870 <line:8:2, col:9>
      `-IntegerLiteral 0x836850 <col:9> 'int' 01 error generated.

Ok, this only works on valid code. If you have errors, all bets are off.

Yeah, I suspected as much. Is there any real option for finding this location? Would I have to do it where it is building the AST?

Yeah, I suspected as much. Is there any real option for finding this location? Would I have to do it where it is building the AST?

Nope. For parsing incorrect code you need to write your own “parser”. Why do you want to parse incorrect code though?

I want to insert the enum constants from the values that are in the code. One driving reason behind this is that there isn’t really any single group that owns the enum. One group could write it, but then nobody else would be permitted to modify it. A different group could write it, but then they would have to generate some matching tables by hand. Neither option is really acceptable.

The ideal is to write the enum value and pull it out of the code and generate the enum and associated tables.

I want to insert the enum constants from the values that are in the code. One driving reason behind this is that there isn’t really any single group that owns the enum. One group could write it, but then nobody else would be permitted to modify it. A different group could write it, but then they would have to generate some matching tables by hand. Neither option is really acceptable.

The ideal is to write the enum value and pull it out of the code and generate the enum and associated tables.

The way one usually does that is by not using an enum, but by using an integer type and “value ranges”.
I don’t think trying to parse incorrect code is the right approach. If you really want a code-gen approach, you’d need a list of enum values per user that you then just hand to the codgen.
If you want to dynamically generate values, I’d use a dynamic language :slight_smile: