[llvm-bugs] [Bug 32370] New: -Wcomma does not play well with -std=c89

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Mar 22 01:29:25 PDT 2017


            Bug ID: 32370
           Summary: -Wcomma does not play well with -std=c89
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: sneves at dei.uc.pt
                CC: llvm-bugs at lists.llvm.org

Beginning with an offending sample:

void f(void) {
  int i = 0, j = 0;
  for(;i < 10; ++i, ++j) {}


Compiling with `clang -std=c89 -Wcomma` we get

test.c:3:19: warning: possible misuse of comma operator here [-Wcomma]
  for(;i < 10; ++i, ++j) {}
test.c:3:16: note: cast expression to void to silence warning
  for(;i < 10; ++i, ++j) {}
               (void)( )

However, this example clearly belongs to the whitelisted class of comma
operator cases, and indeed in C++ and C99 modes clang behaves as expected.

I looked a little bit into why this happens, and the culprit appears to be the
following. The whitelisting of expressions happens in

  // Scope isn't fine-grained enough to whitelist the specific cases, so
  // instead, skip more than needed, then call back into here with the
  // CommaVisitor in SemaStmt.cpp.
  // The whitelisted locations are the initialization and increment portions
  // of a for loop.  The additional checks are on the condition of
  // if statements, do/while loops, and for loops.
  const unsigned ForIncrementFlags =
      Scope::ControlScope | Scope::ContinueScope | Scope::BreakScope;
  const unsigned ForInitFlags = Scope::ControlScope | Scope::DeclScope;
  const unsigned ScopeFlags = getCurScope()->getFlags();
  if ((ScopeFlags & ForIncrementFlags) == ForIncrementFlags ||
      (ScopeFlags & ForInitFlags) == ForInitFlags)


But, in
we have that these flags are only activated in C99 or C++ modes:

  // C99 6.8.5p5 - In C99, the for statement is a block.  This is not
  // the case for C90.  Start the loop scope.
  // ...
  unsigned ScopeFlags = 0;
  if (C99orCXXorObjC)
    ScopeFlags = Scope::DeclScope | Scope::ControlScope;

Therefore, it appears using these flags will not work in C90 to whitelist for

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/20170322/030aa05a/attachment.html>

More information about the llvm-bugs mailing list