Skip to content

Commit d839a6c

Browse files
authored
Merge pull request #448 from tropicsquare/ETR01SDK-523-Refactor-clang_format-settings
ETR01SDK-523: Refactor clang format settings
2 parents f818c7e + cbd6866 commit d839a6c

File tree

8 files changed

+71
-22
lines changed

8 files changed

+71
-22
lines changed

.clang-format

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
1-
# Tested with Ubuntu clang-format version 14.0.0-1ubuntu1.1
2-
3-
BreakBeforeBinaryOperators: All
4-
5-
ColumnLimit: 120
6-
1+
# Language conformance: C11
2+
# Using Cpp as a language is ok here, since there is no separate C formatter.
3+
# It is common to use Cpp for C code formatting in clang-format.
4+
Language: Cpp
5+
Standard: Cpp11
76
BasedOnStyle: Google
87

8+
# --- BRACE STYLE ---
9+
# "Functions... opening brace on new line"
10+
# "if, for, while... opening brace on the same line"
911
BreakBeforeBraces: Stroustrup
1012

13+
# --- INDENTATION & WIDTH ---
14+
ColumnLimit: 120
15+
16+
# "You shall indent the code with 4 spaces"
1117
IndentWidth: 4
18+
TabWidth: 4
19+
UseTab: Never
20+
ContinuationIndentWidth: 4
1221

22+
# --- POINTERS ---
23+
# Google defaults to Left, but guidelines show Right (void *p)
1324
PointerAlignment: Right
25+
DerivePointerAlignment: false
26+
27+
# --- SPECIFIC TROPIC SQUARE RULES ---
28+
# "You shall place braces around the single statement"
29+
# Valid since Clang-Format 15
30+
InsertBraces: true
31+
32+
# "You shall put spaces after... if, switch..."
33+
SpaceBeforeParens: ControlStatements
34+
# "You shall not add spaces around (inside) parenthesized expressions"
35+
SpacesInParentheses: false
36+
37+
# "Space on each side of most binary and ternary operators"
38+
# Note: "SpacesInBinaryOperators" is implicit in Google style, no key needed.
39+
SpaceBeforeAssignmentOperators: true
1440

15-
# # Insert New line at EOF if missing
16-
#InsertNewlineAtEOF: true
17-
# # Set EOL to LF unconditionally
18-
#LineEnding: LF
41+
# Misc Cleanup
42+
SortIncludes: true
43+
IndentCaseLabels: true
44+
BreakBeforeBinaryOperators: All

.github/workflows/clang_format_check.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ on:
66
- develop
77
paths:
88
- '**.c'
9+
- '**.cpp'
910
- '**.h'
11+
- '**.hpp'
1012
pull_request:
1113
paths:
1214
- '**.c'
15+
- '**.cpp'
1316
- '**.h'
17+
- '**.hpp'
1418

1519
jobs:
1620
check-format:
@@ -31,7 +35,7 @@ jobs:
3135
echo "Formatting issue in $f"
3236
exit_code=1
3337
fi
34-
done < <(find . -type f \( -name '*.c' -o -name '*.h' \) ! -path './vendor/*' ! -path './vendor/**' ! -path '*Vendor/*' ! -path '*_deps/*' ! -path './.git/*' -print0)
38+
done < <(find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) ! -path './vendor/*' ! -path './vendor/**' ! -path '*Vendor/*' ! -path '*_deps/*' ! -path './.git/*' -print0)
3539
3640
if [ $exit_code -ne 0 ]; then
3741
echo "Clang-format check failed."

examples/model/mac_and_destroy/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ static psa_status_t hmac_sha256(const uint8_t *key, const size_t key_len, const
121121
PSA_HASH_LENGTH(PSA_ALG_SHA_256), &output_len);
122122

123123
cleanup:
124-
if (key_id != 0) psa_destroy_key(key_id);
124+
if (key_id != 0) {
125+
psa_destroy_key(key_id);
126+
}
125127
psa_reset_key_attributes(&attributes);
126128
return status;
127129
}

src/libtropic.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,9 @@ static const char *lt_ret_strs[] = {"LT_OK",
14871487

14881488
const char *lt_ret_verbose(lt_ret_t ret)
14891489
{
1490-
if (ret < LT_RET_T_LAST_VALUE) return lt_ret_strs[ret];
1490+
if (ret < LT_RET_T_LAST_VALUE) {
1491+
return lt_ret_strs[ret];
1492+
}
14911493

14921494
return "FATAL ERROR, unknown return value";
14931495
}

src/lt_asn1_der.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ static lt_ret_t parse_object(struct parse_ctx_t *ctx)
145145
LT_ASN1_DER_GET_NEXT_BYTE(ctx, &b);
146146

147147
rv = parse_length(ctx, &len);
148-
if (rv != LT_OK) return rv;
148+
if (rv != LT_OK) {
149+
return rv;
150+
}
149151

150152
uint16_t start = ctx->past;
151153

@@ -160,7 +162,9 @@ static lt_ret_t parse_object(struct parse_ctx_t *ctx)
160162
case LT_ASN1DER_SEQUENCE:
161163
while (ctx->past < start + len - 1) {
162164
rv = parse_object(ctx);
163-
if (rv != LT_OK) return rv;
165+
if (rv != LT_OK) {
166+
return rv;
167+
}
164168
}
165169

166170
if (start + len != ctx->past) {
@@ -279,10 +283,14 @@ lt_ret_t asn1der_find_object(const uint8_t *stream, uint16_t len, int32_t obj_id
279283

280284
while (ctx.past < ctx.len - 1) {
281285
lt_ret_t rv = parse_object(&ctx);
282-
if (rv != LT_OK) return rv;
286+
if (rv != LT_OK) {
287+
return rv;
288+
}
283289
};
284290

285-
if (!ctx.found) return LT_CERT_ITEM_NOT_FOUND;
291+
if (!ctx.found) {
292+
return LT_CERT_ITEM_NOT_FOUND;
293+
}
286294

287295
return LT_OK;
288296
}

tests/functional/src/lt_test_rev_get_info_req_app.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ void lt_test_rev_get_info_req_app(lt_handle_t *h)
4646
LT_TEST_ASSERT(1, (store.cert_len[i] != 0));
4747
LT_LOG_INFO("Size in bytes: %" PRIu16, store.cert_len[i]);
4848

49-
for (int j = 0; j < store.cert_len[i]; j += 16)
49+
for (int j = 0; j < store.cert_len[i]; j += 16) {
5050
LT_LOG_INFO("%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8
5151
"%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8,
5252
cert[j], cert[j + 1], cert[j + 2], cert[j + 3], cert[j + 4], cert[j + 5], cert[j + 6],
5353
cert[j + 7], cert[j + 8], cert[j + 9], cert[j + 10], cert[j + 11], cert[j + 12], cert[j + 13],
5454
cert[j + 14], cert[j + 15]);
55+
}
5556
LT_LOG_INFO();
5657
}
5758
LT_LOG_LINE();

tests/functional/src/lt_test_rev_get_info_req_bootloader.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ void lt_test_rev_get_info_req_bootloader(lt_handle_t *h)
180180
LT_TEST_ASSERT(1, (store.cert_len[i] != 0));
181181
LT_LOG_INFO("Size in bytes: %" PRIu16, store.cert_len[i]);
182182

183-
for (int j = 0; j < store.cert_len[i]; j += 16)
183+
for (int j = 0; j < store.cert_len[i]; j += 16) {
184184
LT_LOG_INFO("%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8
185185
"%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8 "%02" PRIx8,
186186
cert[j], cert[j + 1], cert[j + 2], cert[j + 3], cert[j + 4], cert[j + 5], cert[j + 6],
187187
cert[j + 7], cert[j + 8], cert[j + 9], cert[j + 10], cert[j + 11], cert[j + 12], cert[j + 13],
188188
cert[j + 14], cert[j + 15]);
189+
}
189190
LT_LOG_INFO();
190191
}
191192
LT_LOG_LINE();

tests/functional/src/lt_test_rev_mac_and_destroy.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ static int pin_check(lt_handle_t *h, uint8_t *pin, uint16_t pin_len, lt_mac_and_
3838
LT_TEST_ASSERT(LT_OK, lt_hmac_sha256(w, sizeof(w), pin, pin_len, k_i));
3939

4040
LT_LOG_INFO("Decrypting (XOR) c_i using k_i...");
41-
for (uint8_t j = 0; j < TR01_MAC_AND_DESTROY_DATA_SIZE; j++) s[j] = ciphertexts[slot][j] ^ k_i[j];
41+
for (uint8_t j = 0; j < TR01_MAC_AND_DESTROY_DATA_SIZE; j++) {
42+
s[j] = ciphertexts[slot][j] ^ k_i[j];
43+
}
4244

4345
LT_LOG_INFO("Computing t' = KDF(s, \"0\")...");
4446
LT_TEST_ASSERT(LT_OK, lt_hmac_sha256(s, TR01_MAC_AND_DESTROY_DATA_SIZE, (uint8_t *)"0", 1, t_));
4547

4648
LT_LOG_INFO("Checking if t' != t...");
47-
for (uint8_t i = 0; i < sizeof(t_); i++)
48-
if (t_[i] != t[i]) return 1;
49+
for (uint8_t i = 0; i < sizeof(t_); i++) {
50+
if (t_[i] != t[i]) {
51+
return 1;
52+
}
53+
}
4954

5055
return 0;
5156
}

0 commit comments

Comments
 (0)