]> granicus.if.org Git - clang/commitdiff
Check for redefinitions in MergeVarDecl. This finds redefinitions of globals without...
authorSebastian Redl <sebastian.redl@getdesigned.at>
Tue, 2 Feb 2010 18:35:11 +0000 (18:35 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Tue, 2 Feb 2010 18:35:11 +0000 (18:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@95098 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/CXX/basic/basic.def.odr/p1-var.cpp [new file with mode: 0644]

index ea1f2f2bf5d6119496fa805b5110aa4dae2c10dd..8f756da70da9ef98d7894eb1e23a4e1a82f6e89f 100644 (file)
@@ -1298,6 +1298,17 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     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);
 
diff --git a/test/CXX/basic/basic.def.odr/p1-var.cpp b/test/CXX/basic/basic.def.odr/p1-var.cpp
new file mode 100644 (file)
index 0000000..892f546
--- /dev/null
@@ -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}}