1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/AST/PrettyDeclStackTrace.h" |
15 | #include "clang/Basic/Attributes.h" |
16 | #include "clang/Basic/PrettyStackTrace.h" |
17 | #include "clang/Parse/LoopHint.h" |
18 | #include "clang/Parse/Parser.h" |
19 | #include "clang/Parse/RAIIObjectsForParser.h" |
20 | #include "clang/Sema/DeclSpec.h" |
21 | #include "clang/Sema/Scope.h" |
22 | #include "clang/Sema/TypoCorrection.h" |
23 | using namespace clang; |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc, |
32 | ParsedStmtContext StmtCtx) { |
33 | StmtResult Res; |
34 | |
35 | |
36 | |
37 | do { |
38 | StmtVector Stmts; |
39 | Res = ParseStatementOrDeclaration(Stmts, StmtCtx, TrailingElseLoc); |
40 | } while (!Res.isInvalid() && !Res.get()); |
41 | |
42 | return Res; |
43 | } |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | StmtResult |
95 | Parser::ParseStatementOrDeclaration(StmtVector &Stmts, |
96 | ParsedStmtContext StmtCtx, |
97 | SourceLocation *TrailingElseLoc) { |
98 | |
99 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
100 | |
101 | ParsedAttributesWithRange Attrs(AttrFactory); |
102 | MaybeParseCXX11Attributes(Attrs, nullptr, true); |
103 | if (!MaybeParseOpenCLUnrollHintAttribute(Attrs)) |
104 | return StmtError(); |
105 | |
106 | StmtResult Res = ParseStatementOrDeclarationAfterAttributes( |
107 | Stmts, StmtCtx, TrailingElseLoc, Attrs); |
108 | |
109 | (0) . __assert_fail ("(Attrs.empty() || Res.isInvalid() || Res.isUsable()) && \"attributes on empty statement\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 110, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && |
110 | (0) . __assert_fail ("(Attrs.empty() || Res.isInvalid() || Res.isUsable()) && \"attributes on empty statement\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 110, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "attributes on empty statement"); |
111 | |
112 | if (Attrs.empty() || Res.isInvalid()) |
113 | return Res; |
114 | |
115 | return Actions.ProcessStmtAttributes(Res.get(), Attrs, Attrs.Range); |
116 | } |
117 | |
118 | namespace { |
119 | class StatementFilterCCC final : public CorrectionCandidateCallback { |
120 | public: |
121 | StatementFilterCCC(Token nextTok) : NextToken(nextTok) { |
122 | WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square, |
123 | tok::identifier, tok::star, tok::amp); |
124 | WantExpressionKeywords = |
125 | nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period); |
126 | WantRemainingKeywords = |
127 | nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace); |
128 | WantCXXNamedCasts = false; |
129 | } |
130 | |
131 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
132 | if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>()) |
133 | return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD); |
134 | if (NextToken.is(tok::equal)) |
135 | return candidate.getCorrectionDeclAs<VarDecl>(); |
136 | if (NextToken.is(tok::period) && |
137 | candidate.getCorrectionDeclAs<NamespaceDecl>()) |
138 | return false; |
139 | return CorrectionCandidateCallback::ValidateCandidate(candidate); |
140 | } |
141 | |
142 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
143 | return llvm::make_unique<StatementFilterCCC>(*this); |
144 | } |
145 | |
146 | private: |
147 | Token NextToken; |
148 | }; |
149 | } |
150 | |
151 | StmtResult Parser::ParseStatementOrDeclarationAfterAttributes( |
152 | StmtVector &Stmts, ParsedStmtContext StmtCtx, |
153 | SourceLocation *TrailingElseLoc, ParsedAttributesWithRange &Attrs) { |
154 | const char *SemiError = nullptr; |
155 | StmtResult Res; |
156 | |
157 | |
158 | |
159 | |
160 | Retry: |
161 | tok::TokenKind Kind = Tok.getKind(); |
162 | SourceLocation AtLoc; |
163 | switch (Kind) { |
164 | case tok::at: |
165 | { |
166 | ProhibitAttributes(Attrs); |
167 | AtLoc = ConsumeToken(); |
168 | return ParseObjCAtStatement(AtLoc, StmtCtx); |
169 | } |
170 | |
171 | case tok::code_completion: |
172 | Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); |
173 | cutOffParsing(); |
174 | return StmtError(); |
175 | |
176 | case tok::identifier: { |
177 | Token Next = NextToken(); |
178 | if (Next.is(tok::colon)) { |
179 | |
180 | return ParseLabeledStatement(Attrs, StmtCtx); |
181 | } |
182 | |
183 | |
184 | |
185 | if (Next.isNot(tok::coloncolon)) { |
186 | |
187 | |
188 | StatementFilterCCC CCC(Next); |
189 | if (TryAnnotateName( false, &CCC) == ANK_Error) { |
190 | |
191 | |
192 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
193 | if (Tok.is(tok::semi)) |
194 | ConsumeToken(); |
195 | return StmtError(); |
196 | } |
197 | |
198 | |
199 | if (Tok.isNot(tok::identifier)) |
200 | goto Retry; |
201 | } |
202 | |
203 | |
204 | LLVM_FALLTHROUGH; |
205 | } |
206 | |
207 | default: { |
208 | if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || |
209 | (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) != |
210 | ParsedStmtContext()) && |
211 | isDeclarationStatement()) { |
212 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
213 | DeclGroupPtrTy Decl = ParseDeclaration(DeclaratorContext::BlockContext, |
214 | DeclEnd, Attrs); |
215 | return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd); |
216 | } |
217 | |
218 | if (Tok.is(tok::r_brace)) { |
219 | Diag(Tok, diag::err_expected_statement); |
220 | return StmtError(); |
221 | } |
222 | |
223 | return ParseExprStatement(StmtCtx); |
224 | } |
225 | |
226 | case tok::kw_case: |
227 | return ParseCaseStatement(StmtCtx); |
228 | case tok::kw_default: |
229 | return ParseDefaultStatement(StmtCtx); |
230 | |
231 | case tok::l_brace: |
232 | return ParseCompoundStatement(); |
233 | case tok::semi: { |
234 | bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro(); |
235 | return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro); |
236 | } |
237 | |
238 | case tok::kw_if: |
239 | return ParseIfStatement(TrailingElseLoc); |
240 | case tok::kw_switch: |
241 | return ParseSwitchStatement(TrailingElseLoc); |
242 | |
243 | case tok::kw_while: |
244 | return ParseWhileStatement(TrailingElseLoc); |
245 | case tok::kw_do: |
246 | Res = ParseDoStatement(); |
247 | SemiError = "do/while"; |
248 | break; |
249 | case tok::kw_for: |
250 | return ParseForStatement(TrailingElseLoc); |
251 | |
252 | case tok::kw_goto: |
253 | Res = ParseGotoStatement(); |
254 | SemiError = "goto"; |
255 | break; |
256 | case tok::kw_continue: |
257 | Res = ParseContinueStatement(); |
258 | SemiError = "continue"; |
259 | break; |
260 | case tok::kw_break: |
261 | Res = ParseBreakStatement(); |
262 | SemiError = "break"; |
263 | break; |
264 | case tok::kw_return: |
265 | Res = ParseReturnStatement(); |
266 | SemiError = "return"; |
267 | break; |
268 | case tok::kw_co_return: |
269 | Res = ParseReturnStatement(); |
270 | SemiError = "co_return"; |
271 | break; |
272 | |
273 | case tok::kw_asm: { |
274 | ProhibitAttributes(Attrs); |
275 | bool msAsm = false; |
276 | Res = ParseAsmStatement(msAsm); |
277 | Res = Actions.ActOnFinishFullStmt(Res.get()); |
278 | if (msAsm) return Res; |
279 | SemiError = "asm"; |
280 | break; |
281 | } |
282 | |
283 | case tok::kw___if_exists: |
284 | case tok::kw___if_not_exists: |
285 | ProhibitAttributes(Attrs); |
286 | ParseMicrosoftIfExistsStatement(Stmts); |
287 | |
288 | |
289 | return StmtEmpty(); |
290 | |
291 | case tok::kw_try: |
292 | return ParseCXXTryBlock(); |
293 | |
294 | case tok::kw___try: |
295 | ProhibitAttributes(Attrs); |
296 | return ParseSEHTryBlock(); |
297 | |
298 | case tok::kw___leave: |
299 | Res = ParseSEHLeaveStatement(); |
300 | SemiError = "__leave"; |
301 | break; |
302 | |
303 | case tok::annot_pragma_vis: |
304 | ProhibitAttributes(Attrs); |
305 | HandlePragmaVisibility(); |
306 | return StmtEmpty(); |
307 | |
308 | case tok::annot_pragma_pack: |
309 | ProhibitAttributes(Attrs); |
310 | HandlePragmaPack(); |
311 | return StmtEmpty(); |
312 | |
313 | case tok::annot_pragma_msstruct: |
314 | ProhibitAttributes(Attrs); |
315 | HandlePragmaMSStruct(); |
316 | return StmtEmpty(); |
317 | |
318 | case tok::annot_pragma_align: |
319 | ProhibitAttributes(Attrs); |
320 | HandlePragmaAlign(); |
321 | return StmtEmpty(); |
322 | |
323 | case tok::annot_pragma_weak: |
324 | ProhibitAttributes(Attrs); |
325 | HandlePragmaWeak(); |
326 | return StmtEmpty(); |
327 | |
328 | case tok::annot_pragma_weakalias: |
329 | ProhibitAttributes(Attrs); |
330 | HandlePragmaWeakAlias(); |
331 | return StmtEmpty(); |
332 | |
333 | case tok::annot_pragma_redefine_extname: |
334 | ProhibitAttributes(Attrs); |
335 | HandlePragmaRedefineExtname(); |
336 | return StmtEmpty(); |
337 | |
338 | case tok::annot_pragma_fp_contract: |
339 | ProhibitAttributes(Attrs); |
340 | Diag(Tok, diag::err_pragma_fp_contract_scope); |
341 | ConsumeAnnotationToken(); |
342 | return StmtError(); |
343 | |
344 | case tok::annot_pragma_fp: |
345 | ProhibitAttributes(Attrs); |
346 | Diag(Tok, diag::err_pragma_fp_scope); |
347 | ConsumeAnnotationToken(); |
348 | return StmtError(); |
349 | |
350 | case tok::annot_pragma_fenv_access: |
351 | ProhibitAttributes(Attrs); |
352 | HandlePragmaFEnvAccess(); |
353 | return StmtEmpty(); |
354 | |
355 | case tok::annot_pragma_opencl_extension: |
356 | ProhibitAttributes(Attrs); |
357 | HandlePragmaOpenCLExtension(); |
358 | return StmtEmpty(); |
359 | |
360 | case tok::annot_pragma_captured: |
361 | ProhibitAttributes(Attrs); |
362 | return HandlePragmaCaptured(); |
363 | |
364 | case tok::annot_pragma_openmp: |
365 | ProhibitAttributes(Attrs); |
366 | return ParseOpenMPDeclarativeOrExecutableDirective(StmtCtx); |
367 | |
368 | case tok::annot_pragma_ms_pointers_to_members: |
369 | ProhibitAttributes(Attrs); |
370 | HandlePragmaMSPointersToMembers(); |
371 | return StmtEmpty(); |
372 | |
373 | case tok::annot_pragma_ms_pragma: |
374 | ProhibitAttributes(Attrs); |
375 | HandlePragmaMSPragma(); |
376 | return StmtEmpty(); |
377 | |
378 | case tok::annot_pragma_ms_vtordisp: |
379 | ProhibitAttributes(Attrs); |
380 | HandlePragmaMSVtorDisp(); |
381 | return StmtEmpty(); |
382 | |
383 | case tok::annot_pragma_loop_hint: |
384 | ProhibitAttributes(Attrs); |
385 | return ParsePragmaLoopHint(Stmts, StmtCtx, TrailingElseLoc, Attrs); |
386 | |
387 | case tok::annot_pragma_dump: |
388 | HandlePragmaDump(); |
389 | return StmtEmpty(); |
390 | |
391 | case tok::annot_pragma_attribute: |
392 | HandlePragmaAttribute(); |
393 | return StmtEmpty(); |
394 | } |
395 | |
396 | |
397 | if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) { |
398 | |
399 | |
400 | |
401 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError); |
402 | |
403 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
404 | } |
405 | |
406 | return Res; |
407 | } |
408 | |
409 | |
410 | StmtResult Parser::ParseExprStatement(ParsedStmtContext StmtCtx) { |
411 | |
412 | Token OldToken = Tok; |
413 | |
414 | ExprStatementTokLoc = Tok.getLocation(); |
415 | |
416 | |
417 | ExprResult Expr(ParseExpression()); |
418 | if (Expr.isInvalid()) { |
419 | |
420 | |
421 | |
422 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
423 | if (Tok.is(tok::semi)) |
424 | ConsumeToken(); |
425 | return Actions.ActOnExprStmtError(); |
426 | } |
427 | |
428 | if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() && |
429 | Actions.CheckCaseExpression(Expr.get())) { |
430 | |
431 | |
432 | Diag(OldToken, diag::err_expected_case_before_expression) |
433 | << FixItHint::CreateInsertion(OldToken.getLocation(), "case "); |
434 | |
435 | |
436 | return ParseCaseStatement(StmtCtx, , Expr); |
437 | } |
438 | |
439 | |
440 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
441 | return handleExprStmt(Expr, StmtCtx); |
442 | } |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | StmtResult Parser::ParseSEHTryBlock() { |
454 | (0) . __assert_fail ("Tok.is(tok..kw___try) && \"Expected '__try'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 454, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw___try) && "Expected '__try'"); |
455 | SourceLocation TryLoc = ConsumeToken(); |
456 | |
457 | if (Tok.isNot(tok::l_brace)) |
458 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
459 | |
460 | StmtResult TryBlock(ParseCompoundStatement( |
461 | , |
462 | Scope::DeclScope | Scope::CompoundStmtScope | Scope::SEHTryScope)); |
463 | if (TryBlock.isInvalid()) |
464 | return TryBlock; |
465 | |
466 | StmtResult Handler; |
467 | if (Tok.is(tok::identifier) && |
468 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
469 | SourceLocation Loc = ConsumeToken(); |
470 | Handler = ParseSEHExceptBlock(Loc); |
471 | } else if (Tok.is(tok::kw___finally)) { |
472 | SourceLocation Loc = ConsumeToken(); |
473 | Handler = ParseSEHFinallyBlock(Loc); |
474 | } else { |
475 | return StmtError(Diag(Tok, diag::err_seh_expected_handler)); |
476 | } |
477 | |
478 | if(Handler.isInvalid()) |
479 | return Handler; |
480 | |
481 | return Actions.ActOnSEHTryBlock(false , |
482 | TryLoc, |
483 | TryBlock.get(), |
484 | Handler.get()); |
485 | } |
486 | |
487 | |
488 | |
489 | |
490 | |
491 | |
492 | StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) { |
493 | PoisonIdentifierRAIIObject raii(Ident__exception_code, false), |
494 | raii2(Ident___exception_code, false), |
495 | raii3(Ident_GetExceptionCode, false); |
496 | |
497 | if (ExpectAndConsume(tok::l_paren)) |
498 | return StmtError(); |
499 | |
500 | ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope | |
501 | Scope::SEHExceptScope); |
502 | |
503 | if (getLangOpts().Borland) { |
504 | Ident__exception_info->setIsPoisoned(false); |
505 | Ident___exception_info->setIsPoisoned(false); |
506 | Ident_GetExceptionInfo->setIsPoisoned(false); |
507 | } |
508 | |
509 | ExprResult FilterExpr; |
510 | { |
511 | ParseScopeFlags FilterScope(this, getCurScope()->getFlags() | |
512 | Scope::SEHFilterScope); |
513 | FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression()); |
514 | } |
515 | |
516 | if (getLangOpts().Borland) { |
517 | Ident__exception_info->setIsPoisoned(true); |
518 | Ident___exception_info->setIsPoisoned(true); |
519 | Ident_GetExceptionInfo->setIsPoisoned(true); |
520 | } |
521 | |
522 | if(FilterExpr.isInvalid()) |
523 | return StmtError(); |
524 | |
525 | if (ExpectAndConsume(tok::r_paren)) |
526 | return StmtError(); |
527 | |
528 | if (Tok.isNot(tok::l_brace)) |
529 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
530 | |
531 | StmtResult Block(ParseCompoundStatement()); |
532 | |
533 | if(Block.isInvalid()) |
534 | return Block; |
535 | |
536 | return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get()); |
537 | } |
538 | |
539 | |
540 | |
541 | |
542 | |
543 | |
544 | StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) { |
545 | PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false), |
546 | raii2(Ident___abnormal_termination, false), |
547 | raii3(Ident_AbnormalTermination, false); |
548 | |
549 | if (Tok.isNot(tok::l_brace)) |
550 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
551 | |
552 | ParseScope FinallyScope(this, 0); |
553 | Actions.ActOnStartSEHFinallyBlock(); |
554 | |
555 | StmtResult Block(ParseCompoundStatement()); |
556 | if(Block.isInvalid()) { |
557 | Actions.ActOnAbortSEHFinallyBlock(); |
558 | return Block; |
559 | } |
560 | |
561 | return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get()); |
562 | } |
563 | |
564 | |
565 | |
566 | |
567 | |
568 | |
569 | StmtResult Parser::ParseSEHLeaveStatement() { |
570 | SourceLocation LeaveLoc = ConsumeToken(); |
571 | return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope()); |
572 | } |
573 | |
574 | |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs, |
581 | ParsedStmtContext StmtCtx) { |
582 | (0) . __assert_fail ("Tok.is(tok..identifier) && Tok.getIdentifierInfo() && \"Not an identifier!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 583, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() && |
583 | (0) . __assert_fail ("Tok.is(tok..identifier) && Tok.getIdentifierInfo() && \"Not an identifier!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 583, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Not an identifier!"); |
584 | |
585 | |
586 | |
587 | StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; |
588 | |
589 | Token IdentTok = Tok; |
590 | ConsumeToken(); |
591 | |
592 | (0) . __assert_fail ("Tok.is(tok..colon) && \"Not a label!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 592, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::colon) && "Not a label!"); |
593 | |
594 | |
595 | SourceLocation ColonLoc = ConsumeToken(); |
596 | |
597 | |
598 | StmtResult SubStmt; |
599 | if (Tok.is(tok::kw___attribute)) { |
600 | ParsedAttributesWithRange TempAttrs(AttrFactory); |
601 | ParseGNUAttributes(TempAttrs); |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | if (!getLangOpts().CPlusPlus || Tok.is(tok::semi)) |
611 | attrs.takeAllFrom(TempAttrs); |
612 | else if (isDeclarationStatement()) { |
613 | StmtVector Stmts; |
614 | |
615 | |
616 | |
617 | |
618 | SubStmt = ParseStatementOrDeclarationAfterAttributes(Stmts, StmtCtx, |
619 | nullptr, TempAttrs); |
620 | if (!TempAttrs.empty() && !SubStmt.isInvalid()) |
621 | SubStmt = Actions.ProcessStmtAttributes(SubStmt.get(), TempAttrs, |
622 | TempAttrs.Range); |
623 | } else { |
624 | Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi; |
625 | } |
626 | } |
627 | |
628 | |
629 | if (!SubStmt.isInvalid() && !SubStmt.isUsable()) |
630 | SubStmt = ParseStatement(nullptr, StmtCtx); |
631 | |
632 | |
633 | if (SubStmt.isInvalid()) |
634 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
635 | |
636 | LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(), |
637 | IdentTok.getLocation()); |
638 | Actions.ProcessDeclAttributeList(Actions.CurScope, LD, attrs); |
639 | attrs.clear(); |
640 | |
641 | return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc, |
642 | SubStmt.get()); |
643 | } |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, |
651 | bool MissingCase, ExprResult Expr) { |
652 | (0) . __assert_fail ("(MissingCase || Tok.is(tok..kw_case)) && \"Not a case stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 652, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!"); |
653 | |
654 | |
655 | |
656 | StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; |
657 | |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | |
672 | |
673 | |
674 | StmtResult TopLevelCase(true); |
675 | |
676 | |
677 | |
678 | |
679 | Stmt *DeepestParsedCaseStmt = nullptr; |
680 | |
681 | |
682 | SourceLocation ColonLoc; |
683 | do { |
684 | SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() : |
685 | ConsumeToken(); |
686 | ColonLoc = SourceLocation(); |
687 | |
688 | if (Tok.is(tok::code_completion)) { |
689 | Actions.CodeCompleteCase(getCurScope()); |
690 | cutOffParsing(); |
691 | return StmtError(); |
692 | } |
693 | |
694 | |
695 | |
696 | |
697 | ColonProtectionRAIIObject ColonProtection(*this); |
698 | |
699 | ExprResult LHS; |
700 | if (!MissingCase) { |
701 | LHS = ParseCaseExpression(CaseLoc); |
702 | if (LHS.isInvalid()) { |
703 | |
704 | |
705 | if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) |
706 | return StmtError(); |
707 | } |
708 | } else { |
709 | LHS = Expr; |
710 | MissingCase = false; |
711 | } |
712 | |
713 | |
714 | SourceLocation DotDotDotLoc; |
715 | ExprResult RHS; |
716 | if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) { |
717 | Diag(DotDotDotLoc, diag::ext_gnu_case_range); |
718 | RHS = ParseCaseExpression(CaseLoc); |
719 | if (RHS.isInvalid()) { |
720 | if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) |
721 | return StmtError(); |
722 | } |
723 | } |
724 | |
725 | ColonProtection.restore(); |
726 | |
727 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
728 | } else if (TryConsumeToken(tok::semi, ColonLoc) || |
729 | TryConsumeToken(tok::coloncolon, ColonLoc)) { |
730 | |
731 | Diag(ColonLoc, diag::err_expected_after) |
732 | << "'case'" << tok::colon |
733 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
734 | } else { |
735 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
736 | Diag(ExpectedLoc, diag::err_expected_after) |
737 | << "'case'" << tok::colon |
738 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
739 | ColonLoc = ExpectedLoc; |
740 | } |
741 | |
742 | StmtResult Case = |
743 | Actions.ActOnCaseStmt(CaseLoc, LHS, DotDotDotLoc, RHS, ColonLoc); |
744 | |
745 | |
746 | |
747 | if (Case.isInvalid()) { |
748 | if (TopLevelCase.isInvalid()) |
749 | return ParseStatement(, StmtCtx); |
750 | |
751 | } else { |
752 | |
753 | |
754 | Stmt *NextDeepest = Case.get(); |
755 | if (TopLevelCase.isInvalid()) |
756 | TopLevelCase = Case; |
757 | else |
758 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get()); |
759 | DeepestParsedCaseStmt = NextDeepest; |
760 | } |
761 | |
762 | |
763 | } while (Tok.is(tok::kw_case)); |
764 | |
765 | |
766 | StmtResult SubStmt; |
767 | |
768 | if (Tok.isNot(tok::r_brace)) { |
769 | SubStmt = ParseStatement(, StmtCtx); |
770 | } else { |
771 | |
772 | |
773 | |
774 | if (ColonLoc.isValid()) { |
775 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
776 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
777 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
778 | } |
779 | SubStmt = StmtError(); |
780 | } |
781 | |
782 | |
783 | if (DeepestParsedCaseStmt) { |
784 | |
785 | if (SubStmt.isInvalid()) |
786 | SubStmt = Actions.ActOnNullStmt(SourceLocation()); |
787 | Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get()); |
788 | } |
789 | |
790 | |
791 | return TopLevelCase; |
792 | } |
793 | |
794 | |
795 | |
796 | |
797 | |
798 | |
799 | StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) { |
800 | (0) . __assert_fail ("Tok.is(tok..kw_default) && \"Not a default stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 800, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_default) && "Not a default stmt!"); |
801 | |
802 | |
803 | |
804 | StmtCtx &= ~ParsedStmtContext::AllowDeclarationsInC; |
805 | |
806 | SourceLocation DefaultLoc = ConsumeToken(); |
807 | |
808 | SourceLocation ColonLoc; |
809 | if (TryConsumeToken(tok::colon, ColonLoc)) { |
810 | } else if (TryConsumeToken(tok::semi, ColonLoc)) { |
811 | |
812 | Diag(ColonLoc, diag::err_expected_after) |
813 | << "'default'" << tok::colon |
814 | << FixItHint::CreateReplacement(ColonLoc, ":"); |
815 | } else { |
816 | SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation); |
817 | Diag(ExpectedLoc, diag::err_expected_after) |
818 | << "'default'" << tok::colon |
819 | << FixItHint::CreateInsertion(ExpectedLoc, ":"); |
820 | ColonLoc = ExpectedLoc; |
821 | } |
822 | |
823 | StmtResult SubStmt; |
824 | |
825 | if (Tok.isNot(tok::r_brace)) { |
826 | SubStmt = ParseStatement(, StmtCtx); |
827 | } else { |
828 | |
829 | |
830 | SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc); |
831 | Diag(AfterColonLoc, diag::err_label_end_of_compound_statement) |
832 | << FixItHint::CreateInsertion(AfterColonLoc, " ;"); |
833 | SubStmt = true; |
834 | } |
835 | |
836 | |
837 | if (SubStmt.isInvalid()) |
838 | SubStmt = Actions.ActOnNullStmt(ColonLoc); |
839 | |
840 | return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, |
841 | SubStmt.get(), getCurScope()); |
842 | } |
843 | |
844 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) { |
845 | return ParseCompoundStatement(isStmtExpr, |
846 | Scope::DeclScope | Scope::CompoundStmtScope); |
847 | } |
848 | |
849 | |
850 | |
851 | |
852 | |
853 | |
854 | |
855 | |
856 | |
857 | |
858 | |
859 | |
860 | |
861 | |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | |
868 | |
869 | |
870 | |
871 | StmtResult Parser::ParseCompoundStatement(bool isStmtExpr, |
872 | unsigned ScopeFlags) { |
873 | (0) . __assert_fail ("Tok.is(tok..l_brace) && \"Not a compount stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 873, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::l_brace) && "Not a compount stmt!"); |
874 | |
875 | |
876 | |
877 | ParseScope CompoundScope(this, ScopeFlags); |
878 | |
879 | |
880 | return ParseCompoundStatementBody(isStmtExpr); |
881 | } |
882 | |
883 | |
884 | |
885 | |
886 | void Parser::ParseCompoundStatementLeadingPragmas() { |
887 | bool checkForPragmas = true; |
888 | while (checkForPragmas) { |
889 | switch (Tok.getKind()) { |
890 | case tok::annot_pragma_vis: |
891 | HandlePragmaVisibility(); |
892 | break; |
893 | case tok::annot_pragma_pack: |
894 | HandlePragmaPack(); |
895 | break; |
896 | case tok::annot_pragma_msstruct: |
897 | HandlePragmaMSStruct(); |
898 | break; |
899 | case tok::annot_pragma_align: |
900 | HandlePragmaAlign(); |
901 | break; |
902 | case tok::annot_pragma_weak: |
903 | HandlePragmaWeak(); |
904 | break; |
905 | case tok::annot_pragma_weakalias: |
906 | HandlePragmaWeakAlias(); |
907 | break; |
908 | case tok::annot_pragma_redefine_extname: |
909 | HandlePragmaRedefineExtname(); |
910 | break; |
911 | case tok::annot_pragma_opencl_extension: |
912 | HandlePragmaOpenCLExtension(); |
913 | break; |
914 | case tok::annot_pragma_fp_contract: |
915 | HandlePragmaFPContract(); |
916 | break; |
917 | case tok::annot_pragma_fp: |
918 | HandlePragmaFP(); |
919 | break; |
920 | case tok::annot_pragma_fenv_access: |
921 | HandlePragmaFEnvAccess(); |
922 | break; |
923 | case tok::annot_pragma_ms_pointers_to_members: |
924 | HandlePragmaMSPointersToMembers(); |
925 | break; |
926 | case tok::annot_pragma_ms_pragma: |
927 | HandlePragmaMSPragma(); |
928 | break; |
929 | case tok::annot_pragma_ms_vtordisp: |
930 | HandlePragmaMSVtorDisp(); |
931 | break; |
932 | case tok::annot_pragma_dump: |
933 | HandlePragmaDump(); |
934 | break; |
935 | default: |
936 | checkForPragmas = false; |
937 | break; |
938 | } |
939 | } |
940 | |
941 | } |
942 | |
943 | |
944 | |
945 | bool Parser::(StmtVector &Stmts) { |
946 | if (!Tok.is(tok::semi)) |
947 | return false; |
948 | |
949 | SourceLocation StartLoc = Tok.getLocation(); |
950 | SourceLocation EndLoc; |
951 | |
952 | while (Tok.is(tok::semi) && !Tok.hasLeadingEmptyMacro() && |
953 | Tok.getLocation().isValid() && !Tok.getLocation().isMacroID()) { |
954 | EndLoc = Tok.getLocation(); |
955 | |
956 | |
957 | StmtResult R = |
958 | ParseStatementOrDeclaration(Stmts, ParsedStmtContext::SubStmt); |
959 | if (R.isUsable()) |
960 | Stmts.push_back(R.get()); |
961 | } |
962 | |
963 | |
964 | if (EndLoc.isInvalid()) |
965 | return false; |
966 | |
967 | Diag(StartLoc, diag::warn_null_statement) |
968 | << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc)); |
969 | return true; |
970 | } |
971 | |
972 | StmtResult Parser::handleExprStmt(ExprResult E, ParsedStmtContext StmtCtx) { |
973 | bool IsStmtExprResult = false; |
974 | if ((StmtCtx & ParsedStmtContext::InStmtExpr) != ParsedStmtContext()) { |
975 | |
976 | |
977 | |
978 | IsStmtExprResult = Tok.is(tok::r_brace) && NextToken().is(tok::r_paren); |
979 | } |
980 | |
981 | if (IsStmtExprResult) |
982 | E = Actions.ActOnStmtExprResult(E); |
983 | return Actions.ActOnExprStmt(E, !IsStmtExprResult); |
984 | } |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { |
991 | PrettyStackTraceLoc CrashInfo(PP.getSourceManager(), |
992 | Tok.getLocation(), |
993 | "in compound statement ('{}')"); |
994 | |
995 | |
996 | |
997 | Sema::FPContractStateRAII SaveFPContractState(Actions); |
998 | |
999 | InMessageExpressionRAIIObject InMessage(*this, false); |
1000 | BalancedDelimiterTracker T(*this, tok::l_brace); |
1001 | if (T.consumeOpen()) |
1002 | return StmtError(); |
1003 | |
1004 | Sema::CompoundScopeRAII CompoundScope(Actions, isStmtExpr); |
1005 | |
1006 | |
1007 | ParseCompoundStatementLeadingPragmas(); |
1008 | |
1009 | StmtVector Stmts; |
1010 | |
1011 | |
1012 | |
1013 | while (Tok.is(tok::kw___label__)) { |
1014 | SourceLocation LabelLoc = ConsumeToken(); |
1015 | |
1016 | SmallVector<Decl *, 8> DeclsInGroup; |
1017 | while (1) { |
1018 | if (Tok.isNot(tok::identifier)) { |
1019 | Diag(Tok, diag::err_expected) << tok::identifier; |
1020 | break; |
1021 | } |
1022 | |
1023 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
1024 | SourceLocation IdLoc = ConsumeToken(); |
1025 | DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc)); |
1026 | |
1027 | if (!TryConsumeToken(tok::comma)) |
1028 | break; |
1029 | } |
1030 | |
1031 | DeclSpec DS(AttrFactory); |
1032 | DeclGroupPtrTy Res = |
1033 | Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
1034 | StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation()); |
1035 | |
1036 | ExpectAndConsumeSemi(diag::err_expected_semi_declaration); |
1037 | if (R.isUsable()) |
1038 | Stmts.push_back(R.get()); |
1039 | } |
1040 | |
1041 | ParsedStmtContext SubStmtCtx = |
1042 | ParsedStmtContext::Compound | |
1043 | (isStmtExpr ? ParsedStmtContext::InStmtExpr : ParsedStmtContext()); |
1044 | |
1045 | while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) && |
1046 | Tok.isNot(tok::eof)) { |
1047 | if (Tok.is(tok::annot_pragma_unused)) { |
1048 | HandlePragmaUnused(); |
1049 | continue; |
1050 | } |
1051 | |
1052 | if (ConsumeNullStmt(Stmts)) |
1053 | continue; |
1054 | |
1055 | StmtResult R; |
1056 | if (Tok.isNot(tok::kw___extension__)) { |
1057 | R = ParseStatementOrDeclaration(Stmts, SubStmtCtx); |
1058 | } else { |
1059 | |
1060 | |
1061 | |
1062 | |
1063 | SourceLocation ExtLoc = ConsumeToken(); |
1064 | while (Tok.is(tok::kw___extension__)) |
1065 | ConsumeToken(); |
1066 | |
1067 | ParsedAttributesWithRange attrs(AttrFactory); |
1068 | MaybeParseCXX11Attributes(attrs, nullptr, |
1069 | true); |
1070 | |
1071 | |
1072 | if (isDeclarationStatement()) { |
1073 | |
1074 | |
1075 | ExtensionRAIIObject O(Diags); |
1076 | |
1077 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
1078 | DeclGroupPtrTy Res = |
1079 | ParseDeclaration(DeclaratorContext::BlockContext, DeclEnd, attrs); |
1080 | R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd); |
1081 | } else { |
1082 | |
1083 | ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc)); |
1084 | |
1085 | if (Res.isInvalid()) { |
1086 | SkipUntil(tok::semi); |
1087 | continue; |
1088 | } |
1089 | |
1090 | |
1091 | |
1092 | ExpectAndConsumeSemi(diag::err_expected_semi_after_expr); |
1093 | R = handleExprStmt(Res, SubStmtCtx); |
1094 | if (R.isUsable()) |
1095 | R = Actions.ProcessStmtAttributes(R.get(), attrs, attrs.Range); |
1096 | } |
1097 | } |
1098 | |
1099 | if (R.isUsable()) |
1100 | Stmts.push_back(R.get()); |
1101 | } |
1102 | |
1103 | SourceLocation CloseLoc = Tok.getLocation(); |
1104 | |
1105 | |
1106 | if (!T.consumeClose()) |
1107 | |
1108 | |
1109 | CloseLoc = T.getCloseLocation(); |
1110 | |
1111 | return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc, |
1112 | Stmts, isStmtExpr); |
1113 | } |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | |
1119 | |
1120 | |
1121 | |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | |
1127 | bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, |
1128 | Sema::ConditionResult &Cond, |
1129 | SourceLocation Loc, |
1130 | Sema::ConditionKind CK) { |
1131 | BalancedDelimiterTracker T(*this, tok::l_paren); |
1132 | T.consumeOpen(); |
1133 | |
1134 | if (getLangOpts().CPlusPlus) |
1135 | Cond = ParseCXXCondition(InitStmt, Loc, CK); |
1136 | else { |
1137 | ExprResult CondExpr = ParseExpression(); |
1138 | |
1139 | |
1140 | if (CondExpr.isInvalid()) |
1141 | Cond = Sema::ConditionError(); |
1142 | else |
1143 | Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK); |
1144 | } |
1145 | |
1146 | |
1147 | |
1148 | |
1149 | if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) { |
1150 | SkipUntil(tok::semi); |
1151 | |
1152 | |
1153 | if (Tok.isNot(tok::r_paren)) |
1154 | return true; |
1155 | } |
1156 | |
1157 | |
1158 | T.consumeClose(); |
1159 | |
1160 | |
1161 | |
1162 | |
1163 | while (Tok.is(tok::r_paren)) { |
1164 | Diag(Tok, diag::err_extraneous_rparen_in_condition) |
1165 | << FixItHint::CreateRemoval(Tok.getLocation()); |
1166 | ConsumeParen(); |
1167 | } |
1168 | |
1169 | return false; |
1170 | } |
1171 | |
1172 | |
1173 | |
1174 | |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { |
1181 | (0) . __assert_fail ("Tok.is(tok..kw_if) && \"Not an if stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1181, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_if) && "Not an if stmt!"); |
1182 | SourceLocation IfLoc = ConsumeToken(); |
1183 | |
1184 | bool IsConstexpr = false; |
1185 | if (Tok.is(tok::kw_constexpr)) { |
1186 | Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if |
1187 | : diag::ext_constexpr_if); |
1188 | IsConstexpr = true; |
1189 | ConsumeToken(); |
1190 | } |
1191 | |
1192 | if (Tok.isNot(tok::l_paren)) { |
1193 | Diag(Tok, diag::err_expected_lparen_after) << "if"; |
1194 | SkipUntil(tok::semi); |
1195 | return StmtError(); |
1196 | } |
1197 | |
1198 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
1199 | |
1200 | |
1201 | |
1202 | |
1203 | |
1204 | |
1205 | |
1206 | |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX); |
1213 | |
1214 | |
1215 | StmtResult InitStmt; |
1216 | Sema::ConditionResult Cond; |
1217 | if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc, |
1218 | IsConstexpr ? Sema::ConditionKind::ConstexprIf |
1219 | : Sema::ConditionKind::Boolean)) |
1220 | return StmtError(); |
1221 | |
1222 | llvm::Optional<bool> ConstexprCondition; |
1223 | if (IsConstexpr) |
1224 | ConstexprCondition = Cond.getKnownValue(); |
1225 | |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
1245 | |
1246 | |
1247 | SourceLocation ThenStmtLoc = Tok.getLocation(); |
1248 | |
1249 | SourceLocation InnerStatementTrailingElseLoc; |
1250 | StmtResult ThenStmt; |
1251 | { |
1252 | EnterExpressionEvaluationContext PotentiallyDiscarded( |
1253 | Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr, |
1254 | Sema::ExpressionEvaluationContextRecord::EK_Other, |
1255 | ConstexprCondition && !*ConstexprCondition); |
1256 | ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc); |
1257 | } |
1258 | |
1259 | |
1260 | InnerScope.Exit(); |
1261 | |
1262 | |
1263 | SourceLocation ElseLoc; |
1264 | SourceLocation ElseStmtLoc; |
1265 | StmtResult ElseStmt; |
1266 | |
1267 | if (Tok.is(tok::kw_else)) { |
1268 | if (TrailingElseLoc) |
1269 | *TrailingElseLoc = Tok.getLocation(); |
1270 | |
1271 | ElseLoc = ConsumeToken(); |
1272 | ElseStmtLoc = Tok.getLocation(); |
1273 | |
1274 | |
1275 | |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, |
1284 | Tok.is(tok::l_brace)); |
1285 | |
1286 | EnterExpressionEvaluationContext PotentiallyDiscarded( |
1287 | Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr, |
1288 | Sema::ExpressionEvaluationContextRecord::EK_Other, |
1289 | ConstexprCondition && *ConstexprCondition); |
1290 | ElseStmt = ParseStatement(); |
1291 | |
1292 | |
1293 | InnerScope.Exit(); |
1294 | } else if (Tok.is(tok::code_completion)) { |
1295 | Actions.CodeCompleteAfterIf(getCurScope()); |
1296 | cutOffParsing(); |
1297 | return StmtError(); |
1298 | } else if (InnerStatementTrailingElseLoc.isValid()) { |
1299 | Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); |
1300 | } |
1301 | |
1302 | IfScope.Exit(); |
1303 | |
1304 | |
1305 | |
1306 | |
1307 | if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) || |
1308 | (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) || |
1309 | (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) { |
1310 | |
1311 | return StmtError(); |
1312 | } |
1313 | |
1314 | |
1315 | if (ThenStmt.isInvalid()) |
1316 | ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc); |
1317 | if (ElseStmt.isInvalid()) |
1318 | ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc); |
1319 | |
1320 | return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond, |
1321 | ThenStmt.get(), ElseLoc, ElseStmt.get()); |
1322 | } |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) { |
1329 | (0) . __assert_fail ("Tok.is(tok..kw_switch) && \"Not a switch stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1329, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_switch) && "Not a switch stmt!"); |
1330 | SourceLocation SwitchLoc = ConsumeToken(); |
1331 | |
1332 | if (Tok.isNot(tok::l_paren)) { |
1333 | Diag(Tok, diag::err_expected_lparen_after) << "switch"; |
1334 | SkipUntil(tok::semi); |
1335 | return StmtError(); |
1336 | } |
1337 | |
1338 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
1339 | |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | unsigned ScopeFlags = Scope::SwitchScope; |
1353 | if (C99orCXX) |
1354 | ScopeFlags |= Scope::DeclScope | Scope::ControlScope; |
1355 | ParseScope SwitchScope(this, ScopeFlags); |
1356 | |
1357 | |
1358 | StmtResult InitStmt; |
1359 | Sema::ConditionResult Cond; |
1360 | if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc, |
1361 | Sema::ConditionKind::Switch)) |
1362 | return StmtError(); |
1363 | |
1364 | StmtResult Switch = |
1365 | Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond); |
1366 | |
1367 | if (Switch.isInvalid()) { |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | if (Tok.is(tok::l_brace)) { |
1373 | ConsumeBrace(); |
1374 | SkipUntil(tok::r_brace); |
1375 | } else |
1376 | SkipUntil(tok::semi); |
1377 | return Switch; |
1378 | } |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | getCurScope()->AddFlags(Scope::BreakScope); |
1392 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
1393 | |
1394 | |
1395 | |
1396 | if (C99orCXX) |
1397 | getCurScope()->decrementMSManglingNumber(); |
1398 | |
1399 | |
1400 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
1401 | |
1402 | |
1403 | InnerScope.Exit(); |
1404 | SwitchScope.Exit(); |
1405 | |
1406 | return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get()); |
1407 | } |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { |
1414 | (0) . __assert_fail ("Tok.is(tok..kw_while) && \"Not a while stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1414, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_while) && "Not a while stmt!"); |
1415 | SourceLocation WhileLoc = Tok.getLocation(); |
1416 | ConsumeToken(); |
1417 | |
1418 | if (Tok.isNot(tok::l_paren)) { |
1419 | Diag(Tok, diag::err_expected_lparen_after) << "while"; |
1420 | SkipUntil(tok::semi); |
1421 | return StmtError(); |
1422 | } |
1423 | |
1424 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
1425 | |
1426 | |
1427 | |
1428 | |
1429 | |
1430 | |
1431 | |
1432 | |
1433 | |
1434 | |
1435 | |
1436 | |
1437 | |
1438 | unsigned ScopeFlags; |
1439 | if (C99orCXX) |
1440 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | |
1441 | Scope::DeclScope | Scope::ControlScope; |
1442 | else |
1443 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
1444 | ParseScope WhileScope(this, ScopeFlags); |
1445 | |
1446 | |
1447 | Sema::ConditionResult Cond; |
1448 | if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc, |
1449 | Sema::ConditionKind::Boolean)) |
1450 | return StmtError(); |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | |
1461 | |
1462 | |
1463 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
1464 | |
1465 | |
1466 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
1467 | |
1468 | |
1469 | InnerScope.Exit(); |
1470 | WhileScope.Exit(); |
1471 | |
1472 | if (Cond.isInvalid() || Body.isInvalid()) |
1473 | return StmtError(); |
1474 | |
1475 | return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get()); |
1476 | } |
1477 | |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | StmtResult Parser::ParseDoStatement() { |
1483 | (0) . __assert_fail ("Tok.is(tok..kw_do) && \"Not a do stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1483, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_do) && "Not a do stmt!"); |
1484 | SourceLocation DoLoc = ConsumeToken(); |
1485 | |
1486 | |
1487 | |
1488 | unsigned ScopeFlags; |
1489 | if (getLangOpts().C99) |
1490 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope; |
1491 | else |
1492 | ScopeFlags = Scope::BreakScope | Scope::ContinueScope; |
1493 | |
1494 | ParseScope DoScope(this, ScopeFlags); |
1495 | |
1496 | |
1497 | |
1498 | |
1499 | |
1500 | |
1501 | |
1502 | |
1503 | |
1504 | bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus; |
1505 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace)); |
1506 | |
1507 | |
1508 | StmtResult Body(ParseStatement()); |
1509 | |
1510 | |
1511 | InnerScope.Exit(); |
1512 | |
1513 | if (Tok.isNot(tok::kw_while)) { |
1514 | if (!Body.isInvalid()) { |
1515 | Diag(Tok, diag::err_expected_while); |
1516 | Diag(DoLoc, diag::note_matching) << "'do'"; |
1517 | SkipUntil(tok::semi, StopBeforeMatch); |
1518 | } |
1519 | return StmtError(); |
1520 | } |
1521 | SourceLocation WhileLoc = ConsumeToken(); |
1522 | |
1523 | if (Tok.isNot(tok::l_paren)) { |
1524 | Diag(Tok, diag::err_expected_lparen_after) << "do/while"; |
1525 | SkipUntil(tok::semi, StopBeforeMatch); |
1526 | return StmtError(); |
1527 | } |
1528 | |
1529 | |
1530 | BalancedDelimiterTracker T(*this, tok::l_paren); |
1531 | T.consumeOpen(); |
1532 | |
1533 | |
1534 | DiagnoseAndSkipCXX11Attributes(); |
1535 | |
1536 | ExprResult Cond = ParseExpression(); |
1537 | |
1538 | if (Cond.isUsable()) |
1539 | Cond = Actions.CorrectDelayedTyposInExpr(Cond); |
1540 | T.consumeClose(); |
1541 | DoScope.Exit(); |
1542 | |
1543 | if (Cond.isInvalid() || Body.isInvalid()) |
1544 | return StmtError(); |
1545 | |
1546 | return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(), |
1547 | Cond.get(), T.getCloseLocation()); |
1548 | } |
1549 | |
1550 | bool Parser::isForRangeIdentifier() { |
1551 | assert(Tok.is(tok::identifier)); |
1552 | |
1553 | const Token &Next = NextToken(); |
1554 | if (Next.is(tok::colon)) |
1555 | return true; |
1556 | |
1557 | if (Next.isOneOf(tok::l_square, tok::kw_alignas)) { |
1558 | TentativeParsingAction PA(*this); |
1559 | ConsumeToken(); |
1560 | SkipCXX11Attributes(); |
1561 | bool Result = Tok.is(tok::colon); |
1562 | PA.Revert(); |
1563 | return Result; |
1564 | } |
1565 | |
1566 | return false; |
1567 | } |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | |
1585 | |
1586 | |
1587 | |
1588 | |
1589 | |
1590 | |
1591 | StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { |
1592 | (0) . __assert_fail ("Tok.is(tok..kw_for) && \"Not a for stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1592, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_for) && "Not a for stmt!"); |
1593 | SourceLocation ForLoc = ConsumeToken(); |
1594 | |
1595 | SourceLocation CoawaitLoc; |
1596 | if (Tok.is(tok::kw_co_await)) |
1597 | CoawaitLoc = ConsumeToken(); |
1598 | |
1599 | if (Tok.isNot(tok::l_paren)) { |
1600 | Diag(Tok, diag::err_expected_lparen_after) << "for"; |
1601 | SkipUntil(tok::semi); |
1602 | return StmtError(); |
1603 | } |
1604 | |
1605 | bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus || |
1606 | getLangOpts().ObjC; |
1607 | |
1608 | |
1609 | |
1610 | |
1611 | |
1612 | |
1613 | |
1614 | |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | |
1623 | unsigned ScopeFlags = 0; |
1624 | if (C99orCXXorObjC) |
1625 | ScopeFlags = Scope::DeclScope | Scope::ControlScope; |
1626 | |
1627 | ParseScope ForScope(this, ScopeFlags); |
1628 | |
1629 | BalancedDelimiterTracker T(*this, tok::l_paren); |
1630 | T.consumeOpen(); |
1631 | |
1632 | ExprResult Value; |
1633 | |
1634 | bool ForEach = false; |
1635 | StmtResult FirstPart; |
1636 | Sema::ConditionResult SecondPart; |
1637 | ExprResult Collection; |
1638 | ForRangeInfo ForRangeInfo; |
1639 | FullExprArg ThirdPart(Actions); |
1640 | |
1641 | if (Tok.is(tok::code_completion)) { |
1642 | Actions.CodeCompleteOrdinaryName(getCurScope(), |
1643 | C99orCXXorObjC? Sema::PCC_ForInit |
1644 | : Sema::PCC_Expression); |
1645 | cutOffParsing(); |
1646 | return StmtError(); |
1647 | } |
1648 | |
1649 | ParsedAttributesWithRange attrs(AttrFactory); |
1650 | MaybeParseCXX11Attributes(attrs); |
1651 | |
1652 | SourceLocation EmptyInitStmtSemiLoc; |
1653 | |
1654 | |
1655 | if (Tok.is(tok::semi)) { |
1656 | ProhibitAttributes(attrs); |
1657 | |
1658 | SourceLocation SemiLoc = Tok.getLocation(); |
1659 | if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID()) |
1660 | EmptyInitStmtSemiLoc = SemiLoc; |
1661 | ConsumeToken(); |
1662 | } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) && |
1663 | isForRangeIdentifier()) { |
1664 | ProhibitAttributes(attrs); |
1665 | IdentifierInfo *Name = Tok.getIdentifierInfo(); |
1666 | SourceLocation Loc = ConsumeToken(); |
1667 | MaybeParseCXX11Attributes(attrs); |
1668 | |
1669 | ForRangeInfo.ColonLoc = ConsumeToken(); |
1670 | if (Tok.is(tok::l_brace)) |
1671 | ForRangeInfo.RangeExpr = ParseBraceInitializer(); |
1672 | else |
1673 | ForRangeInfo.RangeExpr = ParseExpression(); |
1674 | |
1675 | Diag(Loc, diag::err_for_range_identifier) |
1676 | << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus17) |
1677 | ? FixItHint::CreateInsertion(Loc, "auto &&") |
1678 | : FixItHint()); |
1679 | |
1680 | ForRangeInfo.LoopVar = Actions.ActOnCXXForRangeIdentifier( |
1681 | getCurScope(), Loc, Name, attrs, attrs.Range.getEnd()); |
1682 | } else if (isForInitDeclaration()) { |
1683 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
1684 | |
1685 | |
1686 | if (!C99orCXXorObjC) { |
1687 | Diag(Tok, diag::ext_c99_variable_decl_in_for_loop); |
1688 | Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop); |
1689 | } |
1690 | |
1691 | |
1692 | bool MightBeForRangeStmt = getLangOpts().CPlusPlus; |
1693 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
1694 | |
1695 | SourceLocation DeclStart = Tok.getLocation(), DeclEnd; |
1696 | DeclGroupPtrTy DG = ParseSimpleDeclaration( |
1697 | DeclaratorContext::ForContext, DeclEnd, attrs, false, |
1698 | MightBeForRangeStmt ? &ForRangeInfo : nullptr); |
1699 | FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation()); |
1700 | if (ForRangeInfo.ParsedForRangeDecl()) { |
1701 | Diag(ForRangeInfo.ColonLoc, getLangOpts().CPlusPlus11 ? |
1702 | diag::warn_cxx98_compat_for_range : diag::ext_for_range); |
1703 | ForRangeInfo.LoopVar = FirstPart; |
1704 | FirstPart = StmtResult(); |
1705 | } else if (Tok.is(tok::semi)) { |
1706 | ConsumeToken(); |
1707 | } else if ((ForEach = isTokIdentifier_in())) { |
1708 | Actions.ActOnForEachDeclStmt(DG); |
1709 | |
1710 | ConsumeToken(); |
1711 | |
1712 | if (Tok.is(tok::code_completion)) { |
1713 | Actions.CodeCompleteObjCForCollection(getCurScope(), DG); |
1714 | cutOffParsing(); |
1715 | return StmtError(); |
1716 | } |
1717 | Collection = ParseExpression(); |
1718 | } else { |
1719 | Diag(Tok, diag::err_expected_semi_for); |
1720 | } |
1721 | } else { |
1722 | ProhibitAttributes(attrs); |
1723 | Value = Actions.CorrectDelayedTyposInExpr(ParseExpression()); |
1724 | |
1725 | ForEach = isTokIdentifier_in(); |
1726 | |
1727 | |
1728 | if (!Value.isInvalid()) { |
1729 | if (ForEach) |
1730 | FirstPart = Actions.ActOnForEachLValueExpr(Value.get()); |
1731 | else { |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | |
1737 | bool IsRangeBasedFor = |
1738 | getLangOpts().CPlusPlus11 && !ForEach && Tok.is(tok::colon); |
1739 | FirstPart = Actions.ActOnExprStmt(Value, !IsRangeBasedFor); |
1740 | } |
1741 | } |
1742 | |
1743 | if (Tok.is(tok::semi)) { |
1744 | ConsumeToken(); |
1745 | } else if (ForEach) { |
1746 | ConsumeToken(); |
1747 | |
1748 | if (Tok.is(tok::code_completion)) { |
1749 | Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr); |
1750 | cutOffParsing(); |
1751 | return StmtError(); |
1752 | } |
1753 | Collection = ParseExpression(); |
1754 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) { |
1755 | |
1756 | |
1757 | Diag(Tok, diag::err_for_range_expected_decl) |
1758 | << FirstPart.get()->getSourceRange(); |
1759 | SkipUntil(tok::r_paren, StopBeforeMatch); |
1760 | SecondPart = Sema::ConditionError(); |
1761 | } else { |
1762 | if (!Value.isInvalid()) { |
1763 | Diag(Tok, diag::err_expected_semi_for); |
1764 | } else { |
1765 | |
1766 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
1767 | if (Tok.is(tok::semi)) |
1768 | ConsumeToken(); |
1769 | } |
1770 | } |
1771 | } |
1772 | |
1773 | |
1774 | getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope); |
1775 | if (!ForEach && !ForRangeInfo.ParsedForRangeDecl() && |
1776 | !SecondPart.isInvalid()) { |
1777 | |
1778 | if (Tok.is(tok::semi)) { |
1779 | |
1780 | } else if (Tok.is(tok::r_paren)) { |
1781 | |
1782 | } else { |
1783 | if (getLangOpts().CPlusPlus) { |
1784 | |
1785 | |
1786 | bool MightBeForRangeStmt = !ForRangeInfo.ParsedForRangeDecl(); |
1787 | ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt); |
1788 | SecondPart = |
1789 | ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean, |
1790 | MightBeForRangeStmt ? &ForRangeInfo : nullptr); |
1791 | |
1792 | if (ForRangeInfo.ParsedForRangeDecl()) { |
1793 | Diag(FirstPart.get() ? FirstPart.get()->getBeginLoc() |
1794 | : ForRangeInfo.ColonLoc, |
1795 | getLangOpts().CPlusPlus2a |
1796 | ? diag::warn_cxx17_compat_for_range_init_stmt |
1797 | : diag::ext_for_range_init_stmt) |
1798 | << (FirstPart.get() ? FirstPart.get()->getSourceRange() |
1799 | : SourceRange()); |
1800 | if (EmptyInitStmtSemiLoc.isValid()) { |
1801 | Diag(EmptyInitStmtSemiLoc, diag::warn_empty_init_statement) |
1802 | << 2 |
1803 | << FixItHint::CreateRemoval(EmptyInitStmtSemiLoc); |
1804 | } |
1805 | } |
1806 | } else { |
1807 | ExprResult SecondExpr = ParseExpression(); |
1808 | if (SecondExpr.isInvalid()) |
1809 | SecondPart = Sema::ConditionError(); |
1810 | else |
1811 | SecondPart = |
1812 | Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(), |
1813 | Sema::ConditionKind::Boolean); |
1814 | } |
1815 | } |
1816 | } |
1817 | |
1818 | |
1819 | if (!ForEach && !ForRangeInfo.ParsedForRangeDecl()) { |
1820 | if (Tok.isNot(tok::semi)) { |
1821 | if (!SecondPart.isInvalid()) |
1822 | Diag(Tok, diag::err_expected_semi_for); |
1823 | else |
1824 | |
1825 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
1826 | } |
1827 | |
1828 | if (Tok.is(tok::semi)) { |
1829 | ConsumeToken(); |
1830 | } |
1831 | |
1832 | if (Tok.isNot(tok::r_paren)) { |
1833 | ExprResult Third = ParseExpression(); |
1834 | |
1835 | |
1836 | ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get()); |
1837 | } |
1838 | } |
1839 | |
1840 | T.consumeClose(); |
1841 | |
1842 | |
1843 | |
1844 | if (CoawaitLoc.isValid() && !ForRangeInfo.ParsedForRangeDecl()) { |
1845 | Diag(CoawaitLoc, diag::err_for_co_await_not_range_for); |
1846 | CoawaitLoc = SourceLocation(); |
1847 | } |
1848 | |
1849 | |
1850 | |
1851 | |
1852 | StmtResult ForRangeStmt; |
1853 | StmtResult ForEachStmt; |
1854 | |
1855 | if (ForRangeInfo.ParsedForRangeDecl()) { |
1856 | ExprResult CorrectedRange = |
1857 | Actions.CorrectDelayedTyposInExpr(ForRangeInfo.RangeExpr.get()); |
1858 | ForRangeStmt = Actions.ActOnCXXForRangeStmt( |
1859 | getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(), |
1860 | ForRangeInfo.LoopVar.get(), ForRangeInfo.ColonLoc, CorrectedRange.get(), |
1861 | T.getCloseLocation(), Sema::BFRK_Build); |
1862 | |
1863 | |
1864 | |
1865 | } else if (ForEach) { |
1866 | ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, |
1867 | FirstPart.get(), |
1868 | Collection.get(), |
1869 | T.getCloseLocation()); |
1870 | } else { |
1871 | |
1872 | |
1873 | if (getLangOpts().OpenMP && FirstPart.isUsable()) { |
1874 | Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get()); |
1875 | } |
1876 | } |
1877 | |
1878 | |
1879 | |
1880 | |
1881 | |
1882 | |
1883 | |
1884 | |
1885 | |
1886 | |
1887 | |
1888 | |
1889 | ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC, |
1890 | Tok.is(tok::l_brace)); |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | if (C99orCXXorObjC) |
1897 | getCurScope()->decrementMSManglingNumber(); |
1898 | |
1899 | |
1900 | StmtResult Body(ParseStatement(TrailingElseLoc)); |
1901 | |
1902 | |
1903 | InnerScope.Exit(); |
1904 | |
1905 | |
1906 | ForScope.Exit(); |
1907 | |
1908 | if (Body.isInvalid()) |
1909 | return StmtError(); |
1910 | |
1911 | if (ForEach) |
1912 | return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(), |
1913 | Body.get()); |
1914 | |
1915 | if (ForRangeInfo.ParsedForRangeDecl()) |
1916 | return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get()); |
1917 | |
1918 | return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(), |
1919 | SecondPart, ThirdPart, T.getCloseLocation(), |
1920 | Body.get()); |
1921 | } |
1922 | |
1923 | |
1924 | |
1925 | |
1926 | |
1927 | |
1928 | |
1929 | |
1930 | StmtResult Parser::ParseGotoStatement() { |
1931 | (0) . __assert_fail ("Tok.is(tok..kw_goto) && \"Not a goto stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1931, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_goto) && "Not a goto stmt!"); |
1932 | SourceLocation GotoLoc = ConsumeToken(); |
1933 | |
1934 | StmtResult Res; |
1935 | if (Tok.is(tok::identifier)) { |
1936 | LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(), |
1937 | Tok.getLocation()); |
1938 | Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD); |
1939 | ConsumeToken(); |
1940 | } else if (Tok.is(tok::star)) { |
1941 | |
1942 | Diag(Tok, diag::ext_gnu_indirect_goto); |
1943 | SourceLocation StarLoc = ConsumeToken(); |
1944 | ExprResult R(ParseExpression()); |
1945 | if (R.isInvalid()) { |
1946 | SkipUntil(tok::semi, StopBeforeMatch); |
1947 | return StmtError(); |
1948 | } |
1949 | Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get()); |
1950 | } else { |
1951 | Diag(Tok, diag::err_expected) << tok::identifier; |
1952 | return StmtError(); |
1953 | } |
1954 | |
1955 | return Res; |
1956 | } |
1957 | |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | |
1963 | |
1964 | StmtResult Parser::ParseContinueStatement() { |
1965 | SourceLocation ContinueLoc = ConsumeToken(); |
1966 | return Actions.ActOnContinueStmt(ContinueLoc, getCurScope()); |
1967 | } |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | StmtResult Parser::ParseBreakStatement() { |
1976 | SourceLocation BreakLoc = ConsumeToken(); |
1977 | return Actions.ActOnBreakStmt(BreakLoc, getCurScope()); |
1978 | } |
1979 | |
1980 | |
1981 | |
1982 | |
1983 | |
1984 | |
1985 | |
1986 | StmtResult Parser::ParseReturnStatement() { |
1987 | (0) . __assert_fail ("(Tok.is(tok..kw_return) || Tok.is(tok..kw_co_return)) && \"Not a return stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1988, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) && |
1988 | (0) . __assert_fail ("(Tok.is(tok..kw_return) || Tok.is(tok..kw_co_return)) && \"Not a return stmt!\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 1988, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Not a return stmt!"); |
1989 | bool IsCoreturn = Tok.is(tok::kw_co_return); |
1990 | SourceLocation ReturnLoc = ConsumeToken(); |
1991 | |
1992 | ExprResult R; |
1993 | if (Tok.isNot(tok::semi)) { |
1994 | if (!IsCoreturn) |
1995 | PreferredType.enterReturn(Actions, Tok.getLocation()); |
1996 | |
1997 | if (Tok.is(tok::code_completion) && !IsCoreturn) { |
1998 | Actions.CodeCompleteExpression(getCurScope(), |
1999 | PreferredType.get(Tok.getLocation())); |
2000 | cutOffParsing(); |
2001 | return StmtError(); |
2002 | } |
2003 | |
2004 | if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) { |
2005 | R = ParseInitializer(); |
2006 | if (R.isUsable()) |
2007 | Diag(R.get()->getBeginLoc(), |
2008 | getLangOpts().CPlusPlus11 |
2009 | ? diag::warn_cxx98_compat_generalized_initializer_lists |
2010 | : diag::ext_generalized_initializer_lists) |
2011 | << R.get()->getSourceRange(); |
2012 | } else |
2013 | R = ParseExpression(); |
2014 | if (R.isInvalid()) { |
2015 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
2016 | return StmtError(); |
2017 | } |
2018 | } |
2019 | if (IsCoreturn) |
2020 | return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get()); |
2021 | return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope()); |
2022 | } |
2023 | |
2024 | StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, |
2025 | ParsedStmtContext StmtCtx, |
2026 | SourceLocation *TrailingElseLoc, |
2027 | ParsedAttributesWithRange &Attrs) { |
2028 | |
2029 | ParsedAttributesWithRange TempAttrs(AttrFactory); |
2030 | |
2031 | |
2032 | while (Tok.is(tok::annot_pragma_loop_hint)) { |
2033 | LoopHint Hint; |
2034 | if (!HandlePragmaLoopHint(Hint)) |
2035 | continue; |
2036 | |
2037 | ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc, |
2038 | ArgsUnion(Hint.ValueExpr)}; |
2039 | TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr, |
2040 | Hint.PragmaNameLoc->Loc, ArgHints, 4, |
2041 | ParsedAttr::AS_Pragma); |
2042 | } |
2043 | |
2044 | |
2045 | MaybeParseCXX11Attributes(Attrs); |
2046 | |
2047 | StmtResult S = ParseStatementOrDeclarationAfterAttributes( |
2048 | Stmts, StmtCtx, TrailingElseLoc, Attrs); |
2049 | |
2050 | Attrs.takeAllFrom(TempAttrs); |
2051 | return S; |
2052 | } |
2053 | |
2054 | Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) { |
2055 | assert(Tok.is(tok::l_brace)); |
2056 | SourceLocation LBraceLoc = Tok.getLocation(); |
2057 | |
2058 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc, |
2059 | "parsing function body"); |
2060 | |
2061 | |
2062 | bool IsCXXMethod = |
2063 | getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl); |
2064 | Sema::PragmaStackSentinelRAII |
2065 | PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod); |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | StmtResult FnBody(ParseCompoundStatementBody()); |
2071 | |
2072 | |
2073 | if (FnBody.isInvalid()) { |
2074 | Sema::CompoundScopeRAII CompoundScope(Actions); |
2075 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
2076 | } |
2077 | |
2078 | BodyScope.Exit(); |
2079 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.get()); |
2080 | } |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | |
2087 | Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) { |
2088 | (0) . __assert_fail ("Tok.is(tok..kw_try) && \"Expected 'try'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 2088, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
2089 | SourceLocation TryLoc = ConsumeToken(); |
2090 | |
2091 | PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc, |
2092 | "parsing function try block"); |
2093 | |
2094 | |
2095 | if (Tok.is(tok::colon)) |
2096 | ParseConstructorInitializer(Decl); |
2097 | else |
2098 | Actions.ActOnDefaultCtorInitializers(Decl); |
2099 | |
2100 | |
2101 | bool IsCXXMethod = |
2102 | getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl); |
2103 | Sema::PragmaStackSentinelRAII |
2104 | PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod); |
2105 | |
2106 | SourceLocation LBraceLoc = Tok.getLocation(); |
2107 | StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, )); |
2108 | |
2109 | |
2110 | if (FnBody.isInvalid()) { |
2111 | Sema::CompoundScopeRAII CompoundScope(Actions); |
2112 | FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false); |
2113 | } |
2114 | |
2115 | BodyScope.Exit(); |
2116 | return Actions.ActOnFinishFunctionBody(Decl, FnBody.get()); |
2117 | } |
2118 | |
2119 | bool Parser::trySkippingFunctionBody() { |
2120 | (0) . __assert_fail ("SkipFunctionBodies && \"Should only be called when SkipFunctionBodies is enabled\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 2121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SkipFunctionBodies && |
2121 | (0) . __assert_fail ("SkipFunctionBodies && \"Should only be called when SkipFunctionBodies is enabled\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 2121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should only be called when SkipFunctionBodies is enabled"); |
2122 | if (!PP.isCodeCompletionEnabled()) { |
2123 | SkipFunctionBody(); |
2124 | return true; |
2125 | } |
2126 | |
2127 | |
2128 | |
2129 | TentativeParsingAction PA(*this); |
2130 | bool IsTryCatch = Tok.is(tok::kw_try); |
2131 | CachedTokens Toks; |
2132 | bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks); |
2133 | if (llvm::any_of(Toks, [](const Token &Tok) { |
2134 | return Tok.is(tok::code_completion); |
2135 | })) { |
2136 | PA.Revert(); |
2137 | return false; |
2138 | } |
2139 | if (ErrorInPrologue) { |
2140 | PA.Commit(); |
2141 | SkipMalformedDecl(); |
2142 | return true; |
2143 | } |
2144 | if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) { |
2145 | PA.Revert(); |
2146 | return false; |
2147 | } |
2148 | while (IsTryCatch && Tok.is(tok::kw_catch)) { |
2149 | if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) || |
2150 | !SkipUntil(tok::r_brace, StopAtCodeCompletion)) { |
2151 | PA.Revert(); |
2152 | return false; |
2153 | } |
2154 | } |
2155 | PA.Commit(); |
2156 | return true; |
2157 | } |
2158 | |
2159 | |
2160 | |
2161 | |
2162 | |
2163 | |
2164 | StmtResult Parser::ParseCXXTryBlock() { |
2165 | (0) . __assert_fail ("Tok.is(tok..kw_try) && \"Expected 'try'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 2165, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_try) && "Expected 'try'"); |
2166 | |
2167 | SourceLocation TryLoc = ConsumeToken(); |
2168 | return ParseCXXTryBlockCommon(TryLoc); |
2169 | } |
2170 | |
2171 | |
2172 | |
2173 | |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | |
2184 | |
2185 | |
2186 | |
2187 | StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) { |
2188 | if (Tok.isNot(tok::l_brace)) |
2189 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
2190 | |
2191 | StmtResult TryBlock(ParseCompoundStatement( |
2192 | , Scope::DeclScope | Scope::TryScope | |
2193 | Scope::CompoundStmtScope | |
2194 | (FnTry ? Scope::FnTryCatchScope : 0))); |
2195 | if (TryBlock.isInvalid()) |
2196 | return TryBlock; |
2197 | |
2198 | |
2199 | |
2200 | if ((Tok.is(tok::identifier) && |
2201 | Tok.getIdentifierInfo() == getSEHExceptKeyword()) || |
2202 | Tok.is(tok::kw___finally)) { |
2203 | |
2204 | StmtResult Handler; |
2205 | if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) { |
2206 | SourceLocation Loc = ConsumeToken(); |
2207 | Handler = ParseSEHExceptBlock(Loc); |
2208 | } |
2209 | else { |
2210 | SourceLocation Loc = ConsumeToken(); |
2211 | Handler = ParseSEHFinallyBlock(Loc); |
2212 | } |
2213 | if(Handler.isInvalid()) |
2214 | return Handler; |
2215 | |
2216 | return Actions.ActOnSEHTryBlock(true , |
2217 | TryLoc, |
2218 | TryBlock.get(), |
2219 | Handler.get()); |
2220 | } |
2221 | else { |
2222 | StmtVector Handlers; |
2223 | |
2224 | |
2225 | |
2226 | DiagnoseAndSkipCXX11Attributes(); |
2227 | |
2228 | if (Tok.isNot(tok::kw_catch)) |
2229 | return StmtError(Diag(Tok, diag::err_expected_catch)); |
2230 | while (Tok.is(tok::kw_catch)) { |
2231 | StmtResult Handler(ParseCXXCatchBlock(FnTry)); |
2232 | if (!Handler.isInvalid()) |
2233 | Handlers.push_back(Handler.get()); |
2234 | } |
2235 | |
2236 | |
2237 | if (Handlers.empty()) |
2238 | return StmtError(); |
2239 | |
2240 | return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers); |
2241 | } |
2242 | } |
2243 | |
2244 | |
2245 | |
2246 | |
2247 | |
2248 | |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) { |
2255 | (0) . __assert_fail ("Tok.is(tok..kw_catch) && \"Expected 'catch'\"", "/home/seafit/code_projects/clang_source/clang/lib/Parse/ParseStmt.cpp", 2255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tok.is(tok::kw_catch) && "Expected 'catch'"); |
2256 | |
2257 | SourceLocation CatchLoc = ConsumeToken(); |
2258 | |
2259 | BalancedDelimiterTracker T(*this, tok::l_paren); |
2260 | if (T.expectAndConsume()) |
2261 | return StmtError(); |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope | |
2267 | Scope::CatchScope | |
2268 | (FnCatch ? Scope::FnTryCatchScope : 0)); |
2269 | |
2270 | |
2271 | |
2272 | Decl *ExceptionDecl = nullptr; |
2273 | if (Tok.isNot(tok::ellipsis)) { |
2274 | ParsedAttributesWithRange Attributes(AttrFactory); |
2275 | MaybeParseCXX11Attributes(Attributes); |
2276 | |
2277 | DeclSpec DS(AttrFactory); |
2278 | DS.takeAttributesFrom(Attributes); |
2279 | |
2280 | if (ParseCXXTypeSpecifierSeq(DS)) |
2281 | return StmtError(); |
2282 | |
2283 | Declarator ExDecl(DS, DeclaratorContext::CXXCatchContext); |
2284 | ParseDeclarator(ExDecl); |
2285 | ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl); |
2286 | } else |
2287 | ConsumeToken(); |
2288 | |
2289 | T.consumeClose(); |
2290 | if (T.getCloseLocation().isInvalid()) |
2291 | return StmtError(); |
2292 | |
2293 | if (Tok.isNot(tok::l_brace)) |
2294 | return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); |
2295 | |
2296 | |
2297 | StmtResult Block(ParseCompoundStatement()); |
2298 | if (Block.isInvalid()) |
2299 | return Block; |
2300 | |
2301 | return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get()); |
2302 | } |
2303 | |
2304 | void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) { |
2305 | IfExistsCondition Result; |
2306 | if (ParseMicrosoftIfExistsCondition(Result)) |
2307 | return; |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | |
2313 | if (Result.Behavior == IEB_Dependent) { |
2314 | if (!Tok.is(tok::l_brace)) { |
2315 | Diag(Tok, diag::err_expected) << tok::l_brace; |
2316 | return; |
2317 | } |
2318 | |
2319 | StmtResult Compound = ParseCompoundStatement(); |
2320 | if (Compound.isInvalid()) |
2321 | return; |
2322 | |
2323 | StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc, |
2324 | Result.IsIfExists, |
2325 | Result.SS, |
2326 | Result.Name, |
2327 | Compound.get()); |
2328 | if (DepResult.isUsable()) |
2329 | Stmts.push_back(DepResult.get()); |
2330 | return; |
2331 | } |
2332 | |
2333 | BalancedDelimiterTracker Braces(*this, tok::l_brace); |
2334 | if (Braces.consumeOpen()) { |
2335 | Diag(Tok, diag::err_expected) << tok::l_brace; |
2336 | return; |
2337 | } |
2338 | |
2339 | switch (Result.Behavior) { |
2340 | case IEB_Parse: |
2341 | |
2342 | break; |
2343 | |
2344 | case IEB_Dependent: |
2345 | llvm_unreachable("Dependent case handled above"); |
2346 | |
2347 | case IEB_Skip: |
2348 | Braces.skipToEnd(); |
2349 | return; |
2350 | } |
2351 | |
2352 | |
2353 | while (Tok.isNot(tok::r_brace)) { |
2354 | StmtResult R = |
2355 | ParseStatementOrDeclaration(Stmts, ParsedStmtContext::Compound); |
2356 | if (R.isUsable()) |
2357 | Stmts.push_back(R.get()); |
2358 | } |
2359 | Braces.consumeClose(); |
2360 | } |
2361 | |
2362 | bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) { |
2363 | MaybeParseGNUAttributes(Attrs); |
2364 | |
2365 | if (Attrs.empty()) |
2366 | return true; |
2367 | |
2368 | if (Attrs.begin()->getKind() != ParsedAttr::AT_OpenCLUnrollHint) |
2369 | return true; |
2370 | |
2371 | if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) { |
2372 | Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop); |
2373 | return false; |
2374 | } |
2375 | return true; |
2376 | } |
2377 | |