esp-idf: portGET_ARGUMENT_COUNT() defintion broken for C++ (IDFGH-4021)

Environment

  • Development Kit: none
  • Module or chip used: ESP32-WROOM-32
  • IDF version (run git describe --tags to find it): v4.3-dev-1197-g8bc19ba89
  • Build System: idf.py
  • Compiler version (run xtensa-esp32-elf-gcc --version to find it): xtensa-esp32-elf-gcc (crosstool-NG esp-2020r3) 8.4.0
  • Operating System: Linux
  • Using an IDE?: No
  • Power Supply: USB

Problem Description

The macro portGET_ARGUMENT_COUNT() in portmacro.h line 331 seems to be broken when used with C++. Have a look at the demonstration here. With 0 arguments, it returns 1 instead of 0. This triggers the first static assertion directly below the definition:

_Static_assert(portGET_ARGUMENT_COUNT() == 0, "portGET_ARGUMENT_COUNT() result does not match for 0 arguments");

As can be seen here, this works when instead run with C.

Code to reproduce this issue

#include <iostream>

#define portGET_ARGUMENT_COUNT(...) portGET_ARGUMENT_COUNT_INNER(0, ##__VA_ARGS__,1,0)
#define portGET_ARGUMENT_COUNT_INNER(zero, one, count, ...) count

int main() {
    std::cout << portGET_ARGUMENT_COUNT() << std::endl;
    std::cout << portGET_ARGUMENT_COUNT(1) << std::endl;
    std::cout << portGET_ARGUMENT_COUNT(1,2) << std::endl;
    std::cout << "###" << std::endl;

    return 0;
}

Log

I am using a main.cpp in my main component. The include tree looks as follows:

In file included from <esp32_path>/tools/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/xtensa-esp32-elf/sys-include/stdlib.h:19,
                 from <esp32_path>/esp32/tools/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/8.4.0/cstdlib:75,
                 from <esp32_path>/esp32/tools/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/8.4.0/stdlib.h:36,
                 from <esp32_path>/esp32/esp-idf/components/freertos/xtensa/include/freertos/FreeRTOSConfig.h:122,
                 from <esp32_path>/esp32/esp-idf/components/freertos/include/freertos/FreeRTOS.h:101,
                 from ../main/main.cpp:9:
                 
<esp32_path>/esp-idf/components/freertos/xtensa/include/freertos/portmacro.h:334:41: error: static assertion failed: portGET_ARGUMENT_COUNT() result does not match for 0 arguments
 _Static_assert(portGET_ARGUMENT_COUNT() == 0, "portGET_ARGUMENT_COUNT() result does not match for 0 arguments");

Am I doing something wrong here?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 27 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Ah! component_compile_options(-std=gnu++11) and component_compile_options(-std=gnu++17) indeed seem to work. Thanks! I think this might be the description of the differing behavior causing this error.

Ahh, sorry! We actually use -std=gnu++11 and this is what we test with. Interesting. Are you able to use the gnu++11 standard for your projects? When I explicitly set -std=c++11, I also experience this error.

@seijikun Thanks! I can reproduce the error. Will have a look. Please keep in mind that we don’t currently support any other standard than C++11.

Here is a working portGET_ARGUMENT_COUNT() (named as portGET_ARGUMENT_COUNT_ALT). Tested with C, C++, C++11, C++17.