unable to parse header files

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

unable to parse header files

Alexander Christ
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

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: unable to parse header files

Peter Collingbourne
Hi Alex,

On Sat, Dec 25, 2010 at 01:13:42PM +0100, Alexander Christ wrote:
> /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;
>         ~~^

You need to set HeaderSearchOptions::ResourceDir so that Clang can
find its own header files including stddef.h.  It's proably picking
up another stddef.h here which does not define the symbols correctly.
Something like this (CLANG_VERSION_STRING is defined by the header
but you need to define LLVM_PREFIX yourself, which is the LLVM/Clang
installation prefix):

#include "clang/Basic/Version.h"
...
headerSearchOptions.ResourceDir = LLVM_PREFIX "/lib/clang/" CLANG_VERSION_STRING;

> 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>

>From your code:
>     languageOptions.CPlusPlus= 1;

You need to set other language options for C++, including
LangOptions::Bool.  You can set all the correct language options
automatically using CompilerInvocation::setLangDefaults:

CompilerInvocation::setLangDefaults(languageOptions, IK_CXX);

Thanks,
--
Peter
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev