Page 1 of 1

xtensa compiler: case label does not reduce to an integer constant

Posted: Wed Apr 27, 2022 2:09 pm
by danpf1
Hello,

When compiling the simple program below with a macro as a case label, I receive the error:
case label does not reduce to an integer constant
The error only happens with the xtensa-esp32s3-elf-gcc compiler. This does not happen with plain gcc.

Code: Select all

#include <stdio.h>
// #include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <string.h>

#define HTST(s, i, x) (s[2])

int main() {

  switch(12) {
     case HTST("anything", 0, 0):
        printf("anything!");
        break;
     default:
        printf("DEFAIULT");
  }
}
Compile command:

Code: Select all

> xtensa-esp32s3-elf-gcc t.c
t.c: In function 'main':
t.c:12:6: error: case label does not reduce to an integer constant
      case HTST("anything", 0, 0):
      ^~~~
GCC compile shows no warnings or errors.

This is on a Mac OSX 12.3.1
Xtensa compiler: xtensa-esp32s3-elf-gcc (crosstool-NG esp-2021r2) 8.4.0
Gcc compiler: Apple clang version 13.1.6 (clang-1316.0.21.2.3)

Thoughts?

Thanks!

Re: xtensa compiler: case label does not reduce to an integer constant

Posted: Wed Apr 27, 2022 5:08 pm
by ESP_igrr
Hi danpf1,

I'm afraid what you are running into isn't an Xtensa specific behavior. This error is also reported by GCC on other architectures. Here's the example with latest GCC (trunk) on x86_64: https://godbolt.org/z/EsK8qja5v

As you have found, clang does allow such a construct, indeed https://godbolt.org/z/jKPx41nd1.

GCC accepts this only when compiling C++ code: https://godbolt.org/z/Wce8hab7M

Re: xtensa compiler: case label does not reduce to an integer constant

Posted: Wed Apr 27, 2022 6:19 pm
by danpf1
Thanks for the thorough and quick response! I'll look for some workaround for what I'm doing.

- Dan