diff options
Diffstat (limited to 'contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp | 83 |
1 files changed, 49 insertions, 34 deletions
diff --git a/contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp b/contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp index d0ff33bd1379..3ac1f0fa27f8 100644 --- a/contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp +++ b/contrib/llvm-project/clang/lib/Parse/ParseStmt.cpp @@ -22,6 +22,9 @@ #include "clang/Sema/DeclSpec.h" #include "clang/Sema/EnterExpressionEvaluationContext.h" #include "clang/Sema/Scope.h" +#include "clang/Sema/SemaCodeCompletion.h" +#include "clang/Sema/SemaObjC.h" +#include "clang/Sema/SemaOpenMP.h" #include "clang/Sema/TypoCorrection.h" #include "llvm/ADT/STLExtras.h" #include <optional> @@ -111,18 +114,21 @@ Parser::ParseStatementOrDeclaration(StmtVector &Stmts, // here because we don't want to allow arbitrary orderings. ParsedAttributes CXX11Attrs(AttrFactory); MaybeParseCXX11Attributes(CXX11Attrs, /*MightBeObjCMessageSend*/ true); - ParsedAttributes GNUAttrs(AttrFactory); + ParsedAttributes GNUOrMSAttrs(AttrFactory); if (getLangOpts().OpenCL) - MaybeParseGNUAttributes(GNUAttrs); + MaybeParseGNUAttributes(GNUOrMSAttrs); + + if (getLangOpts().HLSL) + MaybeParseMicrosoftAttributes(GNUOrMSAttrs); StmtResult Res = ParseStatementOrDeclarationAfterAttributes( - Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs, GNUAttrs); + Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs, GNUOrMSAttrs); MaybeDestroyTemplateIds(); // Attributes that are left should all go on the statement, so concatenate the // two lists. ParsedAttributes Attrs(AttrFactory); - takeAndConcatenateAttrs(CXX11Attrs, GNUAttrs, Attrs); + takeAndConcatenateAttrs(CXX11Attrs, GNUOrMSAttrs, Attrs); assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) && "attributes on empty statement"); @@ -189,7 +195,8 @@ Retry: case tok::code_completion: cutOffParsing(); - Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement); + Actions.CodeCompletion().CodeCompleteOrdinaryName( + getCurScope(), SemaCodeCompletion::PCC_Statement); return StmtError(); case tok::identifier: @@ -235,7 +242,15 @@ Retry: auto IsStmtAttr = [](ParsedAttr &Attr) { return Attr.isStmtAttr(); }; bool AllAttrsAreStmtAttrs = llvm::all_of(CXX11Attrs, IsStmtAttr) && llvm::all_of(GNUAttrs, IsStmtAttr); - if (((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || + // In C, the grammar production for statement (C23 6.8.1p1) does not allow + // for declarations, which is different from C++ (C++23 [stmt.pre]p1). So + // in C++, we always allow a declaration, but in C we need to check whether + // we're in a statement context that allows declarations. e.g., in C, the + // following is invalid: if (1) int x; + if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt || + (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) != + ParsedStmtContext()) && + ((GNUAttributeLoc.isValid() && !(HaveAttrs && AllAttrsAreStmtAttrs)) || isDeclarationStatement())) { SourceLocation DeclStart = Tok.getLocation(), DeclEnd; DeclGroupPtrTy Decl; @@ -559,11 +574,8 @@ StmtResult Parser::ParseExprStatement(ParsedStmtContext StmtCtx) { } Token *CurTok = nullptr; - // If the semicolon is missing at the end of REPL input, consider if - // we want to do value printing. Note this is only enabled in C++ mode - // since part of the implementation requires C++ language features. // Note we shouldn't eat the token since the callback needs it. - if (Tok.is(tok::annot_repl_input_end) && Actions.getLangOpts().CPlusPlus) + if (Tok.is(tok::annot_repl_input_end)) CurTok = &Tok; else // Otherwise, eat the semicolon. @@ -841,7 +853,7 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, if (Tok.is(tok::code_completion)) { cutOffParsing(); - Actions.CodeCompleteCase(getCurScope()); + Actions.CodeCompletion().CodeCompleteCase(getCurScope()); return StmtError(); } @@ -1496,10 +1508,13 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { SourceLocation ConstevalLoc; if (Tok.is(tok::kw_constexpr)) { - Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if - : diag::ext_constexpr_if); - IsConstexpr = true; - ConsumeToken(); + // C23 supports constexpr keyword, but only for object definitions. + if (getLangOpts().CPlusPlus) { + Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if + : diag::ext_constexpr_if); + IsConstexpr = true; + ConsumeToken(); + } } else { if (Tok.is(tok::exclaim)) { NotLocation = ConsumeToken(); @@ -1647,7 +1662,7 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { InnerScope.Exit(); } else if (Tok.is(tok::code_completion)) { cutOffParsing(); - Actions.CodeCompleteAfterIf(getCurScope(), IsBracedThen); + Actions.CodeCompletion().CodeCompleteAfterIf(getCurScope(), IsBracedThen); return StmtError(); } else if (InnerStatementTrailingElseLoc.isValid()) { Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else); @@ -2038,9 +2053,9 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { if (Tok.is(tok::code_completion)) { cutOffParsing(); - Actions.CodeCompleteOrdinaryName(getCurScope(), - C99orCXXorObjC? Sema::PCC_ForInit - : Sema::PCC_Expression); + Actions.CodeCompletion().CodeCompleteOrdinaryName( + getCurScope(), C99orCXXorObjC ? SemaCodeCompletion::PCC_ForInit + : SemaCodeCompletion::PCC_Expression); return StmtError(); } @@ -2115,7 +2130,8 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { if (Tok.is(tok::code_completion)) { cutOffParsing(); - Actions.CodeCompleteObjCForCollection(getCurScope(), DG); + Actions.CodeCompletion().CodeCompleteObjCForCollection(getCurScope(), + DG); return StmtError(); } Collection = ParseExpression(); @@ -2152,7 +2168,8 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { if (Tok.is(tok::code_completion)) { cutOffParsing(); - Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr); + Actions.CodeCompletion().CodeCompleteObjCForCollection(getCurScope(), + nullptr); return StmtError(); } Collection = ParseExpression(); @@ -2288,20 +2305,18 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { ForRangeStmt = Actions.ActOnCXXForRangeStmt( getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(), ForRangeInfo.LoopVar.get(), ForRangeInfo.ColonLoc, CorrectedRange.get(), - T.getCloseLocation(), Sema::BFRK_Build); - - // Similarly, we need to do the semantic analysis for a for-range - // statement immediately in order to close over temporaries correctly. + T.getCloseLocation(), Sema::BFRK_Build, + ForRangeInfo.LifetimeExtendTemps); } else if (ForEach) { - ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc, - FirstPart.get(), - Collection.get(), - T.getCloseLocation()); + // Similarly, we need to do the semantic analysis for a for-range + // statement immediately in order to close over temporaries correctly. + ForEachStmt = Actions.ObjC().ActOnObjCForCollectionStmt( + ForLoc, FirstPart.get(), Collection.get(), T.getCloseLocation()); } else { // In OpenMP loop region loop control variable must be captured and be // private. Perform analysis of first part (if any). if (getLangOpts().OpenMP && FirstPart.isUsable()) { - Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get()); + Actions.OpenMP().ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get()); } } @@ -2344,8 +2359,8 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) { return StmtError(); if (ForEach) - return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(), - Body.get()); + return Actions.ObjC().FinishObjCForCollectionStmt(ForEachStmt.get(), + Body.get()); if (ForRangeInfo.ParsedForRangeDecl()) return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get()); @@ -2431,8 +2446,8 @@ StmtResult Parser::ParseReturnStatement() { // FIXME: Code completion for co_return. if (Tok.is(tok::code_completion) && !IsCoreturn) { cutOffParsing(); - Actions.CodeCompleteExpression(getCurScope(), - PreferredType.get(Tok.getLocation())); + Actions.CodeCompletion().CodeCompleteExpression( + getCurScope(), PreferredType.get(Tok.getLocation())); return StmtError(); } |