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-12-02 19:32:41 +03:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.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"
|
2009-12-02 19:32:41 +03:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2010-01-09 19:17:37 +03:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2009-12-02 19:32:41 +03:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2009-04-21 09:40:52 +04:00
|
|
|
#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=".
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro,
|
2009-12-03 12:14:12 +03:00
|
|
|
Diagnostic *Diags = 0) {
|
2010-01-09 12:27:11 +03:00
|
|
|
std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('=');
|
|
|
|
llvm::StringRef MacroName = MacroPair.first;
|
|
|
|
llvm::StringRef MacroBody = MacroPair.second;
|
|
|
|
if (!MacroBody.empty()) {
|
2009-04-21 09:40:52 +04:00
|
|
|
// Per GCC -D semantics, the macro ends at \n if it exists.
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r");
|
|
|
|
if (End != llvm::StringRef::npos) {
|
2009-12-03 12:14:12 +03:00
|
|
|
assert(Diags && "Unexpected macro with embedded newline!");
|
|
|
|
Diags->Report(diag::warn_fe_macro_contains_embedded_newline)
|
2010-01-09 12:27:11 +03:00
|
|
|
<< MacroName;
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(MacroName, MacroBody.substr(0, End));
|
2009-04-21 09:40:52 +04:00
|
|
|
} else {
|
|
|
|
// Push "macroname 1".
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(Macro);
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-05-15 20:08:43 +04:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
/// AddImplicitInclude - Add an implicit #include of the specified file to the
|
|
|
|
/// predefines buffer.
|
2010-01-09 19:17:37 +03:00
|
|
|
static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("#include \"" +
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
static void AddImplicitIncludeMacros(MacroBuilder &Builder,
|
|
|
|
llvm::StringRef File) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("#__include_macros \"" +
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
|
2009-04-21 09:40:52 +04:00
|
|
|
// Marker token to stop the __include_macros fetch loop.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("##"); // ##?
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// AddImplicitIncludePTH - Add an implicit #include using the original file
|
|
|
|
/// used to generate a PTH cache.
|
2010-01-09 19:17:37 +03:00
|
|
|
static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
|
|
|
|
llvm::StringRef ImplicitIncludePTH) {
|
2009-04-21 09:40:52 +04:00
|
|
|
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) {
|
2009-12-03 12:14:12 +03:00
|
|
|
PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
|
|
|
|
<< ImplicitIncludePTH;
|
|
|
|
return;
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
AddImplicitInclude(Builder, OriginalFile);
|
2009-04-21 09:40:52 +04: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 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
|
|
|
}
|
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix,
|
2009-04-21 09:40:52 +04:00
|
|
|
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");
|
|
|
|
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
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::Twine DefPrefix = "__" + Prefix + "_";
|
|
|
|
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
|
|
|
|
Builder.defineMacro(DefPrefix + "HAS_DENORM__");
|
|
|
|
Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits));
|
|
|
|
Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon));
|
|
|
|
Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
|
|
|
|
Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
|
|
|
|
Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits));
|
|
|
|
|
|
|
|
Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp));
|
|
|
|
Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp));
|
|
|
|
Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max));
|
|
|
|
|
|
|
|
Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")");
|
|
|
|
Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")");
|
|
|
|
Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min));
|
2009-04-21 09:40:52 +04: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).
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth,
|
|
|
|
llvm::StringRef ValSuffix, bool isSigned,
|
|
|
|
MacroBuilder& Builder) {
|
2009-04-21 09:40:52 +04:00
|
|
|
long long MaxVal;
|
|
|
|
if (isSigned)
|
|
|
|
MaxVal = (1LL << (TypeWidth - 1)) - 1;
|
|
|
|
else
|
|
|
|
MaxVal = ~0LL >> (64-TypeWidth);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix);
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
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
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty,
|
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2009-11-06 00:21:32 +03:00
|
|
|
DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
|
2010-01-09 19:17:37 +03:00
|
|
|
TI.isTypeSigned(Ty), Builder);
|
2009-11-06 00:21:32 +03:00
|
|
|
}
|
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty,
|
|
|
|
MacroBuilder &Builder) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty,
|
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty)));
|
2009-11-18 16:52:57 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
static void DefineExactWidthIntType(TargetInfo::IntType Ty,
|
2010-01-09 19:17:37 +03:00
|
|
|
const TargetInfo &TI, MacroBuilder &Builder) {
|
2009-11-16 19:36:33 +03:00
|
|
|
int TypeWidth = TI.getTypeWidth(Ty);
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder);
|
2009-11-16 19:36:33 +03:00
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
|
|
|
|
if (!ConstSuffix.empty())
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__",
|
2010-01-09 19:17:37 +03:00
|
|
|
ConstSuffix);
|
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,
|
2010-01-09 19:17:37 +03:00
|
|
|
MacroBuilder &Builder) {
|
2009-04-21 09:40:52 +04:00
|
|
|
// Compiler version introspection macros.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__llvm__"); // LLVM Backend
|
|
|
|
Builder.defineMacro("__clang__"); // 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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__GNUC_MINOR__", "2");
|
|
|
|
Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
|
|
|
|
Builder.defineMacro("__GNUC__", "4");
|
|
|
|
Builder.defineMacro("__GXX_ABI_VERSION", "1002");
|
|
|
|
Builder.defineMacro("__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)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STDC__");
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.AsmPreprocessor)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__ASSEMBLER__");
|
2009-07-21 04:07:02 +04:00
|
|
|
|
|
|
|
if (!LangOpts.CPlusPlus) {
|
|
|
|
if (LangOpts.C99)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STDC_VERSION__", "199901L");
|
2009-07-21 04:07:02 +04:00
|
|
|
else if (!LangOpts.GNUMode && LangOpts.Digraphs)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STDC_VERSION__", "199409L");
|
2009-07-21 04:07:02 +04:00
|
|
|
}
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
// Standard conforming mode?
|
|
|
|
if (!LangOpts.GNUMode)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STRICT_ANSI__");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.CPlusPlus0x)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (LangOpts.Freestanding)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STDC_HOSTED__", "0");
|
2009-04-21 09:40:52 +04:00
|
|
|
else
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__STDC_HOSTED__");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.ObjC1) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__OBJC__");
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.ObjCNonFragileABI) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__OBJC2__");
|
|
|
|
Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LangOpts.getGCMode() != LangOptions::NonGC)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__OBJC_GC__");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.NeXTRuntime)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__NEXT_RUNTIME__");
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__CONSTANT_CFSTRINGS__");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.ObjC2)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("OBJC_NEW_PROPERTIES");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (LangOpts.PascalStrings)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__PASCAL_STRINGS__");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (LangOpts.Blocks) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
|
|
|
|
Builder.defineMacro("__BLOCKS__");
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-10-01 17:33:33 +04:00
|
|
|
if (LangOpts.Exceptions)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__EXCEPTIONS");
|
2009-10-01 17:33:33 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.CPlusPlus) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__DEPRECATED");
|
|
|
|
Builder.defineMacro("__GNUG__", "4");
|
|
|
|
Builder.defineMacro("__GXX_WEAK__");
|
2009-08-06 08:09:28 +04:00
|
|
|
if (LangOpts.GNUMode)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__cplusplus");
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__cplusplus", "199711L");
|
|
|
|
Builder.defineMacro("__private_extern__", "extern");
|
2009-08-28 02:01:41 +04:00
|
|
|
// Ugly hack to work with GNU libstdc++.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("_GNU_SOURCE");
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__int8", "__INT8_TYPE__");
|
|
|
|
Builder.defineMacro("__int16", "__INT16_TYPE__");
|
|
|
|
Builder.defineMacro("__int32", "__INT32_TYPE__");
|
|
|
|
Builder.defineMacro("__int64", "__INT64_TYPE__");
|
2009-12-05 00:29:41 +03:00
|
|
|
// Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
|
|
|
|
// VC++ appears to only like __FUNCTION__.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("_WCHAR_T_DEFINED");
|
|
|
|
Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
|
2009-10-16 05:12:00 +04:00
|
|
|
// FIXME: This should be temporary until we have a __pragma
|
|
|
|
// solution, to avoid some errors flagged in VC++ headers.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES", "0");
|
2009-10-16 05:12:00 +04:00
|
|
|
}
|
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)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__OPTIMIZE__");
|
2009-04-21 09:40:52 +04:00
|
|
|
if (LangOpts.OptimizeSize)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__OPTIMIZE_SIZE__");
|
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");
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__CHAR_BIT__", "8");
|
2010-01-09 19:17:37 +03: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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
|
|
|
|
DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
|
|
|
|
DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
// Define a __POINTER_WIDTH__ macro for stdint.h.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__POINTER_WIDTH__",
|
2010-01-09 19:17:37 +03:00
|
|
|
llvm::Twine((int)TI.getPointerWidth(0)));
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-05 11:05:05 +04:00
|
|
|
if (!LangOpts.CharIsSigned)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__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
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
|
2010-01-09 19:17:37 +03:00
|
|
|
"char");
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
if (TI.getShortWidth() > TI.getCharWidth())
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
|
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
if (TI.getIntWidth() > TI.getShortWidth())
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
|
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
if (TI.getLongWidth() > TI.getIntWidth())
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
|
|
|
|
|
2009-11-12 11:08:27 +03:00
|
|
|
if (TI.getLongLongWidth() > TI.getLongWidth())
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Add __builtin_va_list typedef.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append(TI.getVAListDeclaration());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
if (const char *Prefix = TI.getUserLabelPrefix())
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (LangOpts.GNUInline)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__GNUC_GNU_INLINE__");
|
2009-04-21 09:40:52 +04:00
|
|
|
else
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__GNUC_STDC_INLINE__");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (LangOpts.NoInline)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__NO_INLINE__");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
if (unsigned PICLevel = LangOpts.PICLevel) {
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__PIC__", llvm::Twine(PICLevel));
|
|
|
|
Builder.defineMacro("__pic__", llvm::Twine(PICLevel));
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Macros to control C99 numerics and <float.h>
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
|
|
|
|
Builder.defineMacro("__FLT_RADIX__", "2");
|
2010-01-09 19:17:37 +03:00
|
|
|
int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig));
|
2009-06-28 11:36:13 +04:00
|
|
|
|
2009-06-29 03:01:01 +04:00
|
|
|
if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__SSP__");
|
2009-06-29 03:01:01 +04:00
|
|
|
else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.defineMacro("__SSP_ALL__", "2");
|
2009-06-28 11:36:13 +04:00
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
// Get other target #defines.
|
2010-01-09 20:55:51 +03:00
|
|
|
TI.getTargetDefines(LangOpts, Builder);
|
2009-04-21 09:40:52 +04:00
|
|
|
}
|
|
|
|
|
2009-12-02 19:32:41 +03:00
|
|
|
// Initialize the remapping of files to alternative contents, e.g.,
|
|
|
|
// those specified through other files.
|
|
|
|
static void InitializeFileRemapping(Diagnostic &Diags,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
FileManager &FileMgr,
|
|
|
|
const PreprocessorOptions &InitOpts) {
|
|
|
|
// Remap files in the source manager.
|
|
|
|
for (PreprocessorOptions::remapped_file_iterator
|
|
|
|
Remap = InitOpts.remapped_file_begin(),
|
|
|
|
RemapEnd = InitOpts.remapped_file_end();
|
|
|
|
Remap != RemapEnd;
|
|
|
|
++Remap) {
|
|
|
|
// Find the file that we're mapping to.
|
|
|
|
const FileEntry *ToFile = FileMgr.getFile(Remap->second);
|
|
|
|
if (!ToFile) {
|
|
|
|
Diags.Report(diag::err_fe_remap_missing_to_file)
|
|
|
|
<< Remap->first << Remap->second;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-02 21:12:28 +03:00
|
|
|
// Create the file entry for the file that we're mapping from.
|
|
|
|
const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
|
|
|
|
ToFile->getSize(),
|
|
|
|
0);
|
2009-12-02 19:32:41 +03:00
|
|
|
if (!FromFile) {
|
|
|
|
Diags.Report(diag::err_fe_remap_missing_from_file)
|
|
|
|
<< Remap->first;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the contents of the file we're mapping to.
|
|
|
|
std::string ErrorStr;
|
|
|
|
const llvm::MemoryBuffer *Buffer
|
|
|
|
= llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
|
|
|
|
if (!Buffer) {
|
|
|
|
Diags.Report(diag::err_fe_error_opening)
|
|
|
|
<< Remap->second << ErrorStr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the contents of the "from" file with the contents of
|
|
|
|
// the "to" file.
|
|
|
|
SourceMgr.overrideFileContents(FromFile, Buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-21 09:40:52 +04:00
|
|
|
/// 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) {
|
2010-01-09 19:17:37 +03:00
|
|
|
std::string PredefineBuffer;
|
|
|
|
PredefineBuffer.reserve(4080);
|
|
|
|
llvm::raw_string_ostream Predefines(PredefineBuffer);
|
|
|
|
MacroBuilder Builder(Predefines);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-12-02 19:32:41 +03:00
|
|
|
InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
|
|
|
|
PP.getFileManager(), InitOpts);
|
|
|
|
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("# 1 \"<built-in>\" 3");
|
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(),
|
2010-01-09 19:17:37 +03:00
|
|
|
Builder);
|
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.
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("# 1 \"<command line>\" 1");
|
2009-04-21 09:40:52 +04:00
|
|
|
|
|
|
|
// 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
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.undefineMacro(InitOpts.Macros[i].first);
|
2009-04-21 10:00:24 +04:00
|
|
|
else
|
2010-01-09 19:17:37 +03:00
|
|
|
DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
|
2009-12-03 12:14:12 +03:00
|
|
|
&PP.getDiagnostics());
|
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)
|
2010-01-09 19:17:37 +03:00
|
|
|
AddImplicitIncludeMacros(Builder, 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)
|
2010-01-09 19:17:37 +03:00
|
|
|
AddImplicitIncludePTH(Builder, PP, Path);
|
2009-04-21 10:00:24 +04:00
|
|
|
else
|
2010-01-09 19:17:37 +03:00
|
|
|
AddImplicitInclude(Builder, 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).
|
2010-01-09 20:43:21 +03:00
|
|
|
Builder.append("# 1 \"<built-in>\" 2");
|
2009-12-01 21:28:16 +03:00
|
|
|
|
2010-01-09 19:17:37 +03:00
|
|
|
// Copy PredefinedBuffer into the Preprocessor.
|
|
|
|
PP.setPredefines(Predefines.str());
|
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
|
|
|
}
|