usaxena95 updated this revision to Diff 454942.
usaxena95 added a comment.

only allow static members with this.


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,15 @@
   return testDefaultArgForParam() + testDefaultArgForParam((E)1);
 }
 }
+
+namespace GH56246 {
+struct S {
+    static consteval int hello() { 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(); }
+    constexpr int bar() const { return this->hello();}
+} s;
+static_assert(s.bar() == 1);
+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,9 @@
   // 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 +8737,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


Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -908,3 +908,15 @@
   return testDefaultArgForParam() + testDefaultArgForParam((E)1);
 }
 }
+
+namespace GH56246 {
+struct S {
+    static consteval int hello() { 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(); }
+    constexpr int bar() const { return this->hello();}
+} s;
+static_assert(s.bar() == 1);
+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,9 @@
   // 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 +8737,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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to