Coverage Report

Created: 2019-07-24 05:18

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