[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


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

            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
[lib/Sema/SemaExpr.cpp](https://github.com/llvm-mirror/clang/blob/9a1877f04c1a225ff4acae5524d18be91f7fba3f/lib/Sema/SemaExpr.cpp#L10383-L10395):

```
  // 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)
    return;

```

But, in
[lib/Parse/ParseStmt.cpp](https://github.com/llvm-mirror/clang/blob/9a1877f04c1a225ff4acae5524d18be91f7fba3f/lib/Parse/ParseStmt.cpp#L1532-L1549),
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
expressions.

-- 
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