[cfe-commits] r67336 - in /cfe/trunk: include/clang/AST/Decl.h lib/Sema/SemaDecl.cpp test/Sema/tentative-decls.c test/Sema/var-redecl.c

Douglas Gregor dgregor at apple.com
Thu Mar 19 15:01:51 PDT 2009


Author: dgregor
Date: Thu Mar 19 17:01:50 2009
New Revision: 67336

URL: http://llvm.org/viewvc/llvm-project?rev=67336&view=rev
Log:
Variables marked as "extern" can actually have internal linkage if
there is a previous declaration marked "static". This fixes PR3645.

Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/tentative-decls.c
    cfe/trunk/test/Sema/var-redecl.c

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

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Thu Mar 19 17:01:50 2009
@@ -258,6 +258,7 @@
   virtual void Destroy(ASTContext& C);
 
   StorageClass getStorageClass() const { return (StorageClass)SClass; }
+  void setStorageClass(StorageClass SC) { SClass = SC; }
 
   SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; }
 

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Mar 19 17:01:50 2009
@@ -832,9 +832,20 @@
     Diag(Old->getLocation(), diag::note_previous_definition);
     return true;
   }
-  // C99 6.2.2p4: Check if we have a non-static decl followed by a static.
-  if (New->getStorageClass() != VarDecl::Static &&
-      Old->getStorageClass() == VarDecl::Static) {
+  // C99 6.2.2p4: 
+  //   For an identifier declared with the storage-class specifier
+  //   extern in a scope in which a prior declaration of that
+  //   identifier is visible,23) if the prior declaration specifies
+  //   internal or external linkage, the linkage of the identifier at
+  //   the later declaration is the same as the linkage specified at
+  //   the prior declaration. If no prior declaration is visible, or
+  //   if the prior declaration specifies no linkage, then the
+  //   identifier has external linkage.
+  if ((New->hasExternalStorage() || New->getStorageClass() == VarDecl::None) &&
+      Old->hasLinkage())
+    /* Okay */;
+  else if (New->getStorageClass() != VarDecl::Static &&
+           Old->getStorageClass() == VarDecl::Static) {
     Diag(New->getLocation(), diag::err_non_static_static) << New->getDeclName();
     Diag(Old->getLocation(), diag::note_previous_definition);
     return true;

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

==============================================================================
--- cfe/trunk/test/Sema/tentative-decls.c (original)
+++ cfe/trunk/test/Sema/tentative-decls.c Thu Mar 19 17:01:50 2009
@@ -27,7 +27,7 @@
 static int i1; // expected-error{{static declaration of 'i1' follows non-static declaration}}
 
 static int i2 = 5; // expected-note 1 {{previous definition is here}}
-int i2 = 3; // expected-error{{non-static declaration of 'i2' follows static declaration}}
+int i2 = 3; // expected-error{{redefinition of 'i2'}}
 
 __private_extern__ int pExtern;
 int pExtern = 0;

Modified: cfe/trunk/test/Sema/var-redecl.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/var-redecl.c?rev=67336&r1=67335&r2=67336&view=diff

==============================================================================
--- cfe/trunk/test/Sema/var-redecl.c (original)
+++ cfe/trunk/test/Sema/var-redecl.c Thu Mar 19 17:01:50 2009
@@ -54,3 +54,8 @@
   extern int g19;
 }
 int *p=&g19; // expected-error{{use of undeclared identifier 'g19'}}
+
+// PR3645
+static int a;
+extern int a;
+int a;





More information about the cfe-commits mailing list