Internals: cppcheck cleanups. No functional change.

This commit is contained in:
Wilson Snyder 2025-06-28 12:29:41 -04:00
parent 62e5e3aa0c
commit b914cda1c7
20 changed files with 85 additions and 71 deletions

View File

@ -411,10 +411,12 @@ CHECK_YL = $(wildcard \
$(srcdir)/src/*.l )
CPPCHECK = src/cppcheck_filtered cppcheck
CPPCHECK_FLAGS = --enable=all --inline-suppr \
--suppress=cstyleCast --suppress=useInitializationList \
--suppress=nullPointer --suppress=nullPointerRedundantCheck --suppress=ctunullpointer \
--suppress=cstyleCast --suppress=ctunullpointer \
--suppress=derefInvalidIteratorRedundantCheck \
--suppress=nullPointer --suppress=nullPointerRedundantCheck \
--suppress=templateRecursion \
--suppress=unusedFunction --suppress=unusedScopedObject \
--suppress=useStlAlgorithm \
--suppress=useInitializationList --suppress=useStlAlgorithm \
CPPCHECK_FLAGS += --xml
CPPCHECK_DEP = $(subst .cpp,.cppcheck,$(CHECK_CPP))

View File

@ -411,7 +411,7 @@ IData VL_SCOPED_RAND_RESET_I(int obits, uint64_t scopeHash, uint64_t salt) VL_MT
if (Verilated::threadContextp()->randReset() == 0) return 0;
IData data = ~0;
if (Verilated::threadContextp()->randReset() != 1) { // if 2, randomize
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
data = rng.rand64();
}
data &= VL_MASK_I(obits);
@ -422,7 +422,7 @@ QData VL_SCOPED_RAND_RESET_Q(int obits, uint64_t scopeHash, uint64_t salt) VL_MT
if (Verilated::threadContextp()->randReset() == 0) return 0;
QData data = ~0ULL;
if (Verilated::threadContextp()->randReset() != 1) { // if 2, randomize
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
data = rng.rand64();
}
data &= VL_MASK_Q(obits);
@ -432,31 +432,27 @@ QData VL_SCOPED_RAND_RESET_Q(int obits, uint64_t scopeHash, uint64_t salt) VL_MT
WDataOutP VL_SCOPED_RAND_RESET_W(int obits, WDataOutP outwp, uint64_t scopeHash,
uint64_t salt) VL_MT_UNSAFE {
if (Verilated::threadContextp()->randReset() != 2) { return VL_RAND_RESET_W(obits, outwp); }
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
for (int i = 0; i < VL_WORDS_I(obits) - 1; ++i) outwp[i] = rng.rand64();
outwp[VL_WORDS_I(obits) - 1] = rng.rand64() & VL_MASK_E(obits);
return outwp;
}
IData VL_SCOPED_RAND_RESET_ASSIGN_I(int obits, uint64_t scopeHash, uint64_t salt) VL_MT_UNSAFE {
IData data = ~0;
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
data = rng.rand64();
data &= VL_MASK_I(obits);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
const IData data = rng.rand64() & VL_MASK_I(obits);
return data;
}
QData VL_SCOPED_RAND_RESET_ASSIGN_Q(int obits, uint64_t scopeHash, uint64_t salt) VL_MT_UNSAFE {
QData data = ~0ULL;
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
data = rng.rand64();
data &= VL_MASK_Q(obits);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
const QData data = rng.rand64() & VL_MASK_Q(obits);
return data;
}
WDataOutP VL_SCOPED_RAND_RESET_ASSIGN_W(int obits, WDataOutP outwp, uint64_t scopeHash,
uint64_t salt) VL_MT_UNSAFE {
VlRNG rng(Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt);
VlRNG rng{Verilated::threadContextp()->randSeed() ^ scopeHash ^ salt};
for (int i = 0; i < VL_WORDS_I(obits) - 1; ++i) outwp[i] = rng.rand64();
outwp[VL_WORDS_I(obits) - 1] = rng.rand64() & VL_MASK_E(obits);
return outwp;

View File

@ -409,7 +409,7 @@ public:
// Record a flat (non-class) element into the array variable table
template <typename T>
typename std::enable_if<!std::is_class<T>::value, void>::type
record_arr_table(T& var, const std::string name, int dimension, std::vector<IData> indices,
record_arr_table(T& var, const std::string& name, int dimension, std::vector<IData> indices,
std::vector<size_t> idxWidths) {
const std::string key = generateKey(name, m_index);
m_arr_vars[key] = std::make_shared<ArrayInfo>(name, &var, m_index, indices, idxWidths);
@ -418,7 +418,7 @@ public:
// Recursively record all elements in an unpacked array
template <typename T, std::size_t N_Depth>
void record_arr_table(VlUnpacked<T, N_Depth>& var, const std::string name, int dimension,
void record_arr_table(VlUnpacked<T, N_Depth>& var, const std::string& name, int dimension,
std::vector<IData> indices, std::vector<size_t> idxWidths) {
if ((dimension > 0) && (N_Depth != 0)) {
idxWidths.push_back(32);
@ -434,7 +434,7 @@ public:
// Recursively record all elements in a queue
template <typename T>
void record_arr_table(VlQueue<T>& var, const std::string name, int dimension,
void record_arr_table(VlQueue<T>& var, const std::string& name, int dimension,
std::vector<IData> indices, std::vector<size_t> idxWidths) {
if ((dimension > 0) && (var.size() != 0)) {
idxWidths.push_back(32);
@ -449,8 +449,9 @@ public:
// Recursively record all elements in an associative array
template <typename T_Key, typename T_Value>
void record_arr_table(VlAssocArray<T_Key, T_Value>& var, const std::string name, int dimension,
std::vector<IData> indices, std::vector<size_t> idxWidths) {
void record_arr_table(VlAssocArray<T_Key, T_Value>& var, const std::string& name,
int dimension, std::vector<IData> indices,
std::vector<size_t> idxWidths) {
if ((dimension > 0) && (var.size() != 0)) {
for (auto it = var.begin(); it != var.end(); ++it) {
const T_Key& key = it->first;
@ -478,7 +479,7 @@ public:
// Register a single structArray element via write_var
template <typename T>
typename std::enable_if<VlContainsCustomStruct<T>::value, void>::type
record_struct_arr(T& var, const std::string name, int dimension, std::vector<IData> indices,
record_struct_arr(T& var, const std::string& name, int dimension, std::vector<IData> indices,
std::vector<size_t> idxWidths) {
std::ostringstream oss;
for (size_t i = 0; i < indices.size(); ++i) {
@ -492,7 +493,7 @@ public:
// Recursively process VlUnpacked of structs
template <typename T, std::size_t N_Depth>
void record_struct_arr(VlUnpacked<T, N_Depth>& var, const std::string name, int dimension,
void record_struct_arr(VlUnpacked<T, N_Depth>& var, const std::string& name, int dimension,
std::vector<IData> indices, std::vector<size_t> idxWidths) {
if (dimension > 0 && N_Depth != 0) {
constexpr size_t idx_width = 1 << VL_CLOG2_CE_Q(VL_CLOG2_CE_Q(N_Depth) + 1);
@ -507,7 +508,7 @@ public:
// Recursively process VlQueue of structs
template <typename T>
void record_struct_arr(VlQueue<T>& var, const std::string name, int dimension,
void record_struct_arr(VlQueue<T>& var, const std::string& name, int dimension,
std::vector<IData> indices, std::vector<size_t> idxWidths) {
if ((dimension > 0) && (var.size() != 0)) {
idxWidths.push_back(32);
@ -521,9 +522,9 @@ public:
// Recursively process associative arrays of structs
template <typename T_Key, typename T_Value>
void record_struct_arr(VlAssocArray<T_Key, T_Value>& var, const std::string name,
int dimension, std::vector<IData> indices,
std::vector<size_t> idxWidths) {
void record_struct_arr(VlAssocArray<T_Key, T_Value>& var, const std::string& name,
int dimension, const std::vector<IData>& indices,
const std::vector<size_t>& idxWidths) {
if ((dimension > 0) && (!var.empty())) {
for (auto it = var.begin(); it != var.end(); ++it) {
const T_Key& key = it->first;
@ -551,7 +552,7 @@ public:
// Helper: Register all members of a user-defined struct
template <typename T, std::size_t... I>
void modifyMembers(T& obj, std::index_sequence<I...>, std::string baseName) {
void modifyMembers(T& obj, std::index_sequence<I...>, const std::string& baseName) {
// Use the indices to access each member via std::get
(void)std::initializer_list<int>{
(write_var(std::get<I>(obj.getMembers(obj)), obj.memberWidth()[I],

View File

@ -135,7 +135,7 @@ bool VlThreadPool::isNumactlRunning() {
}
std::string VlThreadPool::numaAssign() {
#if defined(__linux) || defined(CPU_ZERO) // Linux-like; assume we have pthreads etc
#if defined(__linux) || defined(CPU_ZERO) || defined(VL_CPPCHECK) // Linux-like pthreads
// If not under numactl, make a reasonable processor affinity selection
if (isNumactlRunning()) return "running under numactl"; // User presumably set affinity
const int num_threads = static_cast<int>(m_workers.size());
@ -157,24 +157,25 @@ std::string VlThreadPool::numaAssign() {
std::map<int, int> processor_core;
std::multimap<int, int> core_processors;
std::set<int> cores;
int processor = -1;
int core = -1;
while (!is.eof()) {
std::string line;
std::getline(is, line);
static std::string::size_type pos = line.find(":");
int number = -1;
if (pos != std::string::npos) number = atoi(line.c_str() + pos + 1);
if (line.compare(0, std::strlen("processor"), "processor") == 0) {
processor = number;
core = -1;
} else if (line.compare(0, std::strlen("core id"), "core id") == 0) {
core = number;
// std::cout << "p" << processor << " socket " << socket << " c" << core << std::endl;
cores.emplace(core);
processor_core[processor] = core;
core_processors.emplace(core, processor);
unassigned_processors.push_back(processor);
{
int processor = -1;
while (!is.eof()) {
std::string line;
std::getline(is, line);
static std::string::size_type pos = line.find(":");
int number = -1;
if (pos != std::string::npos) number = atoi(line.c_str() + pos + 1);
if (line.compare(0, std::strlen("processor"), "processor") == 0) {
processor = number;
} else if (line.compare(0, std::strlen("core id"), "core id") == 0) {
const int core = number;
// std::cout << "p" << processor << " socket " << socket << " c" << core <<
// std::endl;
cores.emplace(core);
processor_core[processor] = core;
core_processors.emplace(core, processor);
unassigned_processors.push_back(processor);
}
}
}

View File

@ -241,6 +241,7 @@ public:
private:
VL_UNCOPYABLE(VlThreadPool);
// cppcheck-suppress unusedPrivateFunction
static bool isNumactlRunning();
std::string numaAssign();
};

View File

@ -2537,6 +2537,7 @@ void vl_vpi_put_word_gen(const VerilatedVpioVar* vop, T word, size_t bitCount, s
= (info.m_datap[info.m_wordOffset + 1] & ~info.m_maskHi)
| ((word >> (wordBits - info.m_bitOffset)) & info.m_maskHi);
}
// cppcheck-has-bug-suppress unreadVariable
info.m_datap[info.m_wordOffset] = (info.m_datap[info.m_wordOffset] & ~info.m_maskLo)
| ((word << info.m_bitOffset) & info.m_maskLo);
}
@ -2978,7 +2979,6 @@ void vl_get_value_array_vectors(unsigned index, const unsigned num, const unsign
p_vpi_vecval dst) {
static_assert(std::is_unsigned<T>::value,
"type T is not unsigned"); // ensure logical right shift
const unsigned element_size_bytes = VL_BYTES_I(packedSize);
const unsigned element_size_words = VL_WORDS_I(packedSize);
if (sizeof(T) == sizeof(QData)) {
for (unsigned i = 0; i < num; ++i) {

View File

@ -53,6 +53,7 @@ class AssertVisitor final : public VNVisitor {
// METHODS
static AstNodeExpr* assertOnCond(FileLine* fl, VAssertType type,
VAssertDirectiveType directiveType) {
// cppcheck-suppress missingReturn
switch (directiveType) {
case VAssertDirectiveType::INTRINSIC: return new AstConst{fl, AstConst::BitTrue{}};
case VAssertDirectiveType::VIOLATION_CASE: {

View File

@ -3072,7 +3072,9 @@ void AstCMethodHard::setPurity() {
return;
}
auto isPureIt = isPureMethod.find(name());
// cppcheck-suppress derefInvalidIteratorRedundantCheck
UASSERT_OBJ(isPureIt != isPureMethod.end(), this, "Unknown purity of method " + name());
// cppcheck-suppress derefInvalidIteratorRedundantCheck
m_pure = isPureIt->second;
if (!m_pure) return;
if (!fromp()->isPure()) m_pure = false;

View File

@ -167,6 +167,8 @@ class DelayedVisitor final : public VNVisitor {
} m_kitUnion;
public:
VarScopeInfo() = default;
~VarScopeInfo() = default;
// Accessors for the above union fields
auto& shadowVariableKit() {
UASSERT(m_scheme == Scheme::ShadowVar, "Inconsistent Scheme");

View File

@ -443,6 +443,7 @@ DfgVarPacked* DfgVertex::getResultVar() {
// Inspect existing variables fully written by this vertex, and choose one
DfgVarPacked* resp = nullptr;
// cppcheck-has-bug-suppress constParameter
this->forEachSink([&resp](DfgVertex& sink) {
DfgVarPacked* const varp = sink.cast<DfgVarPacked>();
if (!varp) return;

View File

@ -165,6 +165,7 @@ class AstToDfgVisitor final : public VNVisitor {
if (AstVarRef* const vrefp = VN_CAST(nodep, VarRef)) {
m_foundUnhandled = false;
visit(vrefp);
// cppcheck-has-bug-suppress knownConditionTrueFalse
if (m_foundUnhandled) return false;
getVertex(vrefp)->as<DfgVarPacked>()->addDriver(flp, 0, vtxp);
return true;
@ -178,6 +179,7 @@ class AstToDfgVisitor final : public VNVisitor {
}
m_foundUnhandled = false;
visit(vrefp);
// cppcheck-has-bug-suppress knownConditionTrueFalse
if (m_foundUnhandled) return false;
getVertex(vrefp)->as<DfgVarPacked>()->addDriver(flp, lsbp->toUInt(), vtxp);
return true;
@ -191,6 +193,7 @@ class AstToDfgVisitor final : public VNVisitor {
}
m_foundUnhandled = false;
visit(vrefp);
// cppcheck-has-bug-suppress knownConditionTrueFalse
if (m_foundUnhandled) return false;
getVertex(vrefp)->as<DfgVarArray>()->addDriver(flp, idxp->toUInt(), vtxp);
return true;
@ -244,6 +247,7 @@ class AstToDfgVisitor final : public VNVisitor {
m_foundUnhandled = false;
iterate(rhsp);
// cppcheck-has-bug-suppress knownConditionTrueFalse
if (m_foundUnhandled) {
revertUncommittedVertices();
markReferenced(nodep);

View File

@ -447,17 +447,17 @@ void V3DfgPasses::binToOneHot(DfgGraph& dfg, V3DfgBinToOneHotContext& ctx) {
const uint32_t val = pair.first;
const std::vector<Term>& terms = pair.second;
// Create the ArraySel
FileLine* const flp = terms.front().m_vtxp->fileline();
DfgArraySel* const aselp = new DfgArraySel{dfg, flp, bitDTypep};
FileLine* const aflp = terms.front().m_vtxp->fileline();
DfgArraySel* const aselp = new DfgArraySel{dfg, aflp, bitDTypep};
aselp->fromp(tabVtxp);
aselp->bitp(new DfgConst{dfg, flp, width, val});
aselp->bitp(new DfgConst{dfg, aflp, width, val});
// The inverted value, if needed
DfgNot* notp = nullptr;
// Repalce the terms
for (const Term& term : terms) {
if (term.m_inv) {
if (!notp) {
notp = new DfgNot{dfg, flp, bitDTypep};
notp = new DfgNot{dfg, aflp, bitDTypep};
notp->srcp(aselp);
}
term.m_vtxp->replaceWith(notp);

View File

@ -345,14 +345,14 @@ class V3DfgPeephole final : public DfgVisitor {
// Make associative trees right leaning to reduce pattern variations, and for better CSE
bool changed = false;
while (true) {
Vertex* const lhsp = vtxp->lhsp()->template cast<Vertex>();
if (!lhsp || lhsp->hasMultipleSinks()) break;
Vertex* const alhsp = vtxp->lhsp()->template cast<Vertex>();
if (!alhsp || alhsp->hasMultipleSinks()) break;
APPLYING(RIGHT_LEANING_ASSOC) {
// Rotate the expression tree rooted at 'vtxp' to the right, producing a
// right-leaning tree
DfgVertex* const ap = lhsp->lhsp();
DfgVertex* const bp = lhsp->rhsp();
DfgVertex* const ap = alhsp->lhsp();
DfgVertex* const bp = alhsp->rhsp();
DfgVertex* const cp = vtxp->rhsp();
AstNodeDType* const rootDtyptp = vtxp->dtypep();
@ -364,7 +364,7 @@ class V3DfgPeephole final : public DfgVisitor {
}
Vertex* const childp = make<Vertex>(vtxp->fileline(), childDtyptp, bp, cp);
Vertex* const rootp = make<Vertex>(lhsp->fileline(), rootDtyptp, ap, childp);
Vertex* const rootp = make<Vertex>(alhsp->fileline(), rootDtyptp, ap, childp);
replace(vtxp, rootp);
changed = true;
vtxp = rootp;

View File

@ -278,7 +278,7 @@ void EmitCBaseVisitorConst::emitVarAccessors(const AstVar* nodep) {
void EmitCBaseVisitorConst::emitModCUse(const AstNodeModule* modp, VUseType useType) {
bool nl = false;
forModCUse(modp, useType, [&](string entry) {
forModCUse(modp, useType, [&](const string& entry) {
puts(entry);
nl = true;
});

View File

@ -78,7 +78,7 @@ private:
std::vector<WorkList*> m_concatenableListsByDescSize; // Lists sorted by size, descending
EmitGroup(std::vector<FilenameWithScore> inputFiles, uint64_t totalScore,
std::string groupFilePrefix)
const std::string& groupFilePrefix)
: m_inputFiles{std::move(inputFiles)}
, m_totalScore{totalScore}
, m_groupFilePrefix{groupFilePrefix} {}

View File

@ -172,10 +172,12 @@ void V3ErrorGuarded::v3errorEndGuts(std::ostringstream& sstr, const string& extr
// If suppressed print only first line to reduce verbosity
string firstLine = msg;
string::size_type pos;
if ((pos = firstLine.find('\n')) != string::npos) {
firstLine.erase(pos, firstLine.length() - pos);
firstLine += "...";
{
string::size_type pos;
if ((pos = firstLine.find('\n')) != string::npos) {
firstLine.erase(pos, firstLine.length() - pos);
firstLine += "...";
}
}
if (m_errorSuppressed) msg = firstLine;
// Suppress duplicate messages

View File

@ -552,8 +552,8 @@ int V3ParseImp::tokenPipelineId(int token) {
VL_RESTORER(yylval); // Remember value, as about to read ahead
if (m_tokenLastBison.token != '@' && m_tokenLastBison.token != '#'
&& m_tokenLastBison.token != '.') {
if (const size_t depth = tokenPipeScanIdInst(0)) return yaID__aINST;
if (const size_t depth = tokenPipeScanIdType(0)) return yaID__aTYPE;
if (tokenPipeScanIdInst(0)) return yaID__aINST;
if (tokenPipeScanIdType(0)) return yaID__aTYPE;
}
if (nexttok == '#') { // e.g. class_type parameter_value_assignment '::'
const size_t depth = tokenPipeScanParam(0, false);

View File

@ -342,6 +342,7 @@ void transformForks(AstNetlist* const netlistp) {
m_funcp = nodep;
m_awaitMoved = false;
iterateChildren(nodep);
// cppcheck-has-bug-suppress knownConditionTrueFalse
if (nodep->isCoroutine() && m_awaitMoved
&& !nodep->stmtsp()->exists([](AstCAwait*) { return true; })) {
// co_return at the end (either that or a co_await is required in a coroutine

View File

@ -204,7 +204,7 @@ string VString::unquoteSVString(const string& text, string& errOut) {
} else if (*cp == '\\') {
if (octal_digits) {
newtext += octal_val;
octal_digits = 0;
// below: octal_digits = 0;
octal_val = 0;
}
quoted = true;

View File

@ -451,12 +451,12 @@ class TaskVisitor final : public VNVisitor {
AstNodeExpr* postRhsp = new AstVarRef{newvscp->fileline(), newvscp, VAccess::READ};
if (AstResizeLValue* soutPinp = VN_CAST(outPinp, ResizeLValue)) {
outPinp = soutPinp->lhsp();
if (AstNodeUniop* soutPinp = VN_CAST(outPinp, Extend)) {
outPinp = soutPinp->lhsp();
} else if (AstNodeUniop* soutPinp = VN_CAST(outPinp, ExtendS)) {
outPinp = soutPinp->lhsp();
} else if (AstSel* soutPinp = VN_CAST(outPinp, Sel)) {
outPinp = soutPinp->fromp();
if (AstNodeUniop* aoutPinp = VN_CAST(outPinp, Extend)) {
outPinp = aoutPinp->lhsp();
} else if (AstNodeUniop* aoutPinp = VN_CAST(outPinp, ExtendS)) {
outPinp = aoutPinp->lhsp();
} else if (AstSel* aoutPinp = VN_CAST(outPinp, Sel)) {
outPinp = aoutPinp->fromp();
} else {
outPinp->v3fatalSrc("Inout pin resizing should have had extend or select");
}