From 8681f9d46dc57b2914814009bc8d77f75626a785 Mon Sep 17 00:00:00 2001 From: Kaelyn Uhrain Date: Mon, 12 Nov 2012 23:48:05 +0000 Subject: Enable C++11 attribute syntax for warn_unused_result and allow it to be applied to CXXRecordDecls, where functions with that return type will inherit the warn_unused_result attribute. Also includes a tiny fix (with no discernable behavior change for existing code) to re-sync AttributeDeclKind enum and err_attribute_wrong_decl_type with warn_attribute_wrong_decl_type since the enum is used with both diagnostic messages to chose the correct description. llvm-svn: 167783 --- clang/lib/Sema/SemaDecl.cpp | 8 ++++++++ clang/lib/Sema/SemaDeclAttr.cpp | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'clang/lib/Sema') diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 0092d5dab1f..f2e840494fc 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -5691,6 +5691,14 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, ProcessDeclAttributes(S, NewFD, D, /*NonInheritable=*/false, /*Inheritable=*/true); + QualType RetType = NewFD->getResultType(); + const CXXRecordDecl *Ret = RetType->isRecordType() ? + RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl(); + if (!NewFD->isInvalidDecl() && !NewFD->hasAttr() && + Ret && Ret->hasAttr()) { + NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(), Context)); + } + if (!getLangOpts().CPlusPlus) { // Perform semantic checking on the function declaration. bool isExplicitSpecialization=false; diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index e326a20c87d..70bc019a740 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -37,6 +37,7 @@ enum AttributeDeclKind { ExpectedFunctionOrMethod, ExpectedParameter, ExpectedFunctionMethodOrBlock, + ExpectedFunctionMethodOrClass, ExpectedFunctionMethodOrParameter, ExpectedClass, ExpectedVariable, @@ -44,6 +45,7 @@ enum AttributeDeclKind { ExpectedVariableFunctionOrLabel, ExpectedFieldOrGlobalVar, ExpectedStruct, + ExpectedVariableFunctionOrTag, ExpectedTLSVar }; @@ -2445,7 +2447,7 @@ static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) if (!checkAttributeNumArgs(S, Attr, 0)) return; - if (!isFunction(D) && !isa(D)) { + if (!isFunction(D) && !isa(D) && !isa(D)) { S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << Attr.getName() << ExpectedFunctionOrMethod; return; -- cgit v1.2.3