Skip to content

Commit f3d5668

Browse files
jay-tuxMaartenS11
andauthored
🔨 Refactorio #0: Fix all compiler warnings ♻️ (#268)
* Remove unused parameter stack * Make psram [[maybe_unused]] since it is not used on some targets * Use static_cast instead of c style cast * Formatting * Fix compiler warnings in src/Primitives/emulated.cpp * Fix warnings + static_cast + fixed memory leak by using std::string copy constructor with length * Use zu for size type * Change one more lu to zu * Remove unused variables from socket.h * Mark some things as maybe_unused when only used in debug mode * Reformat * Fix compiler warnings in src/Debug/debugger.cpp * Ran clang-format * Even more clang-format * Fix linker errors for obscure platforms * Oops, we forgot Zephyr --------- Co-authored-by: MaartenS11 <maarten.steevens@gmail.com>
1 parent d038faf commit f3d5668

File tree

13 files changed

+219
-190
lines changed

13 files changed

+219
-190
lines changed

src/Debug/debugger.cpp

Lines changed: 135 additions & 113 deletions
Large diffs are not rendered by default.

src/Debug/debugger.h

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Debugger {
119119

120120
// Private methods
121121

122-
void printValue(StackValue *v, uint32_t idx, bool end) const;
122+
void printValue(const StackValue *v, uint32_t idx, bool end) const;
123123

124124
// TODO Move parsing to WARDuino class?
125125
void parseDebugBuffer(size_t len, const uint8_t *buff);
@@ -128,25 +128,25 @@ class Debugger {
128128

129129
//// Handle REPL interrupts
130130

131-
void handleInvoke(Module *m, uint8_t *interruptData);
131+
void handleInvoke(Module *m, uint8_t *interruptData) const;
132132

133133
//// Handle Interrupt Types
134134

135-
void handleInterruptRUN(Module *m, RunningState *program_state);
135+
void handleInterruptRUN(const Module *m, RunningState *program_state);
136136

137-
void handleSTEP(Module *m, RunningState *program_state);
137+
void handleSTEP(const Module *m, RunningState *program_state);
138138

139-
void handleSTEPOver(Module *m, RunningState *program_state);
139+
void handleSTEPOver(const Module *m, RunningState *program_state);
140140

141141
void handleInterruptBP(Module *m, uint8_t *interruptData);
142142

143143
//// Information dumps
144144

145145
void dump(Module *m, bool full = false) const;
146146

147-
void dumpStack(Module *m) const;
147+
void dumpStack(const Module *m) const;
148148

149-
void dumpLocals(Module *m) const;
149+
void dumpLocals(const Module *m) const;
150150

151151
void dumpBreakpoints(Module *m) const;
152152

@@ -158,27 +158,28 @@ class Debugger {
158158

159159
void dumpCallbackmapping() const;
160160

161-
void inspect(Module *m, uint16_t sizeStateArray, uint8_t *state);
161+
void inspect(Module *m, uint16_t sizeStateArray,
162+
const uint8_t *state) const;
162163

163164
//// Handle live code update
164165

165-
static bool handleChangedFunction(Module *m, uint8_t *bytes);
166+
static bool handleChangedFunction(const Module *m, uint8_t *bytes);
166167

167-
bool handleChangedLocal(Module *m, uint8_t *bytes) const;
168+
bool handleChangedLocal(const Module *m, uint8_t *bytes) const;
168169

169-
bool handleUpdateModule(Module *m, uint8_t *data);
170+
static bool handleUpdateModule(Module *m, uint8_t *data);
170171

171-
bool handleUpdateGlobalValue(Module *m, uint8_t *data);
172+
bool handleUpdateGlobalValue(const Module *m, uint8_t *data) const;
172173

173-
bool handleUpdateStackValue(Module *m, uint8_t *bytes);
174+
bool handleUpdateStackValue(const Module *m, uint8_t *bytes) const;
174175

175-
bool reset(Module *m);
176+
bool reset(Module *m) const;
176177

177178
//// Handle out-of-place debugging
178179

179180
void freeState(Module *m, uint8_t *interruptData);
180181

181-
static uint8_t *findOpcode(Module *m, Block *block);
182+
static uint8_t *findOpcode(Module *m, const Block *block);
182183

183184
bool saveState(Module *m, uint8_t *interruptData);
184185

@@ -213,7 +214,7 @@ class Debugger {
213214

214215
void stop();
215216

216-
void pauseRuntime(Module *m); // pause runtime for given module
217+
void pauseRuntime(const Module *m); // pause runtime for given module
217218

218219
// Interrupts
219220

@@ -235,20 +236,20 @@ class Debugger {
235236

236237
// Out-of-place debugging: EDWARD
237238

238-
void snapshot(Module *m);
239+
void snapshot(Module *m) const;
239240

240-
void enableSnapshots(uint8_t *interruptData);
241+
void enableSnapshots(const uint8_t *interruptData);
241242

242-
void sendAsyncSnapshots(Module *m);
243+
void sendAsyncSnapshots(Module *m) const;
243244

244245
void proxify();
245246

246247
void handleProxyCall(Module *m, RunningState *program_state,
247-
uint8_t *interruptData);
248+
uint8_t *interruptData) const;
248249

249-
RFC *topProxyCall();
250+
RFC *topProxyCall() const;
250251

251-
void sendProxyCallResult(Module *m);
252+
void sendProxyCallResult(Module *m) const;
252253

253254
bool isProxy() const;
254255

@@ -258,11 +259,11 @@ class Debugger {
258259

259260
bool proxy_connected() const;
260261

261-
void disconnect_proxy();
262+
void disconnect_proxy() const;
262263

263264
// Pull-based
264265

265-
void handleMonitorProxies(Module *m, uint8_t *interruptData);
266+
void handleMonitorProxies(const Module *m, uint8_t *interruptData) const;
266267

267268
// Push-based
268269

src/Interpreter/instructions.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,9 @@ bool i_instr_conversion(Module *m, uint8_t opcode) {
12611261
/**
12621262
* 0xe0 ... 0xe3 callback operations
12631263
*/
1264-
bool i_instr_callback(Module *m, uint8_t opcode) {
1264+
// TODO: Remove [[maybe_unused]] when implemented
1265+
bool i_instr_callback([[maybe_unused]] Module *m,
1266+
[[maybe_unused]] uint8_t opcode) {
12651267
// TODO
12661268
return true;
12671269
}

src/Interpreter/interpreter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ bool Interpreter::interpret(Module *m, bool waiting) {
461461
return success;
462462
}
463463

464-
void Interpreter::report_overflow(Module *m, uint8_t *maddr) {
464+
void Interpreter::report_overflow([[maybe_unused]] Module *m,
465+
[[maybe_unused]] uint8_t *maddr) {
465466
dbg_warn("memory start: %p, memory end: %p, maddr: %p\n", m->memory.bytes,
466467
m->memory.bytes + m->memory.pages * (uint32_t)PAGE_SIZE, maddr);
467468
sprintf(exception, "out of bounds memory access");

src/Memory/mem.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#endif
1111

1212
// Assert calloc
13-
void *acalloc(size_t nmemb, size_t size, const char *name, bool psram) {
14-
if ((int)(nmemb * size) == 0) {
13+
void *acalloc(size_t nmemb, size_t size, const char *name,
14+
[[maybe_unused]] bool psram) {
15+
if (static_cast<int>(nmemb * size) == 0) {
1516
return nullptr;
1617
} else {
1718
debug("IN Acalloc count: %zu, size: %zu for %s \n", nmemb, size, name);
@@ -28,8 +29,8 @@ void *acalloc(size_t nmemb, size_t size, const char *name, bool psram) {
2829
debug("Done ... Acalloc\n");
2930
if (res == nullptr) {
3031
debug("FAILED ... Acalloc\n");
31-
FATAL("Could not allocate %d bytes for %s \n", (int)(nmemb * size),
32-
name);
32+
FATAL("Could not allocate %d bytes for %s \n",
33+
static_cast<int>(nmemb * size), name);
3334
}
3435
debug("NOT FAILED ... Acalloc\n");
3536
return res;
@@ -38,7 +39,7 @@ void *acalloc(size_t nmemb, size_t size, const char *name, bool psram) {
3839

3940
// Assert realloc/calloc
4041
void *arecalloc(void *ptr, size_t old_nmemb, size_t nmemb, size_t size,
41-
const char *name, bool psram) {
42+
const char *name, [[maybe_unused]] bool psram) {
4243
#ifdef ARDUINO
4344
void *res;
4445
if (psramInit() && psram) {
@@ -47,10 +48,11 @@ void *arecalloc(void *ptr, size_t old_nmemb, size_t nmemb, size_t size,
4748
res = (size_t *)calloc(nmemb, size);
4849
}
4950
#else
50-
auto *res = (size_t *)calloc(nmemb, size);
51+
auto *res = static_cast<size_t *>(calloc(nmemb, size));
5152
#endif
5253
if (res == nullptr) {
53-
FATAL("Could not allocate %d bytes for %s", (int)(nmemb * size), name);
54+
FATAL("Could not allocate %d bytes for %s",
55+
static_cast<int>(nmemb * size), name);
5456
}
5557
memset(res, 0, nmemb * size); // initialize memory with 0
5658
memmove(res, ptr, old_nmemb * size);

src/Primitives/arduino.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ bool resolve_external_memory(char *symbol, Memory **val) {
10791079
// Restore external state when restoring a snapshot
10801080
//------------------------------------------------------
10811081
void restore_external_state(Module *m,
1082-
std::vector<IOStateElement> external_state) {
1082+
const std::vector<IOStateElement> &external_state) {
10831083
uint8_t opcode = *m->pc_ptr;
10841084
// TODO: Maybe primitives can also be called using the other call
10851085
// instructions such as call_indirect

src/Primitives/emulated.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ double sensor_emu = 0;
6363

6464
#define def_prim(function_name, type) \
6565
Type function_name##_type = type; \
66-
bool function_name(Module *m)
66+
bool function_name([[maybe_unused]] Module *m)
6767

6868
#define def_prim_reverse(function_name) \
6969
void function_name##_reverse(Module *m, \
@@ -270,7 +270,8 @@ def_prim(test, oneToNoneU32) {
270270
Callback c = Callback(m, topic, fidx);
271271
CallbackHandler::add_callback(c);
272272
auto *payload = reinterpret_cast<const unsigned char *>("TestPayload");
273-
CallbackHandler::push_event(topic, (const char *)payload, 11);
273+
CallbackHandler::push_event(topic, reinterpret_cast<const char *>(payload),
274+
11);
274275
pop_args(1);
275276
return true;
276277
}
@@ -318,11 +319,11 @@ def_prim(wifi_connected, NoneToOneU32) {
318319

319320
def_prim(wifi_localip, twoToOneU32) {
320321
uint32_t buff = arg1.uint32;
321-
uint32_t size = arg0.uint32;
322+
// uint32_t size = arg0.uint32; // never used in emulator
322323
std::string ip = "192.168.0.181";
323324

324325
for (unsigned long i = 0; i < ip.length(); i++) {
325-
m->memory.bytes[buff + i] = (uint32_t)ip[i];
326+
m->memory.bytes[buff + i] = *reinterpret_cast<uint8_t *>(&ip[i]);
326327
}
327328
pop_args(2);
328329
pushInt32(buff);
@@ -345,7 +346,8 @@ def_prim(http_get, fourToOneU32) {
345346
return false; // TRAP
346347
}
347348
for (unsigned long i = 0; i < answer.length(); i++) {
348-
m->memory.bytes[response + i] = (uint32_t)answer[i];
349+
m->memory.bytes[response + i] =
350+
*reinterpret_cast<unsigned char *>(&answer[i]);
349351
}
350352

351353
// Pop args and return response address
@@ -365,7 +367,7 @@ def_prim(http_post, tenToOneU32) {
365367
uint32_t authorization = arg3.uint32;
366368
uint32_t authorization_len = arg2.uint32;
367369
int32_t response = arg1.uint32;
368-
uint32_t size = arg0.uint32;
370+
// uint32_t size = arg0.uint32; // never used in emulator
369371

370372
std::string url_parsed = parse_utf8_string(m->memory.bytes, url_len, url);
371373
std::string body_parsed =
@@ -398,14 +400,14 @@ def_prim(chip_digital_write, twoToNoneU32) {
398400
}
399401

400402
def_prim(chip_digital_read, oneToOneU32) {
401-
uint8_t pin = arg0.uint32;
403+
// uint8_t pin = arg0.uint32; // never used in emulator
402404
pop_args(1);
403405
pushUInt32(1); // HIGH
404406
return true;
405407
}
406408

407409
def_prim(chip_analog_read, oneToOneI32) {
408-
uint8_t pin = arg0.uint32;
410+
// uint8_t pin = arg0.uint32; // never used in emulator
409411
pop_args(1);
410412
pushInt32(sin(sensor_emu) * 100);
411413
sensor_emu += .25;
@@ -460,11 +462,11 @@ def_prim(write_spi_bytes_16, twoToNoneU32) {
460462
def_prim(subscribe_interrupt, threeToNoneU32) {
461463
uint8_t pin = arg2.uint32; // GPIOPin
462464
uint8_t tidx = arg1.uint32; // Table Idx pointing to Callback function
463-
uint8_t mode = arg0.uint32;
465+
// uint8_t mode = arg0.uint32; // never used in emulator
464466

465467
debug("EMU: subscribe_interrupt(%u, %u, %u) \n", pin, tidx, mode);
466468

467-
if (tidx < 0 || m->table.size < tidx) {
469+
if (m->table.size < tidx) {
468470
debug("subscribe_interrupt: out of range table index %i\n", tidx);
469471
return false;
470472
}
@@ -554,6 +556,7 @@ void install_primitives() {
554556
//------------------------------------------------------
555557
// resolving the primitives
556558
//------------------------------------------------------
559+
// ReSharper disable once CppDFAConstantFunctionResult
557560
bool resolve_primitive(char *symbol, Primitive *val) {
558561
debug("Resolve primitives (%d) for %s \n", ALL_PRIMITIVES, symbol);
559562

@@ -566,34 +569,35 @@ bool resolve_primitive(char *symbol, Primitive *val) {
566569
}
567570
}
568571
FATAL("Could not find primitive %s \n", symbol);
569-
return false;
572+
// return false; // unreachable
570573
}
571574

572575
Memory external_mem{};
573576

577+
// ReSharper disable once CppDFAConstantFunctionResult
574578
bool resolve_external_memory(char *symbol, Memory **val) {
575579
if (!strcmp(symbol, "memory")) {
576580
if (external_mem.bytes == nullptr) {
577581
external_mem.initial = 256;
578582
external_mem.maximum = 256;
579583
external_mem.pages = 256;
580-
external_mem.bytes = (uint8_t *)acalloc(
581-
external_mem.pages * PAGE_SIZE, sizeof(uint32_t),
582-
"Module->memory.bytes primitive");
584+
external_mem.bytes = static_cast<uint8_t *>(
585+
acalloc(external_mem.pages * PAGE_SIZE, sizeof(uint32_t),
586+
"Module->memory.bytes primitive"));
583587
}
584588
*val = &external_mem;
585589
return true;
586590
}
587591

588592
FATAL("Could not find memory %s \n", symbol);
589-
return false;
593+
// return false; // unreachable
590594
}
591595

592596
//------------------------------------------------------
593597
// Restore external state when restoring a snapshot
594598
//------------------------------------------------------
595599
void restore_external_state(Module *m,
596-
std::vector<IOStateElement> external_state) {
600+
const std::vector<IOStateElement> &external_state) {
597601
uint8_t opcode = *m->pc_ptr;
598602
// TODO: Maybe primitives can also be called using the other call
599603
// instructions such as call_indirect
@@ -617,7 +621,7 @@ void restore_external_state(Module *m,
617621
}
618622
}
619623

620-
std::vector<IOStateElement *> get_io_state(Module *m) {
624+
std::vector<IOStateElement *> get_io_state(Module *) {
621625
std::vector<IOStateElement *> ioState;
622626
for (auto &primitive : primitives) {
623627
if (primitive.f_serialize_state) {

src/Primitives/idf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ bool resolve_external_memory(char *symbol, Memory **val) {
298298
// Restore external state when restoring a snapshot
299299
//------------------------------------------------------
300300
void restore_external_state(Module *m,
301-
std::vector<IOStateElement> external_state) {
301+
const std::vector<IOStateElement> &external_state) {
302302
uint8_t opcode = *m->pc_ptr;
303303
// TODO: Maybe primitives can also be called using the other call
304304
// instructions such as call_indirect

src/Primitives/primitives.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ void install_primitives();
3030
std::vector<IOStateElement *> get_io_state(Module *m);
3131

3232
void restore_external_state(Module *m,
33-
std::vector<IOStateElement> external_state);
33+
const std::vector<IOStateElement> &external_state);
3434

35-
inline void create_stack(std::vector<StackValue> *stack) {}
35+
inline void create_stack(std::vector<StackValue> *) {}
3636

3737
template <typename T, typename... Ts>
3838
void create_stack(std::vector<StackValue> *stack, T value, Ts... args) {

src/Primitives/zephyr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ bool resolve_external_memory(char *symbol, Memory **val) {
316316
// Restore external state when restoring a snapshot
317317
//------------------------------------------------------
318318
void restore_external_state(Module *m,
319-
std::vector<IOStateElement> external_state) {
319+
const std::vector<IOStateElement> &external_state) {
320320
uint8_t opcode = *m->pc_ptr;
321321
// TODO: Maybe primitives can also be called using the other call
322322
// instructions such as call_indirect

0 commit comments

Comments
 (0)