This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH RFC: Define EXTERN_CONST in ansidecl.h


I have tested this patch to add EXTERN_CONST to include/ansidecl.h.  The
idea here is that we will use EXTERN_CONST for const variables which
must be visible outside of the file in which they are defined.  This is
necessary because C++ and C have different rules for the linkage of a
const variable with no storage class specifier.  This is not pretty, but
is along the same lines as the use of XNEW.

There are not many cases like this in the gcc source code proper (there
are several in the generated files).  One example is in gimple.c.  This
declaration:

const size_t gimple_ops_offset_[] = {

would become

EXTERN_CONST size_t gimple_ops_offset_[] = {

Before I commit this patch (which I can do as libiberty maintainer), and
proceed to changing existing uses to use EXTERN_CONST (which I can do as
middle-end maintainer), I would like to know if anybody has any strong
objections to this, or would prefer to see a different approach.

Note that this is not an example of the ugliness of C++: it is an
example of the ugliness of the common subset of C and C++.

Note that C does actually permit "extern const int i = 1", which means
the same thing as "const int i = 1".  However, this is very uncommon in
C, and I think it's best to use a clearer marker.  That will also avoid
potential problems with non-gcc C compilers used to build stage 1.

Ian


2009-06-02  Ian Lance Taylor  <iant@google.com>

	* ansidecl.h (EXTERN_CONST): Define.


Index: ansidecl.h
===================================================================
--- ansidecl.h	(revision 148085)
+++ ansidecl.h	(working copy)
@@ -395,6 +395,18 @@ So instead we use the macro below and te
 #define __extension__
 #endif
 
+/* This is used to declare a const variable which should be visible
+   outside of the current compilation unit.  Use it as
+     EXTERN_CONST int i = 1;
+   This is because the semantics of const are different in C and C++.
+   "extern const" is permitted in C but it looks strange, and gcc
+   warns about it when -Wc++-compat is not used.  */
+#ifdef __cplusplus
+#define EXTERN_CONST extern const
+#else
+#define EXTERN_CONST const
+#endif
+
 #ifdef __cplusplus
 }
 #endif

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]