Generate defines in c_cpp_properties.json for clang-tidy in vscode-cpptools 1.8.0

I’m trying to use new vscode-cpptools 1.8.0 (insider2) with integrated clang-tidy. And I was able to make it work but it requires some manual work.

The problem is when I run clang-tidy without modification than it has not enough flags to run correctly. So I run verbose pio check to see how it is done by pio itself and it seems that there are a lot of D__* flags added

/Users/lukas/.platformio/packages/tool-clangtidy/clang-tidy --quiet --checks=* /Users/lukas/dev/projects/sauna_meter/src/main.cpp -- -DPLATFORMIO=50203 -DESP8266 -DARDUINO_ARCH_ESP8266 -DARDUINO_ESP8266_WEMOS_D1MINIPRO -DF_CPU=80000000L -D__ets__ -DICACHE_FLASH -DARDUINO=10805 -DARDUINO_BOARD="PLATFORMIO_D1_MINI_PRO" -DFLASHMODE_DIO -DLWIP_OPEN_SRC -DNONOSDK22x_190703=1 -DTCP_MSS=536 -DLWIP_FEATURES=1 -DLWIP_IPV6=0 -DVTABLES_IN_FLASH -DMMU_IRAM_SIZE=0x8000 -DMMU_ICACHE_SIZE=0x8000 -D__DBL_MIN_EXP__=(-1021) -D__cpp_attributes=200809L -D__cpp_nontype_template_parameter_auto=201606L -D__UINT_LEAST16_MAX__=0xffff -D__ATOMIC_ACQUIRE=2 -D__FLT_MIN__=1.1754943508222875e-38F -D__GCC_IEC_559_COMPLEX=0 -D__cpp_aggregate_nsdmi=201304L -D__UINT_LEAST8_TYPE__=unsigned char -D__INTMAX_C(c)=c ## LL -D__CHAR_BIT__=8 -D__UINT8_MAX__=0xff -D__WINT_MAX__=0xffffffffU -D__FLT32_MIN_EXP__=(-125) -D__cpp_static_assert=201411L -D__ORDER_LITTLE_ENDIAN__=1234
... and a lot of others

So I copied this command, do some parsing and I put all of these flags into c_cpp_properties.json in defines section.

"defines": [
>>> generated by PIO
        "PLATFORMIO=50203",
        "ESP8266",
        "ARDUINO_ARCH_ESP8266",
        "ARDUINO_ESP8266_WEMOS_D1MINIPRO",
        "F_CPU=80000000L",
        "__ets__",
        "ICACHE_FLASH",
        "ARDUINO=10805",
        "ARDUINO_BOARD=\"PLATFORMIO_D1_MINI_PRO\"",
        "FLASHMODE_DIO",
        "LWIP_OPEN_SRC",
        "NONOSDK22x_190703=1",
        "TCP_MSS=536",
        "LWIP_FEATURES=1",
        "LWIP_IPV6=0",
        "VTABLES_IN_FLASH",
        "MMU_IRAM_SIZE=0x8000",
        "MMU_ICACHE_SIZE=0x8000",
>>> added by ME
        "__DBL_MIN_EXP__=(-1021)",
        "__cpp_attributes=200809L",
        "__cpp_nontype_template_parameter_auto=201606L",
        "__UINT_LEAST16_MAX__=0xffff",
... and a lot of others

Than I set executable to pio clang-tidy and it start working unless properties are regenerated (its not a big deal, I can write some watcher and replace it again).

Is it possible to generate defines with this flags automatically ? Or to integrate it in more elegant way ?

No, these are not added by PlatformIO. They are inherintly auto-added by the compiler when compiling any C/C++ file. See e.g. GitHub - maxgerhardt/pio-check-flags: An example project that shows how to check if a certain macro is active in a file. in relation to this.

These automatically added compiler defines are not exposed in the c_cpp_properties.json, since the compiler used by the code inspection / intellisense always known about them.

If you think they should be exposed, open an issue Issues · platformio/platformio-core · GitHub.

And why not use pio check that calls into clang-tidy with all the correct flags?

1 Like

I didn’t find a way of integrating pio check into vscode. Maybe I just missed something. With this integration of clang-tidy into vscode cpp I can see the results almost on the fly.

Anyway if these flags are automatically added by a compiler, then I think that it should be handled by cpptools and not by pio.

Thank you for clarification.

The Inspect tool in the PIO home interface’s left sidebar brings you directly into a graphical user-interface for this.

I meant directly into editor. Like with this option.

It seems that I made a mistake with .clang-tidy config in first place and the compiler flags are not necessary to get it works. It will produce some errors without the flags when running clang-tidy but the analysis is done and the results are shown in editor.