From bbe2743b0259e6c8b053b601bfd768dd49c8143a Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 21 Nov 2008 16:29:06 +0000 Subject: [PATCH] Allow redeclaration of typedefs in C++ git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59822 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 9 ++++++++- test/SemaCXX/typedef-redecl.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/SemaCXX/typedef-redecl.cpp diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 7408507f23..0b05177dd3 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -344,7 +344,14 @@ TypedefDecl *Sema::MergeTypeDefDecl(TypedefDecl *New, Decl *OldD) { if (getLangOptions().Microsoft) return New; - // Redeclaration of a type is a constraint violation (6.7.2.3p1). + // C++ [dcl.typedef]p2: + // In a given non-class scope, a typedef specifier can be used to + // redefine the name of any type declared in that scope to refer + // to the type to which it already refers. + if (getLangOptions().CPlusPlus && !isa(CurContext)) + return New; + + // In C, redeclaration of a type is a constraint violation (6.7.2.3p1). // Apparently GCC, Intel, and Sun all silently ignore the redeclaration if // *either* declaration is in a system header. The code below implements // this adhoc compatibility rule. FIXME: The following code will not diff --git a/test/SemaCXX/typedef-redecl.cpp b/test/SemaCXX/typedef-redecl.cpp new file mode 100644 index 0000000000..c37a08ab3a --- /dev/null +++ b/test/SemaCXX/typedef-redecl.cpp @@ -0,0 +1,12 @@ +// RUN: clang -fsyntax-only -verify %s + +typedef int INT; +typedef INT REALLY_INT; // expected-error{{previous definition is here}} +typedef REALLY_INT REALLY_REALLY_INT; +typedef REALLY_INT BOB; +typedef float REALLY_INT; // expected-error{{typedef redefinition with different types ('float' vs 'INT')}} + +class X { + typedef int result_type; // expected-error{{previous definition is here}} + typedef INT result_type; // expected-error{{redefinition of 'result_type'}} +}; -- 2.40.0