2009-04-21 09:40:52 +04: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-07 07:20:15 +03:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-04-21 09:40:52 +04:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2009-11-07 07:20:15 +03:00
|
|
|
#include "clang/Frontend/PreprocessorOptions.h"
|
2009-04-21 09:40:52 +04:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/System/Path.h"
|
2009-11-03 00:48:09 +03:00
|
|
|
using namespace clang;
|
2009-04-21 09:40:52 +04: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=".
|
2009-06-04 20:47:09 +04:00
|
|
|
static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
|
|
|
|
const char *Command = "#define ";
|
2009-04-21 09:40:52 +04:00
|
|
|
Buf.insert(Buf.end(), Command, Command+strlen(Command));
|
|
|
|
if (const char *Equal = strchr(Macro, '=')) {
|
|
|
|
// Turn the = into ' '.
|
|
|
|
Buf.insert(Buf.end(), Macro, Equal);
|
|
|
|
Buf.push_back(' ');
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Per GCC -D semantics, the macro ends at \n if it exists.
|
|
|
|
const char *End = strpbrk(Equal, "\n\r");
|
|
|
|
if (End) {
|
|
|
|
fprintf(stderr, "warning: macro '%s' contains embedded newline, text "
|
|
|
|
"after the newline is ignored.\n",
|
|
|
|
std::string(Macro, Equal).c_str());
|
|
|
|
} else {
|
|
|
|
End = Equal+strlen(Equal);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
Buf.insert(Buf.end(), Equal+1, End);
|
|
|
|
} else {
|
|
|
|
// Push "macroname 1".
|
|
|
|
Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
|
|
|
|
Buf.push_back(' ');
|
|
|
|
Buf.push_back('1');
|
|
|
|
}
|
|
|
|
Buf.push_back('\n');
|
|
|
|
}
|
|
|
|
|
2009-05-15 20:08:43 +04:00
|
|
|
// Append a #undef line to Buf for Macro. Macro should be of the form XXX
|
|
|
|
// and we emit "#undef XXX".
|
|
|
|
static void UndefineBuiltinMacro(std::vector<char> &Buf, const char *Macro) {
|
|
|
|
// Push "macroname".
|
|
|
|
const char *Command = "#undef ";
|
|
|
|
Buf.insert(Buf.end(), Command, Command+strlen(Command));
|
|
|
|
Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
|
|
|
|
Buf.push_back('\n');
|
|
|
|
}
|
|
|
|
|
2009-11-12 02:58:53 +03:00
|
|
|
std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
|
2009-04-22 12:53:01 +04:00
|
|
|
// Implicit include paths should be resolved relative to the current
|
|
|
|
// working directory first, and then use the regular header search
|
|
|
|
// mechanism. The proper way to handle this is to have the
|
|
|
|
// predefines buffer located at the current working directory, but
|
|
|
|
// it has not file entry. For now, workaround this by using an
|
|
|
|
// absolute path if we find the file here, and otherwise letting
|
|
|
|
// header search handle it.
|
2009-04-21 09:40:52 +04:00
|
|
|
llvm::sys::Path Path(File);
|
|
|
|
Path.makeAbsolute();
|
2009-04-22 12:53:01 +04:00
|
|
|
if (!Path.exists())
|
|
|
|
Path = File;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-12 02:58:53 +03:00
|
|
|
return Lexer::Stringify(Path.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add the quoted name of an implicit include file.
|
|
|
|
static void AddQuotedIncludePath(std::vector<char> &Buf,
|
|
|
|
const std::string &File) {
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Escape double quotes etc.
|
|
|
|
Buf.push_back('"');
|
2009-11-12 02:58:53 +03:00
|
|
|
std::string EscapedFile = NormalizeDashIncludePath(File);
|
2009-04-21 09:40:52 +04:00
|
|
|
Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end());
|
|
|
|
Buf.push_back('"');
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddImplicitInclude - Add an implicit #include of the specified file to the
|
|
|
|
/// predefines buffer.
|
2009-09-09 19:08:12 +04:00
|
|
|
static void AddImplicitInclude(std::vector<char> &Buf,
|
2009-04-21 09:40:52 +04:00
|
|
|
const std::string &File) {
|
|
|
|
const char *Inc = "#include ";
|
|
|
|
Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
|
|
|
|
AddQuotedIncludePath(Buf, File);
|
|
|
|
Buf.push_back('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddImplicitIncludeMacros(std::vector<char> &Buf,
|
|
|
|
const std::string &File) {
|
|
|
|
const char *Inc = "#__include_macros ";
|
|
|
|
Buf.insert(Buf.end(), Inc, Inc+strlen(Inc));
|
|
|
|
AddQuotedIncludePath(Buf, File);
|
|
|
|
Buf.push_back('\n');
|
|
|
|
// Marker token to stop the __include_macros fetch loop.
|
|
|
|
const char *Marker = "##\n"; // ##?
|
|
|
|
Buf.insert(Buf.end(), Marker, Marker+strlen(Marker));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// AddImplicitIncludePTH - Add an implicit #include using the original file
|
|
|
|
/// used to generate a PTH cache.
|
2009-09-09 19:08:12 +04:00
|
|
|
static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP,
|
2009-04-21 09:40:52 +04:00
|
|
|
const std::string& ImplicitIncludePTH) {
|
|
|
|
PTHManager *P = PP.getPTHManager();
|
|
|
|
assert(P && "No PTHManager.");
|
|
|
|
const char *OriginalFile = P->getOriginalSourceFile();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (!OriginalFile) {
|
|
|
|
assert(!ImplicitIncludePTH.empty());
|
|
|
|
fprintf(stderr, "error: PTH file '%s' does not designate an original "
|
|
|
|
"source header file for -include-pth\n",
|
|
|
|
ImplicitIncludePTH.c_str());
|
|
|
|
exit (1);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
AddImplicitInclude(Buf, OriginalFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 07:50:01 +04:00
|
|
|
T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
|
|
|
|
T IEEEQuadVal) {
|
2009-06-03 18:28:20 +04:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
|
2009-04-21 09:40:52 +04:00
|
|
|
return IEEESingleVal;
|
2009-06-03 18:28:20 +04:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
|
2009-04-21 09:40:52 +04:00
|
|
|
return IEEEDoubleVal;
|
2009-06-03 18:28:20 +04:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
|
2009-04-21 09:40:52 +04:00
|
|
|
return X87DoubleExtendedVal;
|
2009-06-03 18:28:20 +04:00
|
|
|
if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
|
2009-05-23 07:50:01 +04:00
|
|
|
return PPCDoubleDoubleVal;
|
2009-06-03 18:28:20 +04:00
|
|
|
assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
|
2009-05-23 07:50:01 +04:00
|
|
|
return IEEEQuadVal;
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
|
|
|
|
const llvm::fltSemantics *Sem) {
|
|
|
|
const char *DenormMin, *Epsilon, *Max, *Min;
|
2009-09-09 19:08:12 +04:00
|
|
|
DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
|
2009-04-21 09:40:52 +04:00
|
|
|
"3.64519953188247460253e-4951L",
|
2009-05-23 07:50:01 +04:00
|
|
|
"4.94065645841246544176568792868221e-324L",
|
|
|
|
"6.47517511943802511092443895822764655e-4966L");
|
|
|
|
int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
|
2009-04-21 09:40:52 +04:00
|
|
|
Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
|
|
|
|
"1.08420217248550443401e-19L",
|
2009-05-23 07:50:01 +04:00
|
|
|
"4.94065645841246544176568792868221e-324L",
|
|
|
|
"1.92592994438723585305597794258492732e-34L");
|
2009-04-21 09:40:52 +04:00
|
|
|
int HasInifinity = 1, HasQuietNaN = 1;
|
2009-05-23 07:50:01 +04:00
|
|
|
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 09:40:52 +04:00
|
|
|
Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
|
|
|
|
"3.36210314311209350626e-4932L",
|
2009-05-23 07:50:01 +04:00
|
|
|
"2.00416836000897277799610805135016e-292L",
|
|
|
|
"3.36210314311209350626267781732175260e-4932L");
|
2009-04-21 09:40:52 +04:00
|
|
|
Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
|
|
|
|
"1.18973149535723176502e+4932L",
|
2009-05-23 07:50:01 +04:00
|
|
|
"1.79769313486231580793728971405301e+308L",
|
|
|
|
"1.18973149535723176508575932662800702e+4932L");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-05-23 07:50:01 +04:00
|
|
|
char MacroBuf[100];
|
2009-04-21 09:40:52 +04:00
|
|
|
sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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).
|
|
|
|
static void DefineTypeSize(const char *MacroName, unsigned TypeWidth,
|
|
|
|
const char *ValSuffix, bool isSigned,
|
|
|
|
std::vector<char> &Buf) {
|
|
|
|
char MacroBuf[60];
|
|
|
|
long long MaxVal;
|
|
|
|
if (isSigned)
|
|
|
|
MaxVal = (1LL << (TypeWidth - 1)) - 1;
|
|
|
|
else
|
|
|
|
MaxVal = ~0LL >> (64-TypeWidth);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-09-03 23:23:49 +04:00
|
|
|
// FIXME: Switch to using raw_ostream and avoid utostr().
|
|
|
|
sprintf(MacroBuf, "%s=%s%s", MacroName, llvm::utostr(MaxVal).c_str(),
|
|
|
|
ValSuffix);
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
|
|
|
|
2009-11-06 00:21:32 +03:00
|
|
|
/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
|
|
|
|
/// the width, suffix, and signedness of the given type
|
|
|
|
static void DefineTypeSize(const char *MacroName, TargetInfo::IntType Ty,
|
|
|
|
const TargetInfo &TI, std::vector<char> &Buf) {
|
|
|
|
DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
|
|
|
|
TI.isTypeSigned(Ty), Buf);
|
|
|
|
}
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
static void DefineType(const char *MacroName, TargetInfo::IntType Ty,
|
|
|
|
std::vector<char> &Buf) {
|
|
|
|
char MacroBuf[60];
|
|
|
|
sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty));
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
|
|
|
|
2009-11-18 16:52:57 +03:00
|
|
|
static void DefineTypeWidth(const char *MacroName, TargetInfo::IntType Ty,
|
|
|
|
const TargetInfo &TI, std::vector<char> &Buf) {
|
|
|
|
char MacroBuf[60];
|
|
|
|
sprintf(MacroBuf, "%s=%d", MacroName, TI.getTypeWidth(Ty));
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
static void DefineExactWidthIntType(TargetInfo::IntType Ty,
|
|
|
|
const TargetInfo &TI, std::vector<char> &Buf) {
|
|
|
|
char MacroBuf[60];
|
2009-11-16 19:36:33 +03:00
|
|
|
int TypeWidth = TI.getTypeWidth(Ty);
|
|
|
|
sprintf(MacroBuf, "__INT%d_TYPE__", TypeWidth);
|
2009-11-12 11:08:27 +03:00
|
|
|
DefineType(MacroBuf, Ty, Buf);
|
2009-11-16 19:36:33 +03:00
|
|
|
|
|
|
|
|
|
|
|
const char *ConstSuffix = TargetInfo::getTypeConstantSuffix(Ty);
|
|
|
|
if (strlen(ConstSuffix) > 0) {
|
|
|
|
sprintf(MacroBuf, "__INT%d_C_SUFFIX__=%s", TypeWidth, ConstSuffix);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
2009-11-12 11:08:27 +03:00
|
|
|
}
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
static void InitializePredefinedMacros(const TargetInfo &TI,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
std::vector<char> &Buf) {
|
|
|
|
char MacroBuf[60];
|
|
|
|
// Compiler version introspection macros.
|
|
|
|
DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
|
|
|
|
DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Currently claim to be compatible with GCC 4.2.1-5621.
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC__=4");
|
|
|
|
DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
|
|
|
|
DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\"");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Initialize language-specific preprocessor defines.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// These should all be defined in the preprocessor according to the
|
|
|
|
// current language configuration.
|
|
|
|
if (!LangOpts.Microsoft)
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC__=1");
|
|
|
|
if (LangOpts.AsmPreprocessor)
|
|
|
|
DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
|
2009-07-21 04:07:02 +04:00
|
|
|
|
|
|
|
if (!LangOpts.CPlusPlus) {
|
|
|
|
if (LangOpts.C99)
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
|
|
|
|
else if (!LangOpts.GNUMode && LangOpts.Digraphs)
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
|
|
|
|
}
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
// Standard conforming mode?
|
|
|
|
if (!LangOpts.GNUMode)
|
|
|
|
DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.CPlusPlus0x)
|
|
|
|
DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__");
|
|
|
|
|
|
|
|
if (LangOpts.Freestanding)
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0");
|
|
|
|
else
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.ObjC1) {
|
|
|
|
DefineBuiltinMacro(Buf, "__OBJC__=1");
|
|
|
|
if (LangOpts.ObjCNonFragileABI) {
|
|
|
|
DefineBuiltinMacro(Buf, "__OBJC2__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LangOpts.getGCMode() != LangOptions::NonGC)
|
|
|
|
DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.NeXTRuntime)
|
|
|
|
DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04: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.
|
|
|
|
DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.ObjC2)
|
|
|
|
DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
|
|
|
|
|
|
|
|
if (LangOpts.PascalStrings)
|
|
|
|
DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
|
|
|
|
|
|
|
|
if (LangOpts.Blocks) {
|
|
|
|
DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
|
|
|
|
DefineBuiltinMacro(Buf, "__BLOCKS__=1");
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-01 17:33:33 +04:00
|
|
|
if (LangOpts.Exceptions)
|
|
|
|
DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.CPlusPlus) {
|
|
|
|
DefineBuiltinMacro(Buf, "__DEPRECATED=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUG__=4");
|
|
|
|
DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
|
2009-08-06 08:09:28 +04:00
|
|
|
if (LangOpts.GNUMode)
|
|
|
|
DefineBuiltinMacro(Buf, "__cplusplus=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
else
|
2009-08-06 08:09:28 +04:00
|
|
|
// C++ [cpp.predefined]p1:
|
2009-09-09 19:08:12 +04:00
|
|
|
// The name_ _cplusplusis defined to the value199711Lwhen compiling a
|
2009-08-06 08:09:28 +04:00
|
|
|
// C++ translation unit.
|
|
|
|
DefineBuiltinMacro(Buf, "__cplusplus=199711L");
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineBuiltinMacro(Buf, "__private_extern__=extern");
|
2009-08-28 02:01:41 +04:00
|
|
|
// Ugly hack to work with GNU libstdc++.
|
|
|
|
DefineBuiltinMacro(Buf, "_GNU_SOURCE=1");
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.Microsoft) {
|
2009-10-16 05:12:00 +04:00
|
|
|
// Filter out some microsoft extensions when trying to parse in ms-compat
|
|
|
|
// mode.
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__");
|
|
|
|
DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__");
|
|
|
|
DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__");
|
|
|
|
DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__");
|
2009-10-16 05:12:00 +04:00
|
|
|
// Work around some issues with Visual C++ headerws.
|
|
|
|
if (LangOpts.CPlusPlus) {
|
|
|
|
// Since we define wchar_t in C++ mode.
|
|
|
|
DefineBuiltinMacro(Buf, "_WCHAR_T_DEFINED=1");
|
|
|
|
DefineBuiltinMacro(Buf, "_NATIVE_WCHAR_T_DEFINED=1");
|
|
|
|
// FIXME: This should be temporary until we have a __pragma
|
|
|
|
// solution, to avoid some errors flagged in VC++ headers.
|
|
|
|
DefineBuiltinMacro(Buf, "_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES=0");
|
|
|
|
}
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.Optimize)
|
|
|
|
DefineBuiltinMacro(Buf, "__OPTIMIZE__=1");
|
|
|
|
if (LangOpts.OptimizeSize)
|
|
|
|
DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Initialize target-specific preprocessor defines.
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Define type sizing macros based on the target properties.
|
|
|
|
assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
|
|
|
|
DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
|
|
|
|
|
|
|
|
DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf);
|
2009-11-06 00:21:32 +03:00
|
|
|
DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Buf);
|
|
|
|
DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Buf);
|
|
|
|
DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Buf);
|
|
|
|
DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Buf);
|
2009-11-12 11:04:33 +03:00
|
|
|
DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Buf);
|
2009-11-06 00:21:32 +03:00
|
|
|
DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Buf);
|
2009-04-21 09:40:52 +04:00
|
|
|
|
2009-11-19 16:18:59 +03:00
|
|
|
DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf);
|
|
|
|
DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf);
|
2009-11-18 16:52:57 +03:00
|
|
|
DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Buf);
|
2009-11-19 16:18:59 +03:00
|
|
|
DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf);
|
2009-11-19 15:21:52 +03:00
|
|
|
DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Buf);
|
2009-11-19 16:18:59 +03:00
|
|
|
DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf);
|
2009-11-18 23:05:48 +03:00
|
|
|
DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Buf);
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf);
|
2009-11-19 16:42:09 +03:00
|
|
|
DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Buf);
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf);
|
2009-11-19 18:47:58 +03:00
|
|
|
DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Buf);
|
2009-10-21 08:59:34 +04:00
|
|
|
DefineType("__WINT_TYPE__", TI.getWIntType(), Buf);
|
2009-11-19 17:16:57 +03:00
|
|
|
DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Buf);
|
2009-11-22 18:41:04 +03:00
|
|
|
DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Buf);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
|
|
|
|
DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
|
|
|
|
DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
|
|
|
|
|
|
|
|
// Define a __POINTER_WIDTH__ macro for stdint.h.
|
|
|
|
sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0));
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 11:05:05 +04:00
|
|
|
if (!LangOpts.CharIsSigned)
|
2009-09-09 19:08:12 +04:00
|
|
|
DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
// Define exact-width integer types for stdint.h
|
|
|
|
sprintf(MacroBuf, "__INT%d_TYPE__=char", TI.getCharWidth());
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
if (TI.getShortWidth() > TI.getCharWidth())
|
|
|
|
DefineExactWidthIntType(TargetInfo::SignedShort, TI, Buf);
|
|
|
|
|
|
|
|
if (TI.getIntWidth() > TI.getShortWidth())
|
|
|
|
DefineExactWidthIntType(TargetInfo::SignedInt, TI, Buf);
|
|
|
|
|
|
|
|
if (TI.getLongWidth() > TI.getIntWidth())
|
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLong, TI, Buf);
|
|
|
|
|
|
|
|
if (TI.getLongLongWidth() > TI.getLongWidth())
|
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Buf);
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Add __builtin_va_list typedef.
|
|
|
|
{
|
|
|
|
const char *VAList = TI.getVAListDeclaration();
|
|
|
|
Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
|
|
|
|
Buf.push_back('\n');
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (const char *Prefix = TI.getUserLabelPrefix()) {
|
|
|
|
sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Build configuration options. FIXME: these should be controlled by
|
|
|
|
// command line options or something.
|
|
|
|
DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
|
|
|
|
|
|
|
|
if (LangOpts.GNUInline)
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1");
|
|
|
|
else
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1");
|
|
|
|
|
|
|
|
if (LangOpts.NoInline)
|
|
|
|
DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
|
|
|
|
|
|
|
|
if (unsigned PICLevel = LangOpts.PICLevel) {
|
|
|
|
sprintf(MacroBuf, "__PIC__=%d", PICLevel);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
|
|
|
|
sprintf(MacroBuf, "__pic__=%d", PICLevel);
|
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macros to control C99 numerics and <float.h>
|
|
|
|
DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
|
|
|
|
DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
|
|
|
|
sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
|
2009-05-23 07:50:01 +04:00
|
|
|
PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36));
|
2009-04-21 09:40:52 +04:00
|
|
|
DefineBuiltinMacro(Buf, MacroBuf);
|
2009-06-28 11:36:13 +04:00
|
|
|
|
2009-06-29 03:01:01 +04:00
|
|
|
if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
|
2009-06-28 11:36:13 +04:00
|
|
|
DefineBuiltinMacro(Buf, "__SSP__=1");
|
2009-06-29 03:01:01 +04:00
|
|
|
else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
|
2009-06-28 11:36:13 +04:00
|
|
|
DefineBuiltinMacro(Buf, "__SSP_ALL__=2");
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Get other target #defines.
|
|
|
|
TI.getTargetDefines(LangOpts, Buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// InitializePreprocessor - Initialize the preprocessor getting it and the
|
|
|
|
/// environment ready to process a single file. This returns true on error.
|
|
|
|
///
|
2009-11-05 00:13:15 +03:00
|
|
|
void clang::InitializePreprocessor(Preprocessor &PP,
|
2009-11-12 00:44:42 +03:00
|
|
|
const PreprocessorOptions &InitOpts,
|
|
|
|
const HeaderSearchOptions &HSOpts) {
|
2009-04-21 09:40:52 +04:00
|
|
|
std::vector<char> PredefineBuffer;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-22 07:42:19 +04:00
|
|
|
const char *LineDirective = "# 1 \"<built-in>\" 3\n";
|
|
|
|
PredefineBuffer.insert(PredefineBuffer.end(),
|
|
|
|
LineDirective, LineDirective+strlen(LineDirective));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Install things like __POWERPC__, __GNUC__, etc into the macro table.
|
2009-11-17 08:52:41 +03:00
|
|
|
if (InitOpts.UsePredefines)
|
2009-11-03 22:50:27 +03:00
|
|
|
InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
|
|
|
|
PredefineBuffer);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Add on the predefines from the driver. Wrap in a #line directive to report
|
|
|
|
// that they come from the command line.
|
2009-04-22 07:42:19 +04:00
|
|
|
LineDirective = "# 1 \"<command line>\" 1\n";
|
2009-04-21 09:40:52 +04:00
|
|
|
PredefineBuffer.insert(PredefineBuffer.end(),
|
|
|
|
LineDirective, LineDirective+strlen(LineDirective));
|
|
|
|
|
|
|
|
// Process #define's and #undef's in the order they are given.
|
2009-11-17 08:52:41 +03:00
|
|
|
for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
|
|
|
|
if (InitOpts.Macros[i].second) // isUndef
|
|
|
|
UndefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
|
2009-04-21 10:00:24 +04:00
|
|
|
else
|
2009-11-17 08:52:41 +03:00
|
|
|
DefineBuiltinMacro(PredefineBuffer, InitOpts.Macros[i].first.c_str());
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If -imacros are specified, include them now. These are processed before
|
|
|
|
// any -include directives.
|
2009-11-17 08:52:41 +03:00
|
|
|
for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
|
|
|
|
AddImplicitIncludeMacros(PredefineBuffer, InitOpts.MacroIncludes[i]);
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
// Process -include directives.
|
2009-11-17 08:52:41 +03:00
|
|
|
for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
|
|
|
|
const std::string &Path = InitOpts.Includes[i];
|
|
|
|
if (Path == InitOpts.ImplicitPTHInclude)
|
|
|
|
AddImplicitIncludePTH(PredefineBuffer, PP, Path);
|
2009-04-21 10:00:24 +04:00
|
|
|
else
|
2009-11-17 08:52:41 +03:00
|
|
|
AddImplicitInclude(PredefineBuffer, Path);
|
2009-06-15 13:57:52 +04:00
|
|
|
}
|
2009-04-21 09:40:52 +04:00
|
|
|
|
2009-12-01 21:28:16 +03:00
|
|
|
// Exit the command line and go back to <built-in> (2 is LC_LEAVE).
|
|
|
|
LineDirective = "# 1 \"<built-in>\" 2\n";
|
|
|
|
PredefineBuffer.insert(PredefineBuffer.end(),
|
|
|
|
LineDirective, LineDirective+strlen(LineDirective));
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Null terminate PredefinedBuffer and add it.
|
|
|
|
PredefineBuffer.push_back(0);
|
|
|
|
PP.setPredefines(&PredefineBuffer[0]);
|
2009-11-12 00:44:42 +03:00
|
|
|
|
|
|
|
// Initialize the header search object.
|
|
|
|
ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
|
|
|
|
PP.getLangOptions(),
|
|
|
|
PP.getTargetInfo().getTriple());
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|