Coverage Report

Created: 2021-01-19 06:58

/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
252M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
252M
  if (ID < Builtin::FirstTSBuiltin)
33
4.21M
    return BuiltinInfo[ID];
34
248M
  assert(((ID - Builtin::FirstTSBuiltin) <
35
248M
          (TSRecords.size() + AuxTSRecords.size())) &&
36
248M
         "Invalid builtin ID!");
37
248M
  if (isAuxBuiltinID(ID))
38
206
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
248M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
248M
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
84.1k
                                        const TargetInfo *AuxTarget) {
44
84.1k
  assert(TSRecords.empty() && "Already initialized target?");
45
84.1k
  TSRecords = Target.getTargetBuiltins();
46
84.1k
  if (AuxTarget)
47
138
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
84.1k
}
49
50
138
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
51
118k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; 
++i118k
)
52
118k
    if (FuncName.equals(BuiltinInfo[i].Name))
53
129
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
54
55
9
  return false;
56
138
}
57
58
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
59
255M
                                          const LangOptions &LangOpts) {
60
255M
  bool BuiltinsUnsupported =
61
255M
      (LangOpts.NoBuiltin || 
LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)193M
) &&
62
62.8M
      strchr(BuiltinInfo.Attributes, 'f');
63
255M
  bool MathBuiltinsUnsupported =
64
255M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName9.28k
&&
65
2.36k
    llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
66
255M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)107M
;
67
255M
  bool MSModeUnsupported =
68
255M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)223M
;
69
255M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG193M
;
70
255M
  bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
71
255M
                          (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) ==  OCLC1X_LANG;
72
255M
  bool OclC2Unsupported =
73
255M
      (LangOpts.OpenCLVersion != 200 && 
!LangOpts.OpenCLCPlusPlus255M
) &&
74
255M
      (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
75
255M
  bool OclCUnsupported = !LangOpts.OpenCL &&
76
254M
                         (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
77
255M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG231M
;
78
255M
  bool CPlusPlusUnsupported =
79
255M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG73.6M
;
80
255M
  return !BuiltinsUnsupported && 
!MathBuiltinsUnsupported248M
&&
!OclCUnsupported248M
&&
81
245M
         
!OclC1Unsupported245M
&& !OclC2Unsupported &&
!OpenMPUnsupported245M
&&
82
245M
         !GnuModeUnsupported && 
!MSModeUnsupported245M
&&
!ObjCUnsupported236M
&&
83
235M
         !CPlusPlusUnsupported;
84
255M
}
85
86
/// initializeBuiltins - Mark the identifiers for all the builtins with their
87
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
88
/// such.
89
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
90
80.2k
                                          const LangOptions& LangOpts) {
91
  // Step #1: mark all target-independent builtins with their ID's.
92
99.3M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i99.3M
)
93
99.3M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
94
81.0M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
95
81.0M
    }
96
97
  // Step #2: Register target-specific builtins.
98
156M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i156M
)
99
156M
    if (builtinIsSupported(TSRecords[i], LangOpts))
100
153M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
101
102
  // Step #3: Register target-specific builtins for AuxTarget.
103
203k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i122k
)
104
122k
    Table.get(AuxTSRecords[i].Name)
105
122k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
106
80.2k
}
107
108
1.09k
void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
109
1.09k
  Table.get(getRecord(ID).Name).setBuiltinID(0);
110
1.09k
}
111
112
53.5k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
113
53.5k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
114
53.5k
  if (!WidthPos)
115
42.6k
    return 0;
116
117
10.9k
  ++WidthPos;
118
10.9k
  assert(*WidthPos == ':' &&
119
10.9k
         "Vector width specifier must be followed by a ':'");
120
10.9k
  ++WidthPos;
121
122
10.9k
  char *EndPos;
123
10.9k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
124
10.9k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
125
10.9k
  return Width;
126
10.9k
}
127
128
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
129
16.9M
                              bool &HasVAListArg, const char *Fmt) const {
130
16.9M
  assert(Fmt && "Not passed a format string");
131
16.9M
  assert(::strlen(Fmt) == 2 &&
132
16.9M
         "Format string needs to be two characters long");
133
16.9M
  assert(::toupper(Fmt[0]) == Fmt[1] &&
134
16.9M
         "Format string is not in the form \"xX\"");
135
136
16.9M
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
137
16.9M
  if (!Like)
138
16.9M
    return false;
139
140
2.44k
  HasVAListArg = (*Like == Fmt[1]);
141
142
2.44k
  ++Like;
143
2.44k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
144
2.44k
  ++Like;
145
146
2.44k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
147
2.44k
  FormatIdx = ::strtol(Like, nullptr, 10);
148
2.44k
  return true;
149
2.44k
}
150
151
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
152
8.47M
                                    bool &HasVAListArg) {
153
8.47M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
154
8.47M
}
155
156
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
157
8.47M
                                   bool &HasVAListArg) {
158
8.47M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
159
8.47M
}
160
161
bool Builtin::Context::performsCallback(unsigned ID,
162
8.47M
                                        SmallVectorImpl<int> &Encoding) const {
163
8.47M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
164
8.47M
  if (!CalleePos)
165
8.47M
    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
0
}
187
188
49.2M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
189
49.2M
  return ID == Builtin::NotBuiltin ||
190
36.1M
         ID == Builtin::BI__va_start ||
191
36.1M
         (!hasReferenceArgsOrResult(ID) &&
192
36.1M
          !hasCustomTypechecking(ID));
193
49.2M
}