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 | 39856b71a6e233d9ac8606213c820b17f8f965ea (patch) | |
tree | da171e82ae8eea20bb119afa9b93d204178bf3ae /clang/lib/Format/Format.cpp | |
parent | 3767ccf3187e4494e30a7f624d7e8e37eb4527de (diff) | |
download | bcm5719-llvm-39856b71a6e233d9ac8606213c820b17f8f965ea.tar.gz bcm5719-llvm-39856b71a6e233d9ac8606213c820b17f8f965ea.zip |
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
llvm-svn: 190395
Diffstat (limited to 'clang/lib/Format/Format.cpp')
-rw-r--r-- | clang/lib/Format/Format.cpp | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index fe6660b3bc3..3a0802a4609 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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; } |