diff options
| author | Daniel Jasper <djasper@google.com> | 2013-03-01 16:45:59 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2013-03-01 16:45:59 +0000 |
| commit | 3324cbefcde82930503483819cf76077192840d2 (patch) | |
| tree | 7bbe7cab0b3b7a60cf7f05cfa9eba5c8853fd93b /clang/lib/Format/Format.cpp | |
| parent | e317d1b1e8fae926239aa1e0d75fc1ddd679199d (diff) | |
| download | bcm5719-llvm-3324cbefcde82930503483819cf76077192840d2.tar.gz bcm5719-llvm-3324cbefcde82930503483819cf76077192840d2.zip | |
Remove trailing whitespace of line comments.
This fixed llvm.org/PR15378.
llvm-svn: 176351
Diffstat (limited to 'clang/lib/Format/Format.cpp')
| -rw-r--r-- | clang/lib/Format/Format.cpp | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 01813ef6c85..5fd49157bd4 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -116,20 +116,30 @@ public: // Align line comments if they are trailing or if they continue other // trailing comments. - if (isTrailingComment(Tok) && (Tok.Parent != NULL || !Comments.empty())) { - if (Style.ColumnLimit >= - Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { - Comments.push_back(StoredComment()); - Comments.back().Tok = Tok.FormatTok; - Comments.back().Spaces = Spaces; - Comments.back().NewLines = NewLines; - if (NewLines == 0) - Comments.back().MinColumn = WhitespaceStartColumn + Spaces; - else - Comments.back().MinColumn = Spaces; - Comments.back().MaxColumn = - Style.ColumnLimit - Tok.FormatTok.TokenLength; - return; + if (isTrailingComment(Tok)) { + // Remove the comment's trailing whitespace. + if (Tok.FormatTok.Tok.getLength() != Tok.FormatTok.TokenLength) + Replaces.insert(tooling::Replacement( + SourceMgr, Tok.FormatTok.Tok.getLocation().getLocWithOffset( + Tok.FormatTok.TokenLength), + Tok.FormatTok.Tok.getLength() - Tok.FormatTok.TokenLength, "")); + + // Align comment with other comments. + if (Tok.Parent != NULL || !Comments.empty()) { + if (Style.ColumnLimit >= + Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { + Comments.push_back(StoredComment()); + Comments.back().Tok = Tok.FormatTok; + Comments.back().Spaces = Spaces; + Comments.back().NewLines = NewLines; + if (NewLines == 0) + Comments.back().MinColumn = WhitespaceStartColumn + Spaces; + else + Comments.back().MinColumn = Spaces; + Comments.back().MaxColumn = + Style.ColumnLimit - Tok.FormatTok.TokenLength; + return; + } } } @@ -1017,6 +1027,11 @@ public: GreaterStashed = true; } + // If we reformat comments, we remove trailing whitespace. Update the length + // accordingly. + if (FormatTok.Tok.is(tok::comment)) + FormatTok.TokenLength = Text.rtrim().size(); + return FormatTok; } |

