Coverage Report

Created: 2022-05-14 11:35

/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 "clang/Basic/IdentifierTable.h"
15
#include "clang/Basic/LangOptions.h"
16
#include "clang/Basic/TargetInfo.h"
17
#include "llvm/ADT/StringRef.h"
18
using namespace clang;
19
20
static const Builtin::Info BuiltinInfo[] = {
21
  { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
22
#define BUILTIN(ID, TYPE, ATTRS)                                               \
23
  { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
24
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS)                                    \
25
  { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
26
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS)                             \
27
  { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
28
#include "clang/Basic/Builtins.def"
29
};
30
31
392M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
392M
  if (ID < Builtin::FirstTSBuiltin)
33
6.73M
    return BuiltinInfo[ID];
34
385M
  assert(((ID - Builtin::FirstTSBuiltin) <
35
385M
          (TSRecords.size() + AuxTSRecords.size())) &&
36
385M
         "Invalid builtin ID!");
37
385M
  if (isAuxBuiltinID(ID))
38
61
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
385M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
385M
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
89.3k
                                        const TargetInfo *AuxTarget) {
44
89.3k
  assert(TSRecords.empty() && "Already initialized target?");
45
0
  TSRecords = Target.getTargetBuiltins();
46
89.3k
  if (AuxTarget)
47
124
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
89.3k
}
49
50
142
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
51
142
  bool InStdNamespace = FuncName.consume_front("std-");
52
127k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
53
127k
       
++i127k
) {
54
127k
    if (FuncName.equals(BuiltinInfo[i].Name) &&
55
127k
        
(bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace137
)
56
137
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
57
127k
  }
58
59
5
  return false;
60
142
}
61
62
/// Is this builtin supported according to the given language options?
63
static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
64
287M
                               const LangOptions &LangOpts) {
65
287M
  bool BuiltinsUnsupported =
66
287M
      LangOpts.NoBuiltin && 
strchr(BuiltinInfo.Attributes, 'f') != nullptr65.0M
;
67
287M
  bool CorBuiltinsUnsupported =
68
287M
      !LangOpts.Coroutines && 
(BuiltinInfo.Langs & COR_LANG)275M
;
69
287M
  bool MathBuiltinsUnsupported =
70
287M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName3.25k
&&
71
287M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")442
;
72
287M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)116M
;
73
287M
  bool MSModeUnsupported =
74
287M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)250M
;
75
287M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG213M
;
76
287M
  bool OclCUnsupported =
77
287M
      !LangOpts.OpenCL && 
(BuiltinInfo.Langs & ALL_OCL_LANGUAGES)286M
;
78
287M
  bool OclGASUnsupported =
79
287M
      !LangOpts.OpenCLGenericAddressSpace && 
(BuiltinInfo.Langs & OCL_GAS)286M
;
80
287M
  bool OclPipeUnsupported =
81
287M
      !LangOpts.OpenCLPipes && 
(BuiltinInfo.Langs & OCL_PIPE)286M
;
82
  // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
83
  // support is indicated with language option for blocks.
84
287M
  bool OclDSEUnsupported =
85
287M
      (LangOpts.getOpenCLCompatibleVersion() < 200 || 
!LangOpts.Blocks832k
) &&
86
287M
      
(BuiltinInfo.Langs & OCL_DSE)287M
;
87
287M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG260M
;
88
287M
  bool CUDAUnsupported = !LangOpts.CUDA && 
BuiltinInfo.Langs == CUDA_LANG286M
;
89
287M
  bool CPlusPlusUnsupported =
90
287M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG74.1M
;
91
287M
  return !BuiltinsUnsupported && 
!CorBuiltinsUnsupported279M
&&
92
287M
         
!MathBuiltinsUnsupported278M
&&
!OclCUnsupported278M
&&
!OclGASUnsupported278M
&&
93
287M
         
!OclPipeUnsupported278M
&&
!OclDSEUnsupported276M
&&
!OpenMPUnsupported276M
&&
94
287M
         
!GnuModeUnsupported276M
&&
!MSModeUnsupported275M
&&
!ObjCUnsupported266M
&&
95
287M
         
!CPlusPlusUnsupported264M
&&
!CUDAUnsupported264M
;
96
287M
}
97
98
/// initializeBuiltins - Mark the identifiers for all the builtins with their
99
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
100
/// such.
101
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
102
85.3k
                                          const LangOptions& LangOpts) {
103
  // Step #1: mark all target-independent builtins with their ID's.
104
109M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i109M
)
105
109M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
106
89.0M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
107
89.0M
    }
108
109
  // Step #2: Register target-specific builtins.
110
178M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i178M
)
111
178M
    if (builtinIsSupported(TSRecords[i], LangOpts))
112
175M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
113
114
  // Step #3: Register target-specific builtins for AuxTarget.
115
219k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i133k
)
116
133k
    Table.get(AuxTSRecords[i].Name)
117
133k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
118
119
  // Step #4: Unregister any builtins specified by -fno-builtin-foo.
120
85.3k
  for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) {
121
57
    bool InStdNamespace = Name.consume_front("std-");
122
57
    auto NameIt = Table.find(Name);
123
57
    if (NameIt != Table.end()) {
124
57
      unsigned ID = NameIt->second->getBuiltinID();
125
57
      if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
126
57
          isInStdNamespace(ID) == InStdNamespace) {
127
57
        Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
128
57
      }
129
57
    }
130
57
  }
131
85.3k
}
132
133
56.3k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
134
56.3k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
135
56.3k
  if (!WidthPos)
136
43.1k
    return 0;
137
138
13.1k
  ++WidthPos;
139
13.1k
  assert(*WidthPos == ':' &&
140
13.1k
         "Vector width specifier must be followed by a ':'");
141
0
  ++WidthPos;
142
143
13.1k
  char *EndPos;
144
13.1k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
145
13.1k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
146
0
  return Width;
147
56.3k
}
148
149
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
150
38.0M
                              bool &HasVAListArg, const char *Fmt) const {
151
38.0M
  assert(Fmt && "Not passed a format string");
152
0
  assert(::strlen(Fmt) == 2 &&
153
38.0M
         "Format string needs to be two characters long");
154
0
  assert(::toupper(Fmt[0]) == Fmt[1] &&
155
38.0M
         "Format string is not in the form \"xX\"");
156
157
0
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
158
38.0M
  if (!Like)
159
38.0M
    return false;
160
161
2.24k
  HasVAListArg = (*Like == Fmt[1]);
162
163
2.24k
  ++Like;
164
2.24k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
165
0
  ++Like;
166
167
2.24k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
168
0
  FormatIdx = ::strtol(Like, nullptr, 10);
169
2.24k
  return true;
170
38.0M
}
171
172
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
173
19.0M
                                    bool &HasVAListArg) {
174
19.0M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
175
19.0M
}
176
177
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
178
19.0M
                                   bool &HasVAListArg) {
179
19.0M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
180
19.0M
}
181
182
bool Builtin::Context::performsCallback(unsigned ID,
183
19.0M
                                        SmallVectorImpl<int> &Encoding) const {
184
19.0M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
185
19.0M
  if (!CalleePos)
186
19.0M
    return false;
187
188
0
  ++CalleePos;
189
0
  assert(*CalleePos == '<' &&
190
0
         "Callback callee specifier must be followed by a '<'");
191
0
  ++CalleePos;
192
193
0
  char *EndPos;
194
0
  int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
195
0
  assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
196
0
  Encoding.push_back(CalleeIdx);
197
198
0
  while (*EndPos == ',') {
199
0
    const char *PayloadPos = EndPos + 1;
200
201
0
    int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
202
0
    Encoding.push_back(PayloadIdx);
203
0
  }
204
205
0
  assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
206
0
  return true;
207
19.0M
}
208
209
57.8M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
210
57.8M
  return ID == Builtin::NotBuiltin || 
ID == Builtin::BI__va_start45.8M
||
211
57.8M
         
(45.8M
!hasReferenceArgsOrResult(ID)45.8M
&&
!hasCustomTypechecking(ID)45.8M
) ||
212
57.8M
         
isInStdNamespace(ID)74
;
213
57.8M
}