blob: c7d25509e595e1caca2729a039355a38ab1db099 [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
Daniel Dunbar8863b982009-11-07 04:20:15 +000014#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Basic/FileManager.h"
Chandler Carruth103b71c2010-01-20 06:13:02 +000016#include "clang/Basic/MacroBuilder.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/SourceManager.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000018#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Basic/Version.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000020#include "clang/Frontend/FrontendDiagnostic.h"
Fariborz Jahanian7d957472010-01-13 18:51:17 +000021#include "clang/Frontend/FrontendOptions.h"
Chandler Carruthcb381ea2011-12-09 01:33:57 +000022#include "clang/Lex/HeaderSearch.h"
Chris Lattnere116ccf2009-04-21 05:40:52 +000023#include "clang/Lex/Preprocessor.h"
Douglas Gregor14e71f02012-10-24 17:01:35 +000024#include "clang/Lex/PreprocessorOptions.h"
Douglas Gregor4ec429d2012-10-22 23:59:45 +000025#include "clang/Serialization/ASTReader.h"
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000026#include "llvm/ADT/APFloat.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000027#include "llvm/Support/FileSystem.h"
Douglas Gregor2973c0e2009-12-02 16:32:41 +000028#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000029#include "llvm/Support/Path.h"
Chris Lattner47c06ee2009-11-02 21:48:09 +000030using namespace clang;
Chris Lattnere116ccf2009-04-21 05:40:52 +000031
Eli Friedman62c90e12013-08-28 20:35:38 +000032static bool MacroBodyEndsInBackslash(StringRef MacroBody) {
33 while (!MacroBody.empty() && isWhitespace(MacroBody.back()))
34 MacroBody = MacroBody.drop_back();
35 return !MacroBody.empty() && MacroBody.back() == '\\';
36}
37
Chris Lattnere116ccf2009-04-21 05:40:52 +000038// Append a #define line to Buf for Macro. Macro should be of the form XXX,
39// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
40// "#define XXX Y z W". To get a #define with no value, use "XXX=".
Chris Lattner5f9e2722011-07-23 10:55:15 +000041static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro,
David Blaikied6471f72011-09-25 23:23:43 +000042 DiagnosticsEngine &Diags) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000043 std::pair<StringRef, StringRef> MacroPair = Macro.split('=');
44 StringRef MacroName = MacroPair.first;
45 StringRef MacroBody = MacroPair.second;
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000046 if (MacroName.size() != Macro.size()) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000047 // Per GCC -D semantics, the macro ends at \n if it exists.
Chris Lattner5f9e2722011-07-23 10:55:15 +000048 StringRef::size_type End = MacroBody.find_first_of("\n\r");
49 if (End != StringRef::npos)
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +000050 Diags.Report(diag::warn_fe_macro_contains_embedded_newline)
Kovarththanan Rajaratnam8746e4e2010-01-09 09:27:11 +000051 << MacroName;
Eli Friedman62c90e12013-08-28 20:35:38 +000052 MacroBody = MacroBody.substr(0, End);
53 // We handle macro bodies which end in a backslash by appending an extra
54 // backslash+newline. This makes sure we don't accidentally treat the
55 // backslash as a line continuation marker.
56 if (MacroBodyEndsInBackslash(MacroBody))
57 Builder.defineMacro(MacroName, Twine(MacroBody) + "\\\n");
58 else
59 Builder.defineMacro(MacroName, MacroBody);
Chris Lattnere116ccf2009-04-21 05:40:52 +000060 } else {
61 // Push "macroname 1".
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000062 Builder.defineMacro(Macro);
Chris Lattnere116ccf2009-04-21 05:40:52 +000063 }
Chris Lattnerdcdecf42009-05-15 16:08:43 +000064}
65
James Dennett809d1be2012-06-13 22:07:09 +000066/// AddImplicitInclude - Add an implicit \#include of the specified file to the
Chris Lattnere116ccf2009-04-21 05:40:52 +000067/// predefines buffer.
Chris Lattner5f9e2722011-07-23 10:55:15 +000068static void AddImplicitInclude(MacroBuilder &Builder, StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000069 FileManager &FileMgr) {
Chandler Carruthcb381ea2011-12-09 01:33:57 +000070 Builder.append(Twine("#include \"") +
71 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000072}
73
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000074static void AddImplicitIncludeMacros(MacroBuilder &Builder,
Chris Lattner5f9e2722011-07-23 10:55:15 +000075 StringRef File,
Nick Lewycky277a6e72011-02-23 21:16:44 +000076 FileManager &FileMgr) {
Chandler Carruthcb381ea2011-12-09 01:33:57 +000077 Builder.append(Twine("#__include_macros \"") +
78 HeaderSearch::NormalizeDashIncludePath(File, FileMgr) + "\"");
Chris Lattnere116ccf2009-04-21 05:40:52 +000079 // Marker token to stop the __include_macros fetch loop.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +000080 Builder.append("##"); // ##?
Chris Lattnere116ccf2009-04-21 05:40:52 +000081}
82
James Dennett809d1be2012-06-13 22:07:09 +000083/// AddImplicitIncludePTH - Add an implicit \#include using the original file
84/// used to generate a PTH cache.
Benjamin Kramera3d8ced2010-01-09 16:17:37 +000085static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP,
Chris Lattner5f9e2722011-07-23 10:55:15 +000086 StringRef ImplicitIncludePTH) {
Chris Lattnere116ccf2009-04-21 05:40:52 +000087 PTHManager *P = PP.getPTHManager();
Ted Kremenek4ae4c912010-06-28 20:32:40 +000088 // Null check 'P' in the corner case where it couldn't be created.
89 const char *OriginalFile = P ? P->getOriginalSourceFile() : 0;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnere116ccf2009-04-21 05:40:52 +000091 if (!OriginalFile) {
Daniel Dunbarbaac1032009-12-03 09:14:12 +000092 PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header)
93 << ImplicitIncludePTH;
94 return;
Chris Lattnere116ccf2009-04-21 05:40:52 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Nick Lewycky277a6e72011-02-23 21:16:44 +000097 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +000098}
99
Douglas Gregor4ec429d2012-10-22 23:59:45 +0000100/// \brief Add an implicit \#include using the original file used to generate
101/// a PCH file.
102static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP,
103 StringRef ImplicitIncludePCH) {
104 std::string OriginalFile =
105 ASTReader::getOriginalSourceFile(ImplicitIncludePCH, PP.getFileManager(),
106 PP.getDiagnostics());
107 if (OriginalFile.empty())
108 return;
109
110 AddImplicitInclude(Builder, OriginalFile, PP.getFileManager());
111}
112
Chris Lattnere116ccf2009-04-21 05:40:52 +0000113/// PickFP - This is used to pick a value based on the FP semantics of the
114/// specified FP model.
115template <typename T>
116static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
Eli Friedman2665a752009-05-23 03:50:01 +0000117 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal,
118 T IEEEQuadVal) {
Duncan Sands63682f92009-06-03 14:28:20 +0000119 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000120 return IEEESingleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000121 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000122 return IEEEDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000123 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended)
Chris Lattnere116ccf2009-04-21 05:40:52 +0000124 return X87DoubleExtendedVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000125 if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble)
Eli Friedman2665a752009-05-23 03:50:01 +0000126 return PPCDoubleDoubleVal;
Duncan Sands63682f92009-06-03 14:28:20 +0000127 assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad);
Eli Friedman2665a752009-05-23 03:50:01 +0000128 return IEEEQuadVal;
Chris Lattnere116ccf2009-04-21 05:40:52 +0000129}
130
Chris Lattner5f9e2722011-07-23 10:55:15 +0000131static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix,
Eli Friedman75e8efe2012-11-10 00:20:38 +0000132 const llvm::fltSemantics *Sem, StringRef Ext) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000133 const char *DenormMin, *Epsilon, *Max, *Min;
Eli Friedman75e8efe2012-11-10 00:20:38 +0000134 DenormMin = PickFP(Sem, "1.40129846e-45", "4.9406564584124654e-324",
135 "3.64519953188247460253e-4951",
136 "4.94065645841246544176568792868221e-324",
137 "6.47517511943802511092443895822764655e-4966");
Eli Friedman2665a752009-05-23 03:50:01 +0000138 int Digits = PickFP(Sem, 6, 15, 18, 31, 33);
Eli Friedman75e8efe2012-11-10 00:20:38 +0000139 Epsilon = PickFP(Sem, "1.19209290e-7", "2.2204460492503131e-16",
140 "1.08420217248550443401e-19",
141 "4.94065645841246544176568792868221e-324",
142 "1.92592994438723585305597794258492732e-34");
Eli Friedman2665a752009-05-23 03:50:01 +0000143 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106, 113);
144 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291, -4931);
145 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308, 4932);
146 int MinExp = PickFP(Sem, -125, -1021, -16381, -968, -16381);
147 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024, 16384);
Eli Friedman75e8efe2012-11-10 00:20:38 +0000148 Min = PickFP(Sem, "1.17549435e-38", "2.2250738585072014e-308",
149 "3.36210314311209350626e-4932",
150 "2.00416836000897277799610805135016e-292",
151 "3.36210314311209350626267781732175260e-4932");
152 Max = PickFP(Sem, "3.40282347e+38", "1.7976931348623157e+308",
153 "1.18973149535723176502e+4932",
154 "1.79769313486231580793728971405301e+308",
155 "1.18973149535723176508575932662800702e+4932");
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000157 SmallString<32> DefPrefix;
Daniel Dunbar066515f2010-01-20 06:09:53 +0000158 DefPrefix = "__";
159 DefPrefix += Prefix;
160 DefPrefix += "_";
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000161
Eli Friedman75e8efe2012-11-10 00:20:38 +0000162 Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext);
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000163 Builder.defineMacro(DefPrefix + "HAS_DENORM__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits));
Eli Friedman75e8efe2012-11-10 00:20:38 +0000165 Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext);
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000166 Builder.defineMacro(DefPrefix + "HAS_INFINITY__");
167 Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__");
Chris Lattner5f9e2722011-07-23 10:55:15 +0000168 Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000169
Chris Lattner5f9e2722011-07-23 10:55:15 +0000170 Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp));
171 Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp));
Eli Friedman75e8efe2012-11-10 00:20:38 +0000172 Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext);
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000173
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")");
175 Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")");
Eli Friedman75e8efe2012-11-10 00:20:38 +0000176 Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000177}
178
179
180/// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro
181/// named MacroName with the max value for a type with width 'TypeWidth' a
182/// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL).
Chris Lattner5f9e2722011-07-23 10:55:15 +0000183static void DefineTypeSize(StringRef MacroName, unsigned TypeWidth,
184 StringRef ValSuffix, bool isSigned,
Chris Lattnerd7f758c2011-02-24 06:54:56 +0000185 MacroBuilder &Builder) {
186 llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(TypeWidth)
187 : llvm::APInt::getMaxValue(TypeWidth);
188 Builder.defineMacro(MacroName, MaxVal.toString(10, isSigned) + ValSuffix);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000189}
190
Chris Lattner9099e7b2009-11-05 21:21:32 +0000191/// DefineTypeSize - An overloaded helper that uses TargetInfo to determine
192/// the width, suffix, and signedness of the given type
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193static void DefineTypeSize(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000194 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner9099e7b2009-11-05 21:21:32 +0000195 DefineTypeSize(MacroName, TI.getTypeWidth(Ty), TI.getTypeConstantSuffix(Ty),
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000196 TI.isTypeSigned(Ty), Builder);
Chris Lattner9099e7b2009-11-05 21:21:32 +0000197}
198
Chris Lattner5f9e2722011-07-23 10:55:15 +0000199static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000200 MacroBuilder &Builder) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000201 Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000202}
203
Chris Lattner5f9e2722011-07-23 10:55:15 +0000204static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000205 const TargetInfo &TI, MacroBuilder &Builder) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000206 Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty)));
Ken Dyck186696b2009-11-18 13:52:57 +0000207}
208
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
Dan Gohmancfeac342010-05-28 00:27:15 +0000210 const TargetInfo &TI, MacroBuilder &Builder) {
211 Builder.defineMacro(MacroName,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000212 Twine(BitWidth / TI.getCharWidth()));
Dan Gohmancfeac342010-05-28 00:27:15 +0000213}
214
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000215static void DefineExactWidthIntType(TargetInfo::IntType Ty,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000216 const TargetInfo &TI, MacroBuilder &Builder) {
Ken Dyckeef22ef2009-11-16 16:36:33 +0000217 int TypeWidth = TI.getTypeWidth(Ty);
Daniel Dunbar7f3a5452010-06-30 06:30:56 +0000218
219 // Use the target specified int64 type, when appropriate, so that [u]int64_t
220 // ends up being defined in terms of the correct type.
221 if (TypeWidth == 64)
222 Ty = TI.getInt64Type();
223
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 DefineType("__INT" + Twine(TypeWidth) + "_TYPE__", Ty, Builder);
Ken Dyckeef22ef2009-11-16 16:36:33 +0000225
Chris Lattner5f9e2722011-07-23 10:55:15 +0000226 StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty));
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000227 if (!ConstSuffix.empty())
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228 Builder.defineMacro("__INT" + Twine(TypeWidth) + "_C_SUFFIX__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000229 ConstSuffix);
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000230}
Chris Lattnere116ccf2009-04-21 05:40:52 +0000231
Richard Smith2c39d712012-04-13 00:45:38 +0000232/// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with
233/// the specified properties.
234static const char *getLockFreeValue(unsigned TypeWidth, unsigned TypeAlign,
235 unsigned InlineWidth) {
236 // Fully-aligned, power-of-2 sizes no larger than the inline
237 // width will be inlined as lock-free operations.
238 if (TypeWidth == TypeAlign && (TypeWidth & (TypeWidth - 1)) == 0 &&
239 TypeWidth <= InlineWidth)
240 return "2"; // "always lock free"
241 // We cannot be certain what operations the lib calls might be
242 // able to implement as lock-free on future processors.
243 return "1"; // "sometimes lock free"
244}
245
John McCallf85e1932011-06-15 23:02:42 +0000246/// \brief Add definitions required for a smooth interaction between
John McCallf85e1932011-06-15 23:02:42 +0000247/// Objective-C++ automated reference counting and libstdc++ (4.2).
248static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts,
249 MacroBuilder &Builder) {
250 Builder.defineMacro("_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR");
251
252 std::string Result;
253 {
254 // Provide specializations for the __is_scalar type trait so that
255 // lifetime-qualified objects are not considered "scalar" types, which
256 // libstdc++ uses as an indicator of the presence of trivial copy, assign,
257 // default-construct, and destruct semantics (none of which hold for
258 // lifetime-qualified objects in ARC).
259 llvm::raw_string_ostream Out(Result);
260
261 Out << "namespace std {\n"
262 << "\n"
263 << "struct __true_type;\n"
264 << "struct __false_type;\n"
265 << "\n";
266
267 Out << "template<typename _Tp> struct __is_scalar;\n"
268 << "\n";
269
270 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000271 << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000272 << " enum { __value = 0 };\n"
273 << " typedef __false_type __type;\n"
274 << "};\n"
275 << "\n";
276
John McCall0a7dd782012-08-21 02:47:43 +0000277 if (LangOpts.ObjCARCWeak) {
John McCallf85e1932011-06-15 23:02:42 +0000278 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000279 << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n"
John McCallf85e1932011-06-15 23:02:42 +0000280 << " enum { __value = 0 };\n"
281 << " typedef __false_type __type;\n"
282 << "};\n"
283 << "\n";
284 }
285
286 Out << "template<typename _Tp>\n"
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000287 << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))"
John McCallf85e1932011-06-15 23:02:42 +0000288 << " _Tp> {\n"
289 << " enum { __value = 0 };\n"
290 << " typedef __false_type __type;\n"
291 << "};\n"
292 << "\n";
293
294 Out << "}\n";
295 }
296 Builder.append(Result);
297}
298
Nick Lewyckya9c64412011-06-07 06:07:12 +0000299static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
300 const LangOptions &LangOpts,
301 const FrontendOptions &FEOpts,
302 MacroBuilder &Builder) {
Eli Friedman85037d92011-10-19 23:46:05 +0000303 if (!LangOpts.MicrosoftMode && !LangOpts.TraditionalCPP)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000304 Builder.defineMacro("__STDC__");
305 if (LangOpts.Freestanding)
306 Builder.defineMacro("__STDC_HOSTED__", "0");
307 else
308 Builder.defineMacro("__STDC_HOSTED__");
309
310 if (!LangOpts.CPlusPlus) {
Benjamin Kramerffbe9b92011-12-23 17:00:35 +0000311 if (LangOpts.C11)
Benjamin Kramer430cf512011-12-23 17:00:41 +0000312 Builder.defineMacro("__STDC_VERSION__", "201112L");
Douglas Gregor5aa6dea2011-10-28 23:02:54 +0000313 else if (LangOpts.C99)
Nick Lewyckya9c64412011-06-07 06:07:12 +0000314 Builder.defineMacro("__STDC_VERSION__", "199901L");
315 else if (!LangOpts.GNUMode && LangOpts.Digraphs)
316 Builder.defineMacro("__STDC_VERSION__", "199409L");
317 } else {
Richard Smith7f0ffb32013-05-07 19:32:56 +0000318 // FIXME: Use the right value for __cplusplus for C++1y once one is chosen.
319 if (LangOpts.CPlusPlus1y)
320 Builder.defineMacro("__cplusplus", "201305L");
Richard Smith05134482012-05-03 22:18:20 +0000321 // C++11 [cpp.predefined]p1:
322 // The name __cplusplus is defined to the value 201103L when compiling a
323 // C++ translation unit.
Richard Smith7f0ffb32013-05-07 19:32:56 +0000324 else if (LangOpts.CPlusPlus11)
Richard Smith05134482012-05-03 22:18:20 +0000325 Builder.defineMacro("__cplusplus", "201103L");
326 // C++03 [cpp.predefined]p1:
327 // The name __cplusplus is defined to the value 199711L when compiling a
328 // C++ translation unit.
329 else
330 Builder.defineMacro("__cplusplus", "199711L");
Nick Lewyckya9c64412011-06-07 06:07:12 +0000331 }
332
Ed Schouten5ada7a52013-09-29 07:54:52 +0000333 // In C11 these are environment macros. In C++11 they are only defined
334 // as part of <cuchar>. To prevent breakage when mixing C and C++
335 // code, define these macros unconditionally. We can define them
336 // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit
337 // and 32-bit character literals.
338 Builder.defineMacro("__STDC_UTF_16__", "1");
339 Builder.defineMacro("__STDC_UTF_32__", "1");
340
Nick Lewycky14648092011-06-10 20:56:43 +0000341 if (LangOpts.ObjC1)
342 Builder.defineMacro("__OBJC__");
343
Nick Lewyckya9c64412011-06-07 06:07:12 +0000344 // Not "standard" per se, but available even with the -undef flag.
345 if (LangOpts.AsmPreprocessor)
346 Builder.defineMacro("__ASSEMBLER__");
347}
348
Bill Wendlingc78e5b32013-11-28 00:33:42 +0000349/// Initialize the predefined C++ language feature test macros defined in
350/// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations".
351static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts,
352 MacroBuilder &Builder) {
353 // C++11 features.
354 if (LangOpts.CPlusPlus11) {
355 Builder.defineMacro("__cpp_unicode_characters", "200704");
356 Builder.defineMacro("__cpp_raw_strings", "200710");
357 Builder.defineMacro("__cpp_unicode_literals", "200710");
358 Builder.defineMacro("__cpp_user_defined_literals", "200809");
359 Builder.defineMacro("__cpp_lambdas", "200907");
360 Builder.defineMacro("__cpp_constexpr",
361 LangOpts.CPlusPlus1y ? "201304" : "200704");
362 Builder.defineMacro("__cpp_static_assert", "200410");
363 Builder.defineMacro("__cpp_decltype", "200707");
364 Builder.defineMacro("__cpp_attributes", "200809");
365 Builder.defineMacro("__cpp_rvalue_references", "200610");
366 Builder.defineMacro("__cpp_variadic_templates", "200704");
367 }
368
369 // C++14 features.
370 if (LangOpts.CPlusPlus1y) {
371 Builder.defineMacro("__cpp_binary_literals", "201304");
372 Builder.defineMacro("__cpp_init_captures", "201304");
373 Builder.defineMacro("__cpp_generic_lambdas", "201304");
374 Builder.defineMacro("__cpp_decltype_auto", "201304");
375 Builder.defineMacro("__cpp_return_type_deduction", "201304");
376 Builder.defineMacro("__cpp_aggregate_nsdmi", "201304");
377 Builder.defineMacro("__cpp_variable_templates", "201304");
378 }
379}
380
Chris Lattnere116ccf2009-04-21 05:40:52 +0000381static void InitializePredefinedMacros(const TargetInfo &TI,
382 const LangOptions &LangOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000383 const FrontendOptions &FEOpts,
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000384 MacroBuilder &Builder) {
Chris Lattnere116ccf2009-04-21 05:40:52 +0000385 // Compiler version introspection macros.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000386 Builder.defineMacro("__llvm__"); // LLVM Backend
387 Builder.defineMacro("__clang__"); // Clang Frontend
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000388#define TOSTR2(X) #X
389#define TOSTR(X) TOSTR2(X)
390 Builder.defineMacro("__clang_major__", TOSTR(CLANG_VERSION_MAJOR));
391 Builder.defineMacro("__clang_minor__", TOSTR(CLANG_VERSION_MINOR));
392#ifdef CLANG_VERSION_PATCHLEVEL
393 Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL));
394#else
395 Builder.defineMacro("__clang_patchlevel__", "0");
396#endif
397 Builder.defineMacro("__clang_version__",
Benjamin Kramerd7ee1942012-10-08 18:49:39 +0000398 "\"" CLANG_VERSION_STRING " "
399 + getClangFullRepositoryVersion() + "\"");
Douglas Gregor4290fbd2010-04-30 02:51:06 +0000400#undef TOSTR
401#undef TOSTR2
Aaron Ballman5b31d552012-03-10 22:21:14 +0000402 if (!LangOpts.MicrosoftMode) {
403 // Currently claim to be compatible with GCC 4.2.1-5621, but only if we're
404 // not compiling for MSVC compatibility
405 Builder.defineMacro("__GNUC_MINOR__", "2");
406 Builder.defineMacro("__GNUC_PATCHLEVEL__", "1");
407 Builder.defineMacro("__GNUC__", "4");
408 Builder.defineMacro("__GXX_ABI_VERSION", "1002");
409 }
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000410
David Chisnall7a7ee302012-01-16 17:27:18 +0000411 // Define macros for the C11 / C++11 memory orderings
412 Builder.defineMacro("__ATOMIC_RELAXED", "0");
413 Builder.defineMacro("__ATOMIC_CONSUME", "1");
414 Builder.defineMacro("__ATOMIC_ACQUIRE", "2");
415 Builder.defineMacro("__ATOMIC_RELEASE", "3");
416 Builder.defineMacro("__ATOMIC_ACQ_REL", "4");
417 Builder.defineMacro("__ATOMIC_SEQ_CST", "5");
418
David Chisnall5f3c1632012-02-18 16:12:34 +0000419 // Support for #pragma redefine_extname (Sun compatibility)
420 Builder.defineMacro("__PRAGMA_REDEFINE_EXTNAME", "1");
421
Daniel Dunbarad1a4c62011-03-31 00:53:51 +0000422 // As sad as it is, enough software depends on the __VERSION__ for version
423 // checks that it is necessary to report 4.2.1 (the base GCC version we claim
424 // compatibility with) first.
425 Builder.defineMacro("__VERSION__", "\"4.2.1 Compatible " +
Chris Lattner5f9e2722011-07-23 10:55:15 +0000426 Twine(getClangFullCPPVersion()) + "\"");
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Chris Lattnere116ccf2009-04-21 05:40:52 +0000428 // Initialize language-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattnere116ccf2009-04-21 05:40:52 +0000430 // Standard conforming mode?
431 if (!LangOpts.GNUMode)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000432 Builder.defineMacro("__STRICT_ANSI__");
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Richard Smith80ad52f2013-01-02 11:42:31 +0000434 if (LangOpts.CPlusPlus11)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000435 Builder.defineMacro("__GXX_EXPERIMENTAL_CXX0X__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000436
Chris Lattnere116ccf2009-04-21 05:40:52 +0000437 if (LangOpts.ObjC1) {
John McCall260611a2012-06-20 06:18:46 +0000438 if (LangOpts.ObjCRuntime.isNonFragile()) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000439 Builder.defineMacro("__OBJC2__");
Douglas Gregoreced60c2011-09-12 15:17:19 +0000440
441 if (LangOpts.ObjCExceptions)
442 Builder.defineMacro("OBJC_ZEROCOST_EXCEPTIONS");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000443 }
444
Douglas Gregore289d812011-09-13 17:21:33 +0000445 if (LangOpts.getGC() != LangOptions::NonGC)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000446 Builder.defineMacro("__OBJC_GC__");
Mike Stump1eb44332009-09-09 15:08:12 +0000447
John McCall260611a2012-06-20 06:18:46 +0000448 if (LangOpts.ObjCRuntime.isNeXTFamily())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000449 Builder.defineMacro("__NEXT_RUNTIME__");
Ted Kremenek3065cf92012-06-19 00:37:39 +0000450
Benjamin Kramer585c84c2013-09-16 16:31:49 +0000451 if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) {
452 VersionTuple tuple = LangOpts.ObjCRuntime.getVersion();
453
454 unsigned minor = 0;
455 if (tuple.getMinor().hasValue())
456 minor = tuple.getMinor().getValue();
457
458 unsigned subminor = 0;
459 if (tuple.getSubminor().hasValue())
460 subminor = tuple.getSubminor().getValue();
461
462 Builder.defineMacro("__OBJFW_RUNTIME_ABI__",
463 Twine(tuple.getMajor() * 10000 + minor * 100 +
464 subminor));
465 }
466
Ted Kremenek3065cf92012-06-19 00:37:39 +0000467 Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))");
468 Builder.defineMacro("IBOutletCollection(ClassName)",
469 "__attribute__((iboutletcollection(ClassName)))");
470 Builder.defineMacro("IBAction", "void)__attribute__((ibaction)");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Bill Wendlingc78e5b32013-11-28 00:33:42 +0000473 if (LangOpts.CPlusPlus)
474 InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder);
475
Chris Lattnere116ccf2009-04-21 05:40:52 +0000476 // darwin_constant_cfstrings controls this. This is also dependent
477 // on other things like the runtime I believe. This is set even for C code.
Fariborz Jahanian16c3eae2011-07-05 16:00:59 +0000478 if (!LangOpts.NoConstantCFStrings)
479 Builder.defineMacro("__CONSTANT_CFSTRINGS__");
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattnere116ccf2009-04-21 05:40:52 +0000481 if (LangOpts.ObjC2)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000482 Builder.defineMacro("OBJC_NEW_PROPERTIES");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000483
484 if (LangOpts.PascalStrings)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000485 Builder.defineMacro("__PASCAL_STRINGS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000486
487 if (LangOpts.Blocks) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000488 Builder.defineMacro("__block", "__attribute__((__blocks__(byref)))");
489 Builder.defineMacro("__BLOCKS__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregoreced60c2011-09-12 15:17:19 +0000492 if (LangOpts.CXXExceptions)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000493 Builder.defineMacro("__EXCEPTIONS");
Dan Gohmancfeac342010-05-28 00:27:15 +0000494 if (LangOpts.RTTI)
495 Builder.defineMacro("__GXX_RTTI");
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000496 if (LangOpts.SjLjExceptions)
497 Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__");
Rafael Espindolaf759df02009-10-01 13:33:33 +0000498
Chandler Carruthf8c247d2011-04-23 19:48:40 +0000499 if (LangOpts.Deprecated)
500 Builder.defineMacro("__DEPRECATED");
501
Chris Lattnere116ccf2009-04-21 05:40:52 +0000502 if (LangOpts.CPlusPlus) {
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000503 Builder.defineMacro("__GNUG__", "4");
504 Builder.defineMacro("__GXX_WEAK__");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000505 Builder.defineMacro("__private_extern__", "extern");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Francois Pichet62ec1f22011-09-17 17:15:52 +0000508 if (LangOpts.MicrosoftExt) {
Steve Narofffdd6aaf2009-12-04 21:29:41 +0000509 // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
510 // VC++ appears to only like __FUNCTION__.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000511 Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000512 // Work around some issues with Visual C++ headers.
513 if (LangOpts.WChar) {
514 // wchar_t supported as a keyword.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000515 Builder.defineMacro("_WCHAR_T_DEFINED");
516 Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
Abramo Bagnara5b86ffd2012-09-05 17:30:57 +0000517 }
518 if (LangOpts.CPlusPlus) {
Francois Pichet4b7e0482011-05-09 22:32:46 +0000519 // FIXME: Support Microsoft's __identifier extension in the lexer.
Francois Picheta23ae3f2011-05-07 17:47:38 +0000520 Builder.append("#define __identifier(x) x");
Douglas Gregor5c0ca522010-08-30 14:44:26 +0000521 Builder.append("class type_info;");
John Thompson24ee8042009-10-16 01:12:00 +0000522 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Chris Lattnere116ccf2009-04-21 05:40:52 +0000525 if (LangOpts.Optimize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000526 Builder.defineMacro("__OPTIMIZE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000527 if (LangOpts.OptimizeSize)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000528 Builder.defineMacro("__OPTIMIZE_SIZE__");
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chandler Carruthba9186c2012-01-03 02:46:46 +0000530 if (LangOpts.FastMath)
531 Builder.defineMacro("__FAST_MATH__");
532
Chris Lattnere116ccf2009-04-21 05:40:52 +0000533 // Initialize target-specific preprocessor defines.
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Dylan Noblesmith5aeda882012-08-08 16:09:12 +0000535 // __BYTE_ORDER__ was added in GCC 4.6. It's analogous
536 // to the macro __BYTE_ORDER (no trailing underscores)
537 // from glibc's <endian.h> header.
Dylan Noblesmith3b198a92012-07-27 18:34:31 +0000538 // We don't support the PDP-11 as a target, but include
539 // the define so it can still be compared against.
540 Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234");
541 Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321");
542 Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412");
543 if (TI.isBigEndian())
544 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__");
545 else
546 Builder.defineMacro("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__");
547
Dylan Noblesmith6bd32912012-08-10 19:12:37 +0000548
549 if (TI.getPointerWidth(0) == 64 && TI.getLongWidth() == 64
550 && TI.getIntWidth() == 32) {
551 Builder.defineMacro("_LP64");
552 Builder.defineMacro("__LP64__");
553 }
554
Chris Lattnere116ccf2009-04-21 05:40:52 +0000555 // Define type sizing macros based on the target properties.
556 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000557 Builder.defineMacro("__CHAR_BIT__", "8");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000558
Stepan Dyatkovskiy7b7bef12013-09-05 11:23:21 +0000559 DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder);
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000560 DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder);
561 DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder);
562 DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder);
563 DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder);
564 DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder);
565 DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder);
Evgeniy Stepanovedaf2812013-03-28 08:36:54 +0000566 DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000567
Dan Gohmancfeac342010-05-28 00:27:15 +0000568 DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder);
569 DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder);
570 DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder);
571 DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder);
572 DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder);
573 DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder);
574 DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder);
575 DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder);
576 DefineTypeSizeof("__SIZEOF_PTRDIFF_T__",
577 TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder);
578 DefineTypeSizeof("__SIZEOF_SIZE_T__",
579 TI.getTypeWidth(TI.getSizeType()), TI, Builder);
580 DefineTypeSizeof("__SIZEOF_WCHAR_T__",
581 TI.getTypeWidth(TI.getWCharType()), TI, Builder);
582 DefineTypeSizeof("__SIZEOF_WINT_T__",
583 TI.getTypeWidth(TI.getWIntType()), TI, Builder);
Richard Smith84268902012-11-29 05:41:51 +0000584 if (TI.hasInt128Type())
585 DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder);
Dan Gohmancfeac342010-05-28 00:27:15 +0000586
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000587 DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder);
588 DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder);
589 DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder);
590 DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder);
591 DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder);
592 DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
593 DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder);
594 DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
595 DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder);
596 DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder);
597 DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder);
598 DefineType("__WINT_TYPE__", TI.getWIntType(), Builder);
599 DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder);
600 DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder);
Dan Gohmancfeac342010-05-28 00:27:15 +0000601 DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder);
602 DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Eli Friedman75e8efe2012-11-10 00:20:38 +0000604 DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat(), "F");
605 DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat(), "");
606 DefineFloatMacros(Builder, "LDBL", &TI.getLongDoubleFormat(), "L");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000607
608 // Define a __POINTER_WIDTH__ macro for stdint.h.
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000609 Builder.defineMacro("__POINTER_WIDTH__",
Chris Lattner5f9e2722011-07-23 10:55:15 +0000610 Twine((int)TI.getPointerWidth(0)));
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Eli Friedman15b91762009-06-05 07:05:05 +0000612 if (!LangOpts.CharIsSigned)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000613 Builder.defineMacro("__CHAR_UNSIGNED__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000614
James Molloya30d8602012-05-04 11:23:40 +0000615 if (!TargetInfo::isTypeSigned(TI.getWCharType()))
616 Builder.defineMacro("__WCHAR_UNSIGNED__");
617
Eli Friedman1cfeefd2011-04-21 05:45:45 +0000618 if (!TargetInfo::isTypeSigned(TI.getWIntType()))
619 Builder.defineMacro("__WINT_UNSIGNED__");
620
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000621 // Define exact-width integer types for stdint.h
Chris Lattner5f9e2722011-07-23 10:55:15 +0000622 Builder.defineMacro("__INT" + Twine(TI.getCharWidth()) + "_TYPE__",
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000623 "char");
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Chris Lattnerdcdd2a02009-11-12 08:08:27 +0000625 if (TI.getShortWidth() > TI.getCharWidth())
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000626 DefineExactWidthIntType(TargetInfo::SignedShort, TI, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000628 if (TI.getIntWidth() > TI.getShortWidth())
629 DefineExactWidthIntType(TargetInfo::SignedInt, TI, Builder);
630
631 if (TI.getLongWidth() > TI.getIntWidth())
632 DefineExactWidthIntType(TargetInfo::SignedLong, TI, Builder);
633
634 if (TI.getLongLongWidth() > TI.getLongWidth())
635 DefineExactWidthIntType(TargetInfo::SignedLongLong, TI, Builder);
636
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000637 if (const char *Prefix = TI.getUserLabelPrefix())
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000638 Builder.defineMacro("__USER_LABEL_PREFIX__", Prefix);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Bob Wilson455e72e2012-07-19 03:52:53 +0000640 if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
641 Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
642 else
643 Builder.defineMacro("__FINITE_MATH_ONLY__", "0");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000644
Chris Lattnere116ccf2009-04-21 05:40:52 +0000645 if (LangOpts.GNUInline)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000646 Builder.defineMacro("__GNUC_GNU_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000647 else
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000648 Builder.defineMacro("__GNUC_STDC_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000649
Richard Smith2c39d712012-04-13 00:45:38 +0000650 // The value written by __atomic_test_and_set.
651 // FIXME: This is target-dependent.
652 Builder.defineMacro("__GCC_ATOMIC_TEST_AND_SET_TRUEVAL", "1");
653
654 // Used by libstdc++ to implement ATOMIC_<foo>_LOCK_FREE.
655 unsigned InlineWidthBits = TI.getMaxAtomicInlineWidth();
656#define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \
657 Builder.defineMacro("__GCC_ATOMIC_" #TYPE "_LOCK_FREE", \
658 getLockFreeValue(TI.get##Type##Width(), \
659 TI.get##Type##Align(), \
660 InlineWidthBits));
661 DEFINE_LOCK_FREE_MACRO(BOOL, Bool);
662 DEFINE_LOCK_FREE_MACRO(CHAR, Char);
663 DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16);
664 DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32);
665 DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar);
666 DEFINE_LOCK_FREE_MACRO(SHORT, Short);
667 DEFINE_LOCK_FREE_MACRO(INT, Int);
668 DEFINE_LOCK_FREE_MACRO(LONG, Long);
669 DEFINE_LOCK_FREE_MACRO(LLONG, LongLong);
670 Builder.defineMacro("__GCC_ATOMIC_POINTER_LOCK_FREE",
671 getLockFreeValue(TI.getPointerWidth(0),
672 TI.getPointerAlign(0),
673 InlineWidthBits));
674#undef DEFINE_LOCK_FREE_MACRO
675
Daniel Dunbar7b140262012-03-09 15:39:08 +0000676 if (LangOpts.NoInlineDefine)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000677 Builder.defineMacro("__NO_INLINE__");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000678
679 if (unsigned PICLevel = LangOpts.PICLevel) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000680 Builder.defineMacro("__PIC__", Twine(PICLevel));
681 Builder.defineMacro("__pic__", Twine(PICLevel));
Chris Lattnere116ccf2009-04-21 05:40:52 +0000682 }
Chandler Carruth5e219cf2012-04-08 16:40:35 +0000683 if (unsigned PIELevel = LangOpts.PIELevel) {
684 Builder.defineMacro("__PIE__", Twine(PIELevel));
685 Builder.defineMacro("__pie__", Twine(PIELevel));
686 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000687
688 // Macros to control C99 numerics and <float.h>
Benjamin Kramerb4066692011-12-28 15:47:06 +0000689 Builder.defineMacro("__FLT_EVAL_METHOD__", Twine(TI.getFloatEvalMethod()));
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000690 Builder.defineMacro("__FLT_RADIX__", "2");
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000691 int Dig = PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33, 36);
Chris Lattner5f9e2722011-07-23 10:55:15 +0000692 Builder.defineMacro("__DECIMAL_DIG__", Twine(Dig));
Bill Wendling45483f72009-06-28 07:36:13 +0000693
Douglas Gregore289d812011-09-13 17:21:33 +0000694 if (LangOpts.getStackProtector() == LangOptions::SSPOn)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000695 Builder.defineMacro("__SSP__");
Douglas Gregore289d812011-09-13 17:21:33 +0000696 else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000697 Builder.defineMacro("__SSP_ALL__", "2");
Bill Wendling45483f72009-06-28 07:36:13 +0000698
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000699 if (FEOpts.ProgramAction == frontend::RewriteObjC)
700 Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
Ted Kremenekf0af7772010-05-26 21:36:54 +0000701
702 // Define a macro that exists only when using the static analyzer.
703 if (FEOpts.ProgramAction == frontend::RunAnalysis)
704 Builder.defineMacro("__clang_analyzer__");
705
Peter Collingbournef0840822010-12-04 01:51:23 +0000706 if (LangOpts.FastRelaxedMath)
707 Builder.defineMacro("__FAST_RELAXED_MATH__");
708
John McCall098df7f2011-06-16 00:03:19 +0000709 if (LangOpts.ObjCAutoRefCount) {
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000710 Builder.defineMacro("__weak", "__attribute__((objc_ownership(weak)))");
711 Builder.defineMacro("__strong", "__attribute__((objc_ownership(strong)))");
John McCall098df7f2011-06-16 00:03:19 +0000712 Builder.defineMacro("__autoreleasing",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000713 "__attribute__((objc_ownership(autoreleasing)))");
John McCall098df7f2011-06-16 00:03:19 +0000714 Builder.defineMacro("__unsafe_unretained",
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +0000715 "__attribute__((objc_ownership(none)))");
John McCall098df7f2011-06-16 00:03:19 +0000716 }
717
Douglas Gregor50a70cd2013-01-15 06:45:29 +0000718 // OpenMP definition
719 if (LangOpts.OpenMP) {
720 // OpenMP 2.2:
721 // In implementations that support a preprocessor, the _OPENMP
722 // macro name is defined to have the decimal value yyyymm where
723 // yyyy and mm are the year and the month designations of the
724 // version of the OpenMP API that the implementation support.
725 Builder.defineMacro("_OPENMP", "201107");
726 }
727
Chris Lattnere116ccf2009-04-21 05:40:52 +0000728 // Get other target #defines.
Benjamin Kramera9992772010-01-09 17:55:51 +0000729 TI.getTargetDefines(LangOpts, Builder);
Chris Lattnere116ccf2009-04-21 05:40:52 +0000730}
731
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000732// Initialize the remapping of files to alternative contents, e.g.,
733// those specified through other files.
David Blaikied6471f72011-09-25 23:23:43 +0000734static void InitializeFileRemapping(DiagnosticsEngine &Diags,
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000735 SourceManager &SourceMgr,
736 FileManager &FileMgr,
737 const PreprocessorOptions &InitOpts) {
Douglas Gregor4db64a42010-01-23 00:14:00 +0000738 // Remap files in the source manager (with buffers).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000739 for (PreprocessorOptions::const_remapped_file_buffer_iterator
Douglas Gregor4db64a42010-01-23 00:14:00 +0000740 Remap = InitOpts.remapped_file_buffer_begin(),
741 RemapEnd = InitOpts.remapped_file_buffer_end();
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000742 Remap != RemapEnd;
743 ++Remap) {
Douglas Gregor057e5672009-12-02 18:12:28 +0000744 // Create the file entry for the file that we're mapping from.
745 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000746 Remap->second->getBufferSize(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000747 0);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000748 if (!FromFile) {
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000749 Diags.Report(diag::err_fe_remap_missing_from_file)
750 << Remap->first;
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000751 if (!InitOpts.RetainRemappedFileBuffers)
752 delete Remap->second;
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000753 continue;
754 }
755
Douglas Gregor4db64a42010-01-23 00:14:00 +0000756 // Override the contents of the "from" file with the contents of
757 // the "to" file.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000758 SourceMgr.overrideFileContents(FromFile, Remap->second,
759 InitOpts.RetainRemappedFileBuffers);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000760 }
761
762 // Remap files in the source manager (with other files).
Douglas Gregor44c181a2010-07-23 00:33:23 +0000763 for (PreprocessorOptions::const_remapped_file_iterator
764 Remap = InitOpts.remapped_file_begin(),
765 RemapEnd = InitOpts.remapped_file_end();
Douglas Gregor4db64a42010-01-23 00:14:00 +0000766 Remap != RemapEnd;
767 ++Remap) {
768 // Find the file that we're mapping to.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000769 const FileEntry *ToFile = FileMgr.getFile(Remap->second);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000770 if (!ToFile) {
771 Diags.Report(diag::err_fe_remap_missing_to_file)
772 << Remap->first << Remap->second;
773 continue;
774 }
775
776 // Create the file entry for the file that we're mapping from.
777 const FileEntry *FromFile = FileMgr.getVirtualFile(Remap->first,
Chris Lattner39b49bc2010-11-23 08:35:12 +0000778 ToFile->getSize(), 0);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000779 if (!FromFile) {
780 Diags.Report(diag::err_fe_remap_missing_from_file)
781 << Remap->first;
782 continue;
783 }
784
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000785 // Override the contents of the "from" file with the contents of
786 // the "to" file.
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000787 SourceMgr.overrideFileContents(FromFile, ToFile);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000788 }
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000789
790 SourceMgr.setOverridenFilesKeepOriginalName(
791 InitOpts.RemappedFilesKeepOriginalName);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000792}
793
Chris Lattnere116ccf2009-04-21 05:40:52 +0000794/// InitializePreprocessor - Initialize the preprocessor getting it and the
795/// environment ready to process a single file. This returns true on error.
796///
Daniel Dunbar938963f2009-11-04 21:13:15 +0000797void clang::InitializePreprocessor(Preprocessor &PP,
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000798 const PreprocessorOptions &InitOpts,
Fariborz Jahanian7d957472010-01-13 18:51:17 +0000799 const HeaderSearchOptions &HSOpts,
800 const FrontendOptions &FEOpts) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000801 const LangOptions &LangOpts = PP.getLangOpts();
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000802 std::string PredefineBuffer;
803 PredefineBuffer.reserve(4080);
804 llvm::raw_string_ostream Predefines(PredefineBuffer);
805 MacroBuilder Builder(Predefines);
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000807 InitializeFileRemapping(PP.getDiagnostics(), PP.getSourceManager(),
Chris Lattner39b49bc2010-11-23 08:35:12 +0000808 PP.getFileManager(), InitOpts);
Douglas Gregor2973c0e2009-12-02 16:32:41 +0000809
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000810 // Emit line markers for various builtin sections of the file. We don't do
811 // this in asm preprocessor mode, because "# 4" is not a line marker directive
812 // in this mode.
David Blaikie4e4d0842012-03-11 07:00:24 +0000813 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000814 Builder.append("# 1 \"<built-in>\" 3");
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Chris Lattnere116ccf2009-04-21 05:40:52 +0000816 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
John McCallf85e1932011-06-15 23:02:42 +0000817 if (InitOpts.UsePredefines) {
818 InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +0000819
John McCallf85e1932011-06-15 23:02:42 +0000820 // Install definitions to make Objective-C++ ARC work well with various
821 // C++ Standard Library implementations.
822 if (LangOpts.ObjC1 && LangOpts.CPlusPlus && LangOpts.ObjCAutoRefCount) {
823 switch (InitOpts.ObjCXXARCStandardLibrary) {
824 case ARCXX_nolib:
Douglas Gregored73b102011-09-27 22:48:19 +0000825 case ARCXX_libcxx:
John McCallf85e1932011-06-15 23:02:42 +0000826 break;
827
828 case ARCXX_libstdcxx:
829 AddObjCXXARCLibstdcxxDefines(LangOpts, Builder);
830 break;
831 }
832 }
833 }
834
Nick Lewyckya9c64412011-06-07 06:07:12 +0000835 // Even with predefines off, some macros are still predefined.
836 // These should all be defined in the preprocessor according to the
837 // current language configuration.
David Blaikie4e4d0842012-03-11 07:00:24 +0000838 InitializeStandardPredefinedMacros(PP.getTargetInfo(), PP.getLangOpts(),
Nick Lewyckya9c64412011-06-07 06:07:12 +0000839 FEOpts, Builder);
840
Chris Lattnere116ccf2009-04-21 05:40:52 +0000841 // Add on the predefines from the driver. Wrap in a #line directive to report
842 // that they come from the command line.
David Blaikie4e4d0842012-03-11 07:00:24 +0000843 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000844 Builder.append("# 1 \"<command line>\" 1");
Chris Lattnere116ccf2009-04-21 05:40:52 +0000845
846 // Process #define's and #undef's in the order they are given.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000847 for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) {
848 if (InitOpts.Macros[i].second) // isUndef
Benjamin Kramerb1b5b902010-01-09 17:43:21 +0000849 Builder.undefineMacro(InitOpts.Macros[i].first);
Chris Lattner62f86c42009-04-21 06:00:24 +0000850 else
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000851 DefineBuiltinMacro(Builder, InitOpts.Macros[i].first,
Daniel Dunbar1ee4b9e2010-01-10 00:46:21 +0000852 PP.getDiagnostics());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000853 }
854
855 // If -imacros are specified, include them now. These are processed before
856 // any -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000857 for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i)
Nick Lewycky277a6e72011-02-23 21:16:44 +0000858 AddImplicitIncludeMacros(Builder, InitOpts.MacroIncludes[i],
859 PP.getFileManager());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000860
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000861 // Process -include-pch/-include-pth directives.
862 if (!InitOpts.ImplicitPCHInclude.empty())
863 AddImplicitIncludePCH(Builder, PP, InitOpts.ImplicitPCHInclude);
864 if (!InitOpts.ImplicitPTHInclude.empty())
865 AddImplicitIncludePTH(Builder, PP, InitOpts.ImplicitPTHInclude);
866
Chris Lattnere116ccf2009-04-21 05:40:52 +0000867 // Process -include directives.
Daniel Dunbar049d3a02009-11-17 05:52:41 +0000868 for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) {
869 const std::string &Path = InitOpts.Includes[i];
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000870 AddImplicitInclude(Builder, Path, PP.getFileManager());
Eli Friedmanae96a962009-06-15 09:57:52 +0000871 }
Chris Lattnere116ccf2009-04-21 05:40:52 +0000872
Rafael Espindola0259d202009-12-01 18:28:16 +0000873 // Exit the command line and go back to <built-in> (2 is LC_LEAVE).
David Blaikie4e4d0842012-03-11 07:00:24 +0000874 if (!PP.getLangOpts().AsmPreprocessor)
Chris Lattnerfcaa9e72010-04-26 22:08:10 +0000875 Builder.append("# 1 \"<built-in>\" 2");
Rafael Espindola0259d202009-12-01 18:28:16 +0000876
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000877 // Instruct the preprocessor to skip the preamble.
878 PP.setSkipMainFilePreamble(InitOpts.PrecompiledPreambleBytes.first,
879 InitOpts.PrecompiledPreambleBytes.second);
880
Benjamin Kramera3d8ced2010-01-09 16:17:37 +0000881 // Copy PredefinedBuffer into the Preprocessor.
882 PP.setPredefines(Predefines.str());
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000883
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000884 // Initialize the header search object.
885 ApplyHeaderSearchOptions(PP.getHeaderSearchInfo(), HSOpts,
David Blaikie4e4d0842012-03-11 07:00:24 +0000886 PP.getLangOpts(),
Daniel Dunbar961c76e2009-11-11 21:44:42 +0000887 PP.getTargetInfo().getTriple());
Chris Lattnere116ccf2009-04-21 05:40:52 +0000888}