<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body text="#333333" bgcolor="#FFFFFF">
<p>Hi everybody,
<br>
<br>
As a beginner with clang I am struggling to support
source-to-source transformation in one step with clang.
<br>
<br>
How is the state of the art in source-to-source transformation
with clang?
<br>
<br>
I followed almost every resource on the Web and I can achieve to
do source rewrites (Rewriter) via a clang plugin, but the final
binary is not updated (the CodeGen is the main activity, and it is
compiled regardless of what I have modified in my plugin, even
using AddBeforeMainAction in the getActionType).
<br>
<br>
I have seen some documents regarding libTooling and how to create
an independent program that uses clang as library, but my purpose
it is to create a plugin (FrontendPluginRegistry::Add<>,
something "easy" to plug to a non-custom clang binary) and achieve
source-to-source modifications (transparently to the users,
avoiding overwriting their source files).
<br>
<br>
Being a bit pedantic, in case it is not clear: I need something
like a "plugin" to extend clang in an easy way. I need something
that is "integrated" in the compiling process. Why? because I need
to modify the source code during the compilation phase, inject new
code, modify the source code from the user in one step (I don't
want to create a tool to parse the user source code and then
compile the output files). Also, I would like to distribute my
code (plugin) to allow users to use it by themselves.
<br>
<br>
It is mandatory that it is during the compilation phase of clang
(clang $FLAGS $PLUGIN $ETC -o program source_files...).
<br>
<br>
<br>
*<b>Use cases I would like to try:</b>*
<br>
- 1. I would like to transform the source code in a really fast
and confident way, even before the lexer. I don't know if it is
possible. Like having a stream of text and transforming, previous
to the preprocessor/lexer. I know it can be dangerous, but at
least to know if I can and *<b>how to proceed</b>*.
<br>
- 2. I would like to transform specific parts of the source code
(can be with the Rewriter, as I have achieved). For that, I use
the ASTConsumer and Visitor pattern. Then, I would like to
rewrite, inject text, etc. *<b>So far I know how to do it, but not
how to preserve for the CodeGen (main action) and create the
final binary with it (reparsing again the source code and going
through every phase).</b>*
<br>
- 3. I would like to create AST Nodes. Although I have read it is
not the best option, I would like to be able to add new nodes,
statements, etc. *I didn't try yet this phase, but any guess?*
<br>
<br>
<br>
Regards, rNoz
<br>
<br>
Some threads, repositories or blogs that I have reviewed and
tested. In some of them there are answers that don't work:
<br>
<br>
<a
href="https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/rewritersample.cpp"
target="_top" rel="nofollow">https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/rewritersample.cpp</a>
<br>
<a href="https://github.com/fmtlib/fmt" target="_top"
rel="nofollow">https://github.com/fmtlib/fmt</a>
<br>
<a
href="https://github.com/amorri40/ClangFunctionInstrumentation/blob/master/MyRecursiveASTVisitor.cpp"
target="_top" rel="nofollow">https://github.com/amorri40/ClangFunctionInstrumentation/blob/master/MyRecursiveASTVisitor.cpp</a>
<br>
<a
href="https://github.com/RadeonOpenCompute/HCC_syscall/blob/e3fa6c24a6525c66006218ee33dbd0bc049cb511/lib/clang-plugins/StmtRewriter.cpp"
target="_top" rel="nofollow">https://github.com/RadeonOpenCompute/HCC_syscall/blob/e3fa6c24a6525c66006218ee33dbd0bc049cb511/lib/clang-plugins/StmtRewriter.cpp</a>
<br>
<a
href="https://github.com/CPFL/gloop/tree/d30bdd267c2453f71167efca79afbfb8771b8a28"
target="_top" rel="nofollow">https://github.com/CPFL/gloop/tree/d30bdd267c2453f71167efca79afbfb8771b8a28</a>
<br>
<a
href="https://jonasdevlieghere.com/understanding-the-clang-ast/"
target="_top" rel="nofollow">https://jonasdevlieghere.com/understanding-the-clang-ast/</a>
<br>
<a
href="https://github.com/sinelaw/elfs-clang-plugins/tree/master/large_assignment"
target="_top" rel="nofollow">https://github.com/sinelaw/elfs-clang-plugins/tree/master/large_assignment</a>
<br>
<a
href="https://github.com/loarabia/Clang-tutorial/blob/master/CIrewriter.cpp"
target="_top" rel="nofollow">https://github.com/loarabia/Clang-tutorial/blob/master/CIrewriter.cpp</a>
<br>
<a
href="https://github.com/loarabia/Clang-tutorial/wiki/TutorialOrig"
target="_top" rel="nofollow">https://github.com/loarabia/Clang-tutorial/wiki/TutorialOrig</a> <a
href="http://clang-developers.42468.n3.nabble.com/RFC-Add-custom-built-in-functions-using-a-plugin-tt4052587.html#none"
target="_top" rel="nofollow">http://clang-developers.42468.n3.nabble.com/RFC-Add-custom-built-in-functions-using-a-plugin-tt4052587.html#none</a>
<br>
<a
href="https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/matchers_rewriter.cpp"
target="_top" rel="nofollow">https://github.com/eliben/llvm-clang-samples/blob/master/src_clang/matchers_rewriter.cpp</a>
<br>
<a
href="http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html"
target="_top" rel="nofollow">http://clang-developers.42468.n3.nabble.com/Adding-nodes-to-Clang-s-AST-td4054800.html</a><br>
<a href="https://github.com/vgvassilev/clad/tree/master/tools"
target="_top" rel="nofollow">https://github.com/vgvassilev/clad/tree/master/tools</a>
<br>
*<b><a
href="http://clang-developers.42468.n3.nabble.com/How-to-make-clang-compile-the-data-from-the-RewriteBuffer-td4041049.html"
target="_top" rel="nofollow">http://clang-developers.42468.n3.nabble.com/How-to-make-clang-compile-the-data-from-the-RewriteBuffer-td4041049.html</a></b>*
<br>
<a
href="https://github.com/sampsyo/quala/blob/master/TypeAnnotations.h"
target="_top" rel="nofollow">https://github.com/sampsyo/quala/blob/master/TypeAnnotations.h</a>
<br>
*<b><a
href="http://lists.llvm.org/pipermail/cfe-dev/2016-November/051668.html"
target="_top" rel="nofollow">http://lists.llvm.org/pipermail/cfe-dev/2016-November/051668.html</a></b>*
<br>
<a
href="https://stackoverflow.com/questions/32941882/changing-the-source-code-with-a-clang-plugin"
target="_top" rel="nofollow">https://stackoverflow.com/questions/32941882/changing-the-source-code-with-a-clang-plugin</a><br>
<br>
I mark the "most important" in bold. The second one just hits the
knot, I am in the same "state" as him.
</p>
</body>
</html>