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

Tom Honermann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 4 06:56:02 PST 2023


tahonermann added inline comments.


================
Comment at: clang/lib/Frontend/Rewrite/InclusionRewriter.cpp:292-303
     // Output the file one line at a time, rewriting the line endings as we go.
     StringRef Rest = TextToWrite;
     while (!Rest.empty()) {
-      StringRef LineText;
-      std::tie(LineText, Rest) = Rest.split(LocalEOL);
+      size_t Idx = Rest.find(LocalEOL);
+      StringRef LineText = Rest.substr(0, Idx);
       OS << LineText;
-      Line++;
----------------
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.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D140984/new/

https://reviews.llvm.org/D140984



More information about the cfe-commits mailing list