[PATCH] D30528: [clang-format] Use number of unwrapped lines for short namespace

2017-03-02 Thread Krasimir Georgiev via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL296736: [clang-format] Use number of unwrapped lines for 
short namespace (authored by krasimir).

Changed prior to commit:
  https://reviews.llvm.org/D30528?vs=90299&id=90302#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D30528

Files:
  cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
  cfe/trunk/unittests/Format/FormatTest.cpp
  cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp

Index: cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
===
--- cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
+++ cfe/trunk/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -23,7 +23,7 @@
 namespace format {
 
 namespace {
-// The maximal number of lines that a short namespace spans.
+// The maximal number of unwrapped lines that a short namespace spans.
 // Short namespaces don't need an end comment.
 static const int kShortNamespaceMaxLines = 1;
 
@@ -60,14 +60,6 @@
   return text;
 }
 
-bool isShort(const FormatToken *NamespaceTok, const FormatToken *RBraceTok,
- const SourceManager &SourceMgr) {
-  int StartLine =
-  SourceMgr.getSpellingLineNumber(NamespaceTok->Tok.getLocation());
-  int EndLine = SourceMgr.getSpellingLineNumber(RBraceTok->Tok.getLocation());
-  return EndLine - StartLine + 1 <= kShortNamespaceMaxLines;
-}
-
 bool hasEndComment(const FormatToken *RBraceTok) {
   return RBraceTok->Next && RBraceTok->Next->is(tok::comment);
 }
@@ -151,7 +143,8 @@
 const std::string EndCommentText =
 computeEndCommentText(NamespaceName, AddNewline);
 if (!hasEndComment(RBraceTok)) {
-  if (!isShort(NamespaceTok, RBraceTok, SourceMgr))
+  bool isShort = I - StartLineIndex <= kShortNamespaceMaxLines + 1;
+  if (!isShort)
 addEndComment(RBraceTok, EndCommentText, SourceMgr, &Fixes);
   continue;
 }
Index: cfe/trunk/unittests/Format/FormatTest.cpp
===
--- cfe/trunk/unittests/Format/FormatTest.cpp
+++ cfe/trunk/unittests/Format/FormatTest.cpp
@@ -185,7 +185,7 @@
   EXPECT_EQ("namespace N {\n"
 "\n"
 "int i;\n"
-"}  // namespace N",
+"}",
 format("namespace N {\n"
"\n"
"inti;\n"
@@ -281,8 +281,7 @@
"}", LLVMWithNoNamespaceFix));
   EXPECT_EQ("namespace {\n"
 "int i;\n"
-"\n"
-"} // namespace",
+"}",
 format("namespace {\n"
"int i;\n"
"\n"
@@ -5460,7 +5459,7 @@
   EXPECT_EQ("namespace N {\n"
 "void f() {}\n"
 "void g()\n"
-"}",
+"} // namespace N",
 format("namespace N  { void f( ) { } void g( ) }"));
 }
 
@@ -6140,8 +6139,8 @@
"  void f() {}\n"
"  int *a;\n"
"};\n"
-   "}\n"
-   "}");
+   "} // namespace b\n"
+   "} // namespace a");
 }
 
 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
@@ -7934,7 +7933,7 @@
"struct B {\n"
"  int x;\n"
"};\n"
-   "}\n",
+   "} // namespace a\n",
StroustrupBraceStyle);
 
   verifyFormat("void foo()\n"
Index: cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===
--- cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ cfe/trunk/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -47,96 +47,132 @@
 TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) {
   EXPECT_EQ("namespace {\n"
 "  int i;\n"
+"  int j;\n"
 "}// namespace",
 fixNamespaceEndComments("namespace {\n"
 "  int i;\n"
+"  int j;\n"
 "}"));
   EXPECT_EQ("namespace {\n"
 "  int i;\n"
+"  int j;\n"
 "}// namespace\n",
 fixNamespaceEndComments("namespace {\n"
 "  int i;\n"
+"  int j;\n"
 "}\n"));
   EXPECT_EQ("namespace A {\n"
 "  int i;\n"
+"  int j;\n"
 "}// namespace A",
 fixNamespaceEndComments("namespace A {\n"
 "  int i;\n"
+"  int j;\n"
 "}"));
   EXPECT_EQ("inline namespace A {\n"
 "  int i;\n"
+"  int j;\n"
 "}// namespace A",
 fixNamespaceEndComments("inline namespace A {\n"
 "  int i;\n"
+"  int j;\n"

[PATCH] D30528: [clang-format] Use number of unwrapped lines for short namespace

2017-03-02 Thread Daniel Jasper via Phabricator via cfe-commits
djasper accepted this revision.
djasper added a comment.
This revision is now accepted and ready to land.

Please include the reasoning in the patch description, i.e. that otherwise 
clang-format might need to runs to add all the namespace comments.


https://reviews.llvm.org/D30528



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits