Skip to content

Add IAR warning flags to CMake build and resolve warnings#3528

Merged
hathach merged 2 commits into
masterfrom
improve-iar-warnings
Mar 6, 2026
Merged

Add IAR warning flags to CMake build and resolve warnings#3528
hathach merged 2 commits into
masterfrom
improve-iar-warnings

Conversation

@hathach
Copy link
Copy Markdown
Owner

@hathach hathach commented Mar 5, 2026

### Description

This pull request adds IAR-specific warning flags to the CMake build system and resolves associated warnings

Copilot AI review requested due to automatic review settings March 5, 2026 16:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds IAR-specific warning flags to the CMake build and adjusts code patterns to eliminate IAR warning diagnostics (notably around volatile access ordering and unreachable code).

Changes:

  • Add IAR warning/error flags in the common CMake family configuration.
  • Refactor FIFO helpers to avoid IAR volatile access ordering warnings without compiler pragmas.
  • Remove unreachable returns / tidy control-flow and casts to resolve compiler warnings across examples and DFU.

Reviewed changes

Copilot reviewed 19 out of 19 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/osal/osal_freertos.h Uses compile-time branching for ISR spinlock behavior to avoid warnings.
src/common/tusb_fifo.h Reworks volatile reads in inline FIFO helpers; minor comment cleanup.
src/common/tusb_fifo.c Reworks volatile reads passed as function args to avoid IAR warnings.
src/class/dfu/dfu_device.c Adds explicit casts to satisfy enum/type warnings.
hw/bsp/family_support.cmake Introduces IAR warning flags and wires them into target compile options.
examples/host/msc_file_explorer/src/msc_app.c Removes unreachable return.
examples/host/msc_file_explorer/src/main.c Removes unreachable return.
examples/host/midi_rx/src/main.c Removes unreachable return.
examples/host/device_info/src/main.c Removes unreachable return.
examples/host/bare_api/src/main.c Removes unreachable return.
examples/dual/host_hid_to_device_cdc/src/main.c Removes unreachable return.
examples/device/uac2_speaker_fb/src/main.c Adds braces; refactors expression to reduce warning/noise.
examples/device/net_lwip_webserver/src/main.c Removes unreachable return.
examples/device/msc_dual_lun/src/main.c Removes unreachable return.
examples/device/hid_boot_interface/src/main.c Removes unreachable return.
examples/device/dynamic_configuration/src/msc_disk.c Simplifies unsupported SCSI handling; adds explicit unused markers.
examples/device/cdc_uac2/src/main.c Removes unreachable return.
examples/device/cdc_msc_freertos/src/msc_disk.c Simplifies unsupported SCSI handling; adds explicit unused markers.
examples/device/cdc_msc/src/msc_disk.c Fixes grammar in comment.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +108 to +113
set(WARN_FLAGS_IAR
--warnings_are_errors
--diag_suppress=Pa089
--diag_suppress=Pe236
)

Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

Applying --warnings_are_errors plus globally suppressing specific diagnostics at the family level can make downstream/3rd-party code builds fail unexpectedly while also hiding those suppressed warning classes across the entire target. Consider making --warnings_are_errors opt-in (e.g., via a CMake option) and scoping --diag_suppress to only the specific targets/files that need it, with a short comment explaining what Pa089/Pe236 represent and why suppression is safe.

Suggested change
set(WARN_FLAGS_IAR
--warnings_are_errors
--diag_suppress=Pa089
--diag_suppress=Pe236
)
# Control how strictly IAR treats warnings and whether specific diagnostics
# are suppressed. These options are provided to avoid forcing downstream or
# 3rd-party code to build with warnings-as-errors or hidden diagnostics.
option(IAR_WARNINGS_ARE_ERRORS "Treat all IAR compiler warnings as errors" OFF)
option(IAR_SUPPRESS_DIAGNOSTICS "Suppress specific IAR diagnostics (Pa089, Pe236)" ON)
set(WARN_FLAGS_IAR)
if (IAR_WARNINGS_ARE_ERRORS)
list(APPEND WARN_FLAGS_IAR
--warnings_are_errors
)
endif()
if (IAR_SUPPRESS_DIAGNOSTICS)
# Pa089: disabled optimization messages that are noisy but not correctness issues.
# Pe236: controlling expression is constant; common in embedded register/bitfield code.
# These are considered safe to suppress for this BSP, but can be disabled via
# -DIAR_SUPPRESS_DIAGNOSTICS=OFF if a project prefers to see them.
list(APPEND WARN_FLAGS_IAR
--diag_suppress=Pa089
--diag_suppress=Pe236
)
endif()

Copilot uses AI. Check for mistakes.
Comment thread src/common/tusb_fifo.h
Comment on lines +294 to +296
const uint16_t wr_idx = f->wr_idx;
const uint16_t rd_idx = f->rd_idx;
return tu_min16(tu_ff_overflow_count(f->depth, wr_idx, rd_idx), f->depth);
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

The code now intentionally snapshots volatile indices into locals to avoid IAR's Pa082 (unspecified ordering of volatile accesses). Since the previous pragma-based explanation was removed, add a brief comment near these snapshots (or once above the related inline helpers) explaining that this pattern is required to enforce a defined volatile read order for IAR and similar compilers.

Copilot uses AI. Check for mistakes.
// - READ10 and WRITE10 has their own callbacks
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
// read10 & write10 has their own callback and MUST not be handled here
(void) lun;
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

lun is explicitly marked unused but is still used in the tud_msc_set_sense(lun, ...) call. This is harmless but misleading for readers and can mask real unused-parameter situations during refactors. Drop the (void) lun; line (keep (void) scsi_cmd; if you intend to ignore the command), or alternatively keep using scsi_cmd[0] in a minimal switch to justify the parameter.

Suggested change
(void) lun;

Copilot uses AI. Check for mistakes.
Comment on lines +225 to +226
// Set Sense = Invalid Command Operation
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

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

lun is explicitly marked unused but is still used in the tud_msc_set_sense(lun, ...) call. This is harmless but misleading for readers and can mask real unused-parameter situations during refactors. Drop the (void) lun; line (keep (void) scsi_cmd; if you intend to ignore the command), or alternatively keep using scsi_cmd[0] in a minimal switch to justify the parameter.

Copilot uses AI. Check for mistakes.
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1;
return -1; // stall/failed command request;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

Copilot Autofix

AI 3 months ago

To fix the issue without changing functionality, we just need to adjust the comment so it no longer looks code-like to the analyzer while still documenting the meaning of return -1;. The best approach is to rewrite it as a full sentence in natural language, possibly expanding abbreviations, so it’s clearly not intended as code. We only need to edit the comment on line 337 in examples/device/cdc_msc_freertos/src/msc_disk.c.

Concretely, replace // stall/failed command request; with something like // Indicate a stalled or failed command request.. This keeps the same intent but removes the trailing semicolon and uses normal prose, which should satisfy the static analysis rule. No additional methods, imports, or definitions are needed.

Suggested changeset 1
examples/device/cdc_msc_freertos/src/msc_disk.c

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/device/cdc_msc_freertos/src/msc_disk.c b/examples/device/cdc_msc_freertos/src/msc_disk.c
--- a/examples/device/cdc_msc_freertos/src/msc_disk.c
+++ b/examples/device/cdc_msc_freertos/src/msc_disk.c
@@ -334,7 +334,7 @@
   // Set Sense = Invalid Command Operation
   (void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
 
-  return -1; // stall/failed command request;
+  return -1; // Indicate a stalled or failed command request.
 }
 
 #endif
EOF
@@ -334,7 +334,7 @@
// Set Sense = Invalid Command Operation
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1; // stall/failed command request;
return -1; // Indicate a stalled or failed command request.
}

#endif
Copilot is powered by AI and may make mistakes. Always verify output.
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1;
return -1; // stall/failed command request;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

Copilot Autofix

AI 3 months ago

In general, to address “commented-out code” findings, either delete truly dead commented code or reword comments so they clearly read as prose instead of code. Here, the best fix is to keep the documentation but rewrite the comment so it no longer resembles a code statement (for example, remove the trailing semicolon and phrase it as an explanatory sentence). This maintains existing functionality and behavior exactly while satisfying the static analysis rule.

Concretely, in examples/device/dynamic_configuration/src/msc_disk.c, change the comment on line 228 that currently reads // stall/failed command request; to a clearer explanation such as // Return value -1 indicates a stalled or failed command request.. No additional methods, imports, or definitions are required; we are only editing this comment.

Suggested changeset 1
examples/device/dynamic_configuration/src/msc_disk.c

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/examples/device/dynamic_configuration/src/msc_disk.c b/examples/device/dynamic_configuration/src/msc_disk.c
--- a/examples/device/dynamic_configuration/src/msc_disk.c
+++ b/examples/device/dynamic_configuration/src/msc_disk.c
@@ -225,7 +225,7 @@
   // Set Sense = Invalid Command Operation
   (void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
 
-  return -1; // stall/failed command request;
+  return -1; // Return value -1 indicates a stalled or failed command request.
 }
 
 #endif
EOF
@@ -225,7 +225,7 @@
// Set Sense = Invalid Command Operation
(void) tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);

return -1; // stall/failed command request;
return -1; // Return value -1 indicates a stalled or failed command request.
}

#endif
Copilot is powered by AI and may make mistakes. Always verify output.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 6, 2026

Size Difference Report

Because TinyUSB code size varies by port and configuration, the metrics below represent the averaged totals across all example builds.

Note: If there is no change, only one value is shown.

Changes >1% in size

No entries.

Changes <1% in size

file .text .data .bss size % diff
midi_device.c 1127 ➙ 1129 (+2) 991 ➙ 995 (+4) 589 1714 ➙ 1716 (+2) +0.1%
tusb.c 430 ➙ 431 (+1) 368 3 432 ➙ 433 (+1) +0.2%
TOTAL 1557 ➙ 1560 (+3) 1359 ➙ 1363 (+4) 592 2146 ➙ 2149 (+3) +0.1%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2849 ➙ 2850 (+1) 0 1248 1673 4518 +0.0%
cdc_device.c 1326 16 19 660 1985 +0.0%
cdc_host.c 6610 487 15 1539 8371 +0.0%
dcd_ch32_usbfs.c 1472 0 0 2444 3916 +0.0%
dcd_ch32_usbhs.c 1648 0 0 448 2096 +0.0%
dcd_ci_fs.c 1925 0 0 1290 3215 +0.0%
dcd_ci_hs.c 1762 0 0 1280 2530 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4174 25 0 265 4464 +0.0%
dcd_eptri.c 2270 0 0 259 2529 +0.0%
dcd_khci.c 1953 0 0 1290 3243 +0.0%
dcd_lpc17_40.c 1470 0 0 648 1794 +0.0%
dcd_lpc_ip3511.c 1463 0 0 264 1639 +0.0%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 +0.0%
dcd_msp430x5xx.c 1796 0 0 176 1972 +0.0%
dcd_musb.c 2446 0 0 160 2606 +0.0%
dcd_nrf5x.c 2919 0 0 292 3211 +0.0%
dcd_nuc120.c 1093 0 0 78 1171 +0.0%
dcd_nuc121.c 1167 0 0 101 1268 +0.0%
dcd_nuc505.c 0 0 1529 157 1686 +0.0%
dcd_rp2040.c 859 20 604 655 2138 +0.0%
dcd_rusb2.c 2917 0 0 156 3073 +0.0%
dcd_samd.c 1032 0 0 266 1298 +0.0%
dcd_samg.c 1319 0 0 72 1391 +0.0%
dcd_stm32_fsdev.c 2557 0 0 291 2848 +0.0%
dfu_device.c 744 28 712 183 926 +0.0%
dfu_rt_device.c 156 0 134 0 156 +0.0%
dwc2_common.c 602 30 0 0 618 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +0.0%
ehci.c 2761 0 0 5970 7537 +0.0%
fsdev_common.c 180 0 0 0 180 +0.0%
hcd_ch32_usbfs.c 2484 0 0 498 2982 +0.0%
hcd_ci_hs.c 190 0 0 0 190 +0.0%
hcd_dwc2.c 4994 33 1 513 5540 +0.0%
hcd_khci.c 2442 0 0 449 2891 +0.0%
hcd_musb.c 3073 0 0 157 3230 +0.0%
hcd_pio_usb.c 262 0 240 0 502 +0.0%
hcd_rp2040.c 976 73 416 384 1849 +0.0%
hcd_rusb2.c 2923 0 0 245 3168 +0.0%
hcd_samd.c 2220 0 0 324 2544 +0.0%
hcd_stm32_fsdev.c 3287 0 1 420 3708 +0.0%
hid_device.c 1118 44 997 115 1233 +0.0%
hid_host.c 1206 0 0 1250 2456 +0.0%
hub.c 1235 8 8 29 1269 +0.0%
midi_host.c 1353 7 7 3740 5097 +0.0%
msc_device.c 2518 108 2286 538 3056 +0.0%
msc_host.c 1589 0 0 394 1984 +0.0%
mtp_device.c 1689 22 1449 579 2275 +0.0%
ncm_device.c 1514 28 1408 5830 7358 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rp2040_usb.c 172 75 718 4 969 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb_fifo.c 844 0 477 0 838 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd.c 3191 57 89 276 3532 +0.0%
usbd_control.c 523 0 474 78 600 +0.0%
usbh.c 4562 55 100 964 5647 +0.0%
usbtmc_device.c 2176 24 69 291 2500 +0.0%
vendor_device.c 624 0 530 464 1087 +0.0%
video_device.c 4391 5 1851 472 4855 +0.0%
TOTAL 111950 ➙ 111951 (+1) 1155 15421 44929 159010 +0.0%

@hathach hathach force-pushed the improve-iar-warnings branch from 0e0e920 to 94baf39 Compare March 6, 2026 04:26
@hathach hathach merged commit 06a4c6d into master Mar 6, 2026
499 of 503 checks passed
@hathach hathach deleted the improve-iar-warnings branch March 6, 2026 04:57
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