/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Parse/ParseInit.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- ParseInit.cpp - Initializer Parsing ------------------------------===// |
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 initializer parsing as specified by C99 6.7.8. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Basic/TokenKinds.h" |
14 | | #include "clang/Parse/ParseDiagnostic.h" |
15 | | #include "clang/Parse/Parser.h" |
16 | | #include "clang/Parse/RAIIObjectsForParser.h" |
17 | | #include "clang/Sema/Designator.h" |
18 | | #include "clang/Sema/Ownership.h" |
19 | | #include "clang/Sema/Scope.h" |
20 | | #include "llvm/ADT/STLExtras.h" |
21 | | #include "llvm/ADT/SmallString.h" |
22 | | using namespace clang; |
23 | | |
24 | | |
25 | | /// MayBeDesignationStart - Return true if the current token might be the start |
26 | | /// of a designator. If we can tell it is impossible that it is a designator, |
27 | | /// return false. |
28 | 394k | bool Parser::MayBeDesignationStart() { |
29 | 394k | switch (Tok.getKind()) { |
30 | 146k | default: |
31 | 146k | return false; |
32 | | |
33 | 1.42k | case tok::period: // designator: '.' identifier |
34 | 1.42k | return true; |
35 | | |
36 | 398 | case tok::l_square: { // designator: array-designator |
37 | 398 | if (!PP.getLangOpts().CPlusPlus11) |
38 | 247 | return true; |
39 | | |
40 | | // C++11 lambda expressions and C99 designators can be ambiguous all the |
41 | | // way through the closing ']' and to the next character. Handle the easy |
42 | | // cases here, and fall back to tentative parsing if those fail. |
43 | 151 | switch (PP.LookAhead(0).getKind()) { |
44 | 0 | case tok::equal: |
45 | 8 | case tok::ellipsis: |
46 | 30 | case tok::r_square: |
47 | | // Definitely starts a lambda expression. |
48 | 30 | return false; |
49 | | |
50 | 2 | case tok::amp: |
51 | 4 | case tok::kw_this: |
52 | 6 | case tok::star: |
53 | 58 | case tok::identifier: |
54 | | // We have to do additional analysis, because these could be the |
55 | | // start of a constant expression or a lambda capture list. |
56 | 58 | break; |
57 | | |
58 | 63 | default: |
59 | | // Anything not mentioned above cannot occur following a '[' in a |
60 | | // lambda expression. |
61 | 63 | return true; |
62 | 58 | } |
63 | | |
64 | | // Handle the complicated case below. |
65 | 58 | break; |
66 | 58 | } |
67 | 246k | case tok::identifier: // designation: identifier ':' |
68 | 246k | return PP.LookAhead(0).is(tok::colon); |
69 | 58 | } |
70 | | |
71 | | // Parse up to (at most) the token after the closing ']' to determine |
72 | | // whether this is a C99 designator or a lambda. |
73 | 58 | RevertingTentativeParsingAction Tentative(*this); |
74 | | |
75 | 58 | LambdaIntroducer Intro; |
76 | 58 | LambdaIntroducerTentativeParse ParseResult; |
77 | 58 | if (ParseLambdaIntroducer(Intro, &ParseResult)) { |
78 | | // Hit and diagnosed an error in a lambda. |
79 | | // FIXME: Tell the caller this happened so they can recover. |
80 | 0 | return true; |
81 | 0 | } |
82 | | |
83 | 58 | switch (ParseResult) { |
84 | 28 | case LambdaIntroducerTentativeParse::Success: |
85 | 41 | case LambdaIntroducerTentativeParse::Incomplete: |
86 | | // Might be a lambda-expression. Keep looking. |
87 | | // FIXME: If our tentative parse was not incomplete, parse the lambda from |
88 | | // here rather than throwing away then reparsing the LambdaIntroducer. |
89 | 41 | break; |
90 | | |
91 | 12 | case LambdaIntroducerTentativeParse::MessageSend: |
92 | 17 | case LambdaIntroducerTentativeParse::Invalid: |
93 | | // Can't be a lambda-expression. Treat it as a designator. |
94 | | // FIXME: Should we disambiguate against a message-send? |
95 | 17 | return true; |
96 | 41 | } |
97 | | |
98 | | // Once we hit the closing square bracket, we look at the next |
99 | | // token. If it's an '=', this is a designator. Otherwise, it's a |
100 | | // lambda expression. This decision favors lambdas over the older |
101 | | // GNU designator syntax, which allows one to omit the '=', but is |
102 | | // consistent with GCC. |
103 | 41 | return Tok.is(tok::equal); |
104 | 41 | } |
105 | | |
106 | | static void CheckArrayDesignatorSyntax(Parser &P, SourceLocation Loc, |
107 | 68 | Designation &Desig) { |
108 | | // If we have exactly one array designator, this used the GNU |
109 | | // 'designation: array-designator' extension, otherwise there should be no |
110 | | // designators at all! |
111 | 68 | if (Desig.getNumDesignators() == 1 && |
112 | 12 | (Desig.getDesignator(0).isArrayDesignator() || |
113 | 4 | Desig.getDesignator(0).isArrayRangeDesignator())) |
114 | 8 | P.Diag(Loc, diag::ext_gnu_missing_equal_designator); |
115 | 60 | else if (Desig.getNumDesignators() > 0) |
116 | 4 | P.Diag(Loc, diag::err_expected_equal_designator); |
117 | 68 | } |
118 | | |
119 | | /// ParseInitializerWithPotentialDesignator - Parse the 'initializer' production |
120 | | /// checking to see if the token stream starts with a designator. |
121 | | /// |
122 | | /// C99: |
123 | | /// |
124 | | /// designation: |
125 | | /// designator-list '=' |
126 | | /// [GNU] array-designator |
127 | | /// [GNU] identifier ':' |
128 | | /// |
129 | | /// designator-list: |
130 | | /// designator |
131 | | /// designator-list designator |
132 | | /// |
133 | | /// designator: |
134 | | /// array-designator |
135 | | /// '.' identifier |
136 | | /// |
137 | | /// array-designator: |
138 | | /// '[' constant-expression ']' |
139 | | /// [GNU] '[' constant-expression '...' constant-expression ']' |
140 | | /// |
141 | | /// C++20: |
142 | | /// |
143 | | /// designated-initializer-list: |
144 | | /// designated-initializer-clause |
145 | | /// designated-initializer-list ',' designated-initializer-clause |
146 | | /// |
147 | | /// designated-initializer-clause: |
148 | | /// designator brace-or-equal-initializer |
149 | | /// |
150 | | /// designator: |
151 | | /// '.' identifier |
152 | | /// |
153 | | /// We allow the C99 syntax extensions in C++20, but do not allow the C++20 |
154 | | /// extension (a braced-init-list after the designator with no '=') in C99. |
155 | | /// |
156 | | /// NOTE: [OBC] allows '[ objc-receiver objc-message-args ]' as an |
157 | | /// initializer (because it is an expression). We need to consider this case |
158 | | /// when parsing array designators. |
159 | | /// |
160 | | /// \p CodeCompleteCB is called with Designation parsed so far. |
161 | | ExprResult Parser::ParseInitializerWithPotentialDesignator( |
162 | 1.78k | llvm::function_ref<void(const Designation &)> CodeCompleteCB) { |
163 | | |
164 | | // If this is the old-style GNU extension: |
165 | | // designation ::= identifier ':' |
166 | | // Handle it as a field designator. Otherwise, this must be the start of a |
167 | | // normal expression. |
168 | 1.78k | if (Tok.is(tok::identifier)) { |
169 | 9 | const IdentifierInfo *FieldName = Tok.getIdentifierInfo(); |
170 | | |
171 | 9 | SmallString<256> NewSyntax; |
172 | 9 | llvm::raw_svector_ostream(NewSyntax) << '.' << FieldName->getName() |
173 | 9 | << " = "; |
174 | | |
175 | 9 | SourceLocation NameLoc = ConsumeToken(); // Eat the identifier. |
176 | | |
177 | 9 | assert(Tok.is(tok::colon) && "MayBeDesignationStart not working properly!"); |
178 | 9 | SourceLocation ColonLoc = ConsumeToken(); |
179 | | |
180 | 9 | Diag(NameLoc, diag::ext_gnu_old_style_field_designator) |
181 | 9 | << FixItHint::CreateReplacement(SourceRange(NameLoc, ColonLoc), |
182 | 9 | NewSyntax); |
183 | | |
184 | 9 | Designation D; |
185 | 9 | D.AddDesignator(Designator::getField(FieldName, SourceLocation(), NameLoc)); |
186 | 9 | return Actions.ActOnDesignatedInitializer(D, ColonLoc, true, |
187 | 9 | ParseInitializer()); |
188 | 9 | } |
189 | | |
190 | | // Desig - This is initialized when we see our first designator. We may have |
191 | | // an objc message send with no designator, so we don't want to create this |
192 | | // eagerly. |
193 | 1.77k | Designation Desig; |
194 | | |
195 | | // Parse each designator in the designator list until we find an initializer. |
196 | 3.77k | while (Tok.is(tok::period) || Tok.is(tok::l_square)2.12k ) { |
197 | 2.08k | if (Tok.is(tok::period)) { |
198 | | // designator: '.' identifier |
199 | 1.65k | SourceLocation DotLoc = ConsumeToken(); |
200 | | |
201 | 1.65k | if (Tok.is(tok::code_completion)) { |
202 | 10 | CodeCompleteCB(Desig); |
203 | 10 | cutOffParsing(); |
204 | 10 | return ExprError(); |
205 | 10 | } |
206 | 1.64k | if (Tok.isNot(tok::identifier)) { |
207 | 0 | Diag(Tok.getLocation(), diag::err_expected_field_designator); |
208 | 0 | return ExprError(); |
209 | 0 | } |
210 | | |
211 | 1.64k | Desig.AddDesignator(Designator::getField(Tok.getIdentifierInfo(), DotLoc, |
212 | 1.64k | Tok.getLocation())); |
213 | 1.64k | ConsumeToken(); // Eat the identifier. |
214 | 1.64k | continue; |
215 | 1.64k | } |
216 | | |
217 | | // We must have either an array designator now or an objc message send. |
218 | 426 | assert(Tok.is(tok::l_square) && "Unexpected token!"); |
219 | | |
220 | | // Handle the two forms of array designator: |
221 | | // array-designator: '[' constant-expression ']' |
222 | | // array-designator: '[' constant-expression '...' constant-expression ']' |
223 | | // |
224 | | // Also, we have to handle the case where the expression after the |
225 | | // designator an an objc message send: '[' objc-message-expr ']'. |
226 | | // Interesting cases are: |
227 | | // [foo bar] -> objc message send |
228 | | // [foo] -> array designator |
229 | | // [foo ... bar] -> array designator |
230 | | // [4][foo bar] -> obsolete GNU designation with objc message send. |
231 | | // |
232 | | // We do not need to check for an expression starting with [[ here. If it |
233 | | // contains an Objective-C message send, then it is not an ill-formed |
234 | | // attribute. If it is a lambda-expression within an array-designator, then |
235 | | // it will be rejected because a constant-expression cannot begin with a |
236 | | // lambda-expression. |
237 | 426 | InMessageExpressionRAIIObject InMessage(*this, true); |
238 | | |
239 | 426 | BalancedDelimiterTracker T(*this, tok::l_square); |
240 | 426 | T.consumeOpen(); |
241 | 426 | SourceLocation StartLoc = T.getOpenLocation(); |
242 | | |
243 | 426 | ExprResult Idx; |
244 | | |
245 | | // If Objective-C is enabled and this is a typename (class message |
246 | | // send) or send to 'super', parse this as a message send |
247 | | // expression. We handle C++ and C separately, since C++ requires |
248 | | // much more complicated parsing. |
249 | 426 | if (getLangOpts().ObjC && getLangOpts().CPlusPlus96 ) { |
250 | | // Send to 'super'. |
251 | 58 | if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super30 && |
252 | 3 | NextToken().isNot(tok::period) && |
253 | 3 | getCurScope()->isInObjcMethodScope()) { |
254 | 3 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
255 | 3 | return ParseAssignmentExprWithObjCMessageExprStart( |
256 | 3 | StartLoc, ConsumeToken(), nullptr, nullptr); |
257 | 3 | } |
258 | | |
259 | | // Parse the receiver, which is either a type or an expression. |
260 | 55 | bool IsExpr; |
261 | 55 | void *TypeOrExpr; |
262 | 55 | if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) { |
263 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
264 | 0 | return ExprError(); |
265 | 0 | } |
266 | | |
267 | | // If the receiver was a type, we have a class message; parse |
268 | | // the rest of it. |
269 | 55 | if (!IsExpr) { |
270 | 20 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
271 | 20 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
272 | 20 | SourceLocation(), |
273 | 20 | ParsedType::getFromOpaquePtr(TypeOrExpr), |
274 | 20 | nullptr); |
275 | 20 | } |
276 | | |
277 | | // If the receiver was an expression, we still don't know |
278 | | // whether we have a message send or an array designator; just |
279 | | // adopt the expression for further analysis below. |
280 | | // FIXME: potentially-potentially evaluated expression above? |
281 | 35 | Idx = ExprResult(static_cast<Expr*>(TypeOrExpr)); |
282 | 368 | } else if (getLangOpts().ObjC && Tok.is(tok::identifier)38 ) { |
283 | 10 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
284 | 10 | SourceLocation IILoc = Tok.getLocation(); |
285 | 10 | ParsedType ReceiverType; |
286 | | // Three cases. This is a message send to a type: [type foo] |
287 | | // This is a message send to super: [super foo] |
288 | | // This is a message sent to an expr: [super.bar foo] |
289 | 10 | switch (Actions.getObjCMessageKind( |
290 | 10 | getCurScope(), II, IILoc, II == Ident_super, |
291 | 10 | NextToken().is(tok::period), ReceiverType)) { |
292 | 1 | case Sema::ObjCSuperMessage: |
293 | 1 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
294 | 1 | return ParseAssignmentExprWithObjCMessageExprStart( |
295 | 1 | StartLoc, ConsumeToken(), nullptr, nullptr); |
296 | | |
297 | 3 | case Sema::ObjCClassMessage: |
298 | 3 | CheckArrayDesignatorSyntax(*this, StartLoc, Desig); |
299 | 3 | ConsumeToken(); // the identifier |
300 | 3 | if (!ReceiverType) { |
301 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
302 | 0 | return ExprError(); |
303 | 0 | } |
304 | | |
305 | | // Parse type arguments and protocol qualifiers. |
306 | 3 | if (Tok.is(tok::less)) { |
307 | 0 | SourceLocation NewEndLoc; |
308 | 0 | TypeResult NewReceiverType |
309 | 0 | = parseObjCTypeArgsAndProtocolQualifiers(IILoc, ReceiverType, |
310 | 0 | /*consumeLastToken=*/true, |
311 | 0 | NewEndLoc); |
312 | 0 | if (!NewReceiverType.isUsable()) { |
313 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
314 | 0 | return ExprError(); |
315 | 0 | } |
316 | | |
317 | 0 | ReceiverType = NewReceiverType.get(); |
318 | 0 | } |
319 | | |
320 | 3 | return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, |
321 | 3 | SourceLocation(), |
322 | 3 | ReceiverType, |
323 | 3 | nullptr); |
324 | | |
325 | 6 | case Sema::ObjCInstanceMessage: |
326 | | // Fall through; we'll just parse the expression and |
327 | | // (possibly) treat this like an Objective-C message send |
328 | | // later. |
329 | 6 | break; |
330 | 399 | } |
331 | 399 | } |
332 | | |
333 | | // Parse the index expression, if we haven't already gotten one |
334 | | // above (which can only happen in Objective-C++). |
335 | | // Note that we parse this as an assignment expression, not a constant |
336 | | // expression (allowing *=, =, etc) to handle the objc case. Sema needs |
337 | | // to validate that the expression is a constant. |
338 | | // FIXME: We also need to tell Sema that we're in a |
339 | | // potentially-potentially evaluated context. |
340 | 399 | if (!Idx.get()) { |
341 | 364 | Idx = ParseAssignmentExpression(); |
342 | 364 | if (Idx.isInvalid()) { |
343 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
344 | 0 | return Idx; |
345 | 0 | } |
346 | 399 | } |
347 | | |
348 | | // Given an expression, we could either have a designator (if the next |
349 | | // tokens are '...' or ']' or an objc message send. If this is an objc |
350 | | // message send, handle it now. An objc-message send is the start of |
351 | | // an assignment-expression production. |
352 | 399 | if (getLangOpts().ObjC && Tok.isNot(tok::ellipsis)69 && |
353 | 69 | Tok.isNot(tok::r_square)) { |
354 | 41 | CheckArrayDesignatorSyntax(*this, Tok.getLocation(), Desig); |
355 | 41 | return ParseAssignmentExprWithObjCMessageExprStart( |
356 | 41 | StartLoc, SourceLocation(), nullptr, Idx.get()); |
357 | 41 | } |
358 | | |
359 | | // If this is a normal array designator, remember it. |
360 | 358 | if (Tok.isNot(tok::ellipsis)) { |
361 | 338 | Desig.AddDesignator(Designator::getArray(Idx.get(), StartLoc)); |
362 | 20 | } else { |
363 | | // Handle the gnu array range extension. |
364 | 20 | Diag(Tok, diag::ext_gnu_array_range); |
365 | 20 | SourceLocation EllipsisLoc = ConsumeToken(); |
366 | | |
367 | 20 | ExprResult RHS(ParseConstantExpression()); |
368 | 20 | if (RHS.isInvalid()) { |
369 | 0 | SkipUntil(tok::r_square, StopAtSemi); |
370 | 0 | return RHS; |
371 | 0 | } |
372 | 20 | Desig.AddDesignator(Designator::getArrayRange(Idx.get(), |
373 | 20 | RHS.get(), |
374 | 20 | StartLoc, EllipsisLoc)); |
375 | 20 | } |
376 | | |
377 | 358 | T.consumeClose(); |
378 | 358 | Desig.getDesignator(Desig.getNumDesignators() - 1).setRBracketLoc( |
379 | 358 | T.getCloseLocation()); |
380 | 358 | } |
381 | | |
382 | | // Okay, we're done with the designator sequence. We know that there must be |
383 | | // at least one designator, because the only case we can get into this method |
384 | | // without a designator is when we have an objc message send. That case is |
385 | | // handled and returned from above. |
386 | 1.69k | assert(!Desig.empty() && "Designator is empty?"); |
387 | | |
388 | | // Handle a normal designator sequence end, which is an equal. |
389 | 1.69k | if (Tok.is(tok::equal)) { |
390 | 1.67k | SourceLocation EqualLoc = ConsumeToken(); |
391 | 1.67k | return Actions.ActOnDesignatedInitializer(Desig, EqualLoc, false, |
392 | 1.67k | ParseInitializer()); |
393 | 1.67k | } |
394 | | |
395 | | // Handle a C++20 braced designated initialization, which results in |
396 | | // direct-list-initialization of the aggregate element. We allow this as an |
397 | | // extension from C++11 onwards (when direct-list-initialization was added). |
398 | 22 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus1114 ) { |
399 | 12 | return Actions.ActOnDesignatedInitializer(Desig, SourceLocation(), false, |
400 | 12 | ParseBraceInitializer()); |
401 | 12 | } |
402 | | |
403 | | // We read some number of designators and found something that isn't an = or |
404 | | // an initializer. If we have exactly one array designator, this |
405 | | // is the GNU 'designation: array-designator' extension. Otherwise, it is a |
406 | | // parse error. |
407 | 10 | if (Desig.getNumDesignators() == 1 && |
408 | 9 | (Desig.getDesignator(0).isArrayDesignator() || |
409 | 7 | Desig.getDesignator(0).isArrayRangeDesignator()2 )) { |
410 | 7 | Diag(Tok, diag::ext_gnu_missing_equal_designator) |
411 | 7 | << FixItHint::CreateInsertion(Tok.getLocation(), "= "); |
412 | 7 | return Actions.ActOnDesignatedInitializer(Desig, Tok.getLocation(), |
413 | 7 | true, ParseInitializer()); |
414 | 7 | } |
415 | | |
416 | 3 | Diag(Tok, diag::err_expected_equal_designator); |
417 | 3 | return ExprError(); |
418 | 3 | } |
419 | | |
420 | | /// ParseBraceInitializer - Called when parsing an initializer that has a |
421 | | /// leading open brace. |
422 | | /// |
423 | | /// initializer: [C99 6.7.8] |
424 | | /// '{' initializer-list '}' |
425 | | /// '{' initializer-list ',' '}' |
426 | | /// [GNU] '{' '}' |
427 | | /// |
428 | | /// initializer-list: |
429 | | /// designation[opt] initializer ...[opt] |
430 | | /// initializer-list ',' designation[opt] initializer ...[opt] |
431 | | /// |
432 | 95.1k | ExprResult Parser::ParseBraceInitializer() { |
433 | 95.1k | InMessageExpressionRAIIObject InMessage(*this, false); |
434 | | |
435 | 95.1k | BalancedDelimiterTracker T(*this, tok::l_brace); |
436 | 95.1k | T.consumeOpen(); |
437 | 95.1k | SourceLocation LBraceLoc = T.getOpenLocation(); |
438 | | |
439 | | /// InitExprs - This is the actual list of expressions contained in the |
440 | | /// initializer. |
441 | 95.1k | ExprVector InitExprs; |
442 | | |
443 | 95.1k | if (Tok.is(tok::r_brace)) { |
444 | | // Empty initializers are a C++ feature and a GNU extension to C. |
445 | 11.8k | if (!getLangOpts().CPlusPlus) |
446 | 675 | Diag(LBraceLoc, diag::ext_gnu_empty_initializer); |
447 | | // Match the '}'. |
448 | 11.8k | return Actions.ActOnInitList(LBraceLoc, None, ConsumeBrace()); |
449 | 11.8k | } |
450 | | |
451 | | // Enter an appropriate expression evaluation context for an initializer list. |
452 | 83.3k | EnterExpressionEvaluationContext EnterContext( |
453 | 83.3k | Actions, EnterExpressionEvaluationContext::InitList); |
454 | | |
455 | 83.3k | bool InitExprsOk = true; |
456 | 10 | auto CodeCompleteDesignation = [&](const Designation &D) { |
457 | 10 | Actions.CodeCompleteDesignator(PreferredType.get(T.getOpenLocation()), |
458 | 10 | InitExprs, D); |
459 | 10 | }; |
460 | | |
461 | 394k | while (1) { |
462 | | // Handle Microsoft __if_exists/if_not_exists if necessary. |
463 | 394k | if (getLangOpts().MicrosoftExt && (12.4k Tok.is(tok::kw___if_exists)12.4k || |
464 | 12.4k | Tok.is(tok::kw___if_not_exists))) { |
465 | 9 | if (ParseMicrosoftIfExistsBraceInitializer(InitExprs, InitExprsOk)) { |
466 | 0 | if (Tok.isNot(tok::comma)) break; |
467 | 0 | ConsumeToken(); |
468 | 0 | } |
469 | 9 | if (Tok.is(tok::r_brace)) break0 ; |
470 | 9 | continue; |
471 | 9 | } |
472 | | |
473 | | // Parse: designation[opt] initializer |
474 | | |
475 | | // If we know that this cannot be a designation, just parse the nested |
476 | | // initializer directly. |
477 | 394k | ExprResult SubElt; |
478 | 394k | if (MayBeDesignationStart()) |
479 | 1.78k | SubElt = ParseInitializerWithPotentialDesignator(CodeCompleteDesignation); |
480 | 392k | else |
481 | 392k | SubElt = ParseInitializer(); |
482 | | |
483 | 394k | if (Tok.is(tok::ellipsis)) |
484 | 116 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
485 | | |
486 | 394k | SubElt = Actions.CorrectDelayedTyposInExpr(SubElt.get()); |
487 | | |
488 | | // If we couldn't parse the subelement, bail out. |
489 | 394k | if (SubElt.isUsable()) { |
490 | 394k | InitExprs.push_back(SubElt.get()); |
491 | 57 | } else { |
492 | 57 | InitExprsOk = false; |
493 | | |
494 | | // We have two ways to try to recover from this error: if the code looks |
495 | | // grammatically ok (i.e. we have a comma coming up) try to continue |
496 | | // parsing the rest of the initializer. This allows us to emit |
497 | | // diagnostics for later elements that we find. If we don't see a comma, |
498 | | // assume there is a parse error, and just skip to recover. |
499 | | // FIXME: This comment doesn't sound right. If there is a r_brace |
500 | | // immediately, it can't be an error, since there is no other way of |
501 | | // leaving this loop except through this if. |
502 | 57 | if (Tok.isNot(tok::comma)) { |
503 | 50 | SkipUntil(tok::r_brace, StopBeforeMatch); |
504 | 50 | break; |
505 | 50 | } |
506 | 394k | } |
507 | | |
508 | | // If we don't have a comma continued list, we're done. |
509 | 394k | if (Tok.isNot(tok::comma)) break82.6k ; |
510 | | |
511 | | // TODO: save comma locations if some client cares. |
512 | 311k | ConsumeToken(); |
513 | | |
514 | | // Handle trailing comma. |
515 | 311k | if (Tok.is(tok::r_brace)) break658 ; |
516 | 311k | } |
517 | | |
518 | 83.3k | bool closed = !T.consumeClose(); |
519 | | |
520 | 83.3k | if (InitExprsOk && closed83.2k ) |
521 | 83.2k | return Actions.ActOnInitList(LBraceLoc, InitExprs, |
522 | 83.2k | T.getCloseLocation()); |
523 | | |
524 | 52 | return ExprError(); // an error occurred. |
525 | 52 | } |
526 | | |
527 | | |
528 | | // Return true if a comma (or closing brace) is necessary after the |
529 | | // __if_exists/if_not_exists statement. |
530 | | bool Parser::ParseMicrosoftIfExistsBraceInitializer(ExprVector &InitExprs, |
531 | 9 | bool &InitExprsOk) { |
532 | 9 | bool trailingComma = false; |
533 | 9 | IfExistsCondition Result; |
534 | 9 | if (ParseMicrosoftIfExistsCondition(Result)) |
535 | 0 | return false; |
536 | | |
537 | 9 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
538 | 9 | if (Braces.consumeOpen()) { |
539 | 0 | Diag(Tok, diag::err_expected) << tok::l_brace; |
540 | 0 | return false; |
541 | 0 | } |
542 | | |
543 | 9 | switch (Result.Behavior) { |
544 | 4 | case IEB_Parse: |
545 | | // Parse the declarations below. |
546 | 4 | break; |
547 | | |
548 | 1 | case IEB_Dependent: |
549 | 1 | Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists) |
550 | 1 | << Result.IsIfExists; |
551 | | // Fall through to skip. |
552 | 1 | LLVM_FALLTHROUGH; |
553 | | |
554 | 5 | case IEB_Skip: |
555 | 5 | Braces.skipToEnd(); |
556 | 5 | return false; |
557 | 4 | } |
558 | | |
559 | 4 | auto CodeCompleteDesignation = [&](const Designation &D) { |
560 | 0 | Actions.CodeCompleteDesignator(PreferredType.get(Braces.getOpenLocation()), |
561 | 0 | InitExprs, D); |
562 | 0 | }; |
563 | 4 | while (!isEofOrEom()) { |
564 | 4 | trailingComma = false; |
565 | | // If we know that this cannot be a designation, just parse the nested |
566 | | // initializer directly. |
567 | 4 | ExprResult SubElt; |
568 | 4 | if (MayBeDesignationStart()) |
569 | 0 | SubElt = ParseInitializerWithPotentialDesignator(CodeCompleteDesignation); |
570 | 4 | else |
571 | 4 | SubElt = ParseInitializer(); |
572 | | |
573 | 4 | if (Tok.is(tok::ellipsis)) |
574 | 0 | SubElt = Actions.ActOnPackExpansion(SubElt.get(), ConsumeToken()); |
575 | | |
576 | | // If we couldn't parse the subelement, bail out. |
577 | 4 | if (!SubElt.isInvalid()) |
578 | 4 | InitExprs.push_back(SubElt.get()); |
579 | 0 | else |
580 | 0 | InitExprsOk = false; |
581 | | |
582 | 4 | if (Tok.is(tok::comma)) { |
583 | 4 | ConsumeToken(); |
584 | 4 | trailingComma = true; |
585 | 4 | } |
586 | | |
587 | 4 | if (Tok.is(tok::r_brace)) |
588 | 4 | break; |
589 | 4 | } |
590 | | |
591 | 4 | Braces.consumeClose(); |
592 | | |
593 | 4 | return !trailingComma; |
594 | 4 | } |