aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-07-25 18:44:27 +0000
committerChris Lattner <sabre@nondot.org>2008-07-25 18:44:27 +0000
commit99cb9976efe84f44b3b1957c4fe042dae366b989 (patch)
treea0b74988affe5bf45ebe4dcfb1c438d57bf0f42b
parent806c3b9478b44dec77d28f3de987f793d42ad555 (diff)
Reject typedef redefinitions when the underlying types are not identical,
even if in a system header etc. rdar://6079937 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54038 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticKinds.def2
-rw-r--r--lib/Sema/SemaDecl.cpp12
-rw-r--r--test/Sema/typedef-redef.c11
3 files changed, 24 insertions, 1 deletions
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index e0023e3667..d7698d2901 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -739,6 +739,8 @@ DIAG(err_non_static_static, ERROR,
"non-static declaration of '%0' follows static declaration")
DIAG(err_redefinition_different_kind, ERROR,
"redefinition of '%0' as different kind of symbol")
+DIAG(err_redefinition_different_typedef, ERROR,
+ "typedef redefinition with different types ('%0' vs '%1')")
DIAG(err_conflicting_types, ERROR,
"conflicting types for '%0'")
DIAG(err_nested_redefinition, ERROR,
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 4d726cf0a8..610bf61b60 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -248,6 +248,18 @@ TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) {
return New;
}
+ // If the typedef types are not identical, reject them in all languages and
+ // with any extensions enabled.
+ if (Old->getUnderlyingType() != New->getUnderlyingType() &&
+ Context.getCanonicalType(Old->getUnderlyingType()) !=
+ Context.getCanonicalType(New->getUnderlyingType())) {
+ Diag(New->getLocation(), diag::err_redefinition_different_typedef,
+ New->getUnderlyingType().getAsString(),
+ Old->getUnderlyingType().getAsString());
+ Diag(Old->getLocation(), diag::err_previous_definition);
+ return Old;
+ }
+
// Allow multiple definitions for ObjC built-in typedefs.
// FIXME: Verify the underlying types are equivalent!
if (getLangOptions().ObjC1 && isBuiltinObjCType(New))
diff --git a/test/Sema/typedef-redef.c b/test/Sema/typedef-redef.c
index 1b12ae408c..d3904dd147 100644
--- a/test/Sema/typedef-redef.c
+++ b/test/Sema/typedef-redef.c
@@ -1,4 +1,13 @@
// RUN: clang < %s -fsyntax-only -verify
+// size_t coming from a system header.
#include <stddef.h>
-typedef unsigned long size_t;
+typedef __SIZE_TYPE__ size_t;
+
+
+
+typedef const int x; // expected-error {{previous definition is here}}
+extern x a;
+typedef int x; // expected-error {{typedef redefinition with different types}}
+extern x a;
+