From 0568365cba7e6608afd937275ad73eecc3fdd967 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Sat, 28 Jun 2025 16:17:23 +0200 Subject: [PATCH 1/2] Fixes for cppcoreguidelines-pro-type-cstyle-cast --- lib/binpac_buffer.cc | 4 ++-- lib/binpac_bytestring.cc | 8 +++++--- src/pac_analyzer.cc | 13 +++++++------ src/pac_btype.cc | 7 ++++--- src/pac_case.cc | 2 +- src/pac_strtype.cc | 4 ++-- 6 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/binpac_buffer.cc b/lib/binpac_buffer.cc index 915c370..444f6b5 100644 --- a/lib/binpac_buffer.cc +++ b/lib/binpac_buffer.cc @@ -101,7 +101,7 @@ void FlowBuffer::ExpandBuffer(int length) { // Allocate a new buffer and copy the existing contents buffer_length_ = length; - unsigned char* new_buf = (unsigned char*)realloc(buffer_, buffer_length_); + unsigned char* new_buf = reinterpret_cast(realloc(buffer_, buffer_length_)); if ( ! new_buf ) throw ExceptionFlowBufferAlloc("expand realloc OOM"); @@ -114,7 +114,7 @@ void FlowBuffer::ContractBuffer() { return; buffer_length_ = policy.min_capacity; - unsigned char* new_buf = (unsigned char*)realloc(buffer_, buffer_length_); + unsigned char* new_buf = reinterpret_cast(realloc(buffer_, buffer_length_)); if ( ! new_buf ) throw ExceptionFlowBufferAlloc("contract realloc OOM"); diff --git a/lib/binpac_bytestring.cc b/lib/binpac_bytestring.cc index be34209..b8e7286 100644 --- a/lib/binpac_bytestring.cc +++ b/lib/binpac_bytestring.cc @@ -6,10 +6,12 @@ namespace binpac { -std::string std_string(bytestring const* s) { return std::string((const char*)s->begin(), (const char*)s->end()); } +std::string std_string(bytestring const* s) { + return std::string(reinterpret_cast(s->begin()), reinterpret_cast(s->end())); +} -int bytestring_to_int(bytestring const* s) { return atoi((const char*)s->begin()); } +int bytestring_to_int(bytestring const* s) { return atoi(reinterpret_cast(s->begin())); } -double bytestring_to_double(bytestring const* s) { return atof((const char*)s->begin()); } +double bytestring_to_double(bytestring const* s) { return atof(reinterpret_cast(s->begin())); } } // namespace binpac diff --git a/src/pac_analyzer.cc b/src/pac_analyzer.cc index d404fa3..ba7d250 100644 --- a/src/pac_analyzer.cc +++ b/src/pac_analyzer.cc @@ -2,6 +2,7 @@ #include "pac_action.h" #include "pac_context.h" +#include "pac_dataunit.h" #include "pac_embedded.h" #include "pac_exception.h" #include "pac_expr.h" @@ -49,16 +50,16 @@ void AnalyzerDecl::AddElements(AnalyzerElementList* elemlist) { switch ( elem->type() ) { case AnalyzerElement::STATE: { ASSERT(0); - AnalyzerState* state_elem = (AnalyzerState*)elem; + AnalyzerState* state_elem = dynamic_cast(elem); statevars_->insert(statevars_->end(), state_elem->statevars()->begin(), state_elem->statevars()->end()); } break; case AnalyzerElement::ACTION: { ASSERT(0); - AnalyzerAction* action_elem = (AnalyzerAction*)elem; + AnalyzerAction* action_elem = dynamic_cast(elem); actions_->push_back(action_elem); } break; case AnalyzerElement::HELPER: { - AnalyzerHelper* helper_elem = (AnalyzerHelper*)elem; + AnalyzerHelper* helper_elem = dynamic_cast(elem); switch ( helper_elem->helper_type() ) { case AnalyzerHelper::INIT_CODE: constructor_helpers_->push_back(helper_elem); break; @@ -68,17 +69,17 @@ void AnalyzerDecl::AddElements(AnalyzerElementList* elemlist) { } } break; case AnalyzerElement::FUNCTION: { - AnalyzerFunction* func_elem = (AnalyzerFunction*)elem; + AnalyzerFunction* func_elem = dynamic_cast(elem); Function* func = func_elem->function(); func->set_analyzer_decl(this); functions_->push_back(func); } break; case AnalyzerElement::FLOW: { - AnalyzerFlow* flow_elem = (AnalyzerFlow*)elem; + AnalyzerFlow* flow_elem = dynamic_cast(elem); ProcessFlowElement(flow_elem); } break; case AnalyzerElement::DATAUNIT: { - AnalyzerDataUnit* dataunit_elem = (AnalyzerDataUnit*)elem; + AnalyzerDataUnit* dataunit_elem = dynamic_cast(elem); ProcessDataUnitElement(dataunit_elem); } break; } diff --git a/src/pac_btype.cc b/src/pac_btype.cc index d56b9ab..b38e1b6 100644 --- a/src/pac_btype.cc +++ b/src/pac_btype.cc @@ -94,7 +94,8 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, case INT8: case UINT8: - out_cc->println("%s = *((%s const*)(%s));", lvalue(), DataTypeStr().c_str(), data.ptr_expr()); + out_cc->println("%s = *(reinterpret_cast<%s const*>(%s));", lvalue(), DataTypeStr().c_str(), + data.ptr_expr()); break; case INT16: case UINT16: @@ -109,8 +110,8 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, data.ptr_expr(), EvalByteOrder(out_cc, env).c_str()); #else - out_cc->println("%s = FixByteOrder(%s, *((%s const*)(%s)));", lvalue(), EvalByteOrder(out_cc, env).c_str(), - DataTypeStr().c_str(), data.ptr_expr()); + out_cc->println("%s = FixByteOrder(%s, *(reinterpret_cast<%s const*>(%s)));", lvalue(), + EvalByteOrder(out_cc, env).c_str(), DataTypeStr().c_str(), data.ptr_expr()); #endif break; } diff --git a/src/pac_case.cc b/src/pac_case.cc index 38f7963..54e5c40 100644 --- a/src/pac_case.cc +++ b/src/pac_case.cc @@ -246,7 +246,7 @@ void GenCaseStr(ExprList* index_list, Output* out_cc, Env* env, Type* switch_typ case_type_width = case_type->StaticSize(env); if ( case_type_width > switch_type_width ) { - BuiltInType* st = (BuiltInType*)switch_type; + BuiltInType* st = dynamic_cast(switch_type); if ( switch_type_width == 1 ) { if ( st->bit_type() == BuiltInType::INT8 ) { diff --git a/src/pac_strtype.cc b/src/pac_strtype.cc index 00d9c3d..34f332a 100644 --- a/src/pac_strtype.cc +++ b/src/pac_strtype.cc @@ -219,8 +219,8 @@ void StringType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, i } void StringType::GenStringMismatch(Output* out_cc, Env* env, const DataPtr& data, string pattern) { - string tmp = - strfmt("string((const char *) (%s), (const char *) %s).c_str()", data.ptr_expr(), env->RValue(end_of_data)); + string tmp = strfmt("string(reinterpret_cast(%s), reinterpret_cast(%s)).c_str()", + data.ptr_expr(), env->RValue(end_of_data)); out_cc->println("throw binpac::ExceptionStringMismatch(\"%s\", %s, %s);", Location(), pattern.c_str(), tmp.c_str()); } From 956cc87c8feb784274e6f58ce0c50dd2ca712844 Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Sat, 28 Jun 2025 16:22:57 +0200 Subject: [PATCH 2/2] Bump pre-commit hooks --- .pre-commit-config.yaml | 4 ++-- src/pac_decl.h | 6 ++---- src/pac_record.h | 12 ++++-------- src/pac_type.h | 3 +-- src/pac_varfield.h | 6 ++---- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a3f0de8..71d50be 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,7 +3,7 @@ # repos: - repo: https://github.com/pre-commit/mirrors-clang-format - rev: 'v17.0.3' + rev: 'v20.1.7' hooks: - id: clang-format types_or: @@ -12,7 +12,7 @@ repos: - "json" - repo: https://github.com/maxwinterstein/shfmt-py - rev: v3.7.0.1 + rev: v3.11.0.2 hooks: - id: shfmt args: ["-w", "-i", "4", "-ci"] diff --git a/src/pac_decl.h b/src/pac_decl.h index f3c4426..a6985d3 100644 --- a/src/pac_decl.h +++ b/src/pac_decl.h @@ -23,8 +23,7 @@ class Decl : public Object { virtual void Prepare() = 0; // Generate declarations out of the "binpac" namespace - virtual void GenExternDeclaration(Output* out_h) { /* do nothing */ - } + virtual void GenExternDeclaration(Output* out_h) { /* do nothing */ } // Generate declarations before definition of classes virtual void GenForwardDeclaration(Output* out_h) = 0; @@ -65,8 +64,7 @@ class HelperDecl : public Decl { void Prepare() override; void GenExternDeclaration(Output* out_h) override; - void GenForwardDeclaration(Output* out_h) override { /* do nothing */ - } + void GenForwardDeclaration(Output* out_h) override { /* do nothing */ } void GenCode(Output* out_h, Output* out_cc) override; private: diff --git a/src/pac_record.h b/src/pac_record.h index 539a04a..ffd0ae8 100644 --- a/src/pac_record.h +++ b/src/pac_record.h @@ -142,15 +142,11 @@ class RecordPaddingField : public RecordField { void Prepare(Env* env) override; - void GenPubDecls(Output* out, Env* env) override { /* nothing */ - } - void GenPrivDecls(Output* out, Env* env) override { /* nothing */ - } + void GenPubDecls(Output* out, Env* env) override { /* nothing */ } + void GenPrivDecls(Output* out, Env* env) override { /* nothing */ } - void GenInitCode(Output* out, Env* env) override { /* nothing */ - } - void GenCleanUpCode(Output* out, Env* env) override { /* nothing */ - } + void GenInitCode(Output* out, Env* env) override { /* nothing */ } + void GenCleanUpCode(Output* out, Env* env) override { /* nothing */ } void GenParseCode(Output* out, Env* env) override; int StaticSize(Env* env, int offset) const override; diff --git a/src/pac_type.h b/src/pac_type.h index 507ac1c..2137d43 100644 --- a/src/pac_type.h +++ b/src/pac_type.h @@ -100,8 +100,7 @@ class Type : public Object, public DataDepElement { void AddField(Field* f); - void AddCheck(Expr* expr) { /* TODO */ - } + void AddCheck(Expr* expr) { /* TODO */ } virtual bool DefineValueVar() const = 0; diff --git a/src/pac_varfield.h b/src/pac_varfield.h index 7d1685f..60ca3e5 100644 --- a/src/pac_varfield.h +++ b/src/pac_varfield.h @@ -8,8 +8,7 @@ class ParseVarField : public Field { public: ParseVarField(int is_class_member, ID* id, Type* type) : Field(PARSE_VAR_FIELD, TYPE_TO_BE_PARSED | is_class_member | NOT_PUBLIC_READABLE, id, type) {} - void GenPubDecls(Output* out, Env* env) override { /* do nothing */ - } + void GenPubDecls(Output* out, Env* env) override { /* do nothing */ } }; // A public variable @@ -27,8 +26,7 @@ class PrivVarField : public Field { : Field(PRIV_VAR_FIELD, TYPE_NOT_TO_BE_PARSED | CLASS_MEMBER | NOT_PUBLIC_READABLE, id, type) {} ~PrivVarField() override {} - void GenPubDecls(Output* out, Env* env) override { /* do nothing */ - } + void GenPubDecls(Output* out, Env* env) override { /* do nothing */ } }; class TempVarField : public Field {