[cfe-commits] r54533 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDecl.cpp test/Sema/tentative-decls.c

Steve Naroff snaroff at apple.com
Fri Aug 8 10:50:36 PDT 2008


Author: snaroff
Date: Fri Aug  8 12:50:35 2008
New Revision: 54533

URL: http://llvm.org/viewvc/llvm-project?rev=54533&view=rev
Log:
Fix issues with C "tentative" definitions.

- Move checking from MergeVarDecl->FinializeDeclaratorGroup. Since MergeVarDecl is called before the initializer is attacted, it can't be done there (this removes a long standing FIXME).
- Add Sema::isTentativeDefinition() and Sema::CheckForFileScopedRedefinitions().
- Remove FIXME's and touch-up test case.

Still some more work to do (forthcoming)...

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/tentative-decls.c

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

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Aug  8 12:50:35 2008
@@ -290,7 +290,9 @@
                                   bool &Redeclaration);
   VarDecl *MergeVarDecl(VarDecl *New, Decl *Old);
   FunctionDecl *MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old);
-
+  bool isTentativeDefinition(VarDecl *VD);
+  void CheckForFileScopedRedefinitions(Scope *S, VarDecl *VD);
+  
   /// Helpers for dealing with function parameters
   bool CheckParmsForFunctionDef(FunctionDecl *FD);
   ImplicitParamDecl *CreateImplicitParameter(Scope *S, IdentifierInfo *Id, 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Aug  8 12:50:35 2008
@@ -104,7 +104,6 @@
       return;
     }
   }
-
   IdResolver.AddDecl(D);
 }
 
@@ -411,12 +410,51 @@
   return NewQType == OldQType;
 }
 
+/// Predicate for C "tentative" external object definitions (C99 6.9.2).
+bool Sema::isTentativeDefinition(VarDecl *VD) {
+  if (VD->isFileVarDecl())
+    return (!VD->getInit() &&
+            (VD->getStorageClass() == VarDecl::None ||
+             VD->getStorageClass() == VarDecl::Static));
+  return false;
+}
+
+/// CheckForFileScopedRedefinitions - Make sure we forgo redefinition errors
+/// when dealing with C "tentative" external object definitions (C99 6.9.2).
+void Sema::CheckForFileScopedRedefinitions(Scope *S, VarDecl *VD) {
+  bool VDIsTentative = isTentativeDefinition(VD);
+  
+  for (IdentifierResolver::iterator
+       I = IdResolver.begin(VD->getIdentifier(), 
+                            VD->getDeclContext(), false/*LookInParentCtx*/), 
+       E = IdResolver.end(); I != E; ++I) {
+    if (*I != VD && IdResolver.isDeclInScope(*I, VD->getDeclContext(), S)) {
+      VarDecl *OldDecl = dyn_cast<VarDecl>(*I);
+      
+      // Check for "tentative" definitions. We can't accomplish this in 
+      // MergeVarDecl since the initializer hasn't been attached.
+      if (!OldDecl || isTentativeDefinition(OldDecl) || VDIsTentative)
+        continue;
+  
+      // Handle __private_extern__ just like extern.
+      if (OldDecl->getStorageClass() != VarDecl::Extern &&
+          OldDecl->getStorageClass() != VarDecl::PrivateExtern &&
+          VD->getStorageClass() != VarDecl::Extern &&
+          VD->getStorageClass() != VarDecl::PrivateExtern) {
+        Diag(VD->getLocation(), diag::err_redefinition, VD->getName());
+        Diag(OldDecl->getLocation(), diag::err_previous_definition);
+      }
+    }
+  }
+}
+
 /// MergeVarDecl - We just parsed a variable 'New' which has the same name
 /// and scope as a previous declaration 'Old'.  Figure out how to resolve this
 /// situation, merging decls or emitting diagnostics as appropriate.
 ///
-/// FIXME: Need to carefully consider tentative definition rules (C99 6.9.2p2).
-/// For example, we incorrectly complain about i1, i4 from C99 6.9.2p4.
+/// Tentative definition rules (C99 6.9.2p2) are checked by 
+/// FinalizeDeclaratorGroup. Unfortunately, we can't analyze tentative 
+/// definitions here, since the initializer hasn't been attached.
 /// 
 VarDecl *Sema::MergeVarDecl(VarDecl *New, Decl *OldD) {
   // Verify the old decl was also a variable.
@@ -454,34 +492,8 @@
     Diag(Old->getLocation(), diag::err_previous_definition);
     return New;
   }
-  // We've verified the types match, now handle "tentative" definitions.
-  if (Old->isFileVarDecl() && New->isFileVarDecl()) {
-    // Handle C "tentative" external object definitions (C99 6.9.2).
-    bool OldIsTentative = false;
-    bool NewIsTentative = false;
-    
-    if (!Old->getInit() &&
-        (Old->getStorageClass() == VarDecl::None ||
-         Old->getStorageClass() == VarDecl::Static))
-      OldIsTentative = true;
-      
-    // FIXME: this check doesn't work (since the initializer hasn't been
-    // attached yet). This check should be moved to FinalizeDeclaratorGroup.
-    // Unfortunately, by the time we get to FinializeDeclaratorGroup, we've 
-    // thrown out the old decl.
-    if (!New->getInit() &&
-        (New->getStorageClass() == VarDecl::None ||
-         New->getStorageClass() == VarDecl::Static))
-      ; // change to NewIsTentative = true; once the code is moved.
-    
-    if (NewIsTentative || OldIsTentative)
-      return New;
-  }
-  // Handle __private_extern__ just like extern.
-  if (Old->getStorageClass() != VarDecl::Extern &&
-      Old->getStorageClass() != VarDecl::PrivateExtern &&
-      New->getStorageClass() != VarDecl::Extern &&
-      New->getStorageClass() != VarDecl::PrivateExtern) {
+  // File scoped variables are analyzed in FinalizeDeclaratorGroup.
+  if (!New->isFileVarDecl()) {
     Diag(New->getLocation(), diag::err_redefinition, New->getName());
     Diag(Old->getLocation(), diag::err_previous_definition);
   }
@@ -1431,9 +1443,7 @@
     // storage-class specifier or with the storage-class specifier "static",
     // constitutes a tentative definition. Note: A tentative definition with
     // external linkage is valid (C99 6.2.2p5).
-    if (IDecl && !IDecl->getInit() && 
-        (IDecl->getStorageClass() == VarDecl::Static || 
-         IDecl->getStorageClass() == VarDecl::None)) {
+    if (isTentativeDefinition(IDecl)) {
       if (T->isIncompleteArrayType()) {
         // C99 6.9.2 (p2, p5): Implicit initialization causes an incomplete
         // array to be completed. Don't issue a diagnostic.
@@ -1446,6 +1456,8 @@
         IDecl->setInvalidDecl();
       }
     }
+    if (IDecl->isFileVarDecl())
+      CheckForFileScopedRedefinitions(S, IDecl);
   }
   return NewGroup;
 }

Modified: cfe/trunk/test/Sema/tentative-decls.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/tentative-decls.c?rev=54533&r1=54532&r2=54533&view=diff

==============================================================================
--- cfe/trunk/test/Sema/tentative-decls.c (original)
+++ cfe/trunk/test/Sema/tentative-decls.c Fri Aug  8 12:50:35 2008
@@ -11,16 +11,19 @@
 
 int i1 = 1; // expected-error{{previous definition is here}}
 int i1 = 2; // expected-error{{redefinition of 'i1'}} // expected-error{{previous definition is here}}
-// FIXME: the following should not be an error (see related FIXME in Sema::MergeVarDecl). 
-int i1; // expected-error{{redefinition of 'i1'}}
+int i1;
 int i1;
 extern int i1; // expected-error{{previous definition is here}}
 static int i1; // expected-error{{static declaration of 'i1' follows non-static declaration}} expected-error{{previous definition is here}}
-int i1 = 3; // expected-error{{non-static declaration of 'i1' follows static declaration}}
+int i1 = 3; // expected-error{{redefinition of 'i1'}} expected-error{{non-static declaration of 'i1' follows static declaration}}
 
 __private_extern__ int pExtern;
 int pExtern = 0;
 
+int i4;
+int i4;
+extern int i4;
+
 void func() {
   extern int i1; // expected-error{{previous definition is here}}
   static int i1; // expected-error{{static declaration of 'i1' follows non-static declaration}}





More information about the cfe-commits mailing list