problem embedding clang 2.7 (__builtin_fabsf not found) - confused!

Hi,

I'm trying to embed Clang 2.7 into a tool driven by Lua. I had this working fine with 2.6, but am having problems with 2.7: some builtins appear to not be found. Including <math.h> leads to errors such as:

error (/usr/include/architecture/i386/math.h): use of undeclared identifier '__builtin_fabsf'

I printed out the list of builtins used by context, and __builtin_fabsf is in that list:

    llvm::SmallVector<const char *, 32> BuiltinNames;
    Ctx.BuiltinInfo.GetBuiltinNames(BuiltinNames, Ctx.getLangOptions().NoBuiltin);
    for (int i = 0; i<BuiltinNames.size(); i++) {
      printf("Builtin %s\n", BuiltinNames[i]); //--> list includes '__builtin_fabsf'
    }

Also, if I disable C++ in the LangOptions, incuding "stdarg.h" leads to errors about 'redefinition of va_list'.

I'm including the lib/clang/1.1/include path, so these headers are being found.

It's hard to know what else to try - I've been banging on this all day and can't figure it out - any help would be *much* appreciated!

here's the main code driving the compilation - if anything here looks unusual let me know!:

    std::string unitsrc = ... // source code to compile is entered at runtime.
    
    // Souce to compile
    llvm::MemoryBuffer *buffer = llvm::MemoryBuffer::getMemBufferCopy(unitsrc.c_str(), unitsrc.c_str() + unitsrc.size(), unitname.c_str());
    if(!buffer) {
      luaL_error(L, "couldn't load %s", unitsrc.c_str());
    }
    
    clang::Diagnostic diags(&client);
    
    // modify this for specific language, features etc.
    clang::LangOptions langopts;
    langopts.C99 = 1;
    langopts.CPlusPlus = 1;
    langopts.HexFloats = 1;
    langopts.BCPLComment = 1; // Only for C99/C++.
    langopts.Digraphs = 1; // C94, C99, C++.
    langopts.GNUMode = 1;
    langopts.ImplicitInt = 0;
    langopts.DollarIdents = 1;
    langopts.WritableStrings = 0;
    langopts.Exceptions = 0;
    langopts.RTTI = 0;
    langopts.Bool = 0;
    langopts.MathErrno = 0;
    langopts.InstantiationDepth = 99;
    langopts.OptimizeSize = 0;
    langopts.PICLevel = 1;
    langopts.GNUInline = 0;
    langopts.NoInline = 1;
    langopts.Static = 0;
    
    // modify this for specific host, CPU, ABI, features etc.
    clang::TargetOptions targetopts;
    targetopts.Triple = llvm::sys::getHostTriple();
    targetopts.CPU = llvm::sys::getHostCPUName();
    //targetopts.ABI = ?
    printf("cpu %s triple %s\n", targetopts.CPU.data(), targetopts.Triple.data());
    
    clang::TargetInfo * target = clang::TargetInfo::CreateTargetInfo(diags, targetopts);
    if (!target)
      luaL_error(L, "could not create target");
    
    SM.createMainFileIDForMemBuffer(buffer);
    
    clang::FileManager FM;
    clang::HeaderSearch headers(FM);
    
    clang::HeaderSearchOptions headeropts;
    for (std::list<std::string>::iterator it = iflags.begin(); it != iflags.end(); it++) {
      llvm::StringRef Path(*it);
      clang::frontend::IncludeDirGroup Group = clang::frontend::Angled;
      bool IsUserSupplied = true;
      bool IsFramework = false;
      headeropts.AddPath(Path, Group, IsUserSupplied, IsFramework);
      printf("adding path %s\n", Path.data());
    }
    clang::ApplyHeaderSearchOptions(headers, headeropts, langopts, llvm::Triple(targetopts.Triple));
    
    clang::IdentifierInfoLookup * IILookup = 0;
    bool OwnsHeaderSearch = false;
  
    clang::Preprocessor pp(diags, langopts, *target, SM, headers, IILookup, OwnsHeaderSearch);
    
    /// InitializePreprocessor - Initialize the preprocessor getting it and the
    /// environment ready to process a single file.
    clang::PreprocessorOptions ppopts;
    //ppopts.addMacroDef(llvm::StringRef Name)
    clang::FrontendOptions frontendopts;
    //DebugCodeCompletionPrinter = 1;
    //DisableFree = 0;
    //EmptyInputOnly = 0;
    //ProgramAction = frontend::ParseSyntaxOnly;
    //ActionName = "";
    //RelocatablePCH = 0;
    //ShowHelp = 0;
    //ShowMacrosInCodeCompletion = 0;
    //ShowStats = 0;
    //ShowTimers = 0;
    //ShowVersion = 0;
    clang::InitializePreprocessor(pp, ppopts, headeropts, frontendopts);

    printf("predefines %s\n", pp.getPredefines().data());
    
    clang::IdentifierInfoLookup* externalLookup = 0;
    clang::IdentifierTable idents(langopts, externalLookup);
    clang::SelectorTable sels;
    clang::Builtin::Context builtins(*target);
    bool NoBuiltins = false;
    builtins.InitializeBuiltins(idents, NoBuiltins);
    
    bool FreeMemory = true;
    unsigned size_reserve=0;
    
    clang::ASTContext Ctx(pp.getLangOptions(), SM, pp.getTargetInfo(), pp.getIdentifierTable(), pp.getSelectorTable(), pp.getBuiltinInfo(), FreeMemory, size_reserve);
    
    clang::CodeGenOptions codegenopts;
    //codegenopts.AsmVerbose = 0;
    //codegenopts.DebugInfo = 0;
    //codegenopts.DisableFPElim = 0;
    //codegenopts.DisableLLVMOpts = 0;
    //codegenopts.DisableRedZone = 0;
    //codegenopts.MergeAllConstants = 1;
    //codegenopts.NoCommon = 0;
    //codegenopts.NoImplicitFloat = 0;
    //codegenopts.NoZeroInitializedInBSS = 0;
    //codegenopts.ObjCLegacyDispatch = 0;
    //codegenopts.OptimizationLevel = 0;
    //codegenopts.OptimizeSize = 0;
    //codegenopts.SoftFloat = 0;
    //codegenopts.TimePasses = 0;
    //codegenopts.UnitAtATime = 1;
    //codegenopts.UnrollLoops = 0;
    //codegenopts.UnwindTables = 0;
    //codegenopts.VerifyModule = 1;
    //codegenopts.CXXCtorDtorAliases = 0;
    //codegenopts.Inlining = NoInlining;
    //codegenopts.RelocationModel = "pic";
    clang::CodeGenerator * codegen = clang::CreateLLVMCodeGen(diags, unitname, codegenopts, llvm::getGlobalContext());
       
    bool PrintStats = true;
    /// \param CompleteTranslationUnit When true, the parsed file is
    /// considered to be a complete translation unit, and any
    /// end-of-translation-unit wrapup will be performed.
    bool CompleteTranslationUnit = true;
    /// \param CompletionConsumer If given, an object to consume code completion
    /// results.
    clang::CodeCompleteConsumer * CompletionConsumer = 0;
    clang::ParseAST(pp, codegen, Ctx, PrintStats, CompleteTranslationUnit, CompletionConsumer);
    
    llvm::SmallVector<const char *, 32> BuiltinNames;
    Ctx.BuiltinInfo.GetBuiltinNames(BuiltinNames, Ctx.getLangOptions().NoBuiltin);
    for (int i = 0; i<BuiltinNames.size(); i++) {
      printf("Builtin %s\n", BuiltinNames[i]);
    }
    
    llvm::Module * cmodule = codegen->ReleaseModule(); // or GetModule() if we want to reuse it?
    
    ... and on to error handling etc.

This is using LLVM/Clang 2.7 release, built on Mac OS 10.6, with the following ./configure/make invocation (I need to support OS 10.5+ and 32/64 bit universal):

env MACOSX_DEPLOYMENT_TARGET=10.5 ./configure --enable-targets=x86,x86_64 --enable-jit --enable-optimized --enable-pic
env MACOSX_DEPLOYMENT_TARGET=10.5 sudo make DARWIN_VERSION=10.5 UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" UNIVERSAL_SDK_PATH=/Developer/SDKs/MacOSX10.5.sdk/

Output follows:

cpu penryn triple i386-apple-darwin10
predefines # 1 "<built-in>" 3
#define __llvm__ 1
#define __clang__ 1
#define __GNUC_MINOR__ 2
#define __GNUC_PATCHLEVEL__ 1
#define __GNUC__ 4
#define __GXX_ABI_VERSION 1002
#define __VERSION__ "4.2.1 Compatible Clang Compiler"
#define __STDC__ 1
#define __STDC_HOSTED__ 1
#define __CONSTANT_CFSTRINGS__ 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __private_extern__ extern
#define _GNU_SOURCE 1
#define __CHAR_BIT__ 8
#define __SCHAR_MAX__ 127
#define __SHRT_MAX__ 32767
#define __INT_MAX__ 2147483647
#define __LONG_MAX__ 2147483647L
#define __LONG_LONG_MAX__ 9223372036854775807LL
#define __WCHAR_MAX__ 2147483647
#define __INTMAX_MAX__ 9223372036854775807LL
#define __INTMAX_TYPE__ long long int
#define __UINTMAX_TYPE__ long long unsigned int
#define __INTMAX_WIDTH__ 64
#define __PTRDIFF_TYPE__ int
#define __PTRDIFF_WIDTH__ 32
#define __INTPTR_TYPE__ long int
#define __INTPTR_WIDTH__ 32
#define __SIZE_TYPE__ long unsigned int
#define __SIZE_WIDTH__ 32
#define __WCHAR_TYPE__ int
#define __WCHAR_WIDTH__ 32
#define __WINT_TYPE__ int
#define __WINT_WIDTH__ 32
#define __SIG_ATOMIC_WIDTH__ 32
#define __FLT_DENORM_MIN__ 1.40129846e-45F
#define __FLT_HAS_DENORM__ 1
#define __FLT_DIG__ 6
#define __FLT_EPSILON__ 1.19209290e-7F
#define __FLT_HAS_INFINITY__ 1
#define __FLT_HAS_QUIET_NAN__ 1
#define __FLT_MANT_DIG__ 24
#define __FLT_MAX_10_EXP__ 38
#define __FLT_MAX_EXP__ 128
#define __FLT_MAX__ 3.40282347e+38F
#define __FLT_MIN_10_EXP__ (-37)
#define __FLT_MIN_EXP__ (-125)
#define __FLT_MIN__ 1.17549435e-38F
#define __DBL_DENORM_MIN__ 4.9406564584124654e-324
#define __DBL_HAS_DENORM__ 1
#define __DBL_DIG__ 15
#define __DBL_EPSILON__ 2.2204460492503131e-16
#define __DBL_HAS_INFINITY__ 1
#define __DBL_HAS_QUIET_NAN__ 1
#define __DBL_MANT_DIG__ 53
#define __DBL_MAX_10_EXP__ 308
#define __DBL_MAX_EXP__ 1024
#define __DBL_MAX__ 1.7976931348623157e+308
#define __DBL_MIN_10_EXP__ (-307)
#define __DBL_MIN_EXP__ (-1021)
#define __DBL_MIN__ 2.2250738585072014e-308
#define __LDBL_DENORM_MIN__ 3.64519953188247460253e-4951L
#define __LDBL_HAS_DENORM__ 1
#define __LDBL_DIG__ 18
#define __LDBL_EPSILON__ 1.08420217248550443401e-19L
#define __LDBL_HAS_INFINITY__ 1
#define __LDBL_HAS_QUIET_NAN__ 1
#define __LDBL_MANT_DIG__ 64
#define __LDBL_MAX_10_EXP__ 4932
#define __LDBL_MAX_EXP__ 16384
#define __LDBL_MAX__ 1.18973149535723176502e+4932L
#define __LDBL_MIN_10_EXP__ (-4931)
#define __LDBL_MIN_EXP__ (-16381)
#define __LDBL_MIN__ 3.36210314311209350626e-4932L
#define __POINTER_WIDTH__ 32
#define __INT8_TYPE__ char
#define __INT16_TYPE__ short
#define __INT32_TYPE__ int
#define __INT64_TYPE__ long long int
#define __INT64_C_SUFFIX__ LL
typedef char* __builtin_va_list;
#define __USER_LABEL_PREFIX__ _
#define __FINITE_MATH_ONLY__ 0
#define __GNUC_STDC_INLINE__ 1
#define __NO_INLINE__ 1
#define __PIC__ 1
#define __pic__ 1
#define __FLT_EVAL_METHOD__ 0
#define __FLT_RADIX__ 2
#define __DECIMAL_DIG__ 21
#define i386 1
#define __i386 1
#define __i386__ 1
#define __LITTLE_ENDIAN__ 1
#define __nocona 1
#define __nocona__ 1
#define __tune_nocona__ 1
#define __REGISTER_PREFIX__
#define __NO_MATH_INLINES 1
#define __SSE4_1__ 1
#define __SSSE3__ 1
#define __SSE3__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE__ 1
#define __SSE_MATH__ 1
#define __MMX__ 1
#define __APPLE_CC__ 5621
#define __APPLE__ 1
#define __MACH__ 1
#define OBJC_NEW_PROPERTIES 1
#define __weak __attribute__((objc_gc(weak)))
#define __strong
#define __DYNAMIC__ 1
#define __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ 1060
# 1 "<command line>" 1
# 1 "<built-in>" 2

Builtin __builtin_atan2
Builtin __builtin_atan2f
Builtin __builtin_atan2l
Builtin __builtin_abs
Builtin __builtin_copysign
Builtin __builtin_copysignf
Builtin __builtin_copysignl
Builtin __builtin_fabs
Builtin __builtin_fabsf
Builtin __builtin_fabsl
Builtin __builtin_fmod
Builtin __builtin_fmodf
Builtin __builtin_fmodl
Builtin __builtin_frexp
Builtin __builtin_frexpf
Builtin __builtin_frexpl
Builtin __builtin_huge_val
Builtin __builtin_huge_valf
Builtin __builtin_huge_vall
Builtin __builtin_inf
Builtin __builtin_inff
Builtin __builtin_infl
Builtin __builtin_ldexp
Builtin __builtin_ldexpf
Builtin __builtin_ldexpl
Builtin __builtin_modf
Builtin __builtin_modff
Builtin __builtin_modfl
Builtin __builtin_nan
Builtin __builtin_nanf
Builtin __builtin_nanl
Builtin __builtin_nans
Builtin __builtin_nansf
Builtin __builtin_nansl
Builtin __builtin_powi
Builtin __builtin_powif
Builtin __builtin_powil
Builtin __builtin_pow
Builtin __builtin_powf
Builtin __builtin_powl
Builtin __builtin_acos
Builtin __builtin_acosf
Builtin __builtin_acosl
Builtin __builtin_asin
Builtin __builtin_asinf
Builtin __builtin_asinl
Builtin __builtin_atan
Builtin __builtin_atanf
Builtin __builtin_atanl
Builtin __builtin_ceil
Builtin __builtin_ceilf
Builtin __builtin_ceill
Builtin __builtin_cos
Builtin __builtin_cosf
Builtin __builtin_cosh
Builtin __builtin_coshf
Builtin __builtin_coshl
Builtin __builtin_cosl
Builtin __builtin_exp
Builtin __builtin_expf
Builtin __builtin_expl
Builtin __builtin_floor
Builtin __builtin_floorf
Builtin __builtin_floorl
Builtin __builtin_hypot
Builtin __builtin_hypotf
Builtin __builtin_hypotl
Builtin __builtin_log
Builtin __builtin_log10
Builtin __builtin_log10f
Builtin __builtin_log10l
Builtin __builtin_logf
Builtin __builtin_logl
Builtin __builtin_sin
Builtin __builtin_sinf
Builtin __builtin_sinh
Builtin __builtin_sinhf
Builtin __builtin_sinhl
Builtin __builtin_sinl
Builtin __builtin_sqrt
Builtin __builtin_sqrtf
Builtin __builtin_sqrtl
Builtin __builtin_tan
Builtin __builtin_tanf
Builtin __builtin_tanh
Builtin __builtin_tanhf
Builtin __builtin_tanhl
Builtin __builtin_tanl
Builtin __builtin_cabs
Builtin __builtin_cabsf
Builtin __builtin_cabsl
Builtin __builtin_cacos
Builtin __builtin_cacosf
Builtin __builtin_cacosl
Builtin __builtin_carg
Builtin __builtin_cargf
Builtin __builtin_cargl
Builtin __builtin_casin
Builtin __builtin_casinf
Builtin __builtin_casinl
Builtin __builtin_catan
Builtin __builtin_catanf
Builtin __builtin_catanl
Builtin __builtin_ccos
Builtin __builtin_ccosf
Builtin __builtin_ccosl
Builtin __builtin_ccosh
Builtin __builtin_ccoshf
Builtin __builtin_ccoshl
Builtin __builtin_cexp
Builtin __builtin_cexpf
Builtin __builtin_cexpl
Builtin __builtin_cimag
Builtin __builtin_cimagf
Builtin __builtin_cimagl
Builtin __builtin_conj
Builtin __builtin_conjf
Builtin __builtin_conjl
Builtin __builtin_clog
Builtin __builtin_clogf
Builtin __builtin_clogl
Builtin __builtin_cproj
Builtin __builtin_cprojf
Builtin __builtin_cprojl
Builtin __builtin_cpow
Builtin __builtin_cpowf
Builtin __builtin_cpowl
Builtin __builtin_creal
Builtin __builtin_crealf
Builtin __builtin_creall
Builtin __builtin_csin
Builtin __builtin_csinf
Builtin __builtin_csinl
Builtin __builtin_csinh
Builtin __builtin_csinhf
Builtin __builtin_csinhl
Builtin __builtin_csqrt
Builtin __builtin_csqrtf
Builtin __builtin_csqrtl
Builtin __builtin_ctan
Builtin __builtin_ctanf
Builtin __builtin_ctanl
Builtin __builtin_ctanh
Builtin __builtin_ctanhf
Builtin __builtin_ctanhl
Builtin __builtin_isgreater
Builtin __builtin_isgreaterequal
Builtin __builtin_isless
Builtin __builtin_islessequal
Builtin __builtin_islessgreater
Builtin __builtin_isunordered
Builtin __builtin_fpclassify
Builtin __builtin_isfinite
Builtin __builtin_isinf
Builtin __builtin_isinf_sign
Builtin __builtin_isnan
Builtin __builtin_isnormal
Builtin __builtin_signbit
Builtin __builtin_signbitf
Builtin __builtin_signbitl
Builtin __builtin_clz
Builtin __builtin_clzl
Builtin __builtin_clzll
Builtin __builtin_ctz
Builtin __builtin_ctzl
Builtin __builtin_ctzll
Builtin __builtin_ffs
Builtin __builtin_ffsl
Builtin __builtin_ffsll
Builtin __builtin_parity
Builtin __builtin_parityl
Builtin __builtin_parityll
Builtin __builtin_popcount
Builtin __builtin_popcountl
Builtin __builtin_popcountll
Builtin __builtin_bswap32
Builtin __builtin_bswap64
Builtin __builtin_constant_p
Builtin __builtin_classify_type
Builtin __builtin___CFStringMakeConstantString
Builtin __builtin___NSStringMakeConstantString
Builtin __builtin_va_start
Builtin __builtin_va_end
Builtin __builtin_va_copy
Builtin __builtin_stdarg_start
Builtin __builtin_bcmp
Builtin __builtin_bcopy
Builtin __builtin_bzero
Builtin __builtin_memchr
Builtin __builtin_memcmp
Builtin __builtin_memcpy
Builtin __builtin_memmove
Builtin __builtin_mempcpy
Builtin __builtin_memset
Builtin __builtin_stpcpy
Builtin __builtin_stpncpy
Builtin __builtin_strcasecmp
Builtin __builtin_strcat
Builtin __builtin_strchr
Builtin __builtin_strcmp
Builtin __builtin_strcpy
Builtin __builtin_strcspn
Builtin __builtin_strdup
Builtin __builtin_strlen
Builtin __builtin_strncasecmp
Builtin __builtin_strncat
Builtin __builtin_strncmp
Builtin __builtin_strncpy
Builtin __builtin_strndup
Builtin __builtin_strpbrk
Builtin __builtin_strrchr
Builtin __builtin_strspn
Builtin __builtin_strstr
Builtin __builtin_return_address
Builtin __builtin_extract_return_addr
Builtin __builtin_frame_address
Builtin __builtin_flt_rounds
Builtin __builtin_setjmp
Builtin __builtin_longjmp
Builtin __builtin_unwind_init
Builtin __builtin_eh_return_data_regno
Builtin __builtin_vsnprintf
Builtin __builtin_eh_return
Builtin __builtin_frob_return_addr
Builtin __builtin_dwarf_cfa
Builtin __builtin_init_dwarf_reg_size_table
Builtin __builtin_dwarf_sp_column
Builtin __builtin_extend_pointer
Builtin __builtin_object_size
Builtin __builtin___memcpy_chk
Builtin __builtin___memmove_chk
Builtin __builtin___mempcpy_chk
Builtin __builtin___memset_chk
Builtin __builtin___stpcpy_chk
Builtin __builtin___strcat_chk
Builtin __builtin___strcpy_chk
Builtin __builtin___strncat_chk
Builtin __builtin___strncpy_chk
Builtin __builtin___snprintf_chk
Builtin __builtin___sprintf_chk
Builtin __builtin___vsnprintf_chk
Builtin __builtin___vsprintf_chk
Builtin __builtin___fprintf_chk
Builtin __builtin___printf_chk
Builtin __builtin___vfprintf_chk
Builtin __builtin___vprintf_chk
Builtin __builtin_expect
Builtin __builtin_prefetch
Builtin __builtin_abort
Builtin __builtin_trap
Builtin __builtin_unreachable
Builtin __builtin_shufflevector
Builtin __builtin_alloca
Builtin __sync_fetch_and_add
Builtin __sync_fetch_and_add_1
Builtin __sync_fetch_and_add_2
Builtin __sync_fetch_and_add_4
Builtin __sync_fetch_and_add_8
Builtin __sync_fetch_and_add_16
Builtin __sync_fetch_and_sub
Builtin __sync_fetch_and_sub_1
Builtin __sync_fetch_and_sub_2
Builtin __sync_fetch_and_sub_4
Builtin __sync_fetch_and_sub_8
Builtin __sync_fetch_and_sub_16
Builtin __sync_fetch_and_or
Builtin __sync_fetch_and_or_1
Builtin __sync_fetch_and_or_2
Builtin __sync_fetch_and_or_4
Builtin __sync_fetch_and_or_8
Builtin __sync_fetch_and_or_16
Builtin __sync_fetch_and_and
Builtin __sync_fetch_and_and_1
Builtin __sync_fetch_and_and_2
Builtin __sync_fetch_and_and_4
Builtin __sync_fetch_and_and_8
Builtin __sync_fetch_and_and_16
Builtin __sync_fetch_and_xor
Builtin __sync_fetch_and_xor_1
Builtin __sync_fetch_and_xor_2
Builtin __sync_fetch_and_xor_4
Builtin __sync_fetch_and_xor_8
Builtin __sync_fetch_and_xor_16
Builtin __sync_fetch_and_nand
Builtin __sync_fetch_and_nand_1
Builtin __sync_fetch_and_nand_2
Builtin __sync_fetch_and_nand_4
Builtin __sync_fetch_and_nand_8
Builtin __sync_fetch_and_nand_16
Builtin __sync_add_and_fetch
Builtin __sync_add_and_fetch_1
Builtin __sync_add_and_fetch_2
Builtin __sync_add_and_fetch_4
Builtin __sync_add_and_fetch_8
Builtin __sync_add_and_fetch_16
Builtin __sync_sub_and_fetch
Builtin __sync_sub_and_fetch_1
Builtin __sync_sub_and_fetch_2
Builtin __sync_sub_and_fetch_4
Builtin __sync_sub_and_fetch_8
Builtin __sync_sub_and_fetch_16
Builtin __sync_or_and_fetch
Builtin __sync_or_and_fetch_1
Builtin __sync_or_and_fetch_2
Builtin __sync_or_and_fetch_4
Builtin __sync_or_and_fetch_8
Builtin __sync_or_and_fetch_16
Builtin __sync_and_and_fetch
Builtin __sync_and_and_fetch_1
Builtin __sync_and_and_fetch_2
Builtin __sync_and_and_fetch_4
Builtin __sync_and_and_fetch_8
Builtin __sync_and_and_fetch_16
Builtin __sync_xor_and_fetch
Builtin __sync_xor_and_fetch_1
Builtin __sync_xor_and_fetch_2
Builtin __sync_xor_and_fetch_4
Builtin __sync_xor_and_fetch_8
Builtin __sync_xor_and_fetch_16
Builtin __sync_nand_and_fetch
Builtin __sync_nand_and_fetch_1
Builtin __sync_nand_and_fetch_2
Builtin __sync_nand_and_fetch_4
Builtin __sync_nand_and_fetch_8
Builtin __sync_nand_and_fetch_16
Builtin __sync_bool_compare_and_swap
Builtin __sync_bool_compare_and_swap_1
Builtin __sync_bool_compare_and_swap_2
Builtin __sync_bool_compare_and_swap_4
Builtin __sync_bool_compare_and_swap_8
Builtin __sync_bool_compare_and_swap_16
Builtin __sync_val_compare_and_swap
Builtin __sync_val_compare_and_swap_1
Builtin __sync_val_compare_and_swap_2
Builtin __sync_val_compare_and_swap_4
Builtin __sync_val_compare_and_swap_8
Builtin __sync_val_compare_and_swap_16
Builtin __sync_lock_test_and_set
Builtin __sync_lock_test_and_set_1
Builtin __sync_lock_test_and_set_2
Builtin __sync_lock_test_and_set_4
Builtin __sync_lock_test_and_set_8
Builtin __sync_lock_test_and_set_16
Builtin __sync_lock_release
Builtin __sync_lock_release_1
Builtin __sync_lock_release_2
Builtin __sync_lock_release_4
Builtin __sync_lock_release_8
Builtin __sync_lock_release_16
Builtin __sync_synchronize
Builtin __builtin_llvm_memory_barrier
Builtin __sync_fetch_and_min
Builtin __sync_fetch_and_max
Builtin __sync_fetch_and_umin
Builtin __sync_fetch_and_umax
Builtin abort
Builtin calloc
Builtin exit
Builtin _Exit
Builtin malloc
Builtin realloc
Builtin memcpy
Builtin memmove
Builtin strcpy
Builtin strncpy
Builtin strcat
Builtin strncat
Builtin strxfrm
Builtin memchr
Builtin strchr
Builtin strcspn
Builtin strpbrk
Builtin strrchr
Builtin strspn
Builtin strstr
Builtin strtok
Builtin memset
Builtin strerror
Builtin strlen
Builtin printf
Builtin fprintf
Builtin snprintf
Builtin sprintf
Builtin vprintf
Builtin vfprintf
Builtin vsnprintf
Builtin vsprintf
Builtin longjmp
Builtin alloca
Builtin stpcpy
Builtin stpncpy
Builtin strdup
Builtin strndup
Builtin index
Builtin rindex
Builtin bzero
Builtin _exit
Builtin _longjmp
Builtin siglongjmp
Builtin objc_msgSend
Builtin pow
Builtin powl
Builtin powf
Builtin sqrt
Builtin sqrtl
Builtin sqrtf
Builtin sin
Builtin sinl
Builtin sinf
Builtin cos
Builtin cosl
Builtin cosf
Builtin __builtin_ia32_emms
Builtin __builtin_ia32_comieq
Builtin __builtin_ia32_comilt
Builtin __builtin_ia32_comile
Builtin __builtin_ia32_comigt
Builtin __builtin_ia32_comige
Builtin __builtin_ia32_comineq
Builtin __builtin_ia32_ucomieq
Builtin __builtin_ia32_ucomilt
Builtin __builtin_ia32_ucomile
Builtin __builtin_ia32_ucomigt
Builtin __builtin_ia32_ucomige
Builtin __builtin_ia32_ucomineq
Builtin __builtin_ia32_comisdeq
Builtin __builtin_ia32_comisdlt
Builtin __builtin_ia32_comisdle
Builtin __builtin_ia32_comisdgt
Builtin __builtin_ia32_comisdge
Builtin __builtin_ia32_comisdneq
Builtin __builtin_ia32_ucomisdeq
Builtin __builtin_ia32_ucomisdlt
Builtin __builtin_ia32_ucomisdle
Builtin __builtin_ia32_ucomisdgt
Builtin __builtin_ia32_ucomisdge
Builtin __builtin_ia32_ucomisdneq
Builtin __builtin_ia32_cmpps
Builtin __builtin_ia32_cmpss
Builtin __builtin_ia32_minps
Builtin __builtin_ia32_maxps
Builtin __builtin_ia32_minss
Builtin __builtin_ia32_maxss
Builtin __builtin_ia32_paddsb
Builtin __builtin_ia32_paddsw
Builtin __builtin_ia32_psubsb
Builtin __builtin_ia32_psubsw
Builtin __builtin_ia32_paddusb
Builtin __builtin_ia32_paddusw
Builtin __builtin_ia32_psubusb
Builtin __builtin_ia32_psubusw
Builtin __builtin_ia32_pmulhw
Builtin __builtin_ia32_pmulhuw
Builtin __builtin_ia32_pavgb
Builtin __builtin_ia32_pavgw
Builtin __builtin_ia32_pcmpeqb
Builtin __builtin_ia32_pcmpeqw
Builtin __builtin_ia32_pcmpeqd
Builtin __builtin_ia32_pcmpgtb
Builtin __builtin_ia32_pcmpgtw
Builtin __builtin_ia32_pcmpgtd
Builtin __builtin_ia32_pmaxub
Builtin __builtin_ia32_pmaxsw
Builtin __builtin_ia32_pminub
Builtin __builtin_ia32_pminsw
Builtin __builtin_ia32_punpcklwd
Builtin __builtin_ia32_cmppd
Builtin __builtin_ia32_cmpsd
Builtin __builtin_ia32_minpd
Builtin __builtin_ia32_maxpd
Builtin __builtin_ia32_minsd
Builtin __builtin_ia32_maxsd
Builtin __builtin_ia32_paddsb128
Builtin __builtin_ia32_paddsw128
Builtin __builtin_ia32_psubsb128
Builtin __builtin_ia32_psubsw128
Builtin __builtin_ia32_paddusb128
Builtin __builtin_ia32_paddusw128
Builtin __builtin_ia32_psubusb128
Builtin __builtin_ia32_psubusw128
Builtin __builtin_ia32_pmulhw128
Builtin __builtin_ia32_pavgb128
Builtin __builtin_ia32_pavgw128
Builtin __builtin_ia32_pcmpeqb128
Builtin __builtin_ia32_pcmpeqw128
Builtin __builtin_ia32_pcmpeqd128
Builtin __builtin_ia32_pcmpgtb128
Builtin __builtin_ia32_pcmpgtw128
Builtin __builtin_ia32_pcmpgtd128
Builtin __builtin_ia32_pmaxub128
Builtin __builtin_ia32_pmaxsw128
Builtin __builtin_ia32_pminub128
Builtin __builtin_ia32_pminsw128
Builtin __builtin_ia32_packsswb128
Builtin __builtin_ia32_packssdw128
Builtin __builtin_ia32_packuswb128
Builtin __builtin_ia32_pmulhuw128
Builtin __builtin_ia32_addsubps
Builtin __builtin_ia32_addsubpd
Builtin __builtin_ia32_haddps
Builtin __builtin_ia32_haddpd
Builtin __builtin_ia32_hsubps
Builtin __builtin_ia32_hsubpd
Builtin __builtin_ia32_phaddw128
Builtin __builtin_ia32_phaddw
Builtin __builtin_ia32_phaddd128
Builtin __builtin_ia32_phaddd
Builtin __builtin_ia32_phaddsw128
Builtin __builtin_ia32_phaddsw
Builtin __builtin_ia32_phsubw128
Builtin __builtin_ia32_phsubw
Builtin __builtin_ia32_phsubd128
Builtin __builtin_ia32_phsubd
Builtin __builtin_ia32_phsubsw128
Builtin __builtin_ia32_phsubsw
Builtin __builtin_ia32_pmaddubsw128
Builtin __builtin_ia32_pmaddubsw
Builtin __builtin_ia32_pmulhrsw128
Builtin __builtin_ia32_pmulhrsw
Builtin __builtin_ia32_pshufb128
Builtin __builtin_ia32_pshufb
Builtin __builtin_ia32_psignb128
Builtin __builtin_ia32_psignb
Builtin __builtin_ia32_psignw128
Builtin __builtin_ia32_psignw
Builtin __builtin_ia32_psignd128
Builtin __builtin_ia32_psignd
Builtin __builtin_ia32_pabsb128
Builtin __builtin_ia32_pabsb
Builtin __builtin_ia32_pabsw128
Builtin __builtin_ia32_pabsw
Builtin __builtin_ia32_pabsd128
Builtin __builtin_ia32_pabsd
Builtin __builtin_ia32_psllw
Builtin __builtin_ia32_pslld
Builtin __builtin_ia32_psllq
Builtin __builtin_ia32_psrlw
Builtin __builtin_ia32_psrld
Builtin __builtin_ia32_psrlq
Builtin __builtin_ia32_psraw
Builtin __builtin_ia32_psrad
Builtin __builtin_ia32_pmaddwd
Builtin __builtin_ia32_packsswb
Builtin __builtin_ia32_packssdw
Builtin __builtin_ia32_packuswb
Builtin __builtin_ia32_ldmxcsr
Builtin __builtin_ia32_stmxcsr
Builtin __builtin_ia32_cvtpi2ps
Builtin __builtin_ia32_cvtps2pi
Builtin __builtin_ia32_cvtss2si
Builtin __builtin_ia32_cvtss2si64
Builtin __builtin_ia32_cvttps2pi
Builtin __builtin_ia32_maskmovq
Builtin __builtin_ia32_loadups
Builtin __builtin_ia32_storeups
Builtin __builtin_ia32_storehps
Builtin __builtin_ia32_storelps
Builtin __builtin_ia32_movmskps
Builtin __builtin_ia32_pmovmskb
Builtin __builtin_ia32_movntps
Builtin __builtin_ia32_movntq
Builtin __builtin_ia32_sfence
Builtin __builtin_ia32_psadbw
Builtin __builtin_ia32_rcpps
Builtin __builtin_ia32_rcpss
Builtin __builtin_ia32_rsqrtps
Builtin __builtin_ia32_rsqrtss
Builtin __builtin_ia32_sqrtps
Builtin __builtin_ia32_sqrtss
Builtin __builtin_ia32_maskmovdqu
Builtin __builtin_ia32_loadupd
Builtin __builtin_ia32_storeupd
Builtin __builtin_ia32_movmskpd
Builtin __builtin_ia32_pmovmskb128
Builtin __builtin_ia32_movnti
Builtin __builtin_ia32_movntpd
Builtin __builtin_ia32_movntdq
Builtin __builtin_ia32_psadbw128
Builtin __builtin_ia32_sqrtpd
Builtin __builtin_ia32_sqrtsd
Builtin __builtin_ia32_cvtdq2pd
Builtin __builtin_ia32_cvtdq2ps
Builtin __builtin_ia32_cvtpd2dq
Builtin __builtin_ia32_cvtpd2pi
Builtin __builtin_ia32_cvtpd2ps
Builtin __builtin_ia32_cvttpd2dq
Builtin __builtin_ia32_cvttpd2pi
Builtin __builtin_ia32_cvtpi2pd
Builtin __builtin_ia32_cvtsd2si
Builtin __builtin_ia32_cvtsd2si64
Builtin __builtin_ia32_cvtps2dq
Builtin __builtin_ia32_cvtps2pd
Builtin __builtin_ia32_cvttps2dq
Builtin __builtin_ia32_clflush
Builtin __builtin_ia32_lfence
Builtin __builtin_ia32_mfence
Builtin __builtin_ia32_loaddqu
Builtin __builtin_ia32_storedqu
Builtin __builtin_ia32_psllwi
Builtin __builtin_ia32_pslldi
Builtin __builtin_ia32_psllqi
Builtin __builtin_ia32_psrawi
Builtin __builtin_ia32_psradi
Builtin __builtin_ia32_psrlwi
Builtin __builtin_ia32_psrldi
Builtin __builtin_ia32_psrlqi
Builtin __builtin_ia32_pmuludq
Builtin __builtin_ia32_pmuludq128
Builtin __builtin_ia32_psraw128
Builtin __builtin_ia32_psrad128
Builtin __builtin_ia32_psrlw128
Builtin __builtin_ia32_psrld128
Builtin __builtin_ia32_pslldqi128
Builtin __builtin_ia32_psrldqi128
Builtin __builtin_ia32_psrlq128
Builtin __builtin_ia32_psllw128
Builtin __builtin_ia32_pslld128
Builtin __builtin_ia32_psllq128
Builtin __builtin_ia32_psllwi128
Builtin __builtin_ia32_pslldi128
Builtin __builtin_ia32_psllqi128
Builtin __builtin_ia32_psrlwi128
Builtin __builtin_ia32_psrldi128
Builtin __builtin_ia32_psrlqi128
Builtin __builtin_ia32_psrawi128
Builtin __builtin_ia32_psradi128
Builtin __builtin_ia32_pmaddwd128
Builtin __builtin_ia32_monitor
Builtin __builtin_ia32_mwait
Builtin __builtin_ia32_lddqu
Builtin __builtin_ia32_palignr128
Builtin __builtin_ia32_palignr
Builtin __builtin_ia32_insertps128
Builtin __builtin_ia32_storelv4si
Builtin __builtin_ia32_pblendvb128
Builtin __builtin_ia32_pblendw128
Builtin __builtin_ia32_blendpd
Builtin __builtin_ia32_blendps
Builtin __builtin_ia32_blendvpd
Builtin __builtin_ia32_blendvps
Builtin __builtin_ia32_packusdw128
Builtin __builtin_ia32_pmaxsb128
Builtin __builtin_ia32_pmaxsd128
Builtin __builtin_ia32_pmaxud128
Builtin __builtin_ia32_pmaxuw128
Builtin __builtin_ia32_pminsb128
Builtin __builtin_ia32_pminsd128
Builtin __builtin_ia32_pminud128
Builtin __builtin_ia32_pminuw128
Builtin __builtin_ia32_pmovsxbd128
Builtin __builtin_ia32_pmovsxbq128
Builtin __builtin_ia32_pmovsxbw128
Builtin __builtin_ia32_pmovsxdq128
Builtin __builtin_ia32_pmovsxwd128
Builtin __builtin_ia32_pmovsxwq128
Builtin __builtin_ia32_pmovzxbd128
Builtin __builtin_ia32_pmovzxbq128
Builtin __builtin_ia32_pmovzxbw128
Builtin __builtin_ia32_pmovzxdq128
Builtin __builtin_ia32_pmovzxwd128
Builtin __builtin_ia32_pmovzxwq128
Builtin __builtin_ia32_pmuldq128
Builtin __builtin_ia32_pmulld128
Builtin __builtin_ia32_roundps
Builtin __builtin_ia32_roundss
Builtin __builtin_ia32_roundsd
Builtin __builtin_ia32_roundpd
Builtin __builtin_ia32_dpps
Builtin __builtin_ia32_dppd
Builtin __builtin_ia32_movntdqa
clang error (/usr/include/architecture/i386/math.h): use of undeclared identifier '__builtin_fabsf'

pretty please - anyone :slight_smile:

Yes.

Under /include/clang/Basic/ I have a Builtins.def which includes the __builtin_fabsf which is what is complained as being undeclared by Clang when invoked. Note that iterating through the list of builtins using Ctx.BuiltinInfo.GetBuiltinNames() also finds __builtin_fabsf.

But when using the clang libraries to compile code that #includes <math.h>, __builtin_fabsf (and many others) are undeclared.

I'm confused about how they are listed in the ASTContext / Preprocessor objects, but not declared in the code. I wonder if there is some extra step I am missing, or some special defines I should pass before calling ParseAST?

FYI, this is using LLVM/Clang 2.7 release, built on Mac OS 10.6, with the following ./configure/make invocation (I need to support OS 10.5+ and 32/64 bit universal):

env MACOSX_DEPLOYMENT_TARGET=10.5 ./configure --enable-targets=x86,x86_64 --enable-jit --enable-optimized --enable-pic
env MACOSX_DEPLOYMENT_TARGET=10.5 sudo make DARWIN_VERSION=10.5 UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" UNIVERSAL_SDK_PATH=/Developer/SDKs/MacOSX10.5.sdk/

The compiler is being compiled into a dynamic library, loaded by an application.
I'm including the lib/clang/1.1/include path, and have verified that these headers are being found (if I don't include them, I get more errors).

I didn't have this problem with 2.6, if that helps at all.

Hi again,

I've narrowed the problem down to almost the simplest possible; this is using CompilerInstance/CompilerInvocation, in a standard tool (rather than a dynamic library). The 'undeclared identifier __builtin_fabsf' problem only occurs when compiling C++ code, not C code.

Any suggestions would be most appreciated!

The tool is used to compile the following code:

#include <math.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void test() {
  printf("%d\n", fabs(3.14));
}

#ifdef __cplusplus
}
#endif

At this point, it works fine if I compile this as C. If I try to compile it as C++, it reports errors as follows:

In file included from src:2:
In file included from /usr/include/math.h:28:
/usr/include/architecture/i386/math.h:179:63: error: use of undeclared identifier '__builtin_fabsf'

/usr/include/math.h line 27-28:
#elif (defined (__i386__) || defined( __x86_64__ ))
#include "architecture/i386/math.h"

/usr/include/architecture/i386/math.h line 179:
  static __inline__ int __inline_isinff( float __x ) { return __builtin_fabsf(__x) == __builtin_inff(); }

I'm confused why turning C++ on/off makes any difference.

Here is my source:

#include "clang/AST/ASTContext.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/SourceManager.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/TextDiagnosticBuffer.h"
#include "clang/Sema/ParseAST.h"

#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/ExecutionEngine/JITEventListener.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Type.h"
#include "llvm/Support/MemoryBuffer.h"

using namespace clang;

void compile(const char * src) {
  llvm::LLVMContext llvmContext;
  llvm::MemoryBuffer *buffer = llvm::MemoryBuffer::getMemBufferCopy(src, src+strlen(src), "src");
  if(!buffer) {
    printf("couldn't create buffer\n");
  }
  
  CompilerInstance MyCI;
  TextDiagnosticBuffer DiagsBuffer;
  Diagnostic Diags(&DiagsBuffer);
  CompilerInvocation::CreateFromArgs(MyCI.getInvocation(), NULL, NULL, Diags);
  
  /* ENABLE/DISABLE C++ */
  MyCI.getInvocation().getLangOpts().CPlusPlus = 1;
  
  // Create the actual diagnostics engine.
  MyCI.createDiagnostics(0, NULL);
  if (!MyCI.hasDiagnostics())
    return;

  MyCI.createSourceManager();
  MyCI.getSourceManager().createMainFileIDForMemBuffer(buffer);
  MyCI.createFileManager();
  MyCI.setTarget(TargetInfo::CreateTargetInfo(MyCI.getDiagnostics(), MyCI.getTargetOpts()));
  MyCI.createPreprocessor();
  MyCI.getPreprocessor();
  MyCI.createASTContext();

  llvm::SmallVector<const char *, 32> BuiltinNames;
  printf("NoBuiltins: %d\n", MyCI.getASTContext().getLangOptions().NoBuiltin);
  MyCI.getASTContext().BuiltinInfo.GetBuiltinNames(BuiltinNames, MyCI.getASTContext().getLangOptions().NoBuiltin);
  for (int i = 0; i<BuiltinNames.size(); i++) {
    printf("Builtin %s\n", BuiltinNames[i]);
  }
  
  ASTContext &context = MyCI.getASTContext();
  CodeGenOptions CGO;
  CodeGenerator * codegen = CreateLLVMCodeGen(Diags, "mymodule", CGO,llvmContext );
  ParseAST(MyCI.getPreprocessor(),
            codegen,
            MyCI.getASTContext(),
            true,
            true,
            0);
  
  llvm::Module* module = codegen->ReleaseModule();
     delete codegen;
  
  if (module) {
    module->dump();
  }
}

const char * src = "\n"
  "#include <math.h>\n"
  "#include <stdio.h>\n"
  "\n"
  "#ifdef __cplusplus \n"
  "extern \"C\" { \n"
  "#endif \n"
  " \n"
  "void test() { \n"
  " printf(\"%d \\n\", fabs(3.14)); \n"
  "} \n"
  " \n"
  "#ifdef __cplusplus \n"
  "} \n"
  "#endif \n"
  "";

int main (int argc, char * const argv[]) {
  llvm::InitializeAllTargets();
  llvm::InitializeAllAsmPrinters();
  compile(src);
  return 0;
}

This is running on OS X 10.6.3, a 64-bit machine, linking against LLVM/Clang 2.7 libraries built as follows:

sudo make clean
env MACOSX_DEPLOYMENT_TARGET=10.5 ./configure --enable-targets=x86,x86_64 --enable-jit --enable-optimized --enable-pic
env MACOSX_DEPLOYMENT_TARGET=10.5 sudo make DARWIN_VERSION=10.5 UNIVERSAL=1 UNIVERSAL_ARCH="i386 x86_64" UNIVERSAL_SDK_PATH=/Developer/SDKs/MacOSX10.5.sdk/
sudo make install

Thanks!

Hi again,

I've narrowed the problem down to almost the simplest possible; this is using CompilerInstance/CompilerInvocation, in a standard tool (rather than a dynamic library). The 'undeclared identifier __builtin_fabsf' problem only occurs when compiling C++ code, not C code.

Any suggestions would be most appreciated!

Comparing your code to FrontendAction::BeginSourceFile, it appears
you're missing a line something like the following:

    PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
                                           PP.getLangOptions().NoBuiltin);

-Eli

Wonderful, it works! Thank you very much!