This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH: strcmp and strncmp usage cleanup (Re: C++ PATCH to fix thinko)
Manfred Hollstein <manfred.h@gmx.net> writes:
| On Mon, 15 May 2000, 21:25:03 +0200, gdr@codesourcery.com wrote:
|
| Zack Weinberg <zack@wolery.cumb.org> writes:
|
| | On Mon, May 15, 2000 at 08:44:50PM +0200, Gabriel Dos Reis wrote:
| | > Manfred Hollstein <manfred.h@gmx.net> writes:
| | >
| | > | I believe, using constants as the length argument to strncmp
| | > | is error prone; I'd suggest to use this instead:
| | > |
| | > | else if (!strncmp (p, "diagnostics-show-location=",
| | > | sizeof ("diagnostics-show-location=") - 1))
| | >
| | > 100% agreed.
| |
| | I like to go a bit farther:
|
| But then let's go much farther :-)
|
| How about
|
| #define match(PTR, STRING) (!strncmp (PTR, STRING, sizeof (STRING) -1))
|
| used as:
|
| else if (match (p, "diagnostics-show-location="))
|
| ?
|
| That's fine.
|
| manfred
The following replaces every usage of strcmp and strncmp with that of
the macro match_string. I think we should do the same thing in the C
front-end. Jason, Mark?
Bootstrapped and tested on i686-pc-gnu-linux
Is it OK to commit?
-- Gaby
CodeSourcery, LLC http://www.codesourcery.com
2000-05-20 Gabriel Dos Reis <gdr@codesourcery.com>
* decl.h (match_string): New macro.
* decl.c (print_binding_level): Use match_string instead of
!strcmp or !strncmp.
* decl2.c (lang_decode_option): Likewise.
(grokfield): Likewise.
Index: decl.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl.h,v
retrieving revision 1.5
diff -p -r1.5 decl.h
*** decl.h 2000/01/26 20:51:34 1.5
--- decl.h 2000/05/20 13:20:08
*************** extern tree static_aggregates;
*** 47,49 ****
--- 47,55 ----
/* Purely for debugging purposes. */
extern int debug_bindings_indentation;
#endif
+
+ /* Return true if PTR is designating a substring comparing equal
+ to STRING. */
+ #define match_string(PTR, STRING) \
+ (!strncmp (PTR, STRING, strlen (STRING)))
+
Index: decl.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl.c,v
retrieving revision 1.606
diff -p -r1.606 decl.c
*** decl.c 2000/05/19 23:06:54 1.606
--- decl.c 2000/05/20 13:20:09
*************** print_binding_level (lvl)
*** 2002,2008 ****
continue;
if (no_print_builtins
&& (TREE_CODE (t) == TYPE_DECL)
! && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
continue;
/* Function decls tend to have longer names. */
--- 2002,2008 ----
continue;
if (no_print_builtins
&& (TREE_CODE (t) == TYPE_DECL)
! && (match_string (DECL_SOURCE_FILE (t), "<built-in>")))
continue;
/* Function decls tend to have longer names. */
*************** grokfndecl (ctype, type, declarator, ori
*** 8663,8669 ****
|| (IDENTIFIER_LENGTH (declarator) > 10
&& IDENTIFIER_POINTER (declarator)[0] == '_'
&& IDENTIFIER_POINTER (declarator)[1] == '_'
! && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
&& current_lang_name == lang_name_cplusplus
&& ctype == NULL_TREE
/* NULL_TREE means global namespace. */
--- 8663,8669 ----
|| (IDENTIFIER_LENGTH (declarator) > 10
&& IDENTIFIER_POINTER (declarator)[0] == '_'
&& IDENTIFIER_POINTER (declarator)[1] == '_'
! && match_string (IDENTIFIER_POINTER (declarator)+2, "builtin_")))
&& current_lang_name == lang_name_cplusplus
&& ctype == NULL_TREE
/* NULL_TREE means global namespace. */
Index: decl2.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/decl2.c,v
retrieving revision 1.349
diff -p -r1.349 decl2.c
*** decl2.c 2000/05/17 18:46:32 1.349
--- decl2.c 2000/05/20 13:20:10
*************** lang_decode_option (argc, argv)
*** 583,589 ****
strings_processed = 0;
#endif /* ! USE_CPPLIB */
! if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
/* ignore */;
else if (p[0] == '-' && p[1] == 'f')
{
--- 583,589 ----
strings_processed = 0;
#endif /* ! USE_CPPLIB */
! if (match_string (p, "-ftraditional") || match_string (p, "-traditional"))
/* ignore */;
else if (p[0] == '-' && p[1] == 'f')
{
*************** lang_decode_option (argc, argv)
*** 595,664 ****
p += 2;
/* Try special -f options. */
! if (!strcmp (p, "handle-exceptions")
! || !strcmp (p, "no-handle-exceptions"))
warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
! else if (!strcmp (p, "all-virtual")
! || !strcmp (p, "enum-int-equiv")
! || !strcmp (p, "no-nonnull-objects")
! || !strcmp (p, "this-is-variable"))
warning ("-f%s is no longer supported", p);
! else if (! strcmp (p, "alt-external-templates"))
{
flag_external_templates = 1;
flag_alt_external_templates = 1;
cp_deprecated ("-falt-external-templates");
}
! else if (! strcmp (p, "no-alt-external-templates"))
flag_alt_external_templates = 0;
! else if (!strcmp (p, "repo"))
{
flag_use_repository = 1;
flag_implicit_templates = 0;
}
! else if (!strcmp (p, "guiding-decls"))
{
flag_guiding_decls = 1;
name_mangling_version = 0;
}
! else if (!strcmp (p, "no-guiding-decls"))
flag_guiding_decls = 0;
! else if (!strcmp (p, "external-templates"))
{
flag_external_templates = 1;
cp_deprecated ("-fexternal-templates");
}
! else if (!strcmp (p, "new-abi"))
{
flag_new_abi = 1;
flag_do_squangling = 1;
flag_vtable_thunks = 1;
}
! else if (!strcmp (p, "no-new-abi"))
{
flag_new_abi = 0;
flag_do_squangling = 0;
}
! else if (!strncmp (p, "template-depth-", 15))
max_tinst_depth
= read_integral_parameter (p + 15, p - 2, max_tinst_depth);
! else if (!strncmp (p, "name-mangling-version-", 22))
name_mangling_version
= read_integral_parameter (p + 22, p - 2, name_mangling_version);
! else if (!strncmp (p, "message-length=", 15))
set_message_length
(read_integral_parameter (p + 15, p - 2,
/* default line-wrap length */ 72));
! else if (!strncmp (p, "diagnostics-show-location=", 26))
{
! if (!strncmp (p + 26, "once", 4))
set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
! else if (!strncmp (p + 26, "every-line", 10))
set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE);
else
error ("Unrecognized option `%s'", p - 2);
}
! else if (!strncmp (p, "dump-translation-unit-", 22))
{
if (p[22] == '\0')
error ("no file specified with -fdump-translation-unit");
--- 595,664 ----
p += 2;
/* Try special -f options. */
! if (match_string (p, "handle-exceptions")
! || match_string (p, "no-handle-exceptions"))
warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
! else if (match_string (p, "all-virtual")
! || match_string (p, "enum-int-equiv")
! || match_string (p, "no-nonnull-objects")
! || match_string (p, "this-is-variable"))
warning ("-f%s is no longer supported", p);
! else if (match_string (p, "alt-external-templates"))
{
flag_external_templates = 1;
flag_alt_external_templates = 1;
cp_deprecated ("-falt-external-templates");
}
! else if (match_string (p, "no-alt-external-templates"))
flag_alt_external_templates = 0;
! else if (match_string (p, "repo"))
{
flag_use_repository = 1;
flag_implicit_templates = 0;
}
! else if (match_string (p, "guiding-decls"))
{
flag_guiding_decls = 1;
name_mangling_version = 0;
}
! else if (match_string (p, "no-guiding-decls"))
flag_guiding_decls = 0;
! else if (match_string (p, "external-templates"))
{
flag_external_templates = 1;
cp_deprecated ("-fexternal-templates");
}
! else if (match_string (p, "new-abi"))
{
flag_new_abi = 1;
flag_do_squangling = 1;
flag_vtable_thunks = 1;
}
! else if (match_string (p, "no-new-abi"))
{
flag_new_abi = 0;
flag_do_squangling = 0;
}
! else if (match_string (p, "template-depth-"))
max_tinst_depth
= read_integral_parameter (p + 15, p - 2, max_tinst_depth);
! else if (match_string (p, "name-mangling-version-"))
name_mangling_version
= read_integral_parameter (p + 22, p - 2, name_mangling_version);
! else if (match_string (p, "message-length="))
set_message_length
(read_integral_parameter (p + 15, p - 2,
/* default line-wrap length */ 72));
! else if (match_string (p, "diagnostics-show-location="))
{
! if (match_string (p + 26, "once"))
set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
! else if (match_string (p + 26, "every-line"))
set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE);
else
error ("Unrecognized option `%s'", p - 2);
}
! else if (match_string (p, "dump-translation-unit-"))
{
if (p[22] == '\0')
error ("no file specified with -fdump-translation-unit");
*************** lang_decode_option (argc, argv)
*** 702,776 ****
if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
setting = 0, p += 3;
! if (!strcmp (p, "implicit"))
warn_implicit = setting;
! else if (!strcmp (p, "long-long"))
warn_long_long = setting;
! else if (!strcmp (p, "return-type"))
warn_return_type = setting;
! else if (!strcmp (p, "ctor-dtor-privacy"))
warn_ctor_dtor_privacy = setting;
! else if (!strcmp (p, "write-strings"))
warn_write_strings = setting;
! else if (!strcmp (p, "cast-qual"))
warn_cast_qual = setting;
! else if (!strcmp (p, "char-subscripts"))
warn_char_subscripts = setting;
! else if (!strcmp (p, "pointer-arith"))
warn_pointer_arith = setting;
! else if (!strcmp (p, "missing-prototypes"))
warn_missing_prototypes = setting;
! else if (!strcmp (p, "redundant-decls"))
warn_redundant_decls = setting;
! else if (!strcmp (p, "missing-braces"))
warn_missing_braces = setting;
! else if (!strcmp (p, "sign-compare"))
warn_sign_compare = setting;
! else if (!strcmp (p, "float-equal"))
warn_float_equal = setting;
! else if (!strcmp (p, "format"))
warn_format = setting;
! else if (!strcmp (p, "conversion"))
warn_conversion = setting;
! else if (!strcmp (p, "parentheses"))
warn_parentheses = setting;
! else if (!strcmp (p, "non-virtual-dtor"))
warn_nonvdtor = setting;
! else if (!strcmp (p, "extern-inline"))
warn_extern_inline = setting;
! else if (!strcmp (p, "reorder"))
warn_reorder = setting;
! else if (!strcmp (p, "synth"))
warn_synth = setting;
! else if (!strcmp (p, "pmf-conversions"))
warn_pmf2ptr = setting;
! else if (!strcmp (p, "effc++"))
warn_ecpp = setting;
! else if (!strcmp (p, "sign-promo"))
warn_sign_promo = setting;
! else if (!strcmp (p, "old-style-cast"))
warn_old_style_cast = setting;
! else if (!strcmp (p, "overloaded-virtual"))
warn_overloaded_virtual = setting;
! else if (!strcmp (p, "multichar"))
warn_multichar = setting;
! else if (!strcmp (p, "unknown-pragmas"))
/* Set to greater than 1, so that even unknown pragmas in
system headers will be warned about. */
warn_unknown_pragmas = setting * 2;
! else if (!strcmp (p, "non-template-friend"))
warn_nontemplate_friend = setting;
! else if (!strcmp (p, "deprecated"))
warn_deprecated = setting;
! else if (!strcmp (p, "comment"))
; /* cpp handles this one. */
! else if (!strcmp (p, "comments"))
; /* cpp handles this one. */
! else if (!strcmp (p, "trigraphs"))
; /* cpp handles this one. */
! else if (!strcmp (p, "import"))
; /* cpp handles this one. */
! else if (!strcmp (p, "all"))
{
warn_return_type = setting;
set_Wunused (setting);
--- 702,776 ----
if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
setting = 0, p += 3;
! if (match_string (p, "implicit"))
warn_implicit = setting;
! else if (match_string (p, "long-long"))
warn_long_long = setting;
! else if (match_string (p, "return-type"))
warn_return_type = setting;
! else if (match_string (p, "ctor-dtor-privacy"))
warn_ctor_dtor_privacy = setting;
! else if (match_string (p, "write-strings"))
warn_write_strings = setting;
! else if (match_string (p, "cast-qual"))
warn_cast_qual = setting;
! else if (match_string (p, "char-subscripts"))
warn_char_subscripts = setting;
! else if (match_string (p, "pointer-arith"))
warn_pointer_arith = setting;
! else if (match_string (p, "missing-prototypes"))
warn_missing_prototypes = setting;
! else if (match_string (p, "redundant-decls"))
warn_redundant_decls = setting;
! else if (match_string (p, "missing-braces"))
warn_missing_braces = setting;
! else if (match_string (p, "sign-compare"))
warn_sign_compare = setting;
! else if (match_string (p, "float-equal"))
warn_float_equal = setting;
! else if (match_string (p, "format"))
warn_format = setting;
! else if (match_string (p, "conversion"))
warn_conversion = setting;
! else if (match_string (p, "parentheses"))
warn_parentheses = setting;
! else if (match_string (p, "non-virtual-dtor"))
warn_nonvdtor = setting;
! else if (match_string (p, "extern-inline"))
warn_extern_inline = setting;
! else if (match_string (p, "reorder"))
warn_reorder = setting;
! else if (match_string (p, "synth"))
warn_synth = setting;
! else if (match_string (p, "pmf-conversions"))
warn_pmf2ptr = setting;
! else if (match_string (p, "effc++"))
warn_ecpp = setting;
! else if (match_string (p, "sign-promo"))
warn_sign_promo = setting;
! else if (match_string (p, "old-style-cast"))
warn_old_style_cast = setting;
! else if (match_string (p, "overloaded-virtual"))
warn_overloaded_virtual = setting;
! else if (match_string (p, "multichar"))
warn_multichar = setting;
! else if (match_string (p, "unknown-pragmas"))
/* Set to greater than 1, so that even unknown pragmas in
system headers will be warned about. */
warn_unknown_pragmas = setting * 2;
! else if (match_string (p, "non-template-friend"))
warn_nontemplate_friend = setting;
! else if (match_string (p, "deprecated"))
warn_deprecated = setting;
! else if (match_string (p, "comment"))
; /* cpp handles this one. */
! else if (match_string (p, "comments"))
; /* cpp handles this one. */
! else if (match_string (p, "trigraphs"))
; /* cpp handles this one. */
! else if (match_string (p, "import"))
; /* cpp handles this one. */
! else if (match_string (p, "all"))
{
warn_return_type = setting;
set_Wunused (setting);
*************** lang_decode_option (argc, argv)
*** 798,811 ****
}
else return strings_processed;
}
! else if (!strcmp (p, "-ansi"))
flag_no_nonansi_builtin = 1, flag_ansi = 1,
flag_no_gnu_keywords = 1;
#ifdef SPEW_DEBUG
/* Undocumented, only ever used when you're invoking cc1plus by hand, since
it's probably safe to assume no sane person would ever want to use this
under normal circumstances. */
! else if (!strcmp (p, "-spew-debug"))
spew_debug = 1;
#endif
else
--- 798,811 ----
}
else return strings_processed;
}
! else if (match_string (p, "-ansi"))
flag_no_nonansi_builtin = 1, flag_ansi = 1,
flag_no_gnu_keywords = 1;
#ifdef SPEW_DEBUG
/* Undocumented, only ever used when you're invoking cc1plus by hand, since
it's probably safe to assume no sane person would ever want to use this
under normal circumstances. */
! else if (match_string (p, "-spew-debug"))
spew_debug = 1;
#endif
else
*************** grokfield (declarator, declspecs, init,
*** 1636,1642 ****
if (DECL_NAME (value) != NULL_TREE
&& IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
! && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
cp_error ("member `%D' conflicts with virtual function table field name",
value);
--- 1636,1642 ----
if (DECL_NAME (value) != NULL_TREE
&& IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
! && match_string (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
cp_error ("member `%D' conflicts with virtual function table field name",
value);