This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix format specifiers fixits
ClosedPublic

Authored by alexander-shaposhnikov on Jun 6 2017, 8:00 PM.

Details

Summary

This diff fixes printf "fixits" in the case when there is a wrapping macro and
the format string needs multiple replacements.
In the presence of a macro there was an extra logic in EditedSource.cpp
to handle multiple uses of the same macro argument
(see the old comment inside EditedSource::canInsertInOffset)
which was mistriggerred when the argument was used only once
but required multiple adjustments), as a result the "fixit" was breaking down the format string
by dropping the second format specifier, i.e.
Log1("test 4: %s %s", getNSInteger(), getNSInteger())
was getting replaced with
Log1("test 4: %ld ", (long)getNSInteger(), (long)getNSInteger())
(if one removed the macro and used printf directly it would work fine).
In this diff we track the location where the macro argument is used and
(as it was before) the modifications originating from all the locations except the first one are rejected,
but multiple changes are allowed.

Test plan: make check-all

Diff Detail

Repository
rL LLVM

Event Timeline

mehdi_amini edited edge metadata.

Interestingly I got the opposite issue recently: calling through a macro with a single format specifier was *adding* new specific in the fixit in some conditions.

I'm not the best person to review this change though, let me add a few folks.

alexander-shaposhnikov edited the summary of this revision. (Show Details)Jun 6 2017, 10:29 PM

@mehdi_amini , thanks, i see, regarding the "opposite issue" - probably an example / test case would be helpful, that looks like a separate issue.
Thanks for adding @ahatanak and @arphaman, that would be wonderful if smb could look at this diff (which, besides the fix, adds tests)

arphaman accepted this revision.Jun 8 2017, 11:09 AM

Thanks for working on this!

LGTM with a couple of fixes:

test/FixIt/fixit-format-darwin.m
3 ↗(On Diff #101663)

You can just pipe the result of grep into FileCheck without using another temporary file.

54 ↗(On Diff #101663)

Typo: Artificial.

This revision is now accepted and ready to land.Jun 8 2017, 11:09 AM
alexander-shaposhnikov marked 2 inline comments as done.Jun 8 2017, 2:39 PM
This revision was automatically updated to reflect the committed changes.
In D33976#775918, @alexshap wrote:

@mehdi_amini , thanks, i see, regarding the "opposite issue" - probably an example / test case would be helpful, that looks like a separate issue.
Thanks for adding @ahatanak and @arphaman, that would be wonderful if smb could look at this diff (which, besides the fix, adds tests)

I'll CC you on the bug when I get time to reduce the test case and file the bug.

In D33976#775918, @alexshap wrote:

@mehdi_amini , thanks, i see, regarding the "opposite issue" - probably an example / test case would be helpful, that looks like a separate issue.
Thanks for adding @ahatanak and @arphaman, that would be wonderful if smb could look at this diff (which, besides the fix, adds tests)

I'll CC you on the bug when I get time to reduce the test case and file the bug.

I finally got to reduce it: https://bugs.llvm.org/show_bug.cgi?id=33447