[clang] a7d5b3f - [HLSL] Disallow virtual inheritance and functions (#127346)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 9 10:18:47 PDT 2025


Author: Chris B
Date: 2025-03-09T12:18:44-05:00
New Revision: a7d5b3f711b7c852aa1337e329661dd36025865a

URL: https://github.com/llvm/llvm-project/commit/a7d5b3f711b7c852aa1337e329661dd36025865a
DIFF: https://github.com/llvm/llvm-project/commit/a7d5b3f711b7c852aa1337e329661dd36025865a.diff

LOG: [HLSL] Disallow virtual inheritance and functions (#127346)

This PR disallows virtual inheritance and virtual functions in HLSL.

Added: 
    clang/test/SemaHLSL/Language/NoVirtual.hlsl

Modified: 
    clang/include/clang/Basic/DiagnosticParseKinds.td
    clang/lib/Parse/ParseDecl.cpp
    clang/lib/Parse/ParseDeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index c513dab810d1f..a9a8b272bef15 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1817,5 +1817,9 @@ def ext_hlsl_access_specifiers : ExtWarn<
   InGroup<HLSLExtension>;
 def err_hlsl_unsupported_component : Error<"invalid component '%0' used; expected 'x', 'y', 'z', or 'w'">;
 def err_hlsl_packoffset_invalid_reg : Error<"invalid resource class specifier '%0' for packoffset, expected 'c'">;
+def err_hlsl_virtual_function
+    : Error<"virtual functions are unsupported in HLSL">;
+def err_hlsl_virtual_inheritance
+    : Error<"virtual inheritance is unsupported in HLSL">;
 
 } // end of Parser diagnostics

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 31cb4a24ad97e..82b394d5b4ca6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4411,6 +4411,10 @@ void Parser::ParseDeclarationSpecifiers(
         DiagID = diag::err_openclcxx_virtual_function;
         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
         isInvalid = true;
+      } else if (getLangOpts().HLSL) {
+        DiagID = diag::err_hlsl_virtual_function;
+        PrevSpec = Tok.getIdentifierInfo()->getNameStart();
+        isInvalid = true;
       } else {
         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
       }

diff  --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index e48a35078892d..9384f9ab10af0 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2492,6 +2492,9 @@ BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
     IsVirtual = true;
   }
 
+  if (getLangOpts().HLSL && IsVirtual)
+    Diag(Tok.getLocation(), diag::err_hlsl_virtual_inheritance);
+
   CheckMisplacedCXX11Attribute(Attributes, StartLoc);
 
   // Parse the class-name.

diff  --git a/clang/test/SemaHLSL/Language/NoVirtual.hlsl b/clang/test/SemaHLSL/Language/NoVirtual.hlsl
new file mode 100644
index 0000000000000..8d61bde7d836e
--- /dev/null
+++ b/clang/test/SemaHLSL/Language/NoVirtual.hlsl
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -verify %s
+
+struct Base {
+  int X;
+  void MemberFunction();  // valid
+  virtual void MemberFunction2(); // expected-error{{virtual functions are unsupported in HLSL}}
+};
+
+struct Derived : virtual Base { // expected-error{{virtual inheritance is unsupported in HLSL}}
+  int Y;
+
+  void MemberFunction2() override; // expected-error{{only virtual member functions can be marked 'override'}}
+};
+


        


More information about the cfe-commits mailing list