Hi everyone,<br><br>I would like to parse c++ files with clang. While the code below works for self written code, it generates error messages as soon as I include stdlib headers. Can you tell me what I'm doing wrong?<br>
<br><br>input.cpp:<br>#include "stdio.h"<br>#include <map><br>int main(){<br>  std::map<int, int> m;<br>  printf("%d",1);<br>  return 0;<br>}  <br>
<br><br><br>error messages:<br>...<br>/usr/include/c++/4.4/cstddef:50:11: error: no member named 'ptrdiff_t' in the global namespace<br>  using ::ptrdiff_t;<br>        ~~^<br>/usr/include/c++/4.4/cstddef:51:11: error: no member named 'size_t' in the global namespace<br>
  using ::size_t;<br>        ~~^<br>In file included from test.cpp:2:<br>In file included from /usr/include/c++/4.4/map:59:<br>In file included from /usr/include/c++/4.4/bits/stl_tree.h:62:<br>In file included from /usr/include/c++/4.4/bits/stl_algobase.h:63:<br>
/usr/include/c++/4.4/bits/cpp_type_traits.h:80:12: error: unknown type name 'bool'<br>  template<bool><br>           ^<br>/usr/include/c++/4.4/bits/cpp_type_traits.h:85:25: error: use of undeclared identifier 'true'<br>
    struct __truth_type<true><br>...<br>test.cpp:5:3: error: use of undeclared identifier 'printf'<br>  printf("%d",1);<br><br><br><br>main.cpp:<br>#include <iostream><br>#include "llvm/Support/raw_ostream.h"<br>
#include "llvm/System/Host.h"<br>#include "clang/Frontend/DiagnosticOptions.h"<br>#include "clang/Frontend/TextDiagnosticPrinter.h"<br>#include "clang/Basic/LangOptions.h"<br>#include "clang/Basic/FileSystemOptions.h"<br>
#include "clang/Basic/SourceManager.h"<br>#include "clang/Lex/HeaderSearch.h"<br>#include "clang/Basic/FileManager.h"<br>#include "clang/Frontend/HeaderSearchOptions.h"<br>#include "clang/Frontend/Utils.h"<br>
#include "clang/Basic/TargetOptions.h"<br>#include "clang/Basic/TargetInfo.h"<br>#include "clang/Lex/Preprocessor.h"<br>#include "clang/Frontend/PreprocessorOptions.h"<br>#include "clang/Frontend/FrontendOptions.h"<br>
#include "clang/Basic/IdentifierTable.h"<br>#include "clang/Basic/Builtins.h"<br>#include "clang/AST/ASTContext.h"<br>#include "clang/AST/ASTConsumer.h"<br>#include "clang/Sema/Sema.h"<br>
#include "clang/AST/DeclBase.h"<br>#include "clang/AST/Type.h"<br>#include "clang/AST/Decl.h"<br>#include "clang/Sema/Lookup.h"<br>#include "clang/Sema/Ownership.h"<br>#include "clang/AST/DeclGroup.h"<br>
#include "clang/Parse/Parser.h"<br>#include "clang/Parse/ParseAST.h"<br><br>int main()<br>{<br>    clang::DiagnosticOptions diagnosticOptions;<br>    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;<br>
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =<br>        new clang::TextDiagnosticPrinter(llvm::outs(), diagnosticOptions);<br>    clang::Diagnostic diagnostic(pDiagIDs, pTextDiagnosticPrinter);<br>    <br>    clang::LangOptions languageOptions;<br>
    languageOptions.CPlusPlus= 1;<br><br>    clang::FileSystemOptions fileSystemOptions;<br>    clang::FileManager fileManager(fileSystemOptions);<br>    clang::SourceManager sourceManager(diagnostic, fileManager);<br><br>
    clang::HeaderSearch headerSearch(fileManager);<br>    clang::HeaderSearchOptions headerSearchOptions;<br>    headerSearchOptions.AddPath("/usr/include/linux",<br>            clang::frontend::Angled,<br>            false,<br>
            false,<br>            false);<br>    headerSearchOptions.AddPath("/usr/include/c++/4.4",<br>            clang::frontend::Angled,<br>            false,<br>            false,<br>            false);<br>
<br>    headerSearchOptions.AddPath("/usr/include/c++/4.4/tr1",<br>            clang::frontend::Angled,<br>            false,<br>            false,<br>            false);<br>    <br>    clang::TargetOptions targetOptions;<br>
    targetOptions.Triple = llvm::sys::getHostTriple();<br><br>    clang::TargetInfo *pTargetInfo = <br>        clang::TargetInfo::CreateTargetInfo(<br>            diagnostic,<br>            targetOptions);<br><br>    clang::ApplyHeaderSearchOptions(<br>
        headerSearch,<br>        headerSearchOptions,<br>        languageOptions,<br>        pTargetInfo->getTriple());<br><br>    clang::Preprocessor preprocessor(<br>        diagnostic,<br>        languageOptions,<br>
        *pTargetInfo,<br>        sourceManager,<br>        headerSearch);<br><br>    clang::PreprocessorOptions preprocessorOptions;<br>    clang::FrontendOptions frontendOptions;<br>    clang::InitializePreprocessor(<br>
        preprocessor,<br>        preprocessorOptions,<br>        headerSearchOptions,<br>        frontendOptions);<br>    const clang::FileEntry *pFile = fileManager.getFile("test.cpp");<br>    sourceManager.createMainFileID(pFile);<br>
<br>    const clang::TargetInfo &targetInfo = *pTargetInfo;<br><br>    clang::IdentifierTable identifierTable(languageOptions);<br>    clang::SelectorTable selectorTable;<br><br>    clang::Builtin::Context builtinContext(targetInfo);<br>
    clang::ASTContext astContext(<br>        languageOptions,<br>        sourceManager,<br>        targetInfo,<br>        identifierTable,<br>        selectorTable,<br>        builtinContext,<br>        0);<br>    clang::ASTConsumer astConsumer;<br>
    pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);<br>    clang::ParseAST(preprocessor, &astConsumer, astContext); <br><br>    pTextDiagnosticPrinter->EndSourceFile();<br><br>    return 0;<br>
}<br><br><br>thanks, Alex<br>