2009-04-21 13:40:52 +08:00
|
|
|
//===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the clang::InitializePreprocessor function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-04-30 10:51:06 +08:00
|
|
|
#include "clang/Basic/Version.h"
|
2009-11-07 12:20:15 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2010-01-20 14:13:02 +08:00
|
|
|
#include "clang/Basic/MacroBuilder.h"
|
2009-04-21 13:40:52 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-12-03 00:32:41 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2010-01-14 02:51:17 +08:00
|
|
|
#include "clang/Frontend/FrontendOptions.h"
|
2009-11-07 12:20:15 +08:00
|
|
|
#include "clang/Frontend/PreprocessorOptions.h"
|
2011-12-09 09:33:57 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2009-04-21 13:40:52 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-12-03 00:32:41 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2010-01-10 00:17:37 +08:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2010-12-22 00:45:57 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2009-12-03 00:32:41 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2009-11-03 05:48:09 +08:00
|
|
|
using namespace clang;
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
// Append a #define line to Buf for Macro. Macro should be of the form XXX,
|
|
|
|
// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
|
|
|
|
// "#define XXX Y z W". To get a #define with no value, use "XXX=".
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags) {
|
2011-07-23 18:55:15 +08:00
|
|
|
std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
|
|
|
|
StringRef MacroName = MacroPair.first;
|
|
|
|
StringRef MacroBody = MacroPair.second;
|
2010-01-10 08:46:21 +08:00
|
|
|
if (MacroName.size() != Macro.size()) {
|
2009-04-21 13:40:52 +08:00
|
|
|
// Per GCC -D semantics, the macro ends at \n if it exists.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef::size_type End = MacroBody.find_first_of("\n\r");
|
|
|
|
if (End != StringRef::npos)
|
2010-01-10 08:46:21 +08:00
|
|
|
Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
|
2010-01-09 17:27:11 +08:00
|
|
|
<< MacroName;
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro(MacroName, MacroBody.substr(0, End));
|
2009-04-21 13:40:52 +08:00
|
|
|
} else {
|
|
|
|
// Push "macroname 1".
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro(Macro);
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-05-16 00:08:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 06:07:09 +08:00
|
|
|
/// AddImplicitInclude - Add an implicit \#include of the specified file to the
|
2009-04-21 13:40:52 +08:00
|
|
|
/// predefines buffer.
|
2011-07-23 18:55:15 +08:00
|
|
|
static void AddImplicitInclude(MacroBuilder &Builder, StringRef File,
|
2011-02-24 05:16:44 +08:00
|
|
|
FileManager &FileMgr) {
|
2011-12-09 09:33:57 +08:00
|
|
|
Builder.append(Twine("#include \"") +
|
|
|
|
HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 00:17:37 +08:00
|
|
|
static void AddImplicitIncludeMacros(MacroBuilder &Builder,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef File,
|
2011-02-24 05:16:44 +08:00
|
|
|
FileManager &FileMgr) {
|
2011-12-09 09:33:57 +08:00
|
|
|
Builder.append(Twine("#__include_macros \"") +
|
|
|
|
HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
|
2009-04-21 13:40:52 +08:00
|
|
|
// Marker token to stop the __include_macros fetch loop.
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.append("##"); // ##?
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 06:07:09 +08:00
|
|
|
/// AddImplicitIncludePTH - Add an implicit \#include using the original file
|
|
|
|
/// used to generate a PTH cache.
|
2010-01-10 00:17:37 +08:00
|
|
|
static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ImplicitIncludePTH) {
|
2009-04-21 13:40:52 +08:00
|
|
|
PTHManager *P = PP.getPTHManager();
|
2010-06-29 04:32:40 +08:00
|
|
|
// Null check 'P' in the corner case where it couldn't be created.
|
|
|
|
const char *OriginalFile = P ? P->getOriginalSourceFile() : 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
if (!OriginalFile) {
|
2009-12-03 17:14:12 +08:00
|
|
|
PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
|
|
|
|
<< ImplicitIncludePTH;
|
|
|
|
return;
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-24 05:16:44 +08:00
|
|
|
AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// PickFP - This is used to pick a value based on the FP semantics of the
|
|
|
|
/// specified FP model.
|
|
|
|
template <typename T>
|
|
|
|
static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
|
2009-05-23 11:50:01 +08:00
|
|
|
T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
|
|
|
|
T IEEEQuadVal) {
|
2009-06-03 22:28:20 +08:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
|
2009-04-21 13:40:52 +08:00
|
|
|
return IEEESingleVal;
|
2009-06-03 22:28:20 +08:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
|
2009-04-21 13:40:52 +08:00
|
|
|
return IEEEDoubleVal;
|
2009-06-03 22:28:20 +08:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
|
2009-04-21 13:40:52 +08:00
|
|
|
return X87DoubleExtendedVal;
|
2009-06-03 22:28:20 +08:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
|
2009-05-23 11:50:01 +08:00
|
|
|
return PPCDoubleDoubleVal;
|
2009-06-03 22:28:20 +08:00
|
|
|
assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
|
2009-05-23 11:50:01 +08:00
|
|
|
return IEEEQuadVal;
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
|
2009-04-21 13:40:52 +08:00
|
|
|
const llvm::fltSemantics *Sem) {
|
|
|
|
const char *DenormMin, *Epsilon, *Max, *Min;
|
2009-09-09 23:08:12 +08:00
|
|
|
DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
|
2009-04-21 13:40:52 +08:00
|
|
|
"3.64519953188247460253e-4951L",
|
2009-05-23 11:50:01 +08:00
|
|
|
"4.94065645841246544176568792868221e-324L",
|
|
|
|
"6.47517511943802511092443895822764655e-4966L");
|
|
|
|
int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
|
2009-04-21 13:40:52 +08:00
|
|
|
Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
|
|
|
|
"1.08420217248550443401e-19L",
|
2009-05-23 11:50:01 +08:00
|
|
|
"4.94065645841246544176568792868221e-324L",
|
|
|
|
"1.92592994438723585305597794258492732e-34L");
|
|
|
|
int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
|
|
|
|
int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
|
|
|
|
int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
|
|
|
|
int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
|
|
|
|
int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
|
2009-04-21 13:40:52 +08:00
|
|
|
Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
|
|
|
|
"3.36210314311209350626e-4932L",
|
2009-05-23 11:50:01 +08:00
|
|
|
"2.00416836000897277799610805135016e-292L",
|
|
|
|
"3.36210314311209350626267781732175260e-4932L");
|
2009-04-21 13:40:52 +08:00
|
|
|
Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
|
|
|
|
"1.18973149535723176502e+4932L",
|
2009-05-23 11:50:01 +08:00
|
|
|
"1.79769313486231580793728971405301e+308L",
|
|
|
|
"1.18973149535723176508575932662800702e+4932L");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<32> DefPrefix;
|
2010-01-20 14:09:53 +08:00
|
|
|
DefPrefix = "__";
|
|
|
|
DefPrefix += Prefix;
|
|
|
|
DefPrefix += "_";
|
2010-01-10 00:17:37 +08:00
|
|
|
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
|
|
|
|
Builder.defineMacro(DefPrefix + "HAS_DENORM__");
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
|
|
|
|
Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon));
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
|
|
|
|
Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
|
2010-01-10 01:43:21 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
|
|
|
|
Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
|
|
|
|
Builder.defineMacro(DefPrefix + "MAX__", Twine(Max));
|
2010-01-10 01:43:21 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
|
|
|
|
Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
|
|
|
|
Builder.defineMacro(DefPrefix + "MIN__", Twine(Min));
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
|
|
|
|
/// named MacroName with the max value for a type with width 'TypeWidth' a
|
|
|
|
/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth,
|
|
|
|
StringRef ValSuffix, bool isSigned,
|
2011-02-24 14:54:56 +08:00
|
|
|
MacroBuilder &Builder) {
|
|
|
|
llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
|
|
|
|
: llvm::APInt::getMaxValue(TypeWidth);
|
|
|
|
Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2009-11-06 05:21:32 +08:00
|
|
|
/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
|
|
|
|
/// the width, suffix, and signedness of the given type
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty,
|
2010-01-10 00:17:37 +08:00
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2009-11-06 05:21:32 +08:00
|
|
|
DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
|
2010-01-10 00:17:37 +08:00
|
|
|
TI.isTypeSigned(Ty), Builder);
|
2009-11-06 05:21:32 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
|
2010-01-10 00:17:37 +08:00
|
|
|
MacroBuilder &Builder) {
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
|
2010-01-10 00:17:37 +08:00
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
|
2009-11-18 21:52:57 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
|
2010-05-28 08:27:15 +08:00
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
|
|
|
Builder.defineMacro(MacroName,
|
2011-07-23 18:55:15 +08:00
|
|
|
Twine(BitWidth / TI.getCharWidth()));
|
2010-05-28 08:27:15 +08:00
|
|
|
}
|
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
static void DefineExactWidthIntType(TargetInfo::IntType Ty,
|
2010-01-10 00:17:37 +08:00
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2009-11-17 00:36:33 +08:00
|
|
|
int TypeWidth = TI.getTypeWidth(Ty);
|
2010-06-30 14:30:56 +08:00
|
|
|
|
|
|
|
// Use the target specified int64 type, when appropriate, so that [u]int64_t
|
|
|
|
// ends up being defined in terms of the correct type.
|
|
|
|
if (TypeWidth == 64)
|
|
|
|
Ty = TI.getInt64Type();
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
|
2009-11-17 00:36:33 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
|
2010-01-10 00:17:37 +08:00
|
|
|
if (!ConstSuffix.empty())
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__",
|
2010-01-10 00:17:37 +08:00
|
|
|
ConstSuffix);
|
2009-11-12 16:08:27 +08:00
|
|
|
}
|
2009-04-21 13:40:52 +08:00
|
|
|
|
Implement the missing pieces needed to support libstdc++4.7's <atomic>:
__atomic_test_and_set, __atomic_clear, plus a pile of undocumented __GCC_*
predefined macros.
Implement library fallback for __atomic_is_lock_free and
__c11_atomic_is_lock_free, and implement __atomic_always_lock_free.
Contrary to their documentation, GCC's __atomic_fetch_add family don't
multiply the operand by sizeof(T) when operating on a pointer type.
libstdc++ relies on this quirk. Remove this handling for all but the
__c11_atomic_fetch_add and __c11_atomic_fetch_sub builtins.
Contrary to their documentation, __atomic_test_and_set and __atomic_clear
take a first argument of type 'volatile void *', not 'void *' or 'bool *',
and __atomic_is_lock_free and __atomic_always_lock_free have an argument
of type 'const volatile void *', not 'void *'.
With this change, libstdc++4.7's <atomic> passes libc++'s atomic test suite,
except for a couple of libstdc++ bugs and some cases where libc++'s test
suite tests for properties which implementations have latitude to vary.
llvm-svn: 154640
2012-04-13 08:45:38 +08:00
|
|
|
/// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
|
|
|
|
/// the specified properties.
|
|
|
|
static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
|
|
|
|
unsigned InlineWidth) {
|
|
|
|
// Fully-aligned, power-of-2 sizes no larger than the inline
|
|
|
|
// width will be inlined as lock-free operations.
|
|
|
|
if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
|
|
|
|
TypeWidth <= InlineWidth)
|
|
|
|
return "2"; // "always lock free"
|
|
|
|
// We cannot be certain what operations the lib calls might be
|
|
|
|
// able to implement as lock-free on future processors.
|
|
|
|
return "1"; // "sometimes lock free"
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// \brief Add definitions required for a smooth interaction between
|
|
|
|
/// Objective-C++ automated reference counting and libstdc++ (4.2).
|
|
|
|
static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
|
|
|
|
MacroBuilder &Builder) {
|
|
|
|
Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
|
|
|
|
|
|
|
|
std::string Result;
|
|
|
|
{
|
|
|
|
// Provide specializations for the __is_scalar type trait so that
|
|
|
|
// lifetime-qualified objects are not considered "scalar" types, which
|
|
|
|
// libstdc++ uses as an indicator of the presence of trivial copy, assign,
|
|
|
|
// default-construct, and destruct semantics (none of which hold for
|
|
|
|
// lifetime-qualified objects in ARC).
|
|
|
|
llvm::raw_string_ostream Out(Result);
|
|
|
|
|
|
|
|
Out << "namespace std {\n"
|
|
|
|
<< "\n"
|
|
|
|
<< "struct __true_type;\n"
|
|
|
|
<< "struct __false_type;\n"
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
Out << "template<typename _Tp> struct __is_scalar;\n"
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
Out << "template<typename _Tp>\n"
|
2011-06-24 08:08:59 +08:00
|
|
|
<< "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
|
2011-06-16 07:02:42 +08:00
|
|
|
<< " enum { __value = 0 };\n"
|
|
|
|
<< " typedef __false_type __type;\n"
|
|
|
|
<< "};\n"
|
|
|
|
<< "\n";
|
|
|
|
|
2012-08-21 10:47:43 +08:00
|
|
|
if (LangOpts.ObjCARCWeak) {
|
2011-06-16 07:02:42 +08:00
|
|
|
Out << "template<typename _Tp>\n"
|
2011-06-24 08:08:59 +08:00
|
|
|
<< "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
|
2011-06-16 07:02:42 +08:00
|
|
|
<< " enum { __value = 0 };\n"
|
|
|
|
<< " typedef __false_type __type;\n"
|
|
|
|
<< "};\n"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "template<typename _Tp>\n"
|
2011-06-24 08:08:59 +08:00
|
|
|
<< "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
|
2011-06-16 07:02:42 +08:00
|
|
|
<< " _Tp> {\n"
|
|
|
|
<< " enum { __value = 0 };\n"
|
|
|
|
<< " typedef __false_type __type;\n"
|
|
|
|
<< "};\n"
|
|
|
|
<< "\n";
|
|
|
|
|
|
|
|
Out << "}\n";
|
|
|
|
}
|
|
|
|
Builder.append(Result);
|
|
|
|
}
|
|
|
|
|
2011-06-07 14:07:12 +08:00
|
|
|
static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
const FrontendOptions &FEOpts,
|
|
|
|
MacroBuilder &Builder) {
|
2011-10-20 07:46:05 +08:00
|
|
|
if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP)
|
2011-06-07 14:07:12 +08:00
|
|
|
Builder.defineMacro("__STDC__");
|
|
|
|
if (LangOpts.Freestanding)
|
|
|
|
Builder.defineMacro("__STDC_HOSTED__", "0");
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__STDC_HOSTED__");
|
|
|
|
|
|
|
|
if (!LangOpts.CPlusPlus) {
|
2011-12-24 01:00:35 +08:00
|
|
|
if (LangOpts.C11)
|
2011-12-24 01:00:41 +08:00
|
|
|
Builder.defineMacro("__STDC_VERSION__", "201112L");
|
2011-10-29 07:02:54 +08:00
|
|
|
else if (LangOpts.C99)
|
2011-06-07 14:07:12 +08:00
|
|
|
Builder.defineMacro("__STDC_VERSION__", "199901L");
|
|
|
|
else if (!LangOpts.GNUMode && LangOpts.Digraphs)
|
|
|
|
Builder.defineMacro("__STDC_VERSION__", "199409L");
|
|
|
|
} else {
|
2012-05-04 06:18:20 +08:00
|
|
|
// C++11 [cpp.predefined]p1:
|
|
|
|
// The name __cplusplus is defined to the value 201103L when compiling a
|
|
|
|
// C++ translation unit.
|
|
|
|
if (LangOpts.CPlusPlus0x)
|
|
|
|
Builder.defineMacro("__cplusplus", "201103L");
|
|
|
|
// C++03 [cpp.predefined]p1:
|
|
|
|
// The name __cplusplus is defined to the value 199711L when compiling a
|
|
|
|
// C++ translation unit.
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__cplusplus", "199711L");
|
2011-06-07 14:07:12 +08:00
|
|
|
}
|
|
|
|
|
2011-06-11 04:56:43 +08:00
|
|
|
if (LangOpts.ObjC1)
|
|
|
|
Builder.defineMacro("__OBJC__");
|
|
|
|
|
2011-06-07 14:07:12 +08:00
|
|
|
// Not "standard" per se, but available even with the -undef flag.
|
|
|
|
if (LangOpts.AsmPreprocessor)
|
|
|
|
Builder.defineMacro("__ASSEMBLER__");
|
|
|
|
}
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
static void InitializePredefinedMacros(const TargetInfo &TI,
|
|
|
|
const LangOptions &LangOpts,
|
2010-01-14 02:51:17 +08:00
|
|
|
const FrontendOptions &FEOpts,
|
2010-01-10 00:17:37 +08:00
|
|
|
MacroBuilder &Builder) {
|
2009-04-21 13:40:52 +08:00
|
|
|
// Compiler version introspection macros.
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__llvm__"); // LLVM Backend
|
|
|
|
Builder.defineMacro("__clang__"); // Clang Frontend
|
2010-04-30 10:51:06 +08:00
|
|
|
#define TOSTR2(X) #X
|
|
|
|
#define TOSTR(X) TOSTR2(X)
|
|
|
|
Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
|
|
|
|
Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
|
|
|
|
#ifdef CLANG_VERSION_PATCHLEVEL
|
|
|
|
Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
|
|
|
|
#else
|
|
|
|
Builder.defineMacro("__clang_patchlevel__", "0");
|
|
|
|
#endif
|
|
|
|
Builder.defineMacro("__clang_version__",
|
|
|
|
"\"" CLANG_VERSION_STRING " ("
|
|
|
|
+ getClangFullRepositoryVersion() + ")\"");
|
|
|
|
#undef TOSTR
|
|
|
|
#undef TOSTR2
|
2012-03-11 06:21:14 +08:00
|
|
|
if (!LangOpts.MicrosoftMode) {
|
|
|
|
// Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
|
|
|
|
// not compiling for MSVC compatibility
|
|
|
|
Builder.defineMacro("__GNUC_MINOR__", "2");
|
|
|
|
Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
|
|
|
|
Builder.defineMacro("__GNUC__", "4");
|
|
|
|
Builder.defineMacro("__GXX_ABI_VERSION", "1002");
|
|
|
|
}
|
2011-03-31 08:53:51 +08:00
|
|
|
|
2012-01-17 01:27:18 +08:00
|
|
|
// Define macros for the C11 / C++11 memory orderings
|
|
|
|
Builder.defineMacro("__ATOMIC_RELAXED", "0");
|
|
|
|
Builder.defineMacro("__ATOMIC_CONSUME", "1");
|
|
|
|
Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
|
|
|
|
Builder.defineMacro("__ATOMIC_RELEASE", "3");
|
|
|
|
Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
|
|
|
|
Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
|
|
|
|
|
2012-02-19 00:12:34 +08:00
|
|
|
// Support for #pragma redefine_extname (Sun compatibility)
|
|
|
|
Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
|
|
|
|
|
2011-03-31 08:53:51 +08:00
|
|
|
// As sad as it is, enough software depends on the __VERSION__ for version
|
|
|
|
// checks that it is necessary to report 4.2.1 (the base GCC version we claim
|
|
|
|
// compatibility with) first.
|
|
|
|
Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
|
2011-07-23 18:55:15 +08:00
|
|
|
Twine(getClangFullCPPVersion()) + "\"");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Initialize language-specific preprocessor defines.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Standard conforming mode?
|
|
|
|
if (!LangOpts.GNUMode)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__STRICT_ANSI__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
if (LangOpts.CPlusPlus0x)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
if (LangOpts.ObjC1) {
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__OBJC2__");
|
2011-09-12 23:17:19 +08:00
|
|
|
|
|
|
|
if (LangOpts.ObjCExceptions)
|
|
|
|
Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2011-09-14 01:21:33 +08:00
|
|
|
if (LangOpts.getGC() != LangOptions::NonGC)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__OBJC_GC__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNeXTFamily())
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__NEXT_RUNTIME__");
|
2012-06-19 08:37:39 +08:00
|
|
|
|
|
|
|
Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
|
|
|
|
Builder.defineMacro("IBOutletCollection(ClassName)",
|
|
|
|
"__attribute__((iboutletcollection(ClassName)))");
|
|
|
|
Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// darwin_constant_cfstrings controls this. This is also dependent
|
|
|
|
// on other things like the runtime I believe. This is set even for C code.
|
2011-07-06 00:00:59 +08:00
|
|
|
if (!LangOpts.NoConstantCFStrings)
|
|
|
|
Builder.defineMacro("__CONSTANT_CFSTRINGS__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
if (LangOpts.ObjC2)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("OBJC_NEW_PROPERTIES");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
if (LangOpts.PascalStrings)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__PASCAL_STRINGS__");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
if (LangOpts.Blocks) {
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
|
|
|
|
Builder.defineMacro("__BLOCKS__");
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-12 23:17:19 +08:00
|
|
|
if (LangOpts.CXXExceptions)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__EXCEPTIONS");
|
2010-05-28 08:27:15 +08:00
|
|
|
if (LangOpts.RTTI)
|
|
|
|
Builder.defineMacro("__GXX_RTTI");
|
2010-02-11 02:49:11 +08:00
|
|
|
if (LangOpts.SjLjExceptions)
|
|
|
|
Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
|
2009-10-01 21:33:33 +08:00
|
|
|
|
2011-04-24 03:48:40 +08:00
|
|
|
if (LangOpts.Deprecated)
|
|
|
|
Builder.defineMacro("__DEPRECATED");
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
if (LangOpts.CPlusPlus) {
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__GNUG__", "4");
|
|
|
|
Builder.defineMacro("__GXX_WEAK__");
|
|
|
|
Builder.defineMacro("__private_extern__", "extern");
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-18 01:15:52 +08:00
|
|
|
if (LangOpts.MicrosoftExt) {
|
2009-12-05 05:29:41 +08:00
|
|
|
// Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
|
|
|
|
// VC++ appears to only like __FUNCTION__.
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
|
2009-10-16 09:12:00 +08:00
|
|
|
// Work around some issues with Visual C++ headerws.
|
|
|
|
if (LangOpts.CPlusPlus) {
|
|
|
|
// Since we define wchar_t in C++ mode.
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("_WCHAR_T_DEFINED");
|
|
|
|
Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
|
2011-05-10 06:32:46 +08:00
|
|
|
// FIXME: Support Microsoft's __identifier extension in the lexer.
|
2011-05-08 01:47:38 +08:00
|
|
|
Builder.append("#define __identifier(x) x");
|
2010-08-30 22:44:26 +08:00
|
|
|
Builder.append("class type_info;");
|
2009-10-16 09:12:00 +08:00
|
|
|
}
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
if (LangOpts.Optimize)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__OPTIMIZE__");
|
2009-04-21 13:40:52 +08:00
|
|
|
if (LangOpts.OptimizeSize)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__OPTIMIZE_SIZE__");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-03 10:46:46 +08:00
|
|
|
if (LangOpts.FastMath)
|
|
|
|
Builder.defineMacro("__FAST_MATH__");
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Initialize target-specific preprocessor defines.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-08-09 00:09:12 +08:00
|
|
|
// __BYTE_ORDER__ was added in GCC 4.6. It's analogous
|
|
|
|
// to the macro __BYTE_ORDER (no trailing underscores)
|
|
|
|
// from glibc's <endian.h> header.
|
2012-07-28 02:34:31 +08:00
|
|
|
// We don't support the PDP-11 as a target, but include
|
|
|
|
// the define so it can still be compared against.
|
|
|
|
Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
|
|
|
|
Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
|
|
|
|
Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
|
|
|
|
if (TI.isBigEndian())
|
|
|
|
Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
|
|
|
|
|
2012-08-11 03:12:37 +08:00
|
|
|
|
|
|
|
if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
|
|
|
|
&& TI.getIntWidth() == 32) {
|
|
|
|
Builder.defineMacro("_LP64");
|
|
|
|
Builder.defineMacro("__LP64__");
|
|
|
|
}
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Define type sizing macros based on the target properties.
|
|
|
|
assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__CHAR_BIT__", "8");
|
2010-01-10 00:17:37 +08:00
|
|
|
|
|
|
|
DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
|
|
|
|
DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
|
|
|
|
DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
|
|
|
|
DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
|
|
|
|
DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
|
|
|
|
DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
|
|
|
|
DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
|
|
|
|
|
2010-05-28 08:27:15 +08:00
|
|
|
DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
|
|
|
|
TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_SIZE_T__",
|
|
|
|
TI.getTypeWidth(TI.getSizeType()), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_WCHAR_T__",
|
|
|
|
TI.getTypeWidth(TI.getWCharType()), TI, Builder);
|
|
|
|
DefineTypeSizeof("__SIZEOF_WINT_T__",
|
|
|
|
TI.getTypeWidth(TI.getWIntType()), TI, Builder);
|
|
|
|
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
|
|
|
|
DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
|
|
|
|
DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
|
|
|
|
DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
|
|
|
|
DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
|
|
|
|
DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
|
|
|
|
DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
|
|
|
|
DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
|
|
|
|
DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
|
|
|
|
DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
|
|
|
|
DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
|
|
|
|
DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
|
|
|
|
DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
|
|
|
|
DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
|
2010-05-28 08:27:15 +08:00
|
|
|
DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
|
|
|
|
DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
|
2010-01-10 00:17:37 +08:00
|
|
|
|
|
|
|
DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
|
|
|
|
DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
|
|
|
|
DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
// Define a __POINTER_WIDTH__ macro for stdint.h.
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__POINTER_WIDTH__",
|
2011-07-23 18:55:15 +08:00
|
|
|
Twine((int)TI.getPointerWidth(0)));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-05 15:05:05 +08:00
|
|
|
if (!LangOpts.CharIsSigned)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__CHAR_UNSIGNED__");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
2012-05-04 19:23:40 +08:00
|
|
|
if (!TargetInfo::isTypeSigned(TI.getWCharType()))
|
|
|
|
Builder.defineMacro("__WCHAR_UNSIGNED__");
|
|
|
|
|
2011-04-21 13:45:45 +08:00
|
|
|
if (!TargetInfo::isTypeSigned(TI.getWIntType()))
|
|
|
|
Builder.defineMacro("__WINT_UNSIGNED__");
|
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
// Define exact-width integer types for stdint.h
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
|
2010-01-10 00:17:37 +08:00
|
|
|
"char");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
if (TI.getShortWidth() > TI.getCharWidth())
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
|
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
if (TI.getIntWidth() > TI.getShortWidth())
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
|
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
if (TI.getLongWidth() > TI.getIntWidth())
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
|
|
|
|
|
2009-11-12 16:08:27 +08:00
|
|
|
if (TI.getLongLongWidth() > TI.getLongWidth())
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
|
|
|
|
|
|
|
|
if (const char *Prefix = TI.getUserLabelPrefix())
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-07-19 11:52:53 +08:00
|
|
|
if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
|
|
|
|
Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
|
|
|
|
else
|
|
|
|
Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
if (LangOpts.GNUInline)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__GNUC_GNU_INLINE__");
|
2009-04-21 13:40:52 +08:00
|
|
|
else
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__GNUC_STDC_INLINE__");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
Implement the missing pieces needed to support libstdc++4.7's <atomic>:
__atomic_test_and_set, __atomic_clear, plus a pile of undocumented __GCC_*
predefined macros.
Implement library fallback for __atomic_is_lock_free and
__c11_atomic_is_lock_free, and implement __atomic_always_lock_free.
Contrary to their documentation, GCC's __atomic_fetch_add family don't
multiply the operand by sizeof(T) when operating on a pointer type.
libstdc++ relies on this quirk. Remove this handling for all but the
__c11_atomic_fetch_add and __c11_atomic_fetch_sub builtins.
Contrary to their documentation, __atomic_test_and_set and __atomic_clear
take a first argument of type 'volatile void *', not 'void *' or 'bool *',
and __atomic_is_lock_free and __atomic_always_lock_free have an argument
of type 'const volatile void *', not 'void *'.
With this change, libstdc++4.7's <atomic> passes libc++'s atomic test suite,
except for a couple of libstdc++ bugs and some cases where libc++'s test
suite tests for properties which implementations have latitude to vary.
llvm-svn: 154640
2012-04-13 08:45:38 +08:00
|
|
|
// The value written by __atomic_test_and_set.
|
|
|
|
// FIXME: This is target-dependent.
|
|
|
|
Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
|
|
|
|
|
|
|
|
// Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
|
|
|
|
unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
|
|
|
|
#define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
|
|
|
|
Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
|
|
|
|
getLockFreeValue(TI.get##Type##Width(), \
|
|
|
|
TI.get##Type##Align(), \
|
|
|
|
InlineWidthBits));
|
|
|
|
DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(CHAR, Char);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(SHORT, Short);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(INT, Int);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(LONG, Long);
|
|
|
|
DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
|
|
|
|
Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
|
|
|
|
getLockFreeValue(TI.getPointerWidth(0),
|
|
|
|
TI.getPointerAlign(0),
|
|
|
|
InlineWidthBits));
|
|
|
|
#undef DEFINE_LOCK_FREE_MACRO
|
|
|
|
|
2012-03-09 23:39:08 +08:00
|
|
|
if (LangOpts.NoInlineDefine)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__NO_INLINE__");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
if (unsigned PICLevel = LangOpts.PICLevel) {
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro("__PIC__", Twine(PICLevel));
|
|
|
|
Builder.defineMacro("__pic__", Twine(PICLevel));
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
Teach Clang about PIE compilations. This is the first step of PR12380.
First, this patch cleans up the parsing of the PIC and PIE family of
options in the driver. The existing logic failed to claim arguments all
over the place resulting in kludges that marked the options as unused.
Instead actually walk all of the arguments and claim them properly.
We now treat -f{,no-}{pic,PIC,pie,PIE} as a single set, accepting the
last one on the commandline. Previously there were lots of ordering bugs
that could creep in due to the nature of the parsing. Let me know if
folks would like weird things such as "-fPIE -fno-pic" to turn on PIE,
but disable full PIC. This doesn't make any sense to me, but we could in
theory support it.
Options that seem to have intentional "trump" status (-static, -mkernel,
etc) continue to do so and are commented as such.
Next, a -pie-level flag is threaded into the frontend, rigged to
a language option, and handled preprocessor, setting up the appropriate
defines. We'll now have the correct defines when compiling with -fpie.
The one place outside of the preprocessor that was inspecting the PIC
level (as opposed to the relocation model, which is set and handled
separately, yay!) is in the GNU ObjC runtime. I changed it to exactly
preserve existing behavior. If folks want to change its behavior in the
face of PIE, they can do that in a separate patch.
Essentially the only functionality changed here is the preprocessor
defines and bug-fixes to the argument management.
Tests have been updated and extended to test all of this a bit more
thoroughly.
llvm-svn: 154291
2012-04-09 00:40:35 +08:00
|
|
|
if (unsigned PIELevel = LangOpts.PIELevel) {
|
|
|
|
Builder.defineMacro("__PIE__", Twine(PIELevel));
|
|
|
|
Builder.defineMacro("__pie__", Twine(PIELevel));
|
|
|
|
}
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
// Macros to control C99 numerics and <float.h>
|
2011-12-28 23:47:06 +08:00
|
|
|
Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__FLT_RADIX__", "2");
|
2010-01-10 00:17:37 +08:00
|
|
|
int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
|
2011-07-23 18:55:15 +08:00
|
|
|
Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));
|
2009-06-28 15:36:13 +08:00
|
|
|
|
2011-09-14 01:21:33 +08:00
|
|
|
if (LangOpts.getStackProtector() == LangOptions::SSPOn)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__SSP__");
|
2011-09-14 01:21:33 +08:00
|
|
|
else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.defineMacro("__SSP_ALL__", "2");
|
2009-06-28 15:36:13 +08:00
|
|
|
|
2010-01-14 02:51:17 +08:00
|
|
|
if (FEOpts.ProgramAction == frontend::RewriteObjC)
|
|
|
|
Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
|
2010-05-27 05:36:54 +08:00
|
|
|
|
|
|
|
// Define a macro that exists only when using the static analyzer.
|
|
|
|
if (FEOpts.ProgramAction == frontend::RunAnalysis)
|
|
|
|
Builder.defineMacro("__clang_analyzer__");
|
|
|
|
|
2010-12-04 09:51:23 +08:00
|
|
|
if (LangOpts.FastRelaxedMath)
|
|
|
|
Builder.defineMacro("__FAST_RELAXED_MATH__");
|
|
|
|
|
2011-06-16 08:03:19 +08:00
|
|
|
if (LangOpts.ObjCAutoRefCount) {
|
2011-06-24 08:08:59 +08:00
|
|
|
Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
|
|
|
|
Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
|
2011-06-16 08:03:19 +08:00
|
|
|
Builder.defineMacro("__autoreleasing",
|
2011-06-24 08:08:59 +08:00
|
|
|
"__attribute__((objc_ownership(autoreleasing)))");
|
2011-06-16 08:03:19 +08:00
|
|
|
Builder.defineMacro("__unsafe_unretained",
|
2011-06-24 08:08:59 +08:00
|
|
|
"__attribute__((objc_ownership(none)))");
|
2011-06-16 08:03:19 +08:00
|
|
|
}
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Get other target #defines.
|
2010-01-10 01:55:51 +08:00
|
|
|
TI.getTargetDefines(LangOpts, Builder);
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
2009-12-03 00:32:41 +08:00
|
|
|
// Initialize the remapping of files to alternative contents, e.g.,
|
|
|
|
// those specified through other files.
|
2011-09-26 07:23:43 +08:00
|
|
|
static void InitializeFileRemapping(DiagnosticsEngine &Diags,
|
2009-12-03 00:32:41 +08:00
|
|
|
SourceManager &SourceMgr,
|
|
|
|
FileManager &FileMgr,
|
|
|
|
const PreprocessorOptions &InitOpts) {
|
2010-01-23 08:14:00 +08:00
|
|
|
// Remap files in the source manager (with buffers).
|
2010-07-23 08:33:23 +08:00
|
|
|
for (PreprocessorOptions::const_remapped_file_buffer_iterator
|
2010-01-23 08:14:00 +08:00
|
|
|
Remap = InitOpts.remapped_file_buffer_begin(),
|
|
|
|
RemapEnd = InitOpts.remapped_file_buffer_end();
|
|
|
|
Remap != RemapEnd;
|
|
|
|
++Remap) {
|
|
|
|
// Create the file entry for the file that we're mapping from.
|
|
|
|
const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
|
|
|
|
Remap->second->getBufferSize(),
|
2010-11-23 16:35:12 +08:00
|
|
|
0);
|
2010-01-23 08:14:00 +08:00
|
|
|
if (!FromFile) {
|
|
|
|
Diags.Report(diag::err_fe_remap_missing_from_file)
|
|
|
|
<< Remap->first;
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
if (!InitOpts.RetainRemappedFileBuffers)
|
|
|
|
delete Remap->second;
|
2010-01-23 08:14:00 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the contents of the "from" file with the contents of
|
|
|
|
// the "to" file.
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
SourceMgr.overrideFileContents(FromFile, Remap->second,
|
|
|
|
InitOpts.RetainRemappedFileBuffers);
|
2010-01-23 08:14:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remap files in the source manager (with other files).
|
2010-07-23 08:33:23 +08:00
|
|
|
for (PreprocessorOptions::const_remapped_file_iterator
|
|
|
|
Remap = InitOpts.remapped_file_begin(),
|
|
|
|
RemapEnd = InitOpts.remapped_file_end();
|
2009-12-03 00:32:41 +08:00
|
|
|
Remap != RemapEnd;
|
|
|
|
++Remap) {
|
|
|
|
// Find the file that we're mapping to.
|
2010-11-23 16:35:12 +08:00
|
|
|
const FileEntry *ToFile = FileMgr.getFile(Remap->second);
|
2009-12-03 00:32:41 +08:00
|
|
|
if (!ToFile) {
|
|
|
|
Diags.Report(diag::err_fe_remap_missing_to_file)
|
2010-01-23 08:14:00 +08:00
|
|
|
<< Remap->first << Remap->second;
|
2009-12-03 00:32:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-23 08:14:00 +08:00
|
|
|
|
2009-12-03 02:12:28 +08:00
|
|
|
// Create the file entry for the file that we're mapping from.
|
|
|
|
const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
|
2010-11-23 16:35:12 +08:00
|
|
|
ToFile->getSize(), 0);
|
2009-12-03 00:32:41 +08:00
|
|
|
if (!FromFile) {
|
|
|
|
Diags.Report(diag::err_fe_remap_missing_from_file)
|
2010-01-23 08:14:00 +08:00
|
|
|
<< Remap->first;
|
2009-12-03 00:32:41 +08:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-23 08:14:00 +08:00
|
|
|
|
2009-12-03 00:32:41 +08:00
|
|
|
// Override the contents of the "from" file with the contents of
|
|
|
|
// the "to" file.
|
2011-03-09 07:35:24 +08:00
|
|
|
SourceMgr.overrideFileContents(FromFile, ToFile);
|
2009-12-03 00:32:41 +08:00
|
|
|
}
|
2011-03-09 07:35:24 +08:00
|
|
|
|
|
|
|
SourceMgr.setOverridenFilesKeepOriginalName(
|
|
|
|
InitOpts.RemappedFilesKeepOriginalName);
|
2009-12-03 00:32:41 +08:00
|
|
|
}
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
/// InitializePreprocessor - Initialize the preprocessor getting it and the
|
|
|
|
/// environment ready to process a single file. This returns true on error.
|
|
|
|
///
|
2009-11-05 05:13:15 +08:00
|
|
|
void clang::InitializePreprocessor(Preprocessor &PP,
|
2009-11-12 05:44:42 +08:00
|
|
|
const PreprocessorOptions &InitOpts,
|
2010-01-14 02:51:17 +08:00
|
|
|
const HeaderSearchOptions &HSOpts,
|
|
|
|
const FrontendOptions &FEOpts) {
|
2012-03-11 15:00:24 +08:00
|
|
|
const LangOptions &LangOpts = PP.getLangOpts();
|
2010-01-10 00:17:37 +08:00
|
|
|
std::string PredefineBuffer;
|
|
|
|
PredefineBuffer.reserve(4080);
|
|
|
|
llvm::raw_string_ostream Predefines(PredefineBuffer);
|
|
|
|
MacroBuilder Builder(Predefines);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-12-03 00:32:41 +08:00
|
|
|
InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
|
2010-11-23 16:35:12 +08:00
|
|
|
PP.getFileManager(), InitOpts);
|
2009-12-03 00:32:41 +08:00
|
|
|
|
2010-04-27 06:08:10 +08:00
|
|
|
// Emit line markers for various builtin sections of the file. We don't do
|
|
|
|
// this in asm preprocessor mode, because "# 4" is not a line marker directive
|
|
|
|
// in this mode.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!PP.getLangOpts().AsmPreprocessor)
|
2010-04-27 06:08:10 +08:00
|
|
|
Builder.append("# 1 \"<built-in>\" 3");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Install things like __POWERPC__, __GNUC__, etc into the macro table.
|
2011-06-16 07:02:42 +08:00
|
|
|
if (InitOpts.UsePredefines) {
|
|
|
|
InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
|
|
|
|
|
|
|
|
// Install definitions to make Objective-C++ ARC work well with various
|
|
|
|
// C++ Standard Library implementations.
|
|
|
|
if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
|
|
|
|
switch (InitOpts.ObjCXXARCStandardLibrary) {
|
|
|
|
case ARCXX_nolib:
|
2011-09-28 06:48:19 +08:00
|
|
|
case ARCXX_libcxx:
|
2011-06-16 07:02:42 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ARCXX_libstdcxx:
|
|
|
|
AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-07 14:07:12 +08:00
|
|
|
// Even with predefines off, some macros are still predefined.
|
|
|
|
// These should all be defined in the preprocessor according to the
|
|
|
|
// current language configuration.
|
2012-03-11 15:00:24 +08:00
|
|
|
InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
|
2011-06-07 14:07:12 +08:00
|
|
|
FEOpts, Builder);
|
|
|
|
|
2009-04-21 13:40:52 +08:00
|
|
|
// Add on the predefines from the driver. Wrap in a #line directive to report
|
|
|
|
// that they come from the command line.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!PP.getLangOpts().AsmPreprocessor)
|
2010-04-27 06:08:10 +08:00
|
|
|
Builder.append("# 1 \"<command line>\" 1");
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
// Process #define's and #undef's in the order they are given.
|
2009-11-17 13:52:41 +08:00
|
|
|
for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
|
|
|
|
if (InitOpts.Macros[i].second) // isUndef
|
2010-01-10 01:43:21 +08:00
|
|
|
Builder.undefineMacro(InitOpts.Macros[i].first);
|
2009-04-21 14:00:24 +08:00
|
|
|
else
|
2010-01-10 00:17:37 +08:00
|
|
|
DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
|
2010-01-10 08:46:21 +08:00
|
|
|
PP.getDiagnostics());
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If -imacros are specified, include them now. These are processed before
|
|
|
|
// any -include directives.
|
2009-11-17 13:52:41 +08:00
|
|
|
for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
|
2011-02-24 05:16:44 +08:00
|
|
|
AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
|
|
|
|
PP.getFileManager());
|
2009-04-21 13:40:52 +08:00
|
|
|
|
|
|
|
// Process -include directives.
|
2009-11-17 13:52:41 +08:00
|
|
|
for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
|
|
|
|
const std::string &Path = InitOpts.Includes[i];
|
|
|
|
if (Path == InitOpts.ImplicitPTHInclude)
|
2010-01-10 00:17:37 +08:00
|
|
|
AddImplicitIncludePTH(Builder, PP, Path);
|
2009-04-21 14:00:24 +08:00
|
|
|
else
|
2011-02-24 05:16:44 +08:00
|
|
|
AddImplicitInclude(Builder, Path, PP.getFileManager());
|
2009-06-15 17:57:52 +08:00
|
|
|
}
|
2009-04-21 13:40:52 +08:00
|
|
|
|
2009-12-02 02:28:16 +08:00
|
|
|
// Exit the command line and go back to <built-in> (2 is LC_LEAVE).
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!PP.getLangOpts().AsmPreprocessor)
|
2010-04-27 06:08:10 +08:00
|
|
|
Builder.append("# 1 \"<built-in>\" 2");
|
2009-12-02 02:28:16 +08:00
|
|
|
|
Introduce basic support for loading a precompiled preamble while
reparsing an ASTUnit. When saving a preamble, create a buffer larger
than the actual file we're working with but fill everything from the
end of the preamble to the end of the file with spaces (so the lexer
will quickly skip them). When we load the file, create a buffer of the
same size, filling it with the file and then spaces. Then, instruct
the lexer to start lexing after the preamble, therefore continuing the
parse from the spot where the preamble left off.
It's now possible to perform a simple preamble build + parse (+
reparse) with ASTUnit. However, one has to disable a bunch of checking
in the PCH reader to do so. That part isn't committed; it will likely
be handled with some other kind of flag (e.g., -fno-validate-pch).
As part of this, fix some issues with null termination of the memory
buffers created for the preamble; we were trying to explicitly
NULL-terminate them, even though they were also getting implicitly
NULL terminated, leading to excess warnings about NULL characters in
source files.
llvm-svn: 109445
2010-07-27 05:36:20 +08:00
|
|
|
// Instruct the preprocessor to skip the preamble.
|
|
|
|
PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
|
|
|
|
InitOpts.PrecompiledPreambleBytes.second);
|
|
|
|
|
2010-01-10 00:17:37 +08:00
|
|
|
// Copy PredefinedBuffer into the Preprocessor.
|
|
|
|
PP.setPredefines(Predefines.str());
|
2011-09-16 06:00:41 +08:00
|
|
|
|
2009-11-12 05:44:42 +08:00
|
|
|
// Initialize the header search object.
|
|
|
|
ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
|
2012-03-11 15:00:24 +08:00
|
|
|
PP.getLangOpts(),
|
2009-11-12 05:44:42 +08:00
|
|
|
PP.getTargetInfo().getTriple());
|
2009-04-21 13:40:52 +08:00
|
|
|
}
|