Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Targets/OSTargets.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- OSTargets.cpp - Implement OS target feature support --------------===//
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 OS specific TargetInfo types.
10
//===----------------------------------------------------------------------===//
11
12
#include "OSTargets.h"
13
#include "clang/Basic/MacroBuilder.h"
14
#include "llvm/ADT/StringRef.h"
15
16
using namespace clang;
17
using namespace clang::targets;
18
19
namespace clang {
20
namespace targets {
21
22
void getDarwinDefines(MacroBuilder &Builder, const LangOptions &Opts,
23
                      const llvm::Triple &Triple, StringRef &PlatformName,
24
52.9k
                      VersionTuple &PlatformMinVersion) {
25
52.9k
  Builder.defineMacro("__APPLE_CC__", "6000");
26
52.9k
  Builder.defineMacro("__APPLE__");
27
52.9k
  Builder.defineMacro("__STDC_NO_THREADS__");
28
29
  // AddressSanitizer doesn't play well with source fortification, which is on
30
  // by default on Darwin.
31
52.9k
  if (Opts.Sanitize.has(SanitizerKind::Address))
32
40
    Builder.defineMacro("_FORTIFY_SOURCE", "0");
33
34
  // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
35
52.9k
  if (!Opts.ObjC) {
36
    // __weak is always defined, for use in blocks and with objc pointers.
37
30.7k
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
38
30.7k
    Builder.defineMacro("__strong", "");
39
30.7k
    Builder.defineMacro("__unsafe_unretained", "");
40
30.7k
  }
41
42
52.9k
  if (Opts.Static)
43
7
    Builder.defineMacro("__STATIC__");
44
52.9k
  else
45
52.9k
    Builder.defineMacro("__DYNAMIC__");
46
47
52.9k
  if (Opts.POSIXThreads)
48
2
    Builder.defineMacro("_REENTRANT");
49
50
  // Get the platform type and version number from the triple.
51
52.9k
  VersionTuple OsVersion;
52
52.9k
  if (Triple.isMacOSX()) {
53
52.5k
    Triple.getMacOSXVersion(OsVersion);
54
52.5k
    PlatformName = "macos";
55
52.5k
  } else {
56
418
    OsVersion = Triple.getOSVersion();
57
418
    PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
58
418
    if (PlatformName == "ios" && 
Triple.isMacCatalystEnvironment()285
)
59
28
      PlatformName = "maccatalyst";
60
418
  }
61
62
  // If -target arch-pc-win32-macho option specified, we're
63
  // generating code for Win32 ABI. No need to emit
64
  // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
65
52.9k
  if (PlatformName == "win32") {
66
0
    PlatformMinVersion = OsVersion;
67
0
    return;
68
0
  }
69
70
  // Set the appropriate OS version define.
71
52.9k
  if (Triple.isiOS()) {
72
329
    assert(OsVersion < VersionTuple(100) && "Invalid version!");
73
0
    char Str[7];
74
329
    if (OsVersion.getMajor() < 10) {
75
228
      Str[0] = '0' + OsVersion.getMajor();
76
228
      Str[1] = '0' + (OsVersion.getMinor().value_or(0) / 10);
77
228
      Str[2] = '0' + (OsVersion.getMinor().value_or(0) % 10);
78
228
      Str[3] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
79
228
      Str[4] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
80
228
      Str[5] = '\0';
81
228
    } else {
82
      // Handle versions >= 10.
83
101
      Str[0] = '0' + (OsVersion.getMajor() / 10);
84
101
      Str[1] = '0' + (OsVersion.getMajor() % 10);
85
101
      Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
86
101
      Str[3] = '0' + (OsVersion.getMinor().value_or(0) % 10);
87
101
      Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
88
101
      Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
89
101
      Str[6] = '\0';
90
101
    }
91
329
    if (Triple.isTvOS())
92
44
      Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
93
285
    else
94
285
      Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
95
285
                          Str);
96
97
52.5k
  } else if (Triple.isWatchOS()) {
98
55
    assert(OsVersion < VersionTuple(10) && "Invalid version!");
99
0
    char Str[6];
100
55
    Str[0] = '0' + OsVersion.getMajor();
101
55
    Str[1] = '0' + (OsVersion.getMinor().value_or(0) / 10);
102
55
    Str[2] = '0' + (OsVersion.getMinor().value_or(0) % 10);
103
55
    Str[3] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
104
55
    Str[4] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
105
55
    Str[5] = '\0';
106
55
    Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
107
52.5k
  } else if (Triple.isDriverKit()) {
108
9
    assert(OsVersion.getMajor() < 100 &&
109
9
           OsVersion.getMinor().value_or(0) < 100 &&
110
9
           OsVersion.getSubminor().value_or(0) < 100 && "Invalid version!");
111
0
    char Str[7];
112
9
    Str[0] = '0' + (OsVersion.getMajor() / 10);
113
9
    Str[1] = '0' + (OsVersion.getMajor() % 10);
114
9
    Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
115
9
    Str[3] = '0' + (OsVersion.getMinor().value_or(0) % 10);
116
9
    Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
117
9
    Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
118
9
    Str[6] = '\0';
119
9
    Builder.defineMacro("__ENVIRONMENT_DRIVERKIT_VERSION_MIN_REQUIRED__", Str);
120
52.5k
  } else if (Triple.isMacOSX()) {
121
    // Note that the Driver allows versions which aren't representable in the
122
    // define (because we only get a single digit for the minor and micro
123
    // revision numbers). So, we limit them to the maximum representable
124
    // version.
125
52.4k
    assert(OsVersion < VersionTuple(100) && "Invalid version!");
126
0
    char Str[7];
127
52.4k
    if (OsVersion < VersionTuple(10, 10)) {
128
3.12k
      Str[0] = '0' + (OsVersion.getMajor() / 10);
129
3.12k
      Str[1] = '0' + (OsVersion.getMajor() % 10);
130
3.12k
      Str[2] = '0' + std::min(OsVersion.getMinor().value_or(0), 9U);
131
3.12k
      Str[3] = '0' + std::min(OsVersion.getSubminor().value_or(0), 9U);
132
3.12k
      Str[4] = '\0';
133
49.3k
    } else {
134
      // Handle versions > 10.9.
135
49.3k
      Str[0] = '0' + (OsVersion.getMajor() / 10);
136
49.3k
      Str[1] = '0' + (OsVersion.getMajor() % 10);
137
49.3k
      Str[2] = '0' + (OsVersion.getMinor().value_or(0) / 10);
138
49.3k
      Str[3] = '0' + (OsVersion.getMinor().value_or(0) % 10);
139
49.3k
      Str[4] = '0' + (OsVersion.getSubminor().value_or(0) / 10);
140
49.3k
      Str[5] = '0' + (OsVersion.getSubminor().value_or(0) % 10);
141
49.3k
      Str[6] = '\0';
142
49.3k
    }
143
52.4k
    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
144
52.4k
  }
145
146
  // Tell users about the kernel if there is one.
147
52.9k
  if (Triple.isOSDarwin())
148
52.8k
    Builder.defineMacro("__MACH__");
149
150
52.9k
  PlatformMinVersion = OsVersion;
151
52.9k
}
152
153
static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
154
175
                            MacroBuilder &Builder) {
155
175
  DefineStd(Builder, "WIN32", Opts);
156
175
  DefineStd(Builder, "WINNT", Opts);
157
175
  if (Triple.isArch64Bit()) {
158
57
    DefineStd(Builder, "WIN64", Opts);
159
57
    Builder.defineMacro("__MINGW64__");
160
57
  }
161
175
  Builder.defineMacro("__MSVCRT__");
162
175
  Builder.defineMacro("__MINGW32__");
163
175
  addCygMingDefines(Opts, Builder);
164
175
}
165
166
8.79k
static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
167
8.79k
  if (Opts.CPlusPlus) {
168
7.44k
    if (Opts.RTTIData)
169
7.33k
      Builder.defineMacro("_CPPRTTI");
170
171
7.44k
    if (Opts.CXXExceptions)
172
6.71k
      Builder.defineMacro("_CPPUNWIND");
173
7.44k
  }
174
175
8.79k
  if (Opts.Bool)
176
7.45k
    Builder.defineMacro("__BOOL_DEFINED");
177
178
8.79k
  if (!Opts.CharIsSigned)
179
2
    Builder.defineMacro("_CHAR_UNSIGNED");
180
181
  // FIXME: POSIXThreads isn't exactly the option this should be defined for,
182
  //        but it works for now.
183
8.79k
  if (Opts.POSIXThreads)
184
0
    Builder.defineMacro("_MT");
185
186
8.79k
  if (Opts.MSCompatibilityVersion) {
187
7.73k
    Builder.defineMacro("_MSC_VER",
188
7.73k
                        Twine(Opts.MSCompatibilityVersion / 100000));
189
7.73k
    Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
190
    // FIXME We cannot encode the revision information into 32-bits
191
7.73k
    Builder.defineMacro("_MSC_BUILD", Twine(1));
192
193
7.73k
    if (Opts.CPlusPlus11 && 
Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)5.62k
)
194
5.60k
      Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
195
196
7.73k
    if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
197
7.67k
      if (Opts.CPlusPlus2b)
198
1
        Builder.defineMacro("_MSVC_LANG", "202004L");
199
7.67k
      else if (Opts.CPlusPlus20)
200
1.37k
        Builder.defineMacro("_MSVC_LANG", "202002L");
201
6.30k
      else if (Opts.CPlusPlus17)
202
1.35k
        Builder.defineMacro("_MSVC_LANG", "201703L");
203
4.95k
      else if (Opts.CPlusPlus14)
204
1.53k
        Builder.defineMacro("_MSVC_LANG", "201402L");
205
7.67k
    }
206
7.73k
  }
207
208
8.79k
  if (Opts.MicrosoftExt) {
209
8.12k
    Builder.defineMacro("_MSC_EXTENSIONS");
210
211
8.12k
    if (Opts.CPlusPlus11) {
212
5.87k
      Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
213
5.87k
      Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
214
5.87k
      Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
215
5.87k
    }
216
8.12k
  }
217
218
8.79k
  if (Opts.Kernel)
219
1
    Builder.defineMacro("_KERNEL_MODE");
220
221
8.79k
  Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
222
8.79k
  Builder.defineMacro("__STDC_NO_THREADS__");
223
224
  // Starting with VS 2022 17.1, MSVC predefines the below macro to inform
225
  // users of the execution character set defined at compile time.
226
  // The value given is the Windows Code Page Identifier:
227
  // https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
228
  //
229
  // Clang currently only supports UTF-8, so we'll use 65001
230
8.79k
  Builder.defineMacro("_MSVC_EXECUTION_CHARACTER_SET", "65001");
231
8.79k
}
232
233
void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
234
9.00k
                       MacroBuilder &Builder) {
235
9.00k
  Builder.defineMacro("_WIN32");
236
9.00k
  if (Triple.isArch64Bit())
237
8.20k
    Builder.defineMacro("_WIN64");
238
9.00k
  if (Triple.isWindowsGNUEnvironment())
239
175
    addMinGWDefines(Triple, Opts, Builder);
240
8.83k
  else if (Triple.isKnownWindowsMSVCEnvironment() ||
241
8.83k
           
(45
Triple.isWindowsItaniumEnvironment()45
&&
Opts.MSVCCompat42
))
242
8.79k
    addVisualCDefines(Opts, Builder);
243
9.00k
}
244
245
} // namespace targets
246
} // namespace clang