Skip to content

Commit 6a9d50b

Browse files
committed
c: Formatting fixes for the generated output
1 parent 122e561 commit 6a9d50b

4 files changed

Lines changed: 63 additions & 72 deletions

File tree

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ AllowShortLoopsOnASingleLine: false
1616
AllowShortEnumsOnASingleLine: false
1717
IncludeBlocks: Preserve
1818
SortIncludes: Never
19-
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
19+
AlignConsecutiveMacros: AcrossComments
2020
AlignOperands: Align
2121
AlignAfterOpenBracket: Align
2222
AlignEscapedNewlines: Left

compost_rpc/compost_rpc.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,7 @@ class CodeGenerator(ABC):
12301230
_GENERATION_NOTE: str = """
12311231
/******************************************************************************/
12321232
/* G E N E R A T E D P R O T O C O L */
1233-
/******************************************************************************/
1234-
"""
1233+
/******************************************************************************/"""
12351234

12361235
def __init__(self, protocol: type[Protocol]):
12371236
"""Constructor for the interface"""
@@ -1489,7 +1488,7 @@ def _define_type_helper(self, t: type, helper: str) -> None:
14891488
is_store = helper == "store"
14901489
fn = self._serdes_fn(t, helper)
14911490
# load
1492-
fn_proto = f"{self._type(t)} {fn}(const uint8_t** src)"
1491+
fn_proto = f"{self._type(t)} {fn}(const uint8_t ** src)"
14931492
fn_call = self._load_call
14941493
iter_ptr = "src"
14951494
if is_store: #store
@@ -1634,8 +1633,8 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
16341633
params = ", " + ", ".join(params) if params else ""
16351634
protocol_header += f"""
16361635
/**
1637-
* {notif.__doc__}
1638-
*/
1636+
* {notif.__doc__}
1637+
*/
16391638
int16_t {notif.name}_store(uint8_t *tx_buf, size_t tx_buf_size{params});
16401639
"""
16411640
protocol_source += f"""/**
@@ -1647,7 +1646,7 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
16471646
.txn = 0,
16481647
.payload_buf = tx_buf + PAYLOAD_OFFSET
16491648
}};
1650-
uint8_t* dest = tx.payload_buf;
1649+
uint8_t *dest = tx.payload_buf;
16511650
"""
16521651
protocol_source.indent_inc()
16531652
for param_name, param_type in notif.get_param_items():
@@ -1674,21 +1673,21 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
16741673
invoke_prototype = _String()
16751674
invoke_prototype += f"""
16761675
/**
1677-
* {rpc.__doc__}
1678-
*/
1676+
* {rpc.__doc__}
1677+
*/
16791678
{self._handler_signature(rpc)}
16801679
"""
16811680
invoke_fn = _String()
16821681
invoke_fn += f"""
16831682
/**
1684-
* {doc_prefix} function for {rpc.name} function
1685-
*/
1683+
* {doc_prefix} function for {rpc.name} function
1684+
*/
16861685
void invoke_{rpc.name}({", ".join(invoke_params)})
16871686
{{
16881687
"""
16891688
invoke_fn.indent_inc()
16901689
if parameters_items:
1691-
invoke_fn.add("const uint8_t* src = rx.payload_buf;")
1690+
invoke_fn.add("const uint8_t *src = rx.payload_buf;")
16921691
for name, t in parameters_items:
16931692
invoke_fn.add(self._load_call(t.annotation, dest = f"l_{name}", src = "&src"))
16941693
if get_origin(sig.return_annotation) is list or _issubclass(sig.return_annotation, (bytes, str)):
@@ -1702,7 +1701,7 @@ def generate(self, endpoint: Endpoint = Endpoint.REMOTE) -> list[tuple[Path, str
17021701
))
17031702
invoke_fn.add(f"{self._handler_signature(rpc, as_caller=True)}")
17041703
if sig.return_annotation is not Signature.empty:
1705-
invoke_fn.add('uint8_t* dest = tx->payload_buf;')
1704+
invoke_fn.add('uint8_t *dest = tx->payload_buf;')
17061705
invoke_fn.add(self._store_call(sig.return_annotation, "ret"))
17071706
if sig.return_annotation is Signature.empty:
17081707
invoke_fn.add("tx->len = 0;")

compost_rpc/lib/c/header_template.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
1515
${version_info}
1616
17-
#define COMPOST_ERR -1 ///< Generic error
18-
#define COMPOST_EINVAL -22 ///< Invalid argument
19-
#define COMPOST_EMSGSIZE -90 ///< Message too long
20-
#define COMPOST_EFLAGS -91 ///< Reserved flags set
21-
#define COMPOST_ETXN -92 ///< Unexcepted transaction value
17+
#define COMPOST_ERR -1 ///< Generic error
18+
#define COMPOST_EINVAL -22 ///< Invalid argument
19+
#define COMPOST_EMSGSIZE -90 ///< Message too long
20+
#define COMPOST_EFLAGS -91 ///< Reserved flags set
21+
#define COMPOST_ETXN -92 ///< Unexcepted transaction value
2222
2323
struct CompostMsg {
2424
uint16_t len;
@@ -32,11 +32,9 @@
3232
// forward declare compost_alloc to allow usage in compost_slice_xx_new function
3333
struct CompostAlloc;
3434
35-
3635
/******************************************************************************/
3736
/* S L I C E S */
3837
/******************************************************************************/
39-
4038
'''
4139

4240
slice_template = '''
@@ -300,10 +298,8 @@
300298
*
301299
* @param assert_func Function pointer to the custom assert function or NULL to reset to default
302300
*/
303-
void compost_set_assert_func(void(*assert_func)(uint32_t));
304-
301+
void compost_set_assert_func(void (*assert_func)(uint32_t));
305302
${protocol}
306-
307303
#endif /* COMPOST_H */
308304
'''
309305

compost_rpc/lib/c/source_template.py

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@ class SourcePart:
1818
1919
#ifndef COMPOST_ALLOW_SMALL_INT
2020
#if INT_MAX < 2147483647
21-
#error "int is smaller than 32b - Compost expects Enums to have 32b. You can allow smaller int by defining macro COMPOST_ALLOW_SMALL_INT"
21+
#error \\
22+
"int is smaller than 32b - Compost expects Enums to have 32b. You can allow smaller int by defining macro COMPOST_ALLOW_SMALL_INT"
2223
#endif
2324
#endif
2425
2526
#ifdef COMPOST_DEBUG
26-
#define COMPOST_ASSERT(expr) \\
27-
do { \\
28-
if (!(expr)) { \\
29-
if (compost_assert_func != NULL) { \\
30-
compost_assert_func(__LINE__); \\
31-
} \\
32-
while(1); \\
33-
} \\
27+
#define COMPOST_ASSERT(expr) \\
28+
do { \\
29+
if (!(expr)) { \\
30+
if (compost_assert_func != NULL) { \\
31+
compost_assert_func(__LINE__); \\
32+
} \\
33+
while (1) \\
34+
; \\
35+
} \\
3436
} while (0)
3537
#else
36-
#define COMPOST_ASSERT(expr) do { (void)sizeof(expr); } while (0)
38+
#define COMPOST_ASSERT(expr) \\
39+
do { \\
40+
(void)sizeof(expr); \\
41+
} while (0)
3742
#endif
3843
3944
#define LEN_OFFSET 0
@@ -42,11 +47,11 @@ class SourcePart:
4247
#define RPC_ID_LO_OFFSET 3
4348
#define PAYLOAD_OFFSET 4
4449
45-
#define RPC_ID_HI_MASK 0x0F
46-
#define FLAGS_MASK 0xF0
47-
#define RESP_MASK 0x10
50+
#define RPC_ID_HI_MASK 0x0F
51+
#define FLAGS_MASK 0xF0
52+
#define RESP_MASK 0x10
4853
49-
static void(*compost_assert_func)(uint32_t line) = NULL;
54+
static void (*compost_assert_func)(uint32_t line) = NULL;
5055
5156
void compost_invoke_switch(struct CompostMsg *tx, const struct CompostMsg rx);
5257
@@ -110,7 +115,7 @@ class SourcePart:
110115
*/
111116
static inline int8_t compost_i8_load(const uint8_t **src)
112117
{
113-
return (int8_t) compost_u8_load(src);
118+
return (int8_t)compost_u8_load(src);
114119
}
115120
116121
/**
@@ -126,9 +131,7 @@ class SourcePart:
126131
*/
127132
static inline uint16_t compost_u16_load(const uint8_t **src)
128133
{
129-
uint16_t val =
130-
((uint16_t)(*src)[0] << 8) |
131-
((uint16_t)(*src)[1]);
134+
uint16_t val = ((uint16_t)(*src)[0] << 8) | ((uint16_t)(*src)[1]);
132135
*src += sizeof(uint16_t);
133136
return val;
134137
}
@@ -164,11 +167,8 @@ class SourcePart:
164167
*/
165168
static inline uint32_t compost_u32_load(const uint8_t **src)
166169
{
167-
uint32_t val =
168-
((uint32_t)(*src)[0] << 24) |
169-
((uint32_t)(*src)[1] << 16) |
170-
((uint32_t)(*src)[2] << 8) |
171-
((uint32_t)(*src)[3]);
170+
uint32_t val = ((uint32_t)(*src)[0] << 24) | ((uint32_t)(*src)[1] << 16) |
171+
((uint32_t)(*src)[2] << 8) | ((uint32_t)(*src)[3]);
172172
*src += sizeof(uint32_t);
173173
return val;
174174
}
@@ -206,15 +206,10 @@ class SourcePart:
206206
*/
207207
static inline uint64_t compost_u64_load(const uint8_t **src)
208208
{
209-
uint64_t val =
210-
((uint64_t)(*src)[0] << 56) |
211-
((uint64_t)(*src)[1] << 48) |
212-
((uint64_t)(*src)[2] << 40) |
213-
((uint64_t)(*src)[3] << 32) |
214-
((uint64_t)(*src)[4] << 24) |
215-
((uint64_t)(*src)[5] << 16) |
216-
((uint64_t)(*src)[6] << 8) |
217-
((uint64_t)(*src)[7]);
209+
uint64_t val = ((uint64_t)(*src)[0] << 56) | ((uint64_t)(*src)[1] << 48) |
210+
((uint64_t)(*src)[2] << 40) | ((uint64_t)(*src)[3] << 32) |
211+
((uint64_t)(*src)[4] << 24) | ((uint64_t)(*src)[5] << 16) |
212+
((uint64_t)(*src)[6] << 8) | ((uint64_t)(*src)[7]);
218213
*src += sizeof(uint64_t);
219214
return val;
220215
}
@@ -240,7 +235,7 @@ class SourcePart:
240235
*/
241236
static inline int64_t compost_i64_load(const uint8_t **src)
242237
{
243-
return (int64_t) compost_u64_load(src);
238+
return (int64_t)compost_u64_load(src);
244239
}
245240
246241
/**
@@ -296,7 +291,7 @@ class SourcePart:
296291
/**
297292
* Loads bit precise integer from the backing value
298293
*/
299-
uint32_t compost_bituint_load(const uint8_t* src, uint32_t offset_bits, uint32_t size_bits)
294+
uint32_t compost_bituint_load(const uint8_t *src, uint32_t offset_bits, uint32_t size_bits)
300295
{
301296
COMPOST_ASSERT(src != NULL);
302297
uint64_t val = 0;
@@ -323,7 +318,7 @@ class SourcePart:
323318
/**
324319
* Stores bit precise integer to the backing value
325320
*/
326-
void compost_bituint_store(uint8_t* dest, uint32_t value, uint32_t offset_bits, uint32_t size_bits)
321+
void compost_bituint_store(uint8_t *dest, uint32_t value, uint32_t offset_bits, uint32_t size_bits)
327322
{
328323
COMPOST_ASSERT(dest != NULL);
329324
uint32_t byte_index = offset_bits / 8;
@@ -350,7 +345,7 @@ class SourcePart:
350345
static inline struct CompostSlice${struct_suffix} compost_slice_${fn_suffix}_load(const uint8_t **src)
351346
{
352347
uint16_t len = compost_u16_load(src);
353-
struct CompostSlice${struct_suffix} ret = (struct CompostSlice${struct_suffix}){ .ptr = (uint8_t*)(*src), .len = len / sizeof(${type}) };
348+
struct CompostSlice${struct_suffix} ret = (struct CompostSlice${struct_suffix}){.ptr = (uint8_t *)(*src), .len = len / sizeof(${type})};
354349
*src += len;
355350
return ret;
356351
}
@@ -474,7 +469,7 @@ class SourcePart:
474469
}
475470
}
476471
477-
void compost_set_assert_func(void(*assert_func)(uint32_t line))
472+
void compost_set_assert_func(void (*assert_func)(uint32_t line))
478473
{
479474
compost_assert_func = assert_func;
480475
}
@@ -484,7 +479,7 @@ class SourcePart:
484479
source_parts.append(SourcePart(True,'''
485480
struct CompostSlice${struct_suffix} compost_slice_${fn_suffix}_init(void *ptr, uint16_t len)
486481
{
487-
return (struct CompostSlice${struct_suffix}){ .ptr = ptr, .len = len };
482+
return (struct CompostSlice${struct_suffix}){.ptr = ptr, .len = len};
488483
}
489484
490485
struct CompostSlice${struct_suffix} compost_slice_${fn_suffix}_new(struct CompostAlloc *alloc, uint16_t len)
@@ -493,22 +488,22 @@ class SourcePart:
493488
if (ptr == NULL) {
494489
len = 0;
495490
}
496-
return (struct CompostSlice${struct_suffix}){ .ptr = ptr, .len = len};
491+
return (struct CompostSlice${struct_suffix}){.ptr = ptr, .len = len};
497492
}
498493
499494
${type} compost_slice_${fn_suffix}_get(struct CompostSlice${struct_suffix} target, uint16_t idx)
500495
{
501496
COMPOST_ASSERT(idx < target.len);
502497
COMPOST_ASSERT(target.ptr != NULL);
503-
const uint8_t* ptr = target.ptr + (sizeof(${type}) * idx);
498+
const uint8_t *ptr = target.ptr + (sizeof(${type}) * idx);
504499
return compost_${fn_suffix}_load(&ptr);
505500
}
506501
507502
void compost_slice_${fn_suffix}_set(struct CompostSlice${struct_suffix} target, uint16_t idx, ${type} value)
508503
{
509504
COMPOST_ASSERT(idx < target.len);
510505
COMPOST_ASSERT(target.ptr != NULL);
511-
uint8_t* ptr = target.ptr + (sizeof(${type}) * idx);
506+
uint8_t *ptr = target.ptr + (sizeof(${type}) * idx);
512507
compost_${fn_suffix}_store(&ptr, value);
513508
}
514509
@@ -517,7 +512,7 @@ class SourcePart:
517512
COMPOST_ASSERT(dest.ptr != NULL);
518513
COMPOST_ASSERT(src != NULL);
519514
int len_limit = len <= dest.len ? len : dest.len;
520-
if ((uint8_t*)src != dest.ptr) {
515+
if ((uint8_t *)src != dest.ptr) {
521516
for (int i = 0; i < len_limit; i++) {
522517
compost_slice_${fn_suffix}_set(dest, i, src[i]);
523518
}
@@ -537,7 +532,7 @@ class SourcePart:
537532
COMPOST_ASSERT(src.ptr != NULL);
538533
COMPOST_ASSERT(dest != NULL);
539534
int len_limit = len <= src.len ? len : src.len;
540-
if (src.ptr != (uint8_t*)dest) {
535+
if (src.ptr != (uint8_t *)dest) {
541536
for (int i = 0; i < len_limit; i++) {
542537
dest[i] = compost_slice_${fn_suffix}_get(src, i);
543538
}
@@ -563,10 +558,10 @@ class SourcePart:
563558
struct CompostAlloc compost_alloc_init(uint8_t *buffer, uint16_t len)
564559
{
565560
return (struct CompostAlloc){.suffixes = NULL,
566-
.suffixes_len = 0,
567-
.alloc_ctr = 0,
568-
.ptr = buffer,
569-
.buffer = compost_slice_u8_init(buffer, len)};
561+
.suffixes_len = 0,
562+
.alloc_ctr = 0,
563+
.ptr = buffer,
564+
.buffer = compost_slice_u8_init(buffer, len)};
570565
}
571566
572567
void compost_alloc_set_suffixes(struct CompostAlloc *alloc, uint16_t *suffixes, uint16_t len)
@@ -590,7 +585,8 @@ class SourcePart:
590585
{
591586
COMPOST_ASSERT(alloc != NULL);
592587
int capacity = alloc->buffer.len - (alloc->ptr - alloc->buffer.ptr);
593-
if ((int)len > capacity || (alloc->suffixes != NULL && alloc->alloc_ctr >= alloc->suffixes_len)) {
588+
if ((int)len > capacity ||
589+
(alloc->suffixes != NULL && alloc->alloc_ctr >= alloc->suffixes_len)) {
594590
return NULL;
595591
}
596592
@@ -624,7 +620,7 @@ class SourcePart:
624620
compost_slice_copy_from(dest, (void *)src, len);
625621
if (len < dest.len) {
626622
for (int i = len; i < dest.len; i++) {
627-
compost_slice_set(dest, i, '\\0');
623+
compost_slice_set(dest, i, '\0');
628624
}
629625
}
630626
}

0 commit comments

Comments
 (0)