Internals: Fix cppcheck warnings

This commit is contained in:
Wilson Snyder 2024-01-17 19:48:07 -05:00
parent 8b03f2d47b
commit 89cfa0737f
12 changed files with 30 additions and 28 deletions

View File

@ -340,7 +340,7 @@ CPPCHECK_FLAGS = --enable=all --inline-suppr \
--suppress=useStlAlgorithm --suppress=useStlAlgorithm
CPPCHECK_FLAGS += --xml CPPCHECK_FLAGS += --xml
CPPCHECK_DEP = $(subst .cpp,.cppcheck,$(CHECK_CPP)) CPPCHECK_DEP = $(subst .cpp,.cppcheck,$(CHECK_CPP))
CPPCHECK_INC = -I$(srcdir)/include -I$(srcdir)/src/obj_dbg -I$(srcdir)/src CPPCHECK_INC = -I$(srcdir)/include -I$(srcdir)/include/gtkwave -I$(srcdir)/include/vltstd -I$(srcdir)/src/obj_dbg -I$(srcdir)/src
cppcheck: cppcheck-1 cppcheck-2 cppcheck-3 cppcheck-4 cppcheck-5 cppcheck-6 cppcheck-7 cppcheck-8 cppcheck: cppcheck-1 cppcheck-2 cppcheck-3 cppcheck-4 cppcheck-5 cppcheck-6 cppcheck-7 cppcheck-8
cppcheck-1: cppcheck-1:

View File

@ -2693,7 +2693,7 @@ std::pair<int, char**> VerilatedContextImp::argc_argv() VL_MT_SAFE_EXCLUDES(m_ar
int in = 0; int in = 0;
for (const auto& i : m_args.m_argVec) { for (const auto& i : m_args.m_argVec) {
s_argvp[in] = new char[i.length() + 1]; s_argvp[in] = new char[i.length() + 1];
std::strcpy(s_argvp[in], i.c_str()); std::memcpy(s_argvp[in], i.c_str(), i.length() + 1);
++in; ++in;
} }
s_argvp[s_argc] = nullptr; s_argvp[s_argc] = nullptr;

View File

@ -507,9 +507,9 @@ public:
delete this; // IEEE 37.2.2 vpi_scan at end does a vpi_release_handle delete this; // IEEE 37.2.2 vpi_scan at end does a vpi_release_handle
return nullptr; return nullptr;
} }
const VerilatedScope::Type type = (*m_it)->type(); const VerilatedScope::Type itype = (*m_it)->type();
const VerilatedScope* const modp = *m_it++; const VerilatedScope* const modp = *m_it++;
if (type == VerilatedScope::SCOPE_MODULE) { if (itype == VerilatedScope::SCOPE_MODULE) {
return (new VerilatedVpioModule{modp})->castVpiHandle(); return (new VerilatedVpioModule{modp})->castVpiHandle();
} }
} }
@ -523,9 +523,9 @@ class VerilatedVpioPackage final : public VerilatedVpioScope {
public: public:
explicit VerilatedVpioPackage(const VerilatedScope* modulep) explicit VerilatedVpioPackage(const VerilatedScope* modulep)
: VerilatedVpioScope{modulep} { : VerilatedVpioScope{modulep} {
const char* fullname = m_scopep->name(); const char* sfullname = m_scopep->name();
if (std::strncmp(fullname, "TOP.", 4) == 0) fullname += 4; if (std::strncmp(sfullname, "TOP.", 4) == 0) sfullname += 4;
m_fullname = std::string{fullname} + "::"; m_fullname = std::string{sfullname} + "::";
if (m_fullname == "\\$unit ::") m_fullname = "$unit::"; if (m_fullname == "\\$unit ::") m_fullname = "$unit::";
m_name = std::string(m_scopep->identifier()); m_name = std::string(m_scopep->identifier());
if (m_name == "\\$unit ") m_name = "$unit"; if (m_name == "\\$unit ") m_name = "$unit";
@ -558,12 +558,12 @@ public:
delete this; // IEEE 37.2.2 vpi_scan at end does a vpi_release_handle delete this; // IEEE 37.2.2 vpi_scan at end does a vpi_release_handle
return nullptr; return nullptr;
} }
const VerilatedScope::Type type = (*m_it)->type(); const VerilatedScope::Type itype = (*m_it)->type();
const VerilatedScope* const modp = *m_it++; const VerilatedScope* const modp = *m_it++;
if (type == VerilatedScope::SCOPE_MODULE) { if (itype == VerilatedScope::SCOPE_MODULE) {
return (new VerilatedVpioModule{modp})->castVpiHandle(); return (new VerilatedVpioModule{modp})->castVpiHandle();
} }
if (type == VerilatedScope::SCOPE_PACKAGE) { if (itype == VerilatedScope::SCOPE_PACKAGE) {
return (new VerilatedVpioPackage{modp})->castVpiHandle(); return (new VerilatedVpioPackage{modp})->castVpiHandle();
} }
} }

View File

@ -1116,6 +1116,7 @@ bool AstNode::sameTreeIter(const AstNode* node1p, const AstNode* node2p, bool ig
void AstNode::checkTreeIter(const AstNode* prevBackp) const VL_MT_STABLE { void AstNode::checkTreeIter(const AstNode* prevBackp) const VL_MT_STABLE {
// private: Check a tree and children // private: Check a tree and children
UASSERT_OBJ(prevBackp == this->backp(), this, "Back node inconsistent"); UASSERT_OBJ(prevBackp == this->backp(), this, "Back node inconsistent");
// cppcheck-suppress danglingTempReference
const VNTypeInfo& typeInfo = *type().typeInfo(); const VNTypeInfo& typeInfo = *type().typeInfo();
for (int i = 1; i <= 4; i++) { for (int i = 1; i <= 4; i++) {
AstNode* nodep = nullptr; AstNode* nodep = nullptr;
@ -1126,6 +1127,7 @@ void AstNode::checkTreeIter(const AstNode* prevBackp) const VL_MT_STABLE {
case 4: nodep = op4p(); break; case 4: nodep = op4p(); break;
default: this->v3fatalSrc("Bad case"); break; default: this->v3fatalSrc("Bad case"); break;
} }
// cppcheck-suppress danglingTempReference
const char* opName = typeInfo.m_opNamep[i - 1]; const char* opName = typeInfo.m_opNamep[i - 1];
switch (typeInfo.m_opType[i - 1]) { switch (typeInfo.m_opType[i - 1]) {
case VNTypeInfo::OP_UNUSED: case VNTypeInfo::OP_UNUSED:
@ -1142,7 +1144,7 @@ void AstNode::checkTreeIter(const AstNode* prevBackp) const VL_MT_STABLE {
case VNTypeInfo::OP_LIST: case VNTypeInfo::OP_LIST:
if (const AstNode* const headp = nodep) { if (const AstNode* const headp = nodep) {
const AstNode* backp = this; const AstNode* backp = this;
const AstNode* tailp = headp; const AstNode* tailp;
const AstNode* opp = headp; const AstNode* opp = headp;
do { do {
opp->checkTreeIter(backp); opp->checkTreeIter(backp);

View File

@ -125,6 +125,7 @@ public:
constexpr VNType(en _e) VL_MT_SAFE : m_e{_e} {} constexpr VNType(en _e) VL_MT_SAFE : m_e{_e} {}
explicit VNType(int _e) explicit VNType(int _e)
: m_e(static_cast<en>(_e)) {} // Need () or GCC 4.8 false warning : m_e(static_cast<en>(_e)) {} // Need () or GCC 4.8 false warning
// cppcheck-suppress danglingTempReference
const VNTypeInfo* typeInfo() const VL_MT_SAFE { return &typeInfoTable[m_e]; } const VNTypeInfo* typeInfo() const VL_MT_SAFE { return &typeInfoTable[m_e]; }
constexpr operator en() const VL_MT_SAFE { return m_e; } constexpr operator en() const VL_MT_SAFE { return m_e; }
}; };

View File

@ -542,9 +542,8 @@ class ConstBitOpTreeVisitor final : public VNVisitorConst {
restorer.disableRestore(); // Now all checks passed restorer.disableRestore(); // Now all checks passed
} else if (nodep->type() == m_rootp->type()) { // And, Or, Xor } else if (nodep->type() == m_rootp->type()) { // And, Or, Xor
incrOps(nodep, __LINE__); incrOps(nodep, __LINE__);
VL_RESTORER(m_leafp);
for (const bool right : {false, true}) { for (const bool right : {false, true}) {
VL_RESTORER(m_leafp);
Restorer restorer{*this}; Restorer restorer{*this};
LeafInfo leafInfo{m_lsb}; LeafInfo leafInfo{m_lsb};
m_leafp = &leafInfo; m_leafp = &leafInfo;

View File

@ -237,6 +237,7 @@ class EmitCSyms final : EmitCBaseVisitorConst {
// << scpSym << endl); // << scpSym << endl);
if (v3Global.opt.vpi()) varHierarchyScopes(scpName); if (v3Global.opt.vpi()) varHierarchyScopes(scpName);
if (m_scopeNames.find(scpSym) == m_scopeNames.end()) { if (m_scopeNames.find(scpSym) == m_scopeNames.end()) {
// cppcheck-suppress stlFindInsert
m_scopeNames.emplace(scpSym, m_scopeNames.emplace(scpSym,
ScopeData{scpSym, scpPretty, 0, "SCOPE_OTHER"}); ScopeData{scpSym, scpPretty, 0, "SCOPE_OTHER"});
} }
@ -341,6 +342,7 @@ class EmitCSyms final : EmitCBaseVisitorConst {
ScopeFuncData(nodep, m_cfuncp, m_modp)); ScopeFuncData(nodep, m_cfuncp, m_modp));
} else { } else {
if (m_scopeNames.find(nodep->scopeDpiName()) == m_scopeNames.end()) { if (m_scopeNames.find(nodep->scopeDpiName()) == m_scopeNames.end()) {
// cppcheck-suppress stlFindInsert
m_scopeNames.emplace(nodep->scopeDpiName(), m_scopeNames.emplace(nodep->scopeDpiName(),
ScopeData{nodep->scopeDpiName(), nodep->scopePrettyDpiName(), ScopeData{nodep->scopeDpiName(), nodep->scopePrettyDpiName(),
timeunit, "SCOPE_OTHER"}); timeunit, "SCOPE_OTHER"});

View File

@ -48,7 +48,7 @@ class GateEitherVertex VL_NOT_FINAL : public V3GraphVertex {
bool m_dedupable = true; // True if this node should be able to be deduped bool m_dedupable = true; // True if this node should be able to be deduped
bool m_consumed = false; // Output goes to something meaningful bool m_consumed = false; // Output goes to something meaningful
public: public:
GateEitherVertex(V3Graph* graphp) explicit GateEitherVertex(V3Graph* graphp)
: V3GraphVertex{graphp} {} : V3GraphVertex{graphp} {}
~GateEitherVertex() override = default; ~GateEitherVertex() override = default;
@ -811,8 +811,9 @@ class GateInline final {
// If the consumer logic writes one of the variables that the substitution // If the consumer logic writes one of the variables that the substitution
// is reading, then we would get a cycles, so we cannot do that. // is reading, then we would get a cycles, so we cannot do that.
bool canInline = true; bool canInline = true;
for (V3GraphEdge* edgep = dstVtxp->outBeginp(); edgep; edgep = edgep->outNextp()) { for (V3GraphEdge* dedgep = dstVtxp->outBeginp(); dedgep;
const GateVarVertex* const consVVertexp = edgep->top()->as<GateVarVertex>(); dedgep = dedgep->outNextp()) {
const GateVarVertex* const consVVertexp = dedgep->top()->as<GateVarVertex>();
if (readVscps.count(consVVertexp->varScp())) { if (readVscps.count(consVVertexp->varScp())) {
canInline = false; canInline = false;
break; break;
@ -1327,7 +1328,7 @@ class GateUnused final {
} }
} }
GateUnused(GateGraph& graph) explicit GateUnused(GateGraph& graph)
: m_graph{graph} { : m_graph{graph} {
mark(); // Mark all used vertices mark(); // Mark all used vertices
remove(); // Remove unused vertices remove(); // Remove unused vertices

View File

@ -456,12 +456,12 @@ private:
clearOptimizable(nodep, "Var write & read"); clearOptimizable(nodep, "Var write & read");
} }
m_varAux(vscp).usage |= VU_RV; m_varAux(vscp).usage |= VU_RV;
const bool isConst = (nodep->varp()->isConst() || nodep->varp()->isParam()) const bool varIsConst = (nodep->varp()->isConst() || nodep->varp()->isParam())
&& nodep->varp()->valuep(); && nodep->varp()->valuep();
AstNodeExpr* const valuep AstNodeExpr* const valuep
= isConst ? fetchValueNull(nodep->varp()->valuep()) : nullptr; = varIsConst ? fetchValueNull(nodep->varp()->valuep()) : nullptr;
// Propagate PARAM constants for constant function analysis // Propagate PARAM constants for constant function analysis
if (isConst && valuep) { if (varIsConst && valuep) {
if (!m_checkOnly && optimizable()) newValue(vscp, valuep); if (!m_checkOnly && optimizable()) newValue(vscp, valuep);
} else { } else {
if (m_checkOnly) varRefCb(nodep); if (m_checkOnly) varRefCb(nodep);

View File

@ -58,7 +58,7 @@ class StackCountVisitor final : public VNVisitorConst {
public: public:
// CONSTRUCTORS // CONSTRUCTORS
StackCountVisitor(AstNode* nodep) { iterateConstNull(nodep); } explicit StackCountVisitor(AstNode* nodep) { iterateConstNull(nodep); }
~StackCountVisitor() override = default; ~StackCountVisitor() override = default;
// METHODS // METHODS

View File

@ -391,9 +391,8 @@ class TraceDeclVisitor final : public VNVisitor {
} else { } else {
// This is a subscope: insert a placeholder to be fixed up later // This is a subscope: insert a placeholder to be fixed up later
AstCell* const cellp = entry.cellp(); AstCell* const cellp = entry.cellp();
FileLine* const flp = cellp->fileline(); AstNodeStmt* const stmtp = new AstComment{
AstNodeStmt* const stmtp cellp->fileline(), "Cell init for: " + cellp->prettyName()};
= new AstComment{flp, "Cell init for: " + cellp->prettyName()};
addToSubFunc(stmtp); addToSubFunc(stmtp);
m_cellInitPlaceholders.emplace_back(nodep, cellp, stmtp); m_cellInitPlaceholders.emplace_back(nodep, cellp, stmtp);
} }

View File

@ -4411,10 +4411,8 @@ class WidthVisitor final : public VNVisitor {
static bool usesDynamicScheduler(AstNode* nodep) { static bool usesDynamicScheduler(AstNode* nodep) {
UASSERT_OBJ(nodep->dtypep()->isEvent(), nodep, "Node does not have an event dtype"); UASSERT_OBJ(nodep->dtypep()->isEvent(), nodep, "Node does not have an event dtype");
AstVarRef* vrefp;
while (true) { while (true) {
vrefp = VN_CAST(nodep, VarRef); AstVarRef* const vrefp = VN_CAST(nodep, VarRef);
if (vrefp) return usesDynamicScheduler(vrefp); if (vrefp) return usesDynamicScheduler(vrefp);
if (VN_IS(nodep, MemberSel)) { if (VN_IS(nodep, MemberSel)) {
return true; return true;