diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-06-11 16:01:49 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-06-11 16:01:49 +0000 |
commit | 555efc36d08b07e65e2aba65c91fcaec63834c29 (patch) | |
tree | 4084d1b79103bd7851ece8261c34c59ec040d62e /clang/lib/Format/BreakableToken.cpp | |
parent | 447d2d12f0d4a41feeb88341bddbaa79c36a5103 (diff) | |
download | llvm-555efc36d08b07e65e2aba65c91fcaec63834c29.zip llvm-555efc36d08b07e65e2aba65c91fcaec63834c29.tar.gz llvm-555efc36d08b07e65e2aba65c91fcaec63834c29.tar.bz2 |
Insert a space at the start of a line comment in case it starts with an alphanumeric character.
Summary:
"//Test" becomes "// Test". This change is aimed to improve code
readability and conformance to certain coding styles. If a comment starts with a
non-alphanumeric character, the space isn't added, e.g. "//-*-c++-*-" stays
unchanged.
Reviewers: klimek
Reviewed By: klimek
CC: cfe-commits
Differential Revision: http://llvm-reviews.chandlerc.com/D949
llvm-svn: 183750
Diffstat (limited to 'clang/lib/Format/BreakableToken.cpp')
-rw-r--r-- | clang/lib/Format/BreakableToken.cpp | 57 |
1 files changed, 43 insertions, 14 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 94b4322..d915aef 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -16,6 +16,7 @@ #define DEBUG_TYPE "format-token-breaker" #include "BreakableToken.h" +#include "clang/Basic/CharInfo.h" #include "clang/Format/Format.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Debug.h" @@ -118,15 +119,6 @@ unsigned BreakableSingleLineToken::getLineLengthAfterSplit( encoding::getCodePointCount(Line.substr(Offset, Length), Encoding); } -void BreakableSingleLineToken::insertBreak(unsigned LineIndex, - unsigned TailOffset, Split Split, - bool InPPDirective, - WhitespaceManager &Whitespaces) { - Whitespaces.breakToken(Tok, Prefix.size() + TailOffset + Split.first, - Split.second, Postfix, Prefix, InPPDirective, - StartColumn); -} - BreakableSingleLineToken::BreakableSingleLineToken(const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, @@ -151,6 +143,15 @@ BreakableStringLiteral::getSplit(unsigned LineIndex, unsigned TailOffset, Encoding); } +void BreakableStringLiteral::insertBreak(unsigned LineIndex, + unsigned TailOffset, Split Split, + bool InPPDirective, + WhitespaceManager &Whitespaces) { + Whitespaces.replaceWhitespaceInToken( + Tok, Prefix.size() + TailOffset + Split.first, Split.second, Postfix, + Prefix, InPPDirective, 1, StartColumn); +} + static StringRef getLineCommentPrefix(StringRef Comment) { const char *KnownPrefixes[] = { "/// ", "///", "// ", "//" }; for (size_t i = 0, e = llvm::array_lengthof(KnownPrefixes); i != e; ++i) @@ -164,7 +165,16 @@ BreakableLineComment::BreakableLineComment(const FormatToken &Token, encoding::Encoding Encoding) : BreakableSingleLineToken(Token, StartColumn, getLineCommentPrefix(Token.TokenText), "", - Encoding) {} + Encoding) { + OriginalPrefix = Prefix; + if (Token.TokenText.size() > Prefix.size() && + isAlphanumeric(Token.TokenText[Prefix.size()])) { + if (Prefix == "//") + Prefix = "// "; + else if (Prefix == "///") + Prefix = "/// "; + } +} BreakableToken::Split BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, @@ -173,6 +183,24 @@ BreakableLineComment::getSplit(unsigned LineIndex, unsigned TailOffset, ColumnLimit, Encoding); } +void BreakableLineComment::insertBreak(unsigned LineIndex, unsigned TailOffset, + Split Split, bool InPPDirective, + WhitespaceManager &Whitespaces) { + Whitespaces.replaceWhitespaceInToken( + Tok, OriginalPrefix.size() + TailOffset + Split.first, Split.second, + Postfix, Prefix, InPPDirective, 1, StartColumn); +} + +void +BreakableLineComment::replaceWhitespaceBefore(unsigned LineIndex, + unsigned InPPDirective, + WhitespaceManager &Whitespaces) { + if (OriginalPrefix != Prefix) { + Whitespaces.replaceWhitespaceInToken(Tok, OriginalPrefix.size(), 0, "", "", + false, 0, 1); + } +} + BreakableBlockComment::BreakableBlockComment( const FormatStyle &Style, const FormatToken &Token, unsigned StartColumn, unsigned OriginalStartColumn, bool FirstInLine, encoding::Encoding Encoding) @@ -299,8 +327,9 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset, Text.data() - Tok.TokenText.data() + Split.first; unsigned CharsToRemove = Split.second; assert(IndentAtLineBreak >= Decoration.size()); - Whitespaces.breakToken(Tok, BreakOffsetInToken, CharsToRemove, "", Prefix, - InPPDirective, IndentAtLineBreak - Decoration.size()); + Whitespaces.replaceWhitespaceInToken(Tok, BreakOffsetInToken, CharsToRemove, + "", Prefix, InPPDirective, 1, + IndentAtLineBreak - Decoration.size()); } void @@ -331,9 +360,9 @@ BreakableBlockComment::replaceWhitespaceBefore(unsigned LineIndex, Lines[LineIndex].data() - Tok.TokenText.data() - LeadingWhitespace[LineIndex]; assert(StartOfLineColumn[LineIndex] >= Prefix.size()); - Whitespaces.breakToken( + Whitespaces.replaceWhitespaceInToken( Tok, WhitespaceOffsetInToken, LeadingWhitespace[LineIndex], "", Prefix, - InPPDirective, StartOfLineColumn[LineIndex] - Prefix.size()); + InPPDirective, 1, StartOfLineColumn[LineIndex] - Prefix.size()); } unsigned |