[cfe-commits] r51584 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/Sema/SemaDecl.cpp test/CodeGen/func-decl-cleanup.c

Eli Friedman eli.friedman at gmail.com
Mon May 26 22:07:37 PDT 2008


Author: efriedma
Date: Tue May 27 00:07:37 2008
New Revision: 51584

URL: http://llvm.org/viewvc/llvm-project?rev=51584&view=rev
Log:
Don't swap function decls, and add them to the scope as they are 
encountered.  Mixing up the decls is unintuitive, and confuses the AST 
destruction code. Fixes PR2360.

Note that there is a need to look up the characteristics and 
declarations of a function associated with a particular name or decl, 
but the original swapping code doesn't solve it properly. 
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-May/001644.html is one 
suggestion for how to fix that.


Added:
    cfe/trunk/test/CodeGen/func-decl-cleanup.c
Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Tue May 27 00:07:37 2008
@@ -446,6 +446,10 @@
     return PreviousDeclaration;
   }
 
+  void setPreviousDeclaration(FunctionDecl * PrevDecl) {
+    PreviousDeclaration = PrevDecl;
+  }
+
   // Iterator access to formal parameters.
   unsigned param_size() const { return getNumParams(); }
   typedef ParmVarDecl **param_iterator;
@@ -492,10 +496,6 @@
   }
   StorageClass getStorageClass() const { return StorageClass(SClass); }
   bool isInline() const { return IsInline; }
-   
-  /// AddRedeclaration - Adds the function declaration FD as a
-  /// redeclaration of this function.
-  void AddRedeclaration(FunctionDecl *FD);
  
   // Implement isa/cast/dyncast/etc.
   static bool classof(const Decl *D) { return D->getKind() == Function; }

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=51584&r1=51583&r2=51584&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Tue May 27 00:07:37 2008
@@ -502,63 +502,6 @@
   return NumRequiredArgs;
 }
 
-/// AddRedeclaration - Specifies that this function declaration has been
-/// redeclared by the function declaration FD. FD must be a
-/// redeclaration of this based on the semantics of the language being
-/// translated ("compatible" function types in C, same signatures in
-/// C++). 
-void FunctionDecl::AddRedeclaration(FunctionDecl *FD) {
-  assert(FD->PreviousDeclaration == 0 && 
-         "Redeclaration already has a previous declaration!");
-
-  // Insert FD into the list of previous declarations of this
-  // function.
-  FD->PreviousDeclaration = this->PreviousDeclaration;
-  this->PreviousDeclaration = FD;
-
-  // Swap the contents of this function declaration and FD. This
-  // effectively transforms the original declaration into the most
-  // recent declaration, so that all references to this declaration
-  // remain valid (and have information from *all* declarations),
-  // while retaining all of the information about previous
-  // declarations as well.
-
-  // Swap parameters, so that the most recent parameter names and
-  // exact types (e.g., enum vs int) show up in the original
-  // declaration.
-  std::swap(this->ParamInfo, FD->ParamInfo);
-  
-  // Swap the function body: all declarations share the same function
-  // body, but we keep track of who actually defined that function
-  // body by keeping the pointer to the body stored in that node.
-  std::swap(this->Body, FD->Body);
-
-  // Swap type information: this is important because in C, later
-  // declarations can provide slightly different types (enum vs. int,
-  // for example).
-  QualType thisType = this->getType();
-  this->setType(FD->getType());
-  FD->setType(thisType);
-
-  // Swap location information: this allows us to produce diagnostics
-  // later on that reference the most recent declaration (which has
-  // the most information!) while retaining the location of previous
-  // declarations (good for "redefinition" diagnostics).
-  SourceLocation thisLocation = this->getLocation();
-  this->setLocation(FD->getLocation());
-  FD->setLocation(thisLocation);
-  
-  // Swap attributes. FD will have the union of the attributes from
-  // all previous declarations.
-  this->swapAttrs(FD);
-
-  // If any declaration is inline, the function is inline.
-  this->IsInline |= FD->IsInline;
-
-  // FIXME: Is this the right way to handle storage specifiers?
-  if (FD->SClass) this->SClass = FD->SClass;
-}
-
 //===----------------------------------------------------------------------===//
 // RecordDecl Implementation
 //===----------------------------------------------------------------------===//

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue May 27 00:07:37 2008
@@ -910,13 +910,7 @@
       NewFD = MergeFunctionDecl(NewFD, PrevDecl, Redeclaration);
       if (NewFD == 0) return 0;
       if (Redeclaration) {
-        // Note that the new declaration is a redeclaration of the
-        // older declaration. Then return the older declaration: the
-        // new one is only kept within the set of previous
-        // declarations for this function.
-        FunctionDecl *OldFD = (FunctionDecl *)PrevDecl;
-        OldFD->AddRedeclaration(NewFD);
-        return OldFD;
+        NewFD->setPreviousDeclaration(cast<FunctionDecl>(PrevDecl));
       }
     }
     New = NewFD;

Added: cfe/trunk/test/CodeGen/func-decl-cleanup.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/func-decl-cleanup.c?rev=51584&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/func-decl-cleanup.c (added)
+++ cfe/trunk/test/CodeGen/func-decl-cleanup.c Tue May 27 00:07:37 2008
@@ -0,0 +1,12 @@
+// RUN: clang %s -emit-llvm -o -
+
+
+// PR2360
+typedef void fn_t();
+
+fn_t a,b;
+
+void b()
+{
+}
+





More information about the cfe-commits mailing list