clang 20.0.0git
ParseStmt.cpp
Go to the documentation of this file.
1//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
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 the Statement and Block portions of the Parser
10// interface.
11//
12//===----------------------------------------------------------------------===//
13
20#include "clang/Parse/Parser.h"
22#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/Scope.h"
26#include "clang/Sema/SemaObjC.h"
29#include "llvm/ADT/STLExtras.h"
30#include <optional>
31
32using namespace clang;
33
34//===----------------------------------------------------------------------===//
35// C99 6.8: Statements and Blocks.
36//===----------------------------------------------------------------------===//
37
38/// Parse a standalone statement (for instance, as the body of an 'if',
39/// 'while', or 'for').
40StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
41 ParsedStmtContext StmtCtx) {
42 StmtResult Res;
43
44 // We may get back a null statement if we found a #pragma. Keep going until
45 // we get an actual statement.
46 StmtVector Stmts;
47 do {
48 Res = ParseStatementOrDeclaration(Stmts, StmtCtx, TrailingElseLoc);
49 } while (!Res.isInvalid() && !Res.get());
50
51 return Res;
52}
53
54/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
55/// StatementOrDeclaration:
56/// statement
57/// declaration
58///
59/// statement:
60/// labeled-statement
61/// compound-statement
62/// expression-statement
63/// selection-statement
64/// iteration-statement
65/// jump-statement
66/// [C++] declaration-statement
67/// [C++] try-block
68/// [MS] seh-try-block
69/// [OBC] objc-throw-statement
70/// [OBC] objc-try-catch-statement
71/// [OBC] objc-synchronized-statement
72/// [GNU] asm-statement
73/// [OMP] openmp-construct [TODO]
74///
75/// labeled-statement:
76/// identifier ':' statement
77/// 'case' constant-expression ':' statement
78/// 'default' ':' statement
79///
80/// selection-statement:
81/// if-statement
82/// switch-statement
83///
84/// iteration-statement:
85/// while-statement
86/// do-statement
87/// for-statement
88///
89/// expression-statement:
90/// expression[opt] ';'
91///
92/// jump-statement:
93/// 'goto' identifier ';'
94/// 'continue' ';'
95/// 'break' ';'
96/// 'return' expression[opt] ';'
97/// [GNU] 'goto' '*' expression ';'
98///
99/// [OBC] objc-throw-statement:
100/// [OBC] '@' 'throw' expression ';'
101/// [OBC] '@' 'throw' ';'
102///
104Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
105 ParsedStmtContext StmtCtx,
106 SourceLocation *TrailingElseLoc) {
107
108 ParenBraceBracketBalancer BalancerRAIIObj(*this);
109
110 // Because we're parsing either a statement or a declaration, the order of
111 // attribute parsing is important. [[]] attributes at the start of a
112 // statement are different from [[]] attributes that follow an __attribute__
113 // at the start of the statement. Thus, we're not using MaybeParseAttributes
114 // here because we don't want to allow arbitrary orderings.
115 ParsedAttributes CXX11Attrs(AttrFactory);
116 MaybeParseCXX11Attributes(CXX11Attrs, /*MightBeObjCMessageSend*/ true);
117 ParsedAttributes GNUOrMSAttrs(AttrFactory);
118 if (getLangOpts().OpenCL)
119 MaybeParseGNUAttributes(GNUOrMSAttrs);
120
121 if (getLangOpts().HLSL)
122 MaybeParseMicrosoftAttributes(GNUOrMSAttrs);
123
124 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
125 Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs, GNUOrMSAttrs);
126 MaybeDestroyTemplateIds();
127
128 // Attributes that are left should all go on the statement, so concatenate the
129 // two lists.
130 ParsedAttributes Attrs(AttrFactory);
131 takeAndConcatenateAttrs(CXX11Attrs, GNUOrMSAttrs, Attrs);
132
133 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
134 "attributes on empty statement");
135
136 if (Attrs.empty() || Res.isInvalid())
137 return Res;
138
139 return Actions.ActOnAttributedStmt(Attrs, Res.get());
140}
141
142namespace {
143class StatementFilterCCC final : public CorrectionCandidateCallback {
144public:
145 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
146 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
147 tok::identifier, tok::star, tok::amp);
148 WantExpressionKeywords =
149 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
150 WantRemainingKeywords =
151 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
152 WantCXXNamedCasts = false;
153 }
154
155 bool ValidateCandidate(const TypoCorrection &candidate) override {
156 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
157 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
158 if (NextToken.is(tok::equal))
159 return candidate.getCorrectionDeclAs<VarDecl>();
160 if (NextToken.is(tok::period) &&
162 return false;
164 }
165
166 std::unique_ptr<CorrectionCandidateCallback> clone() override {
167 return std::make_unique<StatementFilterCCC>(*this);
168 }
169
170private:
171 Token NextToken;
172};
173}
174
175StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
176 StmtVector &Stmts, ParsedStmtContext StmtCtx,
177 SourceLocation *TrailingElseLoc, ParsedAttributes &CXX11Attrs,
178 ParsedAttributes &GNUAttrs) {
179 const char *SemiError = nullptr;
180 StmtResult Res;
181 SourceLocation GNUAttributeLoc;
182
183 // Cases in this switch statement should fall through if the parser expects
184 // the token to end in a semicolon (in which case SemiError should be set),
185 // or they directly 'return;' if not.
186Retry:
188 SourceLocation AtLoc;
189 switch (Kind) {
190 case tok::at: // May be a @try or @throw statement
191 {
192 AtLoc = ConsumeToken(); // consume @
193 return ParseObjCAtStatement(AtLoc, StmtCtx);
194 }
195
196 case tok::code_completion:
197 cutOffParsing();
200 return StmtError();
201
202 case tok::identifier:
203 ParseIdentifier: {
204 Token Next = NextToken();
205 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
206 // Both C++11 and GNU attributes preceding the label appertain to the
207 // label, so put them in a single list to pass on to
208 // ParseLabeledStatement().
209 ParsedAttributes Attrs(AttrFactory);
210 takeAndConcatenateAttrs(CXX11Attrs, GNUAttrs, Attrs);
211
212 // identifier ':' statement
213 return ParseLabeledStatement(Attrs, StmtCtx);
214 }
215
216 // Look up the identifier, and typo-correct it to a keyword if it's not
217 // found.
218 if (Next.isNot(tok::coloncolon)) {
219 // Try to limit which sets of keywords should be included in typo
220 // correction based on what the next token is.
221 StatementFilterCCC CCC(Next);
222 if (TryAnnotateName(&CCC) == ANK_Error) {
223 // Handle errors here by skipping up to the next semicolon or '}', and
224 // eat the semicolon if that's what stopped us.
225 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
226 if (Tok.is(tok::semi))
227 ConsumeToken();
228 return StmtError();
229 }
230
231 // If the identifier was typo-corrected, try again.
232 if (Tok.isNot(tok::identifier))
233 goto Retry;
234 }
235
236 // Fall through
237 [[fallthrough]];
238 }
239
240 default: {
241 bool HaveAttrs = !CXX11Attrs.empty() || !GNUAttrs.empty();
242 auto IsStmtAttr = [](ParsedAttr &Attr) { return Attr.isStmtAttr(); };
243 bool AllAttrsAreStmtAttrs = llvm::all_of(CXX11Attrs, IsStmtAttr) &&
244 llvm::all_of(GNUAttrs, IsStmtAttr);
245 // In C, the grammar production for statement (C23 6.8.1p1) does not allow
246 // for declarations, which is different from C++ (C++23 [stmt.pre]p1). So
247 // in C++, we always allow a declaration, but in C we need to check whether
248 // we're in a statement context that allows declarations. e.g., in C, the
249 // following is invalid: if (1) int x;
250 if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt ||
251 (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) !=
252 ParsedStmtContext()) &&
253 ((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) ||
254 isDeclarationStatement())) {
255 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
257 if (GNUAttributeLoc.isValid()) {
258 DeclStart = GNUAttributeLoc;
259 Decl = ParseDeclaration(DeclaratorContext::Block, DeclEnd, CXX11Attrs,
260 GNUAttrs, &GNUAttributeLoc);
261 } else {
262 Decl = ParseDeclaration(DeclaratorContext::Block, DeclEnd, CXX11Attrs,
263 GNUAttrs);
264 }
265 if (CXX11Attrs.Range.getBegin().isValid()) {
266 // The caller must guarantee that the CXX11Attrs appear before the
267 // GNUAttrs, and we rely on that here.
268 assert(GNUAttrs.Range.getBegin().isInvalid() ||
269 GNUAttrs.Range.getBegin() > CXX11Attrs.Range.getBegin());
270 DeclStart = CXX11Attrs.Range.getBegin();
271 } else if (GNUAttrs.Range.getBegin().isValid())
272 DeclStart = GNUAttrs.Range.getBegin();
273 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
274 }
275
276 if (Tok.is(tok::r_brace)) {
277 Diag(Tok, diag::err_expected_statement);
278 return StmtError();
279 }
280
281 switch (Tok.getKind()) {
282#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
283#include "clang/Basic/TransformTypeTraits.def"
284 if (NextToken().is(tok::less)) {
285 Tok.setKind(tok::identifier);
286 Diag(Tok, diag::ext_keyword_as_ident)
287 << Tok.getIdentifierInfo()->getName() << 0;
288 goto ParseIdentifier;
289 }
290 [[fallthrough]];
291 default:
292 return ParseExprStatement(StmtCtx);
293 }
294 }
295
296 case tok::kw___attribute: {
297 GNUAttributeLoc = Tok.getLocation();
298 ParseGNUAttributes(GNUAttrs);
299 goto Retry;
300 }
301
302 case tok::kw_template: {
303 SourceLocation DeclEnd;
304 ParsedAttributes Attrs(AttrFactory);
305 ParseTemplateDeclarationOrSpecialization(DeclaratorContext::Block, DeclEnd,
306 Attrs,
307 getAccessSpecifierIfPresent());
308 return StmtError();
309 }
310
311 case tok::kw_case: // C99 6.8.1: labeled-statement
312 return ParseCaseStatement(StmtCtx);
313 case tok::kw_default: // C99 6.8.1: labeled-statement
314 return ParseDefaultStatement(StmtCtx);
315
316 case tok::l_brace: // C99 6.8.2: compound-statement
317 return ParseCompoundStatement();
318 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
319 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
320 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
321 }
322
323 case tok::kw_if: // C99 6.8.4.1: if-statement
324 return ParseIfStatement(TrailingElseLoc);
325 case tok::kw_switch: // C99 6.8.4.2: switch-statement
326 return ParseSwitchStatement(TrailingElseLoc);
327
328 case tok::kw_while: // C99 6.8.5.1: while-statement
329 return ParseWhileStatement(TrailingElseLoc);
330 case tok::kw_do: // C99 6.8.5.2: do-statement
331 Res = ParseDoStatement();
332 SemiError = "do/while";
333 break;
334 case tok::kw_for: // C99 6.8.5.3: for-statement
335 return ParseForStatement(TrailingElseLoc);
336
337 case tok::kw_goto: // C99 6.8.6.1: goto-statement
338 Res = ParseGotoStatement();
339 SemiError = "goto";
340 break;
341 case tok::kw_continue: // C99 6.8.6.2: continue-statement
342 Res = ParseContinueStatement();
343 SemiError = "continue";
344 break;
345 case tok::kw_break: // C99 6.8.6.3: break-statement
346 Res = ParseBreakStatement();
347 SemiError = "break";
348 break;
349 case tok::kw_return: // C99 6.8.6.4: return-statement
350 Res = ParseReturnStatement();
351 SemiError = "return";
352 break;
353 case tok::kw_co_return: // C++ Coroutines: co_return statement
354 Res = ParseReturnStatement();
355 SemiError = "co_return";
356 break;
357
358 case tok::kw_asm: {
359 for (const ParsedAttr &AL : CXX11Attrs)
360 // Could be relaxed if asm-related regular keyword attributes are
361 // added later.
362 (AL.isRegularKeywordAttribute()
363 ? Diag(AL.getRange().getBegin(), diag::err_keyword_not_allowed)
364 : Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored))
365 << AL;
366 // Prevent these from being interpreted as statement attributes later on.
367 CXX11Attrs.clear();
368 ProhibitAttributes(GNUAttrs);
369 bool msAsm = false;
370 Res = ParseAsmStatement(msAsm);
371 if (msAsm) return Res;
372 SemiError = "asm";
373 break;
374 }
375
376 case tok::kw___if_exists:
377 case tok::kw___if_not_exists:
378 ProhibitAttributes(CXX11Attrs);
379 ProhibitAttributes(GNUAttrs);
380 ParseMicrosoftIfExistsStatement(Stmts);
381 // An __if_exists block is like a compound statement, but it doesn't create
382 // a new scope.
383 return StmtEmpty();
384
385 case tok::kw_try: // C++ 15: try-block
386 return ParseCXXTryBlock();
387
388 case tok::kw___try:
389 ProhibitAttributes(CXX11Attrs);
390 ProhibitAttributes(GNUAttrs);
391 return ParseSEHTryBlock();
392
393 case tok::kw___leave:
394 Res = ParseSEHLeaveStatement();
395 SemiError = "__leave";
396 break;
397
398 case tok::annot_pragma_vis:
399 ProhibitAttributes(CXX11Attrs);
400 ProhibitAttributes(GNUAttrs);
401 HandlePragmaVisibility();
402 return StmtEmpty();
403
404 case tok::annot_pragma_pack:
405 ProhibitAttributes(CXX11Attrs);
406 ProhibitAttributes(GNUAttrs);
407 HandlePragmaPack();
408 return StmtEmpty();
409
410 case tok::annot_pragma_msstruct:
411 ProhibitAttributes(CXX11Attrs);
412 ProhibitAttributes(GNUAttrs);
413 HandlePragmaMSStruct();
414 return StmtEmpty();
415
416 case tok::annot_pragma_align:
417 ProhibitAttributes(CXX11Attrs);
418 ProhibitAttributes(GNUAttrs);
419 HandlePragmaAlign();
420 return StmtEmpty();
421
422 case tok::annot_pragma_weak:
423 ProhibitAttributes(CXX11Attrs);
424 ProhibitAttributes(GNUAttrs);
425 HandlePragmaWeak();
426 return StmtEmpty();
427
428 case tok::annot_pragma_weakalias:
429 ProhibitAttributes(CXX11Attrs);
430 ProhibitAttributes(GNUAttrs);
431 HandlePragmaWeakAlias();
432 return StmtEmpty();
433
434 case tok::annot_pragma_redefine_extname:
435 ProhibitAttributes(CXX11Attrs);
436 ProhibitAttributes(GNUAttrs);
437 HandlePragmaRedefineExtname();
438 return StmtEmpty();
439
440 case tok::annot_pragma_fp_contract:
441 ProhibitAttributes(CXX11Attrs);
442 ProhibitAttributes(GNUAttrs);
443 Diag(Tok, diag::err_pragma_file_or_compound_scope) << "fp_contract";
444 ConsumeAnnotationToken();
445 return StmtError();
446
447 case tok::annot_pragma_fp:
448 ProhibitAttributes(CXX11Attrs);
449 ProhibitAttributes(GNUAttrs);
450 Diag(Tok, diag::err_pragma_file_or_compound_scope) << "clang fp";
451 ConsumeAnnotationToken();
452 return StmtError();
453
454 case tok::annot_pragma_fenv_access:
455 case tok::annot_pragma_fenv_access_ms:
456 ProhibitAttributes(CXX11Attrs);
457 ProhibitAttributes(GNUAttrs);
458 Diag(Tok, diag::err_pragma_file_or_compound_scope)
459 << (Kind == tok::annot_pragma_fenv_access ? "STDC FENV_ACCESS"
460 : "fenv_access");
461 ConsumeAnnotationToken();
462 return StmtEmpty();
463
464 case tok::annot_pragma_fenv_round:
465 ProhibitAttributes(CXX11Attrs);
466 ProhibitAttributes(GNUAttrs);
467 Diag(Tok, diag::err_pragma_file_or_compound_scope) << "STDC FENV_ROUND";
468 ConsumeAnnotationToken();
469 return StmtError();
470
471 case tok::annot_pragma_cx_limited_range:
472 ProhibitAttributes(CXX11Attrs);
473 ProhibitAttributes(GNUAttrs);
474 Diag(Tok, diag::err_pragma_file_or_compound_scope)
475 << "STDC CX_LIMITED_RANGE";
476 ConsumeAnnotationToken();
477 return StmtError();
478
479 case tok::annot_pragma_float_control:
480 ProhibitAttributes(CXX11Attrs);
481 ProhibitAttributes(GNUAttrs);
482 Diag(Tok, diag::err_pragma_file_or_compound_scope) << "float_control";
483 ConsumeAnnotationToken();
484 return StmtError();
485
486 case tok::annot_pragma_opencl_extension:
487 ProhibitAttributes(CXX11Attrs);
488 ProhibitAttributes(GNUAttrs);
489 HandlePragmaOpenCLExtension();
490 return StmtEmpty();
491
492 case tok::annot_pragma_captured:
493 ProhibitAttributes(CXX11Attrs);
494 ProhibitAttributes(GNUAttrs);
495 return HandlePragmaCaptured();
496
497 case tok::annot_pragma_openmp:
498 // Prohibit attributes that are not OpenMP attributes, but only before
499 // processing a #pragma omp clause.
500 ProhibitAttributes(CXX11Attrs);
501 ProhibitAttributes(GNUAttrs);
502 [[fallthrough]];
503 case tok::annot_attr_openmp:
504 // Do not prohibit attributes if they were OpenMP attributes.
505 return ParseOpenMPDeclarativeOrExecutableDirective(StmtCtx);
506
507 case tok::annot_pragma_openacc:
509
510 case tok::annot_pragma_ms_pointers_to_members:
511 ProhibitAttributes(CXX11Attrs);
512 ProhibitAttributes(GNUAttrs);
513 HandlePragmaMSPointersToMembers();
514 return StmtEmpty();
515
516 case tok::annot_pragma_ms_pragma:
517 ProhibitAttributes(CXX11Attrs);
518 ProhibitAttributes(GNUAttrs);
519 HandlePragmaMSPragma();
520 return StmtEmpty();
521
522 case tok::annot_pragma_ms_vtordisp:
523 ProhibitAttributes(CXX11Attrs);
524 ProhibitAttributes(GNUAttrs);
525 HandlePragmaMSVtorDisp();
526 return StmtEmpty();
527
528 case tok::annot_pragma_loop_hint:
529 ProhibitAttributes(CXX11Attrs);
530 ProhibitAttributes(GNUAttrs);
531 return ParsePragmaLoopHint(Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs);
532
533 case tok::annot_pragma_dump:
534 HandlePragmaDump();
535 return StmtEmpty();
536
537 case tok::annot_pragma_attribute:
538 HandlePragmaAttribute();
539 return StmtEmpty();
540 }
541
542 // If we reached this code, the statement must end in a semicolon.
543 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
544 // If the result was valid, then we do want to diagnose this. Use
545 // ExpectAndConsume to emit the diagnostic, even though we know it won't
546 // succeed.
547 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
548 // Skip until we see a } or ;, but don't eat it.
549 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
550 }
551
552 return Res;
553}
554
555/// Parse an expression statement.
556StmtResult Parser::ParseExprStatement(ParsedStmtContext StmtCtx) {
557 // If a case keyword is missing, this is where it should be inserted.
558 Token OldToken = Tok;
559
560 ExprStatementTokLoc = Tok.getLocation();
561
562 // expression[opt] ';'
564 if (Expr.isInvalid()) {
565 // If the expression is invalid, skip ahead to the next semicolon or '}'.
566 // Not doing this opens us up to the possibility of infinite loops if
567 // ParseExpression does not consume any tokens.
568 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
569 if (Tok.is(tok::semi))
570 ConsumeToken();
571 return Actions.ActOnExprStmtError();
572 }
573
574 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
575 Actions.CheckCaseExpression(Expr.get())) {
576 // If a constant expression is followed by a colon inside a switch block,
577 // suggest a missing case keyword.
578 Diag(OldToken, diag::err_expected_case_before_expression)
579 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
580
581 // Recover parsing as a case statement.
582 return ParseCaseStatement(StmtCtx, /*MissingCase=*/true, Expr);
583 }
584
585 Token *CurTok = nullptr;
586 // Note we shouldn't eat the token since the callback needs it.
587 if (Tok.is(tok::annot_repl_input_end))
588 CurTok = &Tok;
589 else
590 // Otherwise, eat the semicolon.
591 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
592
593 StmtResult R = handleExprStmt(Expr, StmtCtx);
594 if (CurTok && !R.isInvalid())
595 CurTok->setAnnotationValue(R.get());
596
597 return R;
598}
599
600/// ParseSEHTryBlockCommon
601///
602/// seh-try-block:
603/// '__try' compound-statement seh-handler
604///
605/// seh-handler:
606/// seh-except-block
607/// seh-finally-block
608///
609StmtResult Parser::ParseSEHTryBlock() {
610 assert(Tok.is(tok::kw___try) && "Expected '__try'");
611 SourceLocation TryLoc = ConsumeToken();
612
613 if (Tok.isNot(tok::l_brace))
614 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
615
616 StmtResult TryBlock(ParseCompoundStatement(
617 /*isStmtExpr=*/false,
619 if (TryBlock.isInvalid())
620 return TryBlock;
621
622 StmtResult Handler;
623 if (Tok.is(tok::identifier) &&
624 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
626 Handler = ParseSEHExceptBlock(Loc);
627 } else if (Tok.is(tok::kw___finally)) {
629 Handler = ParseSEHFinallyBlock(Loc);
630 } else {
631 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
632 }
633
634 if(Handler.isInvalid())
635 return Handler;
636
637 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
638 TryLoc,
639 TryBlock.get(),
640 Handler.get());
641}
642
643/// ParseSEHExceptBlock - Handle __except
644///
645/// seh-except-block:
646/// '__except' '(' seh-filter-expression ')' compound-statement
647///
648StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
649 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
650 raii2(Ident___exception_code, false),
651 raii3(Ident_GetExceptionCode, false);
652
653 if (ExpectAndConsume(tok::l_paren))
654 return StmtError();
655
656 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
658
659 if (getLangOpts().Borland) {
660 Ident__exception_info->setIsPoisoned(false);
661 Ident___exception_info->setIsPoisoned(false);
662 Ident_GetExceptionInfo->setIsPoisoned(false);
663 }
664
665 ExprResult FilterExpr;
666 {
667 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
669 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
670 }
671
672 if (getLangOpts().Borland) {
673 Ident__exception_info->setIsPoisoned(true);
674 Ident___exception_info->setIsPoisoned(true);
675 Ident_GetExceptionInfo->setIsPoisoned(true);
676 }
677
678 if(FilterExpr.isInvalid())
679 return StmtError();
680
681 if (ExpectAndConsume(tok::r_paren))
682 return StmtError();
683
684 if (Tok.isNot(tok::l_brace))
685 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
686
687 StmtResult Block(ParseCompoundStatement());
688
689 if(Block.isInvalid())
690 return Block;
691
692 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
693}
694
695/// ParseSEHFinallyBlock - Handle __finally
696///
697/// seh-finally-block:
698/// '__finally' compound-statement
699///
700StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
701 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
702 raii2(Ident___abnormal_termination, false),
703 raii3(Ident_AbnormalTermination, false);
704
705 if (Tok.isNot(tok::l_brace))
706 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
707
708 ParseScope FinallyScope(this, 0);
710
711 StmtResult Block(ParseCompoundStatement());
712 if(Block.isInvalid()) {
714 return Block;
715 }
716
717 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
718}
719
720/// Handle __leave
721///
722/// seh-leave-statement:
723/// '__leave' ';'
724///
725StmtResult Parser::ParseSEHLeaveStatement() {
726 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
727 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
728}
729
730static void DiagnoseLabelFollowedByDecl(Parser &P, const Stmt *SubStmt) {
731 // When in C mode (but not Microsoft extensions mode), diagnose use of a
732 // label that is followed by a declaration rather than a statement.
733 if (!P.getLangOpts().CPlusPlus && !P.getLangOpts().MicrosoftExt &&
734 isa<DeclStmt>(SubStmt)) {
735 P.Diag(SubStmt->getBeginLoc(),
736 P.getLangOpts().C23
737 ? diag::warn_c23_compat_label_followed_by_declaration
738 : diag::ext_c_label_followed_by_declaration);
739 }
740}
741
742/// ParseLabeledStatement - We have an identifier and a ':' after it.
743///
744/// label:
745/// identifier ':'
746/// [GNU] identifier ':' attributes[opt]
747///
748/// labeled-statement:
749/// label statement
750///
751StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
752 ParsedStmtContext StmtCtx) {
753 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
754 "Not an identifier!");
755
756 // [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a
757 // substatement in a selection statement, in place of the loop body in an
758 // iteration statement, or in place of the statement that follows a label.
759 StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives;
760
761 Token IdentTok = Tok; // Save the whole token.
762 ConsumeToken(); // eat the identifier.
763
764 assert(Tok.is(tok::colon) && "Not a label!");
765
766 // identifier ':' statement
767 SourceLocation ColonLoc = ConsumeToken();
768
769 // Read label attributes, if present.
770 StmtResult SubStmt;
771 if (Tok.is(tok::kw___attribute)) {
772 ParsedAttributes TempAttrs(AttrFactory);
773 ParseGNUAttributes(TempAttrs);
774
775 // In C++, GNU attributes only apply to the label if they are followed by a
776 // semicolon, to disambiguate label attributes from attributes on a labeled
777 // declaration.
778 //
779 // This doesn't quite match what GCC does; if the attribute list is empty
780 // and followed by a semicolon, GCC will reject (it appears to parse the
781 // attributes as part of a statement in that case). That looks like a bug.
782 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
783 Attrs.takeAllFrom(TempAttrs);
784 else {
785 StmtVector Stmts;
786 ParsedAttributes EmptyCXX11Attrs(AttrFactory);
787 SubStmt = ParseStatementOrDeclarationAfterAttributes(
788 Stmts, StmtCtx, nullptr, EmptyCXX11Attrs, TempAttrs);
789 if (!TempAttrs.empty() && !SubStmt.isInvalid())
790 SubStmt = Actions.ActOnAttributedStmt(TempAttrs, SubStmt.get());
791 }
792 }
793
794 // The label may have no statement following it
795 if (SubStmt.isUnset() && Tok.is(tok::r_brace)) {
796 DiagnoseLabelAtEndOfCompoundStatement();
797 SubStmt = Actions.ActOnNullStmt(ColonLoc);
798 }
799
800 // If we've not parsed a statement yet, parse one now.
801 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
802 SubStmt = ParseStatement(nullptr, StmtCtx);
803
804 // Broken substmt shouldn't prevent the label from being added to the AST.
805 if (SubStmt.isInvalid())
806 SubStmt = Actions.ActOnNullStmt(ColonLoc);
807
808 DiagnoseLabelFollowedByDecl(*this, SubStmt.get());
809
810 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
811 IdentTok.getLocation());
812 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
813 Attrs.clear();
814
815 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
816 SubStmt.get());
817}
818
819/// ParseCaseStatement
820/// labeled-statement:
821/// 'case' constant-expression ':' statement
822/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
823///
824StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
825 bool MissingCase, ExprResult Expr) {
826 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
827
828 // [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a
829 // substatement in a selection statement, in place of the loop body in an
830 // iteration statement, or in place of the statement that follows a label.
831 StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives;
832
833 // It is very common for code to contain many case statements recursively
834 // nested, as in (but usually without indentation):
835 // case 1:
836 // case 2:
837 // case 3:
838 // case 4:
839 // case 5: etc.
840 //
841 // Parsing this naively works, but is both inefficient and can cause us to run
842 // out of stack space in our recursive descent parser. As a special case,
843 // flatten this recursion into an iterative loop. This is complex and gross,
844 // but all the grossness is constrained to ParseCaseStatement (and some
845 // weirdness in the actions), so this is just local grossness :).
846
847 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
848 // example above.
849 StmtResult TopLevelCase(true);
850
851 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
852 // gets updated each time a new case is parsed, and whose body is unset so
853 // far. When parsing 'case 4', this is the 'case 3' node.
854 Stmt *DeepestParsedCaseStmt = nullptr;
855
856 // While we have case statements, eat and stack them.
857 SourceLocation ColonLoc;
858 do {
859 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
860 ConsumeToken(); // eat the 'case'.
861 ColonLoc = SourceLocation();
862
863 if (Tok.is(tok::code_completion)) {
864 cutOffParsing();
866 return StmtError();
867 }
868
869 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
870 /// Disable this form of error recovery while we're parsing the case
871 /// expression.
872 ColonProtectionRAIIObject ColonProtection(*this);
873
874 ExprResult LHS;
875 if (!MissingCase) {
876 LHS = ParseCaseExpression(CaseLoc);
877 if (LHS.isInvalid()) {
878 // If constant-expression is parsed unsuccessfully, recover by skipping
879 // current case statement (moving to the colon that ends it).
880 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
881 return StmtError();
882 }
883 } else {
884 LHS = Expr;
885 MissingCase = false;
886 }
887
888 // GNU case range extension.
889 SourceLocation DotDotDotLoc;
890 ExprResult RHS;
891 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
892 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
893 RHS = ParseCaseExpression(CaseLoc);
894 if (RHS.isInvalid()) {
895 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
896 return StmtError();
897 }
898 }
899
900 ColonProtection.restore();
901
902 if (TryConsumeToken(tok::colon, ColonLoc)) {
903 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
904 TryConsumeToken(tok::coloncolon, ColonLoc)) {
905 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
906 Diag(ColonLoc, diag::err_expected_after)
907 << "'case'" << tok::colon
908 << FixItHint::CreateReplacement(ColonLoc, ":");
909 } else {
910 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
911 Diag(ExpectedLoc, diag::err_expected_after)
912 << "'case'" << tok::colon
913 << FixItHint::CreateInsertion(ExpectedLoc, ":");
914 ColonLoc = ExpectedLoc;
915 }
916
917 StmtResult Case =
918 Actions.ActOnCaseStmt(CaseLoc, LHS, DotDotDotLoc, RHS, ColonLoc);
919
920 // If we had a sema error parsing this case, then just ignore it and
921 // continue parsing the sub-stmt.
922 if (Case.isInvalid()) {
923 if (TopLevelCase.isInvalid()) // No parsed case stmts.
924 return ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
925 // Otherwise, just don't add it as a nested case.
926 } else {
927 // If this is the first case statement we parsed, it becomes TopLevelCase.
928 // Otherwise we link it into the current chain.
929 Stmt *NextDeepest = Case.get();
930 if (TopLevelCase.isInvalid())
931 TopLevelCase = Case;
932 else
933 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
934 DeepestParsedCaseStmt = NextDeepest;
935 }
936
937 // Handle all case statements.
938 } while (Tok.is(tok::kw_case));
939
940 // If we found a non-case statement, start by parsing it.
941 StmtResult SubStmt;
942
943 if (Tok.is(tok::r_brace)) {
944 // "switch (X) { case 4: }", is valid and is treated as if label was
945 // followed by a null statement.
946 DiagnoseLabelAtEndOfCompoundStatement();
947 SubStmt = Actions.ActOnNullStmt(ColonLoc);
948 } else {
949 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
950 }
951
952 // Install the body into the most deeply-nested case.
953 if (DeepestParsedCaseStmt) {
954 // Broken sub-stmt shouldn't prevent forming the case statement properly.
955 if (SubStmt.isInvalid())
956 SubStmt = Actions.ActOnNullStmt(SourceLocation());
957 DiagnoseLabelFollowedByDecl(*this, SubStmt.get());
958 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
959 }
960
961 // Return the top level parsed statement tree.
962 return TopLevelCase;
963}
964
965/// ParseDefaultStatement
966/// labeled-statement:
967/// 'default' ':' statement
968/// Note that this does not parse the 'statement' at the end.
969///
970StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) {
971 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
972
973 // [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a
974 // substatement in a selection statement, in place of the loop body in an
975 // iteration statement, or in place of the statement that follows a label.
976 StmtCtx &= ~ParsedStmtContext::AllowStandaloneOpenMPDirectives;
977
978 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
979
980 SourceLocation ColonLoc;
981 if (TryConsumeToken(tok::colon, ColonLoc)) {
982 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
983 // Treat "default;" as a typo for "default:".
984 Diag(ColonLoc, diag::err_expected_after)
985 << "'default'" << tok::colon
986 << FixItHint::CreateReplacement(ColonLoc, ":");
987 } else {
988 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
989 Diag(ExpectedLoc, diag::err_expected_after)
990 << "'default'" << tok::colon
991 << FixItHint::CreateInsertion(ExpectedLoc, ":");
992 ColonLoc = ExpectedLoc;
993 }
994
995 StmtResult SubStmt;
996
997 if (Tok.is(tok::r_brace)) {
998 // "switch (X) {... default: }", is valid and is treated as if label was
999 // followed by a null statement.
1000 DiagnoseLabelAtEndOfCompoundStatement();
1001 SubStmt = Actions.ActOnNullStmt(ColonLoc);
1002 } else {
1003 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
1004 }
1005
1006 // Broken sub-stmt shouldn't prevent forming the case statement properly.
1007 if (SubStmt.isInvalid())
1008 SubStmt = Actions.ActOnNullStmt(ColonLoc);
1009
1010 DiagnoseLabelFollowedByDecl(*this, SubStmt.get());
1011 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
1012 SubStmt.get(), getCurScope());
1013}
1014
1015StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
1016 return ParseCompoundStatement(isStmtExpr,
1018}
1019
1020/// ParseCompoundStatement - Parse a "{}" block.
1021///
1022/// compound-statement: [C99 6.8.2]
1023/// { block-item-list[opt] }
1024/// [GNU] { label-declarations block-item-list } [TODO]
1025///
1026/// block-item-list:
1027/// block-item
1028/// block-item-list block-item
1029///
1030/// block-item:
1031/// declaration
1032/// [GNU] '__extension__' declaration
1033/// statement
1034///
1035/// [GNU] label-declarations:
1036/// [GNU] label-declaration
1037/// [GNU] label-declarations label-declaration
1038///
1039/// [GNU] label-declaration:
1040/// [GNU] '__label__' identifier-list ';'
1041///
1042StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
1043 unsigned ScopeFlags) {
1044 assert(Tok.is(tok::l_brace) && "Not a compound stmt!");
1045
1046 // Enter a scope to hold everything within the compound stmt. Compound
1047 // statements can always hold declarations.
1048 ParseScope CompoundScope(this, ScopeFlags);
1049
1050 // Parse the statements in the body.
1051 return ParseCompoundStatementBody(isStmtExpr);
1052}
1053
1054/// Parse any pragmas at the start of the compound expression. We handle these
1055/// separately since some pragmas (FP_CONTRACT) must appear before any C
1056/// statement in the compound, but may be intermingled with other pragmas.
1057void Parser::ParseCompoundStatementLeadingPragmas() {
1058 bool checkForPragmas = true;
1059 while (checkForPragmas) {
1060 switch (Tok.getKind()) {
1061 case tok::annot_pragma_vis:
1062 HandlePragmaVisibility();
1063 break;
1064 case tok::annot_pragma_pack:
1065 HandlePragmaPack();
1066 break;
1067 case tok::annot_pragma_msstruct:
1068 HandlePragmaMSStruct();
1069 break;
1070 case tok::annot_pragma_align:
1071 HandlePragmaAlign();
1072 break;
1073 case tok::annot_pragma_weak:
1074 HandlePragmaWeak();
1075 break;
1076 case tok::annot_pragma_weakalias:
1077 HandlePragmaWeakAlias();
1078 break;
1079 case tok::annot_pragma_redefine_extname:
1080 HandlePragmaRedefineExtname();
1081 break;
1082 case tok::annot_pragma_opencl_extension:
1083 HandlePragmaOpenCLExtension();
1084 break;
1085 case tok::annot_pragma_fp_contract:
1086 HandlePragmaFPContract();
1087 break;
1088 case tok::annot_pragma_fp:
1089 HandlePragmaFP();
1090 break;
1091 case tok::annot_pragma_fenv_access:
1092 case tok::annot_pragma_fenv_access_ms:
1093 HandlePragmaFEnvAccess();
1094 break;
1095 case tok::annot_pragma_fenv_round:
1096 HandlePragmaFEnvRound();
1097 break;
1098 case tok::annot_pragma_cx_limited_range:
1099 HandlePragmaCXLimitedRange();
1100 break;
1101 case tok::annot_pragma_float_control:
1102 HandlePragmaFloatControl();
1103 break;
1104 case tok::annot_pragma_ms_pointers_to_members:
1105 HandlePragmaMSPointersToMembers();
1106 break;
1107 case tok::annot_pragma_ms_pragma:
1108 HandlePragmaMSPragma();
1109 break;
1110 case tok::annot_pragma_ms_vtordisp:
1111 HandlePragmaMSVtorDisp();
1112 break;
1113 case tok::annot_pragma_dump:
1114 HandlePragmaDump();
1115 break;
1116 default:
1117 checkForPragmas = false;
1118 break;
1119 }
1120 }
1121
1122}
1123
1124void Parser::DiagnoseLabelAtEndOfCompoundStatement() {
1125 if (getLangOpts().CPlusPlus) {
1127 ? diag::warn_cxx20_compat_label_end_of_compound_statement
1128 : diag::ext_cxx_label_end_of_compound_statement);
1129 } else {
1130 Diag(Tok, getLangOpts().C23
1131 ? diag::warn_c23_compat_label_end_of_compound_statement
1132 : diag::ext_c_label_end_of_compound_statement);
1133 }
1134}
1135
1136/// Consume any extra semi-colons resulting in null statements,
1137/// returning true if any tok::semi were consumed.
1138bool Parser::ConsumeNullStmt(StmtVector &Stmts) {
1139 if (!Tok.is(tok::semi))
1140 return false;
1141
1142 SourceLocation StartLoc = Tok.getLocation();
1143 SourceLocation EndLoc;
1144
1145 while (Tok.is(tok::semi) && !Tok.hasLeadingEmptyMacro() &&
1146 Tok.getLocation().isValid() && !Tok.getLocation().isMacroID()) {
1147 EndLoc = Tok.getLocation();
1148
1149 // Don't just ConsumeToken() this tok::semi, do store it in AST.
1150 StmtResult R =
1151 ParseStatementOrDeclaration(Stmts, ParsedStmtContext::SubStmt);
1152 if (R.isUsable())
1153 Stmts.push_back(R.get());
1154 }
1155
1156 // Did not consume any extra semi.
1157 if (EndLoc.isInvalid())
1158 return false;
1159
1160 Diag(StartLoc, diag::warn_null_statement)
1161 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
1162 return true;
1163}
1164
1165StmtResult Parser::handleExprStmt(ExprResult E, ParsedStmtContext StmtCtx) {
1166 bool IsStmtExprResult = false;
1167 if ((StmtCtx & ParsedStmtContext::InStmtExpr) != ParsedStmtContext()) {
1168 // For GCC compatibility we skip past NullStmts.
1169 unsigned LookAhead = 0;
1170 while (GetLookAheadToken(LookAhead).is(tok::semi)) {
1171 ++LookAhead;
1172 }
1173 // Then look to see if the next two tokens close the statement expression;
1174 // if so, this expression statement is the last statement in a statement
1175 // expression.
1176 IsStmtExprResult = GetLookAheadToken(LookAhead).is(tok::r_brace) &&
1177 GetLookAheadToken(LookAhead + 1).is(tok::r_paren);
1178 }
1179
1180 if (IsStmtExprResult)
1181 E = Actions.ActOnStmtExprResult(E);
1182 return Actions.ActOnExprStmt(E, /*DiscardedValue=*/!IsStmtExprResult);
1183}
1184
1185/// ParseCompoundStatementBody - Parse a sequence of statements optionally
1186/// followed by a label and invoke the ActOnCompoundStmt action. This expects
1187/// the '{' to be the current token, and consume the '}' at the end of the
1188/// block. It does not manipulate the scope stack.
1189StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
1191 Tok.getLocation(),
1192 "in compound statement ('{}')");
1193
1194 // Record the current FPFeatures, restore on leaving the
1195 // compound statement.
1196 Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
1197
1198 InMessageExpressionRAIIObject InMessage(*this, false);
1199 BalancedDelimiterTracker T(*this, tok::l_brace);
1200 if (T.consumeOpen())
1201 return StmtError();
1202
1203 Sema::CompoundScopeRAII CompoundScope(Actions, isStmtExpr);
1204
1205 // Parse any pragmas at the beginning of the compound statement.
1206 ParseCompoundStatementLeadingPragmas();
1208
1209 StmtVector Stmts;
1210
1211 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
1212 // only allowed at the start of a compound stmt regardless of the language.
1213 while (Tok.is(tok::kw___label__)) {
1214 SourceLocation LabelLoc = ConsumeToken();
1215
1216 SmallVector<Decl *, 8> DeclsInGroup;
1217 while (true) {
1218 if (Tok.isNot(tok::identifier)) {
1219 Diag(Tok, diag::err_expected) << tok::identifier;
1220 break;
1221 }
1222
1224 SourceLocation IdLoc = ConsumeToken();
1225 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
1226
1227 if (!TryConsumeToken(tok::comma))
1228 break;
1229 }
1230
1231 DeclSpec DS(AttrFactory);
1232 DeclGroupPtrTy Res =
1233 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
1234 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
1235
1236 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
1237 if (R.isUsable())
1238 Stmts.push_back(R.get());
1239 }
1240
1241 ParsedStmtContext SubStmtCtx =
1242 ParsedStmtContext::Compound |
1243 (isStmtExpr ? ParsedStmtContext::InStmtExpr : ParsedStmtContext());
1244
1245 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
1246 Tok.isNot(tok::eof)) {
1247 if (Tok.is(tok::annot_pragma_unused)) {
1248 HandlePragmaUnused();
1249 continue;
1250 }
1251
1252 if (ConsumeNullStmt(Stmts))
1253 continue;
1254
1255 StmtResult R;
1256 if (Tok.isNot(tok::kw___extension__)) {
1257 R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
1258 } else {
1259 // __extension__ can start declarations and it can also be a unary
1260 // operator for expressions. Consume multiple __extension__ markers here
1261 // until we can determine which is which.
1262 // FIXME: This loses extension expressions in the AST!
1263 SourceLocation ExtLoc = ConsumeToken();
1264 while (Tok.is(tok::kw___extension__))
1265 ConsumeToken();
1266
1267 ParsedAttributes attrs(AttrFactory);
1268 MaybeParseCXX11Attributes(attrs, /*MightBeObjCMessageSend*/ true);
1269
1270 // If this is the start of a declaration, parse it as such.
1271 if (isDeclarationStatement()) {
1272 // __extension__ silences extension warnings in the subdeclaration.
1273 // FIXME: Save the __extension__ on the decl as a node somehow?
1274 ExtensionRAIIObject O(Diags);
1275
1276 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
1277 ParsedAttributes DeclSpecAttrs(AttrFactory);
1278 DeclGroupPtrTy Res = ParseDeclaration(DeclaratorContext::Block, DeclEnd,
1279 attrs, DeclSpecAttrs);
1280 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
1281 } else {
1282 // Otherwise this was a unary __extension__ marker.
1283 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
1284
1285 if (Res.isInvalid()) {
1286 SkipUntil(tok::semi);
1287 continue;
1288 }
1289
1290 // Eat the semicolon at the end of stmt and convert the expr into a
1291 // statement.
1292 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
1293 R = handleExprStmt(Res, SubStmtCtx);
1294 if (R.isUsable())
1295 R = Actions.ActOnAttributedStmt(attrs, R.get());
1296 }
1297 }
1298
1299 if (R.isUsable())
1300 Stmts.push_back(R.get());
1301 }
1302 // Warn the user that using option `-ffp-eval-method=source` on a
1303 // 32-bit target and feature `sse` disabled, or using
1304 // `pragma clang fp eval_method=source` and feature `sse` disabled, is not
1305 // supported.
1310 Diag(Tok.getLocation(),
1311 diag::warn_no_support_for_eval_method_source_on_m32);
1312
1313 SourceLocation CloseLoc = Tok.getLocation();
1314
1315 // We broke out of the while loop because we found a '}' or EOF.
1316 if (!T.consumeClose()) {
1317 // If this is the '})' of a statement expression, check that it's written
1318 // in a sensible way.
1319 if (isStmtExpr && Tok.is(tok::r_paren))
1320 checkCompoundToken(CloseLoc, tok::r_brace, CompoundToken::StmtExprEnd);
1321 } else {
1322 // Recover by creating a compound statement with what we parsed so far,
1323 // instead of dropping everything and returning StmtError().
1324 }
1325
1326 if (T.getCloseLocation().isValid())
1327 CloseLoc = T.getCloseLocation();
1328
1329 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
1330 Stmts, isStmtExpr);
1331}
1332
1333/// ParseParenExprOrCondition:
1334/// [C ] '(' expression ')'
1335/// [C++] '(' condition ')'
1336/// [C++1z] '(' init-statement[opt] condition ')'
1337///
1338/// This function parses and performs error recovery on the specified condition
1339/// or expression (depending on whether we're in C++ or C mode). This function
1340/// goes out of its way to recover well. It returns true if there was a parser
1341/// error (the right paren couldn't be found), which indicates that the caller
1342/// should try to recover harder. It returns false if the condition is
1343/// successfully parsed. Note that a successful parse can still have semantic
1344/// errors in the condition.
1345/// Additionally, it will assign the location of the outer-most '(' and ')',
1346/// to LParenLoc and RParenLoc, respectively.
1347bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1351 SourceLocation &LParenLoc,
1352 SourceLocation &RParenLoc) {
1353 BalancedDelimiterTracker T(*this, tok::l_paren);
1354 T.consumeOpen();
1355 SourceLocation Start = Tok.getLocation();
1356
1357 if (getLangOpts().CPlusPlus) {
1358 Cond = ParseCXXCondition(InitStmt, Loc, CK, false);
1359 } else {
1360 ExprResult CondExpr = ParseExpression();
1361
1362 // If required, convert to a boolean value.
1363 if (CondExpr.isInvalid())
1364 Cond = Sema::ConditionError();
1365 else
1366 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK,
1367 /*MissingOK=*/false);
1368 }
1369
1370 // If the parser was confused by the condition and we don't have a ')', try to
1371 // recover by skipping ahead to a semi and bailing out. If condexp is
1372 // semantically invalid but we have well formed code, keep going.
1373 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
1374 SkipUntil(tok::semi);
1375 // Skipping may have stopped if it found the containing ')'. If so, we can
1376 // continue parsing the if statement.
1377 if (Tok.isNot(tok::r_paren))
1378 return true;
1379 }
1380
1381 if (Cond.isInvalid()) {
1382 ExprResult CondExpr = Actions.CreateRecoveryExpr(
1383 Start, Tok.getLocation() == Start ? Start : PrevTokLocation, {},
1384 Actions.PreferredConditionType(CK));
1385 if (!CondExpr.isInvalid())
1386 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK,
1387 /*MissingOK=*/false);
1388 }
1389
1390 // Either the condition is valid or the rparen is present.
1391 T.consumeClose();
1392 LParenLoc = T.getOpenLocation();
1393 RParenLoc = T.getCloseLocation();
1394
1395 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1396 // that all callers are looking for a statement after the condition, so ")"
1397 // isn't valid.
1398 while (Tok.is(tok::r_paren)) {
1399 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1401 ConsumeParen();
1402 }
1403
1404 return false;
1405}
1406
1407namespace {
1408
1409enum MisleadingStatementKind { MSK_if, MSK_else, MSK_for, MSK_while };
1410
1411struct MisleadingIndentationChecker {
1412 Parser &P;
1413 SourceLocation StmtLoc;
1414 SourceLocation PrevLoc;
1415 unsigned NumDirectives;
1416 MisleadingStatementKind Kind;
1417 bool ShouldSkip;
1418 MisleadingIndentationChecker(Parser &P, MisleadingStatementKind K,
1419 SourceLocation SL)
1420 : P(P), StmtLoc(SL), PrevLoc(P.getCurToken().getLocation()),
1421 NumDirectives(P.getPreprocessor().getNumDirectives()), Kind(K),
1422 ShouldSkip(P.getCurToken().is(tok::l_brace)) {
1423 if (!P.MisleadingIndentationElseLoc.isInvalid()) {
1424 StmtLoc = P.MisleadingIndentationElseLoc;
1425 P.MisleadingIndentationElseLoc = SourceLocation();
1426 }
1427 if (Kind == MSK_else && !ShouldSkip)
1428 P.MisleadingIndentationElseLoc = SL;
1429 }
1430
1431 /// Compute the column number will aligning tabs on TabStop (-ftabstop), this
1432 /// gives the visual indentation of the SourceLocation.
1433 static unsigned getVisualIndentation(SourceManager &SM, SourceLocation Loc) {
1434 unsigned TabStop = SM.getDiagnostics().getDiagnosticOptions().TabStop;
1435
1436 unsigned ColNo = SM.getSpellingColumnNumber(Loc);
1437 if (ColNo == 0 || TabStop == 1)
1438 return ColNo;
1439
1440 std::pair<FileID, unsigned> FIDAndOffset = SM.getDecomposedLoc(Loc);
1441
1442 bool Invalid;
1443 StringRef BufData = SM.getBufferData(FIDAndOffset.first, &Invalid);
1444 if (Invalid)
1445 return 0;
1446
1447 const char *EndPos = BufData.data() + FIDAndOffset.second;
1448 // FileOffset are 0-based and Column numbers are 1-based
1449 assert(FIDAndOffset.second + 1 >= ColNo &&
1450 "Column number smaller than file offset?");
1451
1452 unsigned VisualColumn = 0; // Stored as 0-based column, here.
1453 // Loop from beginning of line up to Loc's file position, counting columns,
1454 // expanding tabs.
1455 for (const char *CurPos = EndPos - (ColNo - 1); CurPos != EndPos;
1456 ++CurPos) {
1457 if (*CurPos == '\t')
1458 // Advance visual column to next tabstop.
1459 VisualColumn += (TabStop - VisualColumn % TabStop);
1460 else
1461 VisualColumn++;
1462 }
1463 return VisualColumn + 1;
1464 }
1465
1466 void Check() {
1467 Token Tok = P.getCurToken();
1468 if (P.getActions().getDiagnostics().isIgnored(
1469 diag::warn_misleading_indentation, Tok.getLocation()) ||
1470 ShouldSkip || NumDirectives != P.getPreprocessor().getNumDirectives() ||
1471 Tok.isOneOf(tok::semi, tok::r_brace) || Tok.isAnnotation() ||
1472 Tok.getLocation().isMacroID() || PrevLoc.isMacroID() ||
1473 StmtLoc.isMacroID() ||
1474 (Kind == MSK_else && P.MisleadingIndentationElseLoc.isInvalid())) {
1475 P.MisleadingIndentationElseLoc = SourceLocation();
1476 return;
1477 }
1478 if (Kind == MSK_else)
1479 P.MisleadingIndentationElseLoc = SourceLocation();
1480
1481 SourceManager &SM = P.getPreprocessor().getSourceManager();
1482 unsigned PrevColNum = getVisualIndentation(SM, PrevLoc);
1483 unsigned CurColNum = getVisualIndentation(SM, Tok.getLocation());
1484 unsigned StmtColNum = getVisualIndentation(SM, StmtLoc);
1485
1486 if (PrevColNum != 0 && CurColNum != 0 && StmtColNum != 0 &&
1487 ((PrevColNum > StmtColNum && PrevColNum == CurColNum) ||
1488 !Tok.isAtStartOfLine()) &&
1489 SM.getPresumedLineNumber(StmtLoc) !=
1490 SM.getPresumedLineNumber(Tok.getLocation()) &&
1491 (Tok.isNot(tok::identifier) ||
1492 P.getPreprocessor().LookAhead(0).isNot(tok::colon))) {
1493 P.Diag(Tok.getLocation(), diag::warn_misleading_indentation) << Kind;
1494 P.Diag(StmtLoc, diag::note_previous_statement);
1495 }
1496 }
1497};
1498
1499}
1500
1501/// ParseIfStatement
1502/// if-statement: [C99 6.8.4.1]
1503/// 'if' '(' expression ')' statement
1504/// 'if' '(' expression ')' statement 'else' statement
1505/// [C++] 'if' '(' condition ')' statement
1506/// [C++] 'if' '(' condition ')' statement 'else' statement
1507/// [C++23] 'if' '!' [opt] consteval compound-statement
1508/// [C++23] 'if' '!' [opt] consteval compound-statement 'else' statement
1509///
1510StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
1511 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
1512 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
1513
1514 bool IsConstexpr = false;
1515 bool IsConsteval = false;
1516 SourceLocation NotLocation;
1517 SourceLocation ConstevalLoc;
1518
1519 if (Tok.is(tok::kw_constexpr)) {
1520 Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
1521 : diag::ext_constexpr_if);
1522 IsConstexpr = true;
1523 ConsumeToken();
1524 } else {
1525 if (Tok.is(tok::exclaim)) {
1526 NotLocation = ConsumeToken();
1527 }
1528
1529 if (Tok.is(tok::kw_consteval)) {
1530 Diag(Tok, getLangOpts().CPlusPlus23 ? diag::warn_cxx20_compat_consteval_if
1531 : diag::ext_consteval_if);
1532 IsConsteval = true;
1533 ConstevalLoc = ConsumeToken();
1534 }
1535 }
1536 if (!IsConsteval && (NotLocation.isValid() || Tok.isNot(tok::l_paren))) {
1537 Diag(Tok, diag::err_expected_lparen_after) << "if";
1538 SkipUntil(tok::semi);
1539 return StmtError();
1540 }
1541
1542 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1543
1544 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1545 // the case for C90.
1546 //
1547 // C++ 6.4p3:
1548 // A name introduced by a declaration in a condition is in scope from its
1549 // point of declaration until the end of the substatements controlled by the
1550 // condition.
1551 // C++ 3.3.2p4:
1552 // Names declared in the for-init-statement, and in the condition of if,
1553 // while, for, and switch statements are local to the if, while, for, or
1554 // switch statement (including the controlled statement).
1555 //
1556 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
1557
1558 // Parse the condition.
1559 StmtResult InitStmt;
1561 SourceLocation LParen;
1562 SourceLocation RParen;
1563 std::optional<bool> ConstexprCondition;
1564 if (!IsConsteval) {
1565
1566 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
1569 LParen, RParen))
1570 return StmtError();
1571
1572 if (IsConstexpr)
1573 ConstexprCondition = Cond.getKnownValue();
1574 }
1575
1576 bool IsBracedThen = Tok.is(tok::l_brace);
1577
1578 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1579 // there is no compound stmt. C90 does not have this clause. We only do this
1580 // if the body isn't a compound statement to avoid push/pop in common cases.
1581 //
1582 // C++ 6.4p1:
1583 // The substatement in a selection-statement (each substatement, in the else
1584 // form of the if statement) implicitly defines a local scope.
1585 //
1586 // For C++ we create a scope for the condition and a new scope for
1587 // substatements because:
1588 // -When the 'then' scope exits, we want the condition declaration to still be
1589 // active for the 'else' scope too.
1590 // -Sema will detect name clashes by considering declarations of a
1591 // 'ControlScope' as part of its direct subscope.
1592 // -If we wanted the condition and substatement to be in the same scope, we
1593 // would have to notify ParseStatement not to create a new scope. It's
1594 // simpler to let it create a new scope.
1595 //
1596 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, IsBracedThen);
1597
1598 MisleadingIndentationChecker MIChecker(*this, MSK_if, IfLoc);
1599
1600 // Read the 'then' stmt.
1601 SourceLocation ThenStmtLoc = Tok.getLocation();
1602
1603 SourceLocation InnerStatementTrailingElseLoc;
1604 StmtResult ThenStmt;
1605 {
1606 bool ShouldEnter = ConstexprCondition && !*ConstexprCondition;
1609 if (NotLocation.isInvalid() && IsConsteval) {
1611 ShouldEnter = true;
1612 }
1613
1614 EnterExpressionEvaluationContext PotentiallyDiscarded(
1615 Actions, Context, nullptr,
1617 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1618 }
1619
1620 if (Tok.isNot(tok::kw_else))
1621 MIChecker.Check();
1622
1623 // Pop the 'if' scope if needed.
1624 InnerScope.Exit();
1625
1626 // If it has an else, parse it.
1627 SourceLocation ElseLoc;
1628 SourceLocation ElseStmtLoc;
1629 StmtResult ElseStmt;
1630
1631 if (Tok.is(tok::kw_else)) {
1632 if (TrailingElseLoc)
1633 *TrailingElseLoc = Tok.getLocation();
1634
1635 ElseLoc = ConsumeToken();
1636 ElseStmtLoc = Tok.getLocation();
1637
1638 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1639 // there is no compound stmt. C90 does not have this clause. We only do
1640 // this if the body isn't a compound statement to avoid push/pop in common
1641 // cases.
1642 //
1643 // C++ 6.4p1:
1644 // The substatement in a selection-statement (each substatement, in the else
1645 // form of the if statement) implicitly defines a local scope.
1646 //
1647 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1648 Tok.is(tok::l_brace));
1649
1650 MisleadingIndentationChecker MIChecker(*this, MSK_else, ElseLoc);
1651 bool ShouldEnter = ConstexprCondition && *ConstexprCondition;
1654 if (NotLocation.isValid() && IsConsteval) {
1656 ShouldEnter = true;
1657 }
1658
1659 EnterExpressionEvaluationContext PotentiallyDiscarded(
1660 Actions, Context, nullptr,
1662 ElseStmt = ParseStatement();
1663
1664 if (ElseStmt.isUsable())
1665 MIChecker.Check();
1666
1667 // Pop the 'else' scope if needed.
1668 InnerScope.Exit();
1669 } else if (Tok.is(tok::code_completion)) {
1670 cutOffParsing();
1671 Actions.CodeCompletion().CodeCompleteAfterIf(getCurScope(), IsBracedThen);
1672 return StmtError();
1673 } else if (InnerStatementTrailingElseLoc.isValid()) {
1674 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
1675 }
1676
1677 IfScope.Exit();
1678
1679 // If the then or else stmt is invalid and the other is valid (and present),
1680 // turn the invalid one into a null stmt to avoid dropping the other
1681 // part. If both are invalid, return error.
1682 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1683 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1684 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
1685 // Both invalid, or one is invalid and other is non-present: return error.
1686 return StmtError();
1687 }
1688
1689 if (IsConsteval) {
1690 auto IsCompoundStatement = [](const Stmt *S) {
1691 if (const auto *Outer = dyn_cast_if_present<AttributedStmt>(S))
1692 S = Outer->getSubStmt();
1693 return isa_and_nonnull<clang::CompoundStmt>(S);
1694 };
1695
1696 if (!IsCompoundStatement(ThenStmt.get())) {
1697 Diag(ConstevalLoc, diag::err_expected_after) << "consteval"
1698 << "{";
1699 return StmtError();
1700 }
1701 if (!ElseStmt.isUnset() && !IsCompoundStatement(ElseStmt.get())) {
1702 Diag(ElseLoc, diag::err_expected_after) << "else"
1703 << "{";
1704 return StmtError();
1705 }
1706 }
1707
1708 // Now if either are invalid, replace with a ';'.
1709 if (ThenStmt.isInvalid())
1710 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
1711 if (ElseStmt.isInvalid())
1712 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
1713
1715 if (IsConstexpr)
1717 else if (IsConsteval)
1720
1721 return Actions.ActOnIfStmt(IfLoc, Kind, LParen, InitStmt.get(), Cond, RParen,
1722 ThenStmt.get(), ElseLoc, ElseStmt.get());
1723}
1724
1725/// ParseSwitchStatement
1726/// switch-statement:
1727/// 'switch' '(' expression ')' statement
1728/// [C++] 'switch' '(' condition ')' statement
1729StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
1730 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
1731 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1732
1733 if (Tok.isNot(tok::l_paren)) {
1734 Diag(Tok, diag::err_expected_lparen_after) << "switch";
1735 SkipUntil(tok::semi);
1736 return StmtError();
1737 }
1738
1739 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1740
1741 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1742 // not the case for C90. Start the switch scope.
1743 //
1744 // C++ 6.4p3:
1745 // A name introduced by a declaration in a condition is in scope from its
1746 // point of declaration until the end of the substatements controlled by the
1747 // condition.
1748 // C++ 3.3.2p4:
1749 // Names declared in the for-init-statement, and in the condition of if,
1750 // while, for, and switch statements are local to the if, while, for, or
1751 // switch statement (including the controlled statement).
1752 //
1753 unsigned ScopeFlags = Scope::SwitchScope;
1754 if (C99orCXX)
1755 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
1756 ParseScope SwitchScope(this, ScopeFlags);
1757
1758 // Parse the condition.
1759 StmtResult InitStmt;
1761 SourceLocation LParen;
1762 SourceLocation RParen;
1763 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1764 Sema::ConditionKind::Switch, LParen, RParen))
1765 return StmtError();
1766
1768 SwitchLoc, LParen, InitStmt.get(), Cond, RParen);
1769
1770 if (Switch.isInvalid()) {
1771 // Skip the switch body.
1772 // FIXME: This is not optimal recovery, but parsing the body is more
1773 // dangerous due to the presence of case and default statements, which
1774 // will have no place to connect back with the switch.
1775 if (Tok.is(tok::l_brace)) {
1776 ConsumeBrace();
1777 SkipUntil(tok::r_brace);
1778 } else
1779 SkipUntil(tok::semi);
1780 return Switch;
1781 }
1782
1783 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
1784 // there is no compound stmt. C90 does not have this clause. We only do this
1785 // if the body isn't a compound statement to avoid push/pop in common cases.
1786 //
1787 // C++ 6.4p1:
1788 // The substatement in a selection-statement (each substatement, in the else
1789 // form of the if statement) implicitly defines a local scope.
1790 //
1791 // See comments in ParseIfStatement for why we create a scope for the
1792 // condition and a new scope for substatement in C++.
1793 //
1795 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1796
1797 // We have incremented the mangling number for the SwitchScope and the
1798 // InnerScope, which is one too many.
1799 if (C99orCXX)
1801
1802 // Read the body statement.
1803 StmtResult Body(ParseStatement(TrailingElseLoc));
1804
1805 // Pop the scopes.
1806 InnerScope.Exit();
1807 SwitchScope.Exit();
1808
1809 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
1810}
1811
1812/// ParseWhileStatement
1813/// while-statement: [C99 6.8.5.1]
1814/// 'while' '(' expression ')' statement
1815/// [C++] 'while' '(' condition ')' statement
1816StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
1817 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
1818 SourceLocation WhileLoc = Tok.getLocation();
1819 ConsumeToken(); // eat the 'while'.
1820
1821 if (Tok.isNot(tok::l_paren)) {
1822 Diag(Tok, diag::err_expected_lparen_after) << "while";
1823 SkipUntil(tok::semi);
1824 return StmtError();
1825 }
1826
1827 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1828
1829 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1830 // the case for C90. Start the loop scope.
1831 //
1832 // C++ 6.4p3:
1833 // A name introduced by a declaration in a condition is in scope from its
1834 // point of declaration until the end of the substatements controlled by the
1835 // condition.
1836 // C++ 3.3.2p4:
1837 // Names declared in the for-init-statement, and in the condition of if,
1838 // while, for, and switch statements are local to the if, while, for, or
1839 // switch statement (including the controlled statement).
1840 //
1841 unsigned ScopeFlags;
1842 if (C99orCXX)
1845 else
1847 ParseScope WhileScope(this, ScopeFlags);
1848
1849 // Parse the condition.
1851 SourceLocation LParen;
1852 SourceLocation RParen;
1853 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1854 Sema::ConditionKind::Boolean, LParen, RParen))
1855 return StmtError();
1856
1857 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
1858 // there is no compound stmt. C90 does not have this clause. We only do this
1859 // if the body isn't a compound statement to avoid push/pop in common cases.
1860 //
1861 // C++ 6.5p2:
1862 // The substatement in an iteration-statement implicitly defines a local scope
1863 // which is entered and exited each time through the loop.
1864 //
1865 // See comments in ParseIfStatement for why we create a scope for the
1866 // condition and a new scope for substatement in C++.
1867 //
1868 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1869
1870 MisleadingIndentationChecker MIChecker(*this, MSK_while, WhileLoc);
1871
1872 // Read the body statement.
1873 StmtResult Body(ParseStatement(TrailingElseLoc));
1874
1875 if (Body.isUsable())
1876 MIChecker.Check();
1877 // Pop the body scope if needed.
1878 InnerScope.Exit();
1879 WhileScope.Exit();
1880
1881 if (Cond.isInvalid() || Body.isInvalid())
1882 return StmtError();
1883
1884 return Actions.ActOnWhileStmt(WhileLoc, LParen, Cond, RParen, Body.get());
1885}
1886
1887/// ParseDoStatement
1888/// do-statement: [C99 6.8.5.2]
1889/// 'do' statement 'while' '(' expression ')' ';'
1890/// Note: this lets the caller parse the end ';'.
1891StmtResult Parser::ParseDoStatement() {
1892 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
1893 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
1894
1895 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1896 // the case for C90. Start the loop scope.
1897 unsigned ScopeFlags;
1898 if (getLangOpts().C99)
1900 else
1902
1903 ParseScope DoScope(this, ScopeFlags);
1904
1905 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
1906 // there is no compound stmt. C90 does not have this clause. We only do this
1907 // if the body isn't a compound statement to avoid push/pop in common cases.
1908 //
1909 // C++ 6.5p2:
1910 // The substatement in an iteration-statement implicitly defines a local scope
1911 // which is entered and exited each time through the loop.
1912 //
1913 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1914 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1915
1916 // Read the body statement.
1917 StmtResult Body(ParseStatement());
1918
1919 // Pop the body scope if needed.
1920 InnerScope.Exit();
1921
1922 if (Tok.isNot(tok::kw_while)) {
1923 if (!Body.isInvalid()) {
1924 Diag(Tok, diag::err_expected_while);
1925 Diag(DoLoc, diag::note_matching) << "'do'";
1926 SkipUntil(tok::semi, StopBeforeMatch);
1927 }
1928 return StmtError();
1929 }
1930 SourceLocation WhileLoc = ConsumeToken();
1931
1932 if (Tok.isNot(tok::l_paren)) {
1933 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
1934 SkipUntil(tok::semi, StopBeforeMatch);
1935 return StmtError();
1936 }
1937
1938 // Parse the parenthesized expression.
1939 BalancedDelimiterTracker T(*this, tok::l_paren);
1940 T.consumeOpen();
1941
1942 // A do-while expression is not a condition, so can't have attributes.
1943 DiagnoseAndSkipCXX11Attributes();
1944
1945 SourceLocation Start = Tok.getLocation();
1946 ExprResult Cond = ParseExpression();
1947 // Correct the typos in condition before closing the scope.
1948 if (Cond.isUsable())
1949 Cond = Actions.CorrectDelayedTyposInExpr(Cond, /*InitDecl=*/nullptr,
1950 /*RecoverUncorrectedTypos=*/true);
1951 else {
1952 if (!Tok.isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
1953 SkipUntil(tok::semi);
1954 Cond = Actions.CreateRecoveryExpr(
1955 Start, Start == Tok.getLocation() ? Start : PrevTokLocation, {},
1956 Actions.getASTContext().BoolTy);
1957 }
1958 T.consumeClose();
1959 DoScope.Exit();
1960
1961 if (Cond.isInvalid() || Body.isInvalid())
1962 return StmtError();
1963
1964 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1965 Cond.get(), T.getCloseLocation());
1966}
1967
1968bool Parser::isForRangeIdentifier() {
1969 assert(Tok.is(tok::identifier));
1970
1971 const Token &Next = NextToken();
1972 if (Next.is(tok::colon))
1973 return true;
1974
1975 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
1976 TentativeParsingAction PA(*this);
1977 ConsumeToken();
1978 SkipCXX11Attributes();
1979 bool Result = Tok.is(tok::colon);
1980 PA.Revert();
1981 return Result;
1982 }
1983
1984 return false;
1985}
1986
1987/// ParseForStatement
1988/// for-statement: [C99 6.8.5.3]
1989/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1990/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
1991/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1992/// [C++] statement
1993/// [C++0x] 'for'
1994/// 'co_await'[opt] [Coroutines]
1995/// '(' for-range-declaration ':' for-range-initializer ')'
1996/// statement
1997/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1998/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
1999///
2000/// [C++] for-init-statement:
2001/// [C++] expression-statement
2002/// [C++] simple-declaration
2003/// [C++23] alias-declaration
2004///
2005/// [C++0x] for-range-declaration:
2006/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
2007/// [C++0x] for-range-initializer:
2008/// [C++0x] expression
2009/// [C++0x] braced-init-list [TODO]
2010StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
2011 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
2012 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
2013
2014 SourceLocation CoawaitLoc;
2015 if (Tok.is(tok::kw_co_await))
2016 CoawaitLoc = ConsumeToken();
2017
2018 if (Tok.isNot(tok::l_paren)) {
2019 Diag(Tok, diag::err_expected_lparen_after) << "for";
2020 SkipUntil(tok::semi);
2021 return StmtError();
2022 }
2023
2024 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
2025 getLangOpts().ObjC;
2026
2027 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
2028 // the case for C90. Start the loop scope.
2029 //
2030 // C++ 6.4p3:
2031 // A name introduced by a declaration in a condition is in scope from its
2032 // point of declaration until the end of the substatements controlled by the
2033 // condition.
2034 // C++ 3.3.2p4:
2035 // Names declared in the for-init-statement, and in the condition of if,
2036 // while, for, and switch statements are local to the if, while, for, or
2037 // switch statement (including the controlled statement).
2038 // C++ 6.5.3p1:
2039 // Names declared in the for-init-statement are in the same declarative-region
2040 // as those declared in the condition.
2041 //
2042 unsigned ScopeFlags = 0;
2043 if (C99orCXXorObjC)
2045
2046 ParseScope ForScope(this, ScopeFlags);
2047
2048 BalancedDelimiterTracker T(*this, tok::l_paren);
2049 T.consumeOpen();
2050
2052
2053 bool ForEach = false;
2054 StmtResult FirstPart;
2055 Sema::ConditionResult SecondPart;
2056 ExprResult Collection;
2057 ForRangeInfo ForRangeInfo;
2058 FullExprArg ThirdPart(Actions);
2059
2060 if (Tok.is(tok::code_completion)) {
2061 cutOffParsing();
2065 return StmtError();
2066 }
2067
2068 ParsedAttributes attrs(AttrFactory);
2069 MaybeParseCXX11Attributes(attrs);
2070
2071 SourceLocation EmptyInitStmtSemiLoc;
2072
2073 // Parse the first part of the for specifier.
2074 if (Tok.is(tok::semi)) { // for (;
2075 ProhibitAttributes(attrs);
2076 // no first part, eat the ';'.
2077 SourceLocation SemiLoc = Tok.getLocation();
2078 if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID())
2079 EmptyInitStmtSemiLoc = SemiLoc;
2080 ConsumeToken();
2081 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
2082 isForRangeIdentifier()) {
2083 ProhibitAttributes(attrs);
2084 IdentifierInfo *Name = Tok.getIdentifierInfo();
2086 MaybeParseCXX11Attributes(attrs);
2087
2088 ForRangeInfo.ColonLoc = ConsumeToken();
2089 if (Tok.is(tok::l_brace))
2090 ForRangeInfo.RangeExpr = ParseBraceInitializer();
2091 else
2092 ForRangeInfo.RangeExpr = ParseExpression();
2093
2094 Diag(Loc, diag::err_for_range_identifier)
2095 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus17)
2096 ? FixItHint::CreateInsertion(Loc, "auto &&")
2097 : FixItHint());
2098
2099 ForRangeInfo.LoopVar =
2100 Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name, attrs);
2101 } else if (isForInitDeclaration()) { // for (int X = 4;
2102 ParenBraceBracketBalancer BalancerRAIIObj(*this);
2103
2104 // Parse declaration, which eats the ';'.
2105 if (!C99orCXXorObjC) { // Use of C99-style for loops in C90 mode?
2106 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
2107 Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop);
2108 }
2109 DeclGroupPtrTy DG;
2110 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
2111 if (Tok.is(tok::kw_using)) {
2112 DG = ParseAliasDeclarationInInitStatement(DeclaratorContext::ForInit,
2113 attrs);
2114 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
2115 } else {
2116 // In C++0x, "for (T NS:a" might not be a typo for ::
2117 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
2118 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
2119 ParsedAttributes DeclSpecAttrs(AttrFactory);
2120 DG = ParseSimpleDeclaration(
2121 DeclaratorContext::ForInit, DeclEnd, attrs, DeclSpecAttrs, false,
2122 MightBeForRangeStmt ? &ForRangeInfo : nullptr);
2123 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
2124 if (ForRangeInfo.ParsedForRangeDecl()) {
2125 Diag(ForRangeInfo.ColonLoc, getLangOpts().CPlusPlus11
2126 ? diag::warn_cxx98_compat_for_range
2127 : diag::ext_for_range);
2128 ForRangeInfo.LoopVar = FirstPart;
2129 FirstPart = StmtResult();
2130 } else if (Tok.is(tok::semi)) { // for (int x = 4;
2131 ConsumeToken();
2132 } else if ((ForEach = isTokIdentifier_in())) {
2133 Actions.ActOnForEachDeclStmt(DG);
2134 // ObjC: for (id x in expr)
2135 ConsumeToken(); // consume 'in'
2136
2137 if (Tok.is(tok::code_completion)) {
2138 cutOffParsing();
2140 DG);
2141 return StmtError();
2142 }
2143 Collection = ParseExpression();
2144 } else {
2145 Diag(Tok, diag::err_expected_semi_for);
2146 }
2147 }
2148 } else {
2149 ProhibitAttributes(attrs);
2151
2152 ForEach = isTokIdentifier_in();
2153
2154 // Turn the expression into a stmt.
2155 if (!Value.isInvalid()) {
2156 if (ForEach)
2157 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
2158 else {
2159 // We already know this is not an init-statement within a for loop, so
2160 // if we are parsing a C++11 range-based for loop, we should treat this
2161 // expression statement as being a discarded value expression because
2162 // we will err below. This way we do not warn on an unused expression
2163 // that was an error in the first place, like with: for (expr : expr);
2164 bool IsRangeBasedFor =
2165 getLangOpts().CPlusPlus11 && !ForEach && Tok.is(tok::colon);
2166 FirstPart = Actions.ActOnExprStmt(Value, !IsRangeBasedFor);
2167 }
2168 }
2169
2170 if (Tok.is(tok::semi)) {
2171 ConsumeToken();
2172 } else if (ForEach) {
2173 ConsumeToken(); // consume 'in'
2174
2175 if (Tok.is(tok::code_completion)) {
2176 cutOffParsing();
2178 nullptr);
2179 return StmtError();
2180 }
2181 Collection = ParseExpression();
2182 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
2183 // User tried to write the reasonable, but ill-formed, for-range-statement
2184 // for (expr : expr) { ... }
2185 Diag(Tok, diag::err_for_range_expected_decl)
2186 << FirstPart.get()->getSourceRange();
2187 SkipUntil(tok::r_paren, StopBeforeMatch);
2188 SecondPart = Sema::ConditionError();
2189 } else {
2190 if (!Value.isInvalid()) {
2191 Diag(Tok, diag::err_expected_semi_for);
2192 } else {
2193 // Skip until semicolon or rparen, don't consume it.
2194 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
2195 if (Tok.is(tok::semi))
2196 ConsumeToken();
2197 }
2198 }
2199 }
2200
2201 // Parse the second part of the for specifier.
2202 if (!ForEach && !ForRangeInfo.ParsedForRangeDecl() &&
2203 !SecondPart.isInvalid()) {
2204 // Parse the second part of the for specifier.
2205 if (Tok.is(tok::semi)) { // for (...;;
2206 // no second part.
2207 } else if (Tok.is(tok::r_paren)) {
2208 // missing both semicolons.
2209 } else {
2210 if (getLangOpts().CPlusPlus) {
2211 // C++2a: We've parsed an init-statement; we might have a
2212 // for-range-declaration next.
2213 bool MightBeForRangeStmt = !ForRangeInfo.ParsedForRangeDecl();
2214 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
2215 SourceLocation SecondPartStart = Tok.getLocation();
2217 SecondPart = ParseCXXCondition(
2218 /*InitStmt=*/nullptr, ForLoc, CK,
2219 // FIXME: recovery if we don't see another semi!
2220 /*MissingOK=*/true, MightBeForRangeStmt ? &ForRangeInfo : nullptr,
2221 /*EnterForConditionScope=*/true);
2222
2223 if (ForRangeInfo.ParsedForRangeDecl()) {
2224 Diag(FirstPart.get() ? FirstPart.get()->getBeginLoc()
2225 : ForRangeInfo.ColonLoc,
2227 ? diag::warn_cxx17_compat_for_range_init_stmt
2228 : diag::ext_for_range_init_stmt)
2229 << (FirstPart.get() ? FirstPart.get()->getSourceRange()
2230 : SourceRange());
2231 if (EmptyInitStmtSemiLoc.isValid()) {
2232 Diag(EmptyInitStmtSemiLoc, diag::warn_empty_init_statement)
2233 << /*for-loop*/ 2
2234 << FixItHint::CreateRemoval(EmptyInitStmtSemiLoc);
2235 }
2236 }
2237
2238 if (SecondPart.isInvalid()) {
2239 ExprResult CondExpr = Actions.CreateRecoveryExpr(
2240 SecondPartStart,
2241 Tok.getLocation() == SecondPartStart ? SecondPartStart
2242 : PrevTokLocation,
2243 {}, Actions.PreferredConditionType(CK));
2244 if (!CondExpr.isInvalid())
2245 SecondPart = Actions.ActOnCondition(getCurScope(), ForLoc,
2246 CondExpr.get(), CK,
2247 /*MissingOK=*/false);
2248 }
2249
2250 } else {
2251 // We permit 'continue' and 'break' in the condition of a for loop.
2253
2254 ExprResult SecondExpr = ParseExpression();
2255 if (SecondExpr.isInvalid())
2256 SecondPart = Sema::ConditionError();
2257 else
2258 SecondPart = Actions.ActOnCondition(
2259 getCurScope(), ForLoc, SecondExpr.get(),
2260 Sema::ConditionKind::Boolean, /*MissingOK=*/true);
2261 }
2262 }
2263 }
2264
2265 // Enter a break / continue scope, if we didn't already enter one while
2266 // parsing the second part.
2267 if (!getCurScope()->isContinueScope())
2269
2270 // Parse the third part of the for statement.
2271 if (!ForEach && !ForRangeInfo.ParsedForRangeDecl()) {
2272 if (Tok.isNot(tok::semi)) {
2273 if (!SecondPart.isInvalid())
2274 Diag(Tok, diag::err_expected_semi_for);
2275 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
2276 }
2277
2278 if (Tok.is(tok::semi)) {
2279 ConsumeToken();
2280 }
2281
2282 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
2283 ExprResult Third = ParseExpression();
2284 // FIXME: The C++11 standard doesn't actually say that this is a
2285 // discarded-value expression, but it clearly should be.
2286 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
2287 }
2288 }
2289 // Match the ')'.
2290 T.consumeClose();
2291
2292 // C++ Coroutines [stmt.iter]:
2293 // 'co_await' can only be used for a range-based for statement.
2294 if (CoawaitLoc.isValid() && !ForRangeInfo.ParsedForRangeDecl()) {
2295 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
2296 CoawaitLoc = SourceLocation();
2297 }
2298
2299 if (CoawaitLoc.isValid() && getLangOpts().CPlusPlus20)
2300 Diag(CoawaitLoc, diag::warn_deprecated_for_co_await);
2301
2302 // We need to perform most of the semantic analysis for a C++0x for-range
2303 // statememt before parsing the body, in order to be able to deduce the type
2304 // of an auto-typed loop variable.
2305 StmtResult ForRangeStmt;
2306 StmtResult ForEachStmt;
2307
2308 if (ForRangeInfo.ParsedForRangeDecl()) {
2309 ExprResult CorrectedRange =
2310 Actions.CorrectDelayedTyposInExpr(ForRangeInfo.RangeExpr.get());
2311 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
2312 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
2313 ForRangeInfo.LoopVar.get(), ForRangeInfo.ColonLoc, CorrectedRange.get(),
2314 T.getCloseLocation(), Sema::BFRK_Build,
2315 ForRangeInfo.LifetimeExtendTemps);
2316 } else if (ForEach) {
2317 // Similarly, we need to do the semantic analysis for a for-range
2318 // statement immediately in order to close over temporaries correctly.
2319 ForEachStmt = Actions.ObjC().ActOnObjCForCollectionStmt(
2320 ForLoc, FirstPart.get(), Collection.get(), T.getCloseLocation());
2321 } else {
2322 // In OpenMP loop region loop control variable must be captured and be
2323 // private. Perform analysis of first part (if any).
2324 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
2325 Actions.OpenMP().ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
2326 }
2327 }
2328
2329 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
2330 // there is no compound stmt. C90 does not have this clause. We only do this
2331 // if the body isn't a compound statement to avoid push/pop in common cases.
2332 //
2333 // C++ 6.5p2:
2334 // The substatement in an iteration-statement implicitly defines a local scope
2335 // which is entered and exited each time through the loop.
2336 //
2337 // See comments in ParseIfStatement for why we create a scope for
2338 // for-init-statement/condition and a new scope for substatement in C++.
2339 //
2340 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
2341 Tok.is(tok::l_brace));
2342
2343 // The body of the for loop has the same local mangling number as the
2344 // for-init-statement.
2345 // It will only be incremented if the body contains other things that would
2346 // normally increment the mangling number (like a compound statement).
2347 if (C99orCXXorObjC)
2349
2350 MisleadingIndentationChecker MIChecker(*this, MSK_for, ForLoc);
2351
2352 // Read the body statement.
2353 StmtResult Body(ParseStatement(TrailingElseLoc));
2354
2355 if (Body.isUsable())
2356 MIChecker.Check();
2357
2358 // Pop the body scope if needed.
2359 InnerScope.Exit();
2360
2361 // Leave the for-scope.
2362 ForScope.Exit();
2363
2364 if (Body.isInvalid())
2365 return StmtError();
2366
2367 if (ForEach)
2368 return Actions.ObjC().FinishObjCForCollectionStmt(ForEachStmt.get(),
2369 Body.get());
2370
2371 if (ForRangeInfo.ParsedForRangeDecl())
2372 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
2373
2374 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
2375 SecondPart, ThirdPart, T.getCloseLocation(),
2376 Body.get());
2377}
2378
2379/// ParseGotoStatement
2380/// jump-statement:
2381/// 'goto' identifier ';'
2382/// [GNU] 'goto' '*' expression ';'
2383///
2384/// Note: this lets the caller parse the end ';'.
2385///
2386StmtResult Parser::ParseGotoStatement() {
2387 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
2388 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
2389
2390 StmtResult Res;
2391 if (Tok.is(tok::identifier)) {
2393 Tok.getLocation());
2394 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
2395 ConsumeToken();
2396 } else if (Tok.is(tok::star)) {
2397 // GNU indirect goto extension.
2398 Diag(Tok, diag::ext_gnu_indirect_goto);
2399 SourceLocation StarLoc = ConsumeToken();
2401 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
2402 SkipUntil(tok::semi, StopBeforeMatch);
2403 return StmtError();
2404 }
2405 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
2406 } else {
2407 Diag(Tok, diag::err_expected) << tok::identifier;
2408 return StmtError();
2409 }
2410
2411 return Res;
2412}
2413
2414/// ParseContinueStatement
2415/// jump-statement:
2416/// 'continue' ';'
2417///
2418/// Note: this lets the caller parse the end ';'.
2419///
2420StmtResult Parser::ParseContinueStatement() {
2421 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
2422 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
2423}
2424
2425/// ParseBreakStatement
2426/// jump-statement:
2427/// 'break' ';'
2428///
2429/// Note: this lets the caller parse the end ';'.
2430///
2431StmtResult Parser::ParseBreakStatement() {
2432 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
2433 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
2434}
2435
2436/// ParseReturnStatement
2437/// jump-statement:
2438/// 'return' expression[opt] ';'
2439/// 'return' braced-init-list ';'
2440/// 'co_return' expression[opt] ';'
2441/// 'co_return' braced-init-list ';'
2442StmtResult Parser::ParseReturnStatement() {
2443 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
2444 "Not a return stmt!");
2445 bool IsCoreturn = Tok.is(tok::kw_co_return);
2446 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
2447
2448 ExprResult R;
2449 if (Tok.isNot(tok::semi)) {
2450 if (!IsCoreturn)
2451 PreferredType.enterReturn(Actions, Tok.getLocation());
2452 // FIXME: Code completion for co_return.
2453 if (Tok.is(tok::code_completion) && !IsCoreturn) {
2454 cutOffParsing();
2456 getCurScope(), PreferredType.get(Tok.getLocation()));
2457 return StmtError();
2458 }
2459
2460 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
2461 R = ParseInitializer();
2462 if (R.isUsable())
2463 Diag(R.get()->getBeginLoc(),
2465 ? diag::warn_cxx98_compat_generalized_initializer_lists
2466 : diag::ext_generalized_initializer_lists)
2467 << R.get()->getSourceRange();
2468 } else
2469 R = ParseExpression();
2470 if (R.isInvalid()) {
2471 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2472 return StmtError();
2473 }
2474 }
2475 if (IsCoreturn)
2476 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
2477 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
2478}
2479
2480StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
2481 ParsedStmtContext StmtCtx,
2482 SourceLocation *TrailingElseLoc,
2483 ParsedAttributes &Attrs) {
2484 // Create temporary attribute list.
2485 ParsedAttributes TempAttrs(AttrFactory);
2486
2487 SourceLocation StartLoc = Tok.getLocation();
2488
2489 // Get loop hints and consume annotated token.
2490 while (Tok.is(tok::annot_pragma_loop_hint)) {
2491 LoopHint Hint;
2492 if (!HandlePragmaLoopHint(Hint))
2493 continue;
2494
2495 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
2496 ArgsUnion(Hint.ValueExpr)};
2497 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
2498 Hint.PragmaNameLoc->Loc, ArgHints, 4,
2499 ParsedAttr::Form::Pragma());
2500 }
2501
2502 // Get the next statement.
2503 MaybeParseCXX11Attributes(Attrs);
2504
2505 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
2506 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
2507 Stmts, StmtCtx, TrailingElseLoc, Attrs, EmptyDeclSpecAttrs);
2508
2509 Attrs.takeAllFrom(TempAttrs);
2510
2511 // Start of attribute range may already be set for some invalid input.
2512 // See PR46336.
2513 if (Attrs.Range.getBegin().isInvalid())
2514 Attrs.Range.setBegin(StartLoc);
2515
2516 return S;
2517}
2518
2519Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
2520 assert(Tok.is(tok::l_brace));
2521 SourceLocation LBraceLoc = Tok.getLocation();
2522
2523 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc,
2524 "parsing function body");
2525
2526 // Save and reset current vtordisp stack if we have entered a C++ method body.
2527 bool IsCXXMethod =
2528 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
2530 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
2531
2532 // Do not enter a scope for the brace, as the arguments are in the same scope
2533 // (the function body) as the body itself. Instead, just read the statement
2534 // list and put it into a CompoundStmt for safe keeping.
2535 StmtResult FnBody(ParseCompoundStatementBody());
2536
2537 // If the function body could not be parsed, make a bogus compoundstmt.
2538 if (FnBody.isInvalid()) {
2539 Sema::CompoundScopeRAII CompoundScope(Actions);
2540 FnBody =
2541 Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, std::nullopt, false);
2542 }
2543
2544 BodyScope.Exit();
2545 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
2546}
2547
2548/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2549///
2550/// function-try-block:
2551/// 'try' ctor-initializer[opt] compound-statement handler-seq
2552///
2553Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
2554 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2555 SourceLocation TryLoc = ConsumeToken();
2556
2557 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc,
2558 "parsing function try block");
2559
2560 // Constructor initializer list?
2561 if (Tok.is(tok::colon))
2562 ParseConstructorInitializer(Decl);
2563 else
2565
2566 // Save and reset current vtordisp stack if we have entered a C++ method body.
2567 bool IsCXXMethod =
2568 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
2570 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
2571
2572 SourceLocation LBraceLoc = Tok.getLocation();
2573 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
2574 // If we failed to parse the try-catch, we just give the function an empty
2575 // compound statement as the body.
2576 if (FnBody.isInvalid()) {
2577 Sema::CompoundScopeRAII CompoundScope(Actions);
2578 FnBody =
2579 Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, std::nullopt, false);
2580 }
2581
2582 BodyScope.Exit();
2583 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
2584}
2585
2586bool Parser::trySkippingFunctionBody() {
2587 assert(SkipFunctionBodies &&
2588 "Should only be called when SkipFunctionBodies is enabled");
2589 if (!PP.isCodeCompletionEnabled()) {
2590 SkipFunctionBody();
2591 return true;
2592 }
2593
2594 // We're in code-completion mode. Skip parsing for all function bodies unless
2595 // the body contains the code-completion point.
2596 TentativeParsingAction PA(*this);
2597 bool IsTryCatch = Tok.is(tok::kw_try);
2598 CachedTokens Toks;
2599 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2600 if (llvm::any_of(Toks, [](const Token &Tok) {
2601 return Tok.is(tok::code_completion);
2602 })) {
2603 PA.Revert();
2604 return false;
2605 }
2606 if (ErrorInPrologue) {
2607 PA.Commit();
2609 return true;
2610 }
2611 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2612 PA.Revert();
2613 return false;
2614 }
2615 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2616 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2617 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2618 PA.Revert();
2619 return false;
2620 }
2621 }
2622 PA.Commit();
2623 return true;
2624}
2625
2626/// ParseCXXTryBlock - Parse a C++ try-block.
2627///
2628/// try-block:
2629/// 'try' compound-statement handler-seq
2630///
2631StmtResult Parser::ParseCXXTryBlock() {
2632 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2633
2634 SourceLocation TryLoc = ConsumeToken();
2635 return ParseCXXTryBlockCommon(TryLoc);
2636}
2637
2638/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2639/// function-try-block.
2640///
2641/// try-block:
2642/// 'try' compound-statement handler-seq
2643///
2644/// function-try-block:
2645/// 'try' ctor-initializer[opt] compound-statement handler-seq
2646///
2647/// handler-seq:
2648/// handler handler-seq[opt]
2649///
2650/// [Borland] try-block:
2651/// 'try' compound-statement seh-except-block
2652/// 'try' compound-statement seh-finally-block
2653///
2654StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
2655 if (Tok.isNot(tok::l_brace))
2656 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
2657
2658 StmtResult TryBlock(ParseCompoundStatement(
2659 /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope |
2661 (FnTry ? Scope::FnTryCatchScope : 0)));
2662 if (TryBlock.isInvalid())
2663 return TryBlock;
2664
2665 // Borland allows SEH-handlers with 'try'
2666
2667 if ((Tok.is(tok::identifier) &&
2668 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2669 Tok.is(tok::kw___finally)) {
2670 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2671 StmtResult Handler;
2672 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
2674 Handler = ParseSEHExceptBlock(Loc);
2675 }
2676 else {
2678 Handler = ParseSEHFinallyBlock(Loc);
2679 }
2680 if(Handler.isInvalid())
2681 return Handler;
2682
2683 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2684 TryLoc,
2685 TryBlock.get(),
2686 Handler.get());
2687 }
2688 else {
2689 StmtVector Handlers;
2690
2691 // C++11 attributes can't appear here, despite this context seeming
2692 // statement-like.
2693 DiagnoseAndSkipCXX11Attributes();
2694
2695 if (Tok.isNot(tok::kw_catch))
2696 return StmtError(Diag(Tok, diag::err_expected_catch));
2697 while (Tok.is(tok::kw_catch)) {
2698 StmtResult Handler(ParseCXXCatchBlock(FnTry));
2699 if (!Handler.isInvalid())
2700 Handlers.push_back(Handler.get());
2701 }
2702 // Don't bother creating the full statement if we don't have any usable
2703 // handlers.
2704 if (Handlers.empty())
2705 return StmtError();
2706
2707 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
2708 }
2709}
2710
2711/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2712///
2713/// handler:
2714/// 'catch' '(' exception-declaration ')' compound-statement
2715///
2716/// exception-declaration:
2717/// attribute-specifier-seq[opt] type-specifier-seq declarator
2718/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2719/// '...'
2720///
2721StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
2722 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2723
2724 SourceLocation CatchLoc = ConsumeToken();
2725
2726 BalancedDelimiterTracker T(*this, tok::l_paren);
2727 if (T.expectAndConsume())
2728 return StmtError();
2729
2730 // C++ 3.3.2p3:
2731 // The name in a catch exception-declaration is local to the handler and
2732 // shall not be redeclared in the outermost block of the handler.
2733 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
2735 (FnCatch ? Scope::FnTryCatchScope : 0));
2736
2737 // exception-declaration is equivalent to '...' or a parameter-declaration
2738 // without default arguments.
2739 Decl *ExceptionDecl = nullptr;
2740 if (Tok.isNot(tok::ellipsis)) {
2741 ParsedAttributes Attributes(AttrFactory);
2742 MaybeParseCXX11Attributes(Attributes);
2743
2744 DeclSpec DS(AttrFactory);
2745
2746 if (ParseCXXTypeSpecifierSeq(DS))
2747 return StmtError();
2748
2749 Declarator ExDecl(DS, Attributes, DeclaratorContext::CXXCatch);
2750 ParseDeclarator(ExDecl);
2751 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
2752 } else
2753 ConsumeToken();
2754
2755 T.consumeClose();
2756 if (T.getCloseLocation().isInvalid())
2757 return StmtError();
2758
2759 if (Tok.isNot(tok::l_brace))
2760 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
2761
2762 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
2763 StmtResult Block(ParseCompoundStatement());
2764 if (Block.isInvalid())
2765 return Block;
2766
2767 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
2768}
2769
2770void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
2771 IfExistsCondition Result;
2772 if (ParseMicrosoftIfExistsCondition(Result))
2773 return;
2774
2775 // Handle dependent statements by parsing the braces as a compound statement.
2776 // This is not the same behavior as Visual C++, which don't treat this as a
2777 // compound statement, but for Clang's type checking we can't have anything
2778 // inside these braces escaping to the surrounding code.
2779 if (Result.Behavior == IEB_Dependent) {
2780 if (!Tok.is(tok::l_brace)) {
2781 Diag(Tok, diag::err_expected) << tok::l_brace;
2782 return;
2783 }
2784
2785 StmtResult Compound = ParseCompoundStatement();
2786 if (Compound.isInvalid())
2787 return;
2788
2789 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2790 Result.IsIfExists,
2791 Result.SS,
2792 Result.Name,
2793 Compound.get());
2794 if (DepResult.isUsable())
2795 Stmts.push_back(DepResult.get());
2796 return;
2797 }
2798
2799 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2800 if (Braces.consumeOpen()) {
2801 Diag(Tok, diag::err_expected) << tok::l_brace;
2802 return;
2803 }
2804
2805 switch (Result.Behavior) {
2806 case IEB_Parse:
2807 // Parse the statements below.
2808 break;
2809
2810 case IEB_Dependent:
2811 llvm_unreachable("Dependent case handled above");
2812
2813 case IEB_Skip:
2814 Braces.skipToEnd();
2815 return;
2816 }
2817
2818 // Condition is true, parse the statements.
2819 while (Tok.isNot(tok::r_brace)) {
2820 StmtResult R =
2821 ParseStatementOrDeclaration(Stmts, ParsedStmtContext::Compound);
2822 if (R.isUsable())
2823 Stmts.push_back(R.get());
2824 }
2825 Braces.consumeClose();
2826}
StringRef P
#define SM(sm)
Definition: Cuda.cpp:83
Expr * E
enum clang::sema::@1651::IndirectLocalPathEntry::EntryKind Kind
static void DiagnoseLabelFollowedByDecl(Parser &P, const Stmt *SubStmt)
Definition: ParseStmt.cpp:730
Defines the PrettyStackTraceEntry class, which is used to make crashes give more contextual informati...
This file declares facilities that support code completion.
SourceLocation Loc
Definition: SemaObjC.cpp:758
This file declares semantic analysis for Objective-C.
This file declares semantic analysis for OpenMP constructs and clauses.
static CharSourceRange getRange(const CharSourceRange &EditRange, const SourceManager &SM, const LangOptions &LangOpts, bool IncludeMacroExpansion)
Definition: SourceCode.cpp:152
Defines the clang::TokenKind enum and support functions.
CanQualType BoolTy
Definition: ASTContext.h:1119
bool isUnset() const
Definition: Ownership.h:167
PtrTy get() const
Definition: Ownership.h:170
bool isInvalid() const
Definition: Ownership.h:166
bool isUsable() const
Definition: Ownership.h:168
Attr - This represents one attribute.
Definition: Attr.h:42
RAII class that helps handle the parsing of an open/close delimiter pair, such as braces { ....
ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and restores it when destroyed.
Base class for callback objects used by Sema::CorrectTypo to check the validity of a potential typo c...
virtual bool ValidateCandidate(const TypoCorrection &candidate)
Simple predicate used by the default RankCandidate to determine whether to return an edit distance of...
Captures information about "declaration specifiers".
Definition: DeclSpec.h:247
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Information about one declarator, including the parsed type information and the identifier.
Definition: DeclSpec.h:1900
RAII object that enters a new expression evaluation context.
This represents one expression.
Definition: Expr.h:110
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:277
ExtensionRAIIObject - This saves the state of extension warnings when constructed and disables them.
Represents a member of a struct/union/class.
Definition: Decl.h:3030
Annotates a diagnostic with some code that should be inserted, removed, or replaced to fix the proble...
Definition: Diagnostic.h:71
static FixItHint CreateReplacement(CharSourceRange RemoveRange, StringRef Code)
Create a code modification hint that replaces the given source range with the given code string.
Definition: Diagnostic.h:134
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:123
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:97
One of these records is kept for each identifier that is lexed.
void setIsPoisoned(bool Value=true)
setIsPoisoned - Mark this identifier as poisoned.
StringRef getName() const
Return the actual identifier string.
Represents the declaration of a label.
Definition: Decl.h:499
@ FEM_Source
Use the declared type for fp arithmetic.
Definition: LangOptions.h:293
Represent a C++ namespace.
Definition: Decl.h:547
RAII object that makes sure paren/bracket/brace count is correct after declaration/statement parsing,...
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
ParsedAttributes - A collection of parsed attributes.
Definition: ParsedAttr.h:958
void takeAllFrom(ParsedAttributes &Other)
Definition: ParsedAttr.h:967
Parser - This implements a parser for the C family of languages.
Definition: Parser.h:58
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID)
Definition: Parser.cpp:81
Sema::FullExprArg FullExprArg
Definition: Parser.h:519
SourceLocation ConsumeToken()
ConsumeToken - Consume the current 'peek token' and lex the next one.
Definition: Parser.h:549
ExprResult ParseCaseExpression(SourceLocation CaseLoc)
Definition: ParseExpr.cpp:252
StmtResult ParseOpenACCDirectiveStmt()
bool TryConsumeToken(tok::TokenKind Expected)
Definition: Parser.h:557
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition: Parser.h:514
Scope * getCurScope() const
Definition: Parser.h:503
bool SkipUntil(tok::TokenKind T, SkipUntilFlags Flags=static_cast< SkipUntilFlags >(0))
SkipUntil - Read tokens until we get to the specified token, then consume it (unless StopBeforeMatch ...
Definition: Parser.h:1295
void SkipMalformedDecl()
SkipMalformedDecl - Read tokens until we get to some likely good stopping point for skipping past a s...
Definition: ParseDecl.cpp:2207
const LangOptions & getLangOpts() const
Definition: Parser.h:496
ExprResult ParseExpression(TypeCastState isTypeCast=NotTypeCast)
Simple precedence-based parser for binary/ternary operators.
Definition: ParseExpr.cpp:132
SmallVector< Stmt *, 32 > StmtVector
A SmallVector of statements.
Definition: Parser.h:522
@ StopBeforeMatch
Stop skipping at specified token, but don't skip the token itself.
Definition: Parser.h:1276
@ StopAtCodeCompletion
Stop at code completion.
Definition: Parser.h:1277
@ StopAtSemi
Stop skipping at semicolon.
Definition: Parser.h:1274
const Token & NextToken()
NextToken - This peeks ahead one token and returns it without consuming it.
Definition: Parser.h:873
An RAII object for [un]poisoning an identifier within a scope.
void enterReturn(Sema &S, SourceLocation Tok)
QualType get(SourceLocation Tok) const
Get the expected type associated with this location, if any.
Definition: Sema.h:349
SourceLocation getLastFPEvalPragmaLocation() const
LangOptions::FPEvalMethodKind getCurrentFPEvalMethod() const
SourceManager & getSourceManager() const
const TargetInfo & getTargetInfo() const
bool isCodeCompletionEnabled() const
Determine if we are performing code completion.
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Computes the source location just past the end of the token at this source location.
PrettyDeclStackTraceEntry - If a crash occurs in the parser while parsing something related to a decl...
If a crash happens while one of these objects are live, the message is printed out along with the spe...
void AddFlags(unsigned Flags)
Sets up the specified scope flags and adjusts the scope state variables accordingly.
Definition: Scope.cpp:115
void decrementMSManglingNumber()
Definition: Scope.h:362
@ SEHTryScope
This scope corresponds to an SEH try.
Definition: Scope.h:125
@ ContinueScope
This is a while, do, for, which can have continue statements embedded into it.
Definition: Scope.h:59
@ ControlScope
The controlling scope in a if/switch/while/for statement.
Definition: Scope.h:66
@ SEHFilterScope
We are currently in the filter expression of an SEH except block.
Definition: Scope.h:131
@ SwitchScope
This is a scope that corresponds to a switch statement.
Definition: Scope.h:102
@ BreakScope
This is a while, do, switch, for, etc that can have break statements embedded into it.
Definition: Scope.h:55
@ CatchScope
This is the scope of a C++ catch statement.
Definition: Scope.h:141
@ CompoundStmtScope
This is a compound statement scope.
Definition: Scope.h:134
@ FnTryCatchScope
This is the scope for a function-level C++ try or catch scope.
Definition: Scope.h:108
@ SEHExceptScope
This scope corresponds to an SEH except.
Definition: Scope.h:128
@ TryScope
This is the scope of a C++ try statement.
Definition: Scope.h:105
@ DeclScope
This is a scope that can contain a declaration.
Definition: Scope.h:63
@ PCC_ForInit
Code completion occurs at the beginning of the initialization statement (or expression) in a for loop...
@ PCC_Expression
Code completion occurs within an expression.
@ PCC_Statement
Code completion occurs within a statement, which may also be an expression or a declaration.
void CodeCompleteExpression(Scope *S, const CodeCompleteExpressionData &Data)
Perform code-completion in an expression context when we know what type we're looking for.
void CodeCompleteAfterIf(Scope *S, bool IsBracedThen)
void CodeCompleteOrdinaryName(Scope *S, ParserCompletionContext CompletionContext)
void CodeCompleteObjCForCollection(Scope *S, DeclGroupPtrTy IterationVar)
StmtResult ActOnObjCForCollectionStmt(SourceLocation ForColLoc, Stmt *First, Expr *collection, SourceLocation RParenLoc)
Definition: SemaObjC.cpp:35
StmtResult FinishObjCForCollectionStmt(Stmt *ForCollection, Stmt *Body)
FinishObjCForCollectionStmt - Attach the body to a objective-C foreach statement.
Definition: SemaObjC.cpp:197
void ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init)
Check if the current region is an OpenMP loop region and if it is, mark loop control variable,...
A RAII object to enter scope of a compound statement.
Definition: Sema.h:1009
bool isInvalid() const
Definition: Sema.h:7335
std::optional< bool > getKnownValue() const
Definition: Sema.h:7340
Records and restores the CurFPFeatures state on entry/exit of compound statements.
Definition: Sema.h:13543
StmtResult ActOnCXXForRangeIdentifier(Scope *S, SourceLocation IdentLoc, IdentifierInfo *Ident, ParsedAttributes &Attrs)
Definition: SemaDecl.cpp:14123
SemaOpenMP & OpenMP()
Definition: Sema.h:1219
StmtResult ActOnSEHLeaveStmt(SourceLocation Loc, Scope *CurScope)
Definition: SemaStmt.cpp:4364
StmtResult ActOnForEachLValueExpr(Expr *E)
In an Objective C collection iteration statement: for (x in y) x can be an arbitrary l-value expressi...
Definition: SemaStmt.cpp:2222
void ActOnForEachDeclStmt(DeclGroupPtrTy Decl)
Definition: SemaStmt.cpp:89
ConditionKind
Definition: Sema.h:7355
@ Boolean
A boolean condition, from 'if', 'while', 'for', or 'do'.
@ Switch
An integral condition for a 'switch' statement.
@ ConstexprIf
A constant boolean condition from 'if constexpr'.
StmtResult ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, LabelDecl *TheDecl)
Definition: SemaStmt.cpp:3126
StmtResult ActOnReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp, Scope *CurScope)
Definition: SemaStmt.cpp:3736
StmtResult ActOnExprStmt(ExprResult Arg, bool DiscardedValue=true)
Definition: SemaStmt.cpp:52
ASTContext & Context
Definition: Sema.h:1002
bool CheckCaseExpression(Expr *E)
Definition: SemaExpr.cpp:20881
SemaObjC & ObjC()
Definition: Sema.h:1204
ASTContext & getASTContext() const
Definition: Sema.h:600
StmtResult ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock, Stmt *Handler)
Definition: SemaStmt.cpp:4299
StmtResult ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch, Stmt *Body)
Definition: SemaStmt.cpp:1245
ConditionResult ActOnCondition(Scope *S, SourceLocation Loc, Expr *SubExpr, ConditionKind CK, bool MissingOK=false)
Definition: SemaExpr.cpp:20141
StmtResult ActOnCoreturnStmt(Scope *S, SourceLocation KwLoc, Expr *E)
StmtResult ActOnWhileStmt(SourceLocation WhileLoc, SourceLocation LParenLoc, ConditionResult Cond, SourceLocation RParenLoc, Stmt *Body)
Definition: SemaStmt.cpp:1719
SemaCodeCompletion & CodeCompletion()
Definition: Sema.h:1159
void ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options=ProcessDeclAttributeOptions())
ProcessDeclAttributeList - Apply all the decl attributes in the specified attribute list to the speci...
StmtResult ActOnExprStmtError()
Definition: SemaStmt.cpp:69
StmtResult ActOnNullStmt(SourceLocation SemiLoc, bool HasLeadingEmptyMacro=false)
Definition: SemaStmt.cpp:74
StmtResult ActOnForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, Stmt *First, ConditionResult Second, FullExprArg Third, SourceLocation RParenLoc, Stmt *Body)
Definition: SemaStmt.cpp:2164
StmtResult ActOnIndirectGotoStmt(SourceLocation GotoLoc, SourceLocation StarLoc, Expr *DestExp)
Definition: SemaStmt.cpp:3141
Decl * ActOnExceptionDeclarator(Scope *S, Declarator &D)
ActOnExceptionDeclarator - Parsed the exception-declarator in a C++ catch handler.
LabelDecl * LookupOrCreateLabel(IdentifierInfo *II, SourceLocation IdentLoc, SourceLocation GnuLabelLoc=SourceLocation())
LookupOrCreateLabel - Do a name lookup of a label with the specified name.
Decl * ActOnFinishFunctionBody(Decl *Decl, Stmt *Body)
Definition: SemaDecl.cpp:15692
StmtResult ActOnCXXForRangeStmt(Scope *S, SourceLocation ForLoc, SourceLocation CoawaitLoc, Stmt *InitStmt, Stmt *LoopVar, SourceLocation ColonLoc, Expr *Collection, SourceLocation RParenLoc, BuildForRangeKind Kind, ArrayRef< MaterializeTemporaryExpr * > LifetimeExtendTemps={})
ActOnCXXForRangeStmt - Check and build a C++11 for-range statement.
Definition: SemaStmt.cpp:2334
void ActOnDefaultCtorInitializers(Decl *CDtorDecl)
DeclGroupPtrTy FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS, ArrayRef< Decl * > Group)
Definition: SemaDecl.cpp:14671
StmtResult ActOnFinishSEHFinallyBlock(SourceLocation Loc, Stmt *Block)
Definition: SemaStmt.cpp:4357
StmtResult ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope)
Definition: SemaStmt.cpp:3181
StmtResult ActOnDoStmt(SourceLocation DoLoc, Stmt *Body, SourceLocation WhileLoc, SourceLocation CondLParen, Expr *Cond, SourceLocation CondRParen)
Definition: SemaStmt.cpp:1740
StmtResult ActOnStartOfSwitchStmt(SourceLocation SwitchLoc, SourceLocation LParenLoc, Stmt *InitStmt, ConditionResult Cond, SourceLocation RParenLoc)
Definition: SemaStmt.cpp:1121
StmtResult ActOnMSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists, CXXScopeSpec &SS, UnqualifiedId &Name, Stmt *Nested)
Definition: SemaStmt.cpp:4387
ExpressionEvaluationContext
Describes how the expressions currently being parsed are evaluated at run-time, if at all.
Definition: Sema.h:6225
@ DiscardedStatement
The current expression occurs within a discarded statement.
@ ImmediateFunctionContext
In addition of being constant evaluated, the current expression occurs in an immediate function conte...
StmtResult ActOnSEHExceptBlock(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
Definition: SemaStmt.cpp:4337
void ActOnAfterCompoundStatementLeadingPragmas()
Definition: SemaStmt.cpp:399
StmtResult ActOnDeclStmt(DeclGroupPtrTy Decl, SourceLocation StartLoc, SourceLocation EndLoc)
Definition: SemaStmt.cpp:79
StmtResult ActOnAttributedStmt(const ParsedAttributes &AttrList, Stmt *SubStmt)
Definition: SemaStmt.cpp:623
StmtResult ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope)
Definition: SemaStmt.cpp:3208
FullExprArg MakeFullDiscardedValueExpr(Expr *Arg)
Definition: Sema.h:7305
void ActOnStartSEHFinallyBlock()
Definition: SemaStmt.cpp:4349
void ActOnAbortSEHFinallyBlock()
Definition: SemaStmt.cpp:4353
@ BFRK_Build
Initial building of a for-range statement.
Definition: Sema.h:10756
StmtResult ActOnCXXCatchBlock(SourceLocation CatchLoc, Decl *ExDecl, Stmt *HandlerBlock)
ActOnCXXCatchBlock - Takes an exception declaration and a handler block and creates a proper catch ha...
Definition: SemaStmt.cpp:4066
void ActOnCaseStmtBody(Stmt *CaseStmt, Stmt *SubStmt)
ActOnCaseStmtBody - This installs a statement as the body of a case.
Definition: SemaStmt.cpp:547
ExprResult ActOnStmtExprResult(ExprResult E)
Definition: SemaExpr.cpp:15665
ExprResult CreateRecoveryExpr(SourceLocation Begin, SourceLocation End, ArrayRef< Expr * > SubExprs, QualType T=QualType())
Attempts to produce a RecoveryExpr after some AST node cannot be created.
Definition: SemaExpr.cpp:20889
StmtResult ActOnIfStmt(SourceLocation IfLoc, IfStatementKind StatementKind, SourceLocation LParenLoc, Stmt *InitStmt, ConditionResult Cond, SourceLocation RParenLoc, Stmt *ThenVal, SourceLocation ElseLoc, Stmt *ElseVal)
Definition: SemaStmt.cpp:908
QualType PreferredConditionType(ConditionKind K) const
Definition: Sema.h:7500
static ConditionResult ConditionError()
Definition: Sema.h:7342
StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, ArrayRef< Stmt * > Elts, bool isStmtExpr)
Definition: SemaStmt.cpp:415
StmtResult ActOnLabelStmt(SourceLocation IdentLoc, LabelDecl *TheDecl, SourceLocation ColonLoc, Stmt *SubStmt)
Definition: SemaStmt.cpp:572
StmtResult ActOnCXXTryBlock(SourceLocation TryLoc, Stmt *TryBlock, ArrayRef< Stmt * > Handlers)
ActOnCXXTryBlock - Takes a try compound-statement and a number of handlers and creates a try statemen...
Definition: SemaStmt.cpp:4186
StmtResult ActOnDefaultStmt(SourceLocation DefaultLoc, SourceLocation ColonLoc, Stmt *SubStmt, Scope *CurScope)
Definition: SemaStmt.cpp:552
StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprResult LHS, SourceLocation DotDotDotLoc, ExprResult RHS, SourceLocation ColonLoc)
Definition: SemaStmt.cpp:516
StmtResult FinishCXXForRangeStmt(Stmt *ForRange, Stmt *Body)
FinishCXXForRangeStmt - Attach the body to a C++0x for-range statement.
Definition: SemaStmt.cpp:3108
ExprResult CorrectDelayedTyposInExpr(Expr *E, VarDecl *InitDecl=nullptr, bool RecoverUncorrectedTypos=false, llvm::function_ref< ExprResult(Expr *)> Filter=[](Expr *E) -> ExprResult { return E;})
Process any TypoExprs in the given Expr and its children, generating diagnostics as appropriate and r...
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
A trivial tuple used to represent a source range.
void setBegin(SourceLocation b)
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:326
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:338
virtual bool supportSourceEvalMethod() const
Definition: TargetInfo.h:822
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:187
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
void setKind(tok::TokenKind K)
Definition: Token.h:95
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition: Token.h:99
tok::TokenKind getKind() const
Definition: Token.h:94
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:276
bool hasLeadingEmptyMacro() const
Return true if this token has an empty macro before it.
Definition: Token.h:299
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:101
bool isNot(tok::TokenKind K) const
Definition: Token.h:100
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:121
void setAnnotationValue(void *val)
Definition: Token.h:238
Simple class containing the result of Sema::CorrectTypo.
DeclClass * getCorrectionDeclAs() const
NestedNameSpecifier * getCorrectionSpecifier() const
Gets the NestedNameSpecifier needed to use the typo correction.
Represents a variable declaration or definition.
Definition: Decl.h:879
Defines the clang::TargetInfo interface.
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
The JSON file list parser is used to communicate input to InstallAPI.
@ OpenCL
Definition: LangStandard.h:66
@ CPlusPlus23
Definition: LangStandard.h:61
@ CPlusPlus20
Definition: LangStandard.h:60
@ CPlusPlus
Definition: LangStandard.h:56
@ CPlusPlus11
Definition: LangStandard.h:57
@ CPlusPlus17
Definition: LangStandard.h:59
IfStatementKind
In an if statement, this denotes whether the statement is a constexpr or consteval if statement.
Definition: Specifiers.h:39
llvm::PointerUnion< Expr *, IdentifierLoc * > ArgsUnion
A union of the various pointer types that can be passed to an ParsedAttr as an argument.
Definition: ParsedAttr.h:113
void takeAndConcatenateAttrs(ParsedAttributes &First, ParsedAttributes &Second, ParsedAttributes &Result)
Consumes the attributes from First and Second and concatenates them into Result.
Definition: ParsedAttr.cpp:314
StmtResult StmtError()
Definition: Ownership.h:265
@ Result
The result type of a method or function.
ActionResult< Stmt * > StmtResult
Definition: Ownership.h:249
const FunctionProtoType * T
StmtResult StmtEmpty()
Definition: Ownership.h:272
@ Braces
New-expression has a C++11 list-initializer.
SourceLocation Loc
Definition: ParsedAttr.h:104
IdentifierInfo * Ident
Definition: ParsedAttr.h:105
Loop optimization hint for loop and unroll pragmas.
Definition: LoopHint.h:20
SourceRange Range
Definition: LoopHint.h:22
IdentifierLoc * OptionLoc
Definition: LoopHint.h:30
IdentifierLoc * StateLoc
Definition: LoopHint.h:33
Expr * ValueExpr
Definition: LoopHint.h:35
IdentifierLoc * PragmaNameLoc
Definition: LoopHint.h:26