[cfe-dev] Best approach for duplicate case value error

Philip Reames listmail at philipreames.com
Mon Mar 5 10:35:31 PST 2012


Over the weekend, I took a look at an easy diagnostic bug. 
(http://llvm.org/bugs/show_bug.cgi?id=9243)  Essentially, the issue 
comes down to how to report duplicate case values when some of the 
values are enums or chars.  As shown today, we show the actual integer 
value of the case value.  This is sometimes confusing.

My proposed syntax for the revised error is:
CGDecl.cpp:85:8: error: duplicate case for 'Decl::Label' (underlying 
value '6')
   case Decl::Label:        // __label__ x;
        ^
CGDecl.cpp:73:8: note: previous case defined here
   case Decl::Label:

After some digging around, I couldn't find a way to get the actual text 
of a given source range.  Is there an easy way (within Sema) to get 
this?  I tried playing tricks with looking up enum values from the 
underlying integer, but that has edge cases I wouldn't want to actually 
check in.

The second question I had was whether we should add a warning for the 
use of non-enum labels when switching on an enum value.  While this is 
certainly valid C/C++, it's probably not best practice.  Are best 
practice style warnings something we want to include?  Or is that better 
off in a separate tool?

Yours,
Philip Reames


Here's the c++ test case I've been using.

enum Foo {A=0,B};

int main() {
   Foo a = A;
   switch(a) {
   case A:
   case A:
     break;
   };

   switch(a) {
   case A:
   case 0:
     break;
   };

   switch(a) {
   case 0:
   case 1:
     break;
   };

   switch(a) {
   case A:
   case '\0':
     break;
   };

   char b = '0';
   switch(b) {
   case 'a':
   case 'a':
     break;
   };

#define CASE1 case 'a':
   switch(b) {
   CASE1
   CASE1
     break;
   };

   return 0;
}



More information about the cfe-dev mailing list