https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62074

            Bug ID: 62074
           Summary: "right shift count >= width of type" warning on dead
                    branch in template code
           Product: gcc
           Version: 4.10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: brooks at gcc dot gnu.org

Consider the following code:

------------------cut here---------------------
typedef unsigned int uint32;

template <uint32 offset>
int foo(uint32 x) {
  if (offset <= 1) {
    return x << offset;
  } else {
    // this branch is dead for offset = 0, but it still generates a warning
    return (x << (offset - 1)) +
           (x >> (32 - offset));
  }
}

int main(int argc, char** argv) {
  return foo<0>(argc);
}
------------------cut here---------------------

As noted in the comment, the "else" branch is dead code for the offset=0
instantiation, but (at r213772 on trunk) we still generate warnings for the
shifts that are out of range in the offset=0 case:

$ /home/bmoses/gcc-archive/213772/bin/g++ b16825807.cc -o b16825807
b16825807.cc: In instantiation of ‘int foo(uint32) [with unsigned int offset =
0u; uint32 = unsigned int]’:
b16825807.cc:15:21:   required from here
b16825807.cc:9:15: warning: left shift count >= width of type
     return (x << (offset - 1)) +
               ^
b16825807.cc:10:15: warning: right shift count >= width of type
            (x >> (32 - offset));
               ^

(This is also present in the google/gcc-4_9 branch, and thus I suspect present
on upstream 4.9.1 although I have not yet tested it.)

Google bug: 16825807

Reply via email to