summaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-07-16 21:06:13 +0000
committerAlexander Kornienko <alexfh@google.com>2013-07-16 21:06:13 +0000
commit10c26b2e974f97c75fc3aaa302ca750f422cbce1 (patch)
tree841e43e58bd1f676575aae898f0caf37fed737d6 /lib/Format/Format.cpp
parent561211d35b5a2825fba6d0c017044f6896b204fd (diff)
downloadclang-10c26b2e974f97c75fc3aaa302ca750f422cbce1.tar.gz
clang-10c26b2e974f97c75fc3aaa302ca750f422cbce1.tar.bz2
clang-10c26b2e974f97c75fc3aaa302ca750f422cbce1.tar.xz
Don't break line comments with escaped newlines.
Summary: These can appear when comments contain command lines with quoted line breaks. As the text (including escaped newlines and '//' from consecutive lines) is a single line comment, we used to break it even when it didn't exceed column limit. This is a temporary solution, in the future we may want to support this case completely - at least adjust leading whitespace when changing indentation of the first line. Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D1146 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186456 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index c6927ecbe2..11ab58c18c 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -905,6 +905,11 @@ private:
// Only break up default narrow strings.
if (!Current.TokenText.startswith("\""))
return 0;
+ // Don't break string literals with escaped newlines. As clang-format must
+ // not change the string's content, it is unlikely that we'll end up with
+ // a better format.
+ if (Current.TokenText.find("\\\n") != StringRef::npos)
+ return 0;
// Exempts unterminated string literals from line breaking. The user will
// likely want to terminate the string before any line breaking is done.
if (Current.IsUnterminatedLiteral)
@@ -919,6 +924,23 @@ private:
} else if (Current.Type == TT_LineComment &&
(Current.Previous == NULL ||
Current.Previous->Type != TT_ImplicitStringLiteral)) {
+ // Don't break line comments with escaped newlines. These look like
+ // separate line comments, but in fact contain a single line comment with
+ // multiple lines including leading whitespace and the '//' markers.
+ //
+ // FIXME: If we want to handle them correctly, we'll need to adjust
+ // leading whitespace in consecutive lines when changing indentation of
+ // the first line similar to what we do with block comments.
+ StringRef::size_type EscapedNewlinePos = Current.TokenText.find("\\\n");
+ if (EscapedNewlinePos != StringRef::npos) {
+ State.Column =
+ StartColumn +
+ encoding::getCodePointCount(
+ Current.TokenText.substr(0, EscapedNewlinePos), Encoding) +
+ 1;
+ return 0;
+ }
+
Token.reset(new BreakableLineComment(Current, StartColumn,
Line.InPPDirective, Encoding));
} else {