diff options
author | Takuya Shimizu <shimizu2486@gmail.com> | 2023-09-27 09:26:06 +0900 |
---|---|---|
committer | Takuya Shimizu <shimizu2486@gmail.com> | 2023-09-27 09:26:06 +0900 |
commit | 491b2810fb7fe5f080fa9c4f5945ed0a6909dc92 (patch) | |
tree | be410b7e17f532135322504706990e2e669d659d /clang/lib/Parse/ParseStmt.cpp | |
parent | 7d495d6787170c9882954b19c56de7f5f8bf9b6e (diff) | |
download | llvm-491b2810fb7fe5f080fa9c4f5945ed0a6909dc92.zip llvm-491b2810fb7fe5f080fa9c4f5945ed0a6909dc92.tar.gz llvm-491b2810fb7fe5f080fa9c4f5945ed0a6909dc92.tar.bz2 |
[clang][SemaCXX] Diagnose tautological uses of consteval if and is_constant_evaluated
This patch makes clang diagnose extensive cases of consteval if and is_constant_evaluated usage that are tautologically true or false.
This introduces a new IsRuntimeEvaluated boolean flag to Sema::ExpressionEvaluationContextRecord that means the immediate appearance of if consteval or is_constant_evaluated are tautologically false(e.g. inside if !consteval {} block or non-constexpr-qualified function definition body)
This patch also pushes new expression evaluation context when parsing the condition of if constexpr and initializer of constexpr variables so that Sema can be aware that the use of consteval if and is_consteval are tautologically true in if constexpr condition and constexpr variable initializers.
BEFORE this patch, the warning for is_constant_evaluated was emitted from constant evaluator. This patch moves the warning logic to Sema in order to diagnose tautological use of is_constant_evaluated in the same way as consteval if.
This patch separates initializer evaluation context from InitializerScopeRAII.
This fixes a bug that was happening when user takes address of function address in initializers of non-local variables.
Fixes https://github.com/llvm/llvm-project/issues/43760
Fixes https://github.com/llvm/llvm-project/issues/51567
Reviewed By: cor3ntin, ldionne
Differential Revision: https://reviews.llvm.org/D155064
Diffstat (limited to 'clang/lib/Parse/ParseStmt.cpp')
-rw-r--r-- | clang/lib/Parse/ParseStmt.cpp | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 2531147..75c689f2 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -1293,18 +1293,17 @@ StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { /// errors in the condition. /// Additionally, it will assign the location of the outer-most '(' and ')', /// to LParenLoc and RParenLoc, respectively. -bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt, - Sema::ConditionResult &Cond, - SourceLocation Loc, - Sema::ConditionKind CK, - SourceLocation &LParenLoc, - SourceLocation &RParenLoc) { +bool Parser::ParseParenExprOrCondition( + StmtResult *InitStmt, Sema::ConditionResult &Cond, SourceLocation Loc, + Sema::ConditionKind CK, SourceLocation &LParenLoc, + SourceLocation &RParenLoc, SourceLocation ConstexprLoc) { BalancedDelimiterTracker T(*this, tok::l_paren); T.consumeOpen(); SourceLocation Start = Tok.getLocation(); if (getLangOpts().CPlusPlus) { - Cond = ParseCXXCondition(InitStmt, Loc, CK, false); + Cond = ParseCXXCondition(InitStmt, Loc, CK, false, nullptr, false, + ConstexprLoc); } else { ExprResult CondExpr = ParseExpression(); @@ -1464,12 +1463,13 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { bool IsConsteval = false; SourceLocation NotLocation; SourceLocation ConstevalLoc; + SourceLocation ConstexprLoc; if (Tok.is(tok::kw_constexpr)) { Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if : diag::ext_constexpr_if); IsConstexpr = true; - ConsumeToken(); + ConstexprLoc = ConsumeToken(); } else { if (Tok.is(tok::exclaim)) { NotLocation = ConsumeToken(); @@ -1515,7 +1515,7 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc, IsConstexpr ? Sema::ConditionKind::ConstexprIf : Sema::ConditionKind::Boolean, - LParen, RParen)) + LParen, RParen, ConstexprLoc)) return StmtError(); if (IsConstexpr) @@ -1558,11 +1558,16 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { if (NotLocation.isInvalid() && IsConsteval) { Context = Sema::ExpressionEvaluationContext::ImmediateFunctionContext; ShouldEnter = true; + } else if (NotLocation.isValid() && IsConsteval) { + Context = Actions.ExprEvalContexts.back().Context; + ShouldEnter = true; } EnterExpressionEvaluationContext PotentiallyDiscarded( Actions, Context, nullptr, Sema::ExpressionEvaluationContextRecord::EK_Other, ShouldEnter); + if (NotLocation.isValid() && IsConsteval) + Actions.ExprEvalContexts.back().IsRuntimeEvaluated = true; ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc); } @@ -1603,11 +1608,16 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) { if (NotLocation.isValid() && IsConsteval) { Context = Sema::ExpressionEvaluationContext::ImmediateFunctionContext; ShouldEnter = true; + } else if (NotLocation.isInvalid() && IsConsteval) { + Context = Actions.ExprEvalContexts.back().Context; + ShouldEnter = true; } EnterExpressionEvaluationContext PotentiallyDiscarded( Actions, Context, nullptr, Sema::ExpressionEvaluationContextRecord::EK_Other, ShouldEnter); + if (NotLocation.isInvalid() && IsConsteval) + Actions.ExprEvalContexts.back().IsRuntimeEvaluated = true; ElseStmt = ParseStatement(); if (ElseStmt.isUsable()) |