cobol: Minor changes to genapi.cc to eliminate CPPCHECK warnings.

Several hundred cppcheck warnings were eliminated.

Most of these changes were replacing C-style casts, checking for NULL
pointers, establishing some variables and formal parameters as const,
and moving some variables around to tidy up their scopes.

One memory leak was found and eliminated as a result of the cppcheck.

gcc/cobol/ChangeLog:

	* Make-lang.in: Eliminate the .cc.o override.
	* genapi.cc (level_88_helper): Eliminate cppcheck warning.
	(get_level_88_domain): Likewise.
	(get_class_condition_string): Likewise.
	(parser_call_targets_dump): Likewise.
	(parser_compile_ecs): Likewise.
	(initialize_variable_internal): Likewise.
	(move_tree): Likewise.
	(combined_name): Likewise.
	(assembler_label): Likewise.
	(find_procedure): Likewise.
	(parser_perform): Likewise.
	(parser_perform_times): Likewise.
	(internal_perform_through): Likewise.
	(internal_perform_through_times): Likewise.
	(psa_FldLiteralN): Likewise.
	(psa_FldBlob): Likewise.
	(parser_accept): Likewise.
	(parser_accept_exception): Likewise.
	(parser_accept_exception_end): Likewise.
	(parser_accept_command_line): Likewise.
	(parser_accept_envar): Likewise.
	(parser_display_internal): Likewise.
	(parser_display): Likewise.
	(parser_assign): Likewise.
	(parser_initialize_table): Likewise.
	(parser_arith_error): Likewise.
	(parser_arith_error_end): Likewise.
	(parser_division): Likewise.
	(label_fetch): Likewise.
	(parser_label_label): Likewise.
	(parser_label_goto): Likewise.
	(parser_perform_start): Likewise.
	(parser_perform_conditional): Likewise.
	(parser_perform_conditional_end): Likewise.
	(parser_perform_until): Likewise.
	(parser_file_delete): Likewise.
	(parser_intrinsic_subst): Likewise.
	(create_lsearch_address_pairs): Likewise.
	(parser_bsearch_start): Likewise.
	(is_ascending_key): Likewise.
	(parser_sort): Likewise.
	(parser_file_sort): Likewise.
	(parser_return_start): Likewise.
	(parser_file_merge): Likewise.
	(parser_string_overflow): Likewise.
	(parser_unstring): Likewise.
	(parser_string): Likewise.
	(parser_call_exception): Likewise.
	(create_and_call): Likewise.
	(mh_identical): Likewise.
	(move_helper): Likewise.
	(binary_initial_from_float128): Likewise.
	(initial_from_initial): Likewise.
	(psa_FldLiteralA): Likewise.
	(parser_local_add): Likewise.
	(parser_symbol_add): Likewise.
	* genapi.h (parser_display): Likewise.
	* gengen.cc (gg_call_expr): Explict check for NULL_TREE.
	(gg_call): Likewise.
	* show_parse.h (SHOW_PARSE_LABEL_OK): Likewise.
	(TRACE1_FIELD_VALUE): Likewise.
	(CHECK_FIELD): Likewise.
	(CHECK_FIELD2): Likewise.
	(CHECK_LABEL): Likewise.
	* util.cc (cbl_internal_error): Apply [[noreturn]] attribute.
	* util.h (cbl_internal_error): Likewise.

libgcobol/ChangeLog:

	* common-defs.h (PTRCAST): Moved here from libgcobol.h.
	* libgcobol.h (PTRCAST): Deleted.
This commit is contained in:
Robert Dubner
2025-07-11 17:11:21 -04:00
parent 598455fd73
commit 9b9753718e
9 changed files with 334 additions and 203 deletions

View File

@ -385,22 +385,12 @@ selftest-cobol:
lang_checks += check-cobol
#
# Front-end specific flags: Originally done for the COBOL front end, this
# scripting applies CXXFLAGS_FOR_COBOL only to compilations of source code in the
# gcc/cobol source code tree. Both forms can be used:
#
# CXXFLAGS_FOR_COBOL=xxx ../configure --enable-languages=....
# and
# make <gcc> CXXFLAGS_FOR_COBOL=yyy
#
# The second form overrides the first.
#
# To apply this feature to other front ends, look for and clone lines
# containing "CXXFLAGS_FOR_COBOL" in configure.ac, Makefile.tbl, and Makefile.def.
#
cobol/%.o: cobol/%.cc
@echo $(COMPILE) $(CXXFLAGS_FOR_COBOL) $<
$(COMPILE) $(CXXFLAGS_FOR_COBOL) $<
$(POSTCOMPILE)
###
### Note that the process environment variable CXXFLAGS_FOR_COBOL is applied to
### gcc/cobol compilations. This is not a configuration-level variable.
###
##
##cobol/%.o: cobol/%.cc
## @echo $(COMPILE) $(CXXFLAGS_FOR_COBOL) $<
## $(COMPILE) $(CXXFLAGS_FOR_COBOL) $<
## $(POSTCOMPILE)

File diff suppressed because it is too large Load Diff

View File

@ -268,8 +268,8 @@ void
parser_display( const struct cbl_special_name_t *upon,
std::vector<cbl_refer_t> args,
bool advance = DISPLAY_ADVANCE,
cbl_label_t *not_error = nullptr,
cbl_label_t *compute_error = nullptr );
const cbl_label_t *not_error = nullptr,
const cbl_label_t *compute_error = nullptr );
void parser_display_field(cbl_field_t *fld);

View File

@ -3058,7 +3058,7 @@ gg_call_expr(tree return_type, const char *function_name, ...)
tree arg = va_arg(ap, tree);
if( !arg )
if( arg == NULL_TREE )
{
break;
}
@ -3114,7 +3114,7 @@ gg_call(tree return_type, const char *function_name, ...)
tree arg = va_arg(ap, tree);
if( !arg )
if( arg == NULL_TREE )
{
break;
}

View File

@ -176,11 +176,21 @@ extern bool cursor_at_sol;
} \
else \
{ \
fprintf(stderr, " %p:%s (%s)", (void*)b, b->name, b->type_str()); \
fprintf(stderr, " %p:%s (%s)", static_cast<void*>(b), b->name, b->type_str()); \
} \
show_parse_sol = false; \
} while(0);
// Use this version when b is known to be valid. This is necessary to quiet
// cppcheck nullPointerRedundantCheck warnings
#define SHOW_PARSE_LABEL_OK(a, b) \
do \
{ \
fprintf(stderr, "%s", a); \
fprintf(stderr, " %p:%s (%s)", static_cast<void*>(b), b->name, b->type_str()); \
show_parse_sol = false; \
} while(0);
#define TRACE1 if(bTRACE1)
#define TRACE1_HEADER do \
{ \
@ -211,6 +221,7 @@ extern bool cursor_at_sol;
#define TRACE1_FIELD_VALUE(a, field, b) \
do \
{ \
gcc_assert(field); \
cursor_at_sol=false; \
if ( field->type == FldConditional ) \
{ \
@ -423,13 +434,39 @@ extern bool cursor_at_sol;
} while(0);
// Use CHECK_FIELD when a should be non-null, and a->var_decl_node also should
// by non-null:
// by non-null. (The useless calls to abort() are because cppcheck doesn't
// understand that gcc_unreachable doesn't return);
// Use this after doing any SHOW_PARSE stuff, to avoid cppcheck complaints
// about nullPointerRedundantCheck
#define CHECK_FIELD(a) \
do { \
do { \
if(!a) \
{ \
yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
gcc_unreachable(); \
abort(); \
} \
if( !a->var_decl_node ) \
{ \
yywarn("%s: parameter %<" #a "%> is variable " \
"%s<%s> with NULL %<var_decl_node%>", \
__func__, \
a->name, \
cbl_field_type_str(a->type) ); \
gcc_unreachable(); \
abort(); \
} \
} while(0);
// This version is a bit more lax, for special cases
#define CHECK_FIELD2(a) \
do { \
if(!a) \
{ \
yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
gcc_unreachable(); \
abort(); \
} \
if( !a->var_decl_node && a->type != FldConditional && a->type != FldLiteralA) \
{ \
@ -439,15 +476,18 @@ extern bool cursor_at_sol;
a->name, \
cbl_field_type_str(a->type) ); \
gcc_unreachable(); \
abort(); \
} \
} while(0);
#define CHECK_LABEL(a) \
do{ \
if(!a) \
{ \
yywarn("%s: parameter %<" #a "%> is NULL", __func__); \
gcc_unreachable(); \
abort(); \
} \
}while(0);
@ -504,6 +544,7 @@ class ANALYZE
}
};
#else
// cppcheck-suppress ctuOneDefinitionRuleViolation
class ANALYZE
{
public:

View File

@ -2448,6 +2448,8 @@ cbl_internal_error(const char *gmsgid, ...) {
va_start(ap, gmsgid);
emit_diagnostic_valist( DK_ICE, token_location, option_zero, gmsgid, &ap );
va_end(ap);
abort(); // This unnecessary statement is needed so that [[noreturn]]
// // doesn't cause a warning.
}
void

View File

@ -33,7 +33,7 @@
void cbl_message(int fd, const char *format_string, ...)
ATTRIBUTE_PRINTF_2;
void cbl_internal_error(const char *format_string, ...)
[[noreturn]] void cbl_internal_error(const char *format_string, ...)
ATTRIBUTE_GCOBOL_DIAG(1, 2);
void cbl_err(const char *format_string, ...) ATTRIBUTE_GCOBOL_DIAG(1, 2);

View File

@ -84,6 +84,14 @@
#define MINIMUM_ALLOCATION_SIZE 16
// This was part of an exercise to make cppcheck shut up about invalid
// pointer type conversions.
// It was also to avoid having reinterpret_cast<> all over the place.
// So, instead of reinterpret_cast<char *>(VALUE)
// I sometimes use PTRCAST(char, VALUE)
// Note that "(char *)" is implied by "PTRCAST(char, VALUE)"
#define PTRCAST(TYPE, VALUE) static_cast<TYPE *>(static_cast<void *>(VALUE))
/*
* User-defined names in IBM COBOL can have at most 30 characters.
* For DBCS, the maximum is 14.

View File

@ -47,13 +47,6 @@ extern void __gg__mabort();
// malloc().
#define massert(p) if(!p){__gg__mabort();abort();}
// This was part of an exercise to make cppcheck shut up about invalid
// pointer type conversions.
// It was also to avoid having reinterpret_cast<> all over the place.
// So, instead of reinterpret_cast<char *>(VALUE)
// I sometimes use PTRCAST(char, VALUE)
#define PTRCAST(TYPE, VALUE) static_cast<TYPE *>(static_cast<void *>(VALUE))
extern "C" __int128 __gg__power_of_ten(int n);
extern "C" __int128 __gg__dirty_to_binary_source( const char *dirty,