In a program using libtooling, is there a way to make some types recognized as "built-in type" ? For example, I'd like to make int16_t, uint32_t etc. recognized as canonical built-in types rather than it's typedef to short, unsigned etc.
-
Why do you want to do it? – SingerOfTheFall Apr 25 '14 at 07:12
-
To list global variables to monitore an embedded device. But I need to have fixed-length types. And to avoid to manage typedefs etc. I'd prefer that the compiler sees these types as built-in types, I don't care about the errors generated on the redefinitions of these types. – hl037_ Apr 25 '14 at 07:14
-
What did you mean by *recognized as "built-in type"* ? – Jarod42 Apr 25 '14 at 07:21
-
like in clang doc : "BuiltinType - This class is used for builtin types like 'int'. Builtin types are always canonical and have a literal name field." http://clang.llvm.org/doxygen/classclang_1_1BuiltinType.html – hl037_ Apr 25 '14 at 07:22
-
Oh sorry, I forgot to specify it is for a program using libtooling. – hl037_ Apr 25 '14 at 07:41
1 Answers
If you have a look at ".../llvm/tools/clang/include/clang/AST/BuiltinTypes.def"
, then that would declare the builtin types like int
and long long
. It's not entirely straight forward tho'. You will need to modify quite a bit of code, for example there are portions of type definitions in ".../llvm/tools/clang/lib/Sema/Sema.cpp"
and ".../llvm/tools/clang/lib/AST/Type.cpp"
. If you grep for Int128
(good choice as clang itself doesn't use that [much] in itself, as opposed to for example size_t
), you will see that it turns up in a lot of places. You'd have to cover all (or at least most) of those places with additional code to introduce new types of your own making.
I would say that it's probably much easier to do something like clang -include cstdint myprog.cpp
. In other words, make sure that the #include <cstdint>
[or your own version of the same kind of file] is done behind the scenes in the compiler - you could add this to your driver in your own code too.

- 126,704
- 14
- 140
- 227
-
It seems quite hard (I mean long to do) and modify the sources of the lib doesn't really enjoy me... ^^ Is there another way to add canonicals type (at runtime) before parsing ? – hl037_ Apr 25 '14 at 08:18
-
Like I said, you can make clang read some source file (like an include file) before the actual source. I have to go to work now, but I can look up how that works when I'm back in front of my home computer tonight. – Mats Petersson Apr 25 '14 at 08:23
-
That is what I did, using a custom stdint.h defining the types as structs like : typedef struct {} int8_t etc. But it's quite auwkward... – hl037_ Apr 28 '14 at 14:29