[cfe-commits] r50612 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/Decl.cpp lib/CodeGen/CodeGenFunction.cpp lib/CodeGen/CodeGenModule.cpp

Chris Lattner sabre at nondot.org
Sat May 3 19:29:49 PDT 2008


Author: lattner
Date: Sat May  3 21:29:49 2008
New Revision: 50612

URL: http://llvm.org/viewvc/llvm-project?rev=50612&view=rev
Log:
Simplify FunctionDecl::AddRedeclaration a bit by using std::swap.
Fix 'swapping' of attributes to not insert null values into the 
DeclAttrs map.

Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Sat May  3 21:29:49 2008
@@ -135,6 +135,7 @@
   
   void addAttr(Attr *attr);
   const Attr *getAttrs() const;
+  void swapAttrs(Decl *D);
 
   template<typename T> const T *getAttr() const {
     for (const Attr *attr = getAttrs(); attr; attr = attr->getNext())

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

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Sat May  3 21:29:49 2008
@@ -341,6 +341,31 @@
   return (*DeclAttrs)[this];
 }
 
+void Decl::swapAttrs(Decl *RHS) {
+  bool HasLHSAttr = this->HasAttrs;
+  bool HasRHSAttr = RHS->HasAttrs;
+  
+  // Usually, neither decl has attrs, nothing to do.
+  if (!HasLHSAttr && !HasRHSAttr) return;
+  
+  // If 'this' has no attrs, swap the other way.
+  if (!HasLHSAttr)
+    return RHS->swapAttrs(this);
+  
+  // Handle the case when both decls have attrs.
+  if (HasRHSAttr) {
+    std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
+    return;
+  }
+  
+  // Otherwise, LHS has an attr and RHS doesn't.
+  (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
+  (*DeclAttrs).erase(this);
+  this->HasAttrs = false;
+  RHS->HasAttrs = true;
+}
+
+
 #define CASE(KIND) \
   case KIND: \
     static_cast<KIND##Decl *>(const_cast<Decl *>(this))->~KIND##Decl(); \
@@ -487,16 +512,12 @@
   // Swap parameters, so that the most recent parameter names and
   // exact types (e.g., enum vs int) show up in the original
   // declaration.
-  ParmVarDecl **thisParamInfo = this->ParamInfo;
-  this->ParamInfo = FD->ParamInfo;
-  FD->ParamInfo = thisParamInfo;
+  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.
-  Stmt *thisBody = this->Body;
-  this->Body = FD->Body;
-  FD->Body = thisBody;
+  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,
@@ -515,11 +536,7 @@
   
   // Swap attributes. FD will have the union of the attributes from
   // all previous declarations.
-  if (DeclAttrs) {
-    Attr *thisAttr = (*DeclAttrs)[this];
-    (*DeclAttrs)[this] = (*DeclAttrs)[FD];
-    (*DeclAttrs)[FD] = thisAttr;
-  }
+  this->swapAttrs(FD);
 
   // If any declaration is inline, the function is inline.
   this->IsInline |= FD->IsInline;

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=50612&r1=50611&r2=50612&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Sat May  3 21:29:49 2008
@@ -153,6 +153,7 @@
   CurFuncDecl = FD;
   FnRetTy = FD->getType()->getAsFunctionType()->getResultType();
 
+  FD->getType().dump();
 
   CurFn = cast<llvm::Function>(CGM.GetAddrOfFunctionDecl(FD, true));
   assert(CurFn->isDeclaration() && "Function already has body?");

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=50612&r1=50611&r2=50612&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Sat May  3 21:29:49 2008
@@ -303,28 +303,30 @@
 
 void CodeGenModule::EmitFunction(const FunctionDecl *FD) {
   // If this is not a prototype, emit the body.
-  if (FD->getBody()) {
-    // If the function is a static, defer code generation until later so we can
-    // easily omit unused statics.
-    if (FD->getStorageClass() == FunctionDecl::Static) {
-      // We need to check the Module here to see if GetAddrOfFunctionDecl() has
-      // already added this function to the Module because the address of the
-      // function's prototype was taken.  If this is the case, call 
-      // GetAddrOfFunctionDecl to insert the static FunctionDecl into the used
-      // GlobalDeclsMap, so that EmitStatics will generate code for it later.
-      //
-      // Example:
-      // static int foo();
-      // int bar() { return foo(); }
-      // static int foo() { return 5; }
-      if (getModule().getFunction(FD->getName()))
-        GetAddrOfFunctionDecl(FD, true);
-
-      StaticDecls.push_back(FD);
-      return;
-    }
+  if (!FD->isThisDeclarationADefinition())
+    return;
+  
+  // If the function is a static, defer code generation until later so we can
+  // easily omit unused statics.
+  if (FD->getStorageClass() != FunctionDecl::Static) {
     CodeGenFunction(*this).GenerateCode(FD);
+    return;
   }
+      
+  // We need to check the Module here to see if GetAddrOfFunctionDecl() has
+  // already added this function to the Module because the address of the
+  // function's prototype was taken.  If this is the case, call 
+  // GetAddrOfFunctionDecl to insert the static FunctionDecl into the used
+  // GlobalDeclsMap, so that EmitStatics will generate code for it later.
+  //
+  // Example:
+  // static int foo();
+  // int bar() { return foo(); }
+  // static int foo() { return 5; }
+  if (getModule().getFunction(FD->getName()))
+    GetAddrOfFunctionDecl(FD, true);
+
+  StaticDecls.push_back(FD);
 }
 
 void CodeGenModule::EmitStatics() {





More information about the cfe-commits mailing list