Coverage Report

Created: 2021-08-24 07:12

/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
1.22G
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
1.22G
  if (ID < Builtin::FirstTSBuiltin)
33
4.34M
    return BuiltinInfo[ID];
34
1.22G
  assert(((ID - Builtin::FirstTSBuiltin) <
35
1.22G
          (TSRecords.size() + AuxTSRecords.size())) &&
36
1.22G
         "Invalid builtin ID!");
37
1.22G
  if (isAuxBuiltinID(ID))
38
6.91k
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
1.22G
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
1.22G
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
97.0k
                                        const TargetInfo *AuxTarget) {
44
97.0k
  assert(TSRecords.empty() && "Already initialized target?");
45
0
  TSRecords = Target.getTargetBuiltins();
46
97.0k
  if (AuxTarget)
47
166
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
97.0k
}
49
50
144
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
51
122k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; 
++i122k
)
52
122k
    if (FuncName.equals(BuiltinInfo[i].Name))
53
139
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
54
55
5
  return false;
56
144
}
57
58
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
59
327M
                                          const LangOptions &LangOpts) {
60
327M
  bool BuiltinsUnsupported =
61
327M
      (LangOpts.NoBuiltin || 
LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)230M
) &&
62
327M
      
strchr(BuiltinInfo.Attributes, 'f')97.3M
;
63
327M
  bool CorBuiltinsUnsupported =
64
327M
      !LangOpts.Coroutines && 
(BuiltinInfo.Langs & COR_LANG)316M
;
65
327M
  bool MathBuiltinsUnsupported =
66
327M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName3.28k
&&
67
327M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")435
;
68
327M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)110M
;
69
327M
  bool MSModeUnsupported =
70
327M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)292M
;
71
327M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG222M
;
72
327M
  bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
73
327M
                          
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG326M
;
74
327M
  bool OclC2Unsupported =
75
327M
      (LangOpts.OpenCLVersion != 200 && 
!LangOpts.OpenCLCPlusPlus327M
) &&
76
327M
      
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG327M
;
77
327M
  bool OclCUnsupported = !LangOpts.OpenCL &&
78
327M
                         
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES)326M
;
79
327M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG301M
;
80
327M
  bool CUDAUnsupported = !LangOpts.CUDA && 
BuiltinInfo.Langs == CUDA_LANG326M
;
81
327M
  bool CPlusPlusUnsupported =
82
327M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG88.6M
;
83
327M
  return !BuiltinsUnsupported && 
!CorBuiltinsUnsupported316M
&&
84
327M
         
!MathBuiltinsUnsupported315M
&&
!OclCUnsupported315M
&&
!OclC1Unsupported312M
&&
85
327M
         
!OclC2Unsupported312M
&&
!OpenMPUnsupported312M
&&
!GnuModeUnsupported312M
&&
86
327M
         
!MSModeUnsupported311M
&&
!ObjCUnsupported301M
&&
!CPlusPlusUnsupported299M
&&
87
327M
         
!CUDAUnsupported299M
;
88
327M
}
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
93.0k
                                          const LangOptions& LangOpts) {
95
  // Step #1: mark all target-independent builtins with their ID's.
96
115M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i115M
)
97
115M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
98
91.1M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
99
91.1M
    }
100
101
  // Step #2: Register target-specific builtins.
102
212M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i211M
)
103
211M
    if (builtinIsSupported(TSRecords[i], LangOpts))
104
208M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
105
106
  // Step #3: Register target-specific builtins for AuxTarget.
107
274k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i181k
)
108
181k
    Table.get(AuxTSRecords[i].Name)
109
181k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
110
93.0k
}
111
112
115k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
113
115k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
114
115k
  if (!WidthPos)
115
103k
    return 0;
116
117
11.9k
  ++WidthPos;
118
11.9k
  assert(*WidthPos == ':' &&
119
11.9k
         "Vector width specifier must be followed by a ':'");
120
0
  ++WidthPos;
121
122
11.9k
  char *EndPos;
123
11.9k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
124
11.9k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
125
0
  return Width;
126
115k
}
127
128
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
129
53.3M
                              bool &HasVAListArg, const char *Fmt) const {
130
53.3M
  assert(Fmt && "Not passed a format string");
131
0
  assert(::strlen(Fmt) == 2 &&
132
53.3M
         "Format string needs to be two characters long");
133
0
  assert(::toupper(Fmt[0]) == Fmt[1] &&
134
53.3M
         "Format string is not in the form \"xX\"");
135
136
0
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
137
53.3M
  if (!Like)
138
53.3M
    return false;
139
140
2.47k
  HasVAListArg = (*Like == Fmt[1]);
141
142
2.47k
  ++Like;
143
2.47k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
144
0
  ++Like;
145
146
2.47k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
147
0
  FormatIdx = ::strtol(Like, nullptr, 10);
148
2.47k
  return true;
149
53.3M
}
150
151
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
152
26.6M
                                    bool &HasVAListArg) {
153
26.6M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
154
26.6M
}
155
156
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
157
26.6M
                                   bool &HasVAListArg) {
158
26.6M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
159
26.6M
}
160
161
bool Builtin::Context::performsCallback(unsigned ID,
162
26.6M
                                        SmallVectorImpl<int> &Encoding) const {
163
26.6M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
164
26.6M
  if (!CalleePos)
165
26.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
26.6M
}
187
188
251M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
189
251M
  return ID == Builtin::NotBuiltin ||
190
251M
         
ID == Builtin::BI__va_start237M
||
191
251M
         
(237M
!hasReferenceArgsOrResult(ID)237M
&&
192
237M
          
!hasCustomTypechecking(ID)237M
);
193
251M
}