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

Chris B via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 15 12:36:45 PST 2025


https://github.com/llvm-beanz created https://github.com/llvm/llvm-project/pull/127346

This PR disallows virtual inheritance and virtual functions in HLSL.

>From e62dc4bfc4f1cff2a624caf70fcc7bb0dc4a6236 Mon Sep 17 00:00:00 2001
From: Chris Bieneman <chris.bieneman at me.com>
Date: Sat, 15 Feb 2025 14:34:05 -0600
Subject: [PATCH] [HLSL] Disallow virtual inheritance and functions

This PR disallows virtual inheritance and virtual functions in HLSL.
---
 clang/include/clang/Basic/DiagnosticParseKinds.td |  2 ++
 clang/lib/Parse/ParseDecl.cpp                     |  7 ++++++-
 clang/lib/Parse/ParseDeclCXX.cpp                  |  3 +++
 clang/test/SemaHLSL/Language/NoVirtual.hlsl       | 14 ++++++++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaHLSL/Language/NoVirtual.hlsl

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index c513dab810d1f..bec3b5d48fd51 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1817,5 +1817,7 @@ 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 7ae136af47391..dfa2dbf5ab61f 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4411,7 +4411,12 @@ void Parser::ParseDeclarationSpecifiers(
         DiagID = diag::err_openclcxx_virtual_function;
         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
         isInvalid = true;
-      } else {
+      } else if (getLangOpts().HLSL) {
+        DiagID = diag::err_hlsl_virtual_function;
+        PrevSpec = Tok.getIdentifierInfo()->getNameStart();
+        isInvalid = true;
+      }
+      else {
         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
       }
       break;
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 43db715ac6d70..dbc6d5f7c43a3 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -2491,6 +2491,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