<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Oct 23, 2009, at 10:19 AM, Simone Pellegrini wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
<div bgcolor="#ffffff" text="#000000">
<font face="Helvetica, Arial, sans-serif">Dear all,<br>
I need to perform code transformations based on inter-procedural
analysis done on multiple translation units. <br>
<br>
In order to do that I need to keep the AST generated for each
translation unit in memory, do my analysis and then restart rewriting
part of the AST I need to modify. Conceptually is not difficult my I am
experiencing a weird behavior of Clang and maybe you can help me. <br>
<br>
basically that's what I am doing:<br>
<br>
vector<ASTContext*> ctxs;<br>
for each input file{<br>
    Preprocessor PP(...);<br>
    InitializePreprocessor(PP);<br>
    ctxs.push_back( new ASTContext(...., false, 0) );<br>
    ParseAST(PP, *ctxs.back(),...);<br>
}<br></font></div></blockquote><div><br></div><div>You will need to make sure that the SourceManager is also retained, since the ASTContext stores a reference to its SourceManager. The same is true for TargetInfo, IdentifierTable, SelectorTable, etc.</div><br><blockquote type="cite"><div bgcolor="#ffffff" text="#000000"><font face="Helvetica, Arial, sans-serif">
// Now do ANALYSIS (e.g. building a clang::CallGraph )<br>
for each context in ctxs{<br>
    DeclContext *DC=ctxs[i];<br>
    for each decl{<br>
        if decl is a func{<br>
            FuncDecl* FD = ();<br>
            cout << FD->getNameAsString() << endl;
<--- ERROR<br>
        }<br>
    }<br>
}<br>
    <br>
As you can see from the code, every time I try to access the
information of the ASTContext it looks like everything is gone. why?<br></font></div></blockquote></div><div><br></div>Well, getNameAsString() is going to try to look at an IdentifierInfo that may have been deallocated when the IdentifierTable is destroyed. Your best bet would be to keep the whole Preprocessor alive for each ASTContext.<div><br><div><span class="Apple-tab-span" style="white-space:pre">       </span>- Doug</div></div></body></html>