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

danpf1
Posts: 14
Joined: Mon May 10, 2021 2:59 pm

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

Postby danpf1 » Wed Apr 27, 2022 2:09 pm

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!

ESP_igrr
Posts: 2072
Joined: Tue Dec 01, 2015 8:37 am

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

Postby ESP_igrr » Wed Apr 27, 2022 5:08 pm

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

danpf1
Posts: 14
Joined: Mon May 10, 2021 2:59 pm

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

Postby danpf1 » Wed Apr 27, 2022 6:19 pm

Thanks for the thorough and quick response! I'll look for some workaround for what I'm doing.

- Dan

Who is online

Users browsing this forum: No registered users and 104 guests