From c507dcf610277168f313790b24ab750948df704c Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Fri, 22 May 2026 17:59:57 -0400 Subject: [PATCH] Internals: Enforce types on C++ enums. No functional change intended. --- include/verilated.h | 2 +- src/V3AstNodeExpr.h | 2 +- src/V3AstNodeStmt.h | 2 +- src/V3Control.h | 2 +- src/V3EmitCHeaders.cpp | 10 +++++----- src/V3ExecGraph.cpp | 10 ++++------ src/V3FileLine.h | 2 +- src/V3LinkDotIfaceCapture.h | 2 +- src/V3PreProc.h | 2 +- test_regress/t/t_dist_cppstyle.py | 8 ++++++++ test_regress/t/t_vpi_repetitive_cbs.cpp | 2 +- 11 files changed, 25 insertions(+), 19 deletions(-) diff --git a/include/verilated.h b/include/verilated.h index dcb5144cd..e139b1a25 100644 --- a/include/verilated.h +++ b/include/verilated.h @@ -140,7 +140,7 @@ enum VerilatedVarType : uint8_t { VLVT_REAL // AKA double }; -enum VerilatedVarFlags { +enum VerilatedVarFlags : uint32_t { VLVD_0 = 0, // None VLVD_IN = 1, // == vpiInput VLVD_OUT = 2, // == vpiOutput diff --git a/src/V3AstNodeExpr.h b/src/V3AstNodeExpr.h index 46d4ac746..bab376bdd 100644 --- a/src/V3AstNodeExpr.h +++ b/src/V3AstNodeExpr.h @@ -5329,7 +5329,7 @@ public: class AstAtoN final : public AstNodeUniop { // string.atoi(), atobin(), atohex(), atooct(), atoireal() public: - enum FmtType { ATOI = 10, ATOHEX = 16, ATOOCT = 8, ATOBIN = 2, ATOREAL = -1 }; + enum FmtType : int { ATOI = 10, ATOHEX = 16, ATOOCT = 8, ATOBIN = 2, ATOREAL = -1 }; private: const FmtType m_fmt; // Operation type diff --git a/src/V3AstNodeStmt.h b/src/V3AstNodeStmt.h index 37def443a..e7214e3f2 100644 --- a/src/V3AstNodeStmt.h +++ b/src/V3AstNodeStmt.h @@ -1455,7 +1455,7 @@ public: }; private: - enum operation m_operation; + operation m_operation; public: AstAssignCompound(AstAssignCompound::operation operation, FileLine* fl, AstNodeExpr* lhsp, diff --git a/src/V3Control.h b/src/V3Control.h index 5b8ba76df..c555ea188 100644 --- a/src/V3Control.h +++ b/src/V3Control.h @@ -38,7 +38,7 @@ public: string resetValue; }; - enum class VarSpecKind { + enum class VarSpecKind : uint8_t { PARAM, // Select only matching parameters PORT, // Select only matching ports VAR // Select any matching AstVar (including params and ports) diff --git a/src/V3EmitCHeaders.cpp b/src/V3EmitCHeaders.cpp index 2ab7cffe7..b97def937 100644 --- a/src/V3EmitCHeaders.cpp +++ b/src/V3EmitCHeaders.cpp @@ -279,12 +279,12 @@ class EmitCHeader final : public EmitCConstInit { emitUnpackedUOrSBody(sdtypep); } } - enum class AttributeType { Width, Dimension }; + enum class AttributeType : uint8_t { WIDTH, DIMENSION }; // Get member attribute based on type int getNodeAttribute(const AstMemberDType* itemp, AttributeType type) { const bool isArrayType = itemp->dtypep()->isNonPackedArray(); switch (type) { - case AttributeType::Width: { + case AttributeType::WIDTH: { if (isArrayType) { // For arrays, get innermost element width const AstNodeDType* dtype = itemp->dtypep(); @@ -293,7 +293,7 @@ class EmitCHeader final : public EmitCConstInit { } return itemp->width(); } - case AttributeType::Dimension: { + case AttributeType::DIMENSION: { // Return array dimension or 0 for non-arrays return isArrayType ? itemp->dtypep()->dimensions(true).second : 0; } @@ -345,10 +345,10 @@ class EmitCHeader final : public EmitCConstInit { puts("};\n}\n"); putns(sdtypep, "\nstd::vector memberWidth(void) const {\n"); - emitMemberVector(sdtypep); + emitMemberVector(sdtypep); putns(sdtypep, "\nstd::vector memberDimension(void) const {\n"); - emitMemberVector(sdtypep); + emitMemberVector(sdtypep); needComma = false; putns(sdtypep, "\nauto memberIndices(void) const {\n"); diff --git a/src/V3ExecGraph.cpp b/src/V3ExecGraph.cpp index 6ac658602..edafaa962 100644 --- a/src/V3ExecGraph.cpp +++ b/src/V3ExecGraph.cpp @@ -420,12 +420,10 @@ class PackThreads final { // schedule to ensure that indexes for simulation-time thread pool workers are not shadowed // by another tasks. // For retaining control over thread schedules, we distinguish SchedulingModes: - enum class SchedulingMode { - SCHEDULING // Schedule normal tasks - , - WIDE_TASK_DISCOVERED // We found a wide task, if this is the only one available, - // switch to WIDE_TASK_SCHEDULING - , + enum class SchedulingMode : uint8_t { + SCHEDULING, // Schedule normal tasks + WIDE_TASK_DISCOVERED, // Found a wide task, if this is the only one available, + // switch to WIDE_TASK_SCHEDULING WIDE_TASK_SCHEDULING // Schedule wide tasks }; SchedulingMode mode = SchedulingMode::SCHEDULING; diff --git a/src/V3FileLine.h b/src/V3FileLine.h index 5e66d6db8..4c75f3c96 100644 --- a/src/V3FileLine.h +++ b/src/V3FileLine.h @@ -53,7 +53,7 @@ class FileLineSingleton final { VErrorBitSet m_ctrlEn; // Enabled by control file public: - enum class Subset { + enum class Subset : uint8_t { CODE = 0, // Selects m_codeEn, the enable bits used by in-code directives/metacomments CTRL = 1, // Selects m_ctrlEn, the enable bits used by control files }; diff --git a/src/V3LinkDotIfaceCapture.h b/src/V3LinkDotIfaceCapture.h index 781e03788..b3a8dbc4c 100644 --- a/src/V3LinkDotIfaceCapture.h +++ b/src/V3LinkDotIfaceCapture.h @@ -34,7 +34,7 @@ class VSymEnt; class V3LinkDotIfaceCapture final { public: - enum class CaptureType { IFACE, CLASS }; + enum class CaptureType : uint8_t { IFACE, CLASS }; // Path-based map key: no pointers, only stable strings. // {ownerModName, refName, cellPath, cloneCellPath} uniquely identifies diff --git a/src/V3PreProc.h b/src/V3PreProc.h index 618bc1f58..d968eac5d 100644 --- a/src/V3PreProc.h +++ b/src/V3PreProc.h @@ -40,7 +40,7 @@ protected: public: // CONSTANTS - enum MiscConsts { + enum MiscConsts : int { DEFINE_RECURSION_LEVEL_MAX = 1000, // How many `def substitutions before an error INCLUDE_DEPTH_MAX = 500, // How many `includes deep before an error // Streams deep (sometimes `def deep) before an error. diff --git a/test_regress/t/t_dist_cppstyle.py b/test_regress/t/t_dist_cppstyle.py index 67ed3fe55..7944e98c3 100755 --- a/test_regress/t/t_dist_cppstyle.py +++ b/test_regress/t/t_dist_cppstyle.py @@ -90,6 +90,14 @@ for filename in sorted(files.keys()): r'.*(Need \(\)|: m_e\()|V3OPTION_PARSER_DEF', "Use brace instead of parenthesis-style constructors e.g. ': m_...{...}'") + check_pattern( + filename, + contents, + r'\s*enum\s+(class\s+)?([a-zA-Z0-9_]+)', + # Ignore common m_e enum constructors + r' enum m_e | enum en | : [a-zA-Z]', + "Add type to enum declaration e.g. 'enum class foo : uint8_t'") + if re.search(r'\.(c|cpp)', filename): check_pattern(filename, contents, r'(\w+\s+)*(\binline\b)[^\n]*', None, "'inline' keyword is on functions defined in .cpp files") diff --git a/test_regress/t/t_vpi_repetitive_cbs.cpp b/test_regress/t/t_vpi_repetitive_cbs.cpp index e1d82a101..bf3b7be27 100644 --- a/test_regress/t/t_vpi_repetitive_cbs.cpp +++ b/test_regress/t/t_vpi_repetitive_cbs.cpp @@ -35,7 +35,7 @@ const std::vector cbs_to_test{cbValueChange}; -enum CallbackState { PRE_REGISTER, ACTIVE, ACTIVE_AGAIN, REM_REREG_ACTIVE, POST_REMOVE }; +enum CallbackState : uint8_t { PRE_REGISTER, ACTIVE, ACTIVE_AGAIN, REM_REREG_ACTIVE, POST_REMOVE }; const std::vector cb_states{PRE_REGISTER, ACTIVE, ACTIVE_AGAIN, REM_REREG_ACTIVE, POST_REMOVE};