This is an archive of the discontinued LLVM Phabricator instance.

[clang] Correct -frewrite-includes generation of line control directives with mixed EOL forms.
ClosedPublic

Authored by tahonermann on Jan 4 2023, 6:38 AM.

Details

Summary

Previously, if a header file and a source file used different end of line (EOL) forms, preprocessed output generated with the -frewrite-includes option would, in some cases, generate line control directives with the wrong line number due to an error in how source file lines were counted.

Fixes https://github.com/llvm/llvm-project/issues/59736

Diff Detail

Event Timeline

tahonermann created this revision.Jan 4 2023, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 6:38 AM
tahonermann requested review of this revision.Jan 4 2023, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2023, 6:38 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tahonermann added inline comments.Jan 4 2023, 6:56 AM
clang/lib/Frontend/Rewrite/InclusionRewriter.cpp
292–303

The issue here was that, when the last line of TextToWrite was not empty and did not end with LocalEOL, Line would be incremented at line 298 despite no EOL sequence being written at either line 300 or line 303.

The rewrite avoids use of StringRef::split() so as to avoid the ambiguity created when Rest ends with LocalEOL (Rest ends up empty regardless). I think this simplifies the code by removing the need for differentiated handling of EnsureNewLine in each of the branches of the if statement.

Thanks Tom

I think this looks reasonable but given the subtlety a comment in the code may be useful
+ a release note to indicate the fixed issue

cor3ntin accepted this revision.Jan 4 2023, 7:11 AM
This revision is now accepted and ready to land.Jan 4 2023, 7:11 AM

Addressed Corentin's review feedback. Thank you, Corentin! I'm going to remember to update the release notes doc all on my own one of these days, I'm sure of it! :)

This revision was landed with ongoing or failed builds.Jan 5 2023, 10:24 AM
This revision was automatically updated to reflect the committed changes.