| 1 | //===--- InitPreprocessor.cpp - PP initialization code. ---------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the clang::InitializePreprocessor function. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Basic/DiagnosticLex.h" |
| 14 | #include "clang/Basic/HLSLRuntime.h" |
| 15 | #include "clang/Basic/MacroBuilder.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/SyncScope.h" |
| 18 | #include "clang/Basic/TargetInfo.h" |
| 19 | #include "clang/Basic/Version.h" |
| 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Frontend/FrontendOptions.h" |
| 22 | #include "clang/Frontend/Utils.h" |
| 23 | #include "clang/Lex/HeaderSearch.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
| 25 | #include "clang/Lex/PreprocessorOptions.h" |
| 26 | #include "clang/Serialization/ASTReader.h" |
| 27 | #include "llvm/ADT/APFloat.h" |
| 28 | #include "llvm/IR/DataLayout.h" |
| 29 | #include "llvm/IR/DerivedTypes.h" |
| 30 | using namespace clang; |
| 31 | |
| 32 | static bool MacroBodyEndsInBackslash(StringRef MacroBody) { |
| 33 | while (!MacroBody.empty() && isWhitespace(c: MacroBody.back())) |
| 34 | MacroBody = MacroBody.drop_back(); |
| 35 | return MacroBody.ends_with(Suffix: '\\'); |
| 36 | } |
| 37 | |
| 38 | // 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=". |
| 41 | static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, |
| 42 | DiagnosticsEngine &Diags) { |
| 43 | std::pair<StringRef, StringRef> MacroPair = Macro.split(Separator: '='); |
| 44 | StringRef MacroName = MacroPair.first; |
| 45 | StringRef MacroBody = MacroPair.second; |
| 46 | if (MacroName.size() != Macro.size()) { |
| 47 | // Per GCC -D semantics, the macro ends at \n if it exists. |
| 48 | StringRef::size_type End = MacroBody.find_first_of(Chars: "\n\r" ); |
| 49 | if (End != StringRef::npos) |
| 50 | Diags.Report(DiagID: diag::warn_fe_macro_contains_embedded_newline) |
| 51 | << MacroName; |
| 52 | MacroBody = MacroBody.substr(Start: 0, N: 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(Name: MacroName, Value: Twine(MacroBody) + "\\\n" ); |
| 58 | else |
| 59 | Builder.defineMacro(Name: MacroName, Value: MacroBody); |
| 60 | } else { |
| 61 | // Push "macroname 1". |
| 62 | Builder.defineMacro(Name: Macro); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// AddImplicitInclude - Add an implicit \#include of the specified file to the |
| 67 | /// predefines buffer. |
| 68 | /// As these includes are generated by -include arguments the header search |
| 69 | /// logic is going to search relatively to the current working directory. |
| 70 | static void AddImplicitInclude(MacroBuilder &Builder, StringRef File) { |
| 71 | Builder.append(Str: Twine("#include \"" ) + File + "\"" ); |
| 72 | } |
| 73 | |
| 74 | static void AddImplicitIncludeMacros(MacroBuilder &Builder, StringRef File) { |
| 75 | Builder.append(Str: Twine("#__include_macros \"" ) + File + "\"" ); |
| 76 | // Marker token to stop the __include_macros fetch loop. |
| 77 | Builder.append(Str: "##" ); // ##? |
| 78 | } |
| 79 | |
| 80 | /// Add an implicit \#include using the original file used to generate |
| 81 | /// a PCH file. |
| 82 | static void AddImplicitIncludePCH(MacroBuilder &Builder, Preprocessor &PP, |
| 83 | const PCHContainerReader &PCHContainerRdr, |
| 84 | StringRef ImplicitIncludePCH) { |
| 85 | std::string OriginalFile = ASTReader::getOriginalSourceFile( |
| 86 | ASTFileName: std::string(ImplicitIncludePCH), FileMgr&: PP.getFileManager(), PCHContainerRdr, |
| 87 | Diags&: PP.getDiagnostics()); |
| 88 | if (OriginalFile.empty()) |
| 89 | return; |
| 90 | |
| 91 | AddImplicitInclude(Builder, File: OriginalFile); |
| 92 | } |
| 93 | |
| 94 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 95 | /// specified FP model. |
| 96 | template <typename T> |
| 97 | static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, |
| 98 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, |
| 99 | T IEEEQuadVal) { |
| 100 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf()) |
| 101 | return IEEEHalfVal; |
| 102 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle()) |
| 103 | return IEEESingleVal; |
| 104 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble()) |
| 105 | return IEEEDoubleVal; |
| 106 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended()) |
| 107 | return X87DoubleExtendedVal; |
| 108 | if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) |
| 109 | return PPCDoubleDoubleVal; |
| 110 | assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); |
| 111 | return IEEEQuadVal; |
| 112 | } |
| 113 | |
| 114 | static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, |
| 115 | const llvm::fltSemantics *Sem, StringRef Ext) { |
| 116 | const char *DenormMin, *NormMax, *Epsilon, *Max, *Min; |
| 117 | NormMax = PickFP(Sem, IEEEHalfVal: "6.5504e+4" , IEEESingleVal: "3.40282347e+38" , |
| 118 | IEEEDoubleVal: "1.7976931348623157e+308" , X87DoubleExtendedVal: "1.18973149535723176502e+4932" , |
| 119 | PPCDoubleDoubleVal: "8.98846567431157953864652595394501e+307" , |
| 120 | IEEEQuadVal: "1.18973149535723176508575932662800702e+4932" ); |
| 121 | DenormMin = PickFP(Sem, IEEEHalfVal: "5.9604644775390625e-8" , IEEESingleVal: "1.40129846e-45" , |
| 122 | IEEEDoubleVal: "4.9406564584124654e-324" , X87DoubleExtendedVal: "3.64519953188247460253e-4951" , |
| 123 | PPCDoubleDoubleVal: "4.94065645841246544176568792868221e-324" , |
| 124 | IEEEQuadVal: "6.47517511943802511092443895822764655e-4966" ); |
| 125 | int Digits = PickFP(Sem, IEEEHalfVal: 3, IEEESingleVal: 6, IEEEDoubleVal: 15, X87DoubleExtendedVal: 18, PPCDoubleDoubleVal: 31, IEEEQuadVal: 33); |
| 126 | int DecimalDigits = PickFP(Sem, IEEEHalfVal: 5, IEEESingleVal: 9, IEEEDoubleVal: 17, X87DoubleExtendedVal: 21, PPCDoubleDoubleVal: 33, IEEEQuadVal: 36); |
| 127 | Epsilon = PickFP(Sem, IEEEHalfVal: "9.765625e-4" , IEEESingleVal: "1.19209290e-7" , |
| 128 | IEEEDoubleVal: "2.2204460492503131e-16" , X87DoubleExtendedVal: "1.08420217248550443401e-19" , |
| 129 | PPCDoubleDoubleVal: "4.94065645841246544176568792868221e-324" , |
| 130 | IEEEQuadVal: "1.92592994438723585305597794258492732e-34" ); |
| 131 | int MantissaDigits = PickFP(Sem, IEEEHalfVal: 11, IEEESingleVal: 24, IEEEDoubleVal: 53, X87DoubleExtendedVal: 64, PPCDoubleDoubleVal: 106, IEEEQuadVal: 113); |
| 132 | int Min10Exp = PickFP(Sem, IEEEHalfVal: -4, IEEESingleVal: -37, IEEEDoubleVal: -307, X87DoubleExtendedVal: -4931, PPCDoubleDoubleVal: -291, IEEEQuadVal: -4931); |
| 133 | int Max10Exp = PickFP(Sem, IEEEHalfVal: 4, IEEESingleVal: 38, IEEEDoubleVal: 308, X87DoubleExtendedVal: 4932, PPCDoubleDoubleVal: 308, IEEEQuadVal: 4932); |
| 134 | int MinExp = PickFP(Sem, IEEEHalfVal: -13, IEEESingleVal: -125, IEEEDoubleVal: -1021, X87DoubleExtendedVal: -16381, PPCDoubleDoubleVal: -968, IEEEQuadVal: -16381); |
| 135 | int MaxExp = PickFP(Sem, IEEEHalfVal: 16, IEEESingleVal: 128, IEEEDoubleVal: 1024, X87DoubleExtendedVal: 16384, PPCDoubleDoubleVal: 1024, IEEEQuadVal: 16384); |
| 136 | Min = PickFP(Sem, IEEEHalfVal: "6.103515625e-5" , IEEESingleVal: "1.17549435e-38" , IEEEDoubleVal: "2.2250738585072014e-308" , |
| 137 | X87DoubleExtendedVal: "3.36210314311209350626e-4932" , |
| 138 | PPCDoubleDoubleVal: "2.00416836000897277799610805135016e-292" , |
| 139 | IEEEQuadVal: "3.36210314311209350626267781732175260e-4932" ); |
| 140 | Max = PickFP(Sem, IEEEHalfVal: "6.5504e+4" , IEEESingleVal: "3.40282347e+38" , IEEEDoubleVal: "1.7976931348623157e+308" , |
| 141 | X87DoubleExtendedVal: "1.18973149535723176502e+4932" , |
| 142 | PPCDoubleDoubleVal: "1.79769313486231580793728971405301e+308" , |
| 143 | IEEEQuadVal: "1.18973149535723176508575932662800702e+4932" ); |
| 144 | |
| 145 | SmallString<32> DefPrefix; |
| 146 | DefPrefix = "__" ; |
| 147 | DefPrefix += Prefix; |
| 148 | DefPrefix += "_" ; |
| 149 | |
| 150 | Builder.defineMacro(Name: DefPrefix + "DENORM_MIN__" , Value: Twine(DenormMin)+Ext); |
| 151 | Builder.defineMacro(Name: DefPrefix + "NORM_MAX__" , Value: Twine(NormMax)+Ext); |
| 152 | Builder.defineMacro(Name: DefPrefix + "HAS_DENORM__" ); |
| 153 | Builder.defineMacro(Name: DefPrefix + "DIG__" , Value: Twine(Digits)); |
| 154 | Builder.defineMacro(Name: DefPrefix + "DECIMAL_DIG__" , Value: Twine(DecimalDigits)); |
| 155 | Builder.defineMacro(Name: DefPrefix + "EPSILON__" , Value: Twine(Epsilon)+Ext); |
| 156 | Builder.defineMacro(Name: DefPrefix + "HAS_INFINITY__" ); |
| 157 | Builder.defineMacro(Name: DefPrefix + "HAS_QUIET_NAN__" ); |
| 158 | Builder.defineMacro(Name: DefPrefix + "MANT_DIG__" , Value: Twine(MantissaDigits)); |
| 159 | |
| 160 | Builder.defineMacro(Name: DefPrefix + "MAX_10_EXP__" , Value: Twine(Max10Exp)); |
| 161 | Builder.defineMacro(Name: DefPrefix + "MAX_EXP__" , Value: Twine(MaxExp)); |
| 162 | Builder.defineMacro(Name: DefPrefix + "MAX__" , Value: Twine(Max)+Ext); |
| 163 | |
| 164 | Builder.defineMacro(Name: DefPrefix + "MIN_10_EXP__" ,Value: "(" +Twine(Min10Exp)+")" ); |
| 165 | Builder.defineMacro(Name: DefPrefix + "MIN_EXP__" , Value: "(" +Twine(MinExp)+")" ); |
| 166 | Builder.defineMacro(Name: DefPrefix + "MIN__" , Value: Twine(Min)+Ext); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 171 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 172 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
| 173 | static void DefineTypeSize(const Twine &MacroName, unsigned TypeWidth, |
| 174 | StringRef ValSuffix, bool isSigned, |
| 175 | MacroBuilder &Builder) { |
| 176 | llvm::APInt MaxVal = isSigned ? llvm::APInt::getSignedMaxValue(numBits: TypeWidth) |
| 177 | : llvm::APInt::getMaxValue(numBits: TypeWidth); |
| 178 | Builder.defineMacro(Name: MacroName, Value: toString(I: MaxVal, Radix: 10, Signed: isSigned) + ValSuffix); |
| 179 | } |
| 180 | |
| 181 | /// DefineTypeSize - An overloaded helper that uses TargetInfo to determine |
| 182 | /// the width, suffix, and signedness of the given type |
| 183 | static void DefineTypeSize(const Twine &MacroName, TargetInfo::IntType Ty, |
| 184 | const TargetInfo &TI, MacroBuilder &Builder) { |
| 185 | DefineTypeSize(MacroName, TypeWidth: TI.getTypeWidth(T: Ty), ValSuffix: TI.getTypeConstantSuffix(T: Ty), |
| 186 | isSigned: TI.isTypeSigned(T: Ty), Builder); |
| 187 | } |
| 188 | |
| 189 | static void DefineFmt(const LangOptions &LangOpts, const Twine &Prefix, |
| 190 | TargetInfo::IntType Ty, const TargetInfo &TI, |
| 191 | MacroBuilder &Builder) { |
| 192 | StringRef FmtModifier = TI.getTypeFormatModifier(T: Ty); |
| 193 | auto Emitter = [&](char Fmt) { |
| 194 | Builder.defineMacro(Name: Prefix + "_FMT" + Twine(Fmt) + "__" , |
| 195 | Value: Twine("\"" ) + FmtModifier + Twine(Fmt) + "\"" ); |
| 196 | }; |
| 197 | bool IsSigned = TI.isTypeSigned(T: Ty); |
| 198 | llvm::for_each(Range: StringRef(IsSigned ? "di" : "ouxX" ), F: Emitter); |
| 199 | |
| 200 | // C23 added the b and B modifiers for printing binary output of unsigned |
| 201 | // integers. Conditionally define those if compiling in C23 mode. |
| 202 | if (LangOpts.C23 && !IsSigned) |
| 203 | llvm::for_each(Range: StringRef("bB" ), F: Emitter); |
| 204 | } |
| 205 | |
| 206 | static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, |
| 207 | MacroBuilder &Builder) { |
| 208 | Builder.defineMacro(Name: MacroName, Value: TargetInfo::getTypeName(T: Ty)); |
| 209 | } |
| 210 | |
| 211 | static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty, |
| 212 | const TargetInfo &TI, MacroBuilder &Builder) { |
| 213 | Builder.defineMacro(Name: MacroName, Value: Twine(TI.getTypeWidth(T: Ty))); |
| 214 | } |
| 215 | |
| 216 | static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, |
| 217 | const TargetInfo &TI, MacroBuilder &Builder) { |
| 218 | Builder.defineMacro(Name: MacroName, |
| 219 | Value: Twine(BitWidth / TI.getCharWidth())); |
| 220 | } |
| 221 | |
| 222 | // This will generate a macro based on the prefix with `_MAX__` as the suffix |
| 223 | // for the max value representable for the type, and a macro with a `_WIDTH__` |
| 224 | // suffix for the width of the type. |
| 225 | static void DefineTypeSizeAndWidth(const Twine &Prefix, TargetInfo::IntType Ty, |
| 226 | const TargetInfo &TI, |
| 227 | MacroBuilder &Builder) { |
| 228 | DefineTypeSize(MacroName: Prefix + "_MAX__" , Ty, TI, Builder); |
| 229 | DefineTypeWidth(MacroName: Prefix + "_WIDTH__" , Ty, TI, Builder); |
| 230 | } |
| 231 | |
| 232 | static void DefineExactWidthIntType(const LangOptions &LangOpts, |
| 233 | TargetInfo::IntType Ty, |
| 234 | const TargetInfo &TI, |
| 235 | MacroBuilder &Builder) { |
| 236 | int TypeWidth = TI.getTypeWidth(T: Ty); |
| 237 | bool IsSigned = TI.isTypeSigned(T: Ty); |
| 238 | |
| 239 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
| 240 | // ends up being defined in terms of the correct type. |
| 241 | if (TypeWidth == 64) |
| 242 | Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); |
| 243 | |
| 244 | // Use the target specified int16 type when appropriate. Some MCU targets |
| 245 | // (such as AVR) have definition of [u]int16_t to [un]signed int. |
| 246 | if (TypeWidth == 16) |
| 247 | Ty = IsSigned ? TI.getInt16Type() : TI.getUInt16Type(); |
| 248 | |
| 249 | const char *Prefix = IsSigned ? "__INT" : "__UINT" ; |
| 250 | |
| 251 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
| 252 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
| 253 | |
| 254 | StringRef ConstSuffix(TI.getTypeConstantSuffix(T: Ty)); |
| 255 | Builder.defineMacro(Name: Prefix + Twine(TypeWidth) + "_C_SUFFIX__" , Value: ConstSuffix); |
| 256 | Builder.defineMacro(Name: Prefix + Twine(TypeWidth) + "_C(c)" , |
| 257 | Value: ConstSuffix.size() ? Twine("c##" ) + ConstSuffix : "c" ); |
| 258 | } |
| 259 | |
| 260 | static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty, |
| 261 | const TargetInfo &TI, |
| 262 | MacroBuilder &Builder) { |
| 263 | int TypeWidth = TI.getTypeWidth(T: Ty); |
| 264 | bool IsSigned = TI.isTypeSigned(T: Ty); |
| 265 | |
| 266 | // Use the target specified int64 type, when appropriate, so that [u]int64_t |
| 267 | // ends up being defined in terms of the correct type. |
| 268 | if (TypeWidth == 64) |
| 269 | Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); |
| 270 | |
| 271 | // We don't need to define a _WIDTH macro for the exact-width types because |
| 272 | // we already know the width. |
| 273 | const char *Prefix = IsSigned ? "__INT" : "__UINT" ; |
| 274 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
| 275 | } |
| 276 | |
| 277 | static void DefineLeastWidthIntType(const LangOptions &LangOpts, |
| 278 | unsigned TypeWidth, bool IsSigned, |
| 279 | const TargetInfo &TI, |
| 280 | MacroBuilder &Builder) { |
| 281 | TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(BitWidth: TypeWidth, IsSigned); |
| 282 | if (Ty == TargetInfo::NoInt) |
| 283 | return; |
| 284 | |
| 285 | const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST" ; |
| 286 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
| 287 | // We only want the *_WIDTH macro for the signed types to avoid too many |
| 288 | // predefined macros (the unsigned width and the signed width are identical.) |
| 289 | if (IsSigned) |
| 290 | DefineTypeSizeAndWidth(Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
| 291 | else |
| 292 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
| 293 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
| 294 | } |
| 295 | |
| 296 | static void DefineFastIntType(const LangOptions &LangOpts, unsigned TypeWidth, |
| 297 | bool IsSigned, const TargetInfo &TI, |
| 298 | MacroBuilder &Builder) { |
| 299 | // stdint.h currently defines the fast int types as equivalent to the least |
| 300 | // types. |
| 301 | TargetInfo::IntType Ty = TI.getLeastIntTypeByWidth(BitWidth: TypeWidth, IsSigned); |
| 302 | if (Ty == TargetInfo::NoInt) |
| 303 | return; |
| 304 | |
| 305 | const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST" ; |
| 306 | DefineType(MacroName: Prefix + Twine(TypeWidth) + "_TYPE__" , Ty, Builder); |
| 307 | // We only want the *_WIDTH macro for the signed types to avoid too many |
| 308 | // predefined macros (the unsigned width and the signed width are identical.) |
| 309 | if (IsSigned) |
| 310 | DefineTypeSizeAndWidth(Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
| 311 | else |
| 312 | DefineTypeSize(MacroName: Prefix + Twine(TypeWidth) + "_MAX__" , Ty, TI, Builder); |
| 313 | DefineFmt(LangOpts, Prefix: Prefix + Twine(TypeWidth), Ty, TI, Builder); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with |
| 318 | /// the specified properties. |
| 319 | static const char *getLockFreeValue(unsigned TypeWidth, const TargetInfo &TI) { |
| 320 | // Fully-aligned, power-of-2 sizes no larger than the inline |
| 321 | // width will be inlined as lock-free operations. |
| 322 | // Note: we do not need to check alignment since _Atomic(T) is always |
| 323 | // appropriately-aligned in clang. |
| 324 | if (TI.hasBuiltinAtomic(AtomicSizeInBits: TypeWidth, AlignmentInBits: TypeWidth)) |
| 325 | return "2" ; // "always lock free" |
| 326 | // We cannot be certain what operations the lib calls might be |
| 327 | // able to implement as lock-free on future processors. |
| 328 | return "1" ; // "sometimes lock free" |
| 329 | } |
| 330 | |
| 331 | /// Add definitions required for a smooth interaction between |
| 332 | /// Objective-C++ automated reference counting and libstdc++ (4.2). |
| 333 | static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, |
| 334 | MacroBuilder &Builder) { |
| 335 | Builder.defineMacro(Name: "_GLIBCXX_PREDEFINED_OBJC_ARC_IS_SCALAR" ); |
| 336 | |
| 337 | std::string Result; |
| 338 | { |
| 339 | // Provide specializations for the __is_scalar type trait so that |
| 340 | // lifetime-qualified objects are not considered "scalar" types, which |
| 341 | // libstdc++ uses as an indicator of the presence of trivial copy, assign, |
| 342 | // default-construct, and destruct semantics (none of which hold for |
| 343 | // lifetime-qualified objects in ARC). |
| 344 | llvm::raw_string_ostream Out(Result); |
| 345 | |
| 346 | Out << "namespace std {\n" |
| 347 | << "\n" |
| 348 | << "struct __true_type;\n" |
| 349 | << "struct __false_type;\n" |
| 350 | << "\n" ; |
| 351 | |
| 352 | Out << "template<typename _Tp> struct __is_scalar;\n" |
| 353 | << "\n" ; |
| 354 | |
| 355 | if (LangOpts.ObjCAutoRefCount) { |
| 356 | Out << "template<typename _Tp>\n" |
| 357 | << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" |
| 358 | << " enum { __value = 0 };\n" |
| 359 | << " typedef __false_type __type;\n" |
| 360 | << "};\n" |
| 361 | << "\n" ; |
| 362 | } |
| 363 | |
| 364 | if (LangOpts.ObjCWeak) { |
| 365 | Out << "template<typename _Tp>\n" |
| 366 | << "struct __is_scalar<__attribute__((objc_ownership(weak))) _Tp> {\n" |
| 367 | << " enum { __value = 0 };\n" |
| 368 | << " typedef __false_type __type;\n" |
| 369 | << "};\n" |
| 370 | << "\n" ; |
| 371 | } |
| 372 | |
| 373 | if (LangOpts.ObjCAutoRefCount) { |
| 374 | Out << "template<typename _Tp>\n" |
| 375 | << "struct __is_scalar<__attribute__((objc_ownership(autoreleasing)))" |
| 376 | << " _Tp> {\n" |
| 377 | << " enum { __value = 0 };\n" |
| 378 | << " typedef __false_type __type;\n" |
| 379 | << "};\n" |
| 380 | << "\n" ; |
| 381 | } |
| 382 | |
| 383 | Out << "}\n" ; |
| 384 | } |
| 385 | Builder.append(Str: Result); |
| 386 | } |
| 387 | |
| 388 | static void InitializeStandardPredefinedMacros(const TargetInfo &TI, |
| 389 | const LangOptions &LangOpts, |
| 390 | const FrontendOptions &FEOpts, |
| 391 | MacroBuilder &Builder) { |
| 392 | if (LangOpts.HLSL) { |
| 393 | Builder.defineMacro(Name: "__hlsl_clang" ); |
| 394 | // HLSL Version |
| 395 | Builder.defineMacro(Name: "__HLSL_VERSION" , |
| 396 | Value: Twine((unsigned)LangOpts.getHLSLVersion())); |
| 397 | Builder.defineMacro(Name: "__HLSL_202x" , |
| 398 | Value: Twine((unsigned)LangOptions::HLSLLangStd::HLSL_202x)); |
| 399 | Builder.defineMacro(Name: "__HLSL_202y" , |
| 400 | Value: Twine((unsigned)LangOptions::HLSLLangStd::HLSL_202y)); |
| 401 | |
| 402 | if (LangOpts.NativeHalfType) |
| 403 | Builder.defineMacro(Name: "__HLSL_ENABLE_16_BIT" , Value: "1" ); |
| 404 | |
| 405 | // Shader target information |
| 406 | // "enums" for shader stages |
| 407 | Builder.defineMacro(Name: "__SHADER_STAGE_VERTEX" , |
| 408 | Value: Twine((uint32_t)ShaderStage::Vertex)); |
| 409 | Builder.defineMacro(Name: "__SHADER_STAGE_PIXEL" , |
| 410 | Value: Twine((uint32_t)ShaderStage::Pixel)); |
| 411 | Builder.defineMacro(Name: "__SHADER_STAGE_GEOMETRY" , |
| 412 | Value: Twine((uint32_t)ShaderStage::Geometry)); |
| 413 | Builder.defineMacro(Name: "__SHADER_STAGE_HULL" , |
| 414 | Value: Twine((uint32_t)ShaderStage::Hull)); |
| 415 | Builder.defineMacro(Name: "__SHADER_STAGE_DOMAIN" , |
| 416 | Value: Twine((uint32_t)ShaderStage::Domain)); |
| 417 | Builder.defineMacro(Name: "__SHADER_STAGE_COMPUTE" , |
| 418 | Value: Twine((uint32_t)ShaderStage::Compute)); |
| 419 | Builder.defineMacro(Name: "__SHADER_STAGE_AMPLIFICATION" , |
| 420 | Value: Twine((uint32_t)ShaderStage::Amplification)); |
| 421 | Builder.defineMacro(Name: "__SHADER_STAGE_MESH" , |
| 422 | Value: Twine((uint32_t)ShaderStage::Mesh)); |
| 423 | Builder.defineMacro(Name: "__SHADER_STAGE_LIBRARY" , |
| 424 | Value: Twine((uint32_t)ShaderStage::Library)); |
| 425 | // The current shader stage itself |
| 426 | uint32_t StageInteger = static_cast<uint32_t>( |
| 427 | hlsl::getStageFromEnvironment(E: TI.getTriple().getEnvironment())); |
| 428 | |
| 429 | Builder.defineMacro(Name: "__SHADER_TARGET_STAGE" , Value: Twine(StageInteger)); |
| 430 | // Add target versions |
| 431 | if (TI.getTriple().getOS() == llvm::Triple::ShaderModel) { |
| 432 | VersionTuple Version = TI.getTriple().getOSVersion(); |
| 433 | Builder.defineMacro(Name: "__SHADER_TARGET_MAJOR" , Value: Twine(Version.getMajor())); |
| 434 | unsigned Minor = Version.getMinor().value_or(u: 0); |
| 435 | Builder.defineMacro(Name: "__SHADER_TARGET_MINOR" , Value: Twine(Minor)); |
| 436 | } |
| 437 | return; |
| 438 | } |
| 439 | // C++ [cpp.predefined]p1: |
| 440 | // The following macro names shall be defined by the implementation: |
| 441 | |
| 442 | // -- __STDC__ |
| 443 | // [C++] Whether __STDC__ is predefined and if so, what its value is, |
| 444 | // are implementation-defined. |
| 445 | // (Removed in C++20.) |
| 446 | if ((!LangOpts.MSVCCompat || LangOpts.MSVCEnableStdcMacro) && |
| 447 | !LangOpts.TraditionalCPP) |
| 448 | Builder.defineMacro(Name: "__STDC__" ); |
| 449 | // -- __STDC_HOSTED__ |
| 450 | // The integer literal 1 if the implementation is a hosted |
| 451 | // implementation or the integer literal 0 if it is not. |
| 452 | if (LangOpts.Freestanding) |
| 453 | Builder.defineMacro(Name: "__STDC_HOSTED__" , Value: "0" ); |
| 454 | else |
| 455 | Builder.defineMacro(Name: "__STDC_HOSTED__" ); |
| 456 | |
| 457 | // -- __STDC_VERSION__ |
| 458 | // [C++] Whether __STDC_VERSION__ is predefined and if so, what its |
| 459 | // value is, are implementation-defined. |
| 460 | // (Removed in C++20.) |
| 461 | if (!LangOpts.CPlusPlus) { |
| 462 | if (LangOpts.C2y) |
| 463 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "202400L" ); |
| 464 | else if (LangOpts.C23) |
| 465 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "202311L" ); |
| 466 | else if (LangOpts.C17) |
| 467 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "201710L" ); |
| 468 | else if (LangOpts.C11) |
| 469 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "201112L" ); |
| 470 | else if (LangOpts.C99) |
| 471 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "199901L" ); |
| 472 | else if (!LangOpts.GNUMode && LangOpts.Digraphs) |
| 473 | Builder.defineMacro(Name: "__STDC_VERSION__" , Value: "199409L" ); |
| 474 | } else { |
| 475 | // -- __cplusplus |
| 476 | if (LangOpts.CPlusPlus26) |
| 477 | // FIXME: Use correct value for C++26. |
| 478 | Builder.defineMacro(Name: "__cplusplus" , Value: "202400L" ); |
| 479 | else if (LangOpts.CPlusPlus23) |
| 480 | Builder.defineMacro(Name: "__cplusplus" , Value: "202302L" ); |
| 481 | // [C++20] The integer literal 202002L. |
| 482 | else if (LangOpts.CPlusPlus20) |
| 483 | Builder.defineMacro(Name: "__cplusplus" , Value: "202002L" ); |
| 484 | // [C++17] The integer literal 201703L. |
| 485 | else if (LangOpts.CPlusPlus17) |
| 486 | Builder.defineMacro(Name: "__cplusplus" , Value: "201703L" ); |
| 487 | // [C++14] The name __cplusplus is defined to the value 201402L when |
| 488 | // compiling a C++ translation unit. |
| 489 | else if (LangOpts.CPlusPlus14) |
| 490 | Builder.defineMacro(Name: "__cplusplus" , Value: "201402L" ); |
| 491 | // [C++11] The name __cplusplus is defined to the value 201103L when |
| 492 | // compiling a C++ translation unit. |
| 493 | else if (LangOpts.CPlusPlus11) |
| 494 | Builder.defineMacro(Name: "__cplusplus" , Value: "201103L" ); |
| 495 | // [C++03] The name __cplusplus is defined to the value 199711L when |
| 496 | // compiling a C++ translation unit. |
| 497 | else |
| 498 | Builder.defineMacro(Name: "__cplusplus" , Value: "199711L" ); |
| 499 | |
| 500 | // -- __STDCPP_DEFAULT_NEW_ALIGNMENT__ |
| 501 | // [C++17] An integer literal of type std::size_t whose value is the |
| 502 | // alignment guaranteed by a call to operator new(std::size_t) |
| 503 | // |
| 504 | // We provide this in all language modes, since it seems generally useful. |
| 505 | Builder.defineMacro(Name: "__STDCPP_DEFAULT_NEW_ALIGNMENT__" , |
| 506 | Value: Twine(TI.getNewAlign() / TI.getCharWidth()) + |
| 507 | TI.getTypeConstantSuffix(T: TI.getSizeType())); |
| 508 | |
| 509 | // -- __STDCPP_ÂTHREADS__ |
| 510 | // Defined, and has the value integer literal 1, if and only if a |
| 511 | // program can have more than one thread of execution. |
| 512 | if (LangOpts.getThreadModel() == LangOptions::ThreadModelKind::POSIX) |
| 513 | Builder.defineMacro(Name: "__STDCPP_THREADS__" , Value: "1" ); |
| 514 | } |
| 515 | |
| 516 | // In C11 these are environment macros. In C++11 they are only defined |
| 517 | // as part of <cuchar>. To prevent breakage when mixing C and C++ |
| 518 | // code, define these macros unconditionally. We can define them |
| 519 | // unconditionally, as Clang always uses UTF-16 and UTF-32 for 16-bit |
| 520 | // and 32-bit character literals. |
| 521 | Builder.defineMacro(Name: "__STDC_UTF_16__" , Value: "1" ); |
| 522 | Builder.defineMacro(Name: "__STDC_UTF_32__" , Value: "1" ); |
| 523 | |
| 524 | // __has_embed definitions |
| 525 | Builder.defineMacro(Name: "__STDC_EMBED_NOT_FOUND__" , |
| 526 | Value: llvm::itostr(X: static_cast<int>(EmbedResult::NotFound))); |
| 527 | Builder.defineMacro(Name: "__STDC_EMBED_FOUND__" , |
| 528 | Value: llvm::itostr(X: static_cast<int>(EmbedResult::Found))); |
| 529 | Builder.defineMacro(Name: "__STDC_EMBED_EMPTY__" , |
| 530 | Value: llvm::itostr(X: static_cast<int>(EmbedResult::Empty))); |
| 531 | |
| 532 | if (LangOpts.ObjC) |
| 533 | Builder.defineMacro(Name: "__OBJC__" ); |
| 534 | |
| 535 | // OpenCL v1.0/1.1 s6.9, v1.2/2.0 s6.10: Preprocessor Directives and Macros. |
| 536 | if (LangOpts.OpenCL) { |
| 537 | if (LangOpts.CPlusPlus) { |
| 538 | switch (LangOpts.OpenCLCPlusPlusVersion) { |
| 539 | case 100: |
| 540 | Builder.defineMacro(Name: "__OPENCL_CPP_VERSION__" , Value: "100" ); |
| 541 | break; |
| 542 | case 202100: |
| 543 | Builder.defineMacro(Name: "__OPENCL_CPP_VERSION__" , Value: "202100" ); |
| 544 | break; |
| 545 | default: |
| 546 | llvm_unreachable("Unsupported C++ version for OpenCL" ); |
| 547 | } |
| 548 | Builder.defineMacro(Name: "__CL_CPP_VERSION_1_0__" , Value: "100" ); |
| 549 | Builder.defineMacro(Name: "__CL_CPP_VERSION_2021__" , Value: "202100" ); |
| 550 | } else { |
| 551 | // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the |
| 552 | // language standard with which the program is compiled. __OPENCL_VERSION__ |
| 553 | // is for the OpenCL version supported by the OpenCL device, which is not |
| 554 | // necessarily the language standard with which the program is compiled. |
| 555 | // A shared OpenCL header file requires a macro to indicate the language |
| 556 | // standard. As a workaround, __OPENCL_C_VERSION__ is defined for |
| 557 | // OpenCL v1.0 and v1.1. |
| 558 | switch (LangOpts.OpenCLVersion) { |
| 559 | case 100: |
| 560 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "100" ); |
| 561 | break; |
| 562 | case 110: |
| 563 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "110" ); |
| 564 | break; |
| 565 | case 120: |
| 566 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "120" ); |
| 567 | break; |
| 568 | case 200: |
| 569 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "200" ); |
| 570 | break; |
| 571 | case 300: |
| 572 | Builder.defineMacro(Name: "__OPENCL_C_VERSION__" , Value: "300" ); |
| 573 | break; |
| 574 | default: |
| 575 | llvm_unreachable("Unsupported OpenCL version" ); |
| 576 | } |
| 577 | } |
| 578 | Builder.defineMacro(Name: "CL_VERSION_1_0" , Value: "100" ); |
| 579 | Builder.defineMacro(Name: "CL_VERSION_1_1" , Value: "110" ); |
| 580 | Builder.defineMacro(Name: "CL_VERSION_1_2" , Value: "120" ); |
| 581 | Builder.defineMacro(Name: "CL_VERSION_2_0" , Value: "200" ); |
| 582 | Builder.defineMacro(Name: "CL_VERSION_3_0" , Value: "300" ); |
| 583 | |
| 584 | if (TI.isLittleEndian()) |
| 585 | Builder.defineMacro(Name: "__ENDIAN_LITTLE__" ); |
| 586 | |
| 587 | if (LangOpts.FastRelaxedMath) |
| 588 | Builder.defineMacro(Name: "__FAST_RELAXED_MATH__" ); |
| 589 | } |
| 590 | |
| 591 | if (LangOpts.SYCLIsDevice || LangOpts.SYCLIsHost) { |
| 592 | // SYCL Version is set to a value when building SYCL applications |
| 593 | if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2017) |
| 594 | Builder.defineMacro(Name: "CL_SYCL_LANGUAGE_VERSION" , Value: "121" ); |
| 595 | else if (LangOpts.getSYCLVersion() == LangOptions::SYCL_2020) |
| 596 | Builder.defineMacro(Name: "SYCL_LANGUAGE_VERSION" , Value: "202012L" ); |
| 597 | } |
| 598 | |
| 599 | // Not "standard" per se, but available even with the -undef flag. |
| 600 | if (LangOpts.AsmPreprocessor) |
| 601 | Builder.defineMacro(Name: "__ASSEMBLER__" ); |
| 602 | if (LangOpts.CUDA) { |
| 603 | if (LangOpts.GPURelocatableDeviceCode) |
| 604 | Builder.defineMacro(Name: "__CLANG_RDC__" ); |
| 605 | if (!LangOpts.HIP) |
| 606 | Builder.defineMacro(Name: "__CUDA__" ); |
| 607 | if (LangOpts.GPUDefaultStream == |
| 608 | LangOptions::GPUDefaultStreamKind::PerThread) |
| 609 | Builder.defineMacro(Name: "CUDA_API_PER_THREAD_DEFAULT_STREAM" ); |
| 610 | } |
| 611 | if (LangOpts.HIP) { |
| 612 | Builder.defineMacro(Name: "__HIP__" ); |
| 613 | Builder.defineMacro(Name: "__HIPCC__" ); |
| 614 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_SINGLETHREAD" , Value: "1" ); |
| 615 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_WAVEFRONT" , Value: "2" ); |
| 616 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_WORKGROUP" , Value: "3" ); |
| 617 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_AGENT" , Value: "4" ); |
| 618 | Builder.defineMacro(Name: "__HIP_MEMORY_SCOPE_SYSTEM" , Value: "5" ); |
| 619 | if (LangOpts.HIPStdPar) { |
| 620 | Builder.defineMacro(Name: "__HIPSTDPAR__" ); |
| 621 | if (LangOpts.HIPStdParInterposeAlloc) { |
| 622 | Builder.defineMacro(Name: "__HIPSTDPAR_INTERPOSE_ALLOC__" ); |
| 623 | Builder.defineMacro(Name: "__HIPSTDPAR_INTERPOSE_ALLOC_V1__" ); |
| 624 | } |
| 625 | } |
| 626 | if (LangOpts.CUDAIsDevice) { |
| 627 | Builder.defineMacro(Name: "__HIP_DEVICE_COMPILE__" ); |
| 628 | if (!TI.hasHIPImageSupport()) { |
| 629 | Builder.defineMacro(Name: "__HIP_NO_IMAGE_SUPPORT__" , Value: "1" ); |
| 630 | // Deprecated. |
| 631 | Builder.defineMacro(Name: "__HIP_NO_IMAGE_SUPPORT" , Value: "1" ); |
| 632 | } |
| 633 | } |
| 634 | if (LangOpts.GPUDefaultStream == |
| 635 | LangOptions::GPUDefaultStreamKind::PerThread) { |
| 636 | Builder.defineMacro(Name: "__HIP_API_PER_THREAD_DEFAULT_STREAM__" ); |
| 637 | // Deprecated. |
| 638 | Builder.defineMacro(Name: "HIP_API_PER_THREAD_DEFAULT_STREAM" ); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if (LangOpts.OpenACC) { |
| 643 | // FIXME: When we have full support for OpenACC, we should set this to the |
| 644 | // version we support. Until then, set as '1' by default, but provide a |
| 645 | // temporary mechanism for users to override this so real-world examples can |
| 646 | // be tested against. |
| 647 | if (!LangOpts.OpenACCMacroOverride.empty()) |
| 648 | Builder.defineMacro(Name: "_OPENACC" , Value: LangOpts.OpenACCMacroOverride); |
| 649 | else |
| 650 | Builder.defineMacro(Name: "_OPENACC" , Value: "1" ); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /// Initialize the predefined C++ language feature test macros defined in |
| 655 | /// ISO/IEC JTC1/SC22/WG21 (C++) SD-6: "SG10 Feature Test Recommendations". |
| 656 | static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, |
| 657 | MacroBuilder &Builder) { |
| 658 | // C++98 features. |
| 659 | if (LangOpts.RTTI) |
| 660 | Builder.defineMacro(Name: "__cpp_rtti" , Value: "199711L" ); |
| 661 | if (LangOpts.CXXExceptions) |
| 662 | Builder.defineMacro(Name: "__cpp_exceptions" , Value: "199711L" ); |
| 663 | |
| 664 | // C++11 features. |
| 665 | if (LangOpts.CPlusPlus11) { |
| 666 | Builder.defineMacro(Name: "__cpp_unicode_characters" , Value: "200704L" ); |
| 667 | Builder.defineMacro(Name: "__cpp_raw_strings" , Value: "200710L" ); |
| 668 | Builder.defineMacro(Name: "__cpp_unicode_literals" , Value: "200710L" ); |
| 669 | Builder.defineMacro(Name: "__cpp_user_defined_literals" , Value: "200809L" ); |
| 670 | Builder.defineMacro(Name: "__cpp_lambdas" , Value: "200907L" ); |
| 671 | Builder.defineMacro(Name: "__cpp_constexpr" , Value: LangOpts.CPlusPlus26 ? "202406L" |
| 672 | : LangOpts.CPlusPlus23 ? "202211L" |
| 673 | : LangOpts.CPlusPlus20 ? "202002L" |
| 674 | : LangOpts.CPlusPlus17 ? "201603L" |
| 675 | : LangOpts.CPlusPlus14 ? "201304L" |
| 676 | : "200704" ); |
| 677 | Builder.defineMacro(Name: "__cpp_constexpr_in_decltype" , Value: "201711L" ); |
| 678 | Builder.defineMacro(Name: "__cpp_range_based_for" , |
| 679 | Value: LangOpts.CPlusPlus23 ? "202211L" |
| 680 | : LangOpts.CPlusPlus17 ? "201603L" |
| 681 | : "200907" ); |
| 682 | // C++17 / C++26 static_assert supported as an extension in earlier language |
| 683 | // modes, so we use the C++26 value. |
| 684 | Builder.defineMacro(Name: "__cpp_static_assert" , Value: "202306L" ); |
| 685 | Builder.defineMacro(Name: "__cpp_decltype" , Value: "200707L" ); |
| 686 | Builder.defineMacro(Name: "__cpp_attributes" , Value: "200809L" ); |
| 687 | Builder.defineMacro(Name: "__cpp_rvalue_references" , Value: "200610L" ); |
| 688 | Builder.defineMacro(Name: "__cpp_variadic_templates" , Value: "200704L" ); |
| 689 | Builder.defineMacro(Name: "__cpp_initializer_lists" , Value: "200806L" ); |
| 690 | Builder.defineMacro(Name: "__cpp_delegating_constructors" , Value: "200604L" ); |
| 691 | Builder.defineMacro(Name: "__cpp_nsdmi" , Value: "200809L" ); |
| 692 | Builder.defineMacro(Name: "__cpp_inheriting_constructors" , Value: "201511L" ); |
| 693 | Builder.defineMacro(Name: "__cpp_ref_qualifiers" , Value: "200710L" ); |
| 694 | Builder.defineMacro(Name: "__cpp_alias_templates" , Value: "200704L" ); |
| 695 | } |
| 696 | if (LangOpts.ThreadsafeStatics) |
| 697 | Builder.defineMacro(Name: "__cpp_threadsafe_static_init" , Value: "200806L" ); |
| 698 | |
| 699 | // C++14 features. |
| 700 | if (LangOpts.CPlusPlus14) { |
| 701 | Builder.defineMacro(Name: "__cpp_binary_literals" , Value: "201304L" ); |
| 702 | Builder.defineMacro(Name: "__cpp_digit_separators" , Value: "201309L" ); |
| 703 | Builder.defineMacro(Name: "__cpp_init_captures" , |
| 704 | Value: LangOpts.CPlusPlus20 ? "201803L" : "201304L" ); |
| 705 | Builder.defineMacro(Name: "__cpp_generic_lambdas" , |
| 706 | Value: LangOpts.CPlusPlus20 ? "201707L" : "201304L" ); |
| 707 | Builder.defineMacro(Name: "__cpp_decltype_auto" , Value: "201304L" ); |
| 708 | Builder.defineMacro(Name: "__cpp_return_type_deduction" , Value: "201304L" ); |
| 709 | Builder.defineMacro(Name: "__cpp_aggregate_nsdmi" , Value: "201304L" ); |
| 710 | Builder.defineMacro(Name: "__cpp_variable_templates" , Value: "201304L" ); |
| 711 | } |
| 712 | if (LangOpts.SizedDeallocation) |
| 713 | Builder.defineMacro(Name: "__cpp_sized_deallocation" , Value: "201309L" ); |
| 714 | |
| 715 | // C++17 features. |
| 716 | if (LangOpts.CPlusPlus17) { |
| 717 | Builder.defineMacro(Name: "__cpp_hex_float" , Value: "201603L" ); |
| 718 | Builder.defineMacro(Name: "__cpp_inline_variables" , Value: "201606L" ); |
| 719 | Builder.defineMacro(Name: "__cpp_noexcept_function_type" , Value: "201510L" ); |
| 720 | Builder.defineMacro(Name: "__cpp_capture_star_this" , Value: "201603L" ); |
| 721 | Builder.defineMacro(Name: "__cpp_if_constexpr" , Value: "201606L" ); |
| 722 | Builder.defineMacro(Name: "__cpp_deduction_guides" , Value: "201703L" ); // (not latest) |
| 723 | Builder.defineMacro(Name: "__cpp_template_auto" , Value: "201606L" ); // (old name) |
| 724 | Builder.defineMacro(Name: "__cpp_namespace_attributes" , Value: "201411L" ); |
| 725 | Builder.defineMacro(Name: "__cpp_enumerator_attributes" , Value: "201411L" ); |
| 726 | Builder.defineMacro(Name: "__cpp_nested_namespace_definitions" , Value: "201411L" ); |
| 727 | Builder.defineMacro(Name: "__cpp_variadic_using" , Value: "201611L" ); |
| 728 | Builder.defineMacro(Name: "__cpp_aggregate_bases" , Value: "201603L" ); |
| 729 | Builder.defineMacro(Name: "__cpp_structured_bindings" , Value: "202411L" ); |
| 730 | Builder.defineMacro(Name: "__cpp_nontype_template_args" , |
| 731 | Value: "201411L" ); // (not latest) |
| 732 | Builder.defineMacro(Name: "__cpp_fold_expressions" , Value: "201603L" ); |
| 733 | Builder.defineMacro(Name: "__cpp_guaranteed_copy_elision" , Value: "201606L" ); |
| 734 | Builder.defineMacro(Name: "__cpp_nontype_template_parameter_auto" , Value: "201606L" ); |
| 735 | } |
| 736 | if (LangOpts.AlignedAllocation && !LangOpts.AlignedAllocationUnavailable) |
| 737 | Builder.defineMacro(Name: "__cpp_aligned_new" , Value: "201606L" ); |
| 738 | |
| 739 | Builder.defineMacro(Name: "__cpp_template_template_args" , Value: "201611L" ); |
| 740 | |
| 741 | // C++20 features. |
| 742 | if (LangOpts.CPlusPlus20) { |
| 743 | Builder.defineMacro(Name: "__cpp_aggregate_paren_init" , Value: "201902L" ); |
| 744 | |
| 745 | Builder.defineMacro(Name: "__cpp_concepts" , Value: "202002" ); |
| 746 | Builder.defineMacro(Name: "__cpp_conditional_explicit" , Value: "201806L" ); |
| 747 | Builder.defineMacro(Name: "__cpp_consteval" , Value: "202211L" ); |
| 748 | Builder.defineMacro(Name: "__cpp_constexpr_dynamic_alloc" , Value: "201907L" ); |
| 749 | Builder.defineMacro(Name: "__cpp_constinit" , Value: "201907L" ); |
| 750 | Builder.defineMacro(Name: "__cpp_impl_coroutine" , Value: "201902L" ); |
| 751 | Builder.defineMacro(Name: "__cpp_designated_initializers" , Value: "201707L" ); |
| 752 | Builder.defineMacro(Name: "__cpp_impl_three_way_comparison" , Value: "201907L" ); |
| 753 | //Builder.defineMacro("__cpp_modules", "201907L"); |
| 754 | Builder.defineMacro(Name: "__cpp_using_enum" , Value: "201907L" ); |
| 755 | } |
| 756 | // C++23 features. |
| 757 | if (LangOpts.CPlusPlus23) { |
| 758 | Builder.defineMacro(Name: "__cpp_implicit_move" , Value: "202207L" ); |
| 759 | Builder.defineMacro(Name: "__cpp_size_t_suffix" , Value: "202011L" ); |
| 760 | Builder.defineMacro(Name: "__cpp_if_consteval" , Value: "202106L" ); |
| 761 | Builder.defineMacro(Name: "__cpp_multidimensional_subscript" , Value: "202211L" ); |
| 762 | Builder.defineMacro(Name: "__cpp_auto_cast" , Value: "202110L" ); |
| 763 | Builder.defineMacro(Name: "__cpp_explicit_this_parameter" , Value: "202110L" ); |
| 764 | } |
| 765 | |
| 766 | // We provide those C++23 features as extensions in earlier language modes, so |
| 767 | // we also define their feature test macros. |
| 768 | if (LangOpts.CPlusPlus11) |
| 769 | Builder.defineMacro(Name: "__cpp_static_call_operator" , Value: "202207L" ); |
| 770 | Builder.defineMacro(Name: "__cpp_named_character_escapes" , Value: "202207L" ); |
| 771 | Builder.defineMacro(Name: "__cpp_placeholder_variables" , Value: "202306L" ); |
| 772 | |
| 773 | // C++26 features supported in earlier language modes. |
| 774 | Builder.defineMacro(Name: "__cpp_pack_indexing" , Value: "202311L" ); |
| 775 | Builder.defineMacro(Name: "__cpp_deleted_function" , Value: "202403L" ); |
| 776 | Builder.defineMacro(Name: "__cpp_variadic_friend" , Value: "202403L" ); |
| 777 | // Builder.defineMacro("__cpp_trivial_relocatability", "202502L"); |
| 778 | |
| 779 | if (LangOpts.Char8) |
| 780 | Builder.defineMacro(Name: "__cpp_char8_t" , Value: "202207L" ); |
| 781 | Builder.defineMacro(Name: "__cpp_impl_destroying_delete" , Value: "201806L" ); |
| 782 | |
| 783 | // TODO: Final number? |
| 784 | Builder.defineMacro(Name: "__cpp_type_aware_allocators" , Value: "202500L" ); |
| 785 | } |
| 786 | |
| 787 | /// InitializeOpenCLFeatureTestMacros - Define OpenCL macros based on target |
| 788 | /// settings and language version |
| 789 | void InitializeOpenCLFeatureTestMacros(const TargetInfo &TI, |
| 790 | const LangOptions &Opts, |
| 791 | MacroBuilder &Builder) { |
| 792 | const llvm::StringMap<bool> &OpenCLFeaturesMap = TI.getSupportedOpenCLOpts(); |
| 793 | // FIXME: OpenCL options which affect language semantics/syntax |
| 794 | // should be moved into LangOptions. |
| 795 | auto defineOpenCLExtMacro = [&](llvm::StringRef Name, auto... OptArgs) { |
| 796 | // Check if extension is supported by target and is available in this |
| 797 | // OpenCL version |
| 798 | if (TI.hasFeatureEnabled(Features: OpenCLFeaturesMap, Name) && |
| 799 | OpenCLOptions::isOpenCLOptionAvailableIn(Opts, OptArgs...)) |
| 800 | Builder.defineMacro(Name); |
| 801 | }; |
| 802 | #define OPENCL_GENERIC_EXTENSION(Ext, ...) \ |
| 803 | defineOpenCLExtMacro(#Ext, __VA_ARGS__); |
| 804 | #include "clang/Basic/OpenCLExtensions.def" |
| 805 | |
| 806 | // Assume compiling for FULL profile |
| 807 | Builder.defineMacro(Name: "__opencl_c_int64" ); |
| 808 | } |
| 809 | |
| 810 | llvm::SmallString<32> ConstructFixedPointLiteral(llvm::APFixedPoint Val, |
| 811 | llvm::StringRef Suffix) { |
| 812 | if (Val.isSigned() && Val == llvm::APFixedPoint::getMin(Sema: Val.getSemantics())) { |
| 813 | // When representing the min value of a signed fixed point type in source |
| 814 | // code, we cannot simply write `-<lowest value>`. For example, the min |
| 815 | // value of a `short _Fract` cannot be written as `-1.0hr`. This is because |
| 816 | // the parser will read this (and really any negative numerical literal) as |
| 817 | // a UnaryOperator that owns a FixedPointLiteral with a positive value |
| 818 | // rather than just a FixedPointLiteral with a negative value. Compiling |
| 819 | // `-1.0hr` results in an overflow to the maximal value of that fixed point |
| 820 | // type. The correct way to represent a signed min value is to instead split |
| 821 | // it into two halves, like `(-0.5hr-0.5hr)` which is what the standard |
| 822 | // defines SFRACT_MIN as. |
| 823 | llvm::SmallString<32> Literal; |
| 824 | Literal.push_back(Elt: '('); |
| 825 | llvm::SmallString<32> HalfStr = |
| 826 | ConstructFixedPointLiteral(Val: Val.shr(Amt: 1), Suffix); |
| 827 | Literal += HalfStr; |
| 828 | Literal += HalfStr; |
| 829 | Literal.push_back(Elt: ')'); |
| 830 | return Literal; |
| 831 | } |
| 832 | |
| 833 | llvm::SmallString<32> Str(Val.toString()); |
| 834 | Str += Suffix; |
| 835 | return Str; |
| 836 | } |
| 837 | |
| 838 | void DefineFixedPointMacros(const TargetInfo &TI, MacroBuilder &Builder, |
| 839 | llvm::StringRef TypeName, llvm::StringRef Suffix, |
| 840 | unsigned Width, unsigned Scale, bool Signed) { |
| 841 | // Saturation doesn't affect the size or scale of a fixed point type, so we |
| 842 | // don't need it here. |
| 843 | llvm::FixedPointSemantics FXSema( |
| 844 | Width, Scale, Signed, /*IsSaturated=*/false, |
| 845 | !Signed && TI.doUnsignedFixedPointTypesHavePadding()); |
| 846 | llvm::SmallString<32> MacroPrefix("__" ); |
| 847 | MacroPrefix += TypeName; |
| 848 | Builder.defineMacro(Name: MacroPrefix + "_EPSILON__" , |
| 849 | Value: ConstructFixedPointLiteral( |
| 850 | Val: llvm::APFixedPoint::getEpsilon(Sema: FXSema), Suffix)); |
| 851 | Builder.defineMacro(Name: MacroPrefix + "_FBIT__" , Value: Twine(Scale)); |
| 852 | Builder.defineMacro( |
| 853 | Name: MacroPrefix + "_MAX__" , |
| 854 | Value: ConstructFixedPointLiteral(Val: llvm::APFixedPoint::getMax(Sema: FXSema), Suffix)); |
| 855 | |
| 856 | // ISO/IEC TR 18037:2008 doesn't specify MIN macros for unsigned types since |
| 857 | // they're all just zero. |
| 858 | if (Signed) |
| 859 | Builder.defineMacro( |
| 860 | Name: MacroPrefix + "_MIN__" , |
| 861 | Value: ConstructFixedPointLiteral(Val: llvm::APFixedPoint::getMin(Sema: FXSema), Suffix)); |
| 862 | } |
| 863 | |
| 864 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 865 | const LangOptions &LangOpts, |
| 866 | const FrontendOptions &FEOpts, |
| 867 | const PreprocessorOptions &PPOpts, |
| 868 | MacroBuilder &Builder) { |
| 869 | // Compiler version introspection macros. |
| 870 | Builder.defineMacro(Name: "__llvm__" ); // LLVM Backend |
| 871 | Builder.defineMacro(Name: "__clang__" ); // Clang Frontend |
| 872 | #define TOSTR2(X) #X |
| 873 | #define TOSTR(X) TOSTR2(X) |
| 874 | Builder.defineMacro(Name: "__clang_major__" , TOSTR(CLANG_VERSION_MAJOR)); |
| 875 | Builder.defineMacro(Name: "__clang_minor__" , TOSTR(CLANG_VERSION_MINOR)); |
| 876 | Builder.defineMacro(Name: "__clang_patchlevel__" , TOSTR(CLANG_VERSION_PATCHLEVEL)); |
| 877 | #undef TOSTR |
| 878 | #undef TOSTR2 |
| 879 | Builder.defineMacro(Name: "__clang_version__" , |
| 880 | Value: "\"" CLANG_VERSION_STRING " " |
| 881 | + getClangFullRepositoryVersion() + "\"" ); |
| 882 | |
| 883 | if (LangOpts.GNUCVersion != 0) { |
| 884 | // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes |
| 885 | // 40201. |
| 886 | unsigned GNUCMajor = LangOpts.GNUCVersion / 100 / 100; |
| 887 | unsigned GNUCMinor = LangOpts.GNUCVersion / 100 % 100; |
| 888 | unsigned GNUCPatch = LangOpts.GNUCVersion % 100; |
| 889 | Builder.defineMacro(Name: "__GNUC__" , Value: Twine(GNUCMajor)); |
| 890 | Builder.defineMacro(Name: "__GNUC_MINOR__" , Value: Twine(GNUCMinor)); |
| 891 | Builder.defineMacro(Name: "__GNUC_PATCHLEVEL__" , Value: Twine(GNUCPatch)); |
| 892 | Builder.defineMacro(Name: "__GXX_ABI_VERSION" , Value: "1002" ); |
| 893 | |
| 894 | if (LangOpts.CPlusPlus) { |
| 895 | Builder.defineMacro(Name: "__GNUG__" , Value: Twine(GNUCMajor)); |
| 896 | Builder.defineMacro(Name: "__GXX_WEAK__" ); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // Define macros for the C11 / C++11 memory orderings |
| 901 | Builder.defineMacro(Name: "__ATOMIC_RELAXED" , Value: "0" ); |
| 902 | Builder.defineMacro(Name: "__ATOMIC_CONSUME" , Value: "1" ); |
| 903 | Builder.defineMacro(Name: "__ATOMIC_ACQUIRE" , Value: "2" ); |
| 904 | Builder.defineMacro(Name: "__ATOMIC_RELEASE" , Value: "3" ); |
| 905 | Builder.defineMacro(Name: "__ATOMIC_ACQ_REL" , Value: "4" ); |
| 906 | Builder.defineMacro(Name: "__ATOMIC_SEQ_CST" , Value: "5" ); |
| 907 | |
| 908 | // Define macros for the clang atomic scopes. |
| 909 | Builder.defineMacro(Name: "__MEMORY_SCOPE_SYSTEM" , Value: "0" ); |
| 910 | Builder.defineMacro(Name: "__MEMORY_SCOPE_DEVICE" , Value: "1" ); |
| 911 | Builder.defineMacro(Name: "__MEMORY_SCOPE_WRKGRP" , Value: "2" ); |
| 912 | Builder.defineMacro(Name: "__MEMORY_SCOPE_WVFRNT" , Value: "3" ); |
| 913 | Builder.defineMacro(Name: "__MEMORY_SCOPE_SINGLE" , Value: "4" ); |
| 914 | |
| 915 | // Define macros for the OpenCL memory scope. |
| 916 | // The values should match AtomicScopeOpenCLModel::ID enum. |
| 917 | static_assert( |
| 918 | static_cast<unsigned>(AtomicScopeOpenCLModel::WorkGroup) == 1 && |
| 919 | static_cast<unsigned>(AtomicScopeOpenCLModel::Device) == 2 && |
| 920 | static_cast<unsigned>(AtomicScopeOpenCLModel::AllSVMDevices) == 3 && |
| 921 | static_cast<unsigned>(AtomicScopeOpenCLModel::SubGroup) == 4, |
| 922 | "Invalid OpenCL memory scope enum definition" ); |
| 923 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_WORK_ITEM" , Value: "0" ); |
| 924 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_WORK_GROUP" , Value: "1" ); |
| 925 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_DEVICE" , Value: "2" ); |
| 926 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_ALL_SVM_DEVICES" , Value: "3" ); |
| 927 | Builder.defineMacro(Name: "__OPENCL_MEMORY_SCOPE_SUB_GROUP" , Value: "4" ); |
| 928 | |
| 929 | // Define macros for floating-point data classes, used in __builtin_isfpclass. |
| 930 | Builder.defineMacro(Name: "__FPCLASS_SNAN" , Value: "0x0001" ); |
| 931 | Builder.defineMacro(Name: "__FPCLASS_QNAN" , Value: "0x0002" ); |
| 932 | Builder.defineMacro(Name: "__FPCLASS_NEGINF" , Value: "0x0004" ); |
| 933 | Builder.defineMacro(Name: "__FPCLASS_NEGNORMAL" , Value: "0x0008" ); |
| 934 | Builder.defineMacro(Name: "__FPCLASS_NEGSUBNORMAL" , Value: "0x0010" ); |
| 935 | Builder.defineMacro(Name: "__FPCLASS_NEGZERO" , Value: "0x0020" ); |
| 936 | Builder.defineMacro(Name: "__FPCLASS_POSZERO" , Value: "0x0040" ); |
| 937 | Builder.defineMacro(Name: "__FPCLASS_POSSUBNORMAL" , Value: "0x0080" ); |
| 938 | Builder.defineMacro(Name: "__FPCLASS_POSNORMAL" , Value: "0x0100" ); |
| 939 | Builder.defineMacro(Name: "__FPCLASS_POSINF" , Value: "0x0200" ); |
| 940 | |
| 941 | // Support for #pragma redefine_extname (Sun compatibility) |
| 942 | Builder.defineMacro(Name: "__PRAGMA_REDEFINE_EXTNAME" , Value: "1" ); |
| 943 | |
| 944 | // Previously this macro was set to a string aiming to achieve compatibility |
| 945 | // with GCC 4.2.1. Now, just return the full Clang version |
| 946 | Builder.defineMacro(Name: "__VERSION__" , Value: "\"" + |
| 947 | Twine(getClangFullCPPVersion()) + "\"" ); |
| 948 | |
| 949 | // Initialize language-specific preprocessor defines. |
| 950 | |
| 951 | // Standard conforming mode? |
| 952 | if (!LangOpts.GNUMode && !LangOpts.MSVCCompat) |
| 953 | Builder.defineMacro(Name: "__STRICT_ANSI__" ); |
| 954 | |
| 955 | if (LangOpts.GNUCVersion && LangOpts.CPlusPlus11) |
| 956 | Builder.defineMacro(Name: "__GXX_EXPERIMENTAL_CXX0X__" ); |
| 957 | |
| 958 | if (TI.getTriple().isWindowsGNUEnvironment()) { |
| 959 | // Set ABI defining macros for libstdc++ for MinGW, where the |
| 960 | // default in libstdc++ differs from the defaults for this target. |
| 961 | Builder.defineMacro(Name: "__GXX_TYPEINFO_EQUALITY_INLINE" , Value: "0" ); |
| 962 | } |
| 963 | |
| 964 | if (LangOpts.ObjC) { |
| 965 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
| 966 | Builder.defineMacro(Name: "__OBJC2__" ); |
| 967 | |
| 968 | if (LangOpts.ObjCExceptions) |
| 969 | Builder.defineMacro(Name: "OBJC_ZEROCOST_EXCEPTIONS" ); |
| 970 | } |
| 971 | |
| 972 | if (LangOpts.getGC() != LangOptions::NonGC) |
| 973 | Builder.defineMacro(Name: "__OBJC_GC__" ); |
| 974 | |
| 975 | if (LangOpts.ObjCRuntime.isNeXTFamily()) |
| 976 | Builder.defineMacro(Name: "__NEXT_RUNTIME__" ); |
| 977 | |
| 978 | if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::GNUstep) { |
| 979 | auto version = LangOpts.ObjCRuntime.getVersion(); |
| 980 | // Don't rely on the tuple argument, because we can be asked to target |
| 981 | // later ABIs than we actually support, so clamp these values to those |
| 982 | // currently supported |
| 983 | if (version >= VersionTuple(2, 0)) |
| 984 | Builder.defineMacro(Name: "__OBJC_GNUSTEP_RUNTIME_ABI__" , Value: "20" ); |
| 985 | else |
| 986 | Builder.defineMacro( |
| 987 | Name: "__OBJC_GNUSTEP_RUNTIME_ABI__" , |
| 988 | Value: "1" + Twine(std::min(a: 8U, b: version.getMinor().value_or(u: 0)))); |
| 989 | } |
| 990 | |
| 991 | if (LangOpts.ObjCRuntime.getKind() == ObjCRuntime::ObjFW) { |
| 992 | VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); |
| 993 | unsigned minor = tuple.getMinor().value_or(u: 0); |
| 994 | unsigned subminor = tuple.getSubminor().value_or(u: 0); |
| 995 | Builder.defineMacro(Name: "__OBJFW_RUNTIME_ABI__" , |
| 996 | Value: Twine(tuple.getMajor() * 10000 + minor * 100 + |
| 997 | subminor)); |
| 998 | } |
| 999 | |
| 1000 | Builder.defineMacro(Name: "IBOutlet" , Value: "__attribute__((iboutlet))" ); |
| 1001 | Builder.defineMacro(Name: "IBOutletCollection(ClassName)" , |
| 1002 | Value: "__attribute__((iboutletcollection(ClassName)))" ); |
| 1003 | Builder.defineMacro(Name: "IBAction" , Value: "void)__attribute__((ibaction)" ); |
| 1004 | Builder.defineMacro(Name: "IBInspectable" , Value: "" ); |
| 1005 | Builder.defineMacro(Name: "IB_DESIGNABLE" , Value: "" ); |
| 1006 | } |
| 1007 | |
| 1008 | // Define a macro that describes the Objective-C boolean type even for C |
| 1009 | // and C++ since BOOL can be used from non Objective-C code. |
| 1010 | Builder.defineMacro(Name: "__OBJC_BOOL_IS_BOOL" , |
| 1011 | Value: Twine(TI.useSignedCharForObjCBool() ? "0" : "1" )); |
| 1012 | |
| 1013 | if (LangOpts.CPlusPlus) |
| 1014 | InitializeCPlusPlusFeatureTestMacros(LangOpts, Builder); |
| 1015 | |
| 1016 | // darwin_constant_cfstrings controls this. This is also dependent |
| 1017 | // on other things like the runtime I believe. This is set even for C code. |
| 1018 | if (!LangOpts.NoConstantCFStrings) |
| 1019 | Builder.defineMacro(Name: "__CONSTANT_CFSTRINGS__" ); |
| 1020 | |
| 1021 | if (LangOpts.ObjC) |
| 1022 | Builder.defineMacro(Name: "OBJC_NEW_PROPERTIES" ); |
| 1023 | |
| 1024 | if (LangOpts.PascalStrings) |
| 1025 | Builder.defineMacro(Name: "__PASCAL_STRINGS__" ); |
| 1026 | |
| 1027 | if (LangOpts.Blocks) { |
| 1028 | Builder.defineMacro(Name: "__block" , Value: "__attribute__((__blocks__(byref)))" ); |
| 1029 | Builder.defineMacro(Name: "__BLOCKS__" ); |
| 1030 | } |
| 1031 | |
| 1032 | if (!LangOpts.MSVCCompat && LangOpts.Exceptions) |
| 1033 | Builder.defineMacro(Name: "__EXCEPTIONS" ); |
| 1034 | if (LangOpts.GNUCVersion && LangOpts.RTTI) |
| 1035 | Builder.defineMacro(Name: "__GXX_RTTI" ); |
| 1036 | |
| 1037 | if (LangOpts.hasSjLjExceptions()) |
| 1038 | Builder.defineMacro(Name: "__USING_SJLJ_EXCEPTIONS__" ); |
| 1039 | else if (LangOpts.hasSEHExceptions()) |
| 1040 | Builder.defineMacro(Name: "__SEH__" ); |
| 1041 | else if (LangOpts.hasDWARFExceptions() && |
| 1042 | (TI.getTriple().isThumb() || TI.getTriple().isARM())) |
| 1043 | Builder.defineMacro(Name: "__ARM_DWARF_EH__" ); |
| 1044 | else if (LangOpts.hasWasmExceptions() && TI.getTriple().isWasm()) |
| 1045 | Builder.defineMacro(Name: "__WASM_EXCEPTIONS__" ); |
| 1046 | |
| 1047 | if (LangOpts.Deprecated) |
| 1048 | Builder.defineMacro(Name: "__DEPRECATED" ); |
| 1049 | |
| 1050 | if (!LangOpts.MSVCCompat && LangOpts.CPlusPlus) |
| 1051 | Builder.defineMacro(Name: "__private_extern__" , Value: "extern" ); |
| 1052 | |
| 1053 | if (LangOpts.MicrosoftExt) { |
| 1054 | if (LangOpts.WChar) { |
| 1055 | // wchar_t supported as a keyword. |
| 1056 | Builder.defineMacro(Name: "_WCHAR_T_DEFINED" ); |
| 1057 | Builder.defineMacro(Name: "_NATIVE_WCHAR_T_DEFINED" ); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // Macros to help identify the narrow and wide character sets |
| 1062 | // FIXME: clang currently ignores -fexec-charset=. If this changes, |
| 1063 | // then this may need to be updated. |
| 1064 | Builder.defineMacro(Name: "__clang_literal_encoding__" , Value: "\"UTF-8\"" ); |
| 1065 | if (TI.getTypeWidth(T: TI.getWCharType()) >= 32) { |
| 1066 | // FIXME: 32-bit wchar_t signals UTF-32. This may change |
| 1067 | // if -fwide-exec-charset= is ever supported. |
| 1068 | Builder.defineMacro(Name: "__clang_wide_literal_encoding__" , Value: "\"UTF-32\"" ); |
| 1069 | } else { |
| 1070 | // FIXME: Less-than 32-bit wchar_t generally means UTF-16 |
| 1071 | // (e.g., Windows, 32-bit IBM). This may need to be |
| 1072 | // updated if -fwide-exec-charset= is ever supported. |
| 1073 | Builder.defineMacro(Name: "__clang_wide_literal_encoding__" , Value: "\"UTF-16\"" ); |
| 1074 | } |
| 1075 | |
| 1076 | if (LangOpts.Optimize) |
| 1077 | Builder.defineMacro(Name: "__OPTIMIZE__" ); |
| 1078 | if (LangOpts.OptimizeSize) |
| 1079 | Builder.defineMacro(Name: "__OPTIMIZE_SIZE__" ); |
| 1080 | |
| 1081 | if (LangOpts.FastMath) |
| 1082 | Builder.defineMacro(Name: "__FAST_MATH__" ); |
| 1083 | |
| 1084 | // Initialize target-specific preprocessor defines. |
| 1085 | |
| 1086 | // __BYTE_ORDER__ was added in GCC 4.6. It's analogous |
| 1087 | // to the macro __BYTE_ORDER (no trailing underscores) |
| 1088 | // from glibc's <endian.h> header. |
| 1089 | // We don't support the PDP-11 as a target, but include |
| 1090 | // the define so it can still be compared against. |
| 1091 | Builder.defineMacro(Name: "__ORDER_LITTLE_ENDIAN__" , Value: "1234" ); |
| 1092 | Builder.defineMacro(Name: "__ORDER_BIG_ENDIAN__" , Value: "4321" ); |
| 1093 | Builder.defineMacro(Name: "__ORDER_PDP_ENDIAN__" , Value: "3412" ); |
| 1094 | if (TI.isBigEndian()) { |
| 1095 | Builder.defineMacro(Name: "__BYTE_ORDER__" , Value: "__ORDER_BIG_ENDIAN__" ); |
| 1096 | Builder.defineMacro(Name: "__BIG_ENDIAN__" ); |
| 1097 | } else { |
| 1098 | Builder.defineMacro(Name: "__BYTE_ORDER__" , Value: "__ORDER_LITTLE_ENDIAN__" ); |
| 1099 | Builder.defineMacro(Name: "__LITTLE_ENDIAN__" ); |
| 1100 | } |
| 1101 | |
| 1102 | if (TI.getPointerWidth(AddrSpace: LangAS::Default) == 64 && TI.getLongWidth() == 64 && |
| 1103 | TI.getIntWidth() == 32) { |
| 1104 | Builder.defineMacro(Name: "_LP64" ); |
| 1105 | Builder.defineMacro(Name: "__LP64__" ); |
| 1106 | } |
| 1107 | |
| 1108 | if (TI.getPointerWidth(AddrSpace: LangAS::Default) == 32 && TI.getLongWidth() == 32 && |
| 1109 | TI.getIntWidth() == 32) { |
| 1110 | Builder.defineMacro(Name: "_ILP32" ); |
| 1111 | Builder.defineMacro(Name: "__ILP32__" ); |
| 1112 | } |
| 1113 | |
| 1114 | // Define type sizing macros based on the target properties. |
| 1115 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far" ); |
| 1116 | Builder.defineMacro(Name: "__CHAR_BIT__" , Value: Twine(TI.getCharWidth())); |
| 1117 | |
| 1118 | // The macro is specifying the number of bits in the width, not the number of |
| 1119 | // bits the object requires for its in-memory representation, which is what |
| 1120 | // getBoolWidth() will return. The bool/_Bool data type is only ever one bit |
| 1121 | // wide. See C23 6.2.6.2p2 for the rules in C. Note that |
| 1122 | // C++23 [basic.fundamental]p10 allows an implementation-defined value |
| 1123 | // representation for bool; when lowering to LLVM, Clang represents bool as an |
| 1124 | // i8 in memory but as an i1 when the value is needed, so '1' is also correct |
| 1125 | // for C++. |
| 1126 | Builder.defineMacro(Name: "__BOOL_WIDTH__" , Value: "1" ); |
| 1127 | Builder.defineMacro(Name: "__SHRT_WIDTH__" , Value: Twine(TI.getShortWidth())); |
| 1128 | Builder.defineMacro(Name: "__INT_WIDTH__" , Value: Twine(TI.getIntWidth())); |
| 1129 | Builder.defineMacro(Name: "__LONG_WIDTH__" , Value: Twine(TI.getLongWidth())); |
| 1130 | Builder.defineMacro(Name: "__LLONG_WIDTH__" , Value: Twine(TI.getLongLongWidth())); |
| 1131 | |
| 1132 | size_t BitIntMaxWidth = TI.getMaxBitIntWidth(); |
| 1133 | assert(BitIntMaxWidth <= llvm::IntegerType::MAX_INT_BITS && |
| 1134 | "Target defined a max bit width larger than LLVM can support!" ); |
| 1135 | assert(BitIntMaxWidth >= TI.getLongLongWidth() && |
| 1136 | "Target defined a max bit width smaller than the C standard allows!" ); |
| 1137 | Builder.defineMacro(Name: "__BITINT_MAXWIDTH__" , Value: Twine(BitIntMaxWidth)); |
| 1138 | |
| 1139 | DefineTypeSize(MacroName: "__SCHAR_MAX__" , Ty: TargetInfo::SignedChar, TI, Builder); |
| 1140 | DefineTypeSize(MacroName: "__SHRT_MAX__" , Ty: TargetInfo::SignedShort, TI, Builder); |
| 1141 | DefineTypeSize(MacroName: "__INT_MAX__" , Ty: TargetInfo::SignedInt, TI, Builder); |
| 1142 | DefineTypeSize(MacroName: "__LONG_MAX__" , Ty: TargetInfo::SignedLong, TI, Builder); |
| 1143 | DefineTypeSize(MacroName: "__LONG_LONG_MAX__" , Ty: TargetInfo::SignedLongLong, TI, Builder); |
| 1144 | DefineTypeSizeAndWidth(Prefix: "__WCHAR" , Ty: TI.getWCharType(), TI, Builder); |
| 1145 | DefineTypeSizeAndWidth(Prefix: "__WINT" , Ty: TI.getWIntType(), TI, Builder); |
| 1146 | DefineTypeSizeAndWidth(Prefix: "__INTMAX" , Ty: TI.getIntMaxType(), TI, Builder); |
| 1147 | DefineTypeSizeAndWidth(Prefix: "__SIZE" , Ty: TI.getSizeType(), TI, Builder); |
| 1148 | |
| 1149 | DefineTypeSizeAndWidth(Prefix: "__UINTMAX" , Ty: TI.getUIntMaxType(), TI, Builder); |
| 1150 | DefineTypeSizeAndWidth(Prefix: "__PTRDIFF" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), TI, |
| 1151 | Builder); |
| 1152 | DefineTypeSizeAndWidth(Prefix: "__INTPTR" , Ty: TI.getIntPtrType(), TI, Builder); |
| 1153 | DefineTypeSizeAndWidth(Prefix: "__UINTPTR" , Ty: TI.getUIntPtrType(), TI, Builder); |
| 1154 | |
| 1155 | DefineTypeSizeof(MacroName: "__SIZEOF_DOUBLE__" , BitWidth: TI.getDoubleWidth(), TI, Builder); |
| 1156 | DefineTypeSizeof(MacroName: "__SIZEOF_FLOAT__" , BitWidth: TI.getFloatWidth(), TI, Builder); |
| 1157 | DefineTypeSizeof(MacroName: "__SIZEOF_INT__" , BitWidth: TI.getIntWidth(), TI, Builder); |
| 1158 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG__" , BitWidth: TI.getLongWidth(), TI, Builder); |
| 1159 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG_DOUBLE__" ,BitWidth: TI.getLongDoubleWidth(),TI,Builder); |
| 1160 | DefineTypeSizeof(MacroName: "__SIZEOF_LONG_LONG__" , BitWidth: TI.getLongLongWidth(), TI, Builder); |
| 1161 | DefineTypeSizeof(MacroName: "__SIZEOF_POINTER__" , BitWidth: TI.getPointerWidth(AddrSpace: LangAS::Default), |
| 1162 | TI, Builder); |
| 1163 | DefineTypeSizeof(MacroName: "__SIZEOF_SHORT__" , BitWidth: TI.getShortWidth(), TI, Builder); |
| 1164 | DefineTypeSizeof(MacroName: "__SIZEOF_PTRDIFF_T__" , |
| 1165 | BitWidth: TI.getTypeWidth(T: TI.getPtrDiffType(AddrSpace: LangAS::Default)), TI, |
| 1166 | Builder); |
| 1167 | DefineTypeSizeof(MacroName: "__SIZEOF_SIZE_T__" , |
| 1168 | BitWidth: TI.getTypeWidth(T: TI.getSizeType()), TI, Builder); |
| 1169 | DefineTypeSizeof(MacroName: "__SIZEOF_WCHAR_T__" , |
| 1170 | BitWidth: TI.getTypeWidth(T: TI.getWCharType()), TI, Builder); |
| 1171 | DefineTypeSizeof(MacroName: "__SIZEOF_WINT_T__" , |
| 1172 | BitWidth: TI.getTypeWidth(T: TI.getWIntType()), TI, Builder); |
| 1173 | if (TI.hasInt128Type()) |
| 1174 | DefineTypeSizeof(MacroName: "__SIZEOF_INT128__" , BitWidth: 128, TI, Builder); |
| 1175 | |
| 1176 | DefineType(MacroName: "__INTMAX_TYPE__" , Ty: TI.getIntMaxType(), Builder); |
| 1177 | DefineFmt(LangOpts, Prefix: "__INTMAX" , Ty: TI.getIntMaxType(), TI, Builder); |
| 1178 | StringRef ConstSuffix(TI.getTypeConstantSuffix(T: TI.getIntMaxType())); |
| 1179 | Builder.defineMacro(Name: "__INTMAX_C_SUFFIX__" , Value: ConstSuffix); |
| 1180 | Builder.defineMacro(Name: "__INTMAX_C(c)" , |
| 1181 | Value: ConstSuffix.size() ? Twine("c##" ) + ConstSuffix : "c" ); |
| 1182 | DefineType(MacroName: "__UINTMAX_TYPE__" , Ty: TI.getUIntMaxType(), Builder); |
| 1183 | DefineFmt(LangOpts, Prefix: "__UINTMAX" , Ty: TI.getUIntMaxType(), TI, Builder); |
| 1184 | ConstSuffix = TI.getTypeConstantSuffix(T: TI.getUIntMaxType()); |
| 1185 | Builder.defineMacro(Name: "__UINTMAX_C_SUFFIX__" , Value: ConstSuffix); |
| 1186 | Builder.defineMacro(Name: "__UINTMAX_C(c)" , |
| 1187 | Value: ConstSuffix.size() ? Twine("c##" ) + ConstSuffix : "c" ); |
| 1188 | DefineType(MacroName: "__PTRDIFF_TYPE__" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), Builder); |
| 1189 | DefineFmt(LangOpts, Prefix: "__PTRDIFF" , Ty: TI.getPtrDiffType(AddrSpace: LangAS::Default), TI, |
| 1190 | Builder); |
| 1191 | DefineType(MacroName: "__INTPTR_TYPE__" , Ty: TI.getIntPtrType(), Builder); |
| 1192 | DefineFmt(LangOpts, Prefix: "__INTPTR" , Ty: TI.getIntPtrType(), TI, Builder); |
| 1193 | DefineType(MacroName: "__SIZE_TYPE__" , Ty: TI.getSizeType(), Builder); |
| 1194 | DefineFmt(LangOpts, Prefix: "__SIZE" , Ty: TI.getSizeType(), TI, Builder); |
| 1195 | DefineType(MacroName: "__WCHAR_TYPE__" , Ty: TI.getWCharType(), Builder); |
| 1196 | DefineType(MacroName: "__WINT_TYPE__" , Ty: TI.getWIntType(), Builder); |
| 1197 | DefineTypeSizeAndWidth(Prefix: "__SIG_ATOMIC" , Ty: TI.getSigAtomicType(), TI, Builder); |
| 1198 | if (LangOpts.C23) |
| 1199 | DefineType(MacroName: "__CHAR8_TYPE__" , Ty: TI.UnsignedChar, Builder); |
| 1200 | DefineType(MacroName: "__CHAR16_TYPE__" , Ty: TI.getChar16Type(), Builder); |
| 1201 | DefineType(MacroName: "__CHAR32_TYPE__" , Ty: TI.getChar32Type(), Builder); |
| 1202 | |
| 1203 | DefineType(MacroName: "__UINTPTR_TYPE__" , Ty: TI.getUIntPtrType(), Builder); |
| 1204 | DefineFmt(LangOpts, Prefix: "__UINTPTR" , Ty: TI.getUIntPtrType(), TI, Builder); |
| 1205 | |
| 1206 | // The C standard requires the width of uintptr_t and intptr_t to be the same, |
| 1207 | // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1. |
| 1208 | assert(TI.getTypeWidth(TI.getUIntPtrType()) == |
| 1209 | TI.getTypeWidth(TI.getIntPtrType()) && |
| 1210 | "uintptr_t and intptr_t have different widths?" ); |
| 1211 | assert(TI.getTypeWidth(TI.getUIntMaxType()) == |
| 1212 | TI.getTypeWidth(TI.getIntMaxType()) && |
| 1213 | "uintmax_t and intmax_t have different widths?" ); |
| 1214 | |
| 1215 | if (LangOpts.FixedPoint) { |
| 1216 | // Each unsigned type has the same width as their signed type. |
| 1217 | DefineFixedPointMacros(TI, Builder, TypeName: "SFRACT" , Suffix: "HR" , Width: TI.getShortFractWidth(), |
| 1218 | Scale: TI.getShortFractScale(), /*Signed=*/true); |
| 1219 | DefineFixedPointMacros(TI, Builder, TypeName: "USFRACT" , Suffix: "UHR" , |
| 1220 | Width: TI.getShortFractWidth(), |
| 1221 | Scale: TI.getUnsignedShortFractScale(), /*Signed=*/false); |
| 1222 | DefineFixedPointMacros(TI, Builder, TypeName: "FRACT" , Suffix: "R" , Width: TI.getFractWidth(), |
| 1223 | Scale: TI.getFractScale(), /*Signed=*/true); |
| 1224 | DefineFixedPointMacros(TI, Builder, TypeName: "UFRACT" , Suffix: "UR" , Width: TI.getFractWidth(), |
| 1225 | Scale: TI.getUnsignedFractScale(), /*Signed=*/false); |
| 1226 | DefineFixedPointMacros(TI, Builder, TypeName: "LFRACT" , Suffix: "LR" , Width: TI.getLongFractWidth(), |
| 1227 | Scale: TI.getLongFractScale(), /*Signed=*/true); |
| 1228 | DefineFixedPointMacros(TI, Builder, TypeName: "ULFRACT" , Suffix: "ULR" , |
| 1229 | Width: TI.getLongFractWidth(), |
| 1230 | Scale: TI.getUnsignedLongFractScale(), /*Signed=*/false); |
| 1231 | DefineFixedPointMacros(TI, Builder, TypeName: "SACCUM" , Suffix: "HK" , Width: TI.getShortAccumWidth(), |
| 1232 | Scale: TI.getShortAccumScale(), /*Signed=*/true); |
| 1233 | DefineFixedPointMacros(TI, Builder, TypeName: "USACCUM" , Suffix: "UHK" , |
| 1234 | Width: TI.getShortAccumWidth(), |
| 1235 | Scale: TI.getUnsignedShortAccumScale(), /*Signed=*/false); |
| 1236 | DefineFixedPointMacros(TI, Builder, TypeName: "ACCUM" , Suffix: "K" , Width: TI.getAccumWidth(), |
| 1237 | Scale: TI.getAccumScale(), /*Signed=*/true); |
| 1238 | DefineFixedPointMacros(TI, Builder, TypeName: "UACCUM" , Suffix: "UK" , Width: TI.getAccumWidth(), |
| 1239 | Scale: TI.getUnsignedAccumScale(), /*Signed=*/false); |
| 1240 | DefineFixedPointMacros(TI, Builder, TypeName: "LACCUM" , Suffix: "LK" , Width: TI.getLongAccumWidth(), |
| 1241 | Scale: TI.getLongAccumScale(), /*Signed=*/true); |
| 1242 | DefineFixedPointMacros(TI, Builder, TypeName: "ULACCUM" , Suffix: "ULK" , |
| 1243 | Width: TI.getLongAccumWidth(), |
| 1244 | Scale: TI.getUnsignedLongAccumScale(), /*Signed=*/false); |
| 1245 | |
| 1246 | Builder.defineMacro(Name: "__SACCUM_IBIT__" , Value: Twine(TI.getShortAccumIBits())); |
| 1247 | Builder.defineMacro(Name: "__USACCUM_IBIT__" , |
| 1248 | Value: Twine(TI.getUnsignedShortAccumIBits())); |
| 1249 | Builder.defineMacro(Name: "__ACCUM_IBIT__" , Value: Twine(TI.getAccumIBits())); |
| 1250 | Builder.defineMacro(Name: "__UACCUM_IBIT__" , Value: Twine(TI.getUnsignedAccumIBits())); |
| 1251 | Builder.defineMacro(Name: "__LACCUM_IBIT__" , Value: Twine(TI.getLongAccumIBits())); |
| 1252 | Builder.defineMacro(Name: "__ULACCUM_IBIT__" , |
| 1253 | Value: Twine(TI.getUnsignedLongAccumIBits())); |
| 1254 | } |
| 1255 | |
| 1256 | if (TI.hasFloat16Type()) |
| 1257 | DefineFloatMacros(Builder, Prefix: "FLT16" , Sem: &TI.getHalfFormat(), Ext: "F16" ); |
| 1258 | DefineFloatMacros(Builder, Prefix: "FLT" , Sem: &TI.getFloatFormat(), Ext: "F" ); |
| 1259 | DefineFloatMacros(Builder, Prefix: "DBL" , Sem: &TI.getDoubleFormat(), Ext: "" ); |
| 1260 | DefineFloatMacros(Builder, Prefix: "LDBL" , Sem: &TI.getLongDoubleFormat(), Ext: "L" ); |
| 1261 | |
| 1262 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
| 1263 | Builder.defineMacro(Name: "__POINTER_WIDTH__" , |
| 1264 | Value: Twine((int)TI.getPointerWidth(AddrSpace: LangAS::Default))); |
| 1265 | |
| 1266 | // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc. |
| 1267 | Builder.defineMacro(Name: "__BIGGEST_ALIGNMENT__" , |
| 1268 | Value: Twine(TI.getSuitableAlign() / TI.getCharWidth()) ); |
| 1269 | |
| 1270 | if (!LangOpts.CharIsSigned) |
| 1271 | Builder.defineMacro(Name: "__CHAR_UNSIGNED__" ); |
| 1272 | |
| 1273 | if (!TargetInfo::isTypeSigned(T: TI.getWCharType())) |
| 1274 | Builder.defineMacro(Name: "__WCHAR_UNSIGNED__" ); |
| 1275 | |
| 1276 | if (!TargetInfo::isTypeSigned(T: TI.getWIntType())) |
| 1277 | Builder.defineMacro(Name: "__WINT_UNSIGNED__" ); |
| 1278 | |
| 1279 | // Define exact-width integer types for stdint.h |
| 1280 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedChar, TI, Builder); |
| 1281 | |
| 1282 | if (TI.getShortWidth() > TI.getCharWidth()) |
| 1283 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedShort, TI, Builder); |
| 1284 | |
| 1285 | if (TI.getIntWidth() > TI.getShortWidth()) |
| 1286 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedInt, TI, Builder); |
| 1287 | |
| 1288 | if (TI.getLongWidth() > TI.getIntWidth()) |
| 1289 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedLong, TI, Builder); |
| 1290 | |
| 1291 | if (TI.getLongLongWidth() > TI.getLongWidth()) |
| 1292 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::SignedLongLong, TI, Builder); |
| 1293 | |
| 1294 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedChar, TI, Builder); |
| 1295 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedChar, TI, Builder); |
| 1296 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedChar, TI, Builder); |
| 1297 | |
| 1298 | if (TI.getShortWidth() > TI.getCharWidth()) { |
| 1299 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedShort, TI, Builder); |
| 1300 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedShort, TI, Builder); |
| 1301 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedShort, TI, Builder); |
| 1302 | } |
| 1303 | |
| 1304 | if (TI.getIntWidth() > TI.getShortWidth()) { |
| 1305 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedInt, TI, Builder); |
| 1306 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedInt, TI, Builder); |
| 1307 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedInt, TI, Builder); |
| 1308 | } |
| 1309 | |
| 1310 | if (TI.getLongWidth() > TI.getIntWidth()) { |
| 1311 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedLong, TI, Builder); |
| 1312 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedLong, TI, Builder); |
| 1313 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedLong, TI, Builder); |
| 1314 | } |
| 1315 | |
| 1316 | if (TI.getLongLongWidth() > TI.getLongWidth()) { |
| 1317 | DefineExactWidthIntType(LangOpts, Ty: TargetInfo::UnsignedLongLong, TI, |
| 1318 | Builder); |
| 1319 | DefineExactWidthIntTypeSize(Ty: TargetInfo::UnsignedLongLong, TI, Builder); |
| 1320 | DefineExactWidthIntTypeSize(Ty: TargetInfo::SignedLongLong, TI, Builder); |
| 1321 | } |
| 1322 | |
| 1323 | DefineLeastWidthIntType(LangOpts, TypeWidth: 8, IsSigned: true, TI, Builder); |
| 1324 | DefineLeastWidthIntType(LangOpts, TypeWidth: 8, IsSigned: false, TI, Builder); |
| 1325 | DefineLeastWidthIntType(LangOpts, TypeWidth: 16, IsSigned: true, TI, Builder); |
| 1326 | DefineLeastWidthIntType(LangOpts, TypeWidth: 16, IsSigned: false, TI, Builder); |
| 1327 | DefineLeastWidthIntType(LangOpts, TypeWidth: 32, IsSigned: true, TI, Builder); |
| 1328 | DefineLeastWidthIntType(LangOpts, TypeWidth: 32, IsSigned: false, TI, Builder); |
| 1329 | DefineLeastWidthIntType(LangOpts, TypeWidth: 64, IsSigned: true, TI, Builder); |
| 1330 | DefineLeastWidthIntType(LangOpts, TypeWidth: 64, IsSigned: false, TI, Builder); |
| 1331 | |
| 1332 | DefineFastIntType(LangOpts, TypeWidth: 8, IsSigned: true, TI, Builder); |
| 1333 | DefineFastIntType(LangOpts, TypeWidth: 8, IsSigned: false, TI, Builder); |
| 1334 | DefineFastIntType(LangOpts, TypeWidth: 16, IsSigned: true, TI, Builder); |
| 1335 | DefineFastIntType(LangOpts, TypeWidth: 16, IsSigned: false, TI, Builder); |
| 1336 | DefineFastIntType(LangOpts, TypeWidth: 32, IsSigned: true, TI, Builder); |
| 1337 | DefineFastIntType(LangOpts, TypeWidth: 32, IsSigned: false, TI, Builder); |
| 1338 | DefineFastIntType(LangOpts, TypeWidth: 64, IsSigned: true, TI, Builder); |
| 1339 | DefineFastIntType(LangOpts, TypeWidth: 64, IsSigned: false, TI, Builder); |
| 1340 | |
| 1341 | Builder.defineMacro(Name: "__USER_LABEL_PREFIX__" , Value: TI.getUserLabelPrefix()); |
| 1342 | |
| 1343 | if (!LangOpts.MathErrno) |
| 1344 | Builder.defineMacro(Name: "__NO_MATH_ERRNO__" ); |
| 1345 | |
| 1346 | if (LangOpts.FastMath || (LangOpts.NoHonorInfs && LangOpts.NoHonorNaNs)) |
| 1347 | Builder.defineMacro(Name: "__FINITE_MATH_ONLY__" , Value: "1" ); |
| 1348 | else |
| 1349 | Builder.defineMacro(Name: "__FINITE_MATH_ONLY__" , Value: "0" ); |
| 1350 | |
| 1351 | if (LangOpts.GNUCVersion) { |
| 1352 | if (LangOpts.GNUInline || LangOpts.CPlusPlus) |
| 1353 | Builder.defineMacro(Name: "__GNUC_GNU_INLINE__" ); |
| 1354 | else |
| 1355 | Builder.defineMacro(Name: "__GNUC_STDC_INLINE__" ); |
| 1356 | |
| 1357 | // The value written by __atomic_test_and_set. |
| 1358 | // FIXME: This is target-dependent. |
| 1359 | Builder.defineMacro(Name: "__GCC_ATOMIC_TEST_AND_SET_TRUEVAL" , Value: "1" ); |
| 1360 | } |
| 1361 | |
| 1362 | // GCC defines these macros in both C and C++ modes despite them being needed |
| 1363 | // mostly for STL implementations in C++. |
| 1364 | auto [Destructive, Constructive] = TI.hardwareInterferenceSizes(); |
| 1365 | Builder.defineMacro(Name: "__GCC_DESTRUCTIVE_SIZE" , Value: Twine(Destructive)); |
| 1366 | Builder.defineMacro(Name: "__GCC_CONSTRUCTIVE_SIZE" , Value: Twine(Constructive)); |
| 1367 | // We need to use push_macro to allow users to redefine these macros from the |
| 1368 | // command line with -D and not issue a -Wmacro-redefined warning. |
| 1369 | Builder.append(Str: "#pragma push_macro(\"__GCC_DESTRUCTIVE_SIZE\")" ); |
| 1370 | Builder.append(Str: "#pragma push_macro(\"__GCC_CONSTRUCTIVE_SIZE\")" ); |
| 1371 | |
| 1372 | auto addLockFreeMacros = [&](const llvm::Twine &Prefix) { |
| 1373 | // Used by libc++ and libstdc++ to implement ATOMIC_<foo>_LOCK_FREE. |
| 1374 | #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ |
| 1375 | Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \ |
| 1376 | getLockFreeValue(TI.get##Type##Width(), TI)); |
| 1377 | DEFINE_LOCK_FREE_MACRO(BOOL, Bool); |
| 1378 | DEFINE_LOCK_FREE_MACRO(CHAR, Char); |
| 1379 | // char8_t has the same representation / width as unsigned |
| 1380 | // char in C++ and is a typedef for unsigned char in C23 |
| 1381 | if (LangOpts.Char8 || LangOpts.C23) |
| 1382 | DEFINE_LOCK_FREE_MACRO(CHAR8_T, Char); |
| 1383 | DEFINE_LOCK_FREE_MACRO(CHAR16_T, Char16); |
| 1384 | DEFINE_LOCK_FREE_MACRO(CHAR32_T, Char32); |
| 1385 | DEFINE_LOCK_FREE_MACRO(WCHAR_T, WChar); |
| 1386 | DEFINE_LOCK_FREE_MACRO(SHORT, Short); |
| 1387 | DEFINE_LOCK_FREE_MACRO(INT, Int); |
| 1388 | DEFINE_LOCK_FREE_MACRO(LONG, Long); |
| 1389 | DEFINE_LOCK_FREE_MACRO(LLONG, LongLong); |
| 1390 | Builder.defineMacro( |
| 1391 | Name: Prefix + "POINTER_LOCK_FREE" , |
| 1392 | Value: getLockFreeValue(TypeWidth: TI.getPointerWidth(AddrSpace: LangAS::Default), TI)); |
| 1393 | #undef DEFINE_LOCK_FREE_MACRO |
| 1394 | }; |
| 1395 | addLockFreeMacros("__CLANG_ATOMIC_" ); |
| 1396 | if (LangOpts.GNUCVersion) |
| 1397 | addLockFreeMacros("__GCC_ATOMIC_" ); |
| 1398 | |
| 1399 | if (LangOpts.NoInlineDefine) |
| 1400 | Builder.defineMacro(Name: "__NO_INLINE__" ); |
| 1401 | |
| 1402 | if (unsigned PICLevel = LangOpts.PICLevel) { |
| 1403 | Builder.defineMacro(Name: "__PIC__" , Value: Twine(PICLevel)); |
| 1404 | Builder.defineMacro(Name: "__pic__" , Value: Twine(PICLevel)); |
| 1405 | if (LangOpts.PIE) { |
| 1406 | Builder.defineMacro(Name: "__PIE__" , Value: Twine(PICLevel)); |
| 1407 | Builder.defineMacro(Name: "__pie__" , Value: Twine(PICLevel)); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | // Macros to control C99 numerics and <float.h> |
| 1412 | Builder.defineMacro(Name: "__FLT_RADIX__" , Value: "2" ); |
| 1413 | Builder.defineMacro(Name: "__DECIMAL_DIG__" , Value: "__LDBL_DECIMAL_DIG__" ); |
| 1414 | |
| 1415 | if (LangOpts.getStackProtector() == LangOptions::SSPOn) |
| 1416 | Builder.defineMacro(Name: "__SSP__" ); |
| 1417 | else if (LangOpts.getStackProtector() == LangOptions::SSPStrong) |
| 1418 | Builder.defineMacro(Name: "__SSP_STRONG__" , Value: "2" ); |
| 1419 | else if (LangOpts.getStackProtector() == LangOptions::SSPReq) |
| 1420 | Builder.defineMacro(Name: "__SSP_ALL__" , Value: "3" ); |
| 1421 | |
| 1422 | if (PPOpts.SetUpStaticAnalyzer) |
| 1423 | Builder.defineMacro(Name: "__clang_analyzer__" ); |
| 1424 | |
| 1425 | if (LangOpts.FastRelaxedMath) |
| 1426 | Builder.defineMacro(Name: "__FAST_RELAXED_MATH__" ); |
| 1427 | |
| 1428 | if (FEOpts.ProgramAction == frontend::RewriteObjC || |
| 1429 | LangOpts.getGC() != LangOptions::NonGC) { |
| 1430 | Builder.defineMacro(Name: "__weak" , Value: "__attribute__((objc_gc(weak)))" ); |
| 1431 | Builder.defineMacro(Name: "__strong" , Value: "__attribute__((objc_gc(strong)))" ); |
| 1432 | Builder.defineMacro(Name: "__autoreleasing" , Value: "" ); |
| 1433 | Builder.defineMacro(Name: "__unsafe_unretained" , Value: "" ); |
| 1434 | } else if (LangOpts.ObjC) { |
| 1435 | Builder.defineMacro(Name: "__weak" , Value: "__attribute__((objc_ownership(weak)))" ); |
| 1436 | Builder.defineMacro(Name: "__strong" , Value: "__attribute__((objc_ownership(strong)))" ); |
| 1437 | Builder.defineMacro(Name: "__autoreleasing" , |
| 1438 | Value: "__attribute__((objc_ownership(autoreleasing)))" ); |
| 1439 | Builder.defineMacro(Name: "__unsafe_unretained" , |
| 1440 | Value: "__attribute__((objc_ownership(none)))" ); |
| 1441 | } |
| 1442 | |
| 1443 | // On Darwin, there are __double_underscored variants of the type |
| 1444 | // nullability qualifiers. |
| 1445 | if (TI.getTriple().isOSDarwin()) { |
| 1446 | Builder.defineMacro(Name: "__nonnull" , Value: "_Nonnull" ); |
| 1447 | Builder.defineMacro(Name: "__null_unspecified" , Value: "_Null_unspecified" ); |
| 1448 | Builder.defineMacro(Name: "__nullable" , Value: "_Nullable" ); |
| 1449 | } |
| 1450 | |
| 1451 | // Add a macro to differentiate between regular iOS/tvOS/watchOS targets and |
| 1452 | // the corresponding simulator targets. |
| 1453 | if (TI.getTriple().isOSDarwin() && TI.getTriple().isSimulatorEnvironment()) |
| 1454 | Builder.defineMacro(Name: "__APPLE_EMBEDDED_SIMULATOR__" , Value: "1" ); |
| 1455 | |
| 1456 | // OpenMP definition |
| 1457 | // OpenMP 2.2: |
| 1458 | // In implementations that support a preprocessor, the _OPENMP |
| 1459 | // macro name is defined to have the decimal value yyyymm where |
| 1460 | // yyyy and mm are the year and the month designations of the |
| 1461 | // version of the OpenMP API that the implementation support. |
| 1462 | if (!LangOpts.OpenMPSimd) { |
| 1463 | switch (LangOpts.OpenMP) { |
| 1464 | case 0: |
| 1465 | break; |
| 1466 | case 31: |
| 1467 | Builder.defineMacro(Name: "_OPENMP" , Value: "201107" ); |
| 1468 | break; |
| 1469 | case 40: |
| 1470 | Builder.defineMacro(Name: "_OPENMP" , Value: "201307" ); |
| 1471 | break; |
| 1472 | case 45: |
| 1473 | Builder.defineMacro(Name: "_OPENMP" , Value: "201511" ); |
| 1474 | break; |
| 1475 | case 50: |
| 1476 | Builder.defineMacro(Name: "_OPENMP" , Value: "201811" ); |
| 1477 | break; |
| 1478 | case 51: |
| 1479 | Builder.defineMacro(Name: "_OPENMP" , Value: "202011" ); |
| 1480 | break; |
| 1481 | case 52: |
| 1482 | Builder.defineMacro(Name: "_OPENMP" , Value: "202111" ); |
| 1483 | break; |
| 1484 | case 60: |
| 1485 | Builder.defineMacro(Name: "_OPENMP" , Value: "202411" ); |
| 1486 | break; |
| 1487 | default: // case 51: |
| 1488 | // Default version is OpenMP 5.1 |
| 1489 | Builder.defineMacro(Name: "_OPENMP" , Value: "202011" ); |
| 1490 | break; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | // CUDA device path compilaton |
| 1495 | if (LangOpts.CUDAIsDevice && !LangOpts.HIP) { |
| 1496 | // The CUDA_ARCH value is set for the GPU target specified in the NVPTX |
| 1497 | // backend's target defines. |
| 1498 | Builder.defineMacro(Name: "__CUDA_ARCH__" ); |
| 1499 | } |
| 1500 | |
| 1501 | // We need to communicate this to our CUDA/HIP header wrapper, which in turn |
| 1502 | // informs the proper CUDA/HIP headers of this choice. |
| 1503 | if (LangOpts.GPUDeviceApproxTranscendentals) |
| 1504 | Builder.defineMacro(Name: "__CLANG_GPU_APPROX_TRANSCENDENTALS__" ); |
| 1505 | |
| 1506 | // Define a macro indicating that the source file is being compiled with a |
| 1507 | // SYCL device compiler which doesn't produce host binary. |
| 1508 | if (LangOpts.SYCLIsDevice) { |
| 1509 | Builder.defineMacro(Name: "__SYCL_DEVICE_ONLY__" , Value: "1" ); |
| 1510 | } |
| 1511 | |
| 1512 | // OpenCL definitions. |
| 1513 | if (LangOpts.OpenCL) { |
| 1514 | InitializeOpenCLFeatureTestMacros(TI, Opts: LangOpts, Builder); |
| 1515 | |
| 1516 | if (TI.getTriple().isSPIR() || TI.getTriple().isSPIRV()) |
| 1517 | Builder.defineMacro(Name: "__IMAGE_SUPPORT__" ); |
| 1518 | } |
| 1519 | |
| 1520 | if (TI.hasInt128Type() && LangOpts.CPlusPlus && LangOpts.GNUMode) { |
| 1521 | // For each extended integer type, g++ defines a macro mapping the |
| 1522 | // index of the type (0 in this case) in some list of extended types |
| 1523 | // to the type. |
| 1524 | Builder.defineMacro(Name: "__GLIBCXX_TYPE_INT_N_0" , Value: "__int128" ); |
| 1525 | Builder.defineMacro(Name: "__GLIBCXX_BITSIZE_INT_N_0" , Value: "128" ); |
| 1526 | } |
| 1527 | |
| 1528 | // ELF targets define __ELF__ |
| 1529 | if (TI.getTriple().isOSBinFormatELF()) |
| 1530 | Builder.defineMacro(Name: "__ELF__" ); |
| 1531 | |
| 1532 | // Target OS macro definitions. |
| 1533 | if (PPOpts.DefineTargetOSMacros) { |
| 1534 | const llvm::Triple &Triple = TI.getTriple(); |
| 1535 | #define TARGET_OS(Name, Predicate) \ |
| 1536 | Builder.defineMacro(#Name, (Predicate) ? "1" : "0"); |
| 1537 | #include "clang/Basic/TargetOSMacros.def" |
| 1538 | #undef TARGET_OS |
| 1539 | } |
| 1540 | |
| 1541 | // Get other target #defines. |
| 1542 | TI.getTargetDefines(Opts: LangOpts, Builder); |
| 1543 | } |
| 1544 | |
| 1545 | static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, |
| 1546 | MacroBuilder &Builder) { |
| 1547 | if (CodeGenOpts.hasProfileInstr()) |
| 1548 | Builder.defineMacro(Name: "__LLVM_INSTR_PROFILE_GENERATE" ); |
| 1549 | |
| 1550 | if (CodeGenOpts.hasProfileIRUse() || CodeGenOpts.hasProfileClangUse()) |
| 1551 | Builder.defineMacro(Name: "__LLVM_INSTR_PROFILE_USE" ); |
| 1552 | } |
| 1553 | |
| 1554 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 1555 | /// environment ready to process a single file. |
| 1556 | void clang::InitializePreprocessor(Preprocessor &PP, |
| 1557 | const PreprocessorOptions &InitOpts, |
| 1558 | const PCHContainerReader &PCHContainerRdr, |
| 1559 | const FrontendOptions &FEOpts, |
| 1560 | const CodeGenOptions &CodeGenOpts) { |
| 1561 | const LangOptions &LangOpts = PP.getLangOpts(); |
| 1562 | std::string PredefineBuffer; |
| 1563 | PredefineBuffer.reserve(res_arg: 4080); |
| 1564 | llvm::raw_string_ostream Predefines(PredefineBuffer); |
| 1565 | MacroBuilder Builder(Predefines); |
| 1566 | |
| 1567 | // Emit line markers for various builtin sections of the file. The 3 here |
| 1568 | // marks <built-in> as being a system header, which suppresses warnings when |
| 1569 | // the same macro is defined multiple times. |
| 1570 | Builder.append(Str: "# 1 \"<built-in>\" 3" ); |
| 1571 | |
| 1572 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 1573 | if (InitOpts.UsePredefines) { |
| 1574 | // FIXME: This will create multiple definitions for most of the predefined |
| 1575 | // macros. This is not the right way to handle this. |
| 1576 | if ((LangOpts.CUDA || LangOpts.isTargetDevice()) && PP.getAuxTargetInfo()) |
| 1577 | InitializePredefinedMacros(TI: *PP.getAuxTargetInfo(), LangOpts, FEOpts, |
| 1578 | PPOpts: PP.getPreprocessorOpts(), Builder); |
| 1579 | |
| 1580 | InitializePredefinedMacros(TI: PP.getTargetInfo(), LangOpts, FEOpts, |
| 1581 | PPOpts: PP.getPreprocessorOpts(), Builder); |
| 1582 | |
| 1583 | // Install definitions to make Objective-C++ ARC work well with various |
| 1584 | // C++ Standard Library implementations. |
| 1585 | if (LangOpts.ObjC && LangOpts.CPlusPlus && |
| 1586 | (LangOpts.ObjCAutoRefCount || LangOpts.ObjCWeak)) { |
| 1587 | switch (InitOpts.ObjCXXARCStandardLibrary) { |
| 1588 | case ARCXX_nolib: |
| 1589 | case ARCXX_libcxx: |
| 1590 | break; |
| 1591 | |
| 1592 | case ARCXX_libstdcxx: |
| 1593 | AddObjCXXARCLibstdcxxDefines(LangOpts, Builder); |
| 1594 | break; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | // Even with predefines off, some macros are still predefined. |
| 1600 | // These should all be defined in the preprocessor according to the |
| 1601 | // current language configuration. |
| 1602 | InitializeStandardPredefinedMacros(TI: PP.getTargetInfo(), LangOpts: PP.getLangOpts(), |
| 1603 | FEOpts, Builder); |
| 1604 | |
| 1605 | // The PGO instrumentation profile macros are driven by options |
| 1606 | // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, |
| 1607 | // hence they are not guarded by InitOpts.UsePredefines. |
| 1608 | InitializePGOProfileMacros(CodeGenOpts, Builder); |
| 1609 | |
| 1610 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 1611 | // that they come from the command line. |
| 1612 | Builder.append(Str: "# 1 \"<command line>\" 1" ); |
| 1613 | |
| 1614 | // Process #define's and #undef's in the order they are given. |
| 1615 | for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { |
| 1616 | if (InitOpts.Macros[i].second) // isUndef |
| 1617 | Builder.undefineMacro(Name: InitOpts.Macros[i].first); |
| 1618 | else |
| 1619 | DefineBuiltinMacro(Builder, Macro: InitOpts.Macros[i].first, |
| 1620 | Diags&: PP.getDiagnostics()); |
| 1621 | } |
| 1622 | |
| 1623 | // Exit the command line and go back to <built-in> (2 is LC_LEAVE). |
| 1624 | Builder.append(Str: "# 1 \"<built-in>\" 2" ); |
| 1625 | |
| 1626 | // If -imacros are specified, include them now. These are processed before |
| 1627 | // any -include directives. |
| 1628 | for (unsigned i = 0, e = InitOpts.MacroIncludes.size(); i != e; ++i) |
| 1629 | AddImplicitIncludeMacros(Builder, File: InitOpts.MacroIncludes[i]); |
| 1630 | |
| 1631 | // Process -include-pch/-include-pth directives. |
| 1632 | if (!InitOpts.ImplicitPCHInclude.empty()) |
| 1633 | AddImplicitIncludePCH(Builder, PP, PCHContainerRdr, |
| 1634 | ImplicitIncludePCH: InitOpts.ImplicitPCHInclude); |
| 1635 | |
| 1636 | // Process -include directives. |
| 1637 | for (unsigned i = 0, e = InitOpts.Includes.size(); i != e; ++i) { |
| 1638 | const std::string &Path = InitOpts.Includes[i]; |
| 1639 | AddImplicitInclude(Builder, File: Path); |
| 1640 | } |
| 1641 | |
| 1642 | // Instruct the preprocessor to skip the preamble. |
| 1643 | PP.setSkipMainFilePreamble(Bytes: InitOpts.PrecompiledPreambleBytes.first, |
| 1644 | StartOfLine: InitOpts.PrecompiledPreambleBytes.second); |
| 1645 | |
| 1646 | // Copy PredefinedBuffer into the Preprocessor. |
| 1647 | PP.setPredefines(std::move(PredefineBuffer)); |
| 1648 | |
| 1649 | // Match gcc behavior regarding gnu-line-directive diagnostics, assuming that |
| 1650 | // '-x <*>-cpp-output' is analogous to '-fpreprocessed'. |
| 1651 | if (FEOpts.DashX.isPreprocessed()) { |
| 1652 | PP.getDiagnostics().setSeverity(Diag: diag::ext_pp_gnu_line_directive, |
| 1653 | Map: diag::Severity::Ignored, Loc: SourceLocation()); |
| 1654 | } |
| 1655 | } |
| 1656 | |