[cfe-dev] Broken sizeof(expr) location in SemaExpr.cpp

Xi Wang xi.wang at gmail.com
Sat May 28 07:29:17 PDT 2011


Hi,

Try `clang -cc1 -ast-dump' on the following case using the trunk
version, where both sizeof statements should start at column 3.

void foo(int i)
{
  sizeof(i);
  sizeof(int);
}

Clang outputs

  (UnaryExprOrTypeTraitExpr ... <line:3:9, col:11> 'unsigned long' sizeof
  ...
  (UnaryExprOrTypeTraitExpr ... <line:4:3, col:13> 'unsigned long'
sizeof 'int'))

Note that when sizeof is applied to an expression, sizeof(i), the
column number is 9, rather than 3; sizeof(int) is still good.

I looked through the recent commits.  In rev. 132115, at
lib/Sema/SemaExpr.cpp:3265, the interface of
Sena::CreateUnaryExprOrTypeTraitExpr was changed --- it no longer
takes OpLoc as an argument when sizeof is applied to an expression.
At line 3290, when creating UnaryExprOrTypeTraitExpr, the code uses
E->getExprLoc() as the location of sizeof.  This means that in the
example above, the OpLoc of sizeof(i) was actually that of i.

I noticed this change because it bit my Clang-based source rewriter.
Is this intentional or a bug?  Thanks.

- xi



More information about the cfe-dev mailing list