<div dir="ltr">Looks to be failing existing tests?<br><br><div>FAIL: Clang-Unit :: Format/FormatTests/FormatTest.BreaksLongDeclarations (12427 of 32080)</div><div>******************** TEST 'Clang-Unit :: Format/FormatTests/FormatTest.BreaksLongDeclarations' FAILED ********************</div><div>Note: Google Test filter = FormatTest.BreaksLongDeclarations</div><div>[==========] Running 1 test from 1 test case.</div><div>[----------] Global test environment set-up.</div><div>[----------] 1 test from FormatTest</div><div>[ RUN ] FormatTest.BreaksLongDeclarations</div><div>/usr/local/google/home/blaikie/dev/llvm/src/tools/clang/unittests/Format/FormatTest.cpp:73: Failure</div><div> Expected: Code.str()</div><div> Which is: "template<typename T> // Templates on own line.\nstatic int // Some comment.\nMyFunction(int a);"</div><div>To be equal to: format(test::messUp(Code), Style)</div><div> Which is: "template <typename T> // Templates on own line.\nstatic int // Some comment.\nMyFunction(int a);"</div><div>With diff:</div><div>@@ -1,3 +1,3 @@</div><div>-template<typename T> // Templates on own line.</div><div>-static int // Some comment.</div><div>+template <typename T> // Templates on own line.</div><div>+static int // Some comment.</div><div> MyFunction(int a);</div><div><br></div><div>/usr/local/google/home/blaikie/dev/llvm/src/tools/clang/unittests/Format/FormatTest.cpp:79: Failure</div><div> Expected: Code.str()</div><div> Which is: "template<typename T> // Templates on own line.\nstatic int // Some comment.\nMyFunction(int a);"</div><div>To be equal to: format(test::messUp(Code), ObjCStyle)</div><div> Which is: "template <typename T> // Templates on own line.\nstatic int // Some comment.\nMyFunction(int a);"</div><div>With diff:</div><div>@@ -1,3 +1,3 @@</div><div>-template<typename T> // Templates on own line.</div><div>-static int // Some comment.</div><div>+template <typename T> // Templates on own line.</div><div>+static int // Some comment.</div><div> MyFunction(int a);</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Mar 7, 2017 at 6:19 AM Krasimir Georgiev via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: krasimir<br class="gmail_msg">
Date: Tue Mar 7 08:07:43 2017<br class="gmail_msg">
New Revision: 297140<br class="gmail_msg">
<br class="gmail_msg">
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=297140&view=rev" rel="noreferrer" class="gmail_msg" target="_blank">http://llvm.org/viewvc/llvm-project?rev=297140&view=rev</a><br class="gmail_msg">
Log:<br class="gmail_msg">
[clang-format] Support namespaces ending in semicolon<br class="gmail_msg">
<br class="gmail_msg">
Summary:<br class="gmail_msg">
This patch adds support for namespaces ending in semicolon to the namespace comment fixer.<br class="gmail_msg">
source:<br class="gmail_msg">
```<br class="gmail_msg">
namespace A {<br class="gmail_msg">
int i;<br class="gmail_msg">
int j;<br class="gmail_msg">
};<br class="gmail_msg">
```<br class="gmail_msg">
clang-format before:<br class="gmail_msg">
```<br class="gmail_msg">
namespace A {<br class="gmail_msg">
int i;<br class="gmail_msg">
int j;<br class="gmail_msg">
} // namespace A;<br class="gmail_msg">
```<br class="gmail_msg">
clang-format after:<br class="gmail_msg">
```<br class="gmail_msg">
namespace A {<br class="gmail_msg">
int i;<br class="gmail_msg">
int j;<br class="gmail_msg">
}; // namespace A<br class="gmail_msg">
```<br class="gmail_msg">
<br class="gmail_msg">
Reviewers: djasper<br class="gmail_msg">
<br class="gmail_msg">
Reviewed By: djasper<br class="gmail_msg">
<br class="gmail_msg">
Subscribers: cfe-commits, klimek<br class="gmail_msg">
<br class="gmail_msg">
Differential Revision: <a href="https://reviews.llvm.org/D30688" rel="noreferrer" class="gmail_msg" target="_blank">https://reviews.llvm.org/D30688</a><br class="gmail_msg">
<br class="gmail_msg">
Modified:<br class="gmail_msg">
cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp<br class="gmail_msg">
cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp<br class="gmail_msg">
<br class="gmail_msg">
Modified: cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp<br class="gmail_msg">
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp?rev=297140&r1=297139&r2=297140&view=diff" rel="noreferrer" class="gmail_msg" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp?rev=297140&r1=297139&r2=297140&view=diff</a><br class="gmail_msg">
==============================================================================<br class="gmail_msg">
--- cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp (original)<br class="gmail_msg">
+++ cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp Tue Mar 7 08:07:43 2017<br class="gmail_msg">
@@ -139,20 +139,34 @@ tooling::Replacements NamespaceEndCommen<br class="gmail_msg">
if (RBraceTok->Finalized)<br class="gmail_msg">
continue;<br class="gmail_msg">
RBraceTok->Finalized = true;<br class="gmail_msg">
+ const FormatToken *EndCommentPrevTok = RBraceTok;<br class="gmail_msg">
+ // Namespaces often end with '};'. In that case, attach namespace end<br class="gmail_msg">
+ // comments to the semicolon tokens.<br class="gmail_msg">
+ if (RBraceTok->Next && RBraceTok->Next->is(tok::semi)) {<br class="gmail_msg">
+ EndCommentPrevTok = RBraceTok->Next;<br class="gmail_msg">
+ }<br class="gmail_msg">
+ // The next token in the token stream after the place where the end comment<br class="gmail_msg">
+ // token must be. This is either the next token on the current line or the<br class="gmail_msg">
+ // first token on the next line.<br class="gmail_msg">
+ const FormatToken *EndCommentNextTok = EndCommentPrevTok->Next;<br class="gmail_msg">
+ if (EndCommentNextTok && EndCommentNextTok->is(tok::comment))<br class="gmail_msg">
+ EndCommentNextTok = EndCommentNextTok->Next;<br class="gmail_msg">
+ if (!EndCommentNextTok && I + 1 < E)<br class="gmail_msg">
+ EndCommentNextTok = AnnotatedLines[I + 1]->First;<br class="gmail_msg">
+ bool AddNewline = EndCommentNextTok &&<br class="gmail_msg">
+ EndCommentNextTok->NewlinesBefore == 0 &&<br class="gmail_msg">
+ EndCommentNextTok->isNot(tok::eof);<br class="gmail_msg">
const std::string NamespaceName = computeName(NamespaceTok);<br class="gmail_msg">
- bool AddNewline = (I + 1 < E) &&<br class="gmail_msg">
- AnnotatedLines[I + 1]->First->NewlinesBefore == 0 &&<br class="gmail_msg">
- AnnotatedLines[I + 1]->First->isNot(tok::eof);<br class="gmail_msg">
const std::string EndCommentText =<br class="gmail_msg">
computeEndCommentText(NamespaceName, AddNewline);<br class="gmail_msg">
- if (!hasEndComment(RBraceTok)) {<br class="gmail_msg">
+ if (!hasEndComment(EndCommentPrevTok)) {<br class="gmail_msg">
bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;<br class="gmail_msg">
if (!isShort)<br class="gmail_msg">
- addEndComment(RBraceTok, EndCommentText, SourceMgr, &Fixes);<br class="gmail_msg">
+ addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);<br class="gmail_msg">
continue;<br class="gmail_msg">
}<br class="gmail_msg">
- if (!validEndComment(RBraceTok, NamespaceName))<br class="gmail_msg">
- updateEndComment(RBraceTok, EndCommentText, SourceMgr, &Fixes);<br class="gmail_msg">
+ if (!validEndComment(EndCommentPrevTok, NamespaceName))<br class="gmail_msg">
+ updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);<br class="gmail_msg">
}<br class="gmail_msg">
return Fixes;<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
Modified: cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp<br class="gmail_msg">
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp?rev=297140&r1=297139&r2=297140&view=diff" rel="noreferrer" class="gmail_msg" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp?rev=297140&r1=297139&r2=297140&view=diff</a><br class="gmail_msg">
==============================================================================<br class="gmail_msg">
--- cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp (original)<br class="gmail_msg">
+++ cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp Tue Mar 7 08:07:43 2017<br class="gmail_msg">
@@ -184,6 +184,34 @@ TEST_F(NamespaceEndCommentsFixerTest, Ad<br class="gmail_msg">
"}\n"<br class="gmail_msg">
"}\n"<br class="gmail_msg">
"}"));<br class="gmail_msg">
+<br class="gmail_msg">
+ // Adds an end comment after a semicolon.<br class="gmail_msg">
+ EXPECT_EQ("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};// namespace",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};// namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};// namespace A\n"<br class="gmail_msg">
+ "// unrelated",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};\n"<br class="gmail_msg">
+ "// unrelated"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, AddsNewlineIfNeeded) {<br class="gmail_msg">
@@ -220,6 +248,24 @@ TEST_F(NamespaceEndCommentsFixerTest, Ad<br class="gmail_msg">
" int j;\n"<br class="gmail_msg">
" int k;\n"<br class="gmail_msg">
"}"));<br class="gmail_msg">
+ EXPECT_EQ("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};// namespace\n"<br class="gmail_msg">
+ "int k;",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};int k;"));<br class="gmail_msg">
+ EXPECT_EQ("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};// namespace\n"<br class="gmail_msg">
+ ";",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ " int j;\n"<br class="gmail_msg">
+ "};;"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddEndCommentForShortNamespace) {<br class="gmail_msg">
@@ -227,6 +273,8 @@ TEST_F(NamespaceEndCommentsFixerTest, Do<br class="gmail_msg">
EXPECT_EQ("namespace A {}", fixNamespaceEndComments("namespace A {}"));<br class="gmail_msg">
EXPECT_EQ("namespace A { a }",<br class="gmail_msg">
fixNamespaceEndComments("namespace A { a }"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A { a };",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A { a };"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddCommentAfterUnaffectedRBrace) {<br class="gmail_msg">
@@ -238,6 +286,14 @@ TEST_F(NamespaceEndCommentsFixerTest, Do<br class="gmail_msg">
"}",<br class="gmail_msg">
// The range (16, 3) spans the 'int' above.<br class="gmail_msg">
/*Ranges=*/{1, tooling::Range(16, 3)}));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "};",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "};",<br class="gmail_msg">
+ // The range (16, 3) spans the 'int' above.<br class="gmail_msg">
+ /*Ranges=*/{1, tooling::Range(16, 3)}));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddCommentAfterRBraceInPPDirective) {<br class="gmail_msg">
@@ -276,6 +332,18 @@ TEST_F(NamespaceEndCommentsFixerTest, Ke<br class="gmail_msg">
fixNamespaceEndComments("namespace A::B {\n"<br class="gmail_msg">
" int i;\n"<br class="gmail_msg">
"} // end namespace A::B"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; // end namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; // end namespace A"));<br class="gmail_msg">
+ EXPECT_EQ("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; /* unnamed namespace */",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; /* unnamed namespace */"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) {<br class="gmail_msg">
@@ -309,10 +377,17 @@ TEST_F(NamespaceEndCommentsFixerTest, Up<br class="gmail_msg">
fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
" int i;\n"<br class="gmail_msg">
"} // banamespace A"));<br class="gmail_msg">
-<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; // namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; // banamespace A"));<br class="gmail_msg">
// Updates invalid line comments even for short namespaces.<br class="gmail_msg">
EXPECT_EQ("namespace A {} // namespace A",<br class="gmail_msg">
fixNamespaceEndComments("namespace A {} // namespace"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {}; // namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {}; // namespace"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndBlockComment) {<br class="gmail_msg">
@@ -346,8 +421,16 @@ TEST_F(NamespaceEndCommentsFixerTest, Up<br class="gmail_msg">
fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
" int i;\n"<br class="gmail_msg">
"} /* banamespace A */"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; // namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {\n"<br class="gmail_msg">
+ " int i;\n"<br class="gmail_msg">
+ "}; /* banamespace A */"));<br class="gmail_msg">
EXPECT_EQ("namespace A {} // namespace A",<br class="gmail_msg">
fixNamespaceEndComments("namespace A {} /**/"));<br class="gmail_msg">
+ EXPECT_EQ("namespace A {}; // namespace A",<br class="gmail_msg">
+ fixNamespaceEndComments("namespace A {}; /**/"));<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
TEST_F(NamespaceEndCommentsFixerTest,<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
cfe-commits mailing list<br class="gmail_msg">
<a href="mailto:cfe-commits@lists.llvm.org" class="gmail_msg" target="_blank">cfe-commits@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br class="gmail_msg">
</blockquote></div>