Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions board/body/bldc/bldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ volatile uint16_t batt_percentage = 0;
volatile int rpm_left = 0;
volatile int rpm_right = 0;

volatile bool enable_motors = 0; // initially motors are disabled for safety
static bool enableFin = 0;
volatile bool enable_motors = false; // initially motors are disabled for safety
static bool enableFin = false;

static const uint16_t pwm_res = ( (uint32_t)CORE_FREQ * 1000000U / 2U ) / PWM_FREQ;

Expand Down Expand Up @@ -233,9 +233,9 @@ void bldc_step(void) {

// Safety: Don't enable if offsets are bogus (e.g. ADC failed)
if (offsetrrA == 0 || offsetrrC == 0 || !enable_motors) {
enableFin = 0;
enableFin = false;
} else {
enableFin = 1;
enableFin = true;
}

// Read Hall Sensors
Expand Down Expand Up @@ -308,4 +308,4 @@ void bldc_step(void) {
RIGHT_TIM->CCR3 = (uint16_t)CLAMP((wr + pwm_res / 2), PWM_MARGIN, pwm_res - PWM_MARGIN);
}

#endif
#endif
4 changes: 2 additions & 2 deletions board/drivers/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ can_ring *can_queues[PANDA_CAN_CNT] = {&can_tx1_q, &can_tx2_q, &can_tx3_q};

// ********************* interrupt safe queue *********************
bool can_pop(can_ring *q, CANPacket_t *elem) {
bool ret = 0;
bool ret = false;

ENTER_CRITICAL();
if (q->w_ptr != q->r_ptr) {
Expand All @@ -48,7 +48,7 @@ bool can_pop(can_ring *q, CANPacket_t *elem) {
} else {
q->r_ptr += 1U;
}
ret = 1;
ret = true;
}
EXIT_CRITICAL();

Expand Down
43 changes: 43 additions & 0 deletions tests/misra/bool_literals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
import re

import cppcheck


INTEGER_BOOL_LITERAL = re.compile(r"^[01]([uUlL]*)$")


def _is_external_token(token):
if token is None or token.file is None:
return False
filename = token.file.replace("\\", "/")
return "/.venv/" in filename or "/site-packages/" in filename


def _is_bool_token(token):
if token is None:
return False
if token.valueType is not None and token.valueType.type == "bool":
return True
return token.variable is not None and token.variable.typeEndToken is not None and token.variable.typeEndToken.str == "bool"


def _is_integer_bool_literal(token):
return (
token is not None and
token.isNumber and
token.macroName not in ("true", "false") and
INTEGER_BOOL_LITERAL.match(token.str) is not None
)


@cppcheck.checker
def integer_literal_bool(cfg, data):
for token in cfg.tokenlist:
if token.str != "=":
continue

lhs = token.astOperand1
rhs = token.astOperand2
if not _is_external_token(rhs) and _is_bool_token(lhs) and _is_integer_bool_literal(rhs):
cppcheck.reportError(rhs, "style", "use true/false for bool values", "integerLiteralBool")
2 changes: 1 addition & 1 deletion tests/misra/checkers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Cppcheck checkers list from test_misra.sh:


TEST variant options:
--enable=all --disable=unusedFunction --addon=misra -DSTM32H7 -DSTM32H725xx -I /board/stm32h7/inc/ /board/main.c
--enable=all --disable=unusedFunction --addon=misra --addon=tests/misra/bool_literals.py -DSTM32H7 -DSTM32H725xx -I /board/stm32h7/inc/ /board/main.c


Critical errors
Expand Down
2 changes: 1 addition & 1 deletion tests/misra/test_misra.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cppcheck() {
fi
}

PANDA_OPTS="--enable=all --disable=unusedFunction --addon=misra"
PANDA_OPTS="--enable=all --disable=unusedFunction --addon=misra --addon=tests/misra/bool_literals.py"

printf "\n${GREEN}** PANDA H7 CODE **${NC}\n"
cppcheck $PANDA_OPTS -DSTM32H7 -DSTM32H725xx -I $PANDA_DIR/board/stm32h7/inc/ $PANDA_DIR/board/main.c
Expand Down
5 changes: 3 additions & 2 deletions tests/misra/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

mutations = [
(None, None, False), # no mods, should pass
("board/drivers/can_common.h", "$a bool tx = 0;", True),
("board/stm32h7/llfdcan.h", "s/return ret;/if (true) { return ret; } else { return false; }/g", True),
]

Expand Down Expand Up @@ -62,8 +63,8 @@
for p in patterns:
mutations.append((rng.choice(files), p, True))

# sample to keep CI fast, but always include the no-mutation case
mutations = [mutations[0]] + rng.sample(mutations[1:], min(2, len(mutations) - 1))
# sample to keep CI fast, but always include the no-mutation and local addon cases
mutations = mutations[:2] + rng.sample(mutations[2:], min(1, len(mutations) - 2))

@pytest.mark.parametrize("fn, patch, should_fail", mutations)
def test_misra_mutation(fn, patch, should_fail):
Expand Down
Loading