From 0bcd6c1b18d23e0d47d0a9bbc8084547faa9786f Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Wed, 9 Mar 2016 16:48:08 +0000 Subject: Implement support for [[maybe_unused]] in C++1z that is based off existing support for unused, and treat it as an extension pre-C++1z. This also means extending the existing unused attribute so that it can be placed on an enum and enumerator, in addition to the other subjects. llvm-svn: 263025 --- clang/lib/Sema/SemaAttr.cpp | 3 ++- clang/lib/Sema/SemaDeclAttr.cpp | 24 +++++++++++++++++++++++- clang/lib/Sema/SemaExpr.cpp | 12 ++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) (limited to 'clang/lib/Sema') diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp index 4b7aa2b07e5..7f523c465af 100644 --- a/clang/lib/Sema/SemaAttr.cpp +++ b/clang/lib/Sema/SemaAttr.cpp @@ -459,7 +459,8 @@ void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, if (VD->isUsed()) Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; - VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation())); + VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused, + IdTok.getLocation())); } void Sema::AddCFAuditedAttribute(Decl *D) { diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index f8cec752041..0cae52a299f 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -1807,6 +1807,28 @@ static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { Attr.getAttributeSpellingListIndex())); } +static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { + bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); + + if (IsCXX1zAttr && isa(D)) { + // The C++1z spelling of this attribute cannot be applied to a static data + // member per [dcl.attr.unused]p2. + if (cast(D)->isStaticDataMember()) { + S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) + << Attr.getName() << ExpectedForMaybeUnused; + return; + } + } + + // If this is spelled as the standard C++1z attribute, but not in C++1z, warn + // about using it as an extension. + if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) + S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); + + D->addAttr(::new (S.Context) UnusedAttr( + Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); +} + static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { uint32_t priority = ConstructorAttr::DefaultPriority; if (Attr.getNumArgs() && @@ -5545,7 +5567,7 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, handleSimpleAttribute(S, D, Attr); break; case AttributeList::AT_Unused: - handleSimpleAttribute(S, D, Attr); + handleUnusedAttr(S, D, Attr); break; case AttributeList::AT_ReturnsTwice: handleSimpleAttribute(S, D, Attr); diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 4b8817529e3..54dcced5ba9 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -76,10 +76,14 @@ bool Sema::CanUseDecl(NamedDecl *D) { static void DiagnoseUnusedOfDecl(Sema &S, NamedDecl *D, SourceLocation Loc) { // Warn if this is used but marked unused. - if (D->hasAttr()) { - const Decl *DC = cast_or_null(S.getCurObjCLexicalContext()); - if (DC && !DC->hasAttr()) - S.Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); + if (const auto *A = D->getAttr()) { + // [[maybe_unused]] should not diagnose uses, but __attribute__((unused)) + // should diagnose them. + if (A->getSemanticSpelling() != UnusedAttr::CXX11_maybe_unused) { + const Decl *DC = cast_or_null(S.getCurObjCLexicalContext()); + if (DC && !DC->hasAttr()) + S.Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); + } } } -- cgit v1.2.3