Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Basic/Builtins.cpp
Line
Count
Source
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
38.1M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
38.1M
  if (ID < Builtin::FirstTSBuiltin)
33
4.17M
    return BuiltinInfo[ID];
34
33.9M
  assert(((ID - Builtin::FirstTSBuiltin) <
35
33.9M
          (TSRecords.size() + AuxTSRecords.size())) &&
36
33.9M
         "Invalid builtin ID!");
37
33.9M
  if (isAuxBuiltinID(ID))
38
78
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
33.9M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
33.9M
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
59.0k
                                        const TargetInfo *AuxTarget) {
44
59.0k
  assert(TSRecords.empty() && "Already initialized target?");
45
59.0k
  TSRecords = Target.getTargetBuiltins();
46
59.0k
  if (AuxTarget)
47
87
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
59.0k
}
49
50
102
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
51
79.8k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; 
++i79.7k
)
52
79.8k
    if (FuncName.equals(BuiltinInfo[i].Name))
53
101
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
54
102
55
102
  
return false1
;
56
102
}
57
58
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
59
162M
                                          const LangOptions &LangOpts) {
60
162M
  bool BuiltinsUnsupported =
61
162M
      (LangOpts.NoBuiltin || 
LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)109M
) &&
62
162M
      
strchr(BuiltinInfo.Attributes, 'f')52.4M
;
63
162M
  bool MathBuiltinsUnsupported =
64
162M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName8.86k
&&
65
162M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")2.36k
;
66
162M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)55.2M
;
67
162M
  bool MSModeUnsupported =
68
162M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)153M
;
69
162M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG110M
;
70
162M
  bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
71
162M
                          
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG161M
;
72
162M
  bool OclC2Unsupported =
73
162M
      (LangOpts.OpenCLVersion != 200 && 
!LangOpts.OpenCLCPlusPlus162M
) &&
74
162M
      
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG161M
;
75
162M
  bool OclCUnsupported = !LangOpts.OpenCL &&
76
162M
                         
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES)161M
;
77
162M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG145M
;
78
162M
  bool CPlusPlusUnsupported =
79
162M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG43.7M
;
80
162M
  return !BuiltinsUnsupported && 
!MathBuiltinsUnsupported155M
&&
!OclCUnsupported155M
&&
81
162M
         
!OclC1Unsupported153M
&&
!OclC2Unsupported153M
&&
!OpenMPUnsupported153M
&&
82
162M
         
!GnuModeUnsupported153M
&&
!MSModeUnsupported153M
&&
!ObjCUnsupported146M
&&
83
162M
         
!CPlusPlusUnsupported146M
;
84
162M
}
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
55.9k
                                          const LangOptions& LangOpts) {
91
55.9k
  // Step #1: mark all target-independent builtins with their ID's.
92
65.9M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i65.8M
)
93
65.8M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
94
51.6M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
95
51.6M
    }
96
55.9k
97
55.9k
  // Step #2: Register target-specific builtins.
98
96.5M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i96.4M
)
99
96.4M
    if (builtinIsSupported(TSRecords[i], LangOpts))
100
94.5M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
101
55.9k
102
55.9k
  // Step #3: Register target-specific builtins for AuxTarget.
103
134k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i78.3k
)
104
78.3k
    Table.get(AuxTSRecords[i].Name)
105
78.3k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
106
55.9k
}
107
108
83
void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
109
83
  Table.get(getRecord(ID).Name).setBuiltinID(0);
110
83
}
111
112
32.4k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
113
32.4k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
114
32.4k
  if (!WidthPos)
115
21.6k
    return 0;
116
10.8k
117
10.8k
  ++WidthPos;
118
10.8k
  assert(*WidthPos == ':' &&
119
10.8k
         "Vector width specifier must be followed by a ':'");
120
10.8k
  ++WidthPos;
121
10.8k
122
10.8k
  char *EndPos;
123
10.8k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
124
10.8k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
125
10.8k
  return Width;
126
10.8k
}
127
128
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
129
2.44M
                              bool &HasVAListArg, const char *Fmt) const {
130
2.44M
  assert(Fmt && "Not passed a format string");
131
2.44M
  assert(::strlen(Fmt) == 2 &&
132
2.44M
         "Format string needs to be two characters long");
133
2.44M
  assert(::toupper(Fmt[0]) == Fmt[1] &&
134
2.44M
         "Format string is not in the form \"xX\"");
135
2.44M
136
2.44M
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
137
2.44M
  if (!Like)
138
2.43M
    return false;
139
2.28k
140
2.28k
  HasVAListArg = (*Like == Fmt[1]);
141
2.28k
142
2.28k
  ++Like;
143
2.28k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
144
2.28k
  ++Like;
145
2.28k
146
2.28k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
147
2.28k
  FormatIdx = ::strtol(Like, nullptr, 10);
148
2.28k
  return true;
149
2.28k
}
150
151
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
152
1.22M
                                    bool &HasVAListArg) {
153
1.22M
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
154
1.22M
}
155
156
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
157
1.22M
                                   bool &HasVAListArg) {
158
1.22M
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
159
1.22M
}
160
161
bool Builtin::Context::performsCallback(unsigned ID,
162
1.22M
                                        SmallVectorImpl<int> &Encoding) const {
163
1.22M
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
164
1.22M
  if (!CalleePos)
165
1.22M
    return false;
166
13
167
13
  ++CalleePos;
168
13
  assert(*CalleePos == '<' &&
169
13
         "Callback callee specifier must be followed by a '<'");
170
13
  ++CalleePos;
171
13
172
13
  char *EndPos;
173
13
  int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
174
13
  assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
175
13
  Encoding.push_back(CalleeIdx);
176
13
177
26
  while (*EndPos == ',') {
178
13
    const char *PayloadPos = EndPos + 1;
179
13
180
13
    int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
181
13
    Encoding.push_back(PayloadIdx);
182
13
  }
183
13
184
13
  assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
185
13
  return true;
186
13
}
187
188
12.4M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
189
12.4M
  return ID == Builtin::NotBuiltin ||
190
12.4M
         
ID == Builtin::BI__va_start1.00M
||
191
12.4M
         
(1.00M
!hasReferenceArgsOrResult(ID)1.00M
&&
192
1.00M
          
!hasCustomTypechecking(ID)1.00M
);
193
12.4M
}