From: Alexander Musman Date: Fri, 18 Sep 2015 07:40:22 +0000 (+0000) Subject: Fix for assertion fail for pragma weak on typedef. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=729bae0b12a7a3bfadd84602daeebdc24de00279;p=clang Fix for assertion fail for pragma weak on typedef. Example: typedef int __td3; #pragma weak td3 = __td3 Differential Revision: http://reviews.llvm.org/D12904 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@247975 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/Sema.cpp b/lib/Sema/Sema.cpp index cc509566bd..300e2f040a 100644 --- a/lib/Sema/Sema.cpp +++ b/lib/Sema/Sema.cpp @@ -727,8 +727,15 @@ void Sema::ActOnEndOfTranslationUnit() { if (WeakID.second.getUsed()) continue; - Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) - << WeakID.first; + Decl *PrevDecl = LookupSingleName(TUScope, WeakID.first, SourceLocation(), + LookupOrdinaryName); + if (PrevDecl != nullptr && + !(isa(PrevDecl) || isa(PrevDecl))) + Diag(WeakID.second.getLocation(), diag::warn_attribute_wrong_decl_type) + << "'weak'" << ExpectedVariableOrFunction; + else + Diag(WeakID.second.getLocation(), diag::warn_weak_identifier_undeclared) + << WeakID.first; } if (LangOpts.CPlusPlus11 && diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 2ed4a61aab..701d8c7290 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -14530,7 +14530,7 @@ void Sema::ActOnPragmaWeakAlias(IdentifierInfo* Name, LookupOrdinaryName); WeakInfo W = WeakInfo(Name, NameLoc); - if (PrevDecl) { + if (PrevDecl && (isa(PrevDecl) || isa(PrevDecl))) { if (!PrevDecl->hasAttr()) if (NamedDecl *ND = dyn_cast(PrevDecl)) DeclApplyPragmaWeak(TUScope, ND, W); diff --git a/test/CodeGen/pragma-weak.c b/test/CodeGen/pragma-weak.c index 063dec30ac..36abca5de3 100644 --- a/test/CodeGen/pragma-weak.c +++ b/test/CodeGen/pragma-weak.c @@ -53,12 +53,14 @@ void __foo2(void) {} #pragma weak unused // expected-warning {{weak identifier 'unused' never declared}} #pragma weak unused_alias = __unused_alias // expected-warning {{weak identifier '__unused_alias' never declared}} -#pragma weak td // expected-warning {{weak identifier 'td' never declared}} +#pragma weak td // expected-warning {{'weak' attribute only applies to variables and functions}} typedef int td; -#pragma weak td2 = __td2 // expected-warning {{weak identifier '__td2' never declared}} +#pragma weak td2 = __td2 // expected-warning {{'weak' attribute only applies to variables and functions}} typedef int __td2; +typedef int __td3; +#pragma weak td3 = __td3 // expected-warning {{'weak' attribute only applies to variables and functions}} ///// test weird cases