Question

I want to do a transform on my code, it simplifies the binary operator e.g. "1+ a+ 2" to "a+3", so is there any api in libclang can do such things?

Was it helpful?

Solution

Not really.

If you are referring to Clang as the C/C++/ObjC frontend to LLVM, then there's no such API I know of - this is not the kind of thing a front-end is designed to do.

LLVM core (which is a part of Clang if you refer to it as a complete compiler) can do these kinds of things, but then there's no API to translate it back to the source language.

I think your best bet to do something like this is to write a Clang-based tool to modify the source (see LibFormat as an example). However, I recommend re-thinking why you need this in the first place - since this will happen anyway during compilation. You won't gain any performance, and you might make the code less readable.

And in general, if you want to add a certain transformation for performance reasons, and it currently does not happen during compilation, the correct way to do so is not to modify the source program but to write an LLVM pass which will run during the compilation and perform the fix. Clang is not involved in that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top