[cfe-commits] r59822 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/typedef-redecl.cpp

Douglas Gregor doug.gregor at gmail.com
Fri Nov 21 08:29:06 PST 2008


Author: dgregor
Date: Fri Nov 21 10:29:06 2008
New Revision: 59822

URL: http://llvm.org/viewvc/llvm-project?rev=59822&view=rev
Log:
Allow redeclaration of typedefs in C++

Added:
    cfe/trunk/test/SemaCXX/typedef-redecl.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=59822&r1=59821&r2=59822&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Nov 21 10:29:06 2008
@@ -344,7 +344,14 @@
   
   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<CXXRecordDecl>(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

Added: cfe/trunk/test/SemaCXX/typedef-redecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/typedef-redecl.cpp?rev=59822&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/typedef-redecl.cpp (added)
+++ cfe/trunk/test/SemaCXX/typedef-redecl.cpp Fri Nov 21 10:29:06 2008
@@ -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'}}
+};





More information about the cfe-commits mailing list