Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
38.9M
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
32
38.9M
  if (ID < Builtin::FirstTSBuiltin)
33
20.0M
    return BuiltinInfo[ID];
34
18.9M
  assert(((ID - Builtin::FirstTSBuiltin) <
35
18.9M
          (TSRecords.size() + AuxTSRecords.size())) &&
36
18.9M
         "Invalid builtin ID!");
37
18.9M
  if (isAuxBuiltinID(ID))
38
80
    return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
39
18.9M
  return TSRecords[ID - Builtin::FirstTSBuiltin];
40
18.9M
}
41
42
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
43
44.2k
                                        const TargetInfo *AuxTarget) {
44
44.2k
  assert(TSRecords.empty() && "Already initialized target?");
45
44.2k
  TSRecords = Target.getTargetBuiltins();
46
44.2k
  if (AuxTarget)
47
65
    AuxTSRecords = AuxTarget->getTargetBuiltins();
48
44.2k
}
49
50
76
bool Builtin::Context::isBuiltinFunc(const char *Name) {
51
76
  StringRef FuncName(Name);
52
58.5k
  for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; 
++i58.5k
)
53
58.5k
    if (FuncName.equals(BuiltinInfo[i].Name))
54
76
      return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
55
76
56
76
  
return false0
;
57
76
}
58
59
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
60
111M
                                          const LangOptions &LangOpts) {
61
111M
  bool BuiltinsUnsupported =
62
111M
      (LangOpts.NoBuiltin || 
LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)98.2M
) &&
63
111M
      
strchr(BuiltinInfo.Attributes, 'f')13.4M
;
64
111M
  bool MathBuiltinsUnsupported =
65
111M
    LangOpts.NoMathBuiltin && 
BuiltinInfo.HeaderName8.68k
&&
66
111M
    
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h")2.36k
;
67
111M
  bool GnuModeUnsupported = !LangOpts.GNUMode && 
(BuiltinInfo.Langs & GNU_LANG)43.3M
;
68
111M
  bool MSModeUnsupported =
69
111M
      !LangOpts.MicrosoftExt && 
(BuiltinInfo.Langs & MS_LANG)105M
;
70
111M
  bool ObjCUnsupported = !LangOpts.ObjC && 
BuiltinInfo.Langs == OBJC_LANG97.6M
;
71
111M
  bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
72
111M
                          
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG110M
;
73
111M
  bool OclC2Unsupported =
74
111M
      (LangOpts.OpenCLVersion != 200 && 
!LangOpts.OpenCLCPlusPlus111M
) &&
75
111M
      
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG111M
;
76
111M
  bool OclCUnsupported = !LangOpts.OpenCL &&
77
111M
                         
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES)110M
;
78
111M
  bool OpenMPUnsupported = !LangOpts.OpenMP && 
BuiltinInfo.Langs == OMP_LANG98.3M
;
79
111M
  bool CPlusPlusUnsupported =
80
111M
      !LangOpts.CPlusPlus && 
BuiltinInfo.Langs == CXX_LANG44.7M
;
81
111M
  return !BuiltinsUnsupported && 
!MathBuiltinsUnsupported109M
&&
!OclCUnsupported109M
&&
82
111M
         
!OclC1Unsupported108M
&&
!OclC2Unsupported108M
&&
!OpenMPUnsupported108M
&&
83
111M
         
!GnuModeUnsupported108M
&&
!MSModeUnsupported108M
&&
!ObjCUnsupported102M
&&
84
111M
         
!CPlusPlusUnsupported102M
;
85
111M
}
86
87
/// initializeBuiltins - Mark the identifiers for all the builtins with their
88
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
89
/// such.
90
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
91
41.5k
                                          const LangOptions& LangOpts) {
92
41.5k
  // Step #1: mark all target-independent builtins with their ID's.
93
47.6M
  for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; 
++i47.6M
)
94
47.6M
    if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
95
39.8M
      Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
96
39.8M
    }
97
41.5k
98
41.5k
  // Step #2: Register target-specific builtins.
99
64.0M
  for (unsigned i = 0, e = TSRecords.size(); i != e; 
++i64.0M
)
100
64.0M
    if (builtinIsSupported(TSRecords[i], LangOpts))
101
62.1M
      Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
102
41.5k
103
41.5k
  // Step #3: Register target-specific builtins for AuxTarget.
104
104k
  for (unsigned i = 0, e = AuxTSRecords.size(); i != e; 
++i62.6k
)
105
62.6k
    Table.get(AuxTSRecords[i].Name)
106
62.6k
        .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
107
41.5k
}
108
109
84
void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
110
84
  Table.get(getRecord(ID).Name).setBuiltinID(0);
111
84
}
112
113
35.4k
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
114
35.4k
  const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
115
35.4k
  if (!WidthPos)
116
25.3k
    return 0;
117
10.1k
118
10.1k
  ++WidthPos;
119
10.1k
  assert(*WidthPos == ':' &&
120
10.1k
         "Vector width specifier must be followed by a ':'");
121
10.1k
  ++WidthPos;
122
10.1k
123
10.1k
  char *EndPos;
124
10.1k
  unsigned Width = ::strtol(WidthPos, &EndPos, 10);
125
10.1k
  assert(*EndPos == ':' && "Vector width specific must end with a ':'");
126
10.1k
  return Width;
127
10.1k
}
128
129
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
130
1.84M
                              bool &HasVAListArg, const char *Fmt) const {
131
1.84M
  assert(Fmt && "Not passed a format string");
132
1.84M
  assert(::strlen(Fmt) == 2 &&
133
1.84M
         "Format string needs to be two characters long");
134
1.84M
  assert(::toupper(Fmt[0]) == Fmt[1] &&
135
1.84M
         "Format string is not in the form \"xX\"");
136
1.84M
137
1.84M
  const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
138
1.84M
  if (!Like)
139
1.78M
    return false;
140
55.2k
141
55.2k
  HasVAListArg = (*Like == Fmt[1]);
142
55.2k
143
55.2k
  ++Like;
144
55.2k
  assert(*Like == ':' && "Format specifier must be followed by a ':'");
145
55.2k
  ++Like;
146
55.2k
147
55.2k
  assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
148
55.2k
  FormatIdx = ::strtol(Like, nullptr, 10);
149
55.2k
  return true;
150
55.2k
}
151
152
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
153
920k
                                    bool &HasVAListArg) {
154
920k
  return isLike(ID, FormatIdx, HasVAListArg, "pP");
155
920k
}
156
157
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
158
920k
                                   bool &HasVAListArg) {
159
920k
  return isLike(ID, FormatIdx, HasVAListArg, "sS");
160
920k
}
161
162
bool Builtin::Context::performsCallback(unsigned ID,
163
920k
                                        SmallVectorImpl<int> &Encoding) const {
164
920k
  const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
165
920k
  if (!CalleePos)
166
919k
    return false;
167
328
168
328
  ++CalleePos;
169
328
  assert(*CalleePos == '<' &&
170
328
         "Callback callee specifier must be followed by a '<'");
171
328
  ++CalleePos;
172
328
173
328
  char *EndPos;
174
328
  int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
175
328
  assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
176
328
  Encoding.push_back(CalleeIdx);
177
328
178
656
  while (*EndPos == ',') {
179
328
    const char *PayloadPos = EndPos + 1;
180
328
181
328
    int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
182
328
    Encoding.push_back(PayloadIdx);
183
328
  }
184
328
185
328
  assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
186
328
  return true;
187
328
}
188
189
12.6M
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
190
12.6M
  return ID == Builtin::NotBuiltin ||
191
12.6M
         
ID == Builtin::BI__va_start310k
||
192
12.6M
         
(310k
!hasReferenceArgsOrResult(ID)310k
&&
193
310k
          
!hasCustomTypechecking(ID)310k
);
194
12.6M
}