[cfe-dev] unable to parse header files

Alexander Christ christ.alexander at gmx.net
Sat Dec 25 04:13:42 PST 2010


Hi everyone,

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?


input.cpp:
#include "stdio.h"
#include <map>
int main(){
  std::map<int, int> m;
  printf("%d",1);
  return 0;
}



error messages:
...
/usr/include/c++/4.4/cstddef:50:11: error: no member named 'ptrdiff_t' in
the global namespace
  using ::ptrdiff_t;
        ~~^
/usr/include/c++/4.4/cstddef:51:11: error: no member named 'size_t' in the
global namespace
  using ::size_t;
        ~~^
In file included from test.cpp:2:
In file included from /usr/include/c++/4.4/map:59:
In file included from /usr/include/c++/4.4/bits/stl_tree.h:62:
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:63:
/usr/include/c++/4.4/bits/cpp_type_traits.h:80:12: error: unknown type name
'bool'
  template<bool>
           ^
/usr/include/c++/4.4/bits/cpp_type_traits.h:85:25: error: use of undeclared
identifier 'true'
    struct __truth_type<true>
...
test.cpp:5:3: error: use of undeclared identifier 'printf'
  printf("%d",1);



main.cpp:
#include <iostream>
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Host.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/HeaderSearchOptions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/PreprocessorOptions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Builtins.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Ownership.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"

int main()
{
    clang::DiagnosticOptions diagnosticOptions;
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
        new clang::TextDiagnosticPrinter(llvm::outs(), diagnosticOptions);
    clang::Diagnostic diagnostic(pDiagIDs, pTextDiagnosticPrinter);

    clang::LangOptions languageOptions;
    languageOptions.CPlusPlus= 1;

    clang::FileSystemOptions fileSystemOptions;
    clang::FileManager fileManager(fileSystemOptions);
    clang::SourceManager sourceManager(diagnostic, fileManager);

    clang::HeaderSearch headerSearch(fileManager);
    clang::HeaderSearchOptions headerSearchOptions;
    headerSearchOptions.AddPath("/usr/include/linux",
            clang::frontend::Angled,
            false,
            false,
            false);
    headerSearchOptions.AddPath("/usr/include/c++/4.4",
            clang::frontend::Angled,
            false,
            false,
            false);

    headerSearchOptions.AddPath("/usr/include/c++/4.4/tr1",
            clang::frontend::Angled,
            false,
            false,
            false);

    clang::TargetOptions targetOptions;
    targetOptions.Triple = llvm::sys::getHostTriple();

    clang::TargetInfo *pTargetInfo =
        clang::TargetInfo::CreateTargetInfo(
            diagnostic,
            targetOptions);

    clang::ApplyHeaderSearchOptions(
        headerSearch,
        headerSearchOptions,
        languageOptions,
        pTargetInfo->getTriple());

    clang::Preprocessor preprocessor(
        diagnostic,
        languageOptions,
        *pTargetInfo,
        sourceManager,
        headerSearch);

    clang::PreprocessorOptions preprocessorOptions;
    clang::FrontendOptions frontendOptions;
    clang::InitializePreprocessor(
        preprocessor,
        preprocessorOptions,
        headerSearchOptions,
        frontendOptions);
    const clang::FileEntry *pFile = fileManager.getFile("test.cpp");
    sourceManager.createMainFileID(pFile);

    const clang::TargetInfo &targetInfo = *pTargetInfo;

    clang::IdentifierTable identifierTable(languageOptions);
    clang::SelectorTable selectorTable;

    clang::Builtin::Context builtinContext(targetInfo);
    clang::ASTContext astContext(
        languageOptions,
        sourceManager,
        targetInfo,
        identifierTable,
        selectorTable,
        builtinContext,
        0);
    clang::ASTConsumer astConsumer;
    pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
    clang::ParseAST(preprocessor, &astConsumer, astContext);

    pTextDiagnosticPrinter->EndSourceFile();

    return 0;
}


thanks, Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20101225/ca200276/attachment.html>


More information about the cfe-dev mailing list