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