Remove "incorrect" aligning of trailing comments.

We used to align trailing comments belong to different things.
Before:
void f() { // some function..
}
int a;     // some variable..

After:
void f() { // some function..
}
int a; // some variable..

llvm-svn: 173100
This commit is contained in:
Daniel Jasper 2013-01-21 22:49:20 +00:00
parent b7abb30d77
commit 304a986a72
2 changed files with 26 additions and 4 deletions

View File

@ -202,7 +202,13 @@ public:
void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines,
unsigned Spaces, unsigned WhitespaceStartColumn, unsigned Spaces, unsigned WhitespaceStartColumn,
const FormatStyle &Style) { const FormatStyle &Style) {
if (Tok.Type == TT_LineComment && NewLines < 2 && // 2+ newlines mean an empty line separating logic scopes.
if (NewLines >= 2)
alignComments();
// Align line comments if they are trailing or if they continue other
// trailing comments.
if (Tok.Type == TT_LineComment &&
(Tok.Parent != NULL || !Comments.empty())) { (Tok.Parent != NULL || !Comments.empty())) {
if (Style.ColumnLimit >= if (Style.ColumnLimit >=
Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) { Spaces + WhitespaceStartColumn + Tok.FormatTok.TokenLength) {
@ -215,10 +221,11 @@ public:
Spaces - Tok.FormatTok.TokenLength; Spaces - Tok.FormatTok.TokenLength;
return; return;
} }
} else if (NewLines == 0 && Tok.Children.empty() &&
Tok.Type != TT_LineComment) {
alignComments();
} }
// If this line does not have a trailing comment, align the stored comments.
if (Tok.Children.empty() && Tok.Type != TT_LineComment)
alignComments();
storeReplacement(Tok.FormatTok, storeReplacement(Tok.FormatTok,
std::string(NewLines, '\n') + std::string(Spaces, ' ')); std::string(NewLines, '\n') + std::string(Spaces, ' '));
} }

View File

@ -404,6 +404,21 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) {
" // Comment inside a statement.\n" " // Comment inside a statement.\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
EXPECT_EQ("void f() { // This does something ..\n"
"}\n"
"int a; // This is unrelated",
format("void f() { // This does something ..\n"
" }\n"
"int a; // This is unrelated"));
EXPECT_EQ("void f() { // This does something ..\n"
"} // awesome..\n"
"\n"
"int a; // This is unrelated",
format("void f() { // This does something ..\n"
" } // awesome..\n"
" \n"
"int a; // This is unrelated"));
EXPECT_EQ("int i; // single line trailing comment", EXPECT_EQ("int i; // single line trailing comment",
format("int i;\\\n// single line trailing comment")); format("int i;\\\n// single line trailing comment"));