[cfe-commits] r90344 - in /cfe/trunk: lib/CodeGen/Mangle.cpp test/CodeGenCXX/mangle-extern-local.cpp
Eli Friedman
eli.friedman at gmail.com
Wed Dec 2 12:32:53 PST 2009
Author: efriedma
Date: Wed Dec 2 14:32:49 2009
New Revision: 90344
URL: http://llvm.org/viewvc/llvm-project?rev=90344&view=rev
Log:
Fix for PR5522 and PR5666: fix a bunch of mangling issues with extern variables
and funcctions declared locally within a function.
Added:
cfe/trunk/test/CodeGenCXX/mangle-extern-local.cpp
Modified:
cfe/trunk/lib/CodeGen/Mangle.cpp
Modified: cfe/trunk/lib/CodeGen/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/Mangle.cpp?rev=90344&r1=90343&r2=90344&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/Mangle.cpp (original)
+++ cfe/trunk/lib/CodeGen/Mangle.cpp Wed Dec 2 14:32:49 2009
@@ -89,8 +89,6 @@
void addSubstitution(QualType T);
void addSubstitution(uintptr_t Ptr);
- bool mangleFunctionDecl(const FunctionDecl *FD);
-
void mangleName(const TemplateDecl *TD,
const TemplateArgument *TemplateArgs,
unsigned NumTemplateArgs);
@@ -99,7 +97,7 @@
void mangleUnscopedTemplateName(const TemplateDecl *ND);
void mangleSourceName(const IdentifierInfo *II);
void mangleLocalName(const NamedDecl *ND);
- void mangleNestedName(const NamedDecl *ND);
+ void mangleNestedName(const NamedDecl *ND, const DeclContext *DC);
void mangleNestedName(const TemplateDecl *TD,
const TemplateArgument *TemplateArgs,
unsigned NumTemplateArgs);
@@ -169,11 +167,19 @@
isInExternCSystemHeader(D->getLocation()))
return false;
- // C functions, "main", and variables at global scope are not
- // mangled.
- if ((FD && FD->isMain()) ||
- (!FD && D->getDeclContext()->isTranslationUnit()) ||
- isInCLinkageSpecification(D))
+ // Variables at global scope are not mangled.
+ if (!FD) {
+ const DeclContext *DC = D->getDeclContext();
+ // Check for extern variable declared locally.
+ if (isa<FunctionDecl>(DC) && D->hasLinkage())
+ while (!DC->isNamespace() && !DC->isTranslationUnit())
+ DC = DC->getParent();
+ if (DC->isTranslationUnit())
+ return false;
+ }
+
+ // C functions and "main" are not mangled.
+ if ((FD && FD->isMain()) || isInCLinkageSpecification(D))
return false;
return true;
@@ -278,6 +284,13 @@
// ::= <local-name>
//
const DeclContext *DC = ND->getDeclContext();
+
+ // If this is an extern variable declared locally, the relevant DeclContext
+ // is that of the containing namespace, or the translation unit.
+ if (isa<FunctionDecl>(DC) && ND->hasLinkage())
+ while (!DC->isNamespace() && !DC->isTranslationUnit())
+ DC = DC->getParent();
+
while (isa<LinkageSpecDecl>(DC))
DC = DC->getParent();
@@ -299,7 +312,7 @@
return;
}
- mangleNestedName(ND);
+ mangleNestedName(ND, DC);
}
void CXXNameMangler::mangleName(const TemplateDecl *TD,
const TemplateArgument *TemplateArgs,
@@ -474,7 +487,8 @@
Out << II->getLength() << II->getName();
}
-void CXXNameMangler::mangleNestedName(const NamedDecl *ND) {
+void CXXNameMangler::mangleNestedName(const NamedDecl *ND,
+ const DeclContext *DC) {
// <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
// ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
@@ -488,7 +502,7 @@
mangleTemplatePrefix(TD);
mangleTemplateArgumentList(*TemplateArgs);
} else {
- manglePrefix(ND->getDeclContext());
+ manglePrefix(DC);
mangleUnqualifiedName(ND);
}
Added: cfe/trunk/test/CodeGenCXX/mangle-extern-local.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/mangle-extern-local.cpp?rev=90344&view=auto
==============================================================================
--- cfe/trunk/test/CodeGenCXX/mangle-extern-local.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/mangle-extern-local.cpp Wed Dec 2 14:32:49 2009
@@ -0,0 +1,45 @@
+// RUN: clang-cc %s -emit-llvm -o - | FileCheck %s
+
+// CHECK: @var1 = external global i32
+// CHECK: @_ZN1N4var2E = external global i32
+// CHECK: @var5 = external global i32
+// CHECK: @_ZN1N4var3E = external global i32
+// CHECK: @_ZN1N4var4E = external global i32
+
+// CHECK: declare i32 @_Z5func1v()
+// CHECK: declare i32 @_ZN1N5func2Ev()
+// CHECK: declare i32 @func4()
+// CHECK: declare i32 @_ZN1N5func3Ev()
+
+int f1() {
+ extern int var1, func1();
+ return var1 + func1();
+}
+
+namespace N {
+
+int f2() {
+ extern int var2, func2();
+ return var2 + func2();
+}
+
+struct S {
+ static int f3() {
+ extern int var3, func3();
+ struct LC { int localfunc() { extern int var4; return var4; } };
+ LC localobj;
+ return var3 + func3() + localobj.localfunc();
+ }
+};
+
+int anchorf3() { return S::f3(); }
+
+extern "C" {
+int f4() {
+ extern int var5, func4();
+ return var5 + func4();
+}
+}
+
+}
+
More information about the cfe-commits
mailing list