/src/llvm-project/clang-tools-extra/pseudo/lib/cxx/CXX.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- CXX.cpp - Define public interfaces for C++ grammar ---------------===// |
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 | | #include "clang-pseudo/cxx/CXX.h" |
10 | | #include "clang-pseudo/Forest.h" |
11 | | #include "clang-pseudo/Language.h" |
12 | | #include "clang-pseudo/grammar/Grammar.h" |
13 | | #include "clang-pseudo/grammar/LRTable.h" |
14 | | #include "clang/Basic/CharInfo.h" |
15 | | #include "clang/Basic/TokenKinds.h" |
16 | | #include "llvm/ADT/StringSwitch.h" |
17 | | #include "llvm/Support/Debug.h" |
18 | | #include <utility> |
19 | | #define DEBUG_TYPE "CXX.cpp" |
20 | | |
21 | | namespace clang { |
22 | | namespace pseudo { |
23 | | namespace cxx { |
24 | | namespace { |
25 | | static const char *CXXBNF = |
26 | | #include "CXXBNF.inc" |
27 | | ; |
28 | | |
29 | | // User-defined string literals look like `""suffix`. |
30 | 10.2k | bool isStringUserDefined(const Token &Tok) { |
31 | 10.2k | return !Tok.text().ends_with("\""); |
32 | 10.2k | } |
33 | 7.51k | bool isCharUserDefined(const Token &Tok) { return !Tok.text().ends_with("'"); } |
34 | | |
35 | | // Combinable flags describing numbers. |
36 | | // Clang has just one numeric_token kind, the grammar has 4. |
37 | | enum NumericKind { |
38 | | Integer = 0, |
39 | | Floating = 1 << 0, |
40 | | UserDefined = 1 << 1, |
41 | | }; |
42 | | // Determine the kind of numeric_constant we have. |
43 | | // We can assume it's something valid, as it has been lexed. |
44 | | // FIXME: is this expensive enough that we should set flags on the token |
45 | | // and reuse them rather than computing it for each guard? |
46 | 4.80M | unsigned numKind(const Token &Tok) { |
47 | 4.80M | assert(Tok.Kind == tok::numeric_constant); |
48 | 0 | llvm::StringRef Text = Tok.text(); |
49 | 4.80M | if (Text.size() <= 1) |
50 | 876k | return Integer; |
51 | 3.93M | bool Hex = |
52 | 3.93M | Text.size() > 2 && Text[0] == '0' && (Text[1] == 'x' || Text[1] == 'X'); |
53 | 3.93M | uint8_t K = Integer; |
54 | | |
55 | 48.4M | for (char C : Text) { |
56 | 48.4M | switch (C) { |
57 | 2.94k | case '.': |
58 | 2.94k | K |= Floating; |
59 | 2.94k | break; |
60 | 3.80k | case 'e': |
61 | 8.18k | case 'E': |
62 | 8.18k | if (!Hex) |
63 | 7.42k | K |= Floating; |
64 | 8.18k | break; |
65 | 3.40k | case 'p': |
66 | 7.72k | case 'P': |
67 | 7.72k | if (Hex) |
68 | 5.16k | K |= Floating; |
69 | 7.72k | break; |
70 | 1.99k | case '_': |
71 | 1.99k | K |= UserDefined; |
72 | 1.99k | break; |
73 | 48.4M | default: |
74 | 48.4M | break; |
75 | 48.4M | } |
76 | 48.4M | } |
77 | | |
78 | | // We would be done here, but there are stdlib UDLs that lack _. |
79 | | // We must distinguish these from the builtin suffixes. |
80 | 3.93M | unsigned LastLetter = Text.size(); |
81 | 37.7M | while (LastLetter > 0 && isLetter(Text[LastLetter - 1])) |
82 | 33.7M | --LastLetter; |
83 | 3.93M | if (LastLetter == Text.size()) // Common case |
84 | 9.02k | return NumericKind(K); |
85 | | // Trailing d/e/f are not part of the suffix in hex numbers. |
86 | 5.08M | while (Hex && LastLetter < Text.size() && isHexDigit(Text[LastLetter])) |
87 | 1.16M | ++LastLetter; |
88 | 3.92M | return llvm::StringSwitch<int, unsigned>(Text.substr(LastLetter)) |
89 | | // std::chrono |
90 | 3.92M | .Cases("h", "min", "s", "ms", "us", "ns", "d", "y", K | UserDefined) |
91 | | // complex |
92 | 3.92M | .Cases("il", "i", "if", K | UserDefined) |
93 | 3.92M | .Default(K); |
94 | 3.93M | } |
95 | | |
96 | | // RHS is expected to contain a single terminal. |
97 | | // Returns the corresponding token. |
98 | | const Token &onlyToken(tok::TokenKind Kind, |
99 | | const ArrayRef<const ForestNode *> RHS, |
100 | 4.87M | const TokenStream &Tokens) { |
101 | 4.87M | assert(RHS.size() == 1 && RHS.front()->symbol() == tokenSymbol(Kind)); |
102 | 0 | return Tokens.tokens()[RHS.front()->startTokenIndex()]; |
103 | 4.87M | } |
104 | | // RHS is expected to contain a single symbol. |
105 | | // Returns the corresponding ForestNode. |
106 | | const ForestNode &onlySymbol(SymbolID Kind, |
107 | | const ArrayRef<const ForestNode *> RHS, |
108 | 283k | const TokenStream &Tokens) { |
109 | 283k | assert(RHS.size() == 1 && RHS.front()->symbol() == Kind); |
110 | 0 | return *RHS.front(); |
111 | 283k | } |
112 | | |
113 | 283k | bool isFunctionDeclarator(const ForestNode *Declarator) { |
114 | 283k | assert(Declarator->symbol() == cxx::Symbol::declarator); |
115 | 0 | bool IsFunction = false; |
116 | 850k | while (true) { |
117 | | // not well-formed code, return the best guess. |
118 | 850k | if (Declarator->kind() != ForestNode::Sequence) |
119 | 2.04k | return IsFunction; |
120 | | |
121 | 848k | switch (Declarator->rule()) { |
122 | 280k | case rule::noptr_declarator::declarator_id: // reached the bottom |
123 | 280k | return IsFunction; |
124 | | // *X is a nonfunction (unless X is a function). |
125 | 1.35k | case rule::ptr_declarator::ptr_operator__ptr_declarator: |
126 | 1.35k | Declarator = Declarator->elements()[1]; |
127 | 1.35k | IsFunction = false; |
128 | 1.35k | continue; |
129 | | // X() is a function (unless X is a pointer or similar). |
130 | 666 | case rule::declarator:: |
131 | 666 | noptr_declarator__parameters_and_qualifiers__trailing_return_type: |
132 | 2.16k | case rule::noptr_declarator::noptr_declarator__parameters_and_qualifiers: |
133 | 2.16k | Declarator = Declarator->elements()[0]; |
134 | 2.16k | IsFunction = true; |
135 | 2.16k | continue; |
136 | | // X[] is an array (unless X is a pointer or function). |
137 | 1.76k | case rule::noptr_declarator:: |
138 | 1.76k | noptr_declarator__L_SQUARE__constant_expression__R_SQUARE: |
139 | 1.98k | case rule::noptr_declarator::noptr_declarator__L_SQUARE__R_SQUARE: |
140 | 1.98k | Declarator = Declarator->elements()[0]; |
141 | 1.98k | IsFunction = false; |
142 | 1.98k | continue; |
143 | | // (X) is whatever X is. |
144 | 531 | case rule::noptr_declarator::L_PAREN__ptr_declarator__R_PAREN: |
145 | 531 | Declarator = Declarator->elements()[1]; |
146 | 531 | continue; |
147 | 280k | case rule::ptr_declarator::noptr_declarator: |
148 | 561k | case rule::declarator::ptr_declarator: |
149 | 561k | Declarator = Declarator->elements()[0]; |
150 | 561k | continue; |
151 | | |
152 | 0 | default: |
153 | 0 | assert(false && "unhandled declarator for IsFunction"); |
154 | 0 | return IsFunction; |
155 | 848k | } |
156 | 848k | } |
157 | 0 | llvm_unreachable("unreachable"); |
158 | 0 | } |
159 | | |
160 | 22 | bool guardNextTokenNotElse(const GuardParams &P) { |
161 | 22 | return symbolToToken(P.Lookahead) != tok::kw_else; |
162 | 22 | } |
163 | | |
164 | 598 | bool specifiesStructuredBinding(const GuardParams &P) { |
165 | 598 | const auto DSS = P.RHS[0]; |
166 | 598 | assert(DSS->symbol() == Symbol::decl_specifier_seq); |
167 | | |
168 | 0 | auto Length = P.RHS[1]->startTokenIndex() - DSS->startTokenIndex(); |
169 | 598 | for (const auto &T : |
170 | 748 | P.Tokens.tokens().slice(DSS->startTokenIndex(), Length)) { |
171 | 748 | switch (T.Kind) { |
172 | 111 | case clang::tok::kw_static: |
173 | 111 | case clang::tok::kw_thread_local: |
174 | 111 | case clang::tok::kw_auto: |
175 | 171 | case clang::tok::kw_const: |
176 | 171 | case clang::tok::kw_volatile: |
177 | 171 | break; |
178 | 577 | default: |
179 | 577 | return false; |
180 | 748 | } |
181 | 748 | } |
182 | 21 | return true; |
183 | 598 | } |
184 | | |
185 | | // Whether this e.g. decl-specifier contains an "exclusive" type such as a class |
186 | | // name, and thus can't combine with a second exclusive type. |
187 | | // |
188 | | // Returns false for |
189 | | // - non-types |
190 | | // - "unsigned" etc that may suffice as types but may modify others |
191 | | // - cases of uncertainty (e.g. due to ambiguity) |
192 | 71.2M | bool hasExclusiveType(const ForestNode *N) { |
193 | | // FIXME: every time we apply this check, we walk the whole subtree. |
194 | | // Add per-node caching instead. |
195 | 88.6M | while (true) { |
196 | 88.6M | assert(N->symbol() == Symbol::decl_specifier_seq || |
197 | 88.6M | N->symbol() == Symbol::type_specifier_seq || |
198 | 88.6M | N->symbol() == Symbol::defining_type_specifier_seq || |
199 | 88.6M | N->symbol() == Symbol::decl_specifier || |
200 | 88.6M | N->symbol() == Symbol::type_specifier || |
201 | 88.6M | N->symbol() == Symbol::defining_type_specifier || |
202 | 88.6M | N->symbol() == Symbol::simple_type_specifier); |
203 | 88.6M | if (N->kind() == ForestNode::Opaque) |
204 | 0 | return false; // conservative |
205 | 88.6M | if (N->kind() == ForestNode::Ambiguous) |
206 | 6.22M | return llvm::all_of(N->alternatives(), hasExclusiveType); // conservative |
207 | | // All supported symbols are nonterminals. |
208 | 82.4M | assert(N->kind() == ForestNode::Sequence); |
209 | 0 | switch (N->rule()) { |
210 | | // seq := element seq: check element then continue into seq |
211 | 1.46k | case rule::decl_specifier_seq::decl_specifier__decl_specifier_seq: |
212 | 1.46k | case rule::defining_type_specifier_seq::defining_type_specifier__defining_type_specifier_seq: |
213 | 2.47k | case rule::type_specifier_seq::type_specifier__type_specifier_seq: |
214 | 2.47k | if (hasExclusiveType(N->children()[0])) |
215 | 934 | return true; |
216 | 1.54k | N = N->children()[1]; |
217 | 1.54k | continue; |
218 | | // seq := element: continue into element |
219 | 1.17M | case rule::decl_specifier_seq::decl_specifier: |
220 | 4.25M | case rule::type_specifier_seq::type_specifier: |
221 | 4.25M | case rule::defining_type_specifier_seq::defining_type_specifier: |
222 | 4.25M | N = N->children()[0]; |
223 | 4.25M | continue; |
224 | | |
225 | | // defining-type-specifier |
226 | 2.34M | case rule::defining_type_specifier::type_specifier: |
227 | 2.34M | N = N->children()[0]; |
228 | 2.34M | continue; |
229 | 50 | case rule::defining_type_specifier::class_specifier: |
230 | 117 | case rule::defining_type_specifier::enum_specifier: |
231 | 117 | return true; |
232 | | |
233 | | // decl-specifier |
234 | 2.34M | case rule::decl_specifier::defining_type_specifier: |
235 | 2.34M | N = N->children()[0]; |
236 | 2.34M | continue; |
237 | 0 | case rule::decl_specifier::CONSTEVAL: |
238 | 0 | case rule::decl_specifier::CONSTEXPR: |
239 | 0 | case rule::decl_specifier::CONSTINIT: |
240 | 254 | case rule::decl_specifier::INLINE: |
241 | 254 | case rule::decl_specifier::FRIEND: |
242 | 664 | case rule::decl_specifier::storage_class_specifier: |
243 | 764 | case rule::decl_specifier::TYPEDEF: |
244 | 1.05k | case rule::decl_specifier::function_specifier: |
245 | 1.05k | return false; |
246 | | |
247 | | // type-specifier |
248 | 6.29k | case rule::type_specifier::elaborated_type_specifier: |
249 | 6.29k | case rule::type_specifier::typename_specifier: |
250 | 6.29k | return true; |
251 | 8.51M | case rule::type_specifier::simple_type_specifier: |
252 | 8.51M | N = N->children()[0]; |
253 | 8.51M | continue; |
254 | 859 | case rule::type_specifier::cv_qualifier: |
255 | 859 | return false; |
256 | | |
257 | | // simple-type-specifier |
258 | 3.91M | case rule::simple_type_specifier::type_name: |
259 | 4.11M | case rule::simple_type_specifier::template_name: |
260 | 4.11M | case rule::simple_type_specifier::builtin_type: |
261 | 4.65M | case rule::simple_type_specifier::nested_name_specifier__TEMPLATE__simple_template_id: |
262 | 5.73M | case rule::simple_type_specifier::nested_name_specifier__template_name: |
263 | 65.0M | case rule::simple_type_specifier::nested_name_specifier__type_name: |
264 | 65.0M | case rule::simple_type_specifier::decltype_specifier: |
265 | 65.0M | case rule::simple_type_specifier::placeholder_type_specifier: |
266 | 65.0M | return true; |
267 | 688 | case rule::simple_type_specifier::LONG: |
268 | 1.10k | case rule::simple_type_specifier::SHORT: |
269 | 1.18k | case rule::simple_type_specifier::SIGNED: |
270 | 1.57k | case rule::simple_type_specifier::UNSIGNED: |
271 | 1.57k | return false; |
272 | | |
273 | 0 | default: |
274 | 0 | LLVM_DEBUG(llvm::errs() << "Unhandled rule " << N->rule() << "\n"); |
275 | 0 | llvm_unreachable("hasExclusiveType be exhaustive!"); |
276 | 82.4M | } |
277 | 82.4M | } |
278 | 71.2M | } |
279 | | |
280 | 1 | llvm::DenseMap<ExtensionID, RuleGuard> buildGuards() { |
281 | 1 | #define GUARD(cond) \ |
282 | 3 | { \ |
283 | 8.51M | [](const GuardParams &P) { return cond; } \ Unexecuted instantiation: CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_34::operator()(clang::pseudo::GuardParams const&) const CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_35::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 283 | 6.17M | [](const GuardParams &P) { return cond; } \ |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_36::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 283 | 2.34M | [](const GuardParams &P) { return cond; } \ |
|
284 | 3 | } |
285 | 1 | #define TOKEN_GUARD(kind, cond) \ |
286 | 4.87M | [](const GuardParams& P) { \ |
287 | 4.87M | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ |
288 | 4.87M | return cond; \ |
289 | 4.87M | } CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_2::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 491 | [](const GuardParams& P) { \ | 287 | 491 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 491 | return cond; \ | 289 | 491 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_3::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 491 | [](const GuardParams& P) { \ | 287 | 491 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 491 | return cond; \ | 289 | 491 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_4::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 11.9k | [](const GuardParams& P) { \ | 287 | 11.9k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 11.9k | return cond; \ | 289 | 11.9k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_5::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 16.4k | [](const GuardParams& P) { \ | 287 | 16.4k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 16.4k | return cond; \ | 289 | 16.4k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_6::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 13.6k | [](const GuardParams& P) { \ | 287 | 13.6k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 13.6k | return cond; \ | 289 | 13.6k | } |
Unexecuted instantiation: CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_7::operator()(clang::pseudo::GuardParams const&) const CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_8::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 8.79k | [](const GuardParams& P) { \ | 287 | 8.79k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 8.79k | return cond; \ | 289 | 8.79k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_9::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.79k | [](const GuardParams& P) { \ | 287 | 1.79k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.79k | return cond; \ | 289 | 1.79k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_10::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 486 | [](const GuardParams& P) { \ | 287 | 486 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 486 | return cond; \ | 289 | 486 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_11::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 360 | [](const GuardParams& P) { \ | 287 | 360 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 360 | return cond; \ | 289 | 360 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_12::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 306 | [](const GuardParams& P) { \ | 287 | 306 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 306 | return cond; \ | 289 | 306 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_13::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 623 | [](const GuardParams& P) { \ | 287 | 623 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 623 | return cond; \ | 289 | 623 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_14::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 3.43k | [](const GuardParams& P) { \ | 287 | 3.43k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 3.43k | return cond; \ | 289 | 3.43k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_15::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 921 | [](const GuardParams& P) { \ | 287 | 921 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 921 | return cond; \ | 289 | 921 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_16::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 667 | [](const GuardParams& P) { \ | 287 | 667 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 667 | return cond; \ | 289 | 667 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_17::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 567 | [](const GuardParams& P) { \ | 287 | 567 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 567 | return cond; \ | 289 | 567 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_18::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.08k | [](const GuardParams& P) { \ | 287 | 1.08k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.08k | return cond; \ | 289 | 1.08k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_19::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.85k | [](const GuardParams& P) { \ | 287 | 1.85k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.85k | return cond; \ | 289 | 1.85k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_20::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 100 | [](const GuardParams& P) { \ | 287 | 100 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 100 | return cond; \ | 289 | 100 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_21::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 390 | [](const GuardParams& P) { \ | 287 | 390 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 390 | return cond; \ | 289 | 390 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_22::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 968 | [](const GuardParams& P) { \ | 287 | 968 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 968 | return cond; \ | 289 | 968 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_23::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 447 | [](const GuardParams& P) { \ | 287 | 447 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 447 | return cond; \ | 289 | 447 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_24::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.85k | [](const GuardParams& P) { \ | 287 | 1.85k | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.85k | return cond; \ | 289 | 1.85k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_25::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 100 | [](const GuardParams& P) { \ | 287 | 100 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 100 | return cond; \ | 289 | 100 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_26::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 390 | [](const GuardParams& P) { \ | 287 | 390 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 390 | return cond; \ | 289 | 390 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_27::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 968 | [](const GuardParams& P) { \ | 287 | 968 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 968 | return cond; \ | 289 | 968 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_28::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 447 | [](const GuardParams& P) { \ | 287 | 447 | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 447 | return cond; \ | 289 | 447 | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_29::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.20M | [](const GuardParams& P) { \ | 287 | 1.20M | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.20M | return cond; \ | 289 | 1.20M | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_30::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.20M | [](const GuardParams& P) { \ | 287 | 1.20M | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.20M | return cond; \ | 289 | 1.20M | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_31::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.20M | [](const GuardParams& P) { \ | 287 | 1.20M | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.20M | return cond; \ | 289 | 1.20M | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_32::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 286 | 1.20M | [](const GuardParams& P) { \ | 287 | 1.20M | const Token &Tok = onlyToken(tok::kind, P.RHS, P.Tokens); \ | 288 | 1.20M | return cond; \ | 289 | 1.20M | } |
|
290 | 1 | #define SYMBOL_GUARD(kind, cond) \ |
291 | 283k | [](const GuardParams& P) { \ |
292 | 283k | const ForestNode &N = onlySymbol(Symbol::kind, P.RHS, P.Tokens); \ |
293 | 283k | return cond; \ |
294 | 283k | } CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_0::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 291 | 166k | [](const GuardParams& P) { \ | 292 | 166k | const ForestNode &N = onlySymbol(Symbol::kind, P.RHS, P.Tokens); \ | 293 | 166k | return cond; \ | 294 | 166k | } |
CXX.cpp:clang::pseudo::cxx::(anonymous namespace)::buildGuards()::$_1::operator()(clang::pseudo::GuardParams const&) const Line | Count | Source | 291 | 116k | [](const GuardParams& P) { \ | 292 | 116k | const ForestNode &N = onlySymbol(Symbol::kind, P.RHS, P.Tokens); \ | 293 | 116k | return cond; \ | 294 | 116k | } |
|
295 | 1 | return { |
296 | 1 | {rule::function_declarator::declarator, |
297 | 1 | SYMBOL_GUARD(declarator, isFunctionDeclarator(&N))}, |
298 | 1 | {rule::non_function_declarator::declarator, |
299 | 1 | SYMBOL_GUARD(declarator, !isFunctionDeclarator(&N))}, |
300 | | |
301 | | // A {decl,type,defining-type}-specifier-sequence cannot have multiple |
302 | | // "exclusive" types (like class names): a value has only one type. |
303 | 1 | {rule::defining_type_specifier_seq:: |
304 | 1 | defining_type_specifier__defining_type_specifier_seq, |
305 | 1 | GUARD(!hasExclusiveType(P.RHS[0]) || !hasExclusiveType(P.RHS[1]))}, |
306 | 1 | {rule::type_specifier_seq::type_specifier__type_specifier_seq, |
307 | 1 | GUARD(!hasExclusiveType(P.RHS[0]) || !hasExclusiveType(P.RHS[1]))}, |
308 | 1 | {rule::decl_specifier_seq::decl_specifier__decl_specifier_seq, |
309 | 1 | GUARD(!hasExclusiveType(P.RHS[0]) || !hasExclusiveType(P.RHS[1]))}, |
310 | | |
311 | 1 | {rule::contextual_override::IDENTIFIER, |
312 | 1 | TOKEN_GUARD(identifier, Tok.text() == "override")}, |
313 | 1 | {rule::contextual_final::IDENTIFIER, |
314 | 1 | TOKEN_GUARD(identifier, Tok.text() == "final")}, |
315 | 1 | {rule::import_keyword::IDENTIFIER, |
316 | 1 | TOKEN_GUARD(identifier, Tok.text() == "import")}, |
317 | 1 | {rule::export_keyword::IDENTIFIER, |
318 | 1 | TOKEN_GUARD(identifier, Tok.text() == "export")}, |
319 | 1 | {rule::module_keyword::IDENTIFIER, |
320 | 1 | TOKEN_GUARD(identifier, Tok.text() == "module")}, |
321 | 1 | {rule::contextual_zero::NUMERIC_CONSTANT, |
322 | 1 | TOKEN_GUARD(numeric_constant, Tok.text() == "0")}, |
323 | | |
324 | 1 | {rule::selection_statement::IF__L_PAREN__condition__R_PAREN__statement, |
325 | 1 | guardNextTokenNotElse}, |
326 | 1 | {rule::selection_statement:: |
327 | 1 | IF__L_PAREN__init_statement__condition__R_PAREN__statement, |
328 | 1 | guardNextTokenNotElse}, |
329 | 1 | {rule::selection_statement:: |
330 | 1 | IF__CONSTEXPR__L_PAREN__condition__R_PAREN__statement, |
331 | 1 | guardNextTokenNotElse}, |
332 | 1 | {rule::selection_statement:: |
333 | 1 | IF__CONSTEXPR__L_PAREN__init_statement__condition__R_PAREN__statement, |
334 | 1 | guardNextTokenNotElse}, |
335 | | |
336 | | // Implement C++ [basic.lookup.qual.general]: |
337 | | // If a name, template-id, or decltype-specifier is followed by a |
338 | | // ::, it shall designate a namespace, class, enumeration, or |
339 | | // dependent type, and the :: is never interpreted as a complete |
340 | | // nested-name-specifier. |
341 | 1 | {rule::nested_name_specifier::COLONCOLON, |
342 | 1 | TOKEN_GUARD(coloncolon, Tok.prev().Kind != tok::identifier)}, |
343 | | |
344 | | // Implement C++ [dcl.pre#6]: |
345 | | // A simple-declaration with an identifier-list is called a structured |
346 | | // binding declaration ([dcl.struct.bind]). If the decl-specifier-seq |
347 | | // contains any decl-specifier other than static, thread_local, auto, |
348 | | // or cv-qualifiers, the program is ill-formed. |
349 | 1 | {rule::simple_declaration:: |
350 | 1 | decl_specifier_seq__ref_qualifier__L_SQUARE__identifier_list__R_SQUARE__initializer__SEMI, |
351 | 1 | specifiesStructuredBinding}, |
352 | 1 | {rule::simple_declaration:: |
353 | 1 | decl_specifier_seq__L_SQUARE__identifier_list__R_SQUARE__initializer__SEMI, |
354 | 1 | specifiesStructuredBinding}, |
355 | | |
356 | | // The grammar distinguishes (only) user-defined vs plain string literals, |
357 | | // where the clang lexer distinguishes (only) encoding types. |
358 | 1 | {rule::user_defined_string_literal_chunk::STRING_LITERAL, |
359 | 1 | TOKEN_GUARD(string_literal, isStringUserDefined(Tok))}, |
360 | 1 | {rule::user_defined_string_literal_chunk::UTF8_STRING_LITERAL, |
361 | 1 | TOKEN_GUARD(utf8_string_literal, isStringUserDefined(Tok))}, |
362 | 1 | {rule::user_defined_string_literal_chunk::UTF16_STRING_LITERAL, |
363 | 1 | TOKEN_GUARD(utf16_string_literal, isStringUserDefined(Tok))}, |
364 | 1 | {rule::user_defined_string_literal_chunk::UTF32_STRING_LITERAL, |
365 | 1 | TOKEN_GUARD(utf32_string_literal, isStringUserDefined(Tok))}, |
366 | 1 | {rule::user_defined_string_literal_chunk::WIDE_STRING_LITERAL, |
367 | 1 | TOKEN_GUARD(wide_string_literal, isStringUserDefined(Tok))}, |
368 | 1 | {rule::string_literal_chunk::STRING_LITERAL, |
369 | 1 | TOKEN_GUARD(string_literal, !isStringUserDefined(Tok))}, |
370 | 1 | {rule::string_literal_chunk::UTF8_STRING_LITERAL, |
371 | 1 | TOKEN_GUARD(utf8_string_literal, !isStringUserDefined(Tok))}, |
372 | 1 | {rule::string_literal_chunk::UTF16_STRING_LITERAL, |
373 | 1 | TOKEN_GUARD(utf16_string_literal, !isStringUserDefined(Tok))}, |
374 | 1 | {rule::string_literal_chunk::UTF32_STRING_LITERAL, |
375 | 1 | TOKEN_GUARD(utf32_string_literal, !isStringUserDefined(Tok))}, |
376 | 1 | {rule::string_literal_chunk::WIDE_STRING_LITERAL, |
377 | 1 | TOKEN_GUARD(wide_string_literal, !isStringUserDefined(Tok))}, |
378 | | // And the same for chars. |
379 | 1 | {rule::user_defined_character_literal::CHAR_CONSTANT, |
380 | 1 | TOKEN_GUARD(char_constant, isCharUserDefined(Tok))}, |
381 | 1 | {rule::user_defined_character_literal::UTF8_CHAR_CONSTANT, |
382 | 1 | TOKEN_GUARD(utf8_char_constant, isCharUserDefined(Tok))}, |
383 | 1 | {rule::user_defined_character_literal::UTF16_CHAR_CONSTANT, |
384 | 1 | TOKEN_GUARD(utf16_char_constant, isCharUserDefined(Tok))}, |
385 | 1 | {rule::user_defined_character_literal::UTF32_CHAR_CONSTANT, |
386 | 1 | TOKEN_GUARD(utf32_char_constant, isCharUserDefined(Tok))}, |
387 | 1 | {rule::user_defined_character_literal::WIDE_CHAR_CONSTANT, |
388 | 1 | TOKEN_GUARD(wide_char_constant, isCharUserDefined(Tok))}, |
389 | 1 | {rule::character_literal::CHAR_CONSTANT, |
390 | 1 | TOKEN_GUARD(char_constant, !isCharUserDefined(Tok))}, |
391 | 1 | {rule::character_literal::UTF8_CHAR_CONSTANT, |
392 | 1 | TOKEN_GUARD(utf8_char_constant, !isCharUserDefined(Tok))}, |
393 | 1 | {rule::character_literal::UTF16_CHAR_CONSTANT, |
394 | 1 | TOKEN_GUARD(utf16_char_constant, !isCharUserDefined(Tok))}, |
395 | 1 | {rule::character_literal::UTF32_CHAR_CONSTANT, |
396 | 1 | TOKEN_GUARD(utf32_char_constant, !isCharUserDefined(Tok))}, |
397 | 1 | {rule::character_literal::WIDE_CHAR_CONSTANT, |
398 | 1 | TOKEN_GUARD(wide_char_constant, !isCharUserDefined(Tok))}, |
399 | | // clang just has one NUMERIC_CONSTANT token for {ud,plain}x{float,int} |
400 | 1 | {rule::user_defined_integer_literal::NUMERIC_CONSTANT, |
401 | 1 | TOKEN_GUARD(numeric_constant, numKind(Tok) == (Integer | UserDefined))}, |
402 | 1 | {rule::user_defined_floating_point_literal::NUMERIC_CONSTANT, |
403 | 1 | TOKEN_GUARD(numeric_constant, numKind(Tok) == (Floating | UserDefined))}, |
404 | 1 | {rule::integer_literal::NUMERIC_CONSTANT, |
405 | 1 | TOKEN_GUARD(numeric_constant, numKind(Tok) == Integer)}, |
406 | 1 | {rule::floating_point_literal::NUMERIC_CONSTANT, |
407 | 1 | TOKEN_GUARD(numeric_constant, numKind(Tok) == Floating)}, |
408 | 1 | }; |
409 | 1 | #undef TOKEN_GUARD |
410 | 1 | #undef SYMBOL_GUARD |
411 | 1 | } |
412 | | |
413 | 389k | Token::Index recoverBrackets(Token::Index Begin, const TokenStream &Tokens) { |
414 | 389k | assert(Begin > 0); |
415 | 0 | const Token &Left = Tokens.tokens()[Begin - 1]; |
416 | 389k | assert(Left.Kind == tok::l_brace || Left.Kind == tok::l_paren || |
417 | 389k | Left.Kind == tok::l_square); |
418 | 389k | if (const Token *Right = Left.pair()) { |
419 | 0 | assert(Tokens.index(*Right) > Begin - 1); |
420 | 0 | return Tokens.index(*Right); |
421 | 0 | } |
422 | 389k | return Token::Invalid; |
423 | 389k | } |
424 | | |
425 | 1 | llvm::DenseMap<ExtensionID, RecoveryStrategy> buildRecoveryStrategies() { |
426 | 1 | return { |
427 | 1 | {Extension::Brackets, recoverBrackets}, |
428 | 1 | }; |
429 | 1 | } |
430 | | |
431 | | } // namespace |
432 | | |
433 | 9.56k | const Language &getLanguage() { |
434 | 9.56k | static const auto &CXXLanguage = []() -> const Language & { |
435 | 1 | std::vector<std::string> Diags; |
436 | 1 | auto G = Grammar::parseBNF(CXXBNF, Diags); |
437 | 1 | assert(Diags.empty()); |
438 | 0 | LRTable Table = LRTable::buildSLR(G); |
439 | 1 | const Language *PL = new Language{ |
440 | 1 | std::move(G), |
441 | 1 | std::move(Table), |
442 | 1 | buildGuards(), |
443 | 1 | buildRecoveryStrategies(), |
444 | 1 | }; |
445 | 1 | return *PL; |
446 | 1 | }(); |
447 | 9.56k | return CXXLanguage; |
448 | 9.56k | } |
449 | | |
450 | | } // namespace cxx |
451 | | } // namespace pseudo |
452 | | } // namespace clang |