Store first and last newline position in the token text for string literals and comments.
Summary:
Store first and last newline position in the token text for string literals and
comments to avoid doing .find('\n') for each possible solution.
Reviewers: djasper
Reviewed By: djasper
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D1556
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@189758 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 84bf36c..39d2c0f 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -596,10 +596,16 @@
FormatTok->CodePointCount =
encoding::getCodePointCount(FormatTok->TokenText, Encoding);
- if (FormatTok->isOneOf(tok::string_literal, tok::comment) &&
- FormatTok->TokenText.find('\n') != StringRef::npos)
- FormatTok->IsMultiline = true;
-
+ if (FormatTok->isOneOf(tok::string_literal, tok::comment)) {
+ StringRef Text = FormatTok->TokenText;
+ size_t FirstNewlinePos = Text.find('\n');
+ if (FirstNewlinePos != StringRef::npos) {
+ FormatTok->CodePointsInFirstLine = encoding::getCodePointCount(
+ Text.substr(0, FirstNewlinePos), Encoding);
+ FormatTok->CodePointsInLastLine = encoding::getCodePointCount(
+ Text.substr(Text.find_last_of('\n') + 1), Encoding);
+ }
+ }
// FIXME: Add the CodePointCount to Column.
FormatTok->WhitespaceRange = SourceRange(
WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));