r308725 - [clang-format] Fix comment levels between '}' and PPDirective
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 21 03:26:13 PDT 2017
Author: krasimir
Date: Fri Jul 21 03:26:13 2017
New Revision: 308725
URL: http://llvm.org/viewvc/llvm-project?rev=308725&view=rev
Log:
[clang-format] Fix comment levels between '}' and PPDirective
Summary:
This fixes a regression exposed by r307795 in which the level of a comment line
between '}' and a preprocessor directive is incorrectly set as the level of the
line before the '}'. In effect, this:
```
int f(int i) {
int j = i;
return i + j;
}
// comment
#ifdef A
#endif
```
was formatted as:
```
int f(int i) {
int j = i;
return i + j;
}
// comment
#ifdef A
#endif
```
Reviewers: djasper
Reviewed By: djasper
Subscribers: cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D35485
Modified:
cfe/trunk/lib/Format/UnwrappedLineParser.cpp
cfe/trunk/unittests/Format/FormatTestComments.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=308725&r1=308724&r2=308725&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Fri Jul 21 03:26:13 2017
@@ -486,6 +486,8 @@ void UnwrappedLineParser::parseBlock(boo
return;
}
+ flushComments(isOnNewLine(*FormatTok));
+ Line->Level = InitialLevel;
nextToken(); // Munch the closing brace.
if (MacroBlock && FormatTok->is(tok::l_paren))
@@ -493,7 +495,6 @@ void UnwrappedLineParser::parseBlock(boo
if (MunchSemi && FormatTok->Tok.is(tok::semi))
nextToken();
- Line->Level = InitialLevel;
Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
// Update the opening line to add the forward reference as well
Modified: cfe/trunk/unittests/Format/FormatTestComments.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestComments.cpp?rev=308725&r1=308724&r2=308725&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestComments.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestComments.cpp Fri Jul 21 03:26:13 2017
@@ -836,6 +836,25 @@ TEST_F(FormatTestComments, KeepsLevelOfC
" int j;\n"
"}"));
+ EXPECT_EQ("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ " // comment\n"
+ "#ifdef A\n"
+ " int j;\n"
+ "#endif\n"
+ "}",
+ format("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ " // comment\n"
+ "#ifdef A\n"
+ "int j;\n"
+ "#endif\n"
+ "}"));
+
// Keep the current level if there is an empty line between the comment and
// the preprocessor directive.
EXPECT_EQ("void f() {\n"
@@ -853,6 +872,46 @@ TEST_F(FormatTestComments, KeepsLevelOfC
" int j;\n"
"}"));
+ EXPECT_EQ("void f() {\n"
+ " int i;\n"
+ " return i;\n"
+ "}\n"
+ "// comment\n"
+ "\n"
+ "#ifdef A\n"
+ "int i;\n"
+ "#endif // A",
+ format("void f() {\n"
+ " int i;\n"
+ " return i;\n"
+ "}\n"
+ "// comment\n"
+ "\n"
+ "#ifdef A\n"
+ "int i;\n"
+ "#endif // A"));
+
+ EXPECT_EQ("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ " // comment\n"
+ "\n"
+ "#ifdef A\n"
+ " int j;\n"
+ "#endif\n"
+ "}",
+ format("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ " // comment\n"
+ "\n"
+ "#ifdef A\n"
+ " int j;\n"
+ "#endif\n"
+ "}"));
+
// Align with the preprocessor directive if the comment was originally aligned
// with the preprocessor directive.
EXPECT_EQ("void f() {\n"
@@ -867,6 +926,25 @@ TEST_F(FormatTestComments, KeepsLevelOfC
"#ifdef A\n"
" int j;\n"
"}"));
+
+ EXPECT_EQ("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ "// comment\n"
+ "#ifdef A\n"
+ " int j;\n"
+ "#endif\n"
+ "}",
+ format("int f(int i) {\n"
+ " if (true) {\n"
+ " ++i;\n"
+ " }\n"
+ "// comment\n"
+ "#ifdef A\n"
+ " int j;\n"
+ "#endif\n"
+ "}"));
}
TEST_F(FormatTestComments, SplitsLongLinesInComments) {
More information about the cfe-commits
mailing list