Coverage Report

Created: 2021-08-24 07:12

/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
59.9k
                      VersionTuple &PlatformMinVersion) {
25
59.9k
  Builder.defineMacro("__APPLE_CC__", "6000");
26
59.9k
  Builder.defineMacro("__APPLE__");
27
59.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
59.9k
  if (Opts.Sanitize.has(SanitizerKind::Address))
32
46
    Builder.defineMacro("_FORTIFY_SOURCE", "0");
33
34
  // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
35
59.9k
  if (!Opts.ObjC) {
36
    // __weak is always defined, for use in blocks and with objc pointers.
37
28.8k
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
38
28.8k
    Builder.defineMacro("__strong", "");
39
28.8k
    Builder.defineMacro("__unsafe_unretained", "");
40
28.8k
  }
41
42
59.9k
  if (Opts.Static)
43
7
    Builder.defineMacro("__STATIC__");
44
59.9k
  else
45
59.9k
    Builder.defineMacro("__DYNAMIC__");
46
47
59.9k
  if (Opts.POSIXThreads)
48
2
    Builder.defineMacro("_REENTRANT");
49
50
  // Get the platform type and version number from the triple.
51
59.9k
  unsigned Maj, Min, Rev;
52
59.9k
  if (Triple.isMacOSX()) {
53
59.5k
    Triple.getMacOSXVersion(Maj, Min, Rev);
54
59.5k
    PlatformName = "macos";
55
59.5k
  } else {
56
385
    Triple.getOSVersion(Maj, Min, Rev);
57
385
    PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
58
385
    if (PlatformName == "ios" && 
Triple.isMacCatalystEnvironment()263
)
59
28
      PlatformName = "maccatalyst";
60
385
  }
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
59.9k
  if (PlatformName == "win32") {
66
0
    PlatformMinVersion = VersionTuple(Maj, Min, Rev);
67
0
    return;
68
0
  }
69
70
  // Set the appropriate OS version define.
71
59.9k
  if (Triple.isiOS()) {
72
306
    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
73
0
    char Str[7];
74
306
    if (Maj < 10) {
75
222
      Str[0] = '0' + Maj;
76
222
      Str[1] = '0' + (Min / 10);
77
222
      Str[2] = '0' + (Min % 10);
78
222
      Str[3] = '0' + (Rev / 10);
79
222
      Str[4] = '0' + (Rev % 10);
80
222
      Str[5] = '\0';
81
222
    } else {
82
      // Handle versions >= 10.
83
84
      Str[0] = '0' + (Maj / 10);
84
84
      Str[1] = '0' + (Maj % 10);
85
84
      Str[2] = '0' + (Min / 10);
86
84
      Str[3] = '0' + (Min % 10);
87
84
      Str[4] = '0' + (Rev / 10);
88
84
      Str[5] = '0' + (Rev % 10);
89
84
      Str[6] = '\0';
90
84
    }
91
306
    if (Triple.isTvOS())
92
43
      Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
93
263
    else
94
263
      Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
95
263
                          Str);
96
97
59.6k
  } else if (Triple.isWatchOS()) {
98
54
    assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
99
0
    char Str[6];
100
54
    Str[0] = '0' + Maj;
101
54
    Str[1] = '0' + (Min / 10);
102
54
    Str[2] = '0' + (Min % 10);
103
54
    Str[3] = '0' + (Rev / 10);
104
54
    Str[4] = '0' + (Rev % 10);
105
54
    Str[5] = '\0';
106
54
    Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
107
59.5k
  } 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
59.5k
    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
113
0
    char Str[7];
114
59.5k
    if (
Maj < 1059.5k
|| (Maj == 10 &&
Min < 1059.5k
)) {
115
14.3k
      Str[0] = '0' + (Maj / 10);
116
14.3k
      Str[1] = '0' + (Maj % 10);
117
14.3k
      Str[2] = '0' + std::min(Min, 9U);
118
14.3k
      Str[3] = '0' + std::min(Rev, 9U);
119
14.3k
      Str[4] = '\0';
120
45.1k
    } else {
121
      // Handle versions > 10.9.
122
45.1k
      Str[0] = '0' + (Maj / 10);
123
45.1k
      Str[1] = '0' + (Maj % 10);
124
45.1k
      Str[2] = '0' + (Min / 10);
125
45.1k
      Str[3] = '0' + (Min % 10);
126
45.1k
      Str[4] = '0' + (Rev / 10);
127
45.1k
      Str[5] = '0' + (Rev % 10);
128
45.1k
      Str[6] = '\0';
129
45.1k
    }
130
59.5k
    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
131
59.5k
  }
132
133
  // Tell users about the kernel if there is one.
134
59.9k
  if (Triple.isOSDarwin())
135
59.8k
    Builder.defineMacro("__MACH__");
136
137
59.9k
  PlatformMinVersion = VersionTuple(Maj, Min, Rev);
138
59.9k
}
139
140
static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
141
168
                            MacroBuilder &Builder) {
142
168
  DefineStd(Builder, "WIN32", Opts);
143
168
  DefineStd(Builder, "WINNT", Opts);
144
168
  if (Triple.isArch64Bit()) {
145
54
    DefineStd(Builder, "WIN64", Opts);
146
54
    Builder.defineMacro("__MINGW64__");
147
54
  }
148
168
  Builder.defineMacro("__MSVCRT__");
149
168
  Builder.defineMacro("__MINGW32__");
150
168
  addCygMingDefines(Opts, Builder);
151
168
}
152
153
8.34k
static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
154
8.34k
  if (Opts.CPlusPlus) {
155
7.07k
    if (Opts.RTTIData)
156
6.95k
      Builder.defineMacro("_CPPRTTI");
157
158
7.07k
    if (Opts.CXXExceptions)
159
6.38k
      Builder.defineMacro("_CPPUNWIND");
160
7.07k
  }
161
162
8.34k
  if (Opts.Bool)
163
7.07k
    Builder.defineMacro("__BOOL_DEFINED");
164
165
8.34k
  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.34k
  if (Opts.POSIXThreads)
171
0
    Builder.defineMacro("_MT");
172
173
8.34k
  if (Opts.MSCompatibilityVersion) {
174
7.34k
    Builder.defineMacro("_MSC_VER",
175
7.34k
                        Twine(Opts.MSCompatibilityVersion / 100000));
176
7.34k
    Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
177
    // FIXME We cannot encode the revision information into 32-bits
178
7.34k
    Builder.defineMacro("_MSC_BUILD", Twine(1));
179
180
7.34k
    if (Opts.CPlusPlus11 && 
Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)5.31k
)
181
5.29k
      Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
182
183
7.34k
    if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
184
7.29k
      if (Opts.CPlusPlus20)
185
1.29k
        Builder.defineMacro("_MSVC_LANG", "201705L");
186
5.99k
      else if (Opts.CPlusPlus17)
187
1.29k
        Builder.defineMacro("_MSVC_LANG", "201703L");
188
4.70k
      else if (Opts.CPlusPlus14)
189
1.40k
        Builder.defineMacro("_MSVC_LANG", "201402L");
190
7.29k
    }
191
7.34k
  }
192
193
8.34k
  if (Opts.MicrosoftExt) {
194
7.71k
    Builder.defineMacro("_MSC_EXTENSIONS");
195
196
7.71k
    if (Opts.CPlusPlus11) {
197
5.55k
      Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
198
5.55k
      Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
199
5.55k
      Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
200
5.55k
    }
201
7.71k
  }
202
203
8.34k
  Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
204
8.34k
}
205
206
void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
207
8.55k
                       MacroBuilder &Builder) {
208
8.55k
  Builder.defineMacro("_WIN32");
209
8.55k
  if (Triple.isArch64Bit())
210
7.81k
    Builder.defineMacro("_WIN64");
211
8.55k
  if (Triple.isWindowsGNUEnvironment())
212
168
    addMinGWDefines(Triple, Opts, Builder);
213
8.38k
  else if (Triple.isKnownWindowsMSVCEnvironment() ||
214
8.38k
           
(45
Triple.isWindowsItaniumEnvironment()45
&&
Opts.MSVCCompat42
))
215
8.34k
    addVisualCDefines(Opts, Builder);
216
8.55k
}
217
218
} // namespace targets
219
} // namespace clang