r177686 - <rdar://problem/13479214> Make Clang's <stddef.h> robust against system headers defining size_t/ptrdiff_t/wchar_t.

Douglas Gregor dgregor at apple.com
Thu Mar 21 17:10:49 PDT 2013


Author: dgregor
Date: Thu Mar 21 19:10:49 2013
New Revision: 177686

URL: http://llvm.org/viewvc/llvm-project?rev=177686&view=rev
Log:
<rdar://problem/13479214> Make Clang's <stddef.h> robust against system headers defining size_t/ptrdiff_t/wchar_t.

Clang's <stddef.h> provides definitions for the C standard library
types size_t, ptrdiff_t, and wchar_t. However, the system's C standard
library headers tend to provide the same typedefs, and the two
generally avoid each other using the macros
_SIZE_T/_PTRDIFF_T/_WCHAR_T. With modules, however, we need to see
*all* of the places where these types are defined, so provide the
typedefs (ignoring the macros) when modules are enabled.

Added:
    cfe/trunk/test/Modules/Inputs/StdDef/
    cfe/trunk/test/Modules/Inputs/StdDef/module.map
    cfe/trunk/test/Modules/Inputs/StdDef/other.h
    cfe/trunk/test/Modules/Inputs/StdDef/size_t.h
    cfe/trunk/test/Modules/stddef.m
Modified:
    cfe/trunk/lib/Headers/stddef.h

Modified: cfe/trunk/lib/Headers/stddef.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/stddef.h?rev=177686&r1=177685&r2=177686&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/stddef.h (original)
+++ cfe/trunk/lib/Headers/stddef.h Thu Mar 21 19:10:49 2013
@@ -26,17 +26,28 @@
 #ifndef __STDDEF_H
 #define __STDDEF_H
 
-#ifndef _PTRDIFF_T
+#if !defined(_PTRDIFF_T) || __has_feature(modules)
+/* Always define ptrdiff_t when modules are available. */
+#if !__has_feature(modules)
 #define _PTRDIFF_T
+#endif
 typedef __PTRDIFF_TYPE__ ptrdiff_t;
 #endif
-#ifndef _SIZE_T
+
+#if !defined(_SIZE_T) || __has_feature(modules)
+/* Always define size_t when modules are available. */
+#if !__has_feature(modules)
 #define _SIZE_T
+#endif
 typedef __SIZE_TYPE__ size_t;
 #endif
+
 #ifndef __cplusplus
-#ifndef _WCHAR_T
+/* Always define wchar_t when modules are available. */
+#if !defined(_WCHAR_T) || __has_feature(modules)
+#if !__has_feature(modules)
 #define _WCHAR_T
+#endif
 typedef __WCHAR_TYPE__ wchar_t;
 #endif
 #endif
@@ -66,9 +77,12 @@ using ::std::nullptr_t;
 /* Some C libraries expect to see a wint_t here. Others (notably MinGW) will use
 __WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */
 #if defined(__need_wint_t)
-#if !defined(_WINT_T)
+/* Always define wint_t when modules are available. */
+#if !defined(_WINT_T) || __has_feature(modules)
+#if !__has_feature(modules)
 #define _WINT_T
+#endif
 typedef __WINT_TYPE__ wint_t;
-#endif /* _WINT_T */
+#endif
 #undef __need_wint_t
 #endif /* __need_wint_t */

Added: cfe/trunk/test/Modules/Inputs/StdDef/module.map
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/StdDef/module.map?rev=177686&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/StdDef/module.map (added)
+++ cfe/trunk/test/Modules/Inputs/StdDef/module.map Thu Mar 21 19:10:49 2013
@@ -0,0 +1,11 @@
+module StdDef {
+  module SizeT {
+    header "size_t.h"
+    export *
+  }
+
+  module Other {
+    header "other.h"
+    export *
+  }
+}

Added: cfe/trunk/test/Modules/Inputs/StdDef/other.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/StdDef/other.h?rev=177686&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/StdDef/other.h (added)
+++ cfe/trunk/test/Modules/Inputs/StdDef/other.h Thu Mar 21 19:10:49 2013
@@ -0,0 +1,2 @@
+#include <stddef.h>
+

Added: cfe/trunk/test/Modules/Inputs/StdDef/size_t.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/StdDef/size_t.h?rev=177686&view=auto
==============================================================================
--- cfe/trunk/test/Modules/Inputs/StdDef/size_t.h (added)
+++ cfe/trunk/test/Modules/Inputs/StdDef/size_t.h Thu Mar 21 19:10:49 2013
@@ -0,0 +1,4 @@
+#ifndef _SIZE_T
+#define _SIZE_T
+typedef __SIZE_TYPE__ size_t;
+#endif

Added: cfe/trunk/test/Modules/stddef.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/stddef.m?rev=177686&view=auto
==============================================================================
--- cfe/trunk/test/Modules/stddef.m (added)
+++ cfe/trunk/test/Modules/stddef.m Thu Mar 21 19:10:49 2013
@@ -0,0 +1,7 @@
+ at import StdDef.Other;
+
+size_t getSize();
+
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -I %S/Inputs/StdDef %s -verify
+// expected-no-diagnostics





More information about the cfe-commits mailing list