Skip to content

Conversation

willy-liu
Copy link
Contributor

Static analysis with cppcheck fails because predefined compiler macros are not available during analysis. This causes the environment detection in the list macro to fall back to an invalid code branch, triggering false errors such as unused labels.

Although this fallback branch is not used in supported environments, it is still parsed by cppcheck unless the proper macro definitions are provided.

This commit resolves the issue by passing the necessary compiler environment macros to cppcheck through the -D option, ensuring correct evaluation of conditional logic in macro definitions.

  • Prevent static analysis false positives in list macros
  • Ensure the environment is correctly simulated during checks

See https://hackmd.io/@willy-liu/linux2025-homework1#q_free for details.

Change-Id: If8b51c44f194f19f109bc4aaf91a464f850e7fdc

@willy-liu
Copy link
Contributor Author

Refactored the detection of compiler type and standard version
into a Bash function.

The function get_compiler_macros
determines the compiler in use, extracts the
standard version, and constructs the necessary macro definitions
for static analysis tools. The detected macros are then passed
to cppcheck to ensure proper environment simulation.

  • Improve code organization and clarity
  • Ensure static analysis tools receive accurate macro definitions
  • Support both GCC and Clang detection

@willy-liu
Copy link
Contributor Author

Standardize naming and indentation

Renamed function for consistency and adjusted indentation to align with
project style.

jserv

This comment was marked as resolved.

@willy-liu

This comment was marked as resolved.

Comment on lines 75 to 83
if command -v gcc >/dev/null 2>&1; then
STDC_VERSION=$(gcc -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
EXTRA_DEFINES="-D__GNUC__=1"
elif command -v clang >/dev/null 2>&1; then
STDC_VERSION=$(clang -dM -E -xc /dev/null | awk '/__STDC_VERSION__/ {print $3}')
EXTRA_DEFINES="-D__clang__=1"
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During the make process, compilation utilizes the cc command, which references either gcc or clang based on your system's configuration. On GNU/Linux systems, cc typically defaults to gcc, while BSD systems (including macOS) link cc to clang. We shall analyze the cc command before directly invoking either gcc or clang.

See also: https://github.com/sysprog21/rv32emu/blob/master/mk/toolchain.mk

Copy link
Contributor Author

@willy-liu willy-liu Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After learning the cc command, I understand it’s a better way to identify the C compiler.

However, when I checked the Makefile, I noticed that make defaults to gcc because CC = gcc is set in the first line.

https://github.com/sysprog21/lab0-c/blob/master/Makefile

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that make defaults to gcc because CC = gcc is set in the first line.

The build compiler and configurations can be checked by examining the cc variable, as the CC variable may be subject to override. It's generally acceptable to do this verification.

Added a function to determine the compiler type (GCC or Clang)
and extract the standard version. This ensures that predefined
macros are correctly passed to cppcheck, preventing false positives
caused by incorrect environment detection in macro evaluations.

Change-Id: I0c8cd7507f081a03f1c1ffec648aed544a0e6466
@willy-liu
Copy link
Contributor Author

Refactored the function using cc to check compiler type.

@jserv jserv merged commit a2b0d76 into sysprog21:master Mar 9, 2025
1 of 2 checks passed
@jserv
Copy link
Contributor

jserv commented Mar 9, 2025

Thank @willy-liu for contributing!

@willy-liu willy-liu deleted the cppcheck_dev branch March 9, 2025 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants