Skip to content

[HLSL] Disallow virtual inheritance and functions #127346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make this 1 diagnostic with a selector?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately sharing the diagnostic would require a significant reworking of ParseDecl. The diagnostic for virtual functions is not emitted directly at the place where we encounter the keyword, instead we set the decl to invalid and provide the diagnostic ID. There isn't a way currently to also provide diagnostic arguments.

: Error<"virtual functions are unsupported in HLSL">;
def err_hlsl_virtual_inheritance
: Error<"virtual inheritance is unsupported in HLSL">;

} // end of Parser diagnostics
4 changes: 4 additions & 0 deletions clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaHLSL/Language/NoVirtual.hlsl
Original file line number Diff line number Diff line change
@@ -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'}}
};

Loading