Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Builtins.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Builtins.cpp - Builtin function implementation -------------------===//
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 various things for builtin functions.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Basic/Builtins.h"
14
#include "BuiltinTargetFeatures.h"
15
#include "clang/Basic/IdentifierTable.h"
16
#include "clang/Basic/LangOptions.h"
17
#include "clang/Basic/TargetInfo.h"
18
#include "llvm/ADT/StringRef.h"
19
using namespace clang;
20
21
static const Builtin::Info BuiltinInfo[] = {
22
  { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
23
#define BUILTIN(ID, TYPE, ATTRS)                                               \
24
  { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
25
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS)                                    \
26
  { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
27
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS)                             \
28
  { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
29
#include "clang/Basic/Builtins.def"
30
};
31
32
402M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
33
402M
  if (ID < Builtin::FirstTSBuiltin)
34
15.4M
    return BuiltinInfo[ID];
35
386M
  assert(((ID - Builtin::FirstTSBuiltin) <
36
386M
          (TSRecords.size() + AuxTSRecords.size())) &&
37
386M
         "Invalid builtin ID!");
38
386M
  if (isAuxBuiltinID(ID))
39
137
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
40
386M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
41
386M
}
42
43
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
44
91.0k
                                        const TargetInfo *AuxTarget) {
45
91.0k
  assert(TSRecords.empty() && "Already initialized target?");
46
0
  TSRecords = Target.getTargetBuiltins();
47
91.0k
  if (AuxTarget)
48
143
    AuxTSRecords = AuxTarget->getTargetBuiltins();
49
91.0k
}
50
51
145
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
52
145
  bool InStdNamespace = FuncName.consume_front("std-");
53
130k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
54
130k
       
++i130k
) {
55
130k
    if (FuncName.equals(BuiltinInfo[i].Name) &&
56
130k
        
(bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace140
)
57
140
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
58
130k
  }
59
60
5
  return false;
61
145
}
62
63
/// Is this builtin supported according to the given language options?
64
static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
65
293M
                               const LangOptions &LangOpts) {
66
293M
  bool BuiltinsUnsupported =
67
293M
      LangOpts.NoBuiltin && 
strchr(BuiltinInfo.Attributes, 'f') != nullptr54.8M
;
68
293M
  bool CorBuiltinsUnsupported =
69
293M
      !LangOpts.Coroutines && 
(BuiltinInfo.Langs & COR_LANG)280M
;
70
293M
  bool MathBuiltinsUnsupported =
71
293M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName3.26k
&&
72
293M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")442
;
73
293M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)119M
;
74
293M
  bool MSModeUnsupported =
75
293M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)256M
;
76
293M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG217M
;
77
293M
  bool OclCUnsupported =
78
293M
      !LangOpts.OpenCL && 
(BuiltinInfo.Langs & ALL_OCL_LANGUAGES)291M
;
79
293M
  bool OclGASUnsupported =
80
293M
      !LangOpts.OpenCLGenericAddressSpace && 
(BuiltinInfo.Langs & OCL_GAS)292M
;
81
293M
  bool OclPipeUnsupported =
82
293M
      !LangOpts.OpenCLPipes && 
(BuiltinInfo.Langs & OCL_PIPE)292M
;
83
  // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
84
  // support is indicated with language option for blocks.
85
293M
  bool OclDSEUnsupported =
86
293M
      (LangOpts.getOpenCLCompatibleVersion() < 200 || 
!LangOpts.Blocks885k
) &&
87
293M
      
(BuiltinInfo.Langs & OCL_DSE)292M
;
88
293M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG264M
;
89
293M
  bool CUDAUnsupported = !LangOpts.CUDA && 
BuiltinInfo.Langs == CUDA_LANG292M
;
90
293M
  bool CPlusPlusUnsupported =
91
293M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG76.0M
;
92
293M
  return !BuiltinsUnsupported && 
!CorBuiltinsUnsupported286M
&&
93
293M
         
!MathBuiltinsUnsupported285M
&&
!OclCUnsupported285M
&&
!OclGASUnsupported285M
&&
94
293M
         
!OclPipeUnsupported285M
&&
!OclDSEUnsupported283M
&&
!OpenMPUnsupported283M
&&
95
293M
         
!GnuModeUnsupported283M
&&
!MSModeUnsupported282M
&&
!ObjCUnsupported272M
&&
96
293M
         
!CPlusPlusUnsupported271M
&&
!CUDAUnsupported270M
;
97
293M
}
98
99
/// initializeBuiltins - Mark the identifiers for all the builtins with their
100
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
101
/// such.
102
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
103
87.0k
                                          const LangOptions& LangOpts) {
104
  // Step #1: mark all target-independent builtins with their ID's.
105
112M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i112M
)
106
112M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
107
92.6M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
108
92.6M
    }
109
110
  // Step #2: Register target-specific builtins.
111
181M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i181M
)
112
181M
    if (builtinIsSupported(TSRecords[i], LangOpts))
113
178M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
114
115
  // Step #3: Register target-specific builtins for AuxTarget.
116
255k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i168k
)
117
168k
    Table.get(AuxTSRecords[i].Name)
118
168k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
119
120
  // Step #4: Unregister any builtins specified by -fno-builtin-foo.
121
87.0k
  for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) {
122
57
    bool InStdNamespace = Name.consume_front("std-");
123
57
    auto NameIt = Table.find(Name);
124
57
    if (NameIt != Table.end()) {
125
57
      unsigned ID = NameIt->second->getBuiltinID();
126
57
      if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
127
57
          isInStdNamespace(ID) == InStdNamespace) {
128
57
        Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
129
57
      }
130
57
    }
131
57
  }
132
87.0k
}
133
134
57.0k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
135
57.0k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
136
57.0k
  if (!WidthPos)
137
43.7k
    return 0;
138
139
13.2k
  ++WidthPos;
140
13.2k
  assert(*WidthPos == ':' &&
141
13.2k
         "Vector width specifier must be followed by a ':'");
142
0
  ++WidthPos;
143
144
13.2k
  char *EndPos;
145
13.2k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
146
13.2k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
147
0
  return Width;
148
57.0k
}
149
150
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
151
38.6M
                              bool &HasVAListArg, const char *Fmt) const {
152
38.6M
  assert(Fmt && "Not passed a format string");
153
0
  assert(::strlen(Fmt) == 2 &&
154
38.6M
         "Format string needs to be two characters long");
155
0
  assert(::toupper(Fmt[0]) == Fmt[1] &&
156
38.6M
         "Format string is not in the form \"xX\"");
157
158
0
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
159
38.6M
  if (!Like)
160
38.6M
    return false;
161
162
32.1k
  HasVAListArg = (*Like == Fmt[1]);
163
164
32.1k
  ++Like;
165
32.1k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
166
0
  ++Like;
167
168
32.1k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
169
0
  FormatIdx = ::strtol(Like, nullptr, 10);
170
32.1k
  return true;
171
38.6M
}
172
173
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
174
19.3M
                                    bool &HasVAListArg) {
175
19.3M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
176
19.3M
}
177
178
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
179
19.3M
                                   bool &HasVAListArg) {
180
19.3M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
181
19.3M
}
182
183
bool Builtin::Context::performsCallback(unsigned ID,
184
19.3M
                                        SmallVectorImpl<int> &Encoding) const {
185
19.3M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
186
19.3M
  if (!CalleePos)
187
19.3M
    return false;
188
189
0
  ++CalleePos;
190
0
  assert(*CalleePos == '<' &&
191
0
         "Callback callee specifier must be followed by a '<'");
192
0
  ++CalleePos;
193
194
0
  char *EndPos;
195
0
  int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
196
0
  assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
197
0
  Encoding.push_back(CalleeIdx);
198
199
0
  while (*EndPos == ',') {
200
0
    const char *PayloadPos = EndPos + 1;
201
202
0
    int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
203
0
    Encoding.push_back(PayloadIdx);
204
0
  }
205
206
0
  assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
207
0
  return true;
208
19.3M
}
209
210
59.3M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
211
59.3M
  return ID == Builtin::NotBuiltin || 
ID == Builtin::BI__va_start46.0M
||
212
59.3M
         
(46.0M
!hasReferenceArgsOrResult(ID)46.0M
&&
!hasCustomTypechecking(ID)46.0M
) ||
213
59.3M
         
isInStdNamespace(ID)2.18k
;
214
59.3M
}
215
216
bool Builtin::evaluateRequiredTargetFeatures(
217
85.4k
    StringRef RequiredFeatures, const llvm::StringMap<bool> &TargetFetureMap) {
218
  // Return true if the builtin doesn't have any required features.
219
85.4k
  if (RequiredFeatures.empty())
220
69.7k
    return true;
221
15.7k
  assert(!RequiredFeatures.contains(' ') && "Space in feature list");
222
223
0
  TargetFeatures TF(TargetFetureMap);
224
15.7k
  return TF.hasRequiredFeatures(RequiredFeatures);
225
85.4k
}