blob: 2b35c8e2296b8d899c912934cc6f74b260b7001e [file] [log] [blame]
Chris Lattnere116ccf2009-04-21 05:40:52 +00001//===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the clang::InitializePreprocessor function.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregor4290fbd2010-04-30 02:51:06 +000014#include "clang/Basic/Version.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000015#include "clang/Frontend/Utils.h"
Chandler Carruth103b71c2010-01-20 06:13:02 +000016#include "clang/Basic/MacroBuilder.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000017#include "clang/Basic/TargetInfo.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000018#include "clang/Frontend/FrontendDiagnostic.h"
Fariborz Jahanian7d957472010-01-13 18:51:17 +000019#include "clang/Frontend/FrontendOptions.h"
Daniel Dunbar8863b982009-11-07 04:20:15 +000020#include "clang/Frontend/PreprocessorOptions.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000024#include "llvm/ADT/APFloat.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000026#include "llvm/System/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000027using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000028
29// Append a #define line to Buf for Macro. Macro should be of the form XXX,
30// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
31// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000032static void DefineBuiltinMacro(MacroBuilder &Builder, llvm::StringRef Macro,
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000033 Diagnostic &Diags) {
Kovarththanan Rajaratnam8746e4e2010-01-09 09:27:11 +000034 std::pair<llvm::StringRef, llvm::StringRef> MacroPair = Macro.split('=');
35 llvm::StringRef MacroName = MacroPair.first;
36 llvm::StringRef MacroBody = MacroPair.second;
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000037 if (MacroName.size() != Macro.size()) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000038 // Per GCC -D semantics, the macro ends at \n if it exists.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000039 llvm::StringRef::size_type End = MacroBody.find_first_of("\n\r");
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000040 if (End != llvm::StringRef::npos)
41 Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
Kovarththanan Rajaratnam8746e4e2010-01-09 09:27:11 +000042 << MacroName;
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000043 Builder.defineMacro(MacroName, MacroBody.substr(0, End));
Chris Lattnere116ccf2009-04-21 05:40:52 +000044 } else {
45 // Push "macroname 1".
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000046 Builder.defineMacro(Macro);
Chris Lattnere116ccf2009-04-21 05:40:52 +000047 }
Chris Lattnerdcdecf42009-05-15 16:08:43 +000048}
49
Daniel Dunbarc7162932009-11-11 23:58:53 +000050std::string clang::NormalizeDashIncludePath(llvm::StringRef File) {
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000051 // Implicit include paths should be resolved relative to the current
52 // working directory first, and then use the regular header search
53 // mechanism. The proper way to handle this is to have the
54 // predefines buffer located at the current working directory, but
55 // it has not file entry. For now, workaround this by using an
56 // absolute path if we find the file here, and otherwise letting
57 // header search handle it.
Chris Lattnere116ccf2009-04-21 05:40:52 +000058 llvm::sys::Path Path(File);
59 Path.makeAbsolute();
Daniel Dunbar1e7c6b62009-04-22 08:53:01 +000060 if (!Path.exists())
61 Path = File;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Daniel Dunbarc7162932009-11-11 23:58:53 +000063 return Lexer::Stringify(Path.str());
64}
65
Chris Lattnere116ccf2009-04-21 05:40:52 +000066/// AddImplicitInclude - Add an implicit #include of the specified file to the
67/// predefines buffer.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000068static void AddImplicitInclude(MacroBuilder &Builder, llvm::StringRef File) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000069 Builder.append("#include \"" +
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000070 llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000071}
72
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000073static void AddImplicitIncludeMacros(MacroBuilder &Builder,
74 llvm::StringRef File) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000075 Builder.append("#__include_macros \"" +
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000076 llvm::Twine(NormalizeDashIncludePath(File)) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000077 // Marker token to stop the __include_macros fetch loop.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000078 Builder.append("##"); // ##?
Chris Lattnere116ccf2009-04-21 05:40:52 +000079}
80
81/// AddImplicitIncludePTH - Add an implicit #include using the original file
82/// used to generate a PTH cache.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000083static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
84 llvm::StringRef ImplicitIncludePTH) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000085 PTHManager *P = PP.getPTHManager();
86 assert(P && "No PTHManager.");
87 const char *OriginalFile = P->getOriginalSourceFile();
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattnere116ccf2009-04-21 05:40:52 +000089 if (!OriginalFile) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +000090 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
91 << ImplicitIncludePTH;
92 return;
Chris Lattnere116ccf2009-04-21 05:40:52 +000093 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000095 AddImplicitInclude(Builder, OriginalFile);
Chris Lattnere116ccf2009-04-21 05:40:52 +000096}
97
98/// PickFP - This is used to pick a value based on the FP semantics of the
99/// specified FP model.
100template <typename T>
101static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000102 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
103 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000104 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000105 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000106 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000107 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000108 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000109 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000110 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000111 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000112 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000113 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000114}
115
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000116static void DefineFloatMacros(MacroBuilder &Builder, llvm::StringRef Prefix,
Chris Lattnere116ccf2009-04-21 05:40:52 +0000117 const llvm::fltSemantics *Sem) {
118 const char *DenormMin, *Epsilon, *Max, *Min;
Mike Stump1eb44332009-09-09 15:08:12 +0000119 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
Chris Lattnere116ccf2009-04-21 05:40:52 +0000120 "3.64519953188247460253e-4951L",
Eli Friedman2665a752009-05-23 03:50:01 +0000121 "4.94065645841246544176568792868221e-324L",
122 "6.47517511943802511092443895822764655e-4966L");
123 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000124 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
125 "1.08420217248550443401e-19L",
Eli Friedman2665a752009-05-23 03:50:01 +0000126 "4.94065645841246544176568792868221e-324L",
127 "1.92592994438723585305597794258492732e-34L");
Eli Friedman2665a752009-05-23 03:50:01 +0000128 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
129 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
130 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
131 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
132 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000133 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
134 "3.36210314311209350626e-4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000135 "2.00416836000897277799610805135016e-292L",
136 "3.36210314311209350626267781732175260e-4932L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000137 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
138 "1.18973149535723176502e+4932L",
Eli Friedman2665a752009-05-23 03:50:01 +0000139 "1.79769313486231580793728971405301e+308L",
140 "1.18973149535723176508575932662800702e+4932L");
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Daniel Dunbar066515f2010-01-20 06:09:53 +0000142 llvm::SmallString<32> DefPrefix;
143 DefPrefix = "__";
144 DefPrefix += Prefix;
145 DefPrefix += "_";
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000146
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000147 Builder.defineMacro(DefPrefix + "DENORM_MIN__", DenormMin);
148 Builder.defineMacro(DefPrefix + "HAS_DENORM__");
149 Builder.defineMacro(DefPrefix + "DIG__", llvm::Twine(Digits));
150 Builder.defineMacro(DefPrefix + "EPSILON__", llvm::Twine(Epsilon));
151 Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
152 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
153 Builder.defineMacro(DefPrefix + "MANT_DIG__", llvm::Twine(MantissaDigits));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000154
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000155 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", llvm::Twine(Max10Exp));
156 Builder.defineMacro(DefPrefix + "MAX_EXP__", llvm::Twine(MaxExp));
157 Builder.defineMacro(DefPrefix + "MAX__", llvm::Twine(Max));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000158
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000159 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+llvm::Twine(Min10Exp)+")");
160 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+llvm::Twine(MinExp)+")");
161 Builder.defineMacro(DefPrefix + "MIN__", llvm::Twine(Min));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000162}
163
164
165/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
166/// named MacroName with the max value for a type with width 'TypeWidth' a
167/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000168static void DefineTypeSize(llvm::StringRef MacroName, unsigned TypeWidth,
169 llvm::StringRef ValSuffix, bool isSigned,
170 MacroBuilder& Builder) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000171 long long MaxVal;
172 if (isSigned)
173 MaxVal = (1LL << (TypeWidth - 1)) - 1;
174 else
175 MaxVal = ~0LL >> (64-TypeWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000177 Builder.defineMacro(MacroName, llvm::Twine(MaxVal) + ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000178}
179
Chris Lattner9099e7b2009-11-05 21:21:32 +0000180/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
181/// the width, suffix, and signedness of the given type
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000182static void DefineTypeSize(llvm::StringRef MacroName, TargetInfo::IntType Ty,
183 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner9099e7b2009-11-05 21:21:32 +0000184 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000185 TI.isTypeSigned(Ty), Builder);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000186}
187
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000188static void DefineType(const llvm::Twine &MacroName, TargetInfo::IntType Ty,
189 MacroBuilder &Builder) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000190 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000191}
192
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000193static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty,
194 const TargetInfo &TI, MacroBuilder &Builder) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000195 Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty)));
Ken Dyck186696b2009-11-18 13:52:57 +0000196}
197
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000198static void DefineExactWidthIntType(TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000199 const TargetInfo &TI, MacroBuilder &Builder) {
Ken Dyckeef22ef2009-11-16 16:36:33 +0000200 int TypeWidth = TI.getTypeWidth(Ty);
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000201 DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder);
Ken Dyckeef22ef2009-11-16 16:36:33 +0000202
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000203 llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
204 if (!ConstSuffix.empty())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000205 Builder.defineMacro("__INT" + llvm::Twine(TypeWidth) + "_C_SUFFIX__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000206 ConstSuffix);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000207}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000208
209static void InitializePredefinedMacros(const TargetInfo &TI,
210 const LangOptions &LangOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000211 const FrontendOptions &FEOpts,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000212 MacroBuilder &Builder) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000213 // Compiler version introspection macros.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000214 Builder.defineMacro("__llvm__"); // LLVM Backend
215 Builder.defineMacro("__clang__"); // Clang Frontend
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000216#define TOSTR2(X) #X
217#define TOSTR(X) TOSTR2(X)
218 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
219 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
220#ifdef CLANG_VERSION_PATCHLEVEL
221 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
222#else
223 Builder.defineMacro("__clang_patchlevel__", "0");
224#endif
225 Builder.defineMacro("__clang_version__",
226 "\"" CLANG_VERSION_STRING " ("
227 + getClangFullRepositoryVersion() + ")\"");
228#undef TOSTR
229#undef TOSTR2
Chris Lattnere116ccf2009-04-21 05:40:52 +0000230 // Currently claim to be compatible with GCC 4.2.1-5621.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000231 Builder.defineMacro("__GNUC_MINOR__", "2");
232 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
233 Builder.defineMacro("__GNUC__", "4");
234 Builder.defineMacro("__GXX_ABI_VERSION", "1002");
235 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible Clang Compiler\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattnere116ccf2009-04-21 05:40:52 +0000237 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattnere116ccf2009-04-21 05:40:52 +0000239 // These should all be defined in the preprocessor according to the
240 // current language configuration.
241 if (!LangOpts.Microsoft)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000242 Builder.defineMacro("__STDC__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000243 if (LangOpts.AsmPreprocessor)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000244 Builder.defineMacro("__ASSEMBLER__");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000245
246 if (!LangOpts.CPlusPlus) {
247 if (LangOpts.C99)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000248 Builder.defineMacro("__STDC_VERSION__", "199901L");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000249 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000250 Builder.defineMacro("__STDC_VERSION__", "199409L");
Ryan Flynn07ef8042009-07-21 00:07:02 +0000251 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000252
253 // Standard conforming mode?
254 if (!LangOpts.GNUMode)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000255 Builder.defineMacro("__STRICT_ANSI__");
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnere116ccf2009-04-21 05:40:52 +0000257 if (LangOpts.CPlusPlus0x)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000258 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000259
260 if (LangOpts.Freestanding)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000261 Builder.defineMacro("__STDC_HOSTED__", "0");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000262 else
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000263 Builder.defineMacro("__STDC_HOSTED__");
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattnere116ccf2009-04-21 05:40:52 +0000265 if (LangOpts.ObjC1) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000266 Builder.defineMacro("__OBJC__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000267 if (LangOpts.ObjCNonFragileABI) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000268 Builder.defineMacro("__OBJC2__");
269 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000270 }
271
272 if (LangOpts.getGCMode() != LangOptions::NonGC)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000273 Builder.defineMacro("__OBJC_GC__");
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnere116ccf2009-04-21 05:40:52 +0000275 if (LangOpts.NeXTRuntime)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000276 Builder.defineMacro("__NEXT_RUNTIME__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000277 }
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Chris Lattnere116ccf2009-04-21 05:40:52 +0000279 // darwin_constant_cfstrings controls this. This is also dependent
280 // on other things like the runtime I believe. This is set even for C code.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000281 Builder.defineMacro("__CONSTANT_CFSTRINGS__");
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattnere116ccf2009-04-21 05:40:52 +0000283 if (LangOpts.ObjC2)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000284 Builder.defineMacro("OBJC_NEW_PROPERTIES");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000285
286 if (LangOpts.PascalStrings)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000287 Builder.defineMacro("__PASCAL_STRINGS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000288
289 if (LangOpts.Blocks) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000290 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
291 Builder.defineMacro("__BLOCKS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Rafael Espindolaf759df02009-10-01 13:33:33 +0000294 if (LangOpts.Exceptions)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000295 Builder.defineMacro("__EXCEPTIONS");
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000296 if (LangOpts.SjLjExceptions)
297 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
Rafael Espindolaf759df02009-10-01 13:33:33 +0000298
Chris Lattnere116ccf2009-04-21 05:40:52 +0000299 if (LangOpts.CPlusPlus) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000300 Builder.defineMacro("__DEPRECATED");
301 Builder.defineMacro("__GNUG__", "4");
302 Builder.defineMacro("__GXX_WEAK__");
Douglas Gregor36dd1312009-08-06 04:09:28 +0000303 if (LangOpts.GNUMode)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000304 Builder.defineMacro("__cplusplus");
Mike Stump1eb44332009-09-09 15:08:12 +0000305 else
Douglas Gregor36dd1312009-08-06 04:09:28 +0000306 // C++ [cpp.predefined]p1:
Mike Stump1eb44332009-09-09 15:08:12 +0000307 // The name_ _cplusplusis defined to the value199711Lwhen compiling a
Douglas Gregor36dd1312009-08-06 04:09:28 +0000308 // C++ translation unit.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000309 Builder.defineMacro("__cplusplus", "199711L");
310 Builder.defineMacro("__private_extern__", "extern");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000311 }
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Chris Lattnere116ccf2009-04-21 05:40:52 +0000313 if (LangOpts.Microsoft) {
John Thompson24ee8042009-10-16 01:12:00 +0000314 // Filter out some microsoft extensions when trying to parse in ms-compat
315 // mode.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000316 Builder.defineMacro("__int8", "__INT8_TYPE__");
317 Builder.defineMacro("__int16", "__INT16_TYPE__");
318 Builder.defineMacro("__int32", "__INT32_TYPE__");
319 Builder.defineMacro("__int64", "__INT64_TYPE__");
Steve Narofffdd6aaf2009-12-04 21:29:41 +0000320 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
321 // VC++ appears to only like __FUNCTION__.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000322 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
John Thompson24ee8042009-10-16 01:12:00 +0000323 // Work around some issues with Visual C++ headerws.
324 if (LangOpts.CPlusPlus) {
325 // Since we define wchar_t in C++ mode.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000326 Builder.defineMacro("_WCHAR_T_DEFINED");
327 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
John Thompson24ee8042009-10-16 01:12:00 +0000328 // FIXME: This should be temporary until we have a __pragma
329 // solution, to avoid some errors flagged in VC++ headers.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000330 Builder.defineMacro("_CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES", "0");
John Thompson24ee8042009-10-16 01:12:00 +0000331 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattnere116ccf2009-04-21 05:40:52 +0000334 if (LangOpts.Optimize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000335 Builder.defineMacro("__OPTIMIZE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000336 if (LangOpts.OptimizeSize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000337 Builder.defineMacro("__OPTIMIZE_SIZE__");
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chris Lattnere116ccf2009-04-21 05:40:52 +0000339 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattnere116ccf2009-04-21 05:40:52 +0000341 // Define type sizing macros based on the target properties.
342 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000343 Builder.defineMacro("__CHAR_BIT__", "8");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000344
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000345 DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Builder);
346 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
347 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
348 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
349 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
350 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
351 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000352
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000353 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
354 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
355 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
356 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
357 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
358 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
359 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
360 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
361 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
362 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
363 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
364 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
365 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
366 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000368 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat());
369 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat());
370 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000371
372 // Define a __POINTER_WIDTH__ macro for stdint.h.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000373 Builder.defineMacro("__POINTER_WIDTH__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000374 llvm::Twine((int)TI.getPointerWidth(0)));
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Eli Friedman15b91762009-06-05 07:05:05 +0000376 if (!LangOpts.CharIsSigned)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000377 Builder.defineMacro("__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000378
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000379 // Define exact-width integer types for stdint.h
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000380 Builder.defineMacro("__INT" + llvm::Twine(TI.getCharWidth()) + "_TYPE__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000381 "char");
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000383 if (TI.getShortWidth() > TI.getCharWidth())
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000384 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000386 if (TI.getIntWidth() > TI.getShortWidth())
387 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
388
389 if (TI.getLongWidth() > TI.getIntWidth())
390 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
391
392 if (TI.getLongLongWidth() > TI.getLongWidth())
393 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
394
395 // Add __builtin_va_list typedef.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000396 Builder.append(TI.getVAListDeclaration());
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000397
398 if (const char *Prefix = TI.getUserLabelPrefix())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000399 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnere116ccf2009-04-21 05:40:52 +0000401 // Build configuration options. FIXME: these should be controlled by
402 // command line options or something.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000403 Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000404
Chris Lattnere116ccf2009-04-21 05:40:52 +0000405 if (LangOpts.GNUInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000406 Builder.defineMacro("__GNUC_GNU_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000407 else
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000408 Builder.defineMacro("__GNUC_STDC_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000409
410 if (LangOpts.NoInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000411 Builder.defineMacro("__NO_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000412
413 if (unsigned PICLevel = LangOpts.PICLevel) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000414 Builder.defineMacro("__PIC__", llvm::Twine(PICLevel));
415 Builder.defineMacro("__pic__", llvm::Twine(PICLevel));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000416 }
417
418 // Macros to control C99 numerics and <float.h>
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000419 Builder.defineMacro("__FLT_EVAL_METHOD__", "0");
420 Builder.defineMacro("__FLT_RADIX__", "2");
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000421 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000422 Builder.defineMacro("__DECIMAL_DIG__", llvm::Twine(Dig));
Bill Wendling45483f72009-06-28 07:36:13 +0000423
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000424 if (LangOpts.getStackProtectorMode() == LangOptions::SSPOn)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000425 Builder.defineMacro("__SSP__");
Bill Wendling4ebe3e42009-06-28 23:01:01 +0000426 else if (LangOpts.getStackProtectorMode() == LangOptions::SSPReq)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000427 Builder.defineMacro("__SSP_ALL__", "2");
Bill Wendling45483f72009-06-28 07:36:13 +0000428
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000429 if (FEOpts.ProgramAction == frontend::RewriteObjC)
430 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
Ted Kremenekf0af7772010-05-26 21:36:54 +0000431
432 // Define a macro that exists only when using the static analyzer.
433 if (FEOpts.ProgramAction == frontend::RunAnalysis)
434 Builder.defineMacro("__clang_analyzer__");
435
Chris Lattnere116ccf2009-04-21 05:40:52 +0000436 // Get other target #defines.
Benjamin Kramera9992772010-01-09 17:55:51 +0000437 TI.getTargetDefines(LangOpts, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000438}
439
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000440// Initialize the remapping of files to alternative contents, e.g.,
441// those specified through other files.
442static void InitializeFileRemapping(Diagnostic &Diags,
443 SourceManager &SourceMgr,
444 FileManager &FileMgr,
445 const PreprocessorOptions &InitOpts) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000446 // Remap files in the source manager (with buffers).
447 for (PreprocessorOptions::remapped_file_buffer_iterator
448 Remap = InitOpts.remapped_file_buffer_begin(),
449 RemapEnd = InitOpts.remapped_file_buffer_end();
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000450 Remap != RemapEnd;
451 ++Remap) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000452 // Create the file entry for the file that we're mapping from.
453 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000454 Remap->second->getBufferSize(),
Douglas Gregor057e5672009-12-02 18:12:28 +0000455 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000456 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000457 Diags.Report(diag::err_fe_remap_missing_from_file)
458 << Remap->first;
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000459 delete Remap->second;
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000460 continue;
461 }
462
Douglas Gregor4db64a42010-01-23 00:14:00 +0000463 // Override the contents of the "from" file with the contents of
464 // the "to" file.
465 SourceMgr.overrideFileContents(FromFile, Remap->second);
466 }
467
468 // Remap files in the source manager (with other files).
469 for (PreprocessorOptions::remapped_file_iterator
470 Remap = InitOpts.remapped_file_begin(),
471 RemapEnd = InitOpts.remapped_file_end();
472 Remap != RemapEnd;
473 ++Remap) {
474 // Find the file that we're mapping to.
475 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
476 if (!ToFile) {
477 Diags.Report(diag::err_fe_remap_missing_to_file)
478 << Remap->first << Remap->second;
479 continue;
480 }
481
482 // Create the file entry for the file that we're mapping from.
483 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
484 ToFile->getSize(),
485 0);
486 if (!FromFile) {
487 Diags.Report(diag::err_fe_remap_missing_from_file)
488 << Remap->first;
489 continue;
490 }
491
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000492 // Load the contents of the file we're mapping to.
493 std::string ErrorStr;
494 const llvm::MemoryBuffer *Buffer
Douglas Gregor4db64a42010-01-23 00:14:00 +0000495 = llvm::MemoryBuffer::getFile(ToFile->getName(), &ErrorStr);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000496 if (!Buffer) {
497 Diags.Report(diag::err_fe_error_opening)
Douglas Gregorc8dfe5e2010-02-27 01:32:48 +0000498 << Remap->second << ErrorStr;
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000499 continue;
500 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000501
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000502 // Override the contents of the "from" file with the contents of
503 // the "to" file.
504 SourceMgr.overrideFileContents(FromFile, Buffer);
505 }
506}
507
Chris Lattnere116ccf2009-04-21 05:40:52 +0000508/// InitializePreprocessor - Initialize the preprocessor getting it and the
509/// environment ready to process a single file. This returns true on error.
510///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000511void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000512 const PreprocessorOptions &InitOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000513 const HeaderSearchOptions &HSOpts,
514 const FrontendOptions &FEOpts) {
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000515 std::string PredefineBuffer;
516 PredefineBuffer.reserve(4080);
517 llvm::raw_string_ostream Predefines(PredefineBuffer);
518 MacroBuilder Builder(Predefines);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000520 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
521 PP.getFileManager(), InitOpts);
522
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000523 // Emit line markers for various builtin sections of the file. We don't do
524 // this in asm preprocessor mode, because "# 4" is not a line marker directive
525 // in this mode.
526 if (!PP.getLangOptions().AsmPreprocessor)
527 Builder.append("# 1 \"<built-in>\" 3");
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattnere116ccf2009-04-21 05:40:52 +0000529 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000530 if (InitOpts.UsePredefines)
Chris Lattnere6113de2009-11-03 19:50:27 +0000531 InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(),
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000532 FEOpts, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Chris Lattnere116ccf2009-04-21 05:40:52 +0000534 // Add on the predefines from the driver. Wrap in a #line directive to report
535 // that they come from the command line.
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000536 if (!PP.getLangOptions().AsmPreprocessor)
537 Builder.append("# 1 \"<command line>\" 1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000538
539 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000540 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
541 if (InitOpts.Macros[i].second) // isUndef
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000542 Builder.undefineMacro(InitOpts.Macros[i].first);
Chris Lattner62f86c42009-04-21 06:00:24 +0000543 else
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000544 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +0000545 PP.getDiagnostics());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000546 }
547
548 // If -imacros are specified, include them now. These are processed before
549 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000550 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000551 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i]);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000552
553 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000554 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
555 const std::string &Path = InitOpts.Includes[i];
556 if (Path == InitOpts.ImplicitPTHInclude)
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000557 AddImplicitIncludePTH(Builder, PP, Path);
Chris Lattner62f86c42009-04-21 06:00:24 +0000558 else
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000559 AddImplicitInclude(Builder, Path);
Eli Friedmanae96a962009-06-15 09:57:52 +0000560 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000561
Rafael Espindola0259d202009-12-01 18:28:16 +0000562 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000563 if (!PP.getLangOptions().AsmPreprocessor)
564 Builder.append("# 1 \"<built-in>\" 2");
Rafael Espindola0259d202009-12-01 18:28:16 +0000565
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000566 // Copy PredefinedBuffer into the Preprocessor.
567 PP.setPredefines(Predefines.str());
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000568
569 // Initialize the header search object.
570 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
571 PP.getLangOptions(),
572 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000573}