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