-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix missing compiler macros in cppcheck analysis #240
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
Conversation
Refactored the detection of compiler type and standard version The function
|
Standardize naming and indentation Renamed function for consistency and adjusted indentation to align with |
This comment was marked as resolved.
This comment was marked as resolved.
scripts/pre-commit.hook
Outdated
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 togcc
becauseCC = 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
Refactored the function using |
Thank @willy-liu for contributing! |
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.
See https://hackmd.io/@willy-liu/linux2025-homework1#q_free for details.
Change-Id: If8b51c44f194f19f109bc4aaf91a464f850e7fdc