[cfe-commits] r69391 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/Preprocessor/line-directive.c test/Sema/typedef-redef.c

Chris Lattner sabre at nondot.org
Fri Apr 17 15:04:21 PDT 2009


Author: lattner
Date: Fri Apr 17 17:04:20 2009
New Revision: 69391

URL: http://llvm.org/viewvc/llvm-project?rev=69391&view=rev
Log:
tweak redefinition of a typedef a bit to fix a couple of problems:
1. We had logic in sema to decide whether or not to emit the error
   based on manually checking whether in a system header file.
2. we were allowing redefinitions of typedefs in class scope in C++
   if in header file.
3. there was no way to force typedef redefinitions to be accepted
   by the C compiler, which annoys me when stripping linemarkers out
   of .i files.

The fix is to split the C++ class typedef redefinition path from the
C path, and change the C path to be a warning that normally maps to
error.  This causes it to properly be ignored in system headers, 
etc. and gives us a way to control it.  Passing 
-Wtypedef-redefinition now turns the error into a warning.

One behavior change is that we now diagnose cases where you redefine
a typedef in your .c file that was defined in a header file.  This
seems like reasonable behavior, and the diagnostic now indicates that
it can be controlled with -Wtypedef-redefinition.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Preprocessor/line-directive.c
    cfe/trunk/test/Sema/typedef-redef.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=69391&r1=69390&r2=69391&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Apr 17 17:04:20 2009
@@ -734,6 +734,11 @@
   "no previous prototype for function %0">,
   InGroup<DiagGroup<"missing-prototypes">>, DefaultIgnore;
 def err_redefinition : Error<"redefinition of %0">;
+
+def warn_redefinition_of_typedef : Warning<
+  "redefinition of typedef %0 is invalid in C">,
+  InGroup<DiagGroup<"typedef-redefinition"> >, DefaultError;
+
 def err_static_non_static : Error<
   "static declaration of %0 follows non-static declaration">;
 def err_non_static_static : Error<

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Apr 17 17:04:20 2009
@@ -533,25 +533,22 @@
   //   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 false;
-
-  // 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
-  // work properly when compiling ".i" files (containing preprocessed output).
-  if (PP.getDiagnostics().getSuppressSystemWarnings()) {
-    SourceManager &SrcMgr = Context.getSourceManager();
-    if (SrcMgr.isInSystemHeader(Old->getLocation()))
-      return false;
-    if (SrcMgr.isInSystemHeader(New->getLocation()))
+  if (getLangOptions().CPlusPlus) {
+    if (!isa<CXXRecordDecl>(CurContext))
       return false;
+    Diag(New->getLocation(), diag::err_redefinition)
+      << New->getDeclName();
+    Diag(Old->getLocation(), diag::note_previous_definition);
+    return true;
   }
 
-  Diag(New->getLocation(), diag::err_redefinition) << New->getDeclName();
+  // If we have a redefinition of a typedef in C, emit a warning.  This warning
+  // is normally mapped to an error, but can be controlled with
+  // -Wtypedef-redefinition.
+  Diag(New->getLocation(), diag::warn_redefinition_of_typedef)
+    << New->getDeclName();
   Diag(Old->getLocation(), diag::note_previous_definition);
-  return true;
+  return false;
 }
 
 /// DeclhasAttr - returns true if decl Declaration already has the target

Modified: cfe/trunk/test/Preprocessor/line-directive.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/line-directive.c?rev=69391&r1=69390&r2=69391&view=diff

==============================================================================
--- cfe/trunk/test/Preprocessor/line-directive.c (original)
+++ cfe/trunk/test/Preprocessor/line-directive.c Fri Apr 17 17:04:20 2009
@@ -40,7 +40,7 @@
 
 # 192 "glomp.h" // not a system header.
 typedef int x;  // expected-note {{previous definition is here}}
-typedef int x;  // expected-error {{redefinition of 'x'}}
+typedef int x;  // expected-error {{redefinition of typedef 'x' is invalid in C}}
 
 # 192 "glomp.h" 3 // System header.
 typedef int y;  // ok
@@ -59,4 +59,4 @@
 # 42 "blonk.h"  // DOES change system headerness.
 
 typedef int w;  // expected-note {{previous definition is here}}
-typedef int w;  // expected-error {{redefinition of 'w'}}
+typedef int w;  // expected-error {{redefinition of typedef 'w' is invalid in C}}

Modified: cfe/trunk/test/Sema/typedef-redef.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/typedef-redef.c?rev=69391&r1=69390&r2=69391&view=diff

==============================================================================
--- cfe/trunk/test/Sema/typedef-redef.c (original)
+++ cfe/trunk/test/Sema/typedef-redef.c Fri Apr 17 17:04:20 2009
@@ -1,11 +1,5 @@
 // RUN: clang-cc -fsyntax-only -verify %s
 
-// size_t coming from a system header.
-#include <stddef.h>
-typedef __SIZE_TYPE__ size_t;
-
-
-
 typedef const int x; // expected-note {{previous definition is here}}
 extern x a;
 typedef int x;  // expected-error {{typedef redefinition with different types}}





More information about the cfe-commits mailing list