[clang] [HLSL] Apply resource attributes to the resource type rather than the handle member (PR #107160)
Justin Bogner via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 5 14:59:06 PDT 2024
================
@@ -559,46 +563,123 @@ void SemaHLSL::handleShaderAttr(Decl *D, const ParsedAttr &AL) {
D->addAttr(NewAttr);
}
-void SemaHLSL::handleResourceClassAttr(Decl *D, const ParsedAttr &AL) {
- if (!AL.isArgIdent(0)) {
- Diag(AL.getLoc(), diag::err_attribute_argument_type)
- << AL << AANT_ArgumentIdentifier;
- return;
- }
+bool clang::CreateHLSLAttributedResourceType(Sema &S, QualType Wrapped,
+ ArrayRef<const Attr *> AttrList,
+ QualType &ResType) {
+ assert(AttrList.size() && "expected list of resource attributes");
- IdentifierLoc *Loc = AL.getArgAsIdent(0);
- StringRef Identifier = Loc->Ident->getName();
- SourceLocation ArgLoc = Loc->Loc;
+ QualType Contained = QualType();
+ HLSLAttributedResourceType::Attributes ResAttrs = {};
- // Validate.
- llvm::dxil::ResourceClass RC;
- if (!HLSLResourceClassAttr::ConvertStrToResourceClass(Identifier, RC)) {
- Diag(ArgLoc, diag::warn_attribute_type_not_supported)
- << "ResourceClass" << Identifier;
- return;
+ bool HasResourceClass = false;
+ for (const Attr *A : AttrList) {
+ if (!A)
+ continue;
+ switch (A->getKind()) {
+ case attr::HLSLResourceClass: {
+ llvm::dxil::ResourceClass RC =
+ dyn_cast<HLSLResourceClassAttr>(A)->getResourceClass();
+ if (!HasResourceClass) {
+ ResAttrs.ResourceClass = RC;
+ HasResourceClass = true;
+ } else {
+ S.Diag(A->getLocation(), ResAttrs.ResourceClass == RC
+ ? diag::warn_duplicate_attribute_exact
+ : diag::warn_duplicate_attribute)
+ << A;
+ return false;
+ }
----------------
bogner wrote:
Checking the positive condition is generally more understandable than the negative. Also, since the diagnostic branch returns early you don't need the else that way:
```suggestion
if (HasResourceClass) {
S.Diag(A->getLocation(), ResAttrs.ResourceClass == RC
? diag::warn_duplicate_attribute_exact
: diag::warn_duplicate_attribute)
<< A;
return false;
}
ResAttrs.ResourceClass = RC;
HasResourceClass = true;
```
https://github.com/llvm/llvm-project/pull/107160
More information about the cfe-commits
mailing list