Skip to content

[clang][bytecode] Restructure Program::CurrentDeclaration handling #127456

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
Feb 17, 2025

Conversation

tbaederr
Copy link
Contributor

Properly reset to the last ID and return the current ID from getCurrentDecl().

Properly reset to the last ID and return the current ID from
getCurrentDecl().
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 17, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Properly reset to the last ID and return the current ID from getCurrentDecl().


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

3 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+1-1)
  • (modified) clang/lib/AST/ByteCode/Program.h (+8-15)
  • (added) clang/test/AST/ByteCode/libcxx/global-decl-id.cpp (+22)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 59c236c9da8c8..b3a81f8ff1516 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -29,7 +29,7 @@ namespace interp {
 template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
 public:
   DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
-      : LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
+      : LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P),
         OldInitializingDecl(Ctx->InitializingDecl) {
     Ctx->InitializingDecl = VD;
     Ctx->InitStack.push_back(InitLink::Decl(VD));
diff --git a/clang/lib/AST/ByteCode/Program.h b/clang/lib/AST/ByteCode/Program.h
index c9c3d20f198c6..d503652abb96f 100644
--- a/clang/lib/AST/ByteCode/Program.h
+++ b/clang/lib/AST/ByteCode/Program.h
@@ -132,20 +132,22 @@ class Program final {
   /// Context to manage declaration lifetimes.
   class DeclScope {
   public:
-    DeclScope(Program &P, const ValueDecl *VD) : P(P) {
-      P.startDeclaration(VD);
+    DeclScope(Program &P) : P(P), PrevDecl(P.CurrentDeclaration) {
+      ++P.LastDeclaration;
+      P.CurrentDeclaration = P.LastDeclaration;
     }
-    ~DeclScope() { P.endDeclaration(); }
+    ~DeclScope() { P.CurrentDeclaration = PrevDecl; }
 
   private:
     Program &P;
+    unsigned PrevDecl;
   };
 
   /// Returns the current declaration ID.
   std::optional<unsigned> getCurrentDecl() const {
     if (CurrentDeclaration == NoDeclaration)
-      return std::optional<unsigned>{};
-    return LastDeclaration;
+      return std::nullopt;
+    return CurrentDeclaration;
   }
 
 private:
@@ -218,21 +220,12 @@ class Program final {
   }
 
   /// No declaration ID.
-  static constexpr unsigned NoDeclaration = (unsigned)-1;
+  static constexpr unsigned NoDeclaration = ~0u;
   /// Last declaration ID.
   unsigned LastDeclaration = 0;
   /// Current declaration ID.
   unsigned CurrentDeclaration = NoDeclaration;
 
-  /// Starts evaluating a declaration.
-  void startDeclaration(const ValueDecl *Decl) {
-    LastDeclaration += 1;
-    CurrentDeclaration = LastDeclaration;
-  }
-
-  /// Ends a global declaration.
-  void endDeclaration() { CurrentDeclaration = NoDeclaration; }
-
 public:
   /// Dumps the disassembled bytecode to \c llvm::errs().
   void dump() const;
diff --git a/clang/test/AST/ByteCode/libcxx/global-decl-id.cpp b/clang/test/AST/ByteCode/libcxx/global-decl-id.cpp
new file mode 100644
index 0000000000000..0dd583c3d467f
--- /dev/null
+++ b/clang/test/AST/ByteCode/libcxx/global-decl-id.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -std=c++2c  -verify=ref,both %s
+
+// both-no-diagnostics
+
+namespace std {
+constexpr int
+midpoint(int __a, int ) {
+  constexpr unsigned  __half_diff = 0;
+  return __half_diff;
+}
+}
+struct Tuple {
+  int min;
+  int mid;
+  constexpr Tuple() {
+    min = 0;
+    mid = std::midpoint(min, min);
+  }
+};
+constexpr Tuple tup;
+

@tbaederr tbaederr merged commit f09fd94 into llvm:main Feb 17, 2025
11 checks passed
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
…lvm#127456)

Properly reset to the last ID and return the current ID from
getCurrentDecl().
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