<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>    }<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!<br><br></div>Pascal<br></div>