Skip to content

Commit 3831eaa

Browse files
committed
refactor: use attribute((format(printf, 1, 2)))
1 parent 83b6b4b commit 3831eaa

11 files changed

+203
-176
lines changed

src/assert-printf.h

+18-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
1515
//
1616

17-
#ifndef PRINTF_ASSERT_H
18-
#define PRINTF_ASSERT_H
17+
#ifndef ASSERT_PRINTF_H
18+
#define ASSERT_PRINTF_H
1919

2020
/// \file
2121
/// \brief Microarchitecture-dependent includes for printf and assert
@@ -27,4 +27,20 @@
2727
#include <cstdio>
2828
#endif
2929

30+
#include <cstdarg>
31+
#include <tuple>
32+
33+
static inline void d_vprintf(const char *fmt, va_list ap) {
34+
std::ignore = vfprintf(stderr, fmt, ap);
35+
}
36+
37+
// Better to use C-style variadic function that checks for format!
38+
// NOLINTNEXTLINE(cert-dcl50-cpp)
39+
__attribute__((__format__(__printf__, 1, 2))) static inline void d_printf(const char *fmt, ...) {
40+
va_list ap;
41+
va_start(ap, fmt);
42+
d_vprintf(fmt, ap);
43+
va_end(ap);
44+
}
45+
3046
#endif

src/dump.h

-28
This file was deleted.

src/i-accept-scoped-notes.h

+21-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
/// \file
2121
/// \brief Accept scoped notes interface
2222

23+
#include <cstdarg>
2324
#include <cstdint>
2425
#include <type_traits>
2526

26-
#include "dump.h"
27+
#include "assert-printf.h"
2728
#include "i-state-access.h"
2829
#include "i-uarch-state-access.h"
2930
#include "meta.h"
@@ -48,32 +49,43 @@ class i_accept_scoped_notes { // CRTP
4849
}
4950

5051
public:
51-
/// \brief Works as printf if we are dumping scoped notes, otherwise does nothing
52-
template <size_t N, typename... ARGS>
53-
static void DSN_PRINTF([[maybe_unused]] const char (&fmt)[N], [[maybe_unused]] ARGS... args) {
52+
/// \brief Works as vprintf if we are dumping scoped notes, otherwise does nothing
53+
static void dsn_vprintf([[maybe_unused]] const char *fmt, [[maybe_unused]] va_list ap) {
5454
#ifdef DUMP_SCOPED_NOTE
5555
if constexpr (is_an_i_state_access_v<DERIVED>) {
56-
DERIVED::DSA_PRINTF(fmt, args...);
56+
DERIVED::dsa_vprintf(fmt, ap);
5757
} else if (is_an_i_uarch_state_access_v<DERIVED>) {
58-
DERIVED::DUSA_PRINTF(fmt, args...);
58+
DERIVED::dusa_vprintf(fmt, ap);
5959
} else {
60-
D_PRINTF(fmt, args...);
60+
d_vprintf(fmt, ap);
6161
}
6262
#endif
6363
}
6464

65+
/// \brief Works as printf if we are dumping scoped notes, otherwise does nothing
66+
// Better to use C-style variadic function that checks for format!
67+
// NOLINTNEXTLINE(cert-dcl50-cpp)
68+
__attribute__((__format__(__printf__, 1, 2))) static void dsn_printf([[maybe_unused]] const char *fmt, ...) {
69+
#ifdef DUMP_SCOPED_NOTE
70+
va_list ap;
71+
va_start(ap, fmt);
72+
dsn_vprintf(fmt, ap);
73+
va_end(ap);
74+
#endif
75+
}
76+
6577
/// \brief Adds a begin bracket annotation to the log
6678
/// \param text String with the text for the annotation
6779
void push_begin_bracket(const char *text) const {
6880
derived().do_push_begin_bracket(text);
69-
DSN_PRINTF("----> begin %s (%s)\n", text, derived().get_name());
81+
dsn_printf("----> begin %s (%s)\n", text, derived().get_name());
7082
}
7183

7284
/// \brief Adds an end bracket annotation to the log
7385
/// \param text String with the text for the annotation
7486
void push_end_bracket(const char *text) const {
7587
derived().do_push_end_bracket(text);
76-
DSN_PRINTF("<---- end %s (%s)\n", text, derived().get_name());
88+
dsn_printf("<---- end %s (%s)\n", text, derived().get_name());
7789
}
7890

7991
/// \brief Adds annotations to the state, bracketing a scope

src/i-state-access.h

+39-27
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
/// \brief State access interface
2222

2323
#include <cinttypes>
24+
#include <cstdarg>
2425
#include <cstdint>
2526
#include <type_traits>
2627
#include <utility>
2728

28-
#include "dump.h"
29+
#include "assert-printf.h"
2930
#include "i-prefer-shadow-state.h"
3031
#include "meta.h"
3132
#include "poor-type-name.h"
@@ -47,7 +48,7 @@ using i_state_access_fast_addr_t = typename i_state_access_fast_addr<STATE_ACCES
4748
uint64_t read_##REG() const { \
4849
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) { \
4950
const auto val = derived().do_read_##REG(); \
50-
DSA_PRINTF("%s::read_" #REG "() = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), val, val); \
51+
dsa_printf("%s::read_" #REG "() = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), val, val); \
5152
return val; \
5253
} else { \
5354
return prefer_read_shadow_state(shadow_state_what::REG); \
@@ -58,7 +59,7 @@ using i_state_access_fast_addr_t = typename i_state_access_fast_addr<STATE_ACCES
5859
void write_##REG(uint64_t val) const { \
5960
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) { \
6061
derived().do_write_##REG(val); \
61-
DSA_PRINTF("%s::write_" #REG "(%" PRIu64 "(0x%" PRIx64 "))\n", get_name(), val, val); \
62+
dsa_printf("%s::write_" #REG "(%" PRIu64 "(0x%" PRIx64 "))\n", get_name(), val, val); \
6263
} else { \
6364
prefer_write_shadow_state(shadow_state_what::REG, val); \
6465
} \
@@ -104,24 +105,35 @@ class i_state_access { // CRTP
104105
uint64_t prefer_read_shadow_state(shadow_state_what what) const {
105106
const auto val = derived().read_shadow_state(what);
106107
[[maybe_unused]] const auto *const what_name = shadow_state_get_what_name(what);
107-
DSA_PRINTF("%s::read_shadow_state(%s) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), what_name, val, val);
108+
dsa_printf("%s::read_shadow_state(%s) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), what_name, val, val);
108109
return val;
109110
}
110111

111112
void prefer_write_shadow_state(shadow_state_what what, uint64_t val) const {
112113
derived().write_shadow_state(what, val);
113114
[[maybe_unused]] const auto *const what_name = shadow_state_get_what_name(what);
114-
DSA_PRINTF("%s::write_shadow_state(%s, %" PRIu64 "(0x%" PRIx64 "))\n", get_name(), what_name, val, val);
115+
dsa_printf("%s::write_shadow_state(%s, %" PRIu64 "(0x%" PRIx64 "))\n", get_name(), what_name, val, val);
115116
}
116117

117118
public:
118119
using fast_addr = i_state_access_fast_addr_t<DERIVED>;
119120

121+
/// \brief Works as vprintf if we are dumping state accesses, otherwise does nothing
122+
static void dsa_vprintf([[maybe_unused]] const char *fmt, [[maybe_unused]] va_list ap) {
123+
#ifdef DUMP_STATE_ACCESS
124+
d_vprintf(fmt, ap);
125+
#endif
126+
}
127+
120128
/// \brief Works as printf if we are dumping state accesses, otherwise does nothing
121-
template <size_t N, typename... ARGS>
122-
static void DSA_PRINTF([[maybe_unused]] const char (&fmt)[N], [[maybe_unused]] ARGS... args) {
129+
// Better to use C-style variadic function that checks for format!
130+
// NOLINTNEXTLINE(cert-dcl50-cpp)
131+
__attribute__((__format__(__printf__, 1, 2))) static void dsa_printf([[maybe_unused]] const char *fmt, ...) {
123132
#ifdef DUMP_STATE_ACCESS
124-
D_PRINTF(fmt, args...);
133+
va_list ap;
134+
va_start(ap, fmt);
135+
dsa_vprintf(fmt, ap);
136+
va_end(ap);
125137
#endif
126138
}
127139

@@ -131,7 +143,7 @@ class i_state_access { // CRTP
131143
uint64_t read_x(int i) const {
132144
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
133145
const auto val = derived().do_read_x(i);
134-
DSA_PRINTF("%s::read_x(%d) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), i, val, val);
146+
dsa_printf("%s::read_x(%d) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), i, val, val);
135147
return val;
136148
} else {
137149
return prefer_read_shadow_state(shadow_state_get_what(shadow_state_what::x0, i));
@@ -146,7 +158,7 @@ class i_state_access { // CRTP
146158
void write_x(int i, uint64_t val) const {
147159
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
148160
derived().do_write_x(i, val);
149-
DSA_PRINTF("%s::write_x(%d, %" PRIu64 "(0x%" PRIx64 "))\n", get_name(), i, val, val);
161+
dsa_printf("%s::write_x(%d, %" PRIu64 "(0x%" PRIx64 "))\n", get_name(), i, val, val);
150162
} else {
151163
prefer_write_shadow_state(shadow_state_get_what(shadow_state_what::x0, i), val);
152164
}
@@ -158,7 +170,7 @@ class i_state_access { // CRTP
158170
uint64_t read_f(int i) const {
159171
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
160172
const auto val = derived().do_read_f(i);
161-
DSA_PRINTF("%s::read_f(%d) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), i, val, val);
173+
dsa_printf("%s::read_f(%d) = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), i, val, val);
162174
return val;
163175
} else {
164176
return prefer_read_shadow_state(shadow_state_get_what(shadow_state_what::f0, i));
@@ -171,7 +183,7 @@ class i_state_access { // CRTP
171183
void write_f(int i, uint64_t val) const {
172184
if constexpr (!is_an_i_prefer_shadow_state_v<DERIVED>) {
173185
derived().do_write_f(i, val);
174-
DSA_PRINTF("%s::write_f(%d, %" PRIu64 "(%" PRIx64 "))\n", get_name(), i, val, val);
186+
dsa_printf("%s::write_f(%d, %" PRIu64 "(%" PRIx64 "))\n", get_name(), i, val, val);
175187
} else {
176188
prefer_write_shadow_state(shadow_state_get_what(shadow_state_what::f0, i), val);
177189
}
@@ -269,7 +281,7 @@ class i_state_access { // CRTP
269281
/// \param index Index of PMA
270282
address_range &read_pma(uint64_t index) const {
271283
auto &ar = derived().do_read_pma(index);
272-
DSA_PRINTF("%s::read_address_range(%" PRIu64 ") = {%s, 0x%" PRIx64 ", 0x%" PRIx64 "}\n", get_name(), index,
284+
dsa_printf("%s::read_address_range(%" PRIu64 ") = {%s, 0x%" PRIx64 ", 0x%" PRIx64 "}\n", get_name(), index,
273285
pmas_get_DID_name(ar.get_driver_id()), ar.get_start(), ar.get_length());
274286
return ar;
275287
}
@@ -281,8 +293,8 @@ class i_state_access { // CRTP
281293
fast_addr get_faddr(uint64_t paddr, uint64_t pma_index) const {
282294
const auto val = derived().do_get_faddr(paddr, pma_index);
283295
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t> ? "phys_addr" : "fast_addr";
284-
DSA_PRINTF("%s::get_faddr(%" PRIu64 "(0x%" PRIx64 ")) = %s{%" PRIu64 "(0x%" PRIx64 ")}\n", get_name(), paddr,
285-
paddr, fast_addr_name, val, val);
296+
dsa_printf("%s::get_faddr(%" PRIu64 "(0x%" PRIx64 ")) = %s{%" PRIu64 "(0x%" PRIx64 ")}\n", get_name(), paddr,
297+
paddr, fast_addr_name, static_cast<uint64_t>(val), static_cast<uint64_t>(val));
286298
return val;
287299
}
288300

@@ -331,9 +343,9 @@ class i_state_access { // CRTP
331343
static_assert(std::is_integral_v<T> && sizeof(T) <= sizeof(uint64_t), "unsupported type");
332344
derived().template do_read_memory_word<T, A>(faddr, pma_index, pval);
333345
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t> ? "phys_addr" : "fast_addr";
334-
DSA_PRINTF("%s::read_memory_word<%s,%s>(%s{0x%" PRIx64 "}, %" PRIu64 ") = %" PRIu64 "(0x%" PRIx64 ")\n",
335-
get_name(), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, faddr, pma_index,
336-
static_cast<uint64_t>(*pval), static_cast<uint64_t>(*pval));
346+
dsa_printf("%s::read_memory_word<%s,%s>(%s{0x%" PRIx64 "}, %" PRIu64 ") = %" PRIu64 "(0x%" PRIx64 ")\n",
347+
get_name(), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, static_cast<uint64_t>(faddr),
348+
pma_index, static_cast<uint64_t>(*pval), static_cast<uint64_t>(*pval));
337349
}
338350

339351
/// \brief Writes a word to memory.
@@ -348,9 +360,9 @@ class i_state_access { // CRTP
348360
static_assert(std::is_integral_v<T> && sizeof(T) <= sizeof(uint64_t), "unsupported type");
349361
derived().template do_write_memory_word<T, A>(faddr, pma_index, val);
350362
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t> ? "phys_addr" : "fast_addr";
351-
DSA_PRINTF("%s::write_memory_word<%s,%s>(%s{0x%" PRIx64 "}, %" PRIu64 ", %" PRIu64 "(0x%" PRIx64 "))\n",
352-
get_name(), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, faddr, pma_index,
353-
static_cast<uint64_t>(val), static_cast<uint64_t>(val));
363+
dsa_printf("%s::write_memory_word<%s,%s>(%s{0x%" PRIx64 "}, %" PRIu64 ", %" PRIu64 "(0x%" PRIx64 "))\n",
364+
get_name(), poor_type_name_v<T>, poor_type_name_v<A>, fast_addr_name, static_cast<uint64_t>(faddr),
365+
pma_index, static_cast<uint64_t>(val), static_cast<uint64_t>(val));
354366
}
355367

356368
/// \brief Reads TLB's vaddr_page
@@ -360,7 +372,7 @@ class i_state_access { // CRTP
360372
template <TLB_set_index SET>
361373
uint64_t read_tlb_vaddr_page(uint64_t slot_index) const {
362374
const auto val = derived().template do_read_tlb_vaddr_page<SET>(slot_index);
363-
DSA_PRINTF("%s::read_tlb_vaddr_page<%" PRIu64 ">(%" PRIu64 ") = 0x%" PRIx64 "\n", get_name(), SET, slot_index,
375+
dsa_printf("%s::read_tlb_vaddr_page<%" PRIu64 ">(%" PRIu64 ") = 0x%" PRIx64 "\n", get_name(), SET, slot_index,
364376
val);
365377
return val;
366378
}
@@ -373,8 +385,8 @@ class i_state_access { // CRTP
373385
fast_addr read_tlb_vp_offset(uint64_t slot_index) const {
374386
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t> ? "phys_addr" : "fast_addr";
375387
const auto val = derived().template do_read_tlb_vp_offset<SET>(slot_index);
376-
DSA_PRINTF("%s::read_tlb_vp_offset<%" PRIu64 ">(%" PRIu64 ") = %s{0x%" PRIx64 "}\n", get_name(), SET,
377-
slot_index, fast_addr_name, val);
388+
dsa_printf("%s::read_tlb_vp_offset<%" PRIu64 ">(%" PRIu64 ") = %s{0x%" PRIx64 "}\n", get_name(), SET,
389+
slot_index, fast_addr_name, static_cast<uint64_t>(val));
378390
return val;
379391
}
380392

@@ -385,7 +397,7 @@ class i_state_access { // CRTP
385397
template <TLB_set_index SET>
386398
uint64_t read_tlb_pma_index(uint64_t slot_index) const {
387399
const auto val = derived().template do_read_tlb_pma_index<SET>(slot_index);
388-
DSA_PRINTF("%s::read_tlb_pma_index<%" PRIu64 ">(%" PRIu64 ") = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), SET,
400+
dsa_printf("%s::read_tlb_pma_index<%" PRIu64 ">(%" PRIu64 ") = %" PRIu64 "(0x%" PRIx64 ")\n", get_name(), SET,
389401
slot_index, val, val);
390402
return val;
391403
}
@@ -402,8 +414,8 @@ class i_state_access { // CRTP
402414
void write_tlb(uint64_t slot_index, uint64_t vaddr_page, fast_addr vp_offset, uint64_t pma_index) const {
403415
derived().template do_write_tlb<SET>(slot_index, vaddr_page, vp_offset, pma_index);
404416
[[maybe_unused]] const auto fast_addr_name = std::is_same_v<fast_addr, uint64_t> ? "phys_addr" : "fast_addr";
405-
DSA_PRINTF("%s::write_tlb<%" PRIu64 ">(%" PRIu64 ", 0x%" PRIx64 ", %s{0x%" PRIx64 "}, %" PRIu64 ")\n",
406-
get_name(), SET, slot_index, vaddr_page, fast_addr_name, vp_offset, pma_index);
417+
dsa_printf("%s::write_tlb<%" PRIu64 ">(%" PRIu64 ", 0x%" PRIx64 ", %s{0x%" PRIx64 "}, %" PRIu64 ")\n",
418+
get_name(), SET, slot_index, vaddr_page, fast_addr_name, static_cast<uint64_t>(vp_offset), pma_index);
407419
}
408420

409421
/// \brief Marks a page as dirty

0 commit comments

Comments
 (0)