[cfe-commits] r60564 - in /cfe/trunk: include/clang/AST/Decl.h lib/Sema/Sema.cpp lib/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaStmt.cpp test/Sema/block-misc.c

Chris Lattner sabre at nondot.org
Thu Dec 4 15:50:21 PST 2008


Author: lattner
Date: Thu Dec  4 17:50:19 2008
New Revision: 60564

URL: http://llvm.org/viewvc/llvm-project?rev=60564&view=rev
Log:
change getCurFunctionDecl to skip through Block contexts to find
the containing block.  Introduce a new getCurFunctionOrMethodDecl
method to check to see if we're in a function or objc method.
Minor cleanups to other related places.  This fixes rdar://6405429.


Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/Sema/Sema.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaStmt.cpp
    cfe/trunk/test/Sema/block-misc.c

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Thu Dec  4 17:50:19 2008
@@ -57,8 +57,8 @@
   friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C);
 };
 
-/// NamedDecl - This represents a decl with a name.  Many
-/// decls have names, but not ObjCMethodDecl, @class, etc.
+/// NamedDecl - This represents a decl with a name.  Many decls have names such
+/// as ObjCMethodDecl, but not @class, etc.
 class NamedDecl : public Decl {
   /// Name - The name of this declaration, which is typically a normal
   /// identifier but may also be a special kind of name (C++

Modified: cfe/trunk/lib/Sema/Sema.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.cpp?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.cpp (original)
+++ cfe/trunk/lib/Sema/Sema.cpp Thu Dec  4 17:50:19 2008
@@ -201,9 +201,29 @@
   return PP.getLangOptions();
 }
 
+/// getCurFunctionDecl - If inside of a function body, this returns a pointer
+/// to the function decl for the function being parsed.  If we're currently
+/// in a 'block', this returns the containing context.
+FunctionDecl *Sema::getCurFunctionDecl() {
+  DeclContext *DC = CurContext;
+  while (isa<BlockDecl>(DC))
+    DC = DC->getParent();
+  return dyn_cast<FunctionDecl>(DC);
+}
+
 ObjCMethodDecl *Sema::getCurMethodDecl() {
   DeclContext *DC = CurContext;
   while (isa<BlockDecl>(DC))
     DC = DC->getParent();
   return dyn_cast<ObjCMethodDecl>(DC);
 }
+
+NamedDecl *Sema::getCurFunctionOrMethodDecl() {
+  DeclContext *DC = CurContext;
+  while (isa<BlockDecl>(DC))
+    DC = DC->getParent();
+  if (isa<ObjCMethodDecl>(DC) || isa<FunctionDecl>(DC))
+    return cast<NamedDecl>(DC);
+  return 0;
+}
+

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu Dec  4 17:50:19 2008
@@ -329,16 +329,21 @@
   void PushDeclContext(DeclContext *DC);
   void PopDeclContext();
   
-  /// CurFunctionDecl - If inside of a function body, this returns a pointer to
-  /// the function decl for the function being parsed.
-  FunctionDecl *getCurFunctionDecl() {
-    return dyn_cast<FunctionDecl>(CurContext);
-  }
-
-  /// CurMethodDecl - If inside of a method body, this returns a pointer to
-  /// the method decl for the method being parsed.
+  /// getCurFunctionDecl - If inside of a function body, this returns a pointer
+  /// to the function decl for the function being parsed.  If we're currently
+  /// in a 'block', this returns the containing context.
+  FunctionDecl *getCurFunctionDecl();
+  
+  /// getCurMethodDecl - If inside of a method body, this returns a pointer to
+  /// the method decl for the method being parsed.  If we're currently
+  /// in a 'block', this returns the containing context.
   ObjCMethodDecl *getCurMethodDecl();
 
+  /// getCurFunctionOrMethodDecl - Return the Decl for the current ObjC method
+  /// or C function we're in, otherwise return null.  If we're currently
+  /// in a 'block', this returns the containing context.
+  NamedDecl *getCurFunctionOrMethodDecl();
+
   /// Add this decl to the scope shadowed decl chains.
   void PushOnScopeChains(NamedDecl *D, Scope *S);
 

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Dec  4 17:50:19 2008
@@ -184,8 +184,8 @@
       // FIXME: This isn't correct for methods (results in bogus warning).
       // Get the last formal in the current function.
       const ParmVarDecl *LastArg;
-      if (getCurFunctionDecl())
-        LastArg = *(getCurFunctionDecl()->param_end()-1);
+      if (FunctionDecl *FD = getCurFunctionDecl())
+        LastArg = *(FD->param_end()-1);
       else
         LastArg = *(getCurMethodDecl()->param_end()-1);
       SecondArgIsLastNamedArgument = PV == LastArg;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Dec  4 17:50:19 2008
@@ -505,14 +505,14 @@
   }
 
   // Verify that this is in a function context.
-  if (getCurFunctionDecl() == 0 && getCurMethodDecl() == 0)
+  if (getCurFunctionOrMethodDecl() == 0)
     return Diag(Loc, diag::err_predef_outside_function);
   
   // Pre-defined identifiers are of type char[x], where x is the length of the
   // string.
   unsigned Length;
-  if (getCurFunctionDecl())
-    Length = getCurFunctionDecl()->getIdentifier()->getLength();
+  if (FunctionDecl *FD = getCurFunctionDecl())
+    Length = FD->getIdentifier()->getLength();
   else
     Length = getCurMethodDecl()->getSynthesizedMethodSize();
   
@@ -1438,7 +1438,7 @@
                             DeclarationName()))
     return true;
 
-  bool isFileScope = !getCurFunctionDecl() && !getCurMethodDecl();
+  bool isFileScope = getCurFunctionOrMethodDecl() == 0;
   if (isFileScope) { // 6.5.2.5p3
     if (CheckForConstantInitializer(literalExpr, literalType))
       return true;

Modified: cfe/trunk/lib/Sema/SemaStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmt.cpp?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaStmt.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmt.cpp Thu Dec  4 17:50:19 2008
@@ -750,9 +750,12 @@
   Expr *RetValExp = static_cast<Expr *>(rex);
   if (CurBlock)
     return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
-  QualType FnRetType =
-        getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() : 
-                               getCurMethodDecl()->getResultType();
+  
+  QualType FnRetType;
+  if (FunctionDecl *FD = getCurFunctionDecl())
+    FnRetType = FD->getResultType();
+  else
+    FnRetType = getCurMethodDecl()->getResultType();
 
   if (FnRetType->isVoidType()) {
     if (RetValExp) {// C99 6.8.6.4p1 (ext_ since GCC warns)

Modified: cfe/trunk/test/Sema/block-misc.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-misc.c?rev=60564&r1=60563&r2=60564&view=diff

==============================================================================
--- cfe/trunk/test/Sema/block-misc.c (original)
+++ cfe/trunk/test/Sema/block-misc.c Thu Dec  4 17:50:19 2008
@@ -70,3 +70,10 @@
   bar(^{ test5g = 1; });
 }
 
+// rdar://6405429 - __func__ in a block refers to the containing function name.
+const char*test6() {
+    return ^{
+        return __func__;
+    } ();
+}
+





More information about the cfe-commits mailing list