以上介绍的介绍的几个GCC扩展特性, 共程序员们参考, 是否利用扩展, 取决于自己的选择.
事实上很多厂商提供的驱动代码中也有利用一些GCC扩展, 有些GCC扩展特性应用很广泛, clang, armcc等编译器也能支持. 以STM32CubeIDE为例, 可以打开-pedantic选项, 看整个工程应用了哪些gcc扩展.
打开这个选项之后, 重新build工程, 可以看到对于以上故意利用扩展的例子, 编译器会给出详细的warning.
- ../Core/Src/main.c:97:10: warning: ISO C forbids zero-size array 'content' [-Wpedantic]
- uint8_t content[0];
- ^~~~~~~
- ../Core/Src/main.c: In function 'ranges':
- ../Core/Src/main.c:102:2: warning: range expressions in switch statements are non-standard [-Wpedantic]
- case '0' ... '9':
- ^~~~
- ../Core/Src/main.c:103:2: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
- printf("[%c] is a number.\n", c);
- ^~~~~~
- ../Core/Src/main.c:103:2: warning: incompatible implicit declaration of built-in function 'printf'
- ../Core/Src/main.c:103:2: note: include '<stdio.h>' or provide a declaration of 'printf'
- ../Core/Src/main.c:106:2: warning: range expressions in switch statements are non-standard [-Wpedantic]
- case 'a' ... 'z':
- ^~~~
- ../Core/Src/main.c:110:2: warning: range expressions in switch statements are non-standard [-Wpedantic]
- case 'A' ... 'Z':
- ^~~~
- ../Core/Src/main.c: At top level:
- ../Core/Src/main.c:125:8: warning: struct has no members [-Wpedantic]
- struct empty {
- ^~~~~
- ../Core/Src/main.c: In function 'main':
- ../Core/Src/main.c:137:2: warning: ISO C forbids nested functions [-Wpedantic]
- void nested_func(void) {
- ^~~~
- ../Core/Src/main.c: In function 'nested_func':
- ../Core/Src/main.c:138:3: warning: incompatible implicit declaration of built-in function 'printf'
- printf("Inside a nested function!\n");
- ^~~~~~
- ../Core/Src/main.c:138:3: note: include '<stdio.h>' or provide a declaration of 'printf'
- ../Core/Src/main.c: In function 'main':
- ../Core/Src/main.c:169:2: warning: incompatible implicit declaration of built-in function 'printf'
- printf("BOR: %u\n", __HAL_RCC_GET_FLAG(RCC_FLAG_BORRST));
- ^~~~~~
- ../Core/Src/main.c:169:2: note: include '<stdio.h>' or provide a declaration of 'printf'
- ../Core/Src/main.c:193:31: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
- msg = (struct msg_header *) malloc(sizeof (struct msg_header) + size);
- ^~~~~~
- ../Core/Src/main.c:193:31: warning: incompatible implicit declaration of built-in function 'malloc'
- ../Core/Src/main.c:193:31: note: include '<stdlib.h>' or provide a declaration of 'malloc'
- ../Core/Src/main.c:200:3: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration]
- free(msg);
- ^~~~
- ../Core/Src/main.c:200:3: warning: incompatible implicit declaration of built-in function 'free'
- ../Core/Src/main.c:200:3: note: include '<stdlib.h>' or provide a declaration of 'free'
- ../Core/Src/main.c:121:3: warning: ISO C forbids braced-groups within expressions [-Wpedantic]
- ({ typeof (a) _a = (a); \
- ^
- ../Core/Src/main.c:219:31: note: in expansion of macro 'max'
- printf("char max is %d\n", max(a,b));
- ^~~
- ../Core/Src/main.c:121:3: warning: ISO C forbids braced-groups within expressions [-Wpedantic]
- ({ typeof (a) _a = (a); \
- ^
- ../Core/Src/main.c:220:31: note: in expansion of macro 'max'
- printf("int max is %d\n", max(x,y));
- ^~~
- ../Core/Src/main.c:121:3: warning: ISO C forbids braced-groups within expressions [-Wpedantic]
- ({ typeof (a) _a = (a); \
- ^
- ../Core/Src/main.c:221:31: note: in expansion of macro 'max'
- printf("float max is %f\n", max(j,k));
- ^~~
- ../Core/Src/main.c:236:11: warning: ISO C forbids omitting the middle term of a ?: expression [-Wpedantic]
- a = x ? : 0b0011;
- ^
- ../Core/Src/main.c:236:13: warning: binary constants are a GCC extension
- a = x ? : 0b0011;
- ^~~~~~
- ../Core/Src/main.c:243:28: warning: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'uint32_t {aka long unsigned int}' [-Wformat=]
- printf("Started ADC DMA, %u\n", SystemCoreClock);
- ~^
- %lu
- arm-none-eabi-gcc "../Core/Src/stm32g4xx_it.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32G431xx -DARM_MATH_DSP -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DDEBUG -c -I../Drivers/STM32G4xx_HAL_Driver/Inc/Legacy -I"F:/stm32_ide_prj/stm32g431_t1_stm32ide/Core/Src/STM32G4xx_Nucleo" -I../Drivers/CMSIS/Include -I../Drivers/CMSIS/Device/ST/STM32G4xx/Include -I../Core/Inc -I../Drivers/STM32G4xx_HAL_Driver/Inc -O0 -ffunction-sections -fdata-sections -Wall -pedantic -fstack-usage -MMD -MP -MF"Core/Src/stm32g4xx_it.d" -MT"Core/Src/stm32g4xx_it.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/stm32g4xx_it.o"
- arm-none-eabi-gcc "../Core/Src/syscalls.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32G431xx -DARM_MATH_DSP -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DDEBUG -c -I../Drivers/STM32G4xx_HAL_Driver/Inc/Legacy -I"F:/stm32_ide_prj/stm32g431_t1_stm32ide/Core/Src/STM32G4xx_Nucleo" -I../Drivers/CMSIS/Include -I../Drivers/CMSIS/Device/ST/STM32G4xx/Include -I../Core/Inc -I../Drivers/STM32G4xx_HAL_Driver/Inc -O0 -ffunction-sections -fdata-sections -Wall -pedantic -fstack-usage -MMD -MP -MF"Core/Src/syscalls.d" -MT"Core/Src/syscalls.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/syscalls.o"
- arm-none-eabi-gcc "../Core/Src/sysmem.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32G431xx -DARM_MATH_DSP -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DDEBUG -c -I../Drivers/STM32G4xx_HAL_Driver/Inc/Legacy -I"F:/stm32_ide_prj/stm32g431_t1_stm32ide/Core/Src/STM32G4xx_Nucleo" -I../Drivers/CMSIS/Include -I../Drivers/CMSIS/Device/ST/STM32G4xx/Include -I../Core/Inc -I../Drivers/STM32G4xx_HAL_Driver/Inc -O0 -ffunction-sections -fdata-sections -Wall -pedantic -fstack-usage -MMD -MP -MF"Core/Src/sysmem.d" -MT"Core/Src/sysmem.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/sysmem.o"
- arm-none-eabi-gcc "../Core/Src/system_stm32g4xx.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32G431xx -DARM_MATH_DSP -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DDEBUG -c -I../Drivers/STM32G4xx_HAL_Driver/Inc/Legacy -I"F:/stm32_ide_prj/stm32g431_t1_stm32ide/Core/Src/STM32G4xx_Nucleo" -I../Drivers/CMSIS/Include -I../Drivers/CMSIS/Device/ST/STM32G4xx/Include -I../Core/Inc -I../Drivers/STM32G4xx_HAL_Driver/Inc -O0 -ffunction-sections -fdata-sections -Wall -pedantic -fstack-usage -MMD -MP -MF"Core/Src/system_stm32g4xx.d" -MT"Core/Src/system_stm32g4xx.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/system_stm32g4xx.o"
- arm-none-eabi-gcc "../Core/Src/usart.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DSTM32G431xx -DARM_MATH_DSP -DUSE_NUCLEO_64 -DUSE_HAL_DRIVER -DDEBUG -c -I../Drivers/STM32G4xx_HAL_Driver/Inc/Legacy -I"F:/stm32_ide_prj/stm32g431_t1_stm32ide/Core/Src/STM32G4xx_Nucleo" -I../Drivers/CMSIS/Include -I../Drivers/CMSIS/Device/ST/STM32G4xx/Include -I../Core/Inc -I../Drivers/STM32G4xx_HAL_Driver/Inc -O0 -ffunction-sections -fdata-sections -Wall -pedantic -fstack-usage -MMD -MP -MF"Core/Src/usart.d" -MT"Core/Src/usart.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/usart.o"
- ../Core/Src/syscalls.c:41:1: warning: file-scope declaration of 'stack_ptr' specifies 'register' [-Wpedantic]
- register char * stack_ptr asm("sp");
- ^~~~~~~~
- ../Core/Src/sysmem.c:30:1: warning: file-scope declaration of 'stack_ptr' specifies 'register' [-Wpedantic]
- register char * stack_ptr asm("sp");
- ^~~~~~~~
|