Skip to content
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 tools/clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2079,6 +2079,10 @@ class FunctionDecl : public DeclaratorDecl, public DeclContext,
/// operators.
bool hasUnusedResultAttr() const;

// HLSL Change Begin - Add support for nodiscard attribute.
Attr *getNoDiscardAttr() const;
// HLSL Change Ends

/// \brief Returns the storage class as written in the source. For the
/// computed linkage of symbol, see getLinkage.
StorageClass getStorageClass() const { return StorageClass(SClass); }
Expand Down
4 changes: 3 additions & 1 deletion tools/clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -2470,13 +2470,15 @@ def WarnUnused : InheritableAttr {
let Documentation = [Undocumented];
}

// HLSL Change Begin - add C++11 spelling.
def WarnUnusedResult : InheritableAttr {
let Spellings = [GCC<"warn_unused_result">,
let Spellings = [CXX11<"", "nodiscard", 2017>, GCC<"warn_unused_result">,
CXX11<"clang", "warn_unused_result">];
let Subjects = SubjectList<[ObjCMethod, CXXRecord, FunctionLike], WarnDiag,
"ExpectedFunctionMethodOrClass">;
let Documentation = [Undocumented];
}
// HLSL Change End - add C++11 spelling.

def Weak : InheritableAttr {
let Spellings = [GCC<"weak">];
Expand Down
8 changes: 5 additions & 3 deletions tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -6368,9 +6368,11 @@ def warn_side_effects_unevaluated_context : Warning<
def warn_side_effects_typeid : Warning<
"expression with side effects will be evaluated despite being used as an "
"operand to 'typeid'">, InGroup<PotentiallyEvaluatedExpression>;
def warn_unused_result : Warning<
"ignoring return value of function declared with warn_unused_result "
"attribute">, InGroup<DiagGroup<"unused-result">>;
// HLSL Change Begin - allow attribute spelling to come in.
def warn_unused_result
: Warning<"ignoring return value of function declared with %0 attribute">,
InGroup<DiagGroup<"unused-result">>;
// HLSL Change End
def warn_unused_volatile : Warning<
"expression result unused; assign into a variable to force a volatile load">,
InGroup<DiagGroup<"unused-volatile-lvalue">>;
Expand Down
14 changes: 14 additions & 0 deletions tools/clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,20 @@ bool FunctionDecl::hasUnusedResultAttr() const {
return hasAttr<WarnUnusedResultAttr>();
}

// HLSL Change Begin - support nodiscard attr.
Attr *FunctionDecl::getNoDiscardAttr() const {
QualType RetType = getReturnType();
if (RetType->isRecordType()) {
const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl();
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(this);
if (Ret && Ret->hasAttr<WarnUnusedResultAttr>() &&
!(MD && MD->getCorrespondingMethodInClass(Ret, true)))
return Ret->getAttr<WarnUnusedResultAttr>();
}
return getAttr<WarnUnusedResultAttr>();
}
// HLSL Change End

/// \brief For an inline function definition in C, or for a gnu_inline function
/// in C++, determine whether the definition will be externally visible.
///
Expand Down
5 changes: 3 additions & 2 deletions tools/clang/lib/Headers/hlsl/dx/linalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ class Matrix {
}

template <typename T>
static typename hlsl::enable_if<hlsl::is_arithmetic<T>::value, Matrix>::type
Splat(T Val) {
[[nodiscard]] static
typename hlsl::enable_if<hlsl::is_arithmetic<T>::value, Matrix>::type
Splat(T Val) {
Matrix Result;
__builtin_LinAlg_FillMatrix(Result.__handle, Val);
return Result;
Expand Down
11 changes: 9 additions & 2 deletions tools/clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
const FunctionDecl *Func = dyn_cast<FunctionDecl>(FD);
if (Func ? Func->hasUnusedResultAttr()
: FD->hasAttr<WarnUnusedResultAttr>()) {
Diag(Loc, diag::warn_unused_result) << R1 << R2;
// HLSL Change Begin - allow attribute spelling to come in.
Attr *NoDiscardAttr = Func ? Func->getNoDiscardAttr()
: FD->getAttr<WarnUnusedResultAttr>();
Diag(Loc, diag::warn_unused_result) << NoDiscardAttr << R1 << R2;
// HLSL Change End
return;
}
if (ShouldSuppress)
Expand All @@ -270,7 +274,10 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
const ObjCMethodDecl *MD = ME->getMethodDecl();
if (MD) {
if (MD->hasAttr<WarnUnusedResultAttr>()) {
Diag(Loc, diag::warn_unused_result) << R1 << R2;
// HLSL Change Begin - allow attribute spelling to come in.
Diag(Loc, diag::warn_unused_result)
<< MD->getAttr<WarnUnusedResultAttr>() << R1 << R2;
// HLSL Change End
return;
}
}
Expand Down
18 changes: 18 additions & 0 deletions tools/clang/test/SemaHLSL/attributes/nodiscard.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %dxc -I %hlsl_headers -T cs_6_10 -verify %s

#include <dx/linalg.h>
using namespace dx::linalg;

using MatrixATy = Matrix<ComponentType::F32, 4, 4, MatrixUse::A, MatrixScope::Wave>;

[nodiscard] int fn() { return 42; }
[[nodiscard]] int fn2() { return 42; }

[numthreads(4, 4, 4)]
void main(uint ID : SV_GroupID)
{
MatrixATy MatA1;
MatA1.Splat(1.0f); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
fn(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
fn2(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
}
Loading