This is an archive of the discontinued LLVM Phabricator instance.

Some heuristics to identify c style casting (PR18937)
ClosedPublic

Authored by dinesh.d on Apr 30 2014, 2:19 PM.

Details

Summary

I have added few heuristics to identify c style casting. Only side
effect of this patch is that single identifier wrapped with parentheses
(identifier) are getting treated as cast.

Diff Detail

Event Timeline

dinesh.d updated this revision to Diff 8991.Apr 30 2014, 2:19 PM
dinesh.d retitled this revision from to Some heuristics to identify c style casting (PR18937).
dinesh.d updated this object.
dinesh.d edited the test plan for this revision. (Show Details)
dinesh.d added reviewers: djasper, klimek.
dinesh.d added a subscriber: Unknown Object (MLST).
djasper edited edge metadata.May 5 2014, 3:11 AM

Thank you for working on this!

lib/Format/TokenAnnotator.cpp
757

Please add:

// FIXME: Pull cast detection into its own function.
790

Please use {} for the if, if there are {} for the else.

792

I'd prefer to put this comment on its own line.

And please make all comments here full sentences.

799

I think we need to check that Current.Next is not null. Otherwise, this might crash if a line ends in a right parenthesis.

On the other hand, I don't think "Prev &&" changes the behavior.

How about writing this as:

if (Current.Next && Current.Next->Next) {
  bool NextIsUnary = Current.Next->isUnaryOperator() ||
                     Current.Next->isOneOf(tok::amp, tok::star);
  IsCast = NextIsUnary && Current.Next->Next->isOneOf(
                              tok::identifier, tok::numeric_constant);
}

I think then it is almost self-explanatory and you don't need the comment.

806

What about:

  • * (pointers)
  • (arrays)
  • <> (templates)

?

808

Further up in this function, we are using a for-loop for this, i.e.

for ( ; Prev != Current.MatchingParen; Prev = Prev->Previous) { ..

I think we should be consistent.

dinesh.d updated this revision to Diff 9068.May 5 2014, 5:22 AM
dinesh.d edited edge metadata.

updated patch as per comments

lib/Format/TokenAnnotator.cpp
757

added comment

790

updated

799

updated. I am setting IsCast to true and then if it is not identified as cast, it is getting resetted.
"(Prev && .." was there to keep IsCast becomming true as it will not get reset later.

I am not sure if I have to check for Prev or can assume that it will always be non-null here.

806

I did not get it. Do you mean something like *(my_int_ptr)+2 etc. I will look for other
casting example and try to handle them in patch.

If you have anything specific in mind, which should be handled, please suggest.

808

updated

djasper accepted this revision.May 5 2014, 5:57 AM
djasper edited edge metadata.

Two nits, otherwise looks good.

lib/Format/TokenAnnotator.cpp
794

nit: Period at the end of a sentence.

806

I mean casts like:

vector<string>* v = (vector<string>*)x;

But nevermind, I think then we don't even get into this branch.

807

nit: I'd prefer {} here..

This revision is now accepted and ready to land.May 5 2014, 5:57 AM
dinesh.d updated this revision to Diff 9069.May 5 2014, 6:04 AM
dinesh.d edited edge metadata.

updated as per comments

dinesh.d added inline comments.May 5 2014, 6:06 AM
lib/Format/TokenAnnotator.cpp
794

updated

807

updated

dinesh.d closed this revision.May 5 2014, 11:10 PM

submitted as r207964.

Thanks for review and support.