r204935 - Handle and warn on aliases to weak aliases.

Rafael Espindola rafael.espindola at gmail.com
Thu Mar 27 08:27:20 PDT 2014


Author: rafael
Date: Thu Mar 27 10:27:20 2014
New Revision: 204935

URL: http://llvm.org/viewvc/llvm-project?rev=204935&view=rev
Log:
Handle and warn on aliases to weak aliases.

This produces valid IR now that llvm rejects aliases to weak aliases and warns
the user that the resolution is not changed if the weak alias is overridden.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/test/CodeGen/alias.c
    cfe/trunk/test/Sema/attr-alias-elf.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=204935&r1=204934&r2=204935&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Mar 27 10:27:20 2014
@@ -2086,6 +2086,9 @@ def err_alias_not_supported_on_darwin :
   "only weak aliases are supported on darwin">;
 def err_alias_to_undefined : Error<
   "alias must point to a defined variable or function">;
+def warn_alias_to_weak_alias : Warning<
+  "alias will always resolve to %0 even if weak definition of alias %1 is overridden">,
+  InGroup<IgnoredAttributes>;
 def err_duplicate_mangled_name : Error<
   "definition with same mangled name as another definition">;
 def err_cyclic_alias : Error<

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=204935&r1=204934&r2=204935&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Mar 27 10:27:20 2014
@@ -208,6 +208,9 @@ void CodeGenModule::applyReplacements()
 }
 
 void CodeGenModule::checkAliases() {
+  // Check if the constructed aliases are well formed. It is really unfortunate
+  // that we have to do this in CodeGen, but we only construct mangled names
+  // and aliases during codegen.
   bool Error = false;
   for (std::vector<GlobalDecl>::iterator I = Aliases.begin(),
          E = Aliases.end(); I != E; ++I) {
@@ -217,7 +220,7 @@ void CodeGenModule::checkAliases() {
     StringRef MangledName = getMangledName(GD);
     llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
     llvm::GlobalAlias *Alias = cast<llvm::GlobalAlias>(Entry);
-    llvm::GlobalValue *GV = Alias->resolveAliasedGlobal(/*stopOnWeak*/ false);
+    llvm::GlobalValue *GV = Alias->getAliasedGlobal();
     if (!GV) {
       Error = true;
       getDiags().Report(AA->getLocation(), diag::err_cyclic_alias);
@@ -225,6 +228,31 @@ void CodeGenModule::checkAliases() {
       Error = true;
       getDiags().Report(AA->getLocation(), diag::err_alias_to_undefined);
     }
+
+    // We have to handle alias to weak aliases in here. LLVM itself disallows
+    // this since the object semantics would not match the IL one. For
+    // compatibility with gcc we implement it by just pointing the alias
+    // to its aliasee's aliasee. We also warn, since the user is probably
+    // expecting the link to be weak.
+    llvm::Constant *Aliasee = Alias->getAliasee();
+    llvm::GlobalValue *AliaseeGV;
+    if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee)) {
+      assert((CE->getOpcode() == llvm::Instruction::BitCast ||
+              CE->getOpcode() == llvm::Instruction::AddrSpaceCast) &&
+             "Unsupported aliasee");
+      AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
+    } else {
+      AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
+    }
+    if (auto GA = dyn_cast<llvm::GlobalAlias>(AliaseeGV)) {
+      if (GA->mayBeOverridden()) {
+        getDiags().Report(AA->getLocation(), diag::warn_alias_to_weak_alias)
+          << GA->getAliasedGlobal()->getName() << GA->getName();
+        Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
+            GA->getAliasee(), Alias->getType());
+        Alias->setAliasee(Aliasee);
+      }
+    }
   }
   if (!Error)
     return;

Modified: cfe/trunk/test/CodeGen/alias.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/alias.c?rev=204935&r1=204934&r2=204935&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/alias.c (original)
+++ cfe/trunk/test/CodeGen/alias.c Thu Mar 27 10:27:20 2014
@@ -14,6 +14,8 @@ void f0(void) { }
 extern void f1(void);
 extern void f1(void) __attribute((alias("f0")));
 // CHECKBASIC-DAG: @f1 = alias void ()* @f0
+// CHECKBASIC-DAG: @test8_foo = alias weak bitcast (void ()* @test8_bar to void (...)*)
+// CHECKBASIC-DAG: @test8_zed = alias bitcast (void ()* @test8_bar to void (...)*)
 // CHECKBASIC: define void @f0() [[NUW:#[0-9]+]] {
 
 // Make sure that aliases cause referenced values to be emitted.
@@ -48,3 +50,7 @@ int outer_weak(int a) { return inner_wea
 // CHECKBASIC: attributes [[NUW]] = { nounwind{{.*}} }
 
 // CHECKCC: attributes [[NUW]] = { nounwind{{.*}} }
+
+void test8_bar() {}
+void test8_foo() __attribute__((weak, alias("test8_bar")));
+void test8_zed() __attribute__((alias("test8_foo")));

Modified: cfe/trunk/test/Sema/attr-alias-elf.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-alias-elf.c?rev=204935&r1=204934&r2=204935&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-alias-elf.c (original)
+++ cfe/trunk/test/Sema/attr-alias-elf.c Thu Mar 27 10:27:20 2014
@@ -52,3 +52,7 @@ extern int b3;
 
 extern int a4 __attribute__((alias("b4"))); // expected-error {{alias must point to a defined variable or function}}
 typedef int b4;
+
+void test2_bar() {}
+void test2_foo() __attribute__((weak, alias("test2_bar")));
+void test2_zed() __attribute__((alias("test2_foo"))); // expected-warning {{alias will always resolve to test2_bar even if weak definition of alias test2_foo is overridden}}





More information about the cfe-commits mailing list