How to find out if ESP-IDF framework is used using the preprocessor (PlatformIO)

You can use How to print all preprocessor flags in PlatformIO to print preprocessor flags. You can find all ESP-IDF related flags using

cat .pio/build/esp32dev/src/main.o | grep IDF

which is - with an empty main.c file just

#define IDF_VER "5.1.2"

Note that IDF_VER is also defined for Arduino since it is used internally by Arduino.

So you can use the following check which distinguistes between Arduino & ESP-iDF

#if !defined(ARDUINO) && defined(IDF_VER)
  // ESP-IDF code goes here
  // Non-ESP-IDF code goes here