/src/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 "BuiltinTargetFeatures.h" |
15 | | #include "clang/Basic/IdentifierTable.h" |
16 | | #include "clang/Basic/LangOptions.h" |
17 | | #include "clang/Basic/TargetInfo.h" |
18 | | #include "llvm/ADT/StringRef.h" |
19 | | using namespace clang; |
20 | | |
21 | 0 | const char *HeaderDesc::getName() const { |
22 | 0 | switch (ID) { |
23 | 0 | #define HEADER(ID, NAME) \ |
24 | 0 | case ID: \ |
25 | 0 | return NAME; |
26 | 0 | #include "clang/Basic/BuiltinHeaders.def" |
27 | 0 | #undef HEADER |
28 | 0 | }; |
29 | 0 | llvm_unreachable("Unknown HeaderDesc::HeaderID enum"); |
30 | 0 | } |
31 | | |
32 | | static constexpr Builtin::Info BuiltinInfo[] = { |
33 | | {"not a builtin function", nullptr, nullptr, nullptr, HeaderDesc::NO_HEADER, |
34 | | ALL_LANGUAGES}, |
35 | | #define BUILTIN(ID, TYPE, ATTRS) \ |
36 | | {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES}, |
37 | | #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \ |
38 | | {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, LANGS}, |
39 | | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \ |
40 | | {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, LANGS}, |
41 | | #include "clang/Basic/Builtins.def" |
42 | | }; |
43 | | |
44 | 5 | const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { |
45 | 5 | if (ID < Builtin::FirstTSBuiltin) |
46 | 5 | return BuiltinInfo[ID]; |
47 | 0 | assert(((ID - Builtin::FirstTSBuiltin) < |
48 | 0 | (TSRecords.size() + AuxTSRecords.size())) && |
49 | 0 | "Invalid builtin ID!"); |
50 | 0 | if (isAuxBuiltinID(ID)) |
51 | 0 | return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin]; |
52 | 0 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
53 | 0 | } |
54 | | |
55 | | void Builtin::Context::InitializeTarget(const TargetInfo &Target, |
56 | 46 | const TargetInfo *AuxTarget) { |
57 | 46 | assert(TSRecords.empty() && "Already initialized target?"); |
58 | 0 | TSRecords = Target.getTargetBuiltins(); |
59 | 46 | if (AuxTarget) |
60 | 0 | AuxTSRecords = AuxTarget->getTargetBuiltins(); |
61 | 46 | } |
62 | | |
63 | 0 | bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) { |
64 | 0 | bool InStdNamespace = FuncName.consume_front("std-"); |
65 | 0 | for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; |
66 | 0 | ++i) { |
67 | 0 | if (FuncName.equals(BuiltinInfo[i].Name) && |
68 | 0 | (bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace) |
69 | 0 | return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr; |
70 | 0 | } |
71 | | |
72 | 0 | return false; |
73 | 0 | } |
74 | | |
75 | | /// Is this builtin supported according to the given language options? |
76 | | static bool builtinIsSupported(const Builtin::Info &BuiltinInfo, |
77 | 156k | const LangOptions &LangOpts) { |
78 | | /* Builtins Unsupported */ |
79 | 156k | if (LangOpts.NoBuiltin && strchr(BuiltinInfo.Attributes, 'f') != nullptr) |
80 | 0 | return false; |
81 | | /* CorBuiltins Unsupported */ |
82 | 156k | if (!LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG)) |
83 | 644 | return false; |
84 | | /* MathBuiltins Unsupported */ |
85 | 156k | if (LangOpts.NoMathBuiltin && BuiltinInfo.Header.ID == HeaderDesc::MATH_H) |
86 | 0 | return false; |
87 | | /* GnuMode Unsupported */ |
88 | 156k | if (!LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG)) |
89 | 0 | return false; |
90 | | /* MSMode Unsupported */ |
91 | 156k | if (!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG)) |
92 | 6.02k | return false; |
93 | | /* ObjC Unsupported */ |
94 | 150k | if (!LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG) |
95 | 529 | return false; |
96 | | /* OpenCLC Unsupported */ |
97 | 149k | if (!LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES)) |
98 | 184 | return false; |
99 | | /* OopenCL GAS Unsupported */ |
100 | 149k | if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS)) |
101 | 138 | return false; |
102 | | /* OpenCL Pipe Unsupported */ |
103 | 149k | if (!LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE)) |
104 | 736 | return false; |
105 | | |
106 | | // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher |
107 | | // support is indicated with language option for blocks. |
108 | | |
109 | | /* OpenCL DSE Unsupported */ |
110 | 148k | if ((LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) && |
111 | 148k | (BuiltinInfo.Langs & OCL_DSE)) |
112 | 230 | return false; |
113 | | /* OpenMP Unsupported */ |
114 | 148k | if (!LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG) |
115 | 0 | return false; |
116 | | /* CUDA Unsupported */ |
117 | 148k | if (!LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG) |
118 | 46 | return false; |
119 | | /* CPlusPlus Unsupported */ |
120 | 148k | if (!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG) |
121 | 184 | return false; |
122 | 148k | return true; |
123 | 148k | } |
124 | | |
125 | | /// initializeBuiltins - Mark the identifiers for all the builtins with their |
126 | | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
127 | | /// such. |
128 | | void Builtin::Context::initializeBuiltins(IdentifierTable &Table, |
129 | 46 | const LangOptions& LangOpts) { |
130 | | // Step #1: mark all target-independent builtins with their ID's. |
131 | 62.7k | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
132 | 62.6k | if (builtinIsSupported(BuiltinInfo[i], LangOpts)) { |
133 | 55.8k | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
134 | 55.8k | } |
135 | | |
136 | | // Step #2: Register target-specific builtins. |
137 | 94.1k | for (unsigned i = 0, e = TSRecords.size(); i != e; ++i) |
138 | 94.0k | if (builtinIsSupported(TSRecords[i], LangOpts)) |
139 | 92.1k | Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); |
140 | | |
141 | | // Step #3: Register target-specific builtins for AuxTarget. |
142 | 46 | for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) |
143 | 0 | Table.get(AuxTSRecords[i].Name) |
144 | 0 | .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); |
145 | | |
146 | | // Step #4: Unregister any builtins specified by -fno-builtin-foo. |
147 | 46 | for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) { |
148 | 0 | bool InStdNamespace = Name.consume_front("std-"); |
149 | 0 | auto NameIt = Table.find(Name); |
150 | 0 | if (NameIt != Table.end()) { |
151 | 0 | unsigned ID = NameIt->second->getBuiltinID(); |
152 | 0 | if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) && |
153 | 0 | isInStdNamespace(ID) == InStdNamespace) { |
154 | 0 | NameIt->second->clearBuiltinID(); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } |
158 | 46 | } |
159 | | |
160 | 0 | unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { |
161 | 0 | const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V'); |
162 | 0 | if (!WidthPos) |
163 | 0 | return 0; |
164 | | |
165 | 0 | ++WidthPos; |
166 | 0 | assert(*WidthPos == ':' && |
167 | 0 | "Vector width specifier must be followed by a ':'"); |
168 | 0 | ++WidthPos; |
169 | |
|
170 | 0 | char *EndPos; |
171 | 0 | unsigned Width = ::strtol(WidthPos, &EndPos, 10); |
172 | 0 | assert(*EndPos == ':' && "Vector width specific must end with a ':'"); |
173 | 0 | return Width; |
174 | 0 | } |
175 | | |
176 | | bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, |
177 | 0 | bool &HasVAListArg, const char *Fmt) const { |
178 | 0 | assert(Fmt && "Not passed a format string"); |
179 | 0 | assert(::strlen(Fmt) == 2 && |
180 | 0 | "Format string needs to be two characters long"); |
181 | 0 | assert(::toupper(Fmt[0]) == Fmt[1] && |
182 | 0 | "Format string is not in the form \"xX\""); |
183 | | |
184 | 0 | const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt); |
185 | 0 | if (!Like) |
186 | 0 | return false; |
187 | | |
188 | 0 | HasVAListArg = (*Like == Fmt[1]); |
189 | |
|
190 | 0 | ++Like; |
191 | 0 | assert(*Like == ':' && "Format specifier must be followed by a ':'"); |
192 | 0 | ++Like; |
193 | |
|
194 | 0 | assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); |
195 | 0 | FormatIdx = ::strtol(Like, nullptr, 10); |
196 | 0 | return true; |
197 | 0 | } |
198 | | |
199 | | bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
200 | 0 | bool &HasVAListArg) { |
201 | 0 | return isLike(ID, FormatIdx, HasVAListArg, "pP"); |
202 | 0 | } |
203 | | |
204 | | bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
205 | 0 | bool &HasVAListArg) { |
206 | 0 | return isLike(ID, FormatIdx, HasVAListArg, "sS"); |
207 | 0 | } |
208 | | |
209 | | bool Builtin::Context::performsCallback(unsigned ID, |
210 | 0 | SmallVectorImpl<int> &Encoding) const { |
211 | 0 | const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C'); |
212 | 0 | if (!CalleePos) |
213 | 0 | return false; |
214 | | |
215 | 0 | ++CalleePos; |
216 | 0 | assert(*CalleePos == '<' && |
217 | 0 | "Callback callee specifier must be followed by a '<'"); |
218 | 0 | ++CalleePos; |
219 | |
|
220 | 0 | char *EndPos; |
221 | 0 | int CalleeIdx = ::strtol(CalleePos, &EndPos, 10); |
222 | 0 | assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!"); |
223 | 0 | Encoding.push_back(CalleeIdx); |
224 | |
|
225 | 0 | while (*EndPos == ',') { |
226 | 0 | const char *PayloadPos = EndPos + 1; |
227 | |
|
228 | 0 | int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10); |
229 | 0 | Encoding.push_back(PayloadIdx); |
230 | 0 | } |
231 | |
|
232 | 0 | assert(*EndPos == '>' && "Callback callee specifier must end with a '>'"); |
233 | 0 | return true; |
234 | 0 | } |
235 | | |
236 | 0 | bool Builtin::Context::canBeRedeclared(unsigned ID) const { |
237 | 0 | return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start || |
238 | 0 | ID == Builtin::BI__builtin_assume_aligned || |
239 | 0 | (!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) || |
240 | 0 | isInStdNamespace(ID); |
241 | 0 | } |
242 | | |
243 | | bool Builtin::evaluateRequiredTargetFeatures( |
244 | 0 | StringRef RequiredFeatures, const llvm::StringMap<bool> &TargetFetureMap) { |
245 | | // Return true if the builtin doesn't have any required features. |
246 | 0 | if (RequiredFeatures.empty()) |
247 | 0 | return true; |
248 | 0 | assert(!RequiredFeatures.contains(' ') && "Space in feature list"); |
249 | | |
250 | 0 | TargetFeatures TF(TargetFetureMap); |
251 | 0 | return TF.hasRequiredFeatures(RequiredFeatures); |
252 | 0 | } |