Coverage Report

Created: 2020-09-19 12:23

/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
48.0k
                      VersionTuple &PlatformMinVersion) {
25
48.0k
  Builder.defineMacro("__APPLE_CC__", "6000");
26
48.0k
  Builder.defineMacro("__APPLE__");
27
48.0k
  Builder.defineMacro("__STDC_NO_THREADS__");
28
48.0k
29
  // AddressSanitizer doesn't play well with source fortification, which is on
30
  // by default on Darwin.
31
48.0k
  if (Opts.Sanitize.has(SanitizerKind::Address))
32
36
    Builder.defineMacro("_FORTIFY_SOURCE", "0");
33
48.0k
34
  // Darwin defines __weak, __strong, and __unsafe_unretained even in C mode.
35
48.0k
  if (!Opts.ObjC) {
36
    // __weak is always defined, for use in blocks and with objc pointers.
37
29.6k
    Builder.defineMacro("__weak", "__attribute__((objc_gc(weak)))");
38
29.6k
    Builder.defineMacro("__strong", "");
39
29.6k
    Builder.defineMacro("__unsafe_unretained", "");
40
29.6k
  }
41
48.0k
42
48.0k
  if (Opts.Static)
43
6
    Builder.defineMacro("__STATIC__");
44
48.0k
  else
45
48.0k
    Builder.defineMacro("__DYNAMIC__");
46
48.0k
47
48.0k
  if (Opts.POSIXThreads)
48
1
    Builder.defineMacro("_REENTRANT");
49
48.0k
50
  // Get the platform type and version number from the triple.
51
48.0k
  unsigned Maj, Min, Rev;
52
48.0k
  if (Triple.isMacOSX()) {
53
47.6k
    Triple.getMacOSXVersion(Maj, Min, Rev);
54
47.6k
    PlatformName = "macos";
55
347
  } else {
56
347
    Triple.getOSVersion(Maj, Min, Rev);
57
347
    PlatformName = llvm::Triple::getOSTypeName(Triple.getOS());
58
347
  }
59
48.0k
60
  // If -target arch-pc-win32-macho option specified, we're
61
  // generating code for Win32 ABI. No need to emit
62
  // __ENVIRONMENT_XX_OS_VERSION_MIN_REQUIRED__.
63
48.0k
  if (PlatformName == "win32") {
64
0
    PlatformMinVersion = VersionTuple(Maj, Min, Rev);
65
0
    return;
66
0
  }
67
48.0k
68
  // Set the appropriate OS version define.
69
48.0k
  if (Triple.isiOS()) {
70
272
    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
71
272
    char Str[7];
72
272
    if (Maj < 10) {
73
206
      Str[0] = '0' + Maj;
74
206
      Str[1] = '0' + (Min / 10);
75
206
      Str[2] = '0' + (Min % 10);
76
206
      Str[3] = '0' + (Rev / 10);
77
206
      Str[4] = '0' + (Rev % 10);
78
206
      Str[5] = '\0';
79
66
    } else {
80
      // Handle versions >= 10.
81
66
      Str[0] = '0' + (Maj / 10);
82
66
      Str[1] = '0' + (Maj % 10);
83
66
      Str[2] = '0' + (Min / 10);
84
66
      Str[3] = '0' + (Min % 10);
85
66
      Str[4] = '0' + (Rev / 10);
86
66
      Str[5] = '0' + (Rev % 10);
87
66
      Str[6] = '\0';
88
66
    }
89
272
    if (Triple.isTvOS())
90
43
      Builder.defineMacro("__ENVIRONMENT_TV_OS_VERSION_MIN_REQUIRED__", Str);
91
229
    else
92
229
      Builder.defineMacro("__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__",
93
229
                          Str);
94
272
95
47.7k
  } else if (Triple.isWatchOS()) {
96
54
    assert(Maj < 10 && Min < 100 && Rev < 100 && "Invalid version!");
97
54
    char Str[6];
98
54
    Str[0] = '0' + Maj;
99
54
    Str[1] = '0' + (Min / 10);
100
54
    Str[2] = '0' + (Min % 10);
101
54
    Str[3] = '0' + (Rev / 10);
102
54
    Str[4] = '0' + (Rev % 10);
103
54
    Str[5] = '\0';
104
54
    Builder.defineMacro("__ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__", Str);
105
47.6k
  } else if (Triple.isMacOSX()) {
106
    // Note that the Driver allows versions which aren't representable in the
107
    // define (because we only get a single digit for the minor and micro
108
    // revision numbers). So, we limit them to the maximum representable
109
    // version.
110
47.6k
    assert(Maj < 100 && Min < 100 && Rev < 100 && "Invalid version!");
111
47.6k
    char Str[7];
112
47.6k
    if (Maj < 10 || (Maj == 10 && 
Min < 1047.6k
)) {
113
2.89k
      Str[0] = '0' + (Maj / 10);
114
2.89k
      Str[1] = '0' + (Maj % 10);
115
2.89k
      Str[2] = '0' + std::min(Min, 9U);
116
2.89k
      Str[3] = '0' + std::min(Rev, 9U);
117
2.89k
      Str[4] = '\0';
118
44.7k
    } else {
119
      // Handle versions > 10.9.
120
44.7k
      Str[0] = '0' + (Maj / 10);
121
44.7k
      Str[1] = '0' + (Maj % 10);
122
44.7k
      Str[2] = '0' + (Min / 10);
123
44.7k
      Str[3] = '0' + (Min % 10);
124
44.7k
      Str[4] = '0' + (Rev / 10);
125
44.7k
      Str[5] = '0' + (Rev % 10);
126
44.7k
      Str[6] = '\0';
127
44.7k
    }
128
47.6k
    Builder.defineMacro("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", Str);
129
47.6k
  }
130
48.0k
131
  // Tell users about the kernel if there is one.
132
48.0k
  if (Triple.isOSDarwin())
133
48.0k
    Builder.defineMacro("__MACH__");
134
48.0k
135
48.0k
  PlatformMinVersion = VersionTuple(Maj, Min, Rev);
136
48.0k
}
137
138
static void addMinGWDefines(const llvm::Triple &Triple, const LangOptions &Opts,
139
166
                            MacroBuilder &Builder) {
140
166
  DefineStd(Builder, "WIN32", Opts);
141
166
  DefineStd(Builder, "WINNT", Opts);
142
166
  if (Triple.isArch64Bit()) {
143
52
    DefineStd(Builder, "WIN64", Opts);
144
52
    Builder.defineMacro("__MINGW64__");
145
52
  }
146
166
  Builder.defineMacro("__MSVCRT__");
147
166
  Builder.defineMacro("__MINGW32__");
148
166
  addCygMingDefines(Opts, Builder);
149
166
}
150
151
8.02k
static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
152
8.02k
  if (Opts.CPlusPlus) {
153
6.80k
    if (Opts.RTTIData)
154
6.68k
      Builder.defineMacro("_CPPRTTI");
155
6.80k
156
6.80k
    if (Opts.CXXExceptions)
157
6.23k
      Builder.defineMacro("_CPPUNWIND");
158
6.80k
  }
159
8.02k
160
8.02k
  if (Opts.Bool)
161
6.80k
    Builder.defineMacro("__BOOL_DEFINED");
162
8.02k
163
8.02k
  if (!Opts.CharIsSigned)
164
2
    Builder.defineMacro("_CHAR_UNSIGNED");
165
8.02k
166
  // FIXME: POSIXThreads isn't exactly the option this should be defined for,
167
  //        but it works for now.
168
8.02k
  if (Opts.POSIXThreads)
169
0
    Builder.defineMacro("_MT");
170
8.02k
171
8.02k
  if (Opts.MSCompatibilityVersion) {
172
7.08k
    Builder.defineMacro("_MSC_VER",
173
7.08k
                        Twine(Opts.MSCompatibilityVersion / 100000));
174
7.08k
    Builder.defineMacro("_MSC_FULL_VER", Twine(Opts.MSCompatibilityVersion));
175
    // FIXME We cannot encode the revision information into 32-bits
176
7.08k
    Builder.defineMacro("_MSC_BUILD", Twine(1));
177
7.08k
178
7.08k
    if (Opts.CPlusPlus11 && 
Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)5.11k
)
179
5.09k
      Builder.defineMacro("_HAS_CHAR16_T_LANGUAGE_SUPPORT", Twine(1));
180
7.08k
181
7.08k
    if (Opts.isCompatibleWithMSVC(LangOptions::MSVC2015)) {
182
7.02k
      if (Opts.CPlusPlus20)
183
1.26k
        Builder.defineMacro("_MSVC_LANG", "201705L");
184
5.76k
      else if (Opts.CPlusPlus17)
185
1.26k
        Builder.defineMacro("_MSVC_LANG", "201703L");
186
4.50k
      else if (Opts.CPlusPlus14)
187
1.30k
        Builder.defineMacro("_MSVC_LANG", "201402L");
188
7.02k
    }
189
7.08k
  }
190
8.02k
191
8.02k
  if (Opts.MicrosoftExt) {
192
7.43k
    Builder.defineMacro("_MSC_EXTENSIONS");
193
7.43k
194
7.43k
    if (Opts.CPlusPlus11) {
195
5.34k
      Builder.defineMacro("_RVALUE_REFERENCES_V2_SUPPORTED");
196
5.34k
      Builder.defineMacro("_RVALUE_REFERENCES_SUPPORTED");
197
5.34k
      Builder.defineMacro("_NATIVE_NULLPTR_SUPPORTED");
198
5.34k
    }
199
7.43k
  }
200
8.02k
201
8.02k
  Builder.defineMacro("_INTEGRAL_MAX_BITS", "64");
202
8.02k
}
203
204
void addWindowsDefines(const llvm::Triple &Triple, const LangOptions &Opts,
205
8.21k
                       MacroBuilder &Builder) {
206
8.21k
  Builder.defineMacro("_WIN32");
207
8.21k
  if (Triple.isArch64Bit())
208
7.50k
    Builder.defineMacro("_WIN64");
209
8.21k
  if (Triple.isWindowsGNUEnvironment())
210
166
    addMinGWDefines(Triple, Opts, Builder);
211
8.05k
  else if (Triple.isKnownWindowsMSVCEnvironment() ||
212
31
           (Triple.isWindowsItaniumEnvironment() && Opts.MSVCCompat))
213
8.02k
    addVisualCDefines(Opts, Builder);
214
8.21k
}
215
216
} // namespace targets
217
} // namespace clang