https://github.com/hekota updated 
https://github.com/llvm/llvm-project/pull/239481

>From b1f37aad8e52a289b6969ecf29e4e38b4506bcc2 Mon Sep 17 00:00:00 2001
From: Helena Kotas <[email protected]>
Date: Thu, 5 Nov 2026 18:58:14 -0800
Subject: [PATCH 1/3] [HLSL] Enable declarations inside cbuffer/tbuffer per
 spec

The cbuffer or tbuffer declaration may contain any declaration
except for a namespace declaration, or a nested cbuffer or
tbuffer declaration.

Fixes #225115
---
 clang/lib/Parse/ParseHLSL.cpp                | 22 ++----
 clang/test/ParserHLSL/decls_inside_cb.hlsl   | 77 ++++++++++++++++++++
 clang/test/ParserHLSL/invalid_inside_cb.hlsl | 20 -----
 clang/test/SemaHLSL/cb_and_namespaces.hlsl   | 60 +++++++++++++++
 4 files changed, 145 insertions(+), 34 deletions(-)
 create mode 100644 clang/test/ParserHLSL/decls_inside_cb.hlsl
 delete mode 100644 clang/test/ParserHLSL/invalid_inside_cb.hlsl
 create mode 100644 clang/test/SemaHLSL/cb_and_namespaces.hlsl

diff --git a/clang/lib/Parse/ParseHLSL.cpp b/clang/lib/Parse/ParseHLSL.cpp
index cf429db80c466d..2f786941e19a88 100644
--- a/clang/lib/Parse/ParseHLSL.cpp
+++ b/clang/lib/Parse/ParseHLSL.cpp
@@ -31,17 +31,11 @@ static bool 
validateDeclsInsideHLSLBuffer(Parser::DeclGroupPtrTy DG,
   // Only allow function, variable, record, and empty decls inside HLSLBuffer.
   for (DeclGroupRef::iterator I = Decls.begin(), E = Decls.end(); I != E; ++I) 
{
     Decl *D = *I;
-    if (isa<CXXRecordDecl, RecordDecl, FunctionDecl, VarDecl, EmptyDecl>(D))
+    if (isa<CXXRecordDecl, RecordDecl, FunctionDecl, VarDecl, EmptyDecl,
+            EnumDecl, UsingDecl, UsingDirectiveDecl, TypeAliasDecl,
+            TemplateDecl, TypedefDecl>(D))
       continue;
 
-    // FIXME: support nested HLSLBuffer and namespace inside HLSLBuffer.
-    if (isa<HLSLBufferDecl, NamespaceDecl>(D)) {
-      P.Diag(D->getLocation(), diag::err_invalid_declaration_in_hlsl_buffer)
-          << IsCBuffer;
-      IsValid = false;
-      continue;
-    }
-
     IsValid = false;
     P.Diag(D->getLocation(), diag::err_invalid_declaration_in_hlsl_buffer)
         << IsCBuffer;
@@ -88,14 +82,14 @@ Decl *Parser::ParseHLSLBuffer(SourceLocation &DeclEnd,
     if (!validateDeclsInsideHLSLBuffer(Result, IdentifierLoc, IsCBuffer,
                                        *this)) {
       T.skipToEnd();
-      DeclEnd = T.getCloseLocation();
-      BufferScope.Exit();
-      Actions.HLSL().ActOnFinishBuffer(D, DeclEnd);
-      return nullptr;
+      D->setInvalidDecl();
+      break;
     }
   }
 
-  T.consumeClose();
+  if (!D->isInvalidDecl())
+    T.consumeClose();
+
   DeclEnd = T.getCloseLocation();
   BufferScope.Exit();
   Actions.HLSL().ActOnFinishBuffer(D, DeclEnd);
diff --git a/clang/test/ParserHLSL/decls_inside_cb.hlsl 
b/clang/test/ParserHLSL/decls_inside_cb.hlsl
new file mode 100644
index 00000000000000..e4807a9a75bcf1
--- /dev/null
+++ b/clang/test/ParserHLSL/decls_inside_cb.hlsl
@@ -0,0 +1,77 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -DCBUFFER -o - 
-fsyntax-only %s -verify
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -o - -fsyntax-only 
%s -verify
+
+// expected-no-diagnostics
+
+namespace Ns1 {
+    class Foo {
+        int a;
+    };
+
+    namespace Ns2 {
+        class Bar {
+            int b;
+        };
+    }
+}
+
+#ifdef CBUFFER
+cbuffer A {
+#else
+tbuffer A {
+#endif
+    // variable declaration
+    float f;
+
+    // class declaration
+    class MyClass;
+    class MyClass {
+        float f;
+    };
+
+    // struct declaration
+    struct MyStruct {
+        float f;
+    };
+    class MyStruct;
+
+    // function declaration
+    float getF();
+    float getF() {
+        return f;
+    }
+
+    // empty declaration
+    ;
+
+    // enumeration declaration
+    enum MyEnum {
+        Value1,
+        Value2
+    };
+
+    // template function declaration
+    template<typename T> T func(T t) { return t; }
+
+    // template struct declaration
+    template<typename T> struct S { float s; };
+
+    // type alias declaration
+    using F32 = float;
+
+    // template type alias declaration
+    template<typename T> using A = S<T>;
+
+    // using namespace declaration
+    using namespace Ns1;
+
+    Foo foo;
+
+    // using declaration
+    using Ns2::Bar;
+
+    Bar bar;
+
+    // typedef declaration
+    typedef float F32Typedef;
+}
diff --git a/clang/test/ParserHLSL/invalid_inside_cb.hlsl 
b/clang/test/ParserHLSL/invalid_inside_cb.hlsl
deleted file mode 100644
index b74021fd22422d..00000000000000
--- a/clang/test/ParserHLSL/invalid_inside_cb.hlsl
+++ /dev/null
@@ -1,20 +0,0 @@
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -o - 
-fsyntax-only %s -verify
-
-// template not allowed inside cbuffer.
-cbuffer A {
-    // expected-error@+2 {{invalid declaration inside cbuffer}}
-    template<typename T>
-    T foo(T t) { return t;}
-}
-
-cbuffer A {
-    // expected-error@+2 {{invalid declaration inside cbuffer}}
-    template<typename T>
-    struct S { float s;};
-}
-
-// typealias not allowed inside cbuffer.
-cbuffer A {
-    // expected-error@+1 {{invalid declaration inside cbuffer}}
-    using F32 = float;
-}
diff --git a/clang/test/SemaHLSL/cb_and_namespaces.hlsl 
b/clang/test/SemaHLSL/cb_and_namespaces.hlsl
new file mode 100644
index 00000000000000..908235205bb44c
--- /dev/null
+++ b/clang/test/SemaHLSL/cb_and_namespaces.hlsl
@@ -0,0 +1,60 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -std=hlsl202x -o - 
-fsyntax-only %s -verify
+
+// This file is based on 
tools/clang/test/SemaHLSL/hlsl/202x-cbuffer-contexts.hlsl
+// from the microsoft/DirectXShaderCompiler repo.
+
+cbuffer A {
+  int Y;
+  // expected-error@+1 {{invalid declaration inside cbuffer}}
+  namespace N {
+  }
+  float4 F5;
+}
+
+cbuffer A {
+  // expected-error@+1 {{invalid declaration inside cbuffer}}
+  cbuffer Nested {
+  }
+}
+
+tbuffer TB {
+  // expected-error@+1 {{invalid declaration inside tbuffer}}
+  namespace NS {}
+}
+
+tbuffer TB2 {
+  // expected-error@+1{{invalid declaration inside tbuffer}}
+  cbuffer CB2 {
+    int X;
+  }
+}
+
+namespace Valid {
+  cbuffer CBValid {
+    int CompletelyFine;
+  }
+
+  tbuffer TBValid {
+    float StillFine;
+  }
+
+  cbuffer GoingOffTheRails {
+    // expected-error@+1{{invalid declaration inside cbuffer}}
+    tbuffer NotCool {
+      ; // even if it is empty...
+    }
+  }
+
+  cbuffer GoingOffTheRailsAgain {
+    // expected-error@+1{{invalid declaration inside cbuffer}}
+    tbuffer StillNotCool {
+      // expected-error@+1{{invalid declaration inside tbuffer}}
+      cbuffer Turtles {
+        // expected-error@+1{{invalid declaration inside cbuffer}}
+        tbuffer AllTheWayDown {
+          ;
+        }
+      }
+    }
+  }
+}

>From ef5b52b40cbfc292384df04f59485e31aef50b4c Mon Sep 17 00:00:00 2001
From: Helena Kotas <[email protected]>
Date: Fri, 6 Nov 2026 16:25:53 -0800
Subject: [PATCH 2/3] Add export and static_assert decls

---
 clang/lib/Parse/ParseHLSL.cpp              |  2 +-
 clang/test/ParserHLSL/decls_inside_cb.hlsl | 10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseHLSL.cpp b/clang/lib/Parse/ParseHLSL.cpp
index 2f786941e19a88..bc6c614e3289f0 100644
--- a/clang/lib/Parse/ParseHLSL.cpp
+++ b/clang/lib/Parse/ParseHLSL.cpp
@@ -33,7 +33,7 @@ static bool 
validateDeclsInsideHLSLBuffer(Parser::DeclGroupPtrTy DG,
     Decl *D = *I;
     if (isa<CXXRecordDecl, RecordDecl, FunctionDecl, VarDecl, EmptyDecl,
             EnumDecl, UsingDecl, UsingDirectiveDecl, TypeAliasDecl,
-            TemplateDecl, TypedefDecl>(D))
+            TemplateDecl, TypedefDecl, ExportDecl, StaticAssertDecl>(D))
       continue;
 
     IsValid = false;
diff --git a/clang/test/ParserHLSL/decls_inside_cb.hlsl 
b/clang/test/ParserHLSL/decls_inside_cb.hlsl
index e4807a9a75bcf1..6010a0fcc2efff 100644
--- a/clang/test/ParserHLSL/decls_inside_cb.hlsl
+++ b/clang/test/ParserHLSL/decls_inside_cb.hlsl
@@ -74,4 +74,14 @@ tbuffer A {
 
     // typedef declaration
     typedef float F32Typedef;
+
+    // export declarations
+    export {
+        void externalFn1();
+    }
+
+    export void externalFn2();
+
+    // static assert declarations
+    static_assert(sizeof(float) == 4, "something's wrong");
 }

>From 04bca5396a95f17780886e388ede69c3c716b05d Mon Sep 17 00:00:00 2001
From: Helena Kotas <[email protected]>
Date: Thu, 19 Nov 2026 16:15:13 -0800
Subject: [PATCH 3/3] add namespace alias decl

---
 clang/lib/Parse/ParseHLSL.cpp              | 3 ++-
 clang/test/ParserHLSL/decls_inside_cb.hlsl | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseHLSL.cpp b/clang/lib/Parse/ParseHLSL.cpp
index bc6c614e3289f0..2c6bbbb9416999 100644
--- a/clang/lib/Parse/ParseHLSL.cpp
+++ b/clang/lib/Parse/ParseHLSL.cpp
@@ -33,7 +33,8 @@ static bool 
validateDeclsInsideHLSLBuffer(Parser::DeclGroupPtrTy DG,
     Decl *D = *I;
     if (isa<CXXRecordDecl, RecordDecl, FunctionDecl, VarDecl, EmptyDecl,
             EnumDecl, UsingDecl, UsingDirectiveDecl, TypeAliasDecl,
-            TemplateDecl, TypedefDecl, ExportDecl, StaticAssertDecl>(D))
+            TemplateDecl, TypedefDecl, ExportDecl, StaticAssertDecl,
+            NamespaceAliasDecl>(D))
       continue;
 
     IsValid = false;
diff --git a/clang/test/ParserHLSL/decls_inside_cb.hlsl 
b/clang/test/ParserHLSL/decls_inside_cb.hlsl
index 6010a0fcc2efff..9d5834ada62ec5 100644
--- a/clang/test/ParserHLSL/decls_inside_cb.hlsl
+++ b/clang/test/ParserHLSL/decls_inside_cb.hlsl
@@ -20,6 +20,7 @@ cbuffer A {
 #else
 tbuffer A {
 #endif
+
     // variable declaration
     float f;
 
@@ -84,4 +85,9 @@ tbuffer A {
 
     // static assert declarations
     static_assert(sizeof(float) == 4, "something's wrong");
+
+    // namespace alias
+    namespace NsAlias = Ns1::Ns2;
+
+    NsAlias::Bar barStruct;
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to