[cfe-dev] libclang preprocessed code erroneous while clang++ -E preprocesses correctly

Karolin Varner via cfe-dev cfe-dev at lists.llvm.org
Tue Nov 17 03:55:39 PST 2015


Hi,

I have been writing a code generator/parser for one of my projects with libclang+libtooling for the past couple of days. (https://github.com/inexor-game/code/tree/karo/tree-gen/inexor/rpc/gluegen)

In it I am using annotations like `__attribute__((annotate("Key=Value"))`.

Now since that syntax is a bit unpleasant, I am using macros to generate it:

  ```
  #if defined(__GNUC__) && !defined(COMPILER_ICC)
      #define INEXOR_ANNOT(...) __attribute__((annotate(#__VA_ARGS__)))
  #else
      #define INEXOR_ANNOT(...)
  #endif

  #define INEXOR_SHARED_TREE(...) INEXOR_ANNOT(SharedTree=__VA_ARGS__)
  ```

Invocation:

  ```INEXOR_SHARED_TREE(/my/path/in/tree) int x = 42;```


When I run this trough my application, the preprocessor does not expand the INEXOR_ANNOT macro correctly:

  ```./gluegen ../../fpsgame/weapon.cpp -- -I../../../ -I/usr/include/SDL2 --std=c++14 -I/usr/lib/clang/3.7.0/include/ -I/usr/include/linux```

I've added some debug output to my application; I am manually parsing the annotation to extract the key value pair; the code below is the attribute's SourceRange:

  ```annotate(#__VA_ARGS__```

The macro invocation being expanded here looks like

  ```INEXOR_SHARED_TREE(settings/blood) ...```

When I run this through gcc -E, cpp or clang -E the code expands quite fine:

  ```__attribute__((annotate("SharedTree=settings/blood"))) ...```

I've also tried running my tool against the already preprocessed file and then the annotation is parsed fine, so I think it might be a problem with the preprocesson invocation.

Any thoughts would be much appreciated!

Thank You,
Karolin Varner



More information about the cfe-dev mailing list