Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable unusedFunction checks #1970

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ jobs:
run: eval "$BUILD"
- name: Build FW
run: ${{ env.RUN }} "scons -j$(nproc)"
- name: Run MISRA C:2012 analysis
- name: Run MISRA C:2012 analysis - ALLOW_DEBUG
timeout-minutes: 1
run: ${{ env.RUN }} "cd tests/misra && ./test_misra.sh"
- name: Run MISRA C:2012 analysis - RELEASE
timeout-minutes: 2
run: ${{ env.RUN }} "cd tests/misra && CERT=./certs/debug RELEASE=1 ./test_misra.sh"
- name: MISRA mutation tests
timeout-minutes: 5
timeout-minutes: 7
run: ${{ env.RUN }} "cd tests/misra && pytest -n8 test_mutation.py"

static_analysis:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ examples/output.csv
nosetests.xml
.mypy_cache/
.sconsign.dblite
compile_commands*.json

# CTU info files generated by Cppcheck
*.*.ctu-info
Expand Down
6 changes: 2 additions & 4 deletions SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def build_project(project_name, project, extra_flags):

# Bootstub
crypto_obj = [
env.Object(f"rsa-{project_name}", f"{panda_root}/crypto/rsa.c"),
env.Object(f"sha-{project_name}", f"{panda_root}/crypto/sha.c")
env.Object(f"bootstub-rsa-{project_name}", f"{panda_root}/crypto/rsa.c"),
env.Object(f"bootstub-sha-{project_name}", f"{panda_root}/crypto/sha.c")
]
bootstub_obj = env.Object(f"bootstub-{project_name}", File(project.get("BOOTSTUB", f"{panda_root}/board/bootstub.c")))
bootstub_elf = env.Program(f"obj/bootstub.{project_name}.elf",
Expand Down Expand Up @@ -136,7 +136,6 @@ base_project_f4 = {
"-mhard-float",
"-DSTM32F4",
"-DSTM32F413xx",
"-Iboard/stm32f4/inc",
"-mfpu=fpv4-sp-d16",
"-fsingle-precision-constant",
"-Os",
Expand All @@ -154,7 +153,6 @@ base_project_h7 = {
"-mhard-float",
"-DSTM32H7",
"-DSTM32H725xx",
"-Iboard/stm32h7/inc",
"-mfpu=fpv5-d16",
"-fsingle-precision-constant",
"-Os",
Expand Down
6 changes: 6 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ env = Environment(
)

if GetOption('compile_db'):
# whole project compilation database
env.CompilationDatabase("compile_commands.json")

# Panda compilation database
env_p = env.Clone()
env_p["COMPILATIONDB_PATH_FILTER"] = '*board/[!jungle/][!bootstub]*'
env_p.CompilationDatabase("compile_commands_panda.json")

# panda fw & test files
SConscript('SConscript')
2 changes: 1 addition & 1 deletion board/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ for project_name, project in build_projects.items():
flags = [
"-DPANDA",
]
if ("ENABLE_SPI" in os.environ or "h7" in project_name):
if ("ENABLE_SPI" in os.environ):
flags.append('-DENABLE_SPI')

build_project(project_name, project, flags)
2 changes: 2 additions & 0 deletions board/drivers/can_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ void can_set_orientation(bool flipped) {
bus_config[2].can_num_lookup = flipped ? 0U : 2U;
}

#ifdef FINAL_PROVISIONING // only used in jungle final provisioning
void can_set_forwarding(uint8_t from, uint8_t to) {
bus_config[from].forwarding_bus = to;
}
#endif

void ignition_can_hook(CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
Expand Down
12 changes: 7 additions & 5 deletions board/drivers/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
#define OUTPUT_TYPE_PUSH_PULL 0U
#define OUTPUT_TYPE_OPEN_DRAIN 1U

typedef struct {
GPIO_TypeDef * const bank;
uint8_t pin;
} gpio_t;

void set_gpio_mode(GPIO_TypeDef *GPIO, unsigned int pin, unsigned int mode) {
ENTER_CRITICAL();
uint32_t tmp = GPIO->MODER;
Expand Down Expand Up @@ -68,6 +63,12 @@ int get_gpio_input(const GPIO_TypeDef *GPIO, unsigned int pin) {
return (GPIO->IDR & (1UL << pin)) == (1UL << pin);
}

#ifdef PANDA_JUNGLE
typedef struct {
GPIO_TypeDef * const bank;
uint8_t pin;
} gpio_t;

void gpio_set_all_output(gpio_t *pins, uint8_t num_pins, bool enabled) {
for (uint8_t i = 0; i < num_pins; i++) {
set_gpio_output(pins[i].bank, pins[i].pin, enabled);
Expand All @@ -79,6 +80,7 @@ void gpio_set_bitmask(gpio_t *pins, uint8_t num_pins, uint32_t bitmask) {
set_gpio_output(pins[i].bank, pins[i].pin, (bitmask >> i) & 1U);
}
}
#endif

// Detection with internal pullup
#define PULL_EFFECTIVE_DELAY 4096
Expand Down
15 changes: 0 additions & 15 deletions board/drivers/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ typedef struct uart_ring {
// ***************************** Function prototypes *****************************
void debug_ring_callback(uart_ring *ring);
void uart_tx_ring(uart_ring *q);
void uart_send_break(uart_ring *u);

// ******************************** UART buffers ********************************

Expand Down Expand Up @@ -151,20 +150,6 @@ void print(const char *a) {
}
}

void putui(uint32_t i) {
uint32_t i_copy = i;
char str[11];
uint8_t idx = 10;
str[idx] = '\0';
idx--;
do {
str[idx] = (i_copy % 10U) + 0x30U;
idx--;
i_copy /= 10;
} while (i_copy != 0U);
print(&str[idx + 1U]);
}

void puthx(uint32_t i, uint8_t len) {
const char c[] = "0123456789abcdef";
for (int pos = ((int)len * 4) - 4; pos > -4; pos -= 4) {
Expand Down
8 changes: 0 additions & 8 deletions board/drivers/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -914,11 +914,3 @@ void can_tx_comms_resume_usb(void) {
}
EXIT_CRITICAL();
}

void usb_soft_disconnect(bool enable) {
if (enable) {
USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
} else {
USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
}
}
24 changes: 0 additions & 24 deletions board/drivers/watchdog.h

This file was deleted.

1 change: 1 addition & 0 deletions board/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void assert_fatal(bool condition, const char *msg) {
}

// cppcheck-suppress misra-c2012-21.2
// cppcheck-suppress unusedFunction ; gcc uses memset to initialize structures
void *memset(void *str, int c, unsigned int n) {
uint8_t *s = str;
for (unsigned int i = 0; i < n; i++) {
Expand Down
2 changes: 0 additions & 2 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,9 @@ int main(void) {
// enable USB (right before interrupts or enum can fail!)
usb_init();

#ifdef ENABLE_SPI
if (current_board->has_spi) {
spi_init();
}
#endif

current_board->set_led(LED_RED, false);
current_board->set_led(LED_GREEN, false);
Expand Down
2 changes: 1 addition & 1 deletion board/safety.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ bool max_limit_check(int val, const int MAX_VAL, const int MIN_VAL) {
}

// check that commanded torque value isn't too far from measured
bool dist_to_meas_check(int val, int val_last, struct sample_t *val_meas,
bool dist_to_meas_check(int val, int val_last, const struct sample_t *val_meas,
const int MAX_RATE_UP, const int MAX_RATE_DOWN, const int MAX_ERROR) {

// *** val rate limit check ***
Expand Down
29 changes: 27 additions & 2 deletions board/safety/safety_hyundai_canfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ RxCheck hyundai_canfd_hda2_long_rx_checks[] = {



const int HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32;
const int HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING = 128;
const uint16_t HYUNDAI_PARAM_CANFD_ALT_BUTTONS = 32;
const uint16_t HYUNDAI_PARAM_CANFD_HDA2_ALT_STEERING = 128;
bool hyundai_canfd_alt_buttons = false;
bool hyundai_canfd_hda2_alt_steering = false;

Expand All @@ -152,6 +152,31 @@ static uint32_t hyundai_canfd_get_checksum(const CANPacket_t *to_push) {
return chksum;
}

uint32_t hyundai_common_canfd_compute_checksum(const CANPacket_t *to_push) {
int len = GET_LEN(to_push);
uint32_t address = GET_ADDR(to_push);

uint16_t crc = 0;

for (int i = 2; i < len; i++) {
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)];
}

// Add address to crc
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)];
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)];

if (len == 24) {
crc ^= 0x819dU;
} else if (len == 32) {
crc ^= 0x9f5bU;
} else {

}

return crc;
}

static void hyundai_canfd_rx_hook(const CANPacket_t *to_push) {
int bus = GET_BUS(to_push);
int addr = GET_ADDR(to_push);
Expand Down
37 changes: 6 additions & 31 deletions board/safety/safety_hyundai_common.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef SAFETY_HYUNDAI_COMMON_H
#define SAFETY_HYUNDAI_COMMON_H

const int HYUNDAI_PARAM_EV_GAS = 1;
const int HYUNDAI_PARAM_HYBRID_GAS = 2;
const int HYUNDAI_PARAM_LONGITUDINAL = 4;
const int HYUNDAI_PARAM_CAMERA_SCC = 8;
const int HYUNDAI_PARAM_CANFD_HDA2 = 16;
const int HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags
const uint8_t HYUNDAI_PARAM_EV_GAS = 1;
const uint8_t HYUNDAI_PARAM_HYBRID_GAS = 2;
const uint8_t HYUNDAI_PARAM_LONGITUDINAL = 4;
const uint8_t HYUNDAI_PARAM_CAMERA_SCC = 8;
const uint8_t HYUNDAI_PARAM_CANFD_HDA2 = 16;
const uint8_t HYUNDAI_PARAM_ALT_LIMITS = 64; // TODO: shift this down with the rest of the common flags

const uint8_t HYUNDAI_PREV_BUTTON_SAMPLES = 8; // roughly 160 ms
const uint32_t HYUNDAI_STANDSTILL_THRSLD = 12; // 0.375 kph
Expand Down Expand Up @@ -86,29 +86,4 @@ void hyundai_common_cruise_buttons_check(const int cruise_button, const bool mai
}
}

uint32_t hyundai_common_canfd_compute_checksum(const CANPacket_t *to_push) {
int len = GET_LEN(to_push);
uint32_t address = GET_ADDR(to_push);

uint16_t crc = 0;

for (int i = 2; i < len; i++) {
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ GET_BYTE(to_push, i)];
}

// Add address to crc
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 0U) & 0xFFU)];
crc = (crc << 8U) ^ hyundai_canfd_crc_lut[(crc >> 8U) ^ ((address >> 8U) & 0xFFU)];

if (len == 24) {
crc ^= 0x819dU;
} else if (len == 32) {
crc ^= 0x9f5bU;
} else {

}

return crc;
}

#endif
2 changes: 1 addition & 1 deletion board/safety/safety_nissan.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ RxCheck nissan_rx_checks[] = {
};

// EPS Location. false = V-CAN, true = C-CAN
const int NISSAN_PARAM_ALT_EPS_BUS = 1;
const uint8_t NISSAN_PARAM_ALT_EPS_BUS = 1;

bool nissan_alt_eps = false;

Expand Down
2 changes: 1 addition & 1 deletion board/safety/safety_subaru_preglobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RxCheck subaru_preglobal_rx_checks[] = {
};


const int SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE = 1;
const uint8_t SUBARU_PG_PARAM_REVERSED_DRIVER_TORQUE = 1;
bool subaru_pg_reversed_driver_torque = false;


Expand Down
6 changes: 3 additions & 3 deletions board/safety/safety_tesla.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const LongitudinalLimits TESLA_LONG_LIMITS = {
};


const int TESLA_FLAG_POWERTRAIN = 1;
const int TESLA_FLAG_LONGITUDINAL_CONTROL = 2;
const int TESLA_FLAG_RAVEN = 4;
const uint8_t TESLA_FLAG_POWERTRAIN = 1;
const uint8_t TESLA_FLAG_LONGITUDINAL_CONTROL = 2;
const uint8_t TESLA_FLAG_RAVEN = 4;

const CanMsg TESLA_TX_MSGS[] = {
{0x488, 0, 4}, // DAS_steeringControl
Expand Down
6 changes: 4 additions & 2 deletions board/safety_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define GET_BIT(msg, b) ((bool)!!(((msg)->data[((b) / 8U)] >> ((b) % 8U)) & 0x1U))
#define GET_BYTE(msg, b) ((msg)->data[(b)])

// cppcheck-suppress-macro misra-c2012-17.3 ; misra addon doesn't know __typeof__ symbol https://github.com/danmar/cppcheck/pull/5568#discussion_r1632304897
#define GET_FLAG(value, mask) (((__typeof__(mask))(value) & (mask)) == (mask)) // cppcheck-suppress misra-c2012-1.2; allow __typeof__

#define BUILD_SAFETY_CFG(rx, tx) ((safety_config){(rx), (sizeof((rx)) / sizeof((rx)[0])), \
Expand Down Expand Up @@ -30,7 +32,7 @@ const uint8_t MAX_MISSED_MSGS = 10U;


// sample struct that keeps 6 samples in memory
struct sample_t {
const struct sample_t {
int values[MAX_SAMPLE_VALS];
int min;
int max;
Expand Down Expand Up @@ -174,7 +176,7 @@ void reset_sample(struct sample_t *sample);
bool max_limit_check(int val, const int MAX, const int MIN);
bool angle_dist_to_meas_check(int val, struct sample_t *val_meas,
const int MAX_ERROR, const int MAX_VAL);
bool dist_to_meas_check(int val, int val_last, struct sample_t *val_meas,
bool dist_to_meas_check(int val, int val_last, const struct sample_t *val_meas,
const int MAX_RATE_UP, const int MAX_RATE_DOWN, const int MAX_ERROR);
bool driver_limit_check(int val, int val_last, const struct sample_t *val_driver,
const int MAX, const int MAX_RATE_UP, const int MAX_RATE_DOWN,
Expand Down
Loading