Menu

#2864 _Generic detects the wrong data types

closed-fixed
None
Front-end
5
2019-03-29
2019-01-10
No

The _Generic keyword is supposed to select at compile time diffent code blocks depending on the data type of given argument.

It is supposed to distinguish between char and int, between signed and unsigned and between constant values and variables. This example code is supposed to compile as func_var(mode):

void func_uint8(const unsigned char);
void func_int8(const unsigned char);
void func_uint16(const unsigned char);
void func_int16(const unsigned char);
void func_var(const unsigned char);

unsigned char mode;

void test_generic(void)
{
_Generic(mode,
    const unsigned char:        func_uint8(mode),
    const char:                 func_int8(mode),
    const unsigned int:         func_uint16(mode),
    const int:                  func_int16(mode),
    default:                    func_var(mode)
);
}

It works with gcc, but fails for sdcc:

$ sdcc -v
SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.8.3 #10694 (Linux)
published under GNU General Public License (GPL)
$ sdcc -mstm8 -c test_generic.c
test_generic.c:16: error 228: multiple matching expressions in generic association

Problem is that mode falsely maches the first two branches for const unsigned char and const char in addition to the expected default branch. Apparently, it ignores the const and the unsigned attribute and only acts on the char type.

Removing one of them compiles, but results in false code referencing func_uint8() or func_int8().
Removing both lines compiles correctly to the expected result referencing func_var().

Discussion

  • Philipp Klaus Krause

    This looks like a duplicate of bug [#2751] to me.

    Philipp

     

    Related

    Bugs: #2751


    Last edit: Philipp Klaus Krause 2019-03-29
  • Michael Mayer

    Michael Mayer - 2019-01-10

    Kind of, yes. signed/unsigned is one problem, const/not-const is another. But it sound quite likely that they both have the same root cause.

     
  • Philipp Klaus Krause

    SDCC is correct in ignoring the const. As the WG14 comittee response to DR 481 states, the type of the controlling expression for _Generic is considered as if it had undergone lvalue conversion. So qualifiers, such as const, are dropped.

    Philipp

     
  • Philipp Klaus Krause

    • status: open --> closed-fixed
    • assigned_to: Philipp Klaus Krause
    • Category: other --> Front-end
     
  • Philipp Klaus Krause

    The char signednedd issue is fixed in [r11150].

    Philipp

     

Log in to post a comment.

MongoDB Logo MongoDB