[Mlir-commits] [mlir] c304be7 - [mlir][docgen] Handle Windows line endings in doc generation.
Scott Todd
llvmlistbot at llvm.org
Thu Jun 29 09:57:07 PDT 2023
Author: Scott Todd
Date: 2023-06-29T09:56:49-07:00
New Revision: c304be7cfdd2261811671feb252e31222365b475
URL: https://github.com/llvm/llvm-project/commit/c304be7cfdd2261811671feb252e31222365b475
DIFF: https://github.com/llvm/llvm-project/commit/c304be7cfdd2261811671feb252e31222365b475.diff
LOG: [mlir][docgen] Handle Windows line endings in doc generation.
The `printReindented` function searches for Unix style line endings (`\n`), but strings may have Windows style line endings (`\r\n`). Prior to this change, generated document sections could have extra indentation, which some markdown renderers interpret as code blocks rather than paragraphs.
Differential Revision: https://reviews.llvm.org/D153591
Added:
Modified:
mlir/include/mlir/Support/IndentedOstream.h
mlir/unittests/Support/IndentedOstreamTest.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/Support/IndentedOstream.h b/mlir/include/mlir/Support/IndentedOstream.h
index 5961e46c64c723..101aa8b631d299 100644
--- a/mlir/include/mlir/Support/IndentedOstream.h
+++ b/mlir/include/mlir/Support/IndentedOstream.h
@@ -117,7 +117,9 @@ mlir::raw_indented_ostream::printReindented(StringRef str,
// Skip empty lines.
while (!output.empty()) {
auto split = output.split('\n');
- size_t indent = split.first.find_first_not_of(" \t");
+ // Trim Windows \r characters from \r\n line endings.
+ auto firstTrimmed = split.first.rtrim('\r');
+ size_t indent = firstTrimmed.find_first_not_of(" \t");
if (indent != StringRef::npos) {
// Set an initial value.
leadingWs = indent;
@@ -129,7 +131,8 @@ mlir::raw_indented_ostream::printReindented(StringRef str,
StringRef remaining = output;
while (!remaining.empty()) {
auto split = remaining.split('\n');
- size_t indent = split.first.find_first_not_of(" \t");
+ auto firstTrimmed = split.first.rtrim('\r');
+ size_t indent = firstTrimmed.find_first_not_of(" \t");
if (indent != StringRef::npos)
leadingWs = std::min(leadingWs, static_cast<int>(indent));
remaining = split.second;
diff --git a/mlir/unittests/Support/IndentedOstreamTest.cpp b/mlir/unittests/Support/IndentedOstreamTest.cpp
index 11b6e573df680a..08a4de533c1c34 100644
--- a/mlir/unittests/Support/IndentedOstreamTest.cpp
+++ b/mlir/unittests/Support/IndentedOstreamTest.cpp
@@ -108,3 +108,19 @@ TEST(FormatTest, Reindent) {
)";
EXPECT_THAT(os.str(), StrEq(expected));
}
+
+TEST(FormatTest, ReindentLineEndings) {
+ std::string str;
+ llvm::raw_string_ostream os(str);
+ raw_indented_ostream ros(os);
+
+ // Similar string as the previous test, but with \r\n (Windows style) line
+ // breaks. Note that C++'s internal string representation uses \n, so just
+ // running the previous test as-is on Windows is not sufficient.
+ const auto *desc =
+ "\r\n\r\n\r\n First line\r\n second line";
+ ros.printReindented(desc);
+ ros.flush();
+ const auto *expected = "First line\r\n second line";
+ EXPECT_THAT(os.str(), StrEq(expected));
+}
More information about the Mlir-commits
mailing list