Skip to content

Commit f298fab

Browse files
author
James K. Lowden
committed
cobol: Some 1000 small changes in answer to cppcheck diagnostics.
constification per cppcheck. Use STRICT_WARN and fix reported diagnostics. Ignore [shadowVariable] in general. Use std::vector to avoid exposing arrays as raw pointers. PR cobol/120621 gcc/cobol/ChangeLog: * Make-lang.in: Use STRICT_WARN. * cbldiag.h (location_dump): suppress shadowVariable. * cdf-copy.cc (esc): Fix shadowVariable. (copybook_elem_t::open_file): Do not use %m. * cdf.y: suppress invalidPrintfArgType for target format. * cdfval.h (struct cdfval_t): Suppress noExplicitConstructor. * cobol1.cc (cobol_name_mangler): Use C++ cast. * copybook.h (class copybook_elem_t): Same. * dts.h: Fixes and suppressions due to cppcheck. * except.cc (cbl_enabled_exceptions_t::status): Suppress useStlAlgorithm. (cbl_enabled_exceptions_t::turn_on_off): Const parameter. (class choose_declarative): Removed. * genapi.cc (struct called_tree_t): Explicit constructor. (parser_compile_ecs): Cast to void * for %p. (parser_compile_dcls): Same. (parser_statement_begin): Same. (initialize_variable_internal): Use std::vector for subscripts. (parser_initialize): Constification. (get_string_from): Same. (combined_name): Same. (parser_perform): Same. (psa_FldLiteralN): Same. (is_figconst): Const parameter. (is_figconst_t): Same. (parser_exit): Same. (parser_division): Const pointer. (parser_perform_conditional): Whitespace. (parser_set_conditional88): Const parameter. (inspect_tally): Use std::vector. (inspect_replacing): Same. (parser_inspect): Same. (parser_intrinsic_subst): Use std::vector (constuct elements). (parser_intrinsic_call_1): Use std::vector for subscripts. (is_ascending_key): Const pointer. (parser_sort): Use std::vector. (parser_file_sort): Same. (parser_file_merge): Same. (parser_unstring): Same. (parser_string): Same. (parser_call): Const pointer. (parser_program_hierarchy): Use std::vector. (conditional_abs): Const paraemeter. (float_type_of): Same. (initial_from_initial): Set value, quoted or not. (parser_symbol_add): Remove redundant nested test. * genapi.h (parser_add): Const parameters. (parser_subtract): Same. (parser_multiply): Same. (parser_divide): Same. (parser_perform): Same. (parser_exit): Same. (parser_initialize): Same. (parser_set_conditional88): Same. (parser_sort): Same. (parser_file_sort): Same. (parser_file_merge): Same. (parser_string): Same. (is_ascending_key): Same. * genmath.cc (arithmetic_operation): Use std::vector. (is_somebody_float): Const parameter. (all_results_binary): Const parameter. (fast_multiply): Remove redundant nested test. (parser_add): Const parameter. (parser_multiply): Remove redundant nested test. (parser_divide): Const parameter. (parser_subtract): Same. * genutil.cc (get_depending_on_value): Use std::vector. (get_data_offset): Same. (tree_type_from_field): Const parameter. (refer_has_depends): Const pointers. (get_literal_string): RAII. (refer_is_clean): Use std::vector. (get_time_nanoseconds): Newline at EOF. * genutil.h (tree_type_from_field): Remove declaration. * inspect.h (struct cbx_inspect_qual_t): Use std::vector. (struct cbl_inspect_qual_t): Same. (struct cbx_inspect_match_t): Same. (class cbl_inspect_match_t): Same. (struct cbx_inspect_replace_t): Same. (struct cbl_inspect_replace_t): Same. (struct cbx_inspect_oper_t): Same. (struct cbl_inspect_oper_t): Same. (struct cbx_inspect_t): Same. (struct cbl_inspect_t): Same. (parser_inspect): Same. * lexio.cc (indicated): Const pointer. (remove_inline_comment): Scope reduction. (maybe_add_space): Const pointer. (recognize_replacements): C++ cast. (check_source_format_directive): Same. (struct replacing_term_t): Explicit constructor. (parse_replace_pairs): Const reference. (location_in): Const reference. (parse_copy_directive): C++ cast. (parse_replace_last_off): Const parameter. (parse_replace_text): Const reference. (parse_replace_directive): C++ cast. (cdftext::lex_open): Const reference. (cdftext::open_output): Scope reduction. (cdftext::free_form_reference_format): Remove unused variable. (cdftext::process_file): Simplify. * lexio.h (struct bytespan_t): Use nullptr. (struct filespan_t): Initialize icol in constructor. (struct span_t): Suppress confused operatorEqRetRefThis. (struct replace_t): Eliminate single-value constructor. * parse.y: Many const cppcheck reports, and portable bit-shift. * parse_ante.h (reject_refmod): Const parameter. (require_pointer): Same. (require_integer): Same. (struct evaluate_elem_t): Explicit constructor. (struct arith_t): Use std::vector. (class eval_subject_t): Const parameter. (dump_inspect_match): Declare. (struct perform_t): Explicit constructor. (list_add): Const parameter. (class tokenset_t): Avoid negative array index. (struct file_list_t): Explicit constructor. (struct field_list_t): Same. (struct refer_list_t): Same. (struct refer_marked_list_t): Const parameter. (struct refer_collection_t): Explicit constructor. (struct ast_inspect_oper_t): Remove class. (ast_inspect_oper_t): Same. (struct ast_inspect_t): Same. (struct ast_inspect_list_t): Same. (ast_inspect): Add location. (struct elem_list_t): Explicit constructor. (struct unstring_tgt_t): Same. (struct unstring_tgt_list_t): Same. (struct unstring_into_t): Same. (struct ffi_args_t): Same. (struct file_sort_io_t): Same. (merge_t): Same. (struct vargs_t): Same. (class prog_descr_t): Eliminate single-value constructor. (class program_stack_t): Suppress useStlAlgorithm. (struct rel_part_t): Eliminate single-value constructor. (class log_expr_t): Explicit constructor. (add_debugging_declarative): Rename local variable. (intrinsic_call_2): Const parameter. (invalid_key): Use std::find_if. (parser_add2): Const parameter. (parser_subtract2): Same. (stringify): Same. (unstringify): Same. (anybody_redefines): Same. (ast_call): Same. * parse_util.h (class cname_cmp): Explicit constructor. (intrinsic_inconsistent_parameter): Same. * scan_ante.h (struct cdf_status_t): Eliminate single-value constructor. (class enter_leave_t): Explicit constructor. (update_location): Const pointer, explicit constructor. (symbol_function_token): Const pointer. (typed_name): Same. * scan_post.h (datetime_format_of): Scope reduction. * show_parse.h (class ANALYZE): Use std::vector, explicit consstructor. * symbols.cc (symbol_table_extend): Scope reduction. (cbl_ffi_arg_t::cbl_ffi_arg_t): Define default constructor. (end_of_group): Const pointer. (symbol_find_odo): Const parameter. (rename_not_ok): Same. (field_str): Use %u instead of %d. (struct capacity_of): Const pointer. (symbols_update): Same. (symbol_field_parent_set): Same. (symbol_file_add): Same. (symbol_typedef_add): Same. (symbol_field_add): Use new operator=(). (symbol_field): Suppress CastIntegerToAddressAtReturn. (symbol_register): Same. (symbol_file): Suppress knownConditionTrueFalse. (next_program): Const parameter. (symbol_file_record): Same. (class is_section): Explicit constructor. (cbl_file_t::no_key): Remove. (cbl_prog_hier_t::cbl_prog_hier_t): Use std::vector. (symbol_label_add): Assert pointer is not NULL. (symbol_label_section_exists): Const reference in lambda. (expand_picture): Use C++ cast. (symbol_program_callables): Const pointer. (symbol_currency_add): Suppress nullPointerRedundantCheck. (cbl_key_t): Use std::vector. (cbl_occurs_t::field_add): Const parameter. (cbl_occurs_t::index_add): Explicit constructor. (class is_field_at): Same. (cbl_file_key_t::deforward): Scope reduction. (cbl_file_t::keys_str): Use allocated memory only. (file_status_status_of): Const pointer. (is_register_field): Const parameter. * symbols.h (struct cbl_field_data_t): Eliminate single-value constructor. (struct cbl_occurs_bounds_t): Same. (struct cbl_refer_t): Use std::vector. (valid_move): Const parameter. (is_register_field): Same. (struct cbl_key_t): Use std::vector. (struct cbl_substitute_t): Eliminate single-value constructor. (refer_of): Return const reference (struct cbl_ffi_arg_t): Eliminate single-value constructor. (class temporaries_t): Same. (struct cbl_file_key_t): Define default constructor. (struct cbl_file_lock_t): Define copy constructor and operator=(). (struct cbl_file_t): Complete default constructor. (struct symbol_elem_t): Explicit constructor. (symbol_elem_of): Suppress cstyleCast. (symbol_redefines): Const parameter. (struct cbl_field_t): Same. (cbl_section_of): Test for NULL pointer. (cbl_field_of): Same. (cbl_label_of): Same. (cbl_special_name_of): Same. (cbl_alphabet_of): Same. (cbl_file_of): Same. (is_figconst): Delete extra "struct" keyword. (is_figconst_low): Same. (is_figconst_zero): Same. (is_figconst_space): Same. (is_figconst_quote): Same. (is_figconst_high): Same. (is_space_value): Same. (is_quoted): Same. (symbol_index): Const parameter. (struct cbl_prog_hier_t): Suppress noExplicitConstructor. (struct cbl_perform_vary_t): Eliminate single-value constructor. (is_signable): Const parameter. (is_temporary): Same. (rename_not_ok): Same. (field_at): Test for NULL pointer. (class procref_base_t): Eliminate single-value constructor. * symfind.cc (is_data_field): Const pointer. (finalize_symbol_map2): Same. (class in_scope): Same. (symbol_match2): Same. * token_names.h: Suppress useInitializationList. * util.cc (normalize_picture): Whitespace and remove extra "continue". (redefine_field): Const pointer. (cbl_field_t::report_invalid_initial_value): Same. (literal_subscript_oob): Rename shadow variable. (cbl_refer_t::subscripts_set): Use std::vector. (cbl_refer_t::str): Same. (cbl_refer_t::deref_str): Same. (locally_unique): Use explicit constructor. (ambiguous_reference): Same. (class unique_stack): Use const reference. (cobol_filename): Const pointer. (verify_format): Scope reduction. (class temp_loc_t): Do not derive from YYLTYPE. (cobol_parse_files): Const pointer. * util.h (as_voidp): Define convenient converter. libgcobol/ChangeLog: * common-defs.h (class cbl_enabled_exceptions_t): Const parameter.
1 parent a26c5fc commit f298fab

30 files changed

+1125
-1066
lines changed

gcc/cobol/Make-lang.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
# - the compiler proper (eg: cc1plus)
3535
# - define the names for selecting the language in LANGUAGES.
3636

37+
# Use strict warnings for this front end.
38+
cobol-warn = $(STRICT_WARN)
39+
3740
GCOBOL_INSTALL_NAME := $(shell echo gcobol|sed '$(program_transform_name)')
3841
GCOBOLIO_INSTALL_NAME := $(shell echo gcobol-io|sed '$(program_transform_name)')
3942
GCOBOL_TARGET_INSTALL_NAME := $(target_noncanonical)-$(shell echo gcobol|sed '$(program_transform_name)')

gcc/cobol/cbldiag.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void gcc_location_set( const YYLTYPE& loc );
106106
template <typename LOC>
107107
static void
108108
location_dump( const char func[], int line, const char tag[], const LOC& loc) {
109-
extern int yy_flex_debug;
109+
extern int yy_flex_debug; // cppcheck-suppress shadowVariable
110110
if( yy_flex_debug && gcobol_getenv("update_location") )
111111
fprintf(stderr, "%s:%d: %s location (%d,%d) to (%d,%d)\n",
112112
func, line, tag,

gcc/cobol/cdf-copy.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ verify_bounds( size_t pos, size_t size, const char input[] ) {
124124
*/
125125
const char *
126126
esc( size_t len, const char input[] ) {
127-
static char spaces[] = "([,;]?[[:space:]])+";
128-
static char spaceD[] = "(\n {6}D" "|" "[,;]?[[:space:]])+";
127+
static const char space[] = "([,;]?[[:space:]])+";
128+
static const char spaceD[] = "(\n {6}D" "|" "[,;]?[[:space:]])+";
129129
static char buffer[64 * 1024];
130130
char *p = buffer;
131131
const char *eoinput = input + len;
132132

133-
const char *spacex = is_reference_format()? spaceD : spaces;
133+
const char *spacex = is_reference_format()? spaceD : space;
134134

135135
for( const char *s=input; *s && s < eoinput; s++ ) {
136136
*p = '\0';
@@ -279,7 +279,7 @@ copybook_elem_t::open_file( const char directory[], bool literally ) {
279279
dbgmsg("copybook_elem_t::open_file: trying %s", path);
280280

281281
if( (this->fd = open(path, O_RDONLY)) == -1 ) {
282-
dbgmsg("could not open %s: %m", path);
282+
dbgmsg("could not open %s: %s", path, xstrerror(errno));
283283
return fd;
284284
}
285285
this->source.name = path;

gcc/cobol/cdf.y

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ apply_cdf_turn( const exception_turn_t& turn ) {
185185
%printer { fprintf(yyo, "%s '%s'",
186186
keyword_str($$.token),
187187
$$.string? $$.string : "<nil>" ); } <cdfarg>
188+
/* cppcheck-suppress invalidPrintfArgType_sint */
188189
%printer { fprintf(yyo, HOST_SIZE_T_PRINT_DEC " '%s'",
189190
(fmt_size_t)$$.number, $$.string? $$.string : "" ); } <cdfval>
190191

@@ -891,9 +892,9 @@ verify_integer( const YDFLTYPE& loc, const cdfval_base_t& val ) {
891892
return true;
892893
}
893894

894-
// cppcheck-suppress returnTempReference
895895
const cdfval_base_t&
896896
cdfval_base_t::operator()( const YDFLTYPE& loc ) {
897897
static cdfval_t zero(0);
898+
// cppcheck-suppress returnTempReference
898899
return verify_integer(loc, *this) ? *this : zero;
899900
}

gcc/cobol/cdfval.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ struct cdfval_t : public cdfval_base_t {
7373
cdfval_base_t::string = NULL;
7474
cdfval_base_t::number = 0;
7575
}
76-
cdfval_t( const char value[] )
76+
cdfval_t( const char value[] ) // cppcheck-suppress noExplicitConstructor
7777
: lineno(yylineno), filename(cobol_filename())
7878
{
7979
cdfval_base_t::off = false;
8080
cdfval_base_t::string = value;
8181
cdfval_base_t::number = 0;
8282
}
83-
cdfval_t( long long value )
83+
cdfval_t( long long value ) // cppcheck-suppress noExplicitConstructor
8484
: lineno(yylineno), filename(cobol_filename())
8585
{
8686
cdfval_base_t::off = false;
8787
cdfval_base_t::string = NULL;
8888
cdfval_base_t::number = value;
8989
}
90-
cdfval_t( long value )
90+
cdfval_t( long value ) // cppcheck-suppress noExplicitConstructor
9191
: lineno(yylineno), filename(cobol_filename())
9292
{
9393
cdfval_base_t::off = false;
9494
cdfval_base_t::string = NULL;
9595
cdfval_base_t::number = value;
9696
}
97-
cdfval_t( int value )
97+
cdfval_t( int value ) // cppcheck-suppress noExplicitConstructor
9898
: lineno(yylineno), filename(cobol_filename())
9999
{
100100
cdfval_base_t::off = false;
@@ -109,7 +109,7 @@ struct cdfval_t : public cdfval_base_t {
109109
HOST_WIDE_INT value = real_to_integer(&r);
110110
cdfval_base_t::number = value;
111111
}
112-
cdfval_t( const cdfval_base_t& value )
112+
cdfval_t( const cdfval_base_t& value ) // cppcheck-suppress noExplicitConstructor
113113
: lineno(yylineno), filename(cobol_filename())
114114
{
115115
cdfval_base_t *self(this);

gcc/cobol/cobol1.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ cobol_name_mangler(const char *cobol_name_)
571571
}
572572

573573
// Allocate enough space for a prepended underscore and a final '\0'
574-
char *cobol_name = (char *)xmalloc(strlen(cobol_name_)+2);
574+
char *cobol_name = static_cast<char *>(xmalloc(strlen(cobol_name_)+2));
575575
size_t n = 0;
576576
if( cobol_name_[0] >= '0' && cobol_name_[0] <= '9' )
577577
{

gcc/cobol/copybook.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class copybook_elem_t {
100100
}
101101
static char * dequote( const char orig[] ) {
102102
gcc_assert(quoted(orig));
103-
auto name = (char*)xcalloc(1, strlen(orig));
103+
auto name = static_cast<char*>(xcalloc(1, strlen(orig)));
104104
gcc_assert(name);
105105
char *tgt = name;
106106

gcc/cobol/dts.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ namespace dts {
3535
{
3636
static regmatch_t empty;
3737
empty.rm_so = empty.rm_eo = -1;
38-
regmatch_t& self(*this);
38+
regmatch_t& self(*this); // cppcheck-suppress constVariableReference
3939
self = empty;
4040
}
4141
csub_match( const char input[], const regmatch_t& m )
4242
: input(input)
4343
{
44-
regmatch_t& self(*this);
44+
regmatch_t& self(*this); // cppcheck-suppress constVariableReference
4545
self = m;
4646
matched = rm_so != -1;
4747
first = rm_so == -1? NULL : input + rm_so;
@@ -68,7 +68,6 @@ namespace dts {
6868
#if __cpp_exceptions
6969
throw std::logic_error(msg);
7070
#else
71-
pattern = NULL;
7271
cbl_errx("%s", msg);
7372
#endif
7473
}
@@ -78,7 +77,7 @@ namespace dts {
7877
size_t size() const { return nsubexpr; }
7978
bool ready() const { return pattern != NULL; }
8079
private:
81-
regex( const regex& ) {}
80+
regex( const regex& ) = default;
8281
};
8382

8483
inline bool regex_search( const char input[], const char *eoinput,
@@ -88,10 +87,10 @@ namespace dts {
8887
static const char msg[] = "input not NUL-terminated";
8988
throw std::domain_error( msg );
9089
#else
91-
eoinput = strchr(input, '\0');
90+
// eoinput terminates input
91+
eoinput = strchr(input, '\0'); // cppcheck-suppress uselessAssignmentPtrArg
9292
#endif
9393
}
94-
if( eoinput == NULL ) eoinput = strchr(input, '\0');
9594
auto ncm = re.size();
9695
cm.resize(ncm);
9796
std::vector <regmatch_t> cms(ncm);

gcc/cobol/except.cc

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,16 @@ cbl_enabled_exceptions_t::dump() const {
108108
std::swap(debug, yydebug);
109109
}
110110

111+
// cppcheck-suppress-begin [useStlAlgorithm] because why?
111112
uint32_t
112113
cbl_enabled_exceptions_t::status() const {
113114
uint32_t status_word = 0;
114115
for( const auto& ena : *this ) {
115116
status_word |= (EC_ALL_E & ena.ec );
116-
}
117+
}
117118
return status_word;
118119
}
120+
// cppcheck-suppress-end useStlAlgorithm
119121

120122
std::vector<uint64_t>
121123
cbl_enabled_exceptions_t::encode() const {
@@ -133,13 +135,13 @@ void
133135
cbl_enabled_exceptions_t::turn_on_off( bool enabled,
134136
bool location,
135137
ec_type_t type,
136-
std::set<size_t> files )
138+
const std::set<size_t>& files )
137139
{
138140
// Update current enabled ECs tree on leaving this function.
139141
class update_parser_t {
140142
const cbl_enabled_exceptions_t& ecs;
141143
public:
142-
update_parser_t(const cbl_enabled_exceptions_t& ecs) : ecs(ecs) {}
144+
explicit update_parser_t(const cbl_enabled_exceptions_t& ecs) : ecs(ecs) {}
143145
~update_parser_t() {
144146
tree ena = parser_compile_ecs(ecs.encode());
145147
current_enabled_ecs(ena);
@@ -244,16 +246,6 @@ cbl_enabled_exceptions_t::match( ec_type_t type, size_t file ) const {
244246
return output != end()? &*output : NULL;
245247
}
246248

247-
class choose_declarative {
248-
size_t program;
249-
public:
250-
choose_declarative( size_t program ) : program(program) {}
251-
252-
bool operator()( const cbl_declarative_t& dcl ) {
253-
return dcl.global || program == symbol_at(dcl.section)->program;
254-
}
255-
};
256-
257249
bool
258250
sort_supers_last( const cbl_declarative_t& a, const cbl_declarative_t& b ) {
259251
if( symbol_at(a.section)->program == symbol_at(b.section)->program ) {

0 commit comments

Comments
 (0)