[llvm-bugs] [Bug 28847] New: -Wenum-compare doesn't catch mismatched enum value comparison in C mode

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 4 11:27:55 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=28847

            Bug ID: 28847
           Summary: -Wenum-compare doesn't catch mismatched enum value
                    comparison in C mode
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ibadawi at cisco.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

Consider this program (test.c)

enum foo_t {
    foo1, foo2, foo3
};

enum bar_t {
    bar1, bar2, bar3
};

int main(void) {
    enum foo_t foo = foo1;
    enum bar_t bar = bar1;

    int cmp1 = foo < bar; // variable - variable
    int cmp2 = foo < bar2; // variable - constant
    int cmp3 = foo1 < bar3; // constant - constant

    return cmp1 || cmp2 || cmp3;
}

If I compile this as C code, I only get a warning for the first comparison. If
I compile in C++ mode, I get a warning for all three.

$ clang -Wall test.c
test.c:13:20: warning: comparison of two values with different enumeration
types
      ('enum foo_t' and 'enum bar_t') [-Wenum-compare]
    int cmp1 = foo < bar;
               ~~~ ^ ~~~
1 warning generated.
$ clang -Wall -x c++ test.c
test.c:13:20: warning: comparison of two values with different enumeration
types
      ('enum foo_t' and 'enum bar_t') [-Wenum-compare]
    int cmp1 = foo < bar;
               ~~~ ^ ~~~
test.c:14:20: warning: comparison of two values with different enumeration
types
      ('enum foo_t' and 'bar_t') [-Wenum-compare]
    int cmp2 = foo < bar2;
               ~~~ ^ ~~~~
test.c:15:21: warning: comparison of two values with different enumeration
types ('foo_t' and 'bar_t')
      [-Wenum-compare]
    int cmp3 = foo1 < bar3;
               ~~~~ ^ ~~~~
3 warnings generated.


I think the warnings should be emitted in C mode too. Tested with gcc 4.9.2 and
3 warnings are emitted for C code:

$ gcc -Wall test.c
test.c: In function 'main':
test.c:13:20: warning: comparison between 'enum foo_t' and 'enum bar_t'
[-Wenum-compare]
     int cmp1 = foo < bar;
                    ^
test.c:14:20: warning: comparison between 'enum foo_t' and 'enum bar_t'
[-Wenum-compare]
     int cmp2 = foo < bar2;
                    ^
test.c:15:21: warning: comparison between 'enum foo_t' and 'enum bar_t'
[-Wenum-compare]
     int cmp3 = foo1 < bar3;
                     ^

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160804/334d4205/attachment.html>


More information about the llvm-bugs mailing list