Clang API and __builtin_va_list definition

I am using the Clang c++ API. I have a blocking issue because the builtin __builtin_va_list clang isn’t defined.

Here is the error:

…lib/clang/3.2/include/stdarg.h:30:9: error: unknown type name ‘__builtin_va_list’; did you mean ‘__builtin_va_list’? typedef __builtin_va_list va_list;

From what I’ve read, this builtin is target dependent.

This builtin is not defined in Builtins.def or BuiltinsX86.def or anywhere else I can find in the clang source code.

I am on a Redhat Linux box.

How can I get this builtin to be visible? Do I have to supply it myself?

I missing something in my Clang C++ API usage it seems to me.

All the other builtins seem to be installed and working.

I am using the Clang c++ API. I have a blocking issue because the builtin
__builtin_va_list clang isn't defined.

..lib/clang/3.2/include/stdarg.h:30:9: error: unknown type name
'__builtin_va_list'; did you mean '__builtin_va_list'? typedef
__builtin_va_list va_list;

How can I get this builtin to be visible? Do I have to supply it myself?

No.

I missing something in my Clang C++ API usage it seems to me.

This. It's not obvious exactly what you messed up, though.

-Eli

Thanks for your reply.

Do you know where the __builtin_va_list is generated by clang?

It is a target dependent declaration I believe. Currently I have my target options set as follows:

    clang::TargetOptions targetOptions;
    targetOptions.Triple = "x86_64-unknown-linux-gnu";
    targetOptions.CPU = "x86-64";

Which is consistent with the options I see when compiling directly with clang++:
clang++ -v test.cpp (this works)

Kam

Here is the code snippet I'm using:

#include <iostream>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Casting.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"
#include "clang/Frontend/CompilerInstance.h"

class MyASTConsumer : public clang::ASTConsumer
{
public:
    MyASTConsumer() : clang::ASTConsumer() { }
    virtual ~MyASTConsumer() { }

    virtual bool HandleTopLevelDecl( clang::DeclGroupRef d)
    {
        static int count = 0;
        clang::DeclGroupRef::iterator it;
        for( it = d.begin(); it != d.end(); it++)
        {
            count++;
            clang::VarDecl *vd = llvm::dyn_cast<clang::VarDecl>(*it);
            if(!vd)
            {
                continue;
            }
            if( vd->isFileVarDecl() && !vd->hasExternalStorage() )
            {
                std::cerr << "Read top-level variable decl: '";
                std::cerr << vd->getDeclName().getAsString() ;
                std::cerr << std::endl;
            }
        }
        return true;
    }
};

int main()
{
    clang::DiagnosticOptions diagnosticOptions;
    diagnosticOptions.ShowColors=1;
    diagnosticOptions.ErrorLimit =10;
    diagnosticOptions.ShowOptionNames=1;
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
        new clang::TextDiagnosticPrinter(
            llvm::outs(),
            diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    clang::DiagnosticsEngine *pDiagnosticsEngine =
        new clang::DiagnosticsEngine(pDiagIDs, pTextDiagnosticPrinter);

    clang::FileSystemOptions fileSystemOptions;
    clang::FileManager fileManager(fileSystemOptions);

    clang::SourceManager sourceManager(
        *pDiagnosticsEngine,
        fileManager);

    clang::HeaderSearchOptions headerSearchOptions;
    headerSearchOptions.ResourceDir = "/home/user/klall/tools/clang_install/bin/../lib/clang/3.2";
    headerSearchOptions.ModuleCachePath = "/var/tmp/clang-module-cache";
    headerSearchOptions.Verbose =true;

    clang::TargetOptions targetOptions;
    targetOptions.Triple = "x86_64-unknown-linux-gnu";
    targetOptions.CPU = "x86-64";

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

        clang::CompilerInstance compInst;
    compInst.getTargetOpts() = targetOptions;

    clang::HeaderSearchOptions &headerSearchOpts = compInst.getHeaderSearchOpts();
    headerSearchOpts = headerSearchOptions;

    clang::CompilerInvocation &compInvocation = compInst.getInvocation();
    clang::LangOptions languageOptions;
    languageOptions.CPlusPlus = 1;
    compInvocation.setLangDefaults(languageOptions, clang::IK_CXX, clang::LangStandard::lang_gnucxx98);

    clang::HeaderSearch headerSearch(fileManager,
                                     *pDiagnosticsEngine,
                                     languageOptions,
                                     pTargetInfo);

    clang::FrontendOptions & frontendOpts = compInvocation.getFrontendOpts();
    frontendOpts.DisableFree=1;
    frontendOpts.ShowHelp=1;
    frontendOpts.ProgramAction = clang::frontend::ParseSyntaxOnly;

    clang::CodeGenOptions & codeGenOpts = compInvocation.getCodeGenOpts ();

    codeGenOpts.RelaxAll = 1;
    codeGenOpts.RelocationModel = "static";
    codeGenOpts.DisableFPElim = 1;
    codeGenOpts.AsmVerbose = 1;
    codeGenOpts.CXXCtorDtorAliases= 1;
    codeGenOpts.UnwindTables = 1;
    codeGenOpts.OmitLeafFramePointer = 1;
    codeGenOpts.StackRealignment = 1;

    std::vector<std::string> res;
    compInvocation.toArgs (res);

    std::vector<std::string>::iterator it;

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

    preprocessor.getBuiltinInfo().InitializeBuiltins
        (preprocessor.getIdentifierTable(),
         languageOptions);

    clang::PreprocessorOptions preprocessorOptions;

    clang::InitializePreprocessor(
        preprocessor,
        preprocessorOptions,
        headerSearchOptions,
        frontendOpts);

    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;
    builtinContext.InitializeTarget(targetInfo);
    builtinContext.InitializeBuiltins(identifierTable, languageOptions);

    llvm::SmallVector<const char *, 32> builtinNames;
    builtinContext.GetBuiltinNames(builtinNames, languageOptions.NoBuiltin);

    clang::ASTContext astContext(
        languageOptions,
        sourceManager,
        pTargetInfo,
        identifierTable,
        selectorTable,
        builtinContext,
        0 /* size_reserve*/);

   MyASTConsumer astConsumer;

   clang::Sema sema(
        preprocessor,
        astContext,
        astConsumer);

   pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
   clang::ParseAST (preprocessor, &astConsumer, astContext);
   pTextDiagnosticPrinter->EndSourceFile();

   return 0;
}