* c-common.c (c_common_nodes_and_builtins): Create global null_node.
(warn_strict_null_sentinel): Define.
(check_function_sentinel): Check for null_node as valid sentinel too.
* c-common.h (c_tree_index): Added CTI_NULL.
(null_node) Define global_tree[CTI_NULL].
(warn_strict_null_sentinel): Declare.
* c-opts.c: (c_common_handle_option): Handle -Wstrict-null-sentinel.
* c.opt: (Wstrict-null-sentinel): New C++ option.
* doc/invoke.texi (C++ Options): Document -Wstrict-null-sentinel.
* cp-tree.h (cp_tree_index): Remove CPTI_NULL, to be defined in C
common frontend.
(null_node): Remove.
* lex.c (cxx_init): Move null_node initialisation to C common frontend.
* g++.dg/warn/sentinel.C: New testcase for __null sentinels added.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@99091
138bc75d-0d04-0410-961f-
82ee72b054a4
+2005-05-02 Michael Matz <matz@suse.de>
+
+ PR c++/19542
+ * c-common.c (c_common_nodes_and_builtins): Create global null_node.
+ (warn_strict_null_sentinel): Define.
+ (check_function_sentinel): Check for null_node as valid sentinel too.
+ * c-common.h (c_tree_index): Added CTI_NULL.
+ (null_node) Define global_tree[CTI_NULL].
+ (warn_strict_null_sentinel): Declare.
+ * c-opts.c: (c_common_handle_option): Handle -Wstrict-null-sentinel.
+ * c.opt: (Wstrict-null-sentinel): New C++ option.
+ * doc/invoke.texi (C++ Options): Document -Wstrict-null-sentinel.
+
2005-05-01 Kazu Hirata <kazu@cs.umass.edu>
* gimplify.c (gimplify_compound_lval): Use VEC instead of
int warn_format;
+/* Warn about using __null (as NULL in C++) as sentinel. For code compiled
+ with GCC this doesn't matter as __null is guaranteed to have the right
+ size. */
+
+int warn_strict_null_sentinel;
+
/* Zero means that faster, ...NonNil variants of objc_msgSend...
calls will be used in ObjC; passing nil receivers to such calls
will most likely result in crashes. */
mudflap_init ();
main_identifier_node = get_identifier ("main");
+
+ /* Create the built-in __null node. It is important that this is
+ not shared. */
+ null_node = make_node (INTEGER_CST);
+ TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
}
/* Look up the function in built_in_decls that corresponds to DECL
}
/* Validate the sentinel. */
- if (!POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (sentinel)))
- || !integer_zerop (TREE_VALUE (sentinel)))
+ if ((!POINTER_TYPE_P (TREE_TYPE (TREE_VALUE (sentinel)))
+ || !integer_zerop (TREE_VALUE (sentinel)))
+ /* Although __null (in C++) is only an integer we allow it
+ nevertheless, as we are guaranteed that it's exactly
+ as wide as a pointer, and we don't want to force
+ users to cast the NULL they have written there.
+ We warn with -Wstrict-null-sentinel, though. */
+ && (warn_strict_null_sentinel
+ || null_node != TREE_VALUE (sentinel)))
warning (0, "missing sentinel in function call");
}
}
CTI_VOID_ZERO,
+ CTI_NULL,
+
CTI_MAX
};
/* A node for `((void) 0)'. */
#define void_zero_node c_global_trees[CTI_VOID_ZERO]
+/* The node for C++ `__null'. */
+#define null_node c_global_trees[CTI_NULL]
+
extern GTY(()) tree c_global_trees[CTI_MAX];
/* In a RECORD_TYPE, a sorted array of the fields of the type, not a
extern int warn_implicit;
+/* Warn about using __null (as NULL in C++) as sentinel. For code compiled
+ with GCC this doesn't matter as __null is guaranteed to have the right
+ size. */
+
+extern int warn_strict_null_sentinel;
+
/* Maximum template instantiation depth. This limit is rather
arbitrary, but it exists to limit the time it takes to notice
infinite template instantiations. */
warn_return_type = value;
break;
+ case OPT_Wstrict_null_sentinel:
+ warn_strict_null_sentinel = value;
+ break;
+
case OPT_Wsystem_headers:
cpp_opts->warn_system_headers = value;
break;
C++ ObjC++ Var(warn_sign_promo)
Warn when overload promotes from unsigned to signed
+Wstrict-null-sentinel
+C++ ObjC++
+Warn about uncasted NULL used as sentinel
+
Wstrict-prototypes
C ObjC Var(warn_strict_prototypes)
Warn about unprototyped function declarations
+2005-05-02 Michael Matz <matz@suse.de>
+
+ PR c++/19542
+ * cp-tree.h (cp_tree_index): Remove CPTI_NULL, to be defined in C
+ common frontend.
+ (null_node): Remove.
+ * lex.c (cxx_init): Move null_node initialisation to C common frontend.
+
2005-04-25 Ian Lance Taylor <ian@airs.com>
* cp-tree.def: Add EXPR_STMT.
CPTI_LANG_NAME_JAVA,
CPTI_EMPTY_EXCEPT_SPEC,
- CPTI_NULL,
CPTI_JCLASS,
CPTI_TERMINATE,
CPTI_CALL_UNEXPECTED,
/* Exception specifier used for throw(). */
#define empty_except_spec cp_global_trees[CPTI_EMPTY_EXCEPT_SPEC]
-/* The node for `__null'. */
-#define null_node cp_global_trees[CPTI_NULL]
-
/* If non-NULL, a POINTER_TYPE equivalent to (java::lang::Class*). */
#define jclass_node cp_global_trees[CPTI_JCLASS]
cxx_init_decl_processing ();
- /* Create the built-in __null node. It is important that this is
- not shared. */
- null_node = make_node (INTEGER_CST);
- TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
-
/* The fact that G++ uses COMDAT for many entities (inline
functions, template instantiations, virtual tables, etc.) mean
that it is fundamentally unreliable to try to make decisions
-fno-default-inline -fvisibility-inlines-hidden @gol
-Wabi -Wctor-dtor-privacy @gol
-Wnon-virtual-dtor -Wreorder @gol
--Weffc++ -Wno-deprecated @gol
+-Weffc++ -Wno-deprecated -Wstrict-null-sentinel @gol
-Wno-non-template-friend -Wold-style-cast @gol
-Woverloaded-virtual -Wno-pmf-conversions @gol
-Wsign-promo}
@opindex Wno-deprecated
Do not warn about usage of deprecated features. @xref{Deprecated Features}.
+@item -Wstrict-null-sentinel @r{(C++ only)}
+@opindex Wstrict-null-sentinel
+Warn also about the use of an uncasted @code{NULL} as sentinel. When
+compiling only with GCC this is a valid sentinel, as @code{NULL} is defined
+to @code{__null}. Although it is a null pointer constant not a null pointer,
+it is guaranteed to of the same size as a pointer. But this use is
+not portable across different compilers.
+
@item -Wno-non-template-friend @r{(C++ only)}
@opindex Wno-non-template-friend
Disable warnings when non-templatized friend functions are declared
+2005-05-02 Michael Matz <matz@suse.de>
+
+ PR c++/19542
+ * g++.dg/warn/sentinel.C: New testcase for __null sentinels added.
+
2005-05-01 Mark Mitchell <mark@codesourcery.com>
* g++.dg/cpp/weak.C: New test.
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+extern void ex (int i, ...) __attribute__ ((__sentinel__(0)));
+
+void f()
+{
+ ex (1, 0); /* { dg-warning "missing sentinel in function call" "" } */
+ ex (1, 0L); /* { dg-warning "missing sentinel in function call" "" } */
+ ex (1, (void *)0);
+ ex (1, __null); /* { dg-bogus "sentinel" } */
+}