Skip to content

[clang][bytecode] Properly diagnose non-const reads #106514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2024

Conversation

tbaederr
Copy link
Contributor

If the global variable is constant (but not constexpr), we need to diagnose, but keep evaluating.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

If the global variable is constant (but not constexpr), we need to diagnose, but keep evaluating.


Full diff: https://github.com/llvm/llvm-project/pull/106514.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+39-23)
  • (added) clang/test/AST/ByteCode/cxx11-pedantic.cpp (+13)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 09d3f4525138ed..0cd106b74d1504 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -323,36 +323,52 @@ bool CheckLive(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
 bool CheckConstant(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
   assert(Desc);
 
-  auto IsConstType = [&S](const VarDecl *VD) -> bool {
-    QualType T = VD->getType();
-
-    if (T.isConstant(S.getASTContext()))
-      return true;
-
-    if (S.getLangOpts().CPlusPlus && !S.getLangOpts().CPlusPlus11)
-      return (T->isSignedIntegerOrEnumerationType() ||
-              T->isUnsignedIntegerOrEnumerationType()) &&
-             T.isConstQualified();
+  const auto *D = Desc->asVarDecl();
+  if (!D || !D->hasGlobalStorage())
+    return true;
 
-    if (T.isConstQualified())
-      return true;
+  if (D == S.EvaluatingDecl)
+    return true;
 
-    if (const auto *RT = T->getAs<ReferenceType>())
-      return RT->getPointeeType().isConstQualified();
+  if (D->isConstexpr())
+    return true;
 
-    if (const auto *PT = T->getAs<PointerType>())
-      return PT->getPointeeType().isConstQualified();
+  QualType T = D->getType();
+  bool IsConstant = T.isConstant(S.getASTContext());
+  if (T->isIntegralOrEnumerationType()) {
+    if (!IsConstant) {
+      diagnoseNonConstVariable(S, OpPC, D);
+      return false;
+    }
+    return true;
+  }
 
-    return false;
-  };
+  if (IsConstant) {
+    if (S.getLangOpts().CPlusPlus) {
+      S.CCEDiag(S.Current->getLocation(OpPC),
+                S.getLangOpts().CPlusPlus11
+                    ? diag::note_constexpr_ltor_non_constexpr
+                    : diag::note_constexpr_ltor_non_integral,
+                1)
+          << D << T;
+      S.Note(D->getLocation(), diag::note_declared_at);
+    } else {
+      S.CCEDiag(S.Current->getLocation(OpPC));
+    }
+    return true;
+  }
 
-  if (const auto *D = Desc->asVarDecl();
-      D && D->hasGlobalStorage() && D != S.EvaluatingDecl && !IsConstType(D)) {
-    diagnoseNonConstVariable(S, OpPC, D);
-    return false;
+  if (T->isPointerOrReferenceType()) {
+    if (!T->getPointeeType().isConstant(S.getASTContext()) ||
+        !S.getLangOpts().CPlusPlus11) {
+      diagnoseNonConstVariable(S, OpPC, D);
+      return false;
+    }
+    return true;
   }
 
-  return true;
+  diagnoseNonConstVariable(S, OpPC, D);
+  return false;
 }
 
 static bool CheckConstant(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
diff --git a/clang/test/AST/ByteCode/cxx11-pedantic.cpp b/clang/test/AST/ByteCode/cxx11-pedantic.cpp
new file mode 100644
index 00000000000000..a51061431f87ad
--- /dev/null
+++ b/clang/test/AST/ByteCode/cxx11-pedantic.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected -std=c++11 -pedantic %s
+// RUN: %clang_cc1 -verify=both,ref -std=c++11 -pedantic %s
+
+struct T { int n; };
+const T t = { 42 }; // both-note 2{{declared here}}
+struct S {
+  int m : t.n; // both-warning {{width of bit-field 'm' (42 bits)}} \
+               // both-warning {{expression is not an integral constant expression}} \
+               // both-note {{read of non-constexpr variable 't' is not allowed}}
+};
+
+static_assert(t.n == 42, ""); // both-error {{expression is not an integral constant expression}} \
+                              // both-note {{read of non-constexpr variable 't' is not allowed}}

If the global variable is constant (but not constexpr), we need to
diagnose, but keep evaluating.
@tbaederr tbaederr merged commit cb608cc into llvm:main Aug 29, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants