[clang] [HLSL] Enable declarations inside cbuffer/tbuffer per spec (PR #239481)
Helena Kotas via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 22:05:58 PST 2026
https://github.com/hekota created https://github.com/llvm/llvm-project/pull/239481
The cbuffer or tbuffer declaration may contain any declaration except for a namespace declaration, or a nested cbuffer or tbuffer declaration.
Related to Ecma TC57 proposal https://hlsl-tc57.github.io/tc57/proposal/0005/.
Fixes #225115
>From b1f37aad8e52a289b6969ecf29e4e38b4506bcc2 Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Thu, 5 Nov 2026 18:58:14 -0800
Subject: [PATCH] [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 at +2 {{invalid declaration inside cbuffer}}
- template<typename T>
- T foo(T t) { return t;}
-}
-
-cbuffer A {
- // expected-error at +2 {{invalid declaration inside cbuffer}}
- template<typename T>
- struct S { float s;};
-}
-
-// typealias not allowed inside cbuffer.
-cbuffer A {
- // expected-error at +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 at +1 {{invalid declaration inside cbuffer}}
+ namespace N {
+ }
+ float4 F5;
+}
+
+cbuffer A {
+ // expected-error at +1 {{invalid declaration inside cbuffer}}
+ cbuffer Nested {
+ }
+}
+
+tbuffer TB {
+ // expected-error at +1 {{invalid declaration inside tbuffer}}
+ namespace NS {}
+}
+
+tbuffer TB2 {
+ // expected-error at +1{{invalid declaration inside tbuffer}}
+ cbuffer CB2 {
+ int X;
+ }
+}
+
+namespace Valid {
+ cbuffer CBValid {
+ int CompletelyFine;
+ }
+
+ tbuffer TBValid {
+ float StillFine;
+ }
+
+ cbuffer GoingOffTheRails {
+ // expected-error at +1{{invalid declaration inside cbuffer}}
+ tbuffer NotCool {
+ ; // even if it is empty...
+ }
+ }
+
+ cbuffer GoingOffTheRailsAgain {
+ // expected-error at +1{{invalid declaration inside cbuffer}}
+ tbuffer StillNotCool {
+ // expected-error at +1{{invalid declaration inside tbuffer}}
+ cbuffer Turtles {
+ // expected-error at +1{{invalid declaration inside cbuffer}}
+ tbuffer AllTheWayDown {
+ ;
+ }
+ }
+ }
+ }
+}
More information about the cfe-commits
mailing list