Hello, could somebody please review the attached patch for PR14253?
The equivalent GCC patch has been accepted (GCC bug #55203) and libstdc++ should use the attribute whenever I or somebody write the patch for it. Presumably this Clang patch should be followed by a patch for libc++ (I do not intend to do that). -- Lubos Lunak [email protected]
From 2b51f5ea6a84d6772d3006b86f75b7b574bae662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <[email protected]> Date: Tue, 16 Jul 2013 08:42:24 +0200 Subject: [PATCH] add type attribute warn_unused , allowing to warn about non-POD unused variables --- include/clang/Basic/Attr.td | 4 ++++ lib/Sema/SemaDecl.cpp | 4 ++-- lib/Sema/SemaDeclAttr.cpp | 14 ++++++++++++++ test/SemaCXX/warn-unused-attribute.cpp | 20 ++++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/SemaCXX/warn-unused-attribute.cpp diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index cdb567d..b639c92 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -749,6 +749,10 @@ def VecReturn : InheritableAttr { let Subjects = [CXXRecord]; } +def WarnUnused : InheritableAttr { + let Spellings = [GNU<"warn_unused">, CXX11<"gnu", "warn_unused">]; +} + def WarnUnusedResult : InheritableAttr { let Spellings = [GNU<"warn_unused_result">, CXX11<"clang", "warn_unused_result">, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 38de4db..34c9404 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1320,7 +1320,7 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { return false; if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Tag)) { - if (!RD->hasTrivialDestructor()) + if (!RD->hasTrivialDestructor() && !RD->hasAttr<WarnUnusedAttr>()) return false; if (const Expr *Init = VD->getInit()) { @@ -1330,7 +1330,7 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { dyn_cast<CXXConstructExpr>(Init); if (Construct && !Construct->isElidable()) { CXXConstructorDecl *CD = Construct->getConstructor(); - if (!CD->isTrivial()) + if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>()) return false; } } diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 4c18a33..76d8f2c 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -2669,6 +2669,17 @@ static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { Attr.getAttributeSpellingListIndex())); } +static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { + // check the attribute arguments. + if (!checkAttributeNumArgs(S, Attr, 0)) + return; + + if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) + RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); + else + S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); +} + static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { // check the attribute arguments. if (!checkAttributeNumArgs(S, Attr, 0)) @@ -4895,6 +4906,9 @@ static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, case AttributeList::AT_TypeVisibility: handleVisibilityAttr(S, D, Attr, true); break; + case AttributeList::AT_WarnUnused: + handleWarnUnusedAttr(S, D, Attr); + break; case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); break; case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; diff --git a/test/SemaCXX/warn-unused-attribute.cpp b/test/SemaCXX/warn-unused-attribute.cpp new file mode 100644 index 0000000..72f96ee --- /dev/null +++ b/test/SemaCXX/warn-unused-attribute.cpp @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -verify %s +struct __attribute__((warn_unused)) Test +{ + Test(); + ~Test(); + void use(); +}; + +struct TestNormal +{ + TestNormal(); +}; + +int main() +{ + Test unused; // expected-warning {{unused variable 'unused'}} + Test used; + TestNormal normal; + used.use(); +} -- 1.8.1.4
_______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
