[PATCH] D132398: Allow constant static members to be used with 'this'
Utkarsh Saxena via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 23 13:45:12 PDT 2022
usaxena95 updated this revision to Diff 454947.
usaxena95 added a comment.
Format.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D132398/new/
https://reviews.llvm.org/D132398
Files:
clang/lib/AST/ExprConstant.cpp
clang/test/SemaCXX/cxx2a-consteval.cpp
Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -908,3 +908,17 @@
return testDefaultArgForParam() + testDefaultArgForParam((E)1);
}
}
+
+namespace GH56246 {
+struct S {
+ static consteval int hello() { return 1;}
+ static constexpr int world() { return 1;}
+
+ consteval int baz() const { return this->hello();}
+ // Previously these two failed because of the use of `this` in the constant expressions.
+ int foo() const { return this->hello() + this->world(); }
+ constexpr int bar() const { return this->hello() + this->world();}
+} s;
+static_assert(s.bar() == 2);
+static_assert(s.baz() == 1);
+}
\ No newline at end of file
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -43,7 +43,9 @@
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/CurrentSourceLocExprScope.h"
+#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
+#include "clang/AST/ExprCXX.h"
#include "clang/AST/OSLog.h"
#include "clang/AST/OptionalDiagnostic.h"
#include "clang/AST/RecordLayout.h"
@@ -926,6 +928,8 @@
/// later on (such as a use of an undefined global).
bool CheckingPotentialConstantExpression = false;
+ /// TODO: add doc.
+ bool AccessingStaticConstantDataMember = false;
/// Whether we're checking for an expression that has undefined behavior.
/// If so, we will produce warnings if we encounter an operation that is
/// always undefined.
@@ -8449,6 +8453,10 @@
// Handle static member functions.
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) {
if (MD->isStatic()) {
+ llvm::SaveAndRestore<bool> StaticMember(
+ Info.AccessingStaticConstantDataMember);
+ if(Info.InConstantContext)
+ Info.AccessingStaticConstantDataMember = true;
VisitIgnoredBaseExpression(E->getBase());
return Success(MD);
}
@@ -8730,6 +8738,9 @@
if (Info.checkingPotentialConstantExpression())
return false;
if (!Info.CurrentCall->This) {
+ if (Info.AccessingStaticConstantDataMember) {
+ return Success(E);
+ }
if (Info.getLangOpts().CPlusPlus11)
Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit();
else
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132398.454947.patch
Type: text/x-patch
Size: 2512 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220823/07a7bc58/attachment-0001.bin>
More information about the cfe-commits
mailing list