From 31b65f70da81708cd6d0159413d13406fbcaf765 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Thu, 21 Aug 2025 15:05:42 +0000 Subject: [PATCH] binpac: Fix a large number of clang-tidy warnings --- tools/binpac/lib/CMakeLists.txt | 2 + tools/binpac/lib/binpac_buffer.cc | 10 ++-- tools/binpac/lib/binpac_bytestring.cc | 4 +- tools/binpac/src/CMakeLists.txt | 16 +++++- tools/binpac/src/pac_action.cc | 2 +- tools/binpac/src/pac_action.h | 8 +-- tools/binpac/src/pac_analyzer.h | 12 +++-- tools/binpac/src/pac_attr.h | 4 +- tools/binpac/src/pac_btype.cc | 3 ++ tools/binpac/src/pac_btype.h | 7 ++- tools/binpac/src/pac_case.cc | 2 +- tools/binpac/src/pac_case.h | 2 +- tools/binpac/src/pac_common.h | 66 ++++++++++++----------- tools/binpac/src/pac_conn.cc | 2 +- tools/binpac/src/pac_cstr.cc | 2 +- tools/binpac/src/pac_cstr.h | 2 +- tools/binpac/src/pac_datadep.cc | 2 +- tools/binpac/src/pac_datadep.h | 11 ++-- tools/binpac/src/pac_dataptr.h | 3 ++ tools/binpac/src/pac_dataunit.h | 4 +- tools/binpac/src/pac_dbg.h | 3 +- tools/binpac/src/pac_decl.cc | 4 +- tools/binpac/src/pac_decl.h | 12 +++-- tools/binpac/src/pac_embedded.cc | 2 +- tools/binpac/src/pac_embedded.h | 4 +- tools/binpac/src/pac_enum.h | 2 +- tools/binpac/src/pac_expr.cc | 52 +++++++++--------- tools/binpac/src/pac_expr.h | 1 + tools/binpac/src/pac_exttype.h | 4 +- tools/binpac/src/pac_field.h | 4 +- tools/binpac/src/pac_flow.cc | 2 +- tools/binpac/src/pac_func.cc | 6 +-- tools/binpac/src/pac_func.h | 8 +-- tools/binpac/src/pac_id.cc | 10 ++-- tools/binpac/src/pac_id.h | 13 ++--- tools/binpac/src/pac_inputbuf.cc | 2 +- tools/binpac/src/pac_main.cc | 7 ++- tools/binpac/src/pac_output.cc | 8 +-- tools/binpac/src/pac_parse.yy | 78 +++++++++++++-------------- tools/binpac/src/pac_primitive.cc | 2 +- tools/binpac/src/pac_primitive.h | 4 +- tools/binpac/src/pac_record.h | 2 +- tools/binpac/src/pac_strtype.cc | 6 +-- tools/binpac/src/pac_strtype.h | 4 +- tools/binpac/src/pac_type.cc | 2 +- tools/binpac/src/pac_type.h | 2 +- tools/binpac/src/pac_typedecl.cc | 8 ++- tools/binpac/src/pac_utils.cc | 8 +-- 48 files changed, 231 insertions(+), 193 deletions(-) diff --git a/tools/binpac/lib/CMakeLists.txt b/tools/binpac/lib/CMakeLists.txt index 3502fa1fa0..eba9d6c43a 100644 --- a/tools/binpac/lib/CMakeLists.txt +++ b/tools/binpac/lib/CMakeLists.txt @@ -21,6 +21,7 @@ if (BUILD_STATIC_BINPAC) target_compile_options(binpac_static PRIVATE "/J") endif () set(BinPAC_LIBRARY binpac_static CACHE STRING "BinPAC library" FORCE) + zeek_target_add_linters(binpac_static) else () add_library(binpac_lib SHARED) target_sources(binpac_lib PRIVATE ${binpac_lib_SRCS}) @@ -31,6 +32,7 @@ else () endif () install(TARGETS binpac_lib DESTINATION ${CMAKE_INSTALL_LIBDIR}) set(BinPAC_LIBRARY binpac_lib CACHE STRING "BinPAC library" FORCE) + zeek_target_add_linters(binpac_lib) endif () if (ZEEK_ROOT_DIR) diff --git a/tools/binpac/lib/binpac_buffer.cc b/tools/binpac/lib/binpac_buffer.cc index 92cde625c8..7655df0a3b 100644 --- a/tools/binpac/lib/binpac_buffer.cc +++ b/tools/binpac/lib/binpac_buffer.cc @@ -1,8 +1,8 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include -#include -#include // for memcpy +#include +#include +#include // for memcpy #define binpac_regex_h @@ -82,7 +82,7 @@ void FlowBuffer::ResetLineState() { case CR_OR_LF: state_ = CR_OR_LF_0; break; case STRICT_CRLF: state_ = STRICT_CRLF_0; break; case LINE_BREAKER: break; // Nothing to reset - default: BINPAC_ASSERT(0); break; + default: BINPAC_ASSERT(false); break; } } @@ -248,7 +248,7 @@ void FlowBuffer::MarkOrCopyLine() { case CR_OR_LF: MarkOrCopyLine_CR_OR_LF(); break; case STRICT_CRLF: MarkOrCopyLine_STRICT_CRLF(); break; case LINE_BREAKER: MarkOrCopyLine_LINEBREAK(); break; - default: BINPAC_ASSERT(0); break; + default: BINPAC_ASSERT(false); break; } } diff --git a/tools/binpac/lib/binpac_bytestring.cc b/tools/binpac/lib/binpac_bytestring.cc index dd07d78976..ce509b851a 100644 --- a/tools/binpac/lib/binpac_bytestring.cc +++ b/tools/binpac/lib/binpac_bytestring.cc @@ -4,11 +4,11 @@ #include "binpac_bytestring.h" -#include +#include 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 {(const char*)s->begin(), (const char*)s->end()}; } int bytestring_to_int(bytestring const* s) { return atoi((const char*)s->begin()); } diff --git a/tools/binpac/src/CMakeLists.txt b/tools/binpac/src/CMakeLists.txt index 2a3654c2e7..dba9085c2f 100644 --- a/tools/binpac/src/CMakeLists.txt +++ b/tools/binpac/src/CMakeLists.txt @@ -11,6 +11,18 @@ else () set_property(SOURCE pac_scan.cc APPEND_STRING PROPERTY COMPILE_FLAGS "-Wno-sign-compare") endif () +set(binpac_bison_generated_files + ${CMAKE_CURRENT_BINARY_DIR}/pac_parse.cc ${CMAKE_CURRENT_BINARY_DIR}/pac_parse.h + ${CMAKE_CURRENT_BINARY_DIR}/pac_scan.cc) + +set_source_files_properties(${binpac_bison_generated_files} PROPERTIES SKIP_LINTING ON) + +# pac_main.cc includes up pac_parse.h, and so clang-tidy reports a bunch of warnings from +# that header as part of it. adding NOLINTBEGIN/END around the include didn't resolve it. +# I fixed the findings in pac_main.cc, but am ignoring it from here on to avoid the extra +# noise. +set_source_files_properties(pac_main.cc PROPERTIES SKIP_LINTING ON) + set(binpac_SRCS ${BISON_PACParser_INPUT} ${FLEX_PACScanner_INPUT} @@ -55,7 +67,9 @@ set(binpac_SRCS pac_exception.cc pac_main.cc) -add_executable(binpac ${binpac_SRCS}) +add_executable(binpac) +target_sources(binpac PRIVATE ${binpac_SRCS}) +zeek_target_add_linters(binpac) target_include_directories(binpac BEFORE PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(binpac BEFORE PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/tools/binpac/src/pac_action.cc b/tools/binpac/src/pac_action.cc index 12afd6a9ba..c7169b8b81 100644 --- a/tools/binpac/src/pac_action.cc +++ b/tools/binpac/src/pac_action.cc @@ -11,7 +11,7 @@ #include "pac_utils.h" AnalyzerAction::AnalyzerAction(ID* action_id, When when, ActionParam* param, EmbeddedCode* code) - : AnalyzerElement(ACTION), action_id_(action_id), when_(when), param_(param), code_(code), analyzer_(nullptr) {} + : AnalyzerElement(ACTION), action_id_(action_id), when_(when), param_(param), code_(code) {} AnalyzerAction::~AnalyzerAction() { delete action_id_; diff --git a/tools/binpac/src/pac_action.h b/tools/binpac/src/pac_action.h index 521fc4d0da..2303bbc3d6 100644 --- a/tools/binpac/src/pac_action.h +++ b/tools/binpac/src/pac_action.h @@ -3,6 +3,8 @@ #ifndef pac_action_h #define pac_action_h +#include + // Classes representing analyzer actions. #include "pac_analyzer.h" @@ -10,7 +12,7 @@ class AnalyzerAction : public AnalyzerElement { public: - enum When { BEFORE, AFTER }; + enum When : uint8_t { BEFORE, AFTER }; AnalyzerAction(ID* action_id, When when, ActionParam* param, EmbeddedCode* code); @@ -35,7 +37,7 @@ private: When when_; ActionParam* param_; EmbeddedCode* code_; - AnalyzerDecl* analyzer_; + AnalyzerDecl* analyzer_ = nullptr; }; class ActionParam { @@ -56,7 +58,7 @@ private: class ActionParamType { public: - ActionParamType(const ID* type_id, const ID* field_id = 0) : type_id_(type_id), field_id_(field_id) {} + ActionParamType(const ID* type_id, const ID* field_id = nullptr) : type_id_(type_id), field_id_(field_id) {} const ID* type_id() const { return type_id_; } const ID* field_id() const { return field_id_; } diff --git a/tools/binpac/src/pac_analyzer.h b/tools/binpac/src/pac_analyzer.h index 7c6a6c523a..67356a6475 100644 --- a/tools/binpac/src/pac_analyzer.h +++ b/tools/binpac/src/pac_analyzer.h @@ -3,6 +3,8 @@ #ifndef pac_analyzer_h #define pac_analyzer_h +#include + #include "pac_common.h" #include "pac_field.h" #include "pac_typedecl.h" @@ -16,8 +18,8 @@ class AnalyzerDataUnit; class AnalyzerFunction; class ConnDecl; class FlowDecl; -typedef vector AnalyzerHelperList; -typedef vector FunctionList; +using AnalyzerHelperList = vector; +using FunctionList = vector; class AnalyzerDecl : public TypeDecl { public: @@ -84,7 +86,7 @@ protected: class AnalyzerElement : public Object { public: - enum ElementType { STATE, ACTION, FUNCTION, HELPER, FLOW, DATAUNIT }; + enum ElementType : uint8_t { STATE, ACTION, FUNCTION, HELPER, FLOW, DATAUNIT }; AnalyzerElement(ElementType type) : type_(type) {} virtual ~AnalyzerElement() {} @@ -109,7 +111,7 @@ private: // A collection of embedded C++ code class AnalyzerHelper : public AnalyzerElement { public: - enum Type { + enum Type : uint8_t { MEMBER_DECLS, INIT_CODE, CLEANUP_CODE, @@ -140,7 +142,7 @@ public: class AnalyzerFlow : public AnalyzerElement { public: - enum Direction { UP, DOWN }; + enum Direction : uint8_t { UP, DOWN }; AnalyzerFlow(Direction dir, ID* type_id, ExprList* params); ~AnalyzerFlow() override; diff --git a/tools/binpac/src/pac_attr.h b/tools/binpac/src/pac_attr.h index 7dd5a74d05..a33a6f8619 100644 --- a/tools/binpac/src/pac_attr.h +++ b/tools/binpac/src/pac_attr.h @@ -3,10 +3,12 @@ #ifndef pac_attr_h #define pac_attr_h +#include + #include "pac_common.h" #include "pac_datadep.h" -enum AttrType { +enum AttrType : uint8_t { ATTR_BYTEORDER, ATTR_CHECK, ATTR_CHUNKED, diff --git a/tools/binpac/src/pac_btype.cc b/tools/binpac/src/pac_btype.cc index 27844d786c..7846d1cadf 100644 --- a/tools/binpac/src/pac_btype.cc +++ b/tools/binpac/src/pac_btype.cc @@ -19,6 +19,7 @@ bool BuiltInType::CompatibleBuiltInTypes(BuiltInType* type1, BuiltInType* type2) } static const char* basic_pactype_name[] = { +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_DEF(name, pactype, ctype, size) pactype, #include "pac_type.def" #undef TYPE_DEF @@ -40,6 +41,7 @@ int BuiltInType::LookUpByName(const char* name) { } static const char* basic_ctype_name[] = { +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_DEF(name, pactype, ctype, size) ctype, #include "pac_type.def" #undef TYPE_DEF @@ -52,6 +54,7 @@ string BuiltInType::DataTypeStr() const { return basic_ctype_name[bit_type_]; } int BuiltInType::StaticSize(Env* /* env */) const { static const size_t basic_type_size[] = { +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_DEF(name, pactype, ctype, size) size, #include "pac_type.def" #undef TYPE_DEF diff --git a/tools/binpac/src/pac_btype.h b/tools/binpac/src/pac_btype.h index 320d2cb886..e843fa66dc 100644 --- a/tools/binpac/src/pac_btype.h +++ b/tools/binpac/src/pac_btype.h @@ -3,11 +3,14 @@ #ifndef pac_btype_h #define pac_btype_h +#include + #include "pac_type.h" class BuiltInType : public Type { public: - enum BITType { + enum BITType : uint8_t { + // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define TYPE_DEF(name, pactype, ctype, size) name, #include "pac_type.def" #undef TYPE_DEF @@ -29,7 +32,7 @@ public: bool IsPointerType() const override { return false; } - bool ByteOrderSensitive() const override { return StaticSize(0) >= 2; } + bool ByteOrderSensitive() const override { return StaticSize(nullptr) >= 2; } void GenInitCode(Output* out_cc, Env* env) override; diff --git a/tools/binpac/src/pac_case.cc b/tools/binpac/src/pac_case.cc index 21583fab99..8a3be41d59 100644 --- a/tools/binpac/src/pac_case.cc +++ b/tools/binpac/src/pac_case.cc @@ -2,7 +2,7 @@ #include "pac_case.h" -#include +#include #include #include "pac_btype.h" diff --git a/tools/binpac/src/pac_case.h b/tools/binpac/src/pac_case.h index bbeefaaf18..13e5701506 100644 --- a/tools/binpac/src/pac_case.h +++ b/tools/binpac/src/pac_case.h @@ -49,7 +49,7 @@ protected: ID* index_var_; CaseFieldList* cases_; - typedef map member_map_t; + using member_map_t = map; member_map_t member_map_; }; diff --git a/tools/binpac/src/pac_common.h b/tools/binpac/src/pac_common.h index 29a1ab5265..66a8886f58 100644 --- a/tools/binpac/src/pac_common.h +++ b/tools/binpac/src/pac_common.h @@ -3,9 +3,9 @@ #ifndef pac_common_h #define pac_common_h -#include -#include -#include +#include +#include +#include #include #include "pac_utils.h" @@ -87,26 +87,28 @@ class WithInputField; // The ID of the current declaration. extern const ID* current_decl_id; -typedef vector ActionParamList; -typedef vector AnalyzerActionList; -typedef vector AnalyzerElementList; -typedef vector AttrList; -typedef vector CaseExprList; -typedef vector CaseFieldList; -typedef vector ContextFieldList; -typedef vector DeclList; -typedef vector EnumList; -typedef vector ExprList; -typedef vector FieldList; -typedef vector LetFieldList; -typedef vector NumList; -typedef vector ParamList; -typedef vector RecordFieldList; -typedef vector StateVarList; +using ActionParamList = vector; +using AnalyzerActionList = vector; +using AnalyzerElementList = vector; +using AttrList = vector; +using CaseExprList = vector; +using CaseFieldList = vector; +using ContextFieldList = vector; +using DeclList = vector; +using EnumList = vector; +using ExprList = vector; +using FieldList = vector; +using LetFieldList = vector; +using NumList = vector; +using ParamList = vector; +using RecordFieldList = vector; +using StateVarList = vector; +// NOLINTBEGIN(cppcoreguidelines-macro-usage,modernize-loop-convert) #define foreach(i, ct, pc) \ if ( pc ) \ - for ( ct::iterator i = (pc)->begin(); i != (pc)->end(); ++i ) + for ( ct::iterator i = (pc)->begin(); (i) != (pc)->end(); ++(i) ) +// NOLINTEND(cppcoreguidelines-macro-usage,modernize-loop-convert) template constexpr void delete_list(T* container) { @@ -117,17 +119,17 @@ constexpr void delete_list(T* container) { } // Constants -const char* const kComputeFrameLength = "compute_frame_length"; -const char* const kFlowBufferClass = "FlowBuffer"; -const char* const kFlowBufferVar = "flow_buffer"; -const char* const kFlowEOF = "FlowEOF"; -const char* const kFlowGap = "NewGap"; -const char* const kInitialBufferLengthFunc = "initial_buffer_length"; -const char* const kNeedMoreData = "need_more_data"; -const char* const kNewData = "NewData"; -const char* const kParseFuncWithBuffer = "ParseBuffer"; -const char* const kParseFuncWithoutBuffer = "Parse"; -const char* const kRefCountClass = "binpac::RefCount"; -const char* const kTypeWithLengthClass = "binpac::TypeWithLength"; +constexpr char kComputeFrameLength[] = "compute_frame_length"; +constexpr char kFlowBufferClass[] = "FlowBuffer"; +constexpr char kFlowBufferVar[] = "flow_buffer"; +constexpr char kFlowEOF[] = "FlowEOF"; +constexpr char kFlowGap[] = "NewGap"; +constexpr char kInitialBufferLengthFunc[] = "initial_buffer_length"; +constexpr char kNeedMoreData[] = "need_more_data"; +constexpr char kNewData[] = "NewData"; +constexpr char kParseFuncWithBuffer[] = "ParseBuffer"; +constexpr char kParseFuncWithoutBuffer[] = "Parse"; +constexpr char kRefCountClass[] = "binpac::RefCount"; +constexpr char kTypeWithLengthClass[] = "binpac::TypeWithLength"; #endif // pac_common_h diff --git a/tools/binpac/src/pac_conn.cc b/tools/binpac/src/pac_conn.cc index ae26c75a1c..eb414036af 100644 --- a/tools/binpac/src/pac_conn.cc +++ b/tools/binpac/src/pac_conn.cc @@ -26,7 +26,7 @@ ConnDecl::~ConnDecl() { } void ConnDecl::AddBaseClass(vector* base_classes) const { - base_classes->push_back("binpac::ConnectionAnalyzer"); + base_classes->emplace_back("binpac::ConnectionAnalyzer"); } void ConnDecl::ProcessFlowElement(AnalyzerFlow* flow_elem) { diff --git a/tools/binpac/src/pac_cstr.cc b/tools/binpac/src/pac_cstr.cc index fa61824af3..af014482d8 100644 --- a/tools/binpac/src/pac_cstr.cc +++ b/tools/binpac/src/pac_cstr.cc @@ -77,7 +77,7 @@ int expand_escape(const char*& s) { } // namespace -ConstString::ConstString(const string& s) : str_(s) { +ConstString::ConstString(string s) : str_(std::move(s)) { // Copied from scan.l of Zeek try { const char* text = str_.c_str(); diff --git a/tools/binpac/src/pac_cstr.h b/tools/binpac/src/pac_cstr.h index 90735cc2d5..02facaaa33 100644 --- a/tools/binpac/src/pac_cstr.h +++ b/tools/binpac/src/pac_cstr.h @@ -7,7 +7,7 @@ class ConstString : public Object { public: - ConstString(const string& s); + ConstString(string s); // The string in its escaped form, with surrounding '"'s const string& str() const { return str_; } diff --git a/tools/binpac/src/pac_datadep.cc b/tools/binpac/src/pac_datadep.cc index dfa85ebb48..c6559b656b 100644 --- a/tools/binpac/src/pac_datadep.cc +++ b/tools/binpac/src/pac_datadep.cc @@ -6,7 +6,7 @@ #include "pac_id.h" #include "pac_type.h" -DataDepElement::DataDepElement(DDE_Type type) : dde_type_(type), in_traversal(false) {} +DataDepElement::DataDepElement(DDE_Type type) : dde_type_(type) {} bool DataDepElement::Traverse(DataDepVisitor* visitor) { // Avoid infinite loop diff --git a/tools/binpac/src/pac_datadep.h b/tools/binpac/src/pac_datadep.h index 9267375241..87039c18d1 100644 --- a/tools/binpac/src/pac_datadep.h +++ b/tools/binpac/src/pac_datadep.h @@ -3,17 +3,18 @@ #ifndef pac_datadep_h #define pac_datadep_h +#include + // To provide a way to traverse through the data dependency graph. // That is, to evaluate X, what must be evaluated. #include "pac_common.h" -#include "pac_dbg.h" class DataDepVisitor; class DataDepElement { public: - enum DDE_Type { + enum DDE_Type : uint8_t { ATTR, CASEEXPR, EXPR, @@ -38,7 +39,7 @@ public: protected: DDE_Type dde_type_; - bool in_traversal; + bool in_traversal = false; }; class DataDepVisitor { @@ -51,8 +52,6 @@ public: class RequiresAnalyzerContext : public DataDepVisitor { public: - RequiresAnalyzerContext() : requires_analyzer_context_(false) {} - // Returns whether to continue traversal bool PreProcess(DataDepElement* element) override; bool PostProcess(DataDepElement* element) override; @@ -64,7 +63,7 @@ public: protected: void ProcessExpr(Expr* expr); - bool requires_analyzer_context_; + bool requires_analyzer_context_ = false; }; #endif // pac_datadep_h diff --git a/tools/binpac/src/pac_dataptr.h b/tools/binpac/src/pac_dataptr.h index 676b610d02..e373c615f6 100644 --- a/tools/binpac/src/pac_dataptr.h +++ b/tools/binpac/src/pac_dataptr.h @@ -17,6 +17,9 @@ public: DataPtr(DataPtr const& x) { *this = x; } DataPtr const& operator=(DataPtr const& x) { + if ( this == &x ) + return *this; + id_ = x.id(); offset_ = x.offset(); ptr_expr_ = x.ptr_expr(); diff --git a/tools/binpac/src/pac_dataunit.h b/tools/binpac/src/pac_dataunit.h index e8a6fe9465..7b8c8fbbdd 100644 --- a/tools/binpac/src/pac_dataunit.h +++ b/tools/binpac/src/pac_dataunit.h @@ -3,6 +3,8 @@ #ifndef pac_dataunit_h #define pac_dataunit_h +#include + #include "pac_analyzer.h" // The type and parameters of input data unit of a flow. For instance, the @@ -10,7 +12,7 @@ class AnalyzerDataUnit : public AnalyzerElement { public: - enum DataUnitType { DATAGRAM, FLOWUNIT }; + enum DataUnitType : uint8_t { DATAGRAM, FLOWUNIT }; AnalyzerDataUnit(DataUnitType type, ID* id, ExprList* type_params, ExprList* context_params); ~AnalyzerDataUnit() override; diff --git a/tools/binpac/src/pac_dbg.h b/tools/binpac/src/pac_dbg.h index 9f930ffc40..4caa716d07 100644 --- a/tools/binpac/src/pac_dbg.h +++ b/tools/binpac/src/pac_dbg.h @@ -8,7 +8,8 @@ extern bool FLAGS_pac_debug; -#define ASSERT(x) assert(x) +constexpr void ASSERT(bool flag) { assert(flag); } +constexpr void ASSERT(int flag) { assert(flag); } #define DEBUG_MSG(...) \ if ( FLAGS_pac_debug ) \ fprintf(stderr, __VA_ARGS__) diff --git a/tools/binpac/src/pac_decl.cc b/tools/binpac/src/pac_decl.cc index 7cbc2005af..b402e4ca5b 100644 --- a/tools/binpac/src/pac_decl.cc +++ b/tools/binpac/src/pac_decl.cc @@ -19,15 +19,13 @@ DeclList* Decl::decl_list_ = nullptr; Decl::DeclMap Decl::decl_map_; -Decl::Decl(ID* id, DeclType decl_type) : id_(id), decl_type_(decl_type), attrlist_(nullptr) { +Decl::Decl(ID* id, DeclType decl_type) : id_(id), decl_type_(decl_type) { decl_map_[id_] = this; if ( ! decl_list_ ) decl_list_ = new DeclList(); decl_list_->push_back(this); DEBUG_MSG("Finished Decl %s\n", id_->Name()); - - analyzer_context_ = nullptr; } Decl::~Decl() { diff --git a/tools/binpac/src/pac_decl.h b/tools/binpac/src/pac_decl.h index be27fb29ce..676a09db73 100644 --- a/tools/binpac/src/pac_decl.h +++ b/tools/binpac/src/pac_decl.h @@ -3,6 +3,8 @@ #ifndef pac_decl_h #define pac_decl_h +#include + #include "pac_common.h" #include "pac_id.h" @@ -10,7 +12,7 @@ class Decl : public Object { public: // Note: ANALYZER is not for AnalyzerDecl (which is an // abstract class) , but for AnalyzerContextDecl. - enum DeclType { ENUM, LET, TYPE, FUNC, CONN, FLOW, ANALYZER, HELPER, REGEX }; + enum DeclType : uint8_t { ENUM, LET, TYPE, FUNC, CONN, FLOW, ANALYZER, HELPER, REGEX }; Decl(ID* id, DeclType decl_type); virtual ~Decl(); @@ -41,8 +43,8 @@ protected: ID* id_; DeclType decl_type_; - AttrList* attrlist_; - AnalyzerContextDecl* analyzer_context_; + AttrList* attrlist_ = nullptr; + AnalyzerContextDecl* analyzer_context_ = nullptr; public: static void ProcessDecls(Output* out_h, Output* out_cc); @@ -50,13 +52,13 @@ public: private: static DeclList* decl_list_; - typedef map DeclMap; + using DeclMap = map; static DeclMap decl_map_; }; class HelperDecl : public Decl { public: - enum HelperType { + enum HelperType : uint8_t { HEADER, CODE, EXTERN, diff --git a/tools/binpac/src/pac_embedded.cc b/tools/binpac/src/pac_embedded.cc index d180e80e2b..adaf6dd958 100644 --- a/tools/binpac/src/pac_embedded.cc +++ b/tools/binpac/src/pac_embedded.cc @@ -6,7 +6,7 @@ #include "pac_output.h" #include "pac_primitive.h" -EmbeddedCodeSegment::EmbeddedCodeSegment(const string& s) : s_(s), primitive_(nullptr) {} +EmbeddedCodeSegment::EmbeddedCodeSegment(string s) : s_(std::move(s)), primitive_(nullptr) {} EmbeddedCodeSegment::EmbeddedCodeSegment(PacPrimitive* primitive) : s_(""), primitive_(primitive) {} diff --git a/tools/binpac/src/pac_embedded.h b/tools/binpac/src/pac_embedded.h index a2f85f74f7..4f5edee4be 100644 --- a/tools/binpac/src/pac_embedded.h +++ b/tools/binpac/src/pac_embedded.h @@ -7,7 +7,7 @@ class EmbeddedCodeSegment { public: - explicit EmbeddedCodeSegment(const string& s); + explicit EmbeddedCodeSegment(string s); explicit EmbeddedCodeSegment(PacPrimitive* primitive); ~EmbeddedCodeSegment(); @@ -18,7 +18,7 @@ private: PacPrimitive* primitive_; }; -typedef vector EmbeddedCodeSegmentList; +using EmbeddedCodeSegmentList = vector; class EmbeddedCode : public Object { public: diff --git a/tools/binpac/src/pac_enum.h b/tools/binpac/src/pac_enum.h index 1b74e9016a..bd3e67bc62 100644 --- a/tools/binpac/src/pac_enum.h +++ b/tools/binpac/src/pac_enum.h @@ -7,7 +7,7 @@ class Enum { public: - Enum(ID* id, Expr* expr = 0); + Enum(ID* id, Expr* expr = nullptr); ~Enum(); void GenHeader(Output* out_h, int* pval); diff --git a/tools/binpac/src/pac_expr.cc b/tools/binpac/src/pac_expr.cc index 451baba3bc..6e55bfa8ed 100644 --- a/tools/binpac/src/pac_expr.cc +++ b/tools/binpac/src/pac_expr.cc @@ -45,6 +45,7 @@ string EvalExprList(ExprList* exprlist, Output* out, Env* env) { } static const char* expr_fmt[] = { +// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define EXPR_DEF(type, num_op, fmt) fmt, #include "pac_expr.def" #undef EXPR_DEF @@ -311,11 +312,11 @@ void Expr::GenEval(Output* out_cc, Env* env) { Type* ty; try { - if ( (rf = GetRecordField(id, env)) != nullptr ) { + if ( rf = GetRecordField(id, env); rf != nullptr ) { str_ = strfmt("%s", rf->FieldSize(out_cc, env)); } } catch ( ExceptionIDNotFound& e ) { - if ( (ty = TypeDecl::LookUpType(id)) != nullptr ) { + if ( ty = TypeDecl::LookUpType(id); ty != nullptr ) { int ty_size = ty->StaticSize(global_env()); if ( ty_size >= 0 ) str_ = strfmt("%d", ty_size); @@ -339,9 +340,9 @@ void Expr::GenEval(Output* out_cc, Env* env) { default: // Evaluate every operand by default - for ( int i = 0; i < 3; ++i ) - if ( operand_[i] ) - operand_[i]->GenEval(out_cc, env); + for ( auto& op : operand_ ) + if ( op ) + op->GenEval(out_cc, env); GenStrFromFormat(env); break; } @@ -373,9 +374,9 @@ void Expr::ForceIDEval(Output* out_cc, Env* env) { default: // Evaluate every operand by default - for ( int i = 0; i < 3; ++i ) - if ( operand_[i] ) - operand_[i]->ForceIDEval(out_cc, env); + for ( auto& op : operand_ ) + if ( op ) + op->ForceIDEval(out_cc, env); break; } } @@ -670,14 +671,14 @@ int Expr::MinimalHeaderSize(Env* env) { RecordField* rf; Type* ty; - if ( (rf = GetRecordField(id, env)) != nullptr ) { + if ( rf = GetRecordField(id, env); rf != nullptr ) { if ( rf->StaticSize(env, -1) >= 0 ) mhs = 0; else mhs = mhs_recordfield(env, rf); } - else if ( (ty = TypeDecl::LookUpType(id)) != nullptr ) { + else if ( ty = TypeDecl::LookUpType(id); ty != nullptr ) { mhs = 0; } @@ -703,25 +704,24 @@ int Expr::MinimalHeaderSize(Env* env) { case EXPR_CALLARGS: { mhs = 0; if ( args_ ) - for ( unsigned int i = 0; i < args_->size(); ++i ) - mhs = mhs_max(mhs, (*args_)[i]->MinimalHeaderSize(env)); + for ( const auto& arg : *args_ ) + mhs = mhs_max(mhs, arg->MinimalHeaderSize(env)); } break; case EXPR_CASE: { mhs = operand_[0]->MinimalHeaderSize(env); - for ( unsigned int i = 0; i < cases_->size(); ++i ) { - CaseExpr* ce = (*cases_)[i]; + for ( const auto& ce : *cases_ ) { if ( ce->index() ) - for ( unsigned int j = 0; j < ce->index()->size(); ++j ) - mhs = mhs_max(mhs, (*ce->index())[j]->MinimalHeaderSize(env)); + for ( const auto& idx : *(ce->index()) ) + mhs = mhs_max(mhs, idx->MinimalHeaderSize(env)); mhs = mhs_max(mhs, ce->value()->MinimalHeaderSize(env)); } } break; default: // Evaluate every operand by default mhs = 0; - for ( int i = 0; i < 3; ++i ) - if ( operand_[i] ) - mhs = mhs_max(mhs, operand_[i]->MinimalHeaderSize(env)); + for ( auto& op : operand_ ) + if ( op ) + mhs = mhs_max(mhs, op->MinimalHeaderSize(env)); break; } @@ -750,8 +750,8 @@ bool Expr::HasReference(const ID* id) const { default: // Evaluate every operand by default - for ( int i = 0; i < 3; ++i ) { - if ( operand_[i] && operand_[i]->HasReference(id) ) { + for ( auto& op : operand_ ) { + if ( op && op->HasReference(id) ) { return true; } } @@ -788,8 +788,8 @@ bool Expr::DoTraverse(DataDepVisitor* visitor) { default: // Evaluate every operand by default - for ( int i = 0; i < 3; ++i ) { - if ( operand_[i] && ! operand_[i]->Traverse(visitor) ) { + for ( auto& op : operand_ ) { + if ( op && ! op->Traverse(visitor) ) { return false; } } @@ -828,9 +828,9 @@ bool Expr::RequiresAnalyzerContext() const { default: // Evaluate every operand by default - for ( int i = 0; i < 3; ++i ) - if ( operand_[i] && operand_[i]->RequiresAnalyzerContext() ) { - DEBUG_MSG("'%s' requires analyzer context\n", operand_[i]->orig()); + for ( auto& op : operand_ ) + if ( op && op->RequiresAnalyzerContext() ) { + DEBUG_MSG("'%s' requires analyzer context\n", op->orig()); return true; } return false; diff --git a/tools/binpac/src/pac_expr.h b/tools/binpac/src/pac_expr.h index 7fc698db75..c428f3e15c 100644 --- a/tools/binpac/src/pac_expr.h +++ b/tools/binpac/src/pac_expr.h @@ -13,6 +13,7 @@ class CaseExpr; class Expr : public Object, public DataDepElement { public: enum ExprType : uint8_t { + // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) #define EXPR_DEF(type, x, y) type, #include "pac_expr.def" #undef EXPR_DEF diff --git a/tools/binpac/src/pac_exttype.h b/tools/binpac/src/pac_exttype.h index cf4ead6643..fc624d2b55 100644 --- a/tools/binpac/src/pac_exttype.h +++ b/tools/binpac/src/pac_exttype.h @@ -3,6 +3,8 @@ #ifndef pac_exttype_h #define pac_exttype_h +#include + #include "pac_type.h" // ExternType represent external C++ types that are not defined in @@ -12,7 +14,7 @@ class ExternType : public Type { public: - enum EXTType { PLAIN, NUMBER, POINTER, BOOLEAN }; + enum EXTType : uint8_t { PLAIN, NUMBER, POINTER, BOOLEAN }; ExternType(const ID* id, EXTType ext_type) : Type(EXTERN), id_(id), ext_type_(ext_type) {} bool DefineValueVar() const override; diff --git a/tools/binpac/src/pac_field.h b/tools/binpac/src/pac_field.h index 8211eca839..049ff177e3 100644 --- a/tools/binpac/src/pac_field.h +++ b/tools/binpac/src/pac_field.h @@ -3,12 +3,14 @@ #ifndef pac_field_h #define pac_field_h +#include + #include "pac_common.h" #include "pac_datadep.h" // A "field" is a member of class. -enum FieldType { +enum FieldType : uint8_t { CASE_FIELD, CONTEXT_FIELD, FLOW_FIELD, diff --git a/tools/binpac/src/pac_flow.cc b/tools/binpac/src/pac_flow.cc index 6e6944a78d..5ec255ffcf 100644 --- a/tools/binpac/src/pac_flow.cc +++ b/tools/binpac/src/pac_flow.cc @@ -38,7 +38,7 @@ ParameterizedType* FlowDecl::flow_buffer_type() { return flow_buffer_type_; } -void FlowDecl::AddBaseClass(vector* base_classes) const { base_classes->push_back("binpac::FlowAnalyzer"); } +void FlowDecl::AddBaseClass(vector* base_classes) const { base_classes->emplace_back("binpac::FlowAnalyzer"); } void FlowDecl::ProcessFlowElement(AnalyzerFlow* flow_elem) { throw Exception(flow_elem, "flow should be defined in only a connection declaration"); diff --git a/tools/binpac/src/pac_func.cc b/tools/binpac/src/pac_func.cc index e27f8a870b..f03478a588 100644 --- a/tools/binpac/src/pac_func.cc +++ b/tools/binpac/src/pac_func.cc @@ -8,11 +8,7 @@ #include "pac_param.h" #include "pac_type.h" -Function::Function(ID* id, Type* type, ParamList* params) - : id_(id), type_(type), params_(params), expr_(nullptr), code_(nullptr) { - analyzer_decl_ = nullptr; - env_ = nullptr; -} +Function::Function(ID* id, Type* type, ParamList* params) : id_(id), type_(type), params_(params) {} Function::~Function() { delete id_; diff --git a/tools/binpac/src/pac_func.h b/tools/binpac/src/pac_func.h index a604a89714..f785146683 100644 --- a/tools/binpac/src/pac_func.h +++ b/tools/binpac/src/pac_func.h @@ -27,16 +27,16 @@ public: void GenCode(Output* out_h, Output* out_cc); private: - Env* env_; + Env* env_ = nullptr; ID* id_; Type* type_; ParamList* params_; - AnalyzerDecl* analyzer_decl_; + AnalyzerDecl* analyzer_decl_ = nullptr; - Expr* expr_; - EmbeddedCode* code_; + Expr* expr_ = nullptr; + EmbeddedCode* code_ = nullptr; }; class FuncDecl : public Decl { diff --git a/tools/binpac/src/pac_id.cc b/tools/binpac/src/pac_id.cc index 1c18e27157..0a8563d39f 100644 --- a/tools/binpac/src/pac_id.cc +++ b/tools/binpac/src/pac_id.cc @@ -168,9 +168,9 @@ Env::Env(Env* parent_env, Object* context_object) : parent(parent_env), context_ } Env::~Env() { - for ( id_map_t::iterator it = id_map.begin(); it != id_map.end(); ++it ) { - delete it->second; - it->second = 0; + for ( auto& id : id_map ) { + delete id.second; + id.second = nullptr; } } @@ -365,8 +365,8 @@ Env* global_env() { the_global_env->AddConstID(null_id, 0, extern_type_nullptr); #if 0 - the_global_env->AddID(null_byteseg_id, - GLOBAL_VAR, + the_global_env->AddID(null_byteseg_id, + GLOBAL_VAR, extern_type_const_byteseg); #endif } diff --git a/tools/binpac/src/pac_id.h b/tools/binpac/src/pac_id.h index 7299283439..dd46fd2659 100644 --- a/tools/binpac/src/pac_id.h +++ b/tools/binpac/src/pac_id.h @@ -3,6 +3,7 @@ #ifndef pac_id_h #define pac_id_h +#include #include #include using namespace std; @@ -24,7 +25,7 @@ using namespace std; // Env -- a mapping from ID names to their L/R-value expressions and evaluation // methods. -enum IDType { +enum IDType : uint8_t { CONST, GLOBAL_VAR, TEMP_VAR, @@ -44,7 +45,7 @@ class Evaluatable; class ID : public Object { public: - ID(string arg_name) : name(arg_name), anonymous_id_(false) { locname = nfmt("%s:%s", Location(), Name()); } + ID(string arg_name) : name(std::move(arg_name)) { locname = nfmt("%s:%s", Location(), Name()); } ~ID() { delete[] locname; } bool operator==(ID const& x) const { return name == x.Name(); } @@ -57,8 +58,8 @@ public: protected: string name; - bool anonymous_id_; - char* locname; + bool anonymous_id_ = false; + char* locname = nullptr; friend class ID_ptr_cmp; public: @@ -146,7 +147,7 @@ public: void set_in_branch(bool x) { in_branch_ = x; } void AddID(const ID* id, IDType id_type, Type* type); - void AddConstID(const ID* id, const int c, Type* type = 0); + void AddConstID(const ID* id, const int c, Type* type = nullptr); void AddMacro(const ID* id, Expr* expr); // Generate a temp ID with a unique name @@ -192,7 +193,7 @@ protected: private: Env* parent; Object* context_object_; - typedef map id_map_t; + using id_map_t = map; id_map_t id_map; bool allow_undefined_id_; bool in_branch_; diff --git a/tools/binpac/src/pac_inputbuf.cc b/tools/binpac/src/pac_inputbuf.cc index d047532ed3..a3aa98c8b8 100644 --- a/tools/binpac/src/pac_inputbuf.cc +++ b/tools/binpac/src/pac_inputbuf.cc @@ -31,5 +31,5 @@ DataPtr InputBuffer::GenDataBeginEnd(Output* out_cc, Env* env) { env->SetEvaluated(begin_of_data); env->SetEvaluated(end_of_data); - return DataPtr(env, begin_of_data, 0); + return {env, begin_of_data, 0}; } diff --git a/tools/binpac/src/pac_main.cc b/tools/binpac/src/pac_main.cc index f3e5847f52..de2859d2c6 100644 --- a/tools/binpac/src/pac_main.cc +++ b/tools/binpac/src/pac_main.cc @@ -1,7 +1,7 @@ // See the file "COPYING" in the main distribution directory for copyright. -#include #include +#include #include "pac_common.h" #include "pac_decl.h" @@ -9,7 +9,9 @@ #include "pac_exttype.h" #include "pac_id.h" #include "pac_output.h" +// NOLINTBEGIN #include "pac_parse.h" +// NOLINTEND #include "pac_type.h" #include "pac_utils.h" @@ -27,7 +29,8 @@ Output* header_output = nullptr; Output* source_output = nullptr; void add_to_include_directories(string dirs) { - unsigned int dir_begin = 0, dir_end; + unsigned int dir_begin = 0; + unsigned int dir_end = 0; while ( dir_begin < dirs.length() ) { for ( dir_end = dir_begin; dir_end < dirs.length(); ++dir_end ) if ( dirs[dir_end] == ':' ) diff --git a/tools/binpac/src/pac_output.cc b/tools/binpac/src/pac_output.cc index 7889d1f224..ac08dc318a 100644 --- a/tools/binpac/src/pac_output.cc +++ b/tools/binpac/src/pac_output.cc @@ -2,10 +2,10 @@ #include "pac_output.h" -#include -#include -#include -#include +#include +#include +#include +#include #include "pac_utils.h" diff --git a/tools/binpac/src/pac_parse.yy b/tools/binpac/src/pac_parse.yy index 7547b421d8..3a752fa306 100644 --- a/tools/binpac/src/pac_parse.yy +++ b/tools/binpac/src/pac_parse.yy @@ -1,7 +1,7 @@ %token TOK_TYPE TOK_RECORD TOK_CASE TOK_ENUM TOK_LET TOK_FUNCTION %token TOK_REFINE TOK_CASEFUNC TOK_CASETYPE TOK_TYPEATTR %token TOK_HELPERHEADER TOK_HELPERCODE -%token TOK_RIGHTARROW TOK_DEFAULT TOK_OF +%token TOK_RIGHTARROW TOK_DEFAULT TOK_OF %token TOK_PADDING TOK_TO TOK_ALIGN %token TOK_WITHINPUT %token TOK_INT8 TOK_INT16 TOK_INT32 TOK_INT64 @@ -11,19 +11,19 @@ %token TOK_ATTR_ALSO %token TOK_ATTR_BYTEORDER TOK_ATTR_CHECK TOK_ATTR_CHUNKED TOK_ATTR_ENFORCE %token TOK_ATTR_EXPORTSOURCEDATA TOK_ATTR_IF -%token TOK_ATTR_LENGTH TOK_ATTR_LET +%token TOK_ATTR_LENGTH TOK_ATTR_LET %token TOK_ATTR_LINEBREAKER TOK_ATTR_MULTILINE TOK_ATTR_ONELINE -%token TOK_ATTR_REFCOUNT TOK_ATTR_REQUIRES +%token TOK_ATTR_REFCOUNT TOK_ATTR_REQUIRES %token TOK_ATTR_RESTOFDATA TOK_ATTR_RESTOFFLOW %token TOK_ATTR_TRANSIENT TOK_ATTR_UNTIL -%token TOK_ANALYZER TOK_CONNECTION TOK_FLOW -%token TOK_STATE TOK_ACTION TOK_WHEN TOK_HELPER +%token TOK_ANALYZER TOK_CONNECTION TOK_FLOW +%token TOK_STATE TOK_ACTION TOK_WHEN TOK_HELPER %token TOK_DATAUNIT TOK_FLOWDIR TOK_WITHCONTEXT -%token TOK_LPB_EXTERN TOK_LPB_HEADER TOK_LPB_CODE +%token TOK_LPB_EXTERN TOK_LPB_HEADER TOK_LPB_CODE %token TOK_LPB_MEMBER TOK_LPB_INIT TOK_LPB_CLEANUP TOK_LPB_EOF -%token TOK_LPB TOK_RPB +%token TOK_LPB TOK_RPB %token TOK_EMBEDDED_ATOM TOK_EMBEDDED_STRING -%token TOK_PAC_VAL TOK_PAC_SET TOK_PAC_TYPE TOK_PAC_TYPEOF TOK_PAC_CONST_DEF +%token TOK_PAC_VAL TOK_PAC_SET TOK_PAC_TYPE TOK_PAC_TYPEOF TOK_PAC_CONST_DEF %token TOK_END_PAC %token TOK_EXTERN TOK_NULLPTR @@ -35,7 +35,7 @@ %left TOK_AND %nonassoc TOK_EQUAL TOK_NEQ TOK_LE TOK_GE '<' '>' %left '&' '|' '^' -%left TOK_LSHIFT TOK_RSHIFT +%left TOK_LSHIFT TOK_RSHIFT %left '+' '-' %left '*' '/' '%' %right '~' '!' @@ -64,14 +64,14 @@ %type withinputfield letfield %type letfieldlist %type funcproto function -%type TOK_ID tok_id optfieldid +%type TOK_ID tok_id optfieldid %type input %type TOK_NULLPTR %type TOK_NUMBER %type embedded_pac_primitive %type param %type optparams paramlist -%type recordfield recordfield0 padding +%type recordfield recordfield0 padding %type recordfieldlist %type regex %type statevar @@ -262,15 +262,15 @@ decl_with_attr : TOK_TYPE tok_id { current_decl_id = $2; } optparams '=' type decl_without_attr: TOK_LPB_HEADER embedded_code TOK_RPB { - $$ = new HelperDecl(HelperDecl::HEADER, 0, $2); + $$ = new HelperDecl(HelperDecl::HEADER, nullptr, $2); } | TOK_LPB_CODE embedded_code TOK_RPB { - $$ = new HelperDecl(HelperDecl::CODE, 0, $2); + $$ = new HelperDecl(HelperDecl::CODE, nullptr, $2); } | TOK_LPB_EXTERN embedded_code TOK_RPB { - $$ = new HelperDecl(HelperDecl::EXTERN, 0, $2); + $$ = new HelperDecl(HelperDecl::EXTERN, nullptr, $2); } | TOK_REFINE TOK_TYPEATTR tok_id TOK_PLUSEQ attrlist { @@ -359,11 +359,11 @@ optparams : '(' paramlist ')' } | /* empty */ { - $$ = 0; + $$ = nullptr; } ; -paramlist : paramlist ',' param +paramlist : paramlist ',' param { $1->push_back($3); $$ = $1; @@ -387,7 +387,7 @@ param : tok_id ':' type2 optinit : /* nothing */ { - $$ = 0; + $$ = nullptr; } | '=' expr { @@ -397,7 +397,7 @@ optinit : /* nothing */ opttype : /* nothing */ { - $$ = 0; + $$ = nullptr; } | ':' type2 { @@ -465,7 +465,7 @@ recordfieldlist : recordfieldlist recordfield ';' $1->push_back($2); $$ = $1; } - | /* empty */ + | /* empty */ { $$ = new RecordFieldList(); } @@ -526,7 +526,7 @@ casefieldlist : casefieldlist casefield ';' $1->push_back($2); $$ = $1; } - | /* empty */ + | /* empty */ { $$ = new CaseFieldList(); } @@ -534,7 +534,7 @@ casefieldlist : casefieldlist casefield ';' casefield : casefield0 optattrs { - $1->AddAttr($2); + $1->AddAttr($2); $$ = $1; } ; @@ -545,13 +545,13 @@ casefield0 : exprlist TOK_RIGHTARROW tok_id ':' type2 } | TOK_DEFAULT TOK_RIGHTARROW tok_id ':' type2 { - $$ = new CaseField(0, $3, $5); + $$ = new CaseField(nullptr, $3, $5); } ; optexprlist : /* nothing */ { - $$ = 0; + $$ = nullptr; } | exprlist { @@ -605,8 +605,8 @@ expr : tok_id } | expr '(' optexprlist ')' { - $$ = new Expr(Expr::EXPR_CALL, - $1, + $$ = new Expr(Expr::EXPR_CALL, + $1, new Expr($3)); } | '-' expr @@ -697,7 +697,7 @@ expr : tok_id { $$ = new Expr(Expr::EXPR_COND, $1, $3, $5); } - | TOK_CASE expr TOK_OF '{' caseexprlist '}' + | TOK_CASE expr TOK_OF '{' caseexprlist '}' { $$ = new Expr($2, $5); } @@ -718,8 +718,8 @@ cstr : TOK_STRING ; regex : TOK_BEGIN_RE TOK_REGEX TOK_END_RE - { - $$ = new RegEx($2); + { + $$ = new RegEx($2); } ; @@ -740,7 +740,7 @@ caseexpr : exprlist TOK_RIGHTARROW expr } | TOK_DEFAULT TOK_RIGHTARROW expr { - $$ = new CaseExpr(0, $3); + $$ = new CaseExpr(nullptr, $3); } ; @@ -823,8 +823,8 @@ sah : TOK_LPB_MEMBER embedded_code TOK_RPB | TOK_DATAUNIT '=' tok_id optargs TOK_WITHCONTEXT '(' optexprlist ')' ';' { $$ = new AnalyzerDataUnit( - (AnalyzerDataUnit::DataUnitType) $1, - $3, + (AnalyzerDataUnit::DataUnitType) $1, + $3, $4, $7); } @@ -916,7 +916,7 @@ embedded_pac_primitive: TOK_PAC_VAL expr TOK_END_PAC optargs : /* empty */ { - $$ = 0; + $$ = nullptr; } | '(' optexprlist ')' { @@ -924,7 +924,7 @@ optargs : /* empty */ } ; -letfieldlist : letfieldlist letfield ';' +letfieldlist : letfieldlist letfield ';' { $1->push_back($2); $$ = $1; @@ -961,9 +961,9 @@ input : expr } ; -optattrs : /* empty */ +optattrs : /* empty */ { - $$ = 0; + $$ = nullptr; } | attrlist { @@ -993,7 +993,7 @@ attr : TOK_ATTR_BYTEORDER '=' expr { $$ = new Attr(ATTR_BYTEORDER, $3); } - | TOK_ATTR_CHECK expr + | TOK_ATTR_CHECK expr { $$ = new Attr(ATTR_CHECK, $2); } @@ -1009,7 +1009,7 @@ attr : TOK_ATTR_BYTEORDER '=' expr { $$ = new Attr(ATTR_EXPORTSOURCEDATA); } - | TOK_ATTR_IF expr + | TOK_ATTR_IF expr { $$ = new Attr(ATTR_IF, $2); } @@ -1061,7 +1061,7 @@ attr : TOK_ATTR_BYTEORDER '=' expr optlinebreaker : /* nothing */ { - $$ = 0; + $$ = nullptr; } | '(' expr ')' { @@ -1071,7 +1071,7 @@ optlinebreaker : /* nothing */ %% -const ID* current_decl_id = 0; +const ID* current_decl_id = nullptr; int yyerror(const char msg[]) { auto n = strlen(msg) + yyleng + 64; diff --git a/tools/binpac/src/pac_primitive.cc b/tools/binpac/src/pac_primitive.cc index e129cc81f6..c6efeea2da 100644 --- a/tools/binpac/src/pac_primitive.cc +++ b/tools/binpac/src/pac_primitive.cc @@ -9,7 +9,7 @@ string PPVal::ToCode(Env* env) { ASSERT(expr_); - return string(expr_->EvalExpr(nullptr, env)); + return {expr_->EvalExpr(nullptr, env)}; } string PPSet::ToCode(Env* env) { diff --git a/tools/binpac/src/pac_primitive.h b/tools/binpac/src/pac_primitive.h index d9869c15d2..b88e03d871 100644 --- a/tools/binpac/src/pac_primitive.h +++ b/tools/binpac/src/pac_primitive.h @@ -3,11 +3,13 @@ #ifndef pac_primitive_h #define pac_primitive_h +#include + #include "pac_common.h" class PacPrimitive { public: - enum PrimitiveType { VAL, SET, TYPE, CONST_DEF }; + enum PrimitiveType : uint8_t { VAL, SET, TYPE, CONST_DEF }; explicit PacPrimitive(PrimitiveType type) : type_(type) {} virtual ~PacPrimitive() {} diff --git a/tools/binpac/src/pac_record.h b/tools/binpac/src/pac_record.h index 58abbc81bb..0323cf62b0 100644 --- a/tools/binpac/src/pac_record.h +++ b/tools/binpac/src/pac_record.h @@ -135,7 +135,7 @@ protected: bool DoTraverse(DataDepVisitor* visitor) override; }; -enum PaddingType { PAD_BY_LENGTH, PAD_TO_OFFSET, PAD_TO_NEXT_WORD }; +enum PaddingType : uint8_t { PAD_BY_LENGTH, PAD_TO_OFFSET, PAD_TO_NEXT_WORD }; class RecordPaddingField : public RecordField { public: diff --git a/tools/binpac/src/pac_strtype.cc b/tools/binpac/src/pac_strtype.cc index 63ea247d87..4c6365af86 100644 --- a/tools/binpac/src/pac_strtype.cc +++ b/tools/binpac/src/pac_strtype.cc @@ -273,11 +273,7 @@ void StringType::GenDynamicSizeRegEx(Output* out_cc, Env* env, const DataPtr& da void StringType::GenDynamicSizeAnyStr(Output* out_cc, Env* env, const DataPtr& data) { ASSERT(type_ == ANYSTR); - if ( attr_restofdata_ || attr_oneline_ ) { - out_cc->println("%s = (%s) - (%s);", env->LValue(string_length_var()), env->RValue(end_of_data), - data.ptr_expr()); - } - else if ( attr_restofflow_ ) { + if ( attr_restofdata_ || attr_oneline_ || attr_restofflow_ ) { out_cc->println("%s = (%s) - (%s);", env->LValue(string_length_var()), env->RValue(end_of_data), data.ptr_expr()); } diff --git a/tools/binpac/src/pac_strtype.h b/tools/binpac/src/pac_strtype.h index 4e725b5f13..e96dc8d261 100644 --- a/tools/binpac/src/pac_strtype.h +++ b/tools/binpac/src/pac_strtype.h @@ -3,12 +3,14 @@ #ifndef pac_strtype_h #define pac_strtype_h +#include + #include "pac_type.h" // TODO: question: shall we merge it with ArrayType? class StringType : public Type { public: - enum StringTypeEnum { CSTR, REGEX, ANYSTR }; + enum StringTypeEnum : uint8_t { CSTR, REGEX, ANYSTR }; explicit StringType(StringTypeEnum anystr); explicit StringType(ConstString* str); diff --git a/tools/binpac/src/pac_type.cc b/tools/binpac/src/pac_type.cc index 83aa507f94..0488c6f8cc 100644 --- a/tools/binpac/src/pac_type.cc +++ b/tools/binpac/src/pac_type.cc @@ -696,7 +696,7 @@ string Type::DataSize(Output* out_cc, Env* env, const DataPtr& data) { } else { if ( ! size_var() || ! env->Evaluated(size_var()) ) { - ASSERT(out_cc != 0); + ASSERT(out_cc != nullptr); GenDynamicSize(out_cc, env, data); ASSERT(size_var()); } diff --git a/tools/binpac/src/pac_type.h b/tools/binpac/src/pac_type.h index 33e8a45c98..d2ec0e910f 100644 --- a/tools/binpac/src/pac_type.h +++ b/tools/binpac/src/pac_type.h @@ -303,7 +303,7 @@ public: static Type* LookUpByID(ID* id); protected: - typedef map type_map_t; + using type_map_t = map; static type_map_t type_map_; }; diff --git a/tools/binpac/src/pac_typedecl.cc b/tools/binpac/src/pac_typedecl.cc index 9395593070..5c4af595a3 100644 --- a/tools/binpac/src/pac_typedecl.cc +++ b/tools/binpac/src/pac_typedecl.cc @@ -101,7 +101,7 @@ void TypeDecl::GenCode(Output* out_h, Output* out_cc) { AddBaseClass(&base_classes); if ( type_->attr_refcount() ) - base_classes.push_back(kRefCountClass); + base_classes.emplace_back(kRefCountClass); // The first line of class definition out_h->println(""); @@ -232,7 +232,8 @@ string TypeDecl::ParseFuncPrototype(Env* env) { } void TypeDecl::GenParsingEnd(Output* out_cc, Env* env, const DataPtr& data) { - string ret_val_0, ret_val_1; + string ret_val_0; + string ret_val_1; if ( type_->incremental_input() ) { ret_val_0 = type_->parsing_complete(env).c_str(); @@ -259,9 +260,6 @@ void TypeDecl::GenParsingEnd(Output* out_cc, Env* env, const DataPtr& data) { out_cc->println("BINPAC_ASSERT(!(%s));", type_->parsing_complete(env).c_str()); out_cc->println("return %s;", ret_val_1.c_str()); } - else if ( type_->incremental_input() ) { - out_cc->println("return %s;", ret_val_0.c_str()); - } else { out_cc->println("return %s;", ret_val_0.c_str()); } diff --git a/tools/binpac/src/pac_utils.cc b/tools/binpac/src/pac_utils.cc index 5d9b2651e2..2f8285e4e8 100644 --- a/tools/binpac/src/pac_utils.cc +++ b/tools/binpac/src/pac_utils.cc @@ -2,9 +2,9 @@ #include "pac_utils.h" -#include -#include -#include +#include +#include +#include char* copy_string(const char* s) { char* c = new char[strlen(s) + 1]; @@ -27,7 +27,7 @@ string strfmt(const char* format, ...) { va_start(ap, format); const char* r = do_fmt(format, ap); va_end(ap); - return string(r); + return {r}; } char* nfmt(const char* format, ...) {