Coverage Report

Created: 2022-05-14 11:35

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/Builtins.h
Line
Count
Source
1
//===--- Builtins.h - Builtin function header -------------------*- C++ -*-===//
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
/// \file
10
/// Defines enum values for all the target-independent builtin
11
/// functions.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CLANG_BASIC_BUILTINS_H
16
#define LLVM_CLANG_BASIC_BUILTINS_H
17
18
#include "llvm/ADT/ArrayRef.h"
19
#include <cstring>
20
21
// VC++ defines 'alloca' as an object-like macro, which interferes with our
22
// builtins.
23
#undef alloca
24
25
namespace clang {
26
class TargetInfo;
27
class IdentifierTable;
28
class LangOptions;
29
30
enum LanguageID {
31
  GNU_LANG = 0x1,            // builtin requires GNU mode.
32
  C_LANG = 0x2,              // builtin for c only.
33
  CXX_LANG = 0x4,            // builtin for cplusplus only.
34
  OBJC_LANG = 0x8,           // builtin for objective-c and objective-c++
35
  MS_LANG = 0x10,            // builtin requires MS mode.
36
  OMP_LANG = 0x20,           // builtin requires OpenMP.
37
  CUDA_LANG = 0x40,          // builtin requires CUDA.
38
  COR_LANG = 0x80,           // builtin requires use of 'fcoroutine-ts' option.
39
  OCL_GAS = 0x100,           // builtin requires OpenCL generic address space.
40
  OCL_PIPE = 0x200,          // builtin requires OpenCL pipe.
41
  OCL_DSE = 0x400,           // builtin requires OpenCL device side enqueue.
42
  ALL_OCL_LANGUAGES = 0x800, // builtin for OCL languages.
43
  ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages.
44
  ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG,  // builtin requires GNU mode.
45
  ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG     // builtin requires MS mode.
46
};
47
48
namespace Builtin {
49
enum ID {
50
  NotBuiltin  = 0,      // This is not a builtin function.
51
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
52
#include "clang/Basic/Builtins.def"
53
  FirstTSBuiltin
54
};
55
56
struct Info {
57
  const char *Name, *Type, *Attributes, *HeaderName;
58
  LanguageID Langs;
59
  const char *Features;
60
};
61
62
/// Holds information about both target-independent and
63
/// target-specific builtins, allowing easy queries by clients.
64
///
65
/// Builtins from an optional auxiliary target are stored in
66
/// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
67
/// be translated back with getAuxBuiltinID() before use.
68
class Context {
69
  llvm::ArrayRef<Info> TSRecords;
70
  llvm::ArrayRef<Info> AuxTSRecords;
71
72
public:
73
97.4k
  Context() = default;
74
75
  /// Perform target-specific initialization
76
  /// \param AuxTarget Target info to incorporate builtins from. May be nullptr.
77
  void InitializeTarget(const TargetInfo &Target, const TargetInfo *AuxTarget);
78
79
  /// Mark the identifiers for all the builtins with their
80
  /// appropriate builtin ID # and mark any non-portable builtin identifiers as
81
  /// such.
82
  void initializeBuiltins(IdentifierTable &Table, const LangOptions& LangOpts);
83
84
  /// Return the identifier name for the specified builtin,
85
  /// e.g. "__builtin_abs".
86
132k
  const char *getName(unsigned ID) const {
87
132k
    return getRecord(ID).Name;
88
132k
  }
89
90
  /// Get the type descriptor string for the specified builtin.
91
2.68M
  const char *getTypeString(unsigned ID) const {
92
2.68M
    return getRecord(ID).Type;
93
2.68M
  }
94
95
  /// Return true if this function is a target-specific builtin.
96
1.64M
  bool isTSBuiltin(unsigned ID) const {
97
1.64M
    return ID >= Builtin::FirstTSBuiltin;
98
1.64M
  }
99
100
  /// Return true if this function has no side effects.
101
19.0M
  bool isPure(unsigned ID) const {
102
19.0M
    return strchr(getRecord(ID).Attributes, 'U') != nullptr;
103
19.0M
  }
104
105
  /// Return true if this function has no side effects and doesn't
106
  /// read memory.
107
19.0M
  bool isConst(unsigned ID) const {
108
19.0M
    return strchr(getRecord(ID).Attributes, 'c') != nullptr;
109
19.0M
  }
110
111
  /// Return true if we know this builtin never throws an exception.
112
19.6M
  bool isNoThrow(unsigned ID) const {
113
19.6M
    return strchr(getRecord(ID).Attributes, 'n') != nullptr;
114
19.6M
  }
115
116
  /// Return true if we know this builtin never returns.
117
2.67M
  bool isNoReturn(unsigned ID) const {
118
2.67M
    return strchr(getRecord(ID).Attributes, 'r') != nullptr;
119
2.67M
  }
120
121
  /// Return true if we know this builtin can return twice.
122
19.0M
  bool isReturnsTwice(unsigned ID) const {
123
19.0M
    return strchr(getRecord(ID).Attributes, 'j') != nullptr;
124
19.0M
  }
125
126
  /// Returns true if this builtin does not perform the side-effects
127
  /// of its arguments.
128
1.60M
  bool isUnevaluated(unsigned ID) const {
129
1.60M
    return strchr(getRecord(ID).Attributes, 'u') != nullptr;
130
1.60M
  }
131
132
  /// Return true if this is a builtin for a libc/libm function,
133
  /// with a "__builtin_" prefix (e.g. __builtin_abs).
134
60.8k
  bool isLibFunction(unsigned ID) const {
135
60.8k
    return strchr(getRecord(ID).Attributes, 'F') != nullptr;
136
60.8k
  }
137
138
  /// Determines whether this builtin is a predefined libc/libm
139
  /// function, such as "malloc", where we know the signature a
140
  /// priori.
141
  /// In C, such functions behave as if they are predeclared,
142
  /// possibly with a warning on first use. In Objective-C and C++,
143
  /// they do not, but they are recognized as builtins once we see
144
  /// a declaration.
145
87.0M
  bool isPredefinedLibFunction(unsigned ID) const {
146
87.0M
    return strchr(getRecord(ID).Attributes, 'f') != nullptr;
147
87.0M
  }
148
149
  /// Returns true if this builtin requires appropriate header in other
150
  /// compilers. In Clang it will work even without including it, but we can emit
151
  /// a warning about missing header.
152
783k
  bool isHeaderDependentFunction(unsigned ID) const {
153
783k
    return strchr(getRecord(ID).Attributes, 'h') != nullptr;
154
783k
  }
155
156
  /// Determines whether this builtin is a predefined compiler-rt/libgcc
157
  /// function, such as "__clear_cache", where we know the signature a
158
  /// priori.
159
3
  bool isPredefinedRuntimeFunction(unsigned ID) const {
160
3
    return strchr(getRecord(ID).Attributes, 'i') != nullptr;
161
3
  }
162
163
  /// Determines whether this builtin is a C++ standard library function
164
  /// that lives in (possibly-versioned) namespace std, possibly a template
165
  /// specialization, where the signature is determined by the standard library
166
  /// declaration.
167
50.4k
  bool isInStdNamespace(unsigned ID) const {
168
50.4k
    return strchr(getRecord(ID).Attributes, 'z') != nullptr;
169
50.4k
  }
170
171
  /// Determines whether this builtin can have its address taken with no
172
  /// special action required.
173
1.82M
  bool isDirectlyAddressable(unsigned ID) const {
174
    // Most standard library functions can have their addresses taken. C++
175
    // standard library functions formally cannot in C++20 onwards, and when
176
    // we allow it, we need to ensure we instantiate a definition.
177
1.82M
    return isPredefinedLibFunction(ID) && 
!isInStdNamespace(ID)15.5k
;
178
1.82M
  }
179
180
  /// Determines whether this builtin has custom typechecking.
181
51.0M
  bool hasCustomTypechecking(unsigned ID) const {
182
51.0M
    return strchr(getRecord(ID).Attributes, 't') != nullptr;
183
51.0M
  }
184
185
  /// Determines whether a declaration of this builtin should be recognized
186
  /// even if the type doesn't match the specified signature.
187
22.7k
  bool allowTypeMismatch(unsigned ID) const {
188
22.7k
    return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
189
22.7k
           
hasCustomTypechecking(ID)22.6k
;
190
22.7k
  }
191
192
  /// Determines whether this builtin has a result or any arguments which
193
  /// are pointer types.
194
1.78M
  bool hasPtrArgsOrResult(unsigned ID) const {
195
1.78M
    return strchr(getRecord(ID).Type, '*') != nullptr;
196
1.78M
  }
197
198
  /// Return true if this builtin has a result or any arguments which are
199
  /// reference types.
200
45.8M
  bool hasReferenceArgsOrResult(unsigned ID) const {
201
45.8M
    return strchr(getRecord(ID).Type, '&') != nullptr ||
202
45.8M
           
strchr(getRecord(ID).Type, 'A') != nullptr45.8M
;
203
45.8M
  }
204
205
  /// If this is a library function that comes from a specific
206
  /// header, retrieve that header name.
207
1.44k
  const char *getHeaderName(unsigned ID) const {
208
1.44k
    return getRecord(ID).HeaderName;
209
1.44k
  }
210
211
  /// Determine whether this builtin is like printf in its
212
  /// formatting rules and, if so, set the index to the format string
213
  /// argument and whether this function as a va_list argument.
214
  bool isPrintfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
215
216
  /// Determine whether this builtin is like scanf in its
217
  /// formatting rules and, if so, set the index to the format string
218
  /// argument and whether this function as a va_list argument.
219
  bool isScanfLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg);
220
221
  /// Determine whether this builtin has callback behavior (see
222
  /// llvm::AbstractCallSites for details). If so, add the index to the
223
  /// callback callee argument and the callback payload arguments.
224
  bool performsCallback(unsigned ID,
225
                        llvm::SmallVectorImpl<int> &Encoding) const;
226
227
  /// Return true if this function has no side effects and doesn't
228
  /// read memory, except for possibly errno.
229
  ///
230
  /// Such functions can be const when the MathErrno lang option is disabled.
231
19.0M
  bool isConstWithoutErrno(unsigned ID) const {
232
19.0M
    return strchr(getRecord(ID).Attributes, 'e') != nullptr;
233
19.0M
  }
234
235
56.5k
  const char *getRequiredFeatures(unsigned ID) const {
236
56.5k
    return getRecord(ID).Features;
237
56.5k
  }
238
239
  unsigned getRequiredVectorWidth(unsigned ID) const;
240
241
  /// Return true if builtin ID belongs to AuxTarget.
242
403M
  bool isAuxBuiltinID(unsigned ID) const {
243
403M
    return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
244
403M
  }
245
246
  /// Return real builtin ID (i.e. ID it would have during compilation
247
  /// for AuxTarget).
248
64
  unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
249
250
  /// Returns true if this is a libc/libm function without the '__builtin_'
251
  /// prefix.
252
  static bool isBuiltinFunc(llvm::StringRef Name);
253
254
  /// Returns true if this is a builtin that can be redeclared.  Returns true
255
  /// for non-builtins.
256
  bool canBeRedeclared(unsigned ID) const;
257
258
private:
259
  const Info &getRecord(unsigned ID) const;
260
261
  /// Helper function for isPrintfLike and isScanfLike.
262
  bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
263
              const char *Fmt) const;
264
};
265
266
}
267
268
/// Kinds of BuiltinTemplateDecl.
269
enum BuiltinTemplateKind : int {
270
  /// This names the __make_integer_seq BuiltinTemplateDecl.
271
  BTK__make_integer_seq,
272
273
  /// This names the __type_pack_element BuiltinTemplateDecl.
274
  BTK__type_pack_element
275
};
276
277
} // end namespace clang
278
#endif