Note that there are some explanatory texts on larger screens.

plurals
  1. POHandle C++ functions with clang API to insert code
    primarykey
    data
    text
    <p>I need to preprocess some <strong>C++</strong> files to automatically insert code for testing and profiling, and I need to do it with the <strong>clang API</strong>. </p> <p>For now, I want to be able to do the following: If there is a function:</p> <pre><code>int SomeFn(int a, int b, int c) { doStuff(); } </code></pre> <p>I want to preprocess it, that it looks like this:</p> <pre><code>int SomeFn(int a, int b, int c) { cout &lt;&lt; "SomeFn is invoked with the following arguments: a=" &lt;&lt; a &lt;&lt; ", b=" &lt;&lt; b &lt;&lt; ", c=" &lt;&lt; c &lt;&lt; endl; doStuff(); } </code></pre> <p>I've tried to extend <code>ASTConsumer</code> and use the methods <code>HandleTopLevelDecl</code> and <code>HandleTopLevelSingleDecl</code> and check if the passed <code>Decls</code> are <code>FunctionDecls</code> and downcast them, so I can get their names and body-locations. But I can't handle class-methods that way, only global functions.</p> <p>I found a function in the <code>ASTConsumer</code> class <code>HandleTagDeclDefinition(TagDecl* D)</code>. The documentation says: </p> <blockquote> <p>This callback is invoked each time a TagDecl (e.g. struct, union, enum, class) is completed.</p> </blockquote> <p>But it looks like this method isn't even invoked, when I test it.</p> <p>So my question is, what would be the right way to do this task? And how are C++ classes are represented in the clang API? Maybe someone knows, where I can find examples because the clang API is poorly documented.</p> <hr> <p>UPDATE:</p> <p>I know there is also the concept of <code>Visitors</code> and there is a method <code>VisitRecordDecl(RecordDecl *D)</code>. The documentation says that <code>RecordDecl</code> represents classes. So I extended <code>RecursiveASTVisitor</code> and implemented <code>VisitRecordDecl(RecordDecl *D)</code> but it seems, that this method isn't invoked neither. Instead <code>VisitVarDecl</code> is invoked, when a class definition is find, as if it is seen as a variable declaration. So I'm a bit confused. I hope, someone can help...</p> <hr> <p>UPDATE2:</p> <p>I tried to parse another file and this time clang found one <code>RecordDecl</code>. But the file defined one struct and two classes, so I think my clang code parses <strong>C instead of C++</strong>. Are there any settings to <strong>switch between C and C++</strong> ?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload