Coverage Report

Created: 2022-01-18 06:27

/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
460M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
460M
  if (ID < Builtin::FirstTSBuiltin)
33
5.07M
    return BuiltinInfo[ID];
34
455M
  assert(((ID - Builtin::FirstTSBuiltin) <
35
455M
          (TSRecords.size() + AuxTSRecords.size())) &&
36
455M
         "Invalid builtin ID!");
37
455M
  if (isAuxBuiltinID(ID))
38
59
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
455M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
455M
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
87.8k
                                        const TargetInfo *AuxTarget) {
44
87.8k
  assert(TSRecords.empty() && "Already initialized target?");
45
0
  TSRecords = Target.getTargetBuiltins();
46
87.8k
  if (AuxTarget)
47
116
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
87.8k
}
49
50
146
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
51
127k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; 
++i127k
)
52
127k
    if (FuncName.equals(BuiltinInfo[i].Name))
53
141
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
54
55
5
  return false;
56
146
}
57
58
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
59
291M
                                          const LangOptions &LangOpts) {
60
291M
  bool BuiltinsUnsupported =
61
291M
      (LangOpts.NoBuiltin || 
LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)225M
) &&
62
291M
      
strchr(BuiltinInfo.Attributes, 'f')65.7M
;
63
291M
  bool CorBuiltinsUnsupported =
64
291M
      !LangOpts.Coroutines && 
(BuiltinInfo.Langs & COR_LANG)279M
;
65
291M
  bool MathBuiltinsUnsupported =
66
291M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName3.34k
&&
67
291M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")435
;
68
291M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)117M
;
69
291M
  bool MSModeUnsupported =
70
291M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)254M
;
71
291M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG216M
;
72
291M
  bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
73
291M
                          
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG291M
;
74
291M
  bool OclC2Unsupported =
75
291M
      (LangOpts.getOpenCLCompatibleVersion() != 200) &&
76
291M
      
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG291M
;
77
291M
  bool OclCUnsupported = !LangOpts.OpenCL &&
78
291M
                         
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES)290M
;
79
291M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG264M
;
80
291M
  bool CUDAUnsupported = !LangOpts.CUDA && 
BuiltinInfo.Langs == CUDA_LANG290M
;
81
291M
  bool CPlusPlusUnsupported =
82
291M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG77.2M
;
83
291M
  return !BuiltinsUnsupported && 
!CorBuiltinsUnsupported284M
&&
84
291M
         
!MathBuiltinsUnsupported283M
&&
!OclCUnsupported283M
&&
!OclC1Unsupported280M
&&
85
291M
         
!OclC2Unsupported280M
&&
!OpenMPUnsupported280M
&&
!GnuModeUnsupported280M
&&
86
291M
         
!MSModeUnsupported279M
&&
!ObjCUnsupported270M
&&
!CPlusPlusUnsupported269M
&&
87
291M
         
!CUDAUnsupported269M
;
88
291M
}
89
90
/// initializeBuiltins - Mark the identifiers for all the builtins with their
91
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
92
/// such.
93
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
94
83.8k
                                          const LangOptions& LangOpts) {
95
  // Step #1: mark all target-independent builtins with their ID's.
96
106M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i106M
)
97
106M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
98
86.9M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
99
86.9M
    }
100
101
  // Step #2: Register target-specific builtins.
102
185M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i185M
)
103
185M
    if (builtinIsSupported(TSRecords[i], LangOpts))
104
182M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
105
106
  // Step #3: Register target-specific builtins for AuxTarget.
107
210k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i126k
)
108
126k
    Table.get(AuxTSRecords[i].Name)
109
126k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
110
83.8k
}
111
112
55.0k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
113
55.0k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
114
55.0k
  if (!WidthPos)
115
43.0k
    return 0;
116
117
12.0k
  ++WidthPos;
118
12.0k
  assert(*WidthPos == ':' &&
119
12.0k
         "Vector width specifier must be followed by a ':'");
120
0
  ++WidthPos;
121
122
12.0k
  char *EndPos;
123
12.0k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
124
12.0k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
125
0
  return Width;
126
55.0k
}
127
128
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
129
45.3M
                              bool &HasVAListArg, const char *Fmt) const {
130
45.3M
  assert(Fmt && "Not passed a format string");
131
0
  assert(::strlen(Fmt) == 2 &&
132
45.3M
         "Format string needs to be two characters long");
133
0
  assert(::toupper(Fmt[0]) == Fmt[1] &&
134
45.3M
         "Format string is not in the form \"xX\"");
135
136
0
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
137
45.3M
  if (!Like)
138
45.3M
    return false;
139
140
2.16k
  HasVAListArg = (*Like == Fmt[1]);
141
142
2.16k
  ++Like;
143
2.16k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
144
0
  ++Like;
145
146
2.16k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
147
0
  FormatIdx = ::strtol(Like, nullptr, 10);
148
2.16k
  return true;
149
45.3M
}
150
151
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
152
22.6M
                                    bool &HasVAListArg) {
153
22.6M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
154
22.6M
}
155
156
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
157
22.6M
                                   bool &HasVAListArg) {
158
22.6M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
159
22.6M
}
160
161
bool Builtin::Context::performsCallback(unsigned ID,
162
22.6M
                                        SmallVectorImpl<int> &Encoding) const {
163
22.6M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
164
22.6M
  if (!CalleePos)
165
22.6M
    return false;
166
167
0
  ++CalleePos;
168
0
  assert(*CalleePos == '<' &&
169
0
         "Callback callee specifier must be followed by a '<'");
170
0
  ++CalleePos;
171
172
0
  char *EndPos;
173
0
  int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
174
0
  assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
175
0
  Encoding.push_back(CalleeIdx);
176
177
0
  while (*EndPos == ',') {
178
0
    const char *PayloadPos = EndPos + 1;
179
180
0
    int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
181
0
    Encoding.push_back(PayloadIdx);
182
0
  }
183
184
0
  assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
185
0
  return true;
186
22.6M
}
187
188
67.5M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
189
67.5M
  return ID == Builtin::NotBuiltin ||
190
67.5M
         
ID == Builtin::BI__va_start55.2M
||
191
67.5M
         
(55.2M
!hasReferenceArgsOrResult(ID)55.2M
&&
192
55.2M
          
!hasCustomTypechecking(ID)55.2M
);
193
67.5M
}