18
18
#include " llvm/ADT/ArrayRef.h"
19
19
#include " llvm/ADT/StringMap.h"
20
20
#include " llvm/ADT/StringRef.h"
21
+ #include " llvm/ADT/StringTable.h"
21
22
#include < cstring>
22
23
23
24
// VC++ defines 'alloca' as an object-like macro, which interferes with our
@@ -55,6 +56,7 @@ struct HeaderDesc {
55
56
#undef HEADER
56
57
} ID;
57
58
59
+ constexpr HeaderDesc () : ID() {}
58
60
constexpr HeaderDesc (HeaderID ID) : ID(ID) {}
59
61
60
62
const char *getName () const ;
@@ -68,23 +70,144 @@ enum ID {
68
70
FirstTSBuiltin
69
71
};
70
72
73
+ // The info used to represent each builtin.
71
74
struct Info {
72
- llvm::StringLiteral Name;
73
- const char *Type, *Attributes;
74
- const char *Features;
75
+ // Rather than store pointers to the string literals describing these four
76
+ // aspects of builtins, we store offsets into a common string table.
77
+ struct StrOffsets {
78
+ llvm::StringTable::Offset Name;
79
+ llvm::StringTable::Offset Type;
80
+ llvm::StringTable::Offset Attributes;
81
+ llvm::StringTable::Offset Features;
82
+ } Offsets;
83
+
75
84
HeaderDesc Header;
76
85
LanguageID Langs;
77
86
};
78
87
88
+ // A constexpr function to construct an infos array from X-macros.
89
+ //
90
+ // The input array uses the same data structure, but the offsets are actually
91
+ // _lengths_ when input. This is all we can compute from the X-macro approach to
92
+ // builtins. This function will convert these lengths into actual offsets to a
93
+ // string table built up through sequentially appending strings with the given
94
+ // lengths.
95
+ template <size_t N>
96
+ static constexpr std::array<Info, N> MakeInfos (std::array<Info, N> Infos) {
97
+ // Translate lengths to offsets. We start past the initial empty string at
98
+ // offset zero.
99
+ unsigned Offset = 1 ;
100
+ for (auto &I : Infos) {
101
+ Info::StrOffsets NewOffsets = {};
102
+ NewOffsets.Name = Offset;
103
+ Offset += I.Offsets .Name .value ();
104
+ NewOffsets.Type = Offset;
105
+ Offset += I.Offsets .Type .value ();
106
+ NewOffsets.Attributes = Offset;
107
+ Offset += I.Offsets .Attributes .value ();
108
+ NewOffsets.Features = Offset;
109
+ Offset += I.Offsets .Features .value ();
110
+ I.Offsets = NewOffsets;
111
+ }
112
+ return Infos;
113
+ }
114
+
115
+ // A detail macro used below to emit a string literal that, after string literal
116
+ // concatenation, ends up triggering the `-Woverlength-strings` warning. While
117
+ // the warning is useful in general to catch accidentally excessive strings,
118
+ // here we are creating them intentionally.
119
+ //
120
+ // This relies on a subtle aspect of `_Pragma`: that the *diagnostic* ones don't
121
+ // turn into actual tokens that would disrupt string literal concatenation.
122
+ #ifdef __clang__
123
+ #define CLANG_BUILTIN_DETAIL_STR_TABLE (S ) \
124
+ _Pragma (" clang diagnostic push" ) \
125
+ _Pragma(" clang diagnostic ignored \" -Woverlength-strings\" " ) \
126
+ S _Pragma(" clang diagnostic pop" )
127
+ #else
128
+ #define CLANG_BUILTIN_DETAIL_STR_TABLE (S ) S
129
+ #endif
130
+
131
+ // We require string tables to start with an empty string so that a `0` offset
132
+ // can always be used to refer to an empty string. To satisfy that when building
133
+ // string tables with X-macros, we use this start macro prior to expanding the
134
+ // X-macros.
135
+ #define CLANG_BUILTIN_STR_TABLE_START CLANG_BUILTIN_DETAIL_STR_TABLE (" \0 " )
136
+
137
+ // A macro that can be used with `Builtins.def` and similar files as an X-macro
138
+ // to add the string arguments to a builtin string table. This is typically the
139
+ // target for the `BUILTIN`, `LANGBUILTIN`, or `LIBBUILTIN` macros in those
140
+ // files.
141
+ #define CLANG_BUILTIN_STR_TABLE (ID, TYPE, ATTRS ) \
142
+ CLANG_BUILTIN_DETAIL_STR_TABLE (#ID " \0 " TYPE " \0 " ATTRS " \0 " /* FEATURE*/ " \0 " )
143
+
144
+ // A macro that can be used with target builtin `.def` and `.inc` files as an
145
+ // X-macro to add the string arguments to a builtin string table. this is
146
+ // typically the target for the `TARGET_BUILTIN` macro.
147
+ #define CLANG_TARGET_BUILTIN_STR_TABLE (ID, TYPE, ATTRS, FEATURE ) \
148
+ CLANG_BUILTIN_DETAIL_STR_TABLE (#ID " \0 " TYPE " \0 " ATTRS " \0 " FEATURE " \0 " )
149
+
150
+ // A macro that can be used with target builtin `.def` and `.inc` files as an
151
+ // X-macro to add the string arguments to a builtin string table. this is
152
+ // typically the target for the `TARGET_HEADER_BUILTIN` macro. We can't delegate
153
+ // to `TARGET_BUILTIN` because the `FEATURE` string changes position.
154
+ #define CLANG_TARGET_HEADER_BUILTIN_STR_TABLE (ID, TYPE, ATTRS, HEADER, LANGS, \
155
+ FEATURE) \
156
+ CLANG_BUILTIN_DETAIL_STR_TABLE (#ID " \0 " TYPE " \0 " ATTRS " \0 " FEATURE " \0 " )
157
+
158
+ // A detail macro used internally to compute the desired string table
159
+ // `StrOffsets` struct for arguments to `MakeInfos`.
160
+ #define CLANG_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS ) \
161
+ Builtin::Info::StrOffsets { \
162
+ sizeof (#ID), sizeof (TYPE), sizeof (ATTRS), sizeof (" " ) \
163
+ }
164
+
165
+ // A detail macro used internally to compute the desired string table
166
+ // `StrOffsets` struct for arguments to `Storage::Make`.
167
+ #define CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS, FEATURE ) \
168
+ Builtin::Info::StrOffsets { \
169
+ sizeof (#ID), sizeof (TYPE), sizeof (ATTRS), sizeof (FEATURE) \
170
+ }
171
+
172
+ // A set of macros that can be used with builtin `.def' files as an X-macro to
173
+ // create an `Info` struct for a particular builtin. It both computes the
174
+ // `StrOffsets` value for the string table (the lengths here, translated to
175
+ // offsets by the `MakeInfos` function), and the other metadata for each
176
+ // builtin.
177
+ //
178
+ // There is a corresponding macro for each of `BUILTIN`, `LANGBUILTIN`,
179
+ // `LIBBUILTIN`, `TARGET_BUILTIN`, and `TARGET_HEADER_BUILTIN`.
180
+ #define CLANG_BUILTIN_ENTRY (ID, TYPE, ATTRS ) \
181
+ Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS), \
182
+ HeaderDesc::NO_HEADER, ALL_LANGUAGES},
183
+ #define CLANG_LANGBUILTIN_ENTRY (ID, TYPE, ATTRS, LANG ) \
184
+ Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS), \
185
+ HeaderDesc::NO_HEADER, LANG},
186
+ #define CLANG_LIBBUILTIN_ENTRY (ID, TYPE, ATTRS, HEADER, LANG ) \
187
+ Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS), \
188
+ HeaderDesc::HEADER, LANG},
189
+ #define CLANG_TARGET_BUILTIN_ENTRY (ID, TYPE, ATTRS, FEATURE ) \
190
+ Builtin::Info{ \
191
+ CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS, FEATURE), \
192
+ HeaderDesc::NO_HEADER, ALL_LANGUAGES},
193
+ #define CLANG_TARGET_HEADER_BUILTIN_ENTRY (ID, TYPE, ATTRS, HEADER, LANG, \
194
+ FEATURE) \
195
+ Builtin::Info{ \
196
+ CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS (ID, TYPE, ATTRS, FEATURE), \
197
+ HeaderDesc::HEADER, LANG},
198
+
79
199
// / Holds information about both target-independent and
80
200
// / target-specific builtins, allowing easy queries by clients.
81
201
// /
82
202
// / Builtins from an optional auxiliary target are stored in
83
203
// / AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
84
204
// / be translated back with getAuxBuiltinID() before use.
85
205
class Context {
86
- llvm::ArrayRef<Info> TSRecords;
87
- llvm::ArrayRef<Info> AuxTSRecords;
206
+ const llvm::StringTable *TSStrTable = nullptr ;
207
+ const llvm::StringTable *AuxTSStrTable = nullptr ;
208
+
209
+ llvm::ArrayRef<Info> TSInfos;
210
+ llvm::ArrayRef<Info> AuxTSInfos;
88
211
89
212
public:
90
213
Context () = default ;
@@ -100,10 +223,13 @@ class Context {
100
223
101
224
// / Return the identifier name for the specified builtin,
102
225
// / e.g. "__builtin_abs".
103
- llvm::StringRef getName (unsigned ID) const { return getRecord (ID). Name ; }
226
+ llvm::StringRef getName (unsigned ID) const ;
104
227
105
228
// / Get the type descriptor string for the specified builtin.
106
- const char *getTypeString (unsigned ID) const { return getRecord (ID).Type ; }
229
+ const char *getTypeString (unsigned ID) const ;
230
+
231
+ // / Get the attributes descriptor string for the specified builtin.
232
+ const char *getAttributesString (unsigned ID) const ;
107
233
108
234
// / Return true if this function is a target-specific builtin.
109
235
bool isTSBuiltin (unsigned ID) const {
@@ -112,40 +238,40 @@ class Context {
112
238
113
239
// / Return true if this function has no side effects.
114
240
bool isPure (unsigned ID) const {
115
- return strchr (getRecord (ID). Attributes , ' U' ) != nullptr ;
241
+ return strchr (getAttributesString (ID), ' U' ) != nullptr ;
116
242
}
117
243
118
244
// / Return true if this function has no side effects and doesn't
119
245
// / read memory.
120
246
bool isConst (unsigned ID) const {
121
- return strchr (getRecord (ID). Attributes , ' c' ) != nullptr ;
247
+ return strchr (getAttributesString (ID), ' c' ) != nullptr ;
122
248
}
123
249
124
250
// / Return true if we know this builtin never throws an exception.
125
251
bool isNoThrow (unsigned ID) const {
126
- return strchr (getRecord (ID). Attributes , ' n' ) != nullptr ;
252
+ return strchr (getAttributesString (ID), ' n' ) != nullptr ;
127
253
}
128
254
129
255
// / Return true if we know this builtin never returns.
130
256
bool isNoReturn (unsigned ID) const {
131
- return strchr (getRecord (ID). Attributes , ' r' ) != nullptr ;
257
+ return strchr (getAttributesString (ID), ' r' ) != nullptr ;
132
258
}
133
259
134
260
// / Return true if we know this builtin can return twice.
135
261
bool isReturnsTwice (unsigned ID) const {
136
- return strchr (getRecord (ID). Attributes , ' j' ) != nullptr ;
262
+ return strchr (getAttributesString (ID), ' j' ) != nullptr ;
137
263
}
138
264
139
265
// / Returns true if this builtin does not perform the side-effects
140
266
// / of its arguments.
141
267
bool isUnevaluated (unsigned ID) const {
142
- return strchr (getRecord (ID). Attributes , ' u' ) != nullptr ;
268
+ return strchr (getAttributesString (ID), ' u' ) != nullptr ;
143
269
}
144
270
145
271
// / Return true if this is a builtin for a libc/libm function,
146
272
// / with a "__builtin_" prefix (e.g. __builtin_abs).
147
273
bool isLibFunction (unsigned ID) const {
148
- return strchr (getRecord (ID). Attributes , ' F' ) != nullptr ;
274
+ return strchr (getAttributesString (ID), ' F' ) != nullptr ;
149
275
}
150
276
151
277
// / Determines whether this builtin is a predefined libc/libm
@@ -156,29 +282,29 @@ class Context {
156
282
// / they do not, but they are recognized as builtins once we see
157
283
// / a declaration.
158
284
bool isPredefinedLibFunction (unsigned ID) const {
159
- return strchr (getRecord (ID). Attributes , ' f' ) != nullptr ;
285
+ return strchr (getAttributesString (ID), ' f' ) != nullptr ;
160
286
}
161
287
162
288
// / Returns true if this builtin requires appropriate header in other
163
289
// / compilers. In Clang it will work even without including it, but we can emit
164
290
// / a warning about missing header.
165
291
bool isHeaderDependentFunction (unsigned ID) const {
166
- return strchr (getRecord (ID). Attributes , ' h' ) != nullptr ;
292
+ return strchr (getAttributesString (ID), ' h' ) != nullptr ;
167
293
}
168
294
169
295
// / Determines whether this builtin is a predefined compiler-rt/libgcc
170
296
// / function, such as "__clear_cache", where we know the signature a
171
297
// / priori.
172
298
bool isPredefinedRuntimeFunction (unsigned ID) const {
173
- return strchr (getRecord (ID). Attributes , ' i' ) != nullptr ;
299
+ return strchr (getAttributesString (ID), ' i' ) != nullptr ;
174
300
}
175
301
176
302
// / Determines whether this builtin is a C++ standard library function
177
303
// / that lives in (possibly-versioned) namespace std, possibly a template
178
304
// / specialization, where the signature is determined by the standard library
179
305
// / declaration.
180
306
bool isInStdNamespace (unsigned ID) const {
181
- return strchr (getRecord (ID). Attributes , ' z' ) != nullptr ;
307
+ return strchr (getAttributesString (ID), ' z' ) != nullptr ;
182
308
}
183
309
184
310
// / Determines whether this builtin can have its address taken with no
@@ -192,33 +318,33 @@ class Context {
192
318
193
319
// / Determines whether this builtin has custom typechecking.
194
320
bool hasCustomTypechecking (unsigned ID) const {
195
- return strchr (getRecord (ID). Attributes , ' t' ) != nullptr ;
321
+ return strchr (getAttributesString (ID), ' t' ) != nullptr ;
196
322
}
197
323
198
324
// / Determines whether a declaration of this builtin should be recognized
199
325
// / even if the type doesn't match the specified signature.
200
326
bool allowTypeMismatch (unsigned ID) const {
201
- return strchr (getRecord (ID). Attributes , ' T' ) != nullptr ||
327
+ return strchr (getAttributesString (ID), ' T' ) != nullptr ||
202
328
hasCustomTypechecking (ID);
203
329
}
204
330
205
331
// / Determines whether this builtin has a result or any arguments which
206
332
// / are pointer types.
207
333
bool hasPtrArgsOrResult (unsigned ID) const {
208
- return strchr (getRecord (ID). Type , ' *' ) != nullptr ;
334
+ return strchr (getTypeString (ID), ' *' ) != nullptr ;
209
335
}
210
336
211
337
// / Return true if this builtin has a result or any arguments which are
212
338
// / reference types.
213
339
bool hasReferenceArgsOrResult (unsigned ID) const {
214
- return strchr (getRecord (ID). Type , ' &' ) != nullptr ||
215
- strchr (getRecord (ID). Type , ' A' ) != nullptr ;
340
+ return strchr (getTypeString (ID), ' &' ) != nullptr ||
341
+ strchr (getTypeString (ID), ' A' ) != nullptr ;
216
342
}
217
343
218
344
// / If this is a library function that comes from a specific
219
345
// / header, retrieve that header name.
220
346
const char *getHeaderName (unsigned ID) const {
221
- return getRecord (ID).Header .getName ();
347
+ return getInfo (ID).Header .getName ();
222
348
}
223
349
224
350
// / Determine whether this builtin is like printf in its
@@ -243,27 +369,25 @@ class Context {
243
369
// / Such functions can be const when the MathErrno lang option and FP
244
370
// / exceptions are disabled.
245
371
bool isConstWithoutErrnoAndExceptions (unsigned ID) const {
246
- return strchr (getRecord (ID). Attributes , ' e' ) != nullptr ;
372
+ return strchr (getAttributesString (ID), ' e' ) != nullptr ;
247
373
}
248
374
249
375
bool isConstWithoutExceptions (unsigned ID) const {
250
- return strchr (getRecord (ID). Attributes , ' g' ) != nullptr ;
376
+ return strchr (getAttributesString (ID), ' g' ) != nullptr ;
251
377
}
252
378
253
- const char *getRequiredFeatures (unsigned ID) const {
254
- return getRecord (ID).Features ;
255
- }
379
+ const char *getRequiredFeatures (unsigned ID) const ;
256
380
257
381
unsigned getRequiredVectorWidth (unsigned ID) const ;
258
382
259
383
// / Return true if builtin ID belongs to AuxTarget.
260
384
bool isAuxBuiltinID (unsigned ID) const {
261
- return ID >= (Builtin::FirstTSBuiltin + TSRecords .size ());
385
+ return ID >= (Builtin::FirstTSBuiltin + TSInfos .size ());
262
386
}
263
387
264
388
// / Return real builtin ID (i.e. ID it would have during compilation
265
389
// / for AuxTarget).
266
- unsigned getAuxBuiltinID (unsigned ID) const { return ID - TSRecords .size (); }
390
+ unsigned getAuxBuiltinID (unsigned ID) const { return ID - TSInfos .size (); }
267
391
268
392
// / Returns true if this is a libc/libm function without the '__builtin_'
269
393
// / prefix.
@@ -275,16 +399,21 @@ class Context {
275
399
276
400
// / Return true if this function can be constant evaluated by Clang frontend.
277
401
bool isConstantEvaluated (unsigned ID) const {
278
- return strchr (getRecord (ID). Attributes , ' E' ) != nullptr ;
402
+ return strchr (getAttributesString (ID), ' E' ) != nullptr ;
279
403
}
280
404
281
405
// / Returns true if this is an immediate (consteval) function
282
406
bool isImmediate (unsigned ID) const {
283
- return strchr (getRecord (ID). Attributes , ' G' ) != nullptr ;
407
+ return strchr (getAttributesString (ID), ' G' ) != nullptr ;
284
408
}
285
409
286
410
private:
287
- const Info &getRecord (unsigned ID) const ;
411
+ std::pair<const llvm::StringTable &, const Info &>
412
+ getStrTableAndInfo (unsigned ID) const ;
413
+
414
+ const Info &getInfo (unsigned ID) const {
415
+ return getStrTableAndInfo (ID).second ;
416
+ }
288
417
289
418
// / Helper function for isPrintfLike and isScanfLike.
290
419
bool isLike (unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,
0 commit comments