[PATCH] D52344: [Clang][CodeGen][ObjC]: Fix non-bridged CoreFoundation builds on ELF targets that use `-fconstant-cfstrings`.

Kristina Brooks via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 21 02:32:11 PDT 2018


kristina created this revision.
kristina added reviewers: rnk, echristo, clang, rjmccall.
Herald added a subscriber: cfe-commits.

Attempt to unbreak behavior caused by https://reviews.llvm.org/D44491 resulting in inability to build standalone/unbridged CoreFoundation on an ELF target with `-fconstant-cfstrings` as the cast<> expression would fail when attempting to use the builtin since the faux-ObjectiveC class backing CFSTR constant strings is an `llvm::Constant` being cast to an `llvm::GlobalValue`. This only affects ELF targets, PE/COFF targets will still follow the same path so this should not affect them and takes an approach more in-line with how Apple toolchains behave.

I will add a lit test for Clang codegen shortly after I figure out how to get it to work correctly, though with said patch I'm able to build standalone CoreFoundation with constant CFStrings without it being bridged against Foundation or Swift runtimes (as far as I know that was previously only supported on Windows).

Trying to make a similar test to cfstring-windows.c but still trying to get the hang of FileCheck.

  ********************
  Testing Time: 10.56s
  ********************
  Failing Tests (1):
      Clang :: CodeGen/cfstring-linux-unbridged.c


Repository:
  rC Clang

https://reviews.llvm.org/D52344

Files:
  lib/CodeGen/CodeGenModule.cpp


Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -4093,15 +4093,20 @@
 
   llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
   llvm::Constant *Zeros[] = { Zero, Zero };
-
+  
   // If we don't already have it, get __CFConstantStringClassReference.
   if (!CFConstantStringClassRef) {
     llvm::Type *Ty = getTypes().ConvertType(getContext().IntTy);
     Ty = llvm::ArrayType::get(Ty, 0);
-    llvm::GlobalValue *GV = cast<llvm::GlobalValue>(
-        CreateRuntimeVariable(Ty, "__CFConstantStringClassReference"));
-
+    llvm::Constant *CGV =
+        CreateRuntimeVariable(Ty, "__CFConstantStringClassReference");
+    llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(CGV);
+    
     if (getTriple().isOSBinFormatCOFF()) {
+      // Instead of using a cast<>, just assert if it's null, this is
+      // consistent with old behavior for this target as it would fail
+      // on the cast<> instead.
+      assert(GV && "isa<CFConstantStringClassReference> isn't a GlobalValue");
       IdentifierInfo &II = getContext().Idents.get(GV->getName());
       TranslationUnitDecl *TUDecl = getContext().getTranslationUnitDecl();
       DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
@@ -4119,11 +4124,12 @@
         GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
       }
     }
-    setDSOLocal(GV);
+    if (GV)
+      setDSOLocal(GV);
 
     // Decay array -> ptr
     CFConstantStringClassRef =
-        llvm::ConstantExpr::getGetElementPtr(Ty, GV, Zeros);
+        llvm::ConstantExpr::getGetElementPtr(Ty, CGV, Zeros);
   }
 
   QualType CFTy = getContext().getCFConstantStringType();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52344.166414.patch
Type: text/x-patch
Size: 1755 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180921/2175005f/attachment.bin>


More information about the cfe-commits mailing list