r189219 - Handle predefined expression for a captured statement

Wei Pan wei.pan at intel.com
Mon Aug 26 07:27:35 PDT 2013


Author: wpan
Date: Mon Aug 26 09:27:34 2013
New Revision: 189219

URL: http://llvm.org/viewvc/llvm-project?rev=189219&view=rev
Log:
Handle predefined expression for a captured statement

- __func__ or __FUNCTION__ returns captured statement's parent
  function name, not the one compiler generated.

Differential Revision: http://llvm-reviews.chandlerc.com/D1491

Reviewed by bkramer


Modified:
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CodeGen/predefined-expr.c
    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=189219&r1=189218&r2=189219&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Aug 26 09:27:34 2013
@@ -604,6 +604,16 @@ std::string PredefinedExpr::ComputeName(
     Out.flush();
     return Name.str().str();
   }
+  if (const CapturedDecl *CD = dyn_cast<CapturedDecl>(CurrentDecl)) {
+    for (const DeclContext *DC = CD->getParent(); DC; DC = DC->getParent())
+      // Skip to its enclosing function or method, but not its enclosing
+      // CapturedDecl.
+      if (DC->isFunctionOrMethod() && (DC->getDeclKind() != Decl::Captured)) {
+        const Decl *D = Decl::castFromDeclContext(DC);
+        return ComputeName(IT, D);
+      }
+    llvm_unreachable("CapturedDecl not inside a function or method");
+  }
   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
     SmallString<256> Name;
     llvm::raw_svector_ostream Out(Name);

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=189219&r1=189218&r2=189219&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Mon Aug 26 09:27:34 2013
@@ -1972,6 +1972,10 @@ LValue CodeGenFunction::EmitPredefinedLV
       // Blocks use the mangled function name.
       // FIXME: ComputeName should handle blocks.
       FunctionName = FnName.str();
+    } else if (isa<CapturedDecl>(CurDecl)) {
+      // For a captured statement, the function name is its enclosing
+      // function name not the one compiler generated.
+      FunctionName = PredefinedExpr::ComputeName(IdentType, CurDecl);
     } else {
       FunctionName = PredefinedExpr::ComputeName(IdentType, CurDecl);
       assert(cast<ConstantArrayType>(E->getType())->getSize() - 1 ==

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=189219&r1=189218&r2=189219&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Aug 26 09:27:34 2013
@@ -2765,6 +2765,8 @@ ExprResult Sema::ActOnPredefinedExpr(Sou
     currentDecl = BSI->TheDecl;
   else if (const LambdaScopeInfo *LSI = getCurLambda())
     currentDecl = LSI->CallOperator;
+  else if (const CapturedRegionScopeInfo *CSI = getCurCapturedRegion())
+    currentDecl = CSI->TheCapturedDecl;
   else
     currentDecl = getCurFunctionOrMethodDecl();
 

Modified: cfe/trunk/test/CodeGen/predefined-expr.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/predefined-expr.c?rev=189219&r1=189218&r2=189219&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/predefined-expr.c (original)
+++ cfe/trunk/test/CodeGen/predefined-expr.c Mon Aug 26 09:27:34 2013
@@ -6,6 +6,8 @@
 // CHECK: @__PRETTY_FUNCTION__.externFunction = private unnamed_addr constant [22 x i8] c"void externFunction()\00"
 // CHECK: @__func__.privateExternFunction = private unnamed_addr constant [22 x i8] c"privateExternFunction\00"
 // CHECK: @__PRETTY_FUNCTION__.privateExternFunction = private unnamed_addr constant [29 x i8] c"void privateExternFunction()\00"
+// CHECK: @__func__.__captured_stmt = private unnamed_addr constant [25 x i8] c"functionWithCapturedStmt\00"
+// CHECK: @__PRETTY_FUNCTION__.__captured_stmt = private unnamed_addr constant [32 x i8] c"void functionWithCapturedStmt()\00"
 // CHECK: @__func__.staticFunction = private unnamed_addr constant [15 x i8] c"staticFunction\00"
 // CHECK: @__PRETTY_FUNCTION__.staticFunction = private unnamed_addr constant [22 x i8] c"void staticFunction()\00"
 
@@ -29,6 +31,15 @@ __private_extern__ void privateExternFun
   printf("__PRETTY_FUNCTION__ %s\n\n", __PRETTY_FUNCTION__);
 }
 
+void functionWithCapturedStmt() {
+  #pragma clang __debug captured
+  {
+    printf("__func__ %s\n", __func__);
+    printf("__FUNCTION__ %s\n", __FUNCTION__);
+    printf("__PRETTY_FUNCTION__ %s\n\n", __PRETTY_FUNCTION__);
+  }
+}
+
 static void staticFunction() {
   printf("__func__ %s\n", __func__);
   printf("__FUNCTION__ %s\n", __FUNCTION__);
@@ -39,6 +50,7 @@ int main() {
   plainFunction();
   externFunction();
   privateExternFunction();
+  functionWithCapturedStmt();
   staticFunction();
 
   return 0;

Modified: cfe/trunk/test/SemaCXX/predefined-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/predefined-expr.cpp?rev=189219&r1=189218&r2=189219&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/predefined-expr.cpp (original)
+++ cfe/trunk/test/SemaCXX/predefined-expr.cpp Mon Aug 26 09:27:34 2013
@@ -37,4 +37,29 @@ int main() {
     static_assert(sizeof(__PRETTY_FUNCTION__) == 1, "__main_block_invoke");
   }
   ();
+
+  #pragma clang __debug captured
+  {
+    static_assert(sizeof(__func__) == 5, "main");
+    static_assert(sizeof(__FUNCTION__) == 5, "main");
+    static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "int main()");
+
+    #pragma clang __debug captured
+    {
+      static_assert(sizeof(__func__) == 5, "main");
+      static_assert(sizeof(__FUNCTION__) == 5, "main");
+      static_assert(sizeof(__PRETTY_FUNCTION__) == 11, "int main()");
+    }
+  }
+
+  []() {
+    #pragma clang __debug captured
+    {
+      static_assert(sizeof(__func__) == 11, "operator()");
+      static_assert(sizeof(__FUNCTION__) == 11, "operator()");
+      static_assert(sizeof(__PRETTY_FUNCTION__) == 51,
+                    "auto main()::<anonymous class>::operator()() const");
+    }
+  }
+  ();
 }





More information about the cfe-commits mailing list