r287085 - Improve handling of __FUNCTION__ and other predefined expression for Objective-C Blocks

Mehdi Amini via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 15 23:07:29 PST 2016


Author: mehdi_amini
Date: Wed Nov 16 01:07:28 2016
New Revision: 287085

URL: http://llvm.org/viewvc/llvm-project?rev=287085&view=rev
Log:
Improve handling of __FUNCTION__ and other predefined expression for Objective-C Blocks

Instead of always displaying the mangled name, try to do better
and get something closer to regular functions.

Recommit r287039 (that was reverted in r287039) with a tweak to
be more generic, and test fixes!

Differential Revision: https://reviews.llvm.org/D26522

Added:
    cfe/trunk/test/CodeGen/block-with-perdefinedexpr.cpp
Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/test/CodeGen/func-in-block.c
    cfe/trunk/test/CodeGen/mangle-blocks.c
    cfe/trunk/test/CodeGenCXX/predefined-expr-cxx14.cpp
    cfe/trunk/test/CodeGenObjC/mangle-blocks.m
    cfe/trunk/test/SemaCXX/predefined-expr.cpp

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Wed Nov 16 01:07:28 2016
@@ -518,20 +518,21 @@ std::string PredefinedExpr::ComputeName(
     }
     return "";
   }
-  if (auto *BD = dyn_cast<BlockDecl>(CurrentDecl)) {
-    std::unique_ptr<MangleContext> MC;
-    MC.reset(Context.createMangleContext());
-    SmallString<256> Buffer;
-    llvm::raw_svector_ostream Out(Buffer);
+  if (isa<BlockDecl>(CurrentDecl)) {
+    // For blocks we only emit something if it is enclosed in a function
+    // For top-level block we'd like to include the name of variable, but we
+    // don't have it at this point.
     auto DC = CurrentDecl->getDeclContext();
     if (DC->isFileContext())
-      MC->mangleGlobalBlock(BD, /*ID*/ nullptr, Out);
-    else if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
-      MC->mangleCtorBlock(CD, /*CT*/ Ctor_Complete, BD, Out);
-    else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
-      MC->mangleDtorBlock(DD, /*DT*/ Dtor_Complete, BD, Out);
-    else
-      MC->mangleBlock(DC, BD, Out);
+      return "";
+
+    SmallString<256> Buffer;
+    llvm::raw_svector_ostream Out(Buffer);
+    if (auto *DCBlock = dyn_cast<BlockDecl>(DC))
+      // For nested blocks, propagate up to the parent.
+      Out << ComputeName(IT, DCBlock);
+    else if (auto *DCDecl = dyn_cast<Decl>(DC))
+      Out << ComputeName(IT, DCDecl) << "_block_invoke";
     return Out.str();
   }
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Wed Nov 16 01:07:28 2016
@@ -2319,9 +2319,19 @@ LValue CodeGenFunction::EmitPredefinedLV
   StringRef NameItems[] = {
       PredefinedExpr::getIdentTypeName(E->getIdentType()), FnName};
   std::string GVName = llvm::join(NameItems, NameItems + 2, ".");
-  if (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)) {
-    auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str());
-    return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
+  if (auto *BD = dyn_cast<BlockDecl>(CurCodeDecl)) {
+    std::string Name = SL->getString();
+    if (!Name.empty()) {
+      unsigned Discriminator =
+          CGM.getCXXABI().getMangleContext().getBlockId(BD, true);
+      if (Discriminator)
+        Name += "_" + Twine(Discriminator + 1).str();
+      auto C = CGM.GetAddrOfConstantCString(Name, GVName.c_str());
+      return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
+    } else {
+      auto C = CGM.GetAddrOfConstantCString(FnName, GVName.c_str());
+      return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);
+    }
   }
   auto C = CGM.GetAddrOfConstantStringFromLiteral(SL, GVName);
   return MakeAddrLValue(C, E->getType(), AlignmentSource::Decl);

Added: cfe/trunk/test/CodeGen/block-with-perdefinedexpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/block-with-perdefinedexpr.cpp?rev=287085&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/block-with-perdefinedexpr.cpp (added)
+++ cfe/trunk/test/CodeGen/block-with-perdefinedexpr.cpp Wed Nov 16 01:07:28 2016
@@ -0,0 +1,87 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - -fblocks -triple x86_64-apple-darwin10 -std=c++11 | FileCheck %s
+
+void bar() {
+  // CHECK-DAG: @__FUNCTION__.___Z3barv_block_invoke = private unnamed_addr constant [17 x i8] c"bar_block_invoke\00", align 1
+  const char * (^block1)() = ^() {
+    return __FUNCTION__;
+  };
+  // CHECK-DAG: @__FUNCTION__.___Z3barv_block_invoke_2 = private unnamed_addr constant [19 x i8] c"bar_block_invoke_2\00", align 1
+  const char * (^block2)() = ^() {
+    return __FUNCTION__;
+  };
+}
+
+void baz() {
+  // CHECK-DAG: @__PRETTY_FUNCTION__.___Z3bazv_block_invoke = private unnamed_addr constant [24 x i8] c"void baz()_block_invoke\00", align 1
+  const char * (^block1)() = ^() {
+    return __PRETTY_FUNCTION__;
+  };
+  // CHECK-DAG: @__PRETTY_FUNCTION__.___Z3bazv_block_invoke_2 = private unnamed_addr constant [26 x i8] c"void baz()_block_invoke_2\00", align 1
+  const char * (^block2)() = ^() {
+    return __PRETTY_FUNCTION__;
+  };
+}
+
+namespace foonamespace {
+class Foo {
+public:
+  Foo() {
+    // CHECK-DAG: @__PRETTY_FUNCTION__.___ZN12foonamespace3FooC2Ev_block_invoke = private unnamed_addr constant [38 x i8] c"foonamespace::Foo::Foo()_block_invoke\00", align 1
+    const char * (^block1)() = ^() {
+      return __PRETTY_FUNCTION__;
+    };
+    // CHECK-DAG: @__PRETTY_FUNCTION__.___ZN12foonamespace3FooC2Ev_block_invoke_2 = private unnamed_addr constant [40 x i8] c"foonamespace::Foo::Foo()_block_invoke_2\00", align 1
+    const char * (^block2)() = ^() {
+      return __PRETTY_FUNCTION__;
+    };
+    // CHECK-DAG: @__func__.___ZN12foonamespace3FooC2Ev_block_invoke_3 = private unnamed_addr constant [19 x i8] c"Foo_block_invoke_3\00", align 1
+    const char * (^block3)() = ^() {
+      return __func__;
+    };
+    bar();
+    inside_lambda();
+  }
+  ~Foo() {
+    // CHECK-DAG: @__func__.___ZN12foonamespace3FooD2Ev_block_invoke = private unnamed_addr constant [18 x i8] c"~Foo_block_invoke\00", align 1
+    const char * (^block1)() = ^() {
+      return __func__;
+    };
+    // CHECK-DAG: @__PRETTY_FUNCTION__.___ZN12foonamespace3FooD2Ev_block_invoke_2 = private unnamed_addr constant [41 x i8] c"foonamespace::Foo::~Foo()_block_invoke_2\00", align 1
+    const char * (^block2)() = ^() {
+      return __PRETTY_FUNCTION__;
+    };
+  }
+  void bar() {
+    // CHECK-DAG: @__PRETTY_FUNCTION__.___ZN12foonamespace3Foo3barEv_block_invoke = private unnamed_addr constant [43 x i8] c"void foonamespace::Foo::bar()_block_invoke\00", align 1
+    const char * (^block1)() = ^() {
+      return __PRETTY_FUNCTION__;
+    };
+    // CHECK-DAG: @__PRETTY_FUNCTION__.___ZN12foonamespace3Foo3barEv_block_invoke_2 = private unnamed_addr constant [45 x i8] c"void foonamespace::Foo::bar()_block_invoke_2\00", align 1
+    const char * (^block2)() = ^() {
+      return __PRETTY_FUNCTION__;
+    };
+    // CHECK-DAG: @__func__.___ZN12foonamespace3Foo3barEv_block_invoke_3 = private unnamed_addr constant [19 x i8] c"bar_block_invoke_3\00", align 1
+    const char * (^block3)() = ^() {
+      return __func__;
+    };
+  }
+  void inside_lambda() {
+    auto lambda = []() {
+      // CHECK-DAG: @__PRETTY_FUNCTION__.___ZZN12foonamespace3Foo13inside_lambdaEvENKUlvE_clEv_block_invoke = private unnamed_addr constant [92 x i8] c"auto foonamespace::Foo::inside_lambda()::(anonymous class)::operator()() const_block_invoke\00", align 1
+      const char * (^block1)() = ^() {
+        return __PRETTY_FUNCTION__;
+      };
+      // CHECK-DAG: 	  @__PRETTY_FUNCTION__.___ZZN12foonamespace3Foo13inside_lambdaEvENKUlvE_clEv_block_invoke_2 = private unnamed_addr constant [94 x i8] c"auto foonamespace::Foo::inside_lambda()::(anonymous class)::operator()() const_block_invoke_2\00", align 1
+      const char * (^block2)() = ^() {
+        return __PRETTY_FUNCTION__;
+      };
+      // CHECK-DAG: @__func__.___ZZN12foonamespace3Foo13inside_lambdaEvENKUlvE_clEv_block_invoke_3 = private unnamed_addr constant [26 x i8] c"operator()_block_invoke_3\00", align 1
+      const char * (^block3)() = ^() {
+        return __func__;
+      };
+    };
+    lambda();
+  }
+};
+Foo f;
+}

Modified: cfe/trunk/test/CodeGen/func-in-block.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/func-in-block.c?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/func-in-block.c (original)
+++ cfe/trunk/test/CodeGen/func-in-block.c Wed Nov 16 01:07:28 2016
@@ -15,5 +15,5 @@ int main()
     return 0; // not reached
 }
 
-// CHECK: @__func__.__main_block_invoke = private unnamed_addr constant [20 x i8] c"__main_block_invoke\00"
-// CHECK: call void @PRINTF({{.*}}@__func__.__main_block_invoke 
+// CHECK: @__func__.__main_block_invoke = private unnamed_addr constant [18 x i8] c"main_block_invoke\00"
+// CHECK: call void @PRINTF({{.*}}@__func__.__main_block_invoke

Modified: cfe/trunk/test/CodeGen/mangle-blocks.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/mangle-blocks.c?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/mangle-blocks.c (original)
+++ cfe/trunk/test/CodeGen/mangle-blocks.c Wed Nov 16 01:07:28 2016
@@ -11,13 +11,13 @@ void (^mangle(void))(void) {
   };
 }
 
-// CHECK: @__func__.__mangle_block_invoke_2 = private unnamed_addr constant [24 x i8] c"__mangle_block_invoke_2\00", align 1
+// CHECK: @__func__.__mangle_block_invoke_2 = private unnamed_addr constant [22 x i8] c"mangle_block_invoke_2\00", align 1
 // CHECK: @.str = private unnamed_addr constant {{.*}}, align 1
 // CHECK: @.str.1 = private unnamed_addr constant [7 x i8] c"mangle\00", align 1
 
 // CHECK: define internal void @__mangle_block_invoke(i8* %.block_descriptor)
 
 // CHECK: define internal void @__mangle_block_invoke_2(i8* %.block_descriptor){{.*}}{
-// CHECK:   call void @__assert_rtn(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @__func__.__mangle_block_invoke_2, i32 0, i32 0), i8* getelementptr inbounds {{.*}}, i32 9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0))
+// CHECK:   call void @__assert_rtn(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @__func__.__mangle_block_invoke_2, i32 0, i32 0), i8* getelementptr inbounds {{.*}}, i32 9, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0))
 // CHECK: }
 

Modified: cfe/trunk/test/CodeGenCXX/predefined-expr-cxx14.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/predefined-expr-cxx14.cpp?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/predefined-expr-cxx14.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/predefined-expr-cxx14.cpp Wed Nov 16 01:07:28 2016
@@ -17,8 +17,8 @@
 // CHECK-DAG: @__func__._ZN24ClassInTopLevelNamespace25topLevelNamespaceFunctionEv = private unnamed_addr constant [26 x i8] c"topLevelNamespaceFunction\00"
 // CHECK-DAG: @__PRETTY_FUNCTION__._ZN24ClassInTopLevelNamespace25topLevelNamespaceFunctionEv = private unnamed_addr constant [60 x i8] c"auto *ClassInTopLevelNamespace::topLevelNamespaceFunction()\00"
 
-// CHECK-DAG: @__func__.___ZN16ClassBlockConstrD2Ev_block_invoke = private unnamed_addr constant [41 x i8] c"___ZN16ClassBlockConstrD2Ev_block_invoke\00"
-// CHECK-DAG: @__func__.___ZN16ClassBlockConstrC2Ev_block_invoke = private unnamed_addr constant [41 x i8] c"___ZN16ClassBlockConstrC2Ev_block_invoke\00"
+// CHECK-DAG: @__func__.___ZN16ClassBlockConstrD2Ev_block_invoke = private unnamed_addr constant [31 x i8] c"~ClassBlockConstr_block_invoke\00"
+// CHECK-DAG: @__func__.___ZN16ClassBlockConstrC2Ev_block_invoke = private unnamed_addr constant [30 x i8] c"ClassBlockConstr_block_invoke\00"
 
 int printf(const char * _Format, ...);
 

Modified: cfe/trunk/test/CodeGenObjC/mangle-blocks.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenObjC/mangle-blocks.m?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenObjC/mangle-blocks.m (original)
+++ cfe/trunk/test/CodeGenObjC/mangle-blocks.m Wed Nov 16 01:07:28 2016
@@ -17,13 +17,12 @@ void __assert_rtn(const char *, const ch
 }
 @end
 
-// CHECK: @"__func__.__14-[Test mangle]_block_invoke_2" = private unnamed_addr constant [34 x i8] c"__14-[Test mangle]_block_invoke_2\00", align 1
+// CHECK: @"__func__.__14-[Test mangle]_block_invoke_2" = private unnamed_addr constant [30 x i8] c"-[Test mangle]_block_invoke_2\00", align 1
 // CHECK: @.str = private unnamed_addr constant {{.*}}, align 1
 // CHECK: @.str.1 = private unnamed_addr constant [7 x i8] c"mangle\00", align 1
 
 // CHECK: define internal void @"__14-[Test mangle]_block_invoke"(i8* %.block_descriptor)
 
 // CHECK: define internal void @"__14-[Test mangle]_block_invoke_2"(i8* %.block_descriptor){{.*}}{
-// CHECK: call void @__assert_rtn(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @"__func__.__14-[Test mangle]_block_invoke_2", i32 0, i32 0), i8* getelementptr inbounds {{.*}}, i32 14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0))
+// CHECK: call void @__assert_rtn(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @"__func__.__14-[Test mangle]_block_invoke_2", i32 0, i32 0), i8* getelementptr inbounds {{.*}}, i32 14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i32 0, i32 0))
 // CHECK: }
-

Modified: cfe/trunk/test/SemaCXX/predefined-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/predefined-expr.cpp?rev=287085&r1=287084&r2=287085&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/predefined-expr.cpp (original)
+++ cfe/trunk/test/SemaCXX/predefined-expr.cpp Wed Nov 16 01:07:28 2016
@@ -33,11 +33,10 @@ int baz() {
   ();
 
   ^{
-    static_assert(sizeof(__func__) == 27, "___Z3bazIiEiv_block_invoke");
-    static_assert(sizeof(__FUNCTION__) == 27, "___Z3bazIiEiv_block_invoke");
-    static_assert(sizeof(__PRETTY_FUNCTION__) == 27, "___Z3bazIiEiv_block_invoke");
-  }
-  ();
+    static_assert(sizeof(__func__) == 17, "baz_block_invoke");
+    static_assert(sizeof(__FUNCTION__) == 17, "baz_block_invoke");
+    static_assert(sizeof(__PRETTY_FUNCTION__) == 33, "int baz() [T = int]_block_invoke");
+  }();
 
   #pragma clang __debug captured
   {
@@ -64,11 +63,10 @@ int main() {
   ();
 
   ^{
-    static_assert(sizeof(__func__) == 20, "__main_block_invoke");
-    static_assert(sizeof(__FUNCTION__) == 20, "__main_block_invoke");
-    static_assert(sizeof(__PRETTY_FUNCTION__) == 20, "__main_block_invoke");
-  }
-  ();
+    static_assert(sizeof(__func__) == 18, "main_block_invoke");
+    static_assert(sizeof(__FUNCTION__) == 18, "main_block_invoke");
+    static_assert(sizeof(__PRETTY_FUNCTION__) == 24, "int main()_block_invoke");
+  }();
 
   #pragma clang __debug captured
   {




More information about the cfe-commits mailing list