On Mon, May 13, 2013 at 8:33 AM, Pascal Ognibene <span dir="ltr"><<a href="mailto:pognibene@gmail.com" target="_blank">pognibene@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div><div><div><div><div><div>Hi all,<br><br></div>I want to parse C++ and C headers coming from a GCC/G++ installation, with clang 3.2 and a recursive visitor pattern. I have difficulties to parse some headers with messages like:<br>
<br>stdlib.h:863:20: error: unknown type name 'wchar_t'<br>extern int mbtowc (wchar_t *__restrict __pwc,<br><br></div>This is while parsing gcc 4.2.4 headers. I do have the wchar.h header in the include path.<br>
<br>
</div>I configure my CompilerInstance the following way:<br><br>...<br><br> CompilerInstance ci;<br> if ( useCpp )<br> {<br> ci.getLangOpts().CPlusPlus = 1;<br> ci.getLangOpts().Bool = 1;<br> ci.getLangOpts().CXXExceptions = 1;<br>
ci.getLangOpts().RTTI = 1;<br> //ci.getLangOpts().GNUMode = 1;<br></div></div></div></div></blockquote><div><br></div><div>You also need to set WChar. But this method of setting up a CompilerInstance is not recommended, because there's a whole big pile of things to remember (and the set of things changes frequently). Instead, consider using CompilerInvocation to set up your CompilerInstance.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div> }<br><br> ci.createDiagnostics ( 0, NULL );<br><br> TargetOptions to;<br>
to.Triple = llvm::sys::getDefaultTargetTriple();<br> TargetInfo *pti = TargetInfo::CreateTargetInfo ( ci.getDiagnostics(), to );<br>
ci.setTarget ( pti );<br><br> ci.createFileManager();<br> ci.createSourceManager ( ci.getFileManager() );<br> ci.createPreprocessor();<br><br>//try to get the gcc built ins<br>//seems to work!<br>//need now the C headers in addition of C++ headers<br>
//for gcc 4.2<br> clang::Preprocessor &PP = ci.getPreprocessor();<br> PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),<br> PP.getLangOpts());<br> HeaderSearchOptions headerSearchOptions;<br>
for ( std::vector<std::string>::iterator it = includeList.begin(); it != includeList.end(); ++it )<br> {<br> headerSearchOptions.AddPath ( *it,<br> clang::frontend::Angled,<br>
false,<br> false,<br> false );<br> }<br><br> clang::PreprocessorOptions &ppOptions = ci.getPreprocessorOpts();<br>
for ( std::vector<std::string>::iterator it = macroList.begin(); it != macroList.end(); ++it )<br> {<br> ppOptions.addMacroDef ( *it );<br> }<br><br> clang::InitializePreprocessor ( ci.getPreprocessor(),<br>
ci.getPreprocessorOpts(),<br> headerSearchOptions,<br> ci.getFrontendOpts() );<br> ci.createASTContext();<br><br>
MyASTConsumer *astConsumer = NULL;<br> MyASTConsumerCpp *astConsumerCpp = NULL;<br><br> if ( useCpp )<br> {<br> astConsumerCpp = new MyASTConsumerCpp ( &ci.getASTContext() );<br> ci.setASTConsumer ( astConsumerCpp );<br>
}<br> else<br> {<br> astConsumer = new MyASTConsumer ( &ci.getASTContext() );<br> ci.setASTConsumer ( astConsumer );<br> }<br><br> const FileEntry *pFile = ci.getFileManager().getFile ( inputFile );<br>
ci.getSourceManager().createMainFileID ( pFile );<br> ci.getDiagnosticClient().BeginSourceFile ( ci.getLangOpts(),<br> &ci.getPreprocessor() );<br><br> if ( useCpp )<br> {<br> clang::ParseAST ( ci.getPreprocessor(), astConsumerCpp, ci.getASTContext() );<br>
}<br> else<br> {<br> clang::ParseAST ( ci.getPreprocessor(), astConsumer, ci.getASTContext() );<br> }<br> ci.getDiagnosticClient().EndSourceFile();<br><br>...<br><br></div>I was hoping that this was enough to make clang 3.2 "gcc 4.2 compatible" but it looks like it's not the case. Any idea of the required options/initialization sequence for clang to understand glibc and gnu libC++ specificities and parse properly GNU headers?<br>
<br></div>Thank you for your help!<span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888">Pascal<br></font></span></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br>