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

Refactor to address compiler warnings #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bdd
Copy link
Contributor

@bdd bdd commented Apr 23, 2023

  • Unused function warnings Move function definitions in include/blisp_util.h to lib/blisp_util.c. Defining them as static leads to internal linkage, accessible in that translation unit, hence the warning. Their references are to be handled in other translation units, so they shouldn't be static.

  • Unused function parameters Define a macro for void casting unused parameters as a portable-across-compilers way to suppress these warnings.

    • blisp_chip_xxxxx_get_eflash_loader implementations accept clock type but don't use it.

    • drain function only has a body under macOS and FreeBSD with preprocessor predicates.

  • Enable compiler warnings Now that warnings are address enable them -Wall -Wextra -Wpedantic for the library and the tool targets.

N.B. An equivalent of MSVC should be added to CMakeLists.txt, as these would only work when using GCC or Clang.

Current warnings on master:

[  3%] Building C object CMakeFiles/libblisp_obj.dir/lib/blisp.c.o
/home/bdd/src/github/pine64/blisp/lib/blisp.c: In function ‘drain’:
/home/bdd/src/github/pine64/blisp/lib/blisp.c:16:35: warning: unused parameter ‘port’ [-Wunused-parameter]
   16 | static void drain(struct sp_port* port) {
      |                   ~~~~~~~~~~~~~~~~^~~~
In file included from /home/bdd/src/github/pine64/blisp/lib/blisp.c:3:
/home/bdd/src/github/pine64/blisp/include/blisp_util.h: At top level:
/home/bdd/src/github/pine64/blisp/include/blisp_util.h:83:17: warning: ‘crc32_calculate’ defined but not used [-Wunused-function]
   83 | static uint32_t crc32_calculate(const void *data, size_t data_len)
      |                 ^~~~~~~~~~~~~~~
[  6%] Building C object CMakeFiles/libblisp_obj.dir/lib/chip/blisp_chip_bl60x.c.o
/home/bdd/src/github/pine64/blisp/lib/chip/blisp_chip_bl60x.c: In function ‘blisp_chip_bl60x_get_eflash_loader’:
/home/bdd/src/github/pine64/blisp/lib/chip/blisp_chip_bl60x.c:7:52: warning: unused parameter ‘clk_type’ [-Wunused-parameter]
    7 | int64_t blisp_chip_bl60x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
      |                                            ~~~~~~~~^~~~~~~~
[  9%] Building C object CMakeFiles/libblisp_obj.dir/lib/chip/blisp_chip_bl70x.c.o
/home/bdd/src/github/pine64/blisp/lib/chip/blisp_chip_bl70x.c: In function ‘blisp_chip_bl70x_get_eflash_loader’:
/home/bdd/src/github/pine64/blisp/lib/chip/blisp_chip_bl70x.c:7:52: warning: unused parameter ‘clk_type’ [-Wunused-parameter]
    7 | int64_t blisp_chip_bl70x_get_eflash_loader(uint8_t clk_type, uint8_t** firmware_buf_ptr)
      |                                            ~~~~~~~~^~~~~~~~
[ 12%] Building C object CMakeFiles/libblisp_obj.dir/lib/blisp_easy.c.o
In file included from /home/bdd/src/github/pine64/blisp/lib/blisp_easy.c:4:
/home/bdd/src/github/pine64/blisp/include/blisp_util.h:24:13: warning: ‘sleep_ms’ defined but not used [-Wunused-function]
   24 | static void sleep_ms(int milliseconds) {

include/blisp.h Outdated
#define UNUSED(x) (void)(x)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sidenote:
Root CMakeLists.txt requests C standard 23 via CMAKE_C_STANDARD. C23 introduced official maybe_unused attribute. Using latter might avoid clumsiness of ancient C helper construct and ease portability ("portable-across-compilers").
Ultimately, also matter of style.

Copy link
Contributor Author

@bdd bdd Apr 30, 2023

Choose a reason for hiding this comment

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

Thanks. I had no idea this made it to C standard, assumed it was C++ only. Didn't notice this project asked for C23, a pleasant surprise. Updated to use the attribute instead.

Copy link
Contributor Author

@bdd bdd Apr 30, 2023

Choose a reason for hiding this comment

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

Well, MSVC's C23 support is...what's the word I'm looking for...sparse. [[maybe_unused]] is not implemented, yet.
image
From: https://en.cppreference.com/w/c/compiler_support/23

Compilation failure from GHA https://github.com/bdd/blisp/actions/runs/4845978847/jobs/8635225026

@robertlipe
Copy link
Collaborator

robertlipe commented Apr 30, 2023 via email

- Unused function warnings
Move function definitions in include/blisp_util.h to lib/blisp_util.c.
Defining them as static leads to internal linkage, accessible in that
translation unit, hence the warning. Their references are to be handled
in other translation units, so they shouldn't be static.

- Unused function parameters
Use C attribute `[[maybe_unused]]` (introduced in C23) to suppress
unused parameter warnings.

  - `blisp_chip_xxxxx_get_eflash_loader` implementations accept clock type
    but don't use it.

  - `drain` function only has a body under macOS and FreeBSD with
    preprocessor predicates.

- Enable compiler warnings
Now that warnings are address enable them `-Wall -Wextra -Wpedantic` for
the library and the tool targets.

N.B. An equivalent of MSVC should be added to CMakeLists.txt, as these
would only work when using GCC or Clang.
@robertlipe
Copy link
Collaborator

robertlipe commented Apr 30, 2023 via email

@robertlipe
Copy link
Collaborator

robertlipe commented May 1, 2023

Oh. I see now there IS a failing presubmit on this. (Why doesn't the GH mailer make that more clear? Ugh!) That's both fortunate (it caught the issue) and unfortunate (it claims to require support for C23, but supports a compiiler that doesn't really support C23.). As [[maybe_unused]] was part of C++17, it would be pretty hamfisted if they special-cased in the C front-end.

Can you please upgrade the requirement in workspaces/whatever.yaml to try to use the latest MSVC as it looks like it might be using a 2019 version
-- The C compiler identification is MSVC 19.34.31944.0
so maybe getting to the latest from MSVC in the presubmit build image will help.

Barring that, it looks like the solution may be to try something approximately like:

#if defined(_MSC_VER) 
#  define MAYBE_UNUSED(x) __pragma(warning(suppress:4100))
#else
#  define MAYBE_UNUSED(x) [[maybe_unused]]x
#endif

(Code is free-handed. Expect problems.)

Please try to trigger on _MSC_VER since we're tip-toeing around broken MSVC and not WIN32; people use Clang, GCC, or other working compilers on Windows.

SO has many hint, but many are based on C++ (wheere this has been required since C++17) or otherwise before C23. There are a few answers that consider it, though, such as https://stackoverflow.com/a/75965534

Sorry, but It's simply annoying that there are so many different ways to spell this, though most compilers have had some form of this for 10+ years in their C/C++ front-ends. This is why the C/C++ groups (finally) decided to unify some of this stuff very recently.

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.

3 participants