diff options
| author | Alexander Kornienko <alexfh@google.com> | 2013-09-10 09:38:25 +0000 |
|---|---|---|
| committer | Alexander Kornienko <alexfh@google.com> | 2013-09-10 09:38:25 +0000 |
| commit | 83a7dcdf5fce1bdf74ce985419d77a41a51abfa2 (patch) | |
| tree | 4247d1859bca0c2aae0a14ca4491d32b880fe5d2 /lib/Format/Format.cpp | |
| parent | 3f46e6f45907b7004e2c8dfec1a84877d6d262d6 (diff) | |
| download | clang-83a7dcdf5fce1bdf74ce985419d77a41a51abfa2.tar.gz clang-83a7dcdf5fce1bdf74ce985419d77a41a51abfa2.tar.bz2 clang-83a7dcdf5fce1bdf74ce985419d77a41a51abfa2.tar.xz | |
Calculate and store ColumnWidth instead of CodePointCount in FormatTokens.
Summary:
This fixes various issues with mixed tabs and spaces handling, e.g.
when realigning block comments.
Reviewers: klimek, djasper
Reviewed By: djasper
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D1608
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190395 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
| -rw-r--r-- | lib/Format/Format.cpp | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index fe6660b3bc..3a0802a460 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -610,7 +610,7 @@ private: FormatTok->WhitespaceRange = SourceRange(GreaterLocation, GreaterLocation); FormatTok->TokenText = ">"; - FormatTok->CodePointCount = 1; + FormatTok->ColumnWidth = 1; GreaterStashed = false; return FormatTok; } @@ -666,6 +666,10 @@ private: Column = 0; FormatTok->TokenText = FormatTok->TokenText.substr(2); } + + FormatTok->WhitespaceRange = SourceRange( + WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); + FormatTok->OriginalColumn = Column; TrailingWhitespace = 0; @@ -685,24 +689,29 @@ private: } // Now FormatTok is the next non-whitespace token. - FormatTok->CodePointCount = - encoding::getCodePointCount(FormatTok->TokenText, Encoding); - - if (FormatTok->isOneOf(tok::string_literal, tok::comment)) { - StringRef Text = FormatTok->TokenText; - size_t FirstNewlinePos = Text.find('\n'); - if (FirstNewlinePos != StringRef::npos) { - // FIXME: Handle embedded tabs. - FormatTok->FirstLineColumnWidth = encoding::columnWidthWithTabs( - Text.substr(0, FirstNewlinePos), 0, Style.TabWidth, Encoding); - FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( - Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, - Encoding); - } + + StringRef Text = FormatTok->TokenText; + size_t FirstNewlinePos = Text.find('\n'); + if (FirstNewlinePos != StringRef::npos) { + FormatTok->IsMultiline = true; + // The last line of the token always starts in column 0. + // Thus, the length can be precomputed even in the presence of tabs. + FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs( + Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth, + Encoding); + Text = Text.substr(0, FirstNewlinePos); } - // FIXME: Add the CodePointCount to Column. - FormatTok->WhitespaceRange = SourceRange( - WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength)); + + // FIXME: ColumnWidth actually depends on the start column, we need to + // take this into account when the token is moved. + FormatTok->ColumnWidth = + encoding::columnWidthWithTabs(Text, Column, Style.TabWidth, Encoding); + + // FIXME: For multi-line tokens this should be LastLineColumnWidth. + // For line comments this should probably be zero. But before changing, + // we need good tests for this. + Column += FormatTok->ColumnWidth; + return FormatTok; } |
