[cfe-commits] r95098 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/CXX/basic/basic.def.odr/p1-var.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Tue Feb 2 10:35:11 PST 2010


Author: cornedbee
Date: Tue Feb  2 12:35:11 2010
New Revision: 95098

URL: http://llvm.org/viewvc/llvm-project?rev=95098&view=rev
Log:
Check for redefinitions in MergeVarDecl. This finds redefinitions of globals without an initializer in C++ and thus fixes PR5451.

Added:
    cfe/trunk/test/CXX/basic/basic.def.odr/p1-var.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Feb  2 12:35:11 2010
@@ -1298,6 +1298,17 @@
     Diag(Old->getLocation(), diag::note_previous_definition);
   }
 
+  // C++ doesn't have tentative definitions, so go right ahead and check here.
+  const VarDecl *Def;
+  if (New->isThisDeclarationADefinition() == VarDecl::Definition &&
+      (Def = Old->getDefinition())) {
+    Diag(New->getLocation(), diag::err_redefinition)
+      << New->getDeclName();
+    Diag(Def->getLocation(), diag::note_previous_definition);
+    New->setInvalidDecl();
+    return;
+  }
+
   // Keep a chain of previous declarations.
   New->setPreviousDeclaration(Old);
 

Added: cfe/trunk/test/CXX/basic/basic.def.odr/p1-var.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/basic/basic.def.odr/p1-var.cpp?rev=95098&view=auto

==============================================================================
--- cfe/trunk/test/CXX/basic/basic.def.odr/p1-var.cpp (added)
+++ cfe/trunk/test/CXX/basic/basic.def.odr/p1-var.cpp Tue Feb  2 12:35:11 2010
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// C++ [basic.def.odr]p1:
+//   No translation unit shall contain more than one definition of any
+//   variable, [...].
+
+// Bad: in C++, these are both definitions. None of that C99 tentative stuff.
+int i; // expected-note {{previous}}
+int i; // expected-error {{redefinition}}
+
+// OK: decl + def
+extern int j;
+int j;
+
+// OK: def + decl
+int k;
+extern int k;
+
+// Bad. The important thing here is that we don't emit the diagnostic twice.
+int l = 1; // expected-note {{previous}}
+int l = 2; // expected-error {{redefinition}}





More information about the cfe-commits mailing list