r256832 - clang-format: Handle \n the same way as std::endl with stream operator.
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 5 05:06:27 PST 2016
Author: djasper
Date: Tue Jan 5 07:06:27 2016
New Revision: 256832
URL: http://llvm.org/viewvc/llvm-project?rev=256832&view=rev
Log:
clang-format: Handle \n the same way as std::endl with stream operator.
clang-format breaks multi-line streams after std::endl.
It now also break for '\n', the suggested replacement for std::endl:
http://llvm.org/docs/CodingStandards.html#avoid-std-endl
Before:
llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << '\n' << bbbbbbbbbbbbbbbbbbbbbb
<< '\n';
llvm::errs() << aaaa << "aaaaaaaaaaaaaaaaaa\n" << bbbb
<< "bbbbbbbbbbbbbbbbbb\n";
After:
llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << '\n'
<< bbbbbbbbbbbbbbbbbbbbbb << '\n';
llvm::errs() << aaaa << "aaaaaaaaaaaaaaaaaa\n"
<< bbbb << "bbbbbbbbbbbbbbbbbb\n";
This changeset ensure that multiline streams have a line break after:
- std::endl
- '\n'
- "\n"
- "Some Text\n"
Patch by Jean-Philippe Dufraigne, thank you.
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=256832&r1=256831&r2=256832&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Tue Jan 5 07:06:27 2016
@@ -257,8 +257,10 @@ bool ContinuationIndenter::mustBreak(con
Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
return true;
- if (Current.is(tok::lessless) && Previous.is(tok::identifier) &&
- Previous.TokenText == "endl")
+ if (Current.is(tok::lessless) &&
+ ((Previous.is(tok::identifier) && Previous.TokenText == "endl") ||
+ (Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") ||
+ Previous.TokenText == "\'\\n\'"))))
return true;
return false;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=256832&r1=256831&r2=256832&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Jan 5 07:06:27 2016
@@ -5065,6 +5065,15 @@ TEST_F(FormatTest, AlignsPipes) {
verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
" << bbbbbbbbbbbbbbbbbbbbbb << endl;");
verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
+
+ // Handle '\n'.
+ verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
+ " << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
+ verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
+ " << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
+ verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
+ " << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
+ verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
}
TEST_F(FormatTest, UnderstandsEquals) {
More information about the cfe-commits
mailing list