Skip to content

Commit db77c4a

Browse files
committed
Be more careful in applying pragma weak. Fixes pr14974.
GCC applies a pragma weak to a decl if it matches the mangled name. We used to apply if it matched the plain name. This patch is a compromise: we apply the pragma only if it matches the name and the decl has C language linkage. llvm-svn: 176110
1 parent d2686ff commit db77c4a

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/DeclObjC.h"
2020
#include "clang/AST/DeclTemplate.h"
2121
#include "clang/AST/Expr.h"
22+
#include "clang/AST/Mangle.h"
2223
#include "clang/Basic/CharInfo.h"
2324
#include "clang/Basic/SourceManager.h"
2425
#include "clang/Basic/TargetInfo.h"
@@ -5096,11 +5097,18 @@ void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
50965097
if (Inheritable) {
50975098
LoadExternalWeakUndeclaredIdentifiers();
50985099
if (!WeakUndeclaredIdentifiers.empty()) {
5099-
if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
5100+
NamedDecl *ND = NULL;
5101+
if (VarDecl *VD = dyn_cast<VarDecl>(D))
5102+
if (VD->isExternC())
5103+
ND = VD;
5104+
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
5105+
if (FD->isExternC())
5106+
ND = FD;
5107+
if (ND) {
51005108
if (IdentifierInfo *Id = ND->getIdentifier()) {
51015109
llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
51025110
= WeakUndeclaredIdentifiers.find(Id);
5103-
if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
5111+
if (I != WeakUndeclaredIdentifiers.end()) {
51045112
WeakInfo W = I->second;
51055113
DeclApplyPragmaWeak(S, ND, W);
51065114
WeakUndeclaredIdentifiers[Id] = W;

clang/test/CodeGenCXX/pragma-weak.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
2+
3+
#pragma weak zex
4+
int zex;
5+
// GCC produces a weak symbol for this because it matches mangled names.
6+
// Different c++ ABIs may or may not mangle this, so we produce a strong
7+
// symbol.
8+
// CHECK: @zex = global i32
9+
10+
#pragma weak foo
11+
struct S { void foo(); };
12+
void S::foo() {}
13+
// CHECK: define void @_ZN1S3fooEv(
14+
15+
#pragma weak zed
16+
namespace bar { void zed() {} }
17+
// CHECK: define void @_ZN3bar3zedEv(
18+
19+
#pragma weak bah
20+
void bah() {}
21+
// CHECK: define void @_Z3bahv(
22+
23+
#pragma weak baz
24+
extern "C" void baz() {}
25+
// CHECK: define weak void @baz(
26+
27+
#pragma weak _Z3baxv
28+
void bax() {}
29+
// GCC produces a weak symbol for this one, but it doesn't look like a good
30+
// idea to expose the mangling to the pragma unless we really have to.
31+
// CHECK: define void @_Z3baxv(

0 commit comments

Comments
 (0)