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

llvm: Fix unused function warning #18870

Merged
merged 1 commit into from
Mar 20, 2024

Conversation

Teufelchen1
Copy link
Contributor

@Teufelchen1 Teufelchen1 commented Nov 10, 2022

Related to #18851

When building example/gnrc_lorawan using LLVM two unused function error are raised. Those are fixed by only include said functions when they are needed.

Additionally, LLVM complains about lorawan struct being not packed correctly. I believe this is because GCC applies the given attribute(packed) to all elements of a given struct including the anonymous struct and unions if present. Clang on the other does not seem to do that. The fix works by adding the packed attribute to the anonymous union as well.

@github-actions github-actions bot added Area: cpu Area: CPU/MCU ports Area: LoRa Area: LoRa radio support Area: network Area: Networking Area: sys Area: System Platform: ARM Platform: This PR/issue effects ARM-based platforms labels Nov 10, 2022
Comment on lines 50 to 53
union __attribute__((packed)) {
uint32_t u8_pad;
le_uint16_t conf_fcnt;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
union __attribute__((packed)) {
uint32_t u8_pad;
le_uint16_t conf_fcnt;
};
uint16_t u8_pad;
le_uint16_t conf_fcnt;

i am not sure why that pad is named u8_pad maybe you are able to tell?

Copy link
Contributor

@benpicco benpicco Nov 11, 2022

Choose a reason for hiding this comment

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

Shouldn't that be

le_uint16_t conf_fcnt;
uint16_t u8_pad;

Copy link
Contributor

Choose a reason for hiding this comment

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

you are right

Suggested change
union __attribute__((packed)) {
uint32_t u8_pad;
le_uint16_t conf_fcnt;
};
le_uint16_t conf_fcnt;
uint16_t u16_pad;

Copy link
Contributor

Choose a reason for hiding this comment

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

diff --git a/sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_crypto.c b/sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_crypto.c
index 18fc6e29b9..6bcddfde2d 100644
--- a/sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_crypto.c
+++ b/sys/net/gnrc/link_layer/lorawan/gnrc_lorawan_crypto.c
@@ -235,11 +235,8 @@ void gnrc_lorawan_encrypt_payload(iolist_t *iolist, const le_uint32_t *dev_addr,
                                   uint32_t fcnt, uint8_t dir,
                                   const uint8_t *appskey)
 {
-    uint8_t s_block[16];
-    uint8_t a_block[16];
-
-    memset(s_block, 0, sizeof(s_block));
-    memset(a_block, 0, sizeof(a_block));
+    uint8_t s_block[16]={0};
+    uint8_t a_block[16]={0};
 
     lorawan_block0_t *block = (lorawan_block0_t *)a_block;
 
@@ -247,14 +244,11 @@ void gnrc_lorawan_encrypt_payload(iolist_t *iolist, const le_uint32_t *dev_addr,
 
     block->fb = CRYPT_B0_START;
 
-    block->u8_pad = 0;
     block->dir = dir & DIR_MASK;
 
     block->dev_addr = *dev_addr;
     block->fcnt = byteorder_htoll(fcnt);
 
-    block->u32_pad = 0;
-
     int c = 0;
 
     for (iolist_t *io = iolist; io != NULL; io = io->iol_next) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rebased my PR. This was fixed here: 4709bbb in PR #19634

Why was this discussed here but not there? I suspect it slipped through, as the PR had well over 1500 edits? We really should aim for small PRs when feasible :)

@@ -160,6 +160,7 @@ static void _gpio_init_ain(void)
* This very teniously avoids optimization, even optimized it's better than
* nothing but periodic review should establish that it doesn't get optimized.
*/
#if defined(STM32_OPTION_BYTES)
Copy link
Contributor

Choose a reason for hiding this comment

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

either use attribute unused or move the #ifdef up from line 244 (not do the same #ifdef twice)

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a fixup that moves the #ifdef. I think attribute(unused) is wrong here. This is not three steps into linking or hidden behind 100 layers of dependency modelling. That function is exactly used three times and only when that option is enabled. An attribute does not convey that meaning/context but the ifdef does.

@kfessel
Copy link
Contributor

kfessel commented Nov 11, 2022

this may better be two PR since the changes are in very different parts of RIOT and not connected but by this PR and the issue

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label May 11, 2023
@@ -396,13 +398,15 @@ void backup_ram_init(void)
#define BACKUP_RAM_MAGIC {'R', 'I', 'O', 'T'}
#endif

#if IS_ACTIVE(CPU_HAS_BACKUP_RAM)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#if IS_ACTIVE(CPU_HAS_BACKUP_RAM)
__attribute__((unused))

is IMO much better than using the preprocessor, as we get better LSP suggestions, better static analysis results and wide compile test coverage when not hiding functions from the compiler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hehe, you did it yourself in f4c5cf1

@riot-ci
Copy link

riot-ci commented May 11, 2023

Murdock results

✔️ PASSED

d19182c llvm: cpu/stm32: Fix unused function warning

Success Failures Total Runtime
10008 0 10009 09m:10s

Artifacts

@benpicco
Copy link
Contributor

Those are both very minor changes, I don't think we need two PRs for that.

@Teufelchen1 Teufelchen1 force-pushed the fix/llvm_gnrc_lorawan branch from 2e4ef0d to ccafa5a Compare March 19, 2024 12:37
@github-actions github-actions bot removed Area: network Area: Networking Area: LoRa Area: LoRa radio support Area: sys Area: System labels Mar 19, 2024
@@ -161,6 +161,7 @@ static void _gpio_init_ain(void)
* This very teniously avoids optimization, even optimized it's better than
* nothing but periodic review should establish that it doesn't get optimized.
*/
#if defined(STM32_OPTION_BYTES)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
#if defined(STM32_OPTION_BYTES)
MAYBE_UNUSED

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Saw this comment too late, see #18870 (comment)

Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

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

Please squash

@Teufelchen1 Teufelchen1 changed the title llvm: Fix unused function warning & struct packing in stm32 & gnrc/lorawan llvm: Fix unused function warning ~~& struct packing in stm32 & gnrc/lorawan~~ Mar 19, 2024
@Teufelchen1 Teufelchen1 changed the title llvm: Fix unused function warning ~~& struct packing in stm32 & gnrc/lorawan~~ llvm: Fix unused function warning Mar 19, 2024
@Teufelchen1 Teufelchen1 force-pushed the fix/llvm_gnrc_lorawan branch from dd83c9e to 7b38e56 Compare March 19, 2024 13:00
@maribu
Copy link
Member

maribu commented Mar 19, 2024

Looks like something has gone wrong when squashing scratch that, I misread

@benpicco benpicco added the Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer label Mar 19, 2024
@benpicco benpicco enabled auto-merge March 19, 2024 13:31
@benpicco benpicco added this pull request to the merge queue Mar 19, 2024
@Teufelchen1 Teufelchen1 removed this pull request from the merge queue due to a manual request Mar 19, 2024
@Teufelchen1 Teufelchen1 force-pushed the fix/llvm_gnrc_lorawan branch from 7b38e56 to d19182c Compare March 19, 2024 16:20
@Teufelchen1
Copy link
Contributor Author

Switched to using MAYBE_UNUSED after maribu asked in private politely.

@maribu maribu enabled auto-merge March 19, 2024 16:52
@maribu maribu added this pull request to the merge queue Mar 19, 2024
Merged via the queue into RIOT-OS:master with commit 23d30cb Mar 20, 2024
25 checks passed
@MrKevinWeiss MrKevinWeiss added this to the Release 2024.04 milestone Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer Platform: ARM Platform: This PR/issue effects ARM-based platforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants