[cfe-commits] [Request for review] Removing redundant implicit casts in the AST

Nicola Gigante nicola.gigante at gmail.com
Sat Oct 22 08:21:34 PDT 2011

Hello @clang

As mentioned in a previous thread early this month, I was working
on fixing a glitch in the AST representation of some kind of casts.

To recall, consider this example:

int func() {
  return int(0.5);

Clang currently builds an AST like this:
int func() (CompoundStmt 
   (CXXFunctionalCastExpr 'int' functional cast to int <NoOp>
     (ImplicitCastExpr  'int' <FloatingToIntegral>
       (FloatingLiteral  'double' 5.000000e-01)))))

As you can see, there's a spurious ImplicitCastExpr that imho shouldn't exist,
while the explicit cast node is marked as NoOp.
This behavior happens every time the user explicitly writes a cast
that could have been done implicitly, not only with functional casts
but also with CStyle and static casts.
This bogus implicit cast node introduces various problems with clients that need an
high degree of source-code fidelity in the AST.

Here is a patch that address the problem. The AST generated now for the previous
example is:

int func() (CompoundStmt 
   (CXXFunctionalCastExpr  'int' functional cast to int <FloatingToIntegral>
     (FloatingLiteral 'double' 5.000000e-01))))

This way, the AST is more compact and closer to the actual source code.
To fix this, I've modified one of the overloads of
the PerformImplicitConversion function.
Previously, it had a CheckedConversionKind argument that was passed
down to ImpCastExprToType(), which however always ignored it.
Now, that overload of PerformImplicitConversion is called PerformConversion,
and creates the right kinds of cast nodes following the CCK parameter.

The patch applies cleanly to a fairly recent clang revision (142493, a couple
of days ago), and passes all the tests.

I'd like to receive your feedback about both the issue itself and the
implementation of the fix.

Thank you,

-------------- next part --------------
A non-text attachment was scrubbed...
Name: cast.patch
Type: application/octet-stream
Size: 27529 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20111022/e5958ce9/attachment.obj>
-------------- next part --------------

More information about the cfe-commits mailing list