Coverage Report

Created: 2022-01-18 06:27

/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
50.7k
                      VersionTuple &PlatformMinVersion) {
25
50.7k
  Builder.defineMacro("__APPLE_CC__", "6000");
26
50.7k
  Builder.defineMacro("__APPLE__");
27
50.7k
  Builder.defineMacro("__STDC_NO_THREADS__");
28
29
  // AddressSanitizer doesn't play well with source fortification, which is on
30
  // by default on Darwin.
31
50.7k
  if (Opts.Sanitize.has(SanitizerKind::Address))
32
41
    Builder.defineMacro("_FORTIFY_SOURCE", "0");
33
34
  // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
35
50.7k
  if (!Opts.ObjC) {
36
    // __weak is always defined, for use in blocks and with objc pointers.
37
29.1k
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
38
29.1k
    Builder.defineMacro("__strong", "");
39
29.1k
    Builder.defineMacro("__unsafe_unretained", "");
40
29.1k
  }
41
42
50.7k
  if (Opts.Static)
43
7
    Builder.defineMacro("__STATIC__");
44
50.7k
  else
45
50.7k
    Builder.defineMacro("__DYNAMIC__");
46
47
50.7k
  if (Opts.POSIXThreads)
48
2
    Builder.defineMacro("_REENTRANT");
49
50
  // Get the platform type and version number from the triple.
51
50.7k
  VersionTuple OsVersion;
52
50.7k
  if (Triple.isMacOSX()) {
53
50.3k
    Triple.getMacOSXVersion(OsVersion);
54
50.3k
    PlatformName = "macos";
55
50.3k
  } else {
56
406
    OsVersion = Triple.getOSVersion();
57
406
    PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
58
406
    if (PlatformName == "ios" && 
Triple.isMacCatalystEnvironment()282
)
59
28
      PlatformName = "maccatalyst";
60
406
  }
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
50.7k
  if (PlatformName == "win32") {
66
0
    PlatformMinVersion = OsVersion;
67
0
    return;
68
0
  }
69
70
  // Set the appropriate OS version define.
71
50.7k
  if (Triple.isiOS()) {
72
326
    assert(OsVersion < VersionTuple(100) && "Invalid version!");
73
0
    char Str[7];
74
326
    if (OsVersion.getMajor() < 10) {
75
225
      Str[0] = '0' + OsVersion.getMajor();
76
225
      Str[1] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
77
225
      Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
78
225
      Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
79
225
      Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
80
225
      Str[5] = '\0';
81
225
    } 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().getValueOr(0) / 10);
86
101
      Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
87
101
      Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
88
101
      Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
89
101
      Str[6] = '\0';
90
101
    }
91
326
    if (Triple.isTvOS())
92
44
      Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
93
282
    else
94
282
      Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
95
282
                          Str);
96
97
50.4k
  } 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().getValueOr(0) / 10);
102
55
    Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
103
55
    Str[3] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
104
55
    Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
105
55
    Str[5] = '\0';
106
55
    Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
107
50.4k
  } else if (Triple.isMacOSX()) {
108
    // Note that the Driver allows versions which aren't representable in the
109
    // define (because we only get a single digit for the minor and micro
110
    // revision numbers). So, we limit them to the maximum representable
111
    // version.
112
50.3k
    assert(OsVersion < VersionTuple(100) && "Invalid version!");
113
0
    char Str[7];
114
50.3k
    if (OsVersion < VersionTuple(10, 10)) {
115
3.07k
      Str[0] = '0' + (OsVersion.getMajor() / 10);
116
3.07k
      Str[1] = '0' + (OsVersion.getMajor() % 10);
117
3.07k
      Str[2] = '0' + std::min(OsVersion.getMinor().getValueOr(0), 9U);
118
3.07k
      Str[3] = '0' + std::min(OsVersion.getSubminor().getValueOr(0), 9U);
119
3.07k
      Str[4] = '\0';
120
47.3k
    } else {
121
      // Handle versions > 10.9.
122
47.3k
      Str[0] = '0' + (OsVersion.getMajor() / 10);
123
47.3k
      Str[1] = '0' + (OsVersion.getMajor() % 10);
124
47.3k
      Str[2] = '0' + (OsVersion.getMinor().getValueOr(0) / 10);
125
47.3k
      Str[3] = '0' + (OsVersion.getMinor().getValueOr(0) % 10);
126
47.3k
      Str[4] = '0' + (OsVersion.getSubminor().getValueOr(0) / 10);
127
47.3k
      Str[5] = '0' + (OsVersion.getSubminor().getValueOr(0) % 10);
128
47.3k
      Str[6] = '\0';
129
47.3k
    }
130
50.3k
    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
131
50.3k
  }
132
133
  // Tell users about the kernel if there is one.
134
50.7k
  if (Triple.isOSDarwin())
135
50.7k
    Builder.defineMacro("__MACH__");
136
137
50.7k
  PlatformMinVersion = OsVersion;
138
50.7k
}
139
140
static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
141
171
                            MacroBuilder &Builder) {
142
171
  DefineStd(Builder, "WIN32", Opts);
143
171
  DefineStd(Builder, "WINNT", Opts);
144
171
  if (Triple.isArch64Bit()) {
145
55
    DefineStd(Builder, "WIN64", Opts);
146
55
    Builder.defineMacro("__MINGW64__");
147
55
  }
148
171
  Builder.defineMacro("__MSVCRT__");
149
171
  Builder.defineMacro("__MINGW32__");
150
171
  addCygMingDefines(Opts, Builder);
151
171
}
152
153
8.73k
static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
154
8.73k
  if (Opts.CPlusPlus) {
155
7.40k
    if (Opts.RTTIData)
156
7.29k
      Builder.defineMacro("_CPPRTTI");
157
158
7.40k
    if (Opts.CXXExceptions)
159
6.69k
      Builder.defineMacro("_CPPUNWIND");
160
7.40k
  }
161
162
8.73k
  if (Opts.Bool)
163
7.41k
    Builder.defineMacro("__BOOL_DEFINED");
164
165
8.73k
  if (!Opts.CharIsSigned)
166
2
    Builder.defineMacro("_CHAR_UNSIGNED");
167
168
  // FIXME: POSIXThreads isn't exactly the option this should be defined for,
169
  //        but it works for now.
170
8.73k
  if (Opts.POSIXThreads)
171
0
    Builder.defineMacro("_MT");
172
173
8.73k
  if (Opts.MSCompatibilityVersion) {
174
7.70k
    Builder.defineMacro("_MSC_VER",
175
7.70k
                        Twine(Opts.MSCompatibilityVersion / 100000));
176
7.70k
    Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
177
    // FIXME We cannot encode the revision information into 32-bits
178
7.70k
    Builder.defineMacro("_MSC_BUILD", Twine(1));
179
180
7.70k
    if (Opts.CPlusPlus11 && 
Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)5.60k
)
181
5.57k
      Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
182
183
7.70k
    if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
184
7.65k
      if (Opts.CPlusPlus2b)
185
1
        Builder.defineMacro("_MSVC_LANG", "202004L");
186
7.65k
      else if (Opts.CPlusPlus20)
187
1.35k
        Builder.defineMacro("_MSVC_LANG", "202002L");
188
6.29k
      else if (Opts.CPlusPlus17)
189
1.34k
        Builder.defineMacro("_MSVC_LANG", "201703L");
190
4.94k
      else if (Opts.CPlusPlus14)
191
1.53k
        Builder.defineMacro("_MSVC_LANG", "201402L");
192
7.65k
    }
193
7.70k
  }
194
195
8.73k
  if (Opts.MicrosoftExt) {
196
8.08k
    Builder.defineMacro("_MSC_EXTENSIONS");
197
198
8.08k
    if (Opts.CPlusPlus11) {
199
5.84k
      Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
200
5.84k
      Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
201
5.84k
      Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
202
5.84k
    }
203
8.08k
  }
204
205
8.73k
  Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
206
8.73k
  Builder.defineMacro("__STDC_NO_THREADS__");
207
208
  // Starting with VS 2022 17.1, MSVC predefines the below macro to inform
209
  // users of the execution character set defined at compile time.
210
  // The value given is the Windows Code Page Identifier:
211
  // https://docs.microsoft.com/en-us/windows/win32/intl/code-page-identifiers
212
  //
213
  // Clang currently only supports UTF-8, so we'll use 65001
214
8.73k
  Builder.defineMacro("_MSVC_EXECUTION_CHARACTER_SET", "65001");
215
8.73k
}
216
217
void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
218
8.94k
                       MacroBuilder &Builder) {
219
8.94k
  Builder.defineMacro("_WIN32");
220
8.94k
  if (Triple.isArch64Bit())
221
8.16k
    Builder.defineMacro("_WIN64");
222
8.94k
  if (Triple.isWindowsGNUEnvironment())
223
171
    addMinGWDefines(Triple, Opts, Builder);
224
8.77k
  else if (Triple.isKnownWindowsMSVCEnvironment() ||
225
8.77k
           
(45
Triple.isWindowsItaniumEnvironment()45
&&
Opts.MSVCCompat42
))
226
8.73k
    addVisualCDefines(Opts, Builder);
227
8.94k
}
228
229
} // namespace targets
230
} // namespace clang