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

When using PlatformIO I get warning about -Wnarrowing #91

Open
EdgarBarranco opened this issue Oct 23, 2022 · 1 comment
Open

When using PlatformIO I get warning about -Wnarrowing #91

EdgarBarranco opened this issue Oct 23, 2022 · 1 comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project

Comments

@EdgarBarranco
Copy link

I am using Visual Studio Code 1.72.2 and PlatformIO 6.1.4. The board I am using is the Sparkfun ProMicro16 based on the 32u4. When compiling the example MIDIUSB_write.ino, I get the following warnings:

Processing micro (platform: atmelavr; board: sparkfun_promicro16; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/sparkfun_promicro16.html
PLATFORM: Atmel AVR (4.0.0) > SparkFun Pro Micro 5V/16MHz
HARDWARE: ATMEGA32U4 16MHz, 2.50KB RAM, 28KB Flash
DEBUG: Current (simavr) On-board (simavr)
PACKAGES:
 - framework-arduino-avr @ 5.1.0
 - tool-avrdude @ 1.60300.200527 (6.3.0)
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 7 compatible libraries
Scanning dependencies...
Dependency Graph
|-- MIDIUSB @ 1.0.5
Building in release mode
Compiling .pio\build\micro\liba61\MIDIUSB\MIDIUSB.cpp.o
Compiling .pio\build\micro\src\main.cpp.o
src\main.cpp: In function 'void noteOn(byte, byte, byte)':
src\main.cpp:11:42: warning: narrowing conversion of '(int)(144 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
   midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
                                     ~~~~~^~~~~~~~~
src\main.cpp: In function 'void noteOff(byte, byte, byte)':
src\main.cpp:16:43: warning: narrowing conversion of '(int)(128 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
   midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
                                      ~~~~~^~~~~~~~~
src\main.cpp: In function 'void controlChange(byte, byte, byte)':
src\main.cpp:30:41: warning: narrowing conversion of '(int)(176 | ((unsigned char)((int)channel)))' from 'int' to 'uint8_t {aka unsigned char}' inside { } [-Wnarrowing]
   midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
                                    ~~~~~^~~~~~~~~
Linking .pio\build\micro\firmware.elf
Checking size .pio\build\micro\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   8.3% (used 213 bytes from 2560 bytes)
Flash: [==        ]  15.9% (used 4562 bytes from 28672 bytes)
Building .pio\build\micro\firmware.hex

Doing a little reading about these warnings I see that in the c99 standards ISO/IEC 9899:1999 section "6.5.11 Bitwise exclusive OR operator" under constrains says "Each of the operands shall have integer type."

Digging through the code, the file MIDIUSB_Defs.h on lines 6 through 9 declares each element of the struct midiEventPacket_t as "uint8_t" which is fine in this case because of the range it should have, but the type should be uint16_t to comply with the standard. Making the following changes to this file clear the warnings:

#pragma once
#include <stdint.h>

typedef struct
{
	uint16_t header;
	uint16_t byte1;
	uint16_t byte2;
	uint16_t byte3;
} midiEventPacket_t;

In my case, these changes do not affect the functionality of the code. I only have this board with the 32u4 to test, so I don't know if it works on other boards, but I don't see why not.

I don't see these warnings using the Arduino IDE 1.8.19 but this is because they added "-Wno-error=narrowing" to the compilation flags which hides the warning issues, but does not solve the problem.

@per1234 per1234 added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels Oct 23, 2022
@fab672000
Copy link
Contributor

fab672000 commented Oct 23, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

3 participants