[clang] [HLSL] Disallow virtual inheritance and functions (PR #127346)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 15 12:37:11 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-hlsl
Author: Chris B (llvm-beanz)
<details>
<summary>Changes</summary>
This PR disallows virtual inheritance and virtual functions in HLSL.
---
Full diff: https://github.com/llvm/llvm-project/pull/127346.diff
4 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+2)
- (modified) clang/lib/Parse/ParseDecl.cpp (+6-1)
- (modified) clang/lib/Parse/ParseDeclCXX.cpp (+3)
- (added) clang/test/SemaHLSL/Language/NoVirtual.hlsl (+14)
``````````diff
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'}}
+};
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/127346
More information about the cfe-commits
mailing list