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