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

            Bug ID: 89962
           Summary: likely/unlikely attributes don't work on a
                    compound-statement
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
                CC: jason at gcc dot gnu.org
  Target Milestone: ---

int signum(int i)
{
  if (i > 0) [[likely]]
    return 1;
  if (i < 0) [[likely]]
  {
    return -1;
  }
  return 0;
}

The first attribute works fine, the second doesn't:

f.cc: In function 'int signum(int)':
f.cc:5:14: warning: attributes at the beginning of statement are ignored
[-Wattributes]
    5 |   if (i < 0) [[likely]]
      |              ^~~~~~~~~~


The example from [dcl.attr.likelihood] similarly warns:

void g(int);

int f(int n) {
  if (n > 5) [[unlikely]] { // n > 5 is considered to be arbitrarily unlikely
    g(0);
    return n * 2 + 1;
  }

  switch (n) {
  case 1:
    g(1);
    [[fallthrough]];

  [[likely]] case 2: // n == 2 is considered to be arbitrarily more
    g(2);            // likely than any other value of n
    break;
  }
  return 3;
}

f.cc: In function 'int f(int)':
f.cc:3:14: warning: attributes at the beginning of statement are ignored
[-Wattributes]
    3 |   if (n > 5) [[unlikely]] {  // n > 5 is considered to be arbitrarily
unlikely
      |              ^~~~~~~~~~~~

Reply via email to