Hello,<div><br></div><div>I am building a program in which I need to have the ASTs of multiple source files.</div><div>Except the main file that I give to clang and using the ASTConsumer, etc. for getting the AST context, I need to do the same for other files since my main file usually contains functions calls, where the functions are defined on other files. So, I need the AST of the other files in order to find the function bodies that are missing. I have the following code which seems ok since it compiles and runs without crashing. However, I am not sure if it does what I want to do.</div>
<div>The code is the following (which are inside a class):</div><div><br></div><div><div>        //visit AST</div><div>        static CXChildVisitResult TranslationUnitVisitor(CXCursor, CXCursor, CXClientData data)</div><div>
        {</div><div>  <span class="Apple-tab-span" style="white-space:pre">       </span>   return CXChildVisit_Recurse; // visit complete AST recursivly</div><div>        }</div><div><br></div><div>      </div><div>      void VisitTranslationUnitWithPCH()</div>
<div>      {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>  //create function pointer</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>  CXChildVisitResult (*pTranslationUnitVisitor)(CXCursor, CXCursor, CXClientData) = TranslationUnitVisitor;</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  </div><div><span class="Apple-tab-span" style="white-space:pre">   </span>   //excludeDeclsFromPCH = 1, displayDiagnostics=1</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>  CXIndex cx_idx = clang_createIndex(1, 1);</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  </div><div> <span class="Apple-tab-span" style="white-space:pre">  </span>  //IndexTest.pch was produced with the following command:</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>  //"clang -x c test.h -emit-ast -o test.pch"</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  system("/home/socrates/llvm/Debug+Asserts/bin/clang -x c test.h -emit-ast -o test.pch");</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>  CXTranslationUnit TU = clang_createTranslationUnit(cx_idx, "test.pch");</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">     </span>  //This will load all the symbols from 'test.pch'</div><div><span class="Apple-tab-span" style="white-space:pre">   </span>  clang_visitChildren(clang_getTranslationUnitCursor(TU), pTranslationUnitVisitor, 0);</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  clang_disposeTranslationUnit(TU);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>  //This will load all the symbols from 'test.c', excluding symbols</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  //from 'test.pch'.</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>  char *args[] = { "-include", "test.pch" };</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  TU = clang_createTranslationUnitFromSourceFile(cx_idx, "test.c", 2, args, 0, 0);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> </div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  clang_visitChildren(clang_getTranslationUnitCursor(TU), pTranslationUnitVisitor,0);</div><div><br></div><div>          //print AST</div><div>          CXString cx_str = clang_getTranslationUnitSpelling(TU);</div>
<div><span class="Apple-tab-span" style="white-space:pre">      </span>  std::cout << clang_getCString(cx_str);</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">      </span>  clang_disposeTranslationUnit(TU);</div>
<div>    }</div></div><div><br></div><div>At the end of the code I want to print the AST buy it does not return anything which makes me suspicious and also I get the following many many times:</div><div>test.pch:1:1: error: unknown type name 'CPCH'</div>
<div>test.pch:1:5: error: expected identifier or '('</div><div>test.pch:1:7: warning: null character ignored [-Wnull-character]</div><div>test.pch:1:8: warning: null character ignored [-Wnull-character]</div><div>
test.pch:1:11: warning: null character ignored [-Wnull-character]</div><div>test.pch:1:12: warning: null character ignored [-Wnull-character]</div><div>test.pch:31:2: warning: missing terminating '"' character</div>
<div>test.pch:34:2: warning: missing terminating '"' character</div><div>test.pch:42:8: warning: null character ignored [-Wnull-character]</div><div>test.pch:47:107: warning: null character ignored [-Wnull-character]</div>
<div>test.pch:47:110: warning: null character ignored [-Wnull-character]</div><div>test.pch:51:2: warning: null character ignored [-Wnull-character]</div><div>test.pch:60:5: warning: null character ignored [-Wnull-character]</div>
<div>etc....</div><div><br></div><div>Is there anything that I am missing ? Am I doing some thing wrong in order to get the AST ? Is there any other way to do what I am looking for ?</div><div>In addition, from the CXTranslationUnit can I get somehow the context ?</div>
<div><br></div><div>I would be really grateful for some help with this.</div><div><br></div><div>Socrates</div><div><br></div><div> </div><div><br></div>