[PATCH] [clang-tidy] Add an option to replace unrecognized namespace ending comments.

Alexander Kornienko alexfh at google.com
Thu Mar 5 06:48:47 PST 2015


Remove unrecognized namespace ending line comments unconditionally.


http://reviews.llvm.org/D8078

Files:
  clang-tidy/readability/NamespaceCommentCheck.cpp
  unittests/clang-tidy/ReadabilityModuleTest.cpp

Index: clang-tidy/readability/NamespaceCommentCheck.cpp
===================================================================
--- clang-tidy/readability/NamespaceCommentCheck.cpp
+++ clang-tidy/readability/NamespaceCommentCheck.cpp
@@ -82,6 +82,9 @@
   // to insert a line break.
   bool NeedLineBreak = NextTokenIsOnSameLine && Tok.isNot(tok::eof);
 
+  SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
+  StringRef Message = "%0 not terminated with a closing comment";
+
   // Try to find existing namespace closing comment on the same line.
   if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
     StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
@@ -101,32 +104,34 @@
 
       // Otherwise we need to fix the comment.
       NeedLineBreak = Comment.startswith("/*");
-      CharSourceRange OldCommentRange = CharSourceRange::getCharRange(
-          SourceRange(Loc, Loc.getLocWithOffset(Tok.getLength())));
-      diag(Loc, "namespace closing comment refers to a wrong namespace '%0'")
-          << NamespaceNameInComment
-          << FixItHint::CreateReplacement(
-                 OldCommentRange, getNamespaceComment(ND, NeedLineBreak));
-      return;
-    }
-
-    // This is not a recognized form of a namespace closing comment.
-    // Leave line comment on the same line. Move block comment to the next line,
-    // as it can be multi-line or there may be other tokens behind it.
-    if (Comment.startswith("//"))
+      OldCommentRange =
+          SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
+      Message =
+          (llvm::Twine(
+               "%0 ends with a comment that refers to a wrong namespace '") +
+           NamespaceNameInComment + "'").str();
+    } else if (Comment.startswith("//")) {
+      // Assume that this is an unrecognized form of a namespace closing line
+      // comment. Replace it.
       NeedLineBreak = false;
+      OldCommentRange =
+          SourceRange(AfterRBrace, Loc.getLocWithOffset(Tok.getLength()));
+      Message = "%0 ends with an unrecognized comment";
+    }
+    // If it's a block comment, just move it to the next line, as it can be
+    // multi-line or there may be other tokens behind it.
   }
 
   std::string NamespaceName =
       ND->isAnonymousNamespace()
           ? "anonymous namespace"
           : ("namespace '" + ND->getNameAsString() + "'");
 
-  diag(AfterRBrace, "%0 not terminated with a closing comment")
+  diag(AfterRBrace, Message)
       << NamespaceName
-      << FixItHint::CreateInsertion(AfterRBrace,
-                                    std::string(SpacesBeforeComments, ' ') +
-                                        getNamespaceComment(ND, NeedLineBreak));
+      << FixItHint::CreateReplacement(
+             OldCommentRange, std::string(SpacesBeforeComments, ' ') +
+                                  getNamespaceComment(ND, NeedLineBreak));
   diag(ND->getLocation(), "%0 starts here", DiagnosticIDs::Note)
       << NamespaceName;
 }
Index: unittests/clang-tidy/ReadabilityModuleTest.cpp
===================================================================
--- unittests/clang-tidy/ReadabilityModuleTest.cpp
+++ unittests/clang-tidy/ReadabilityModuleTest.cpp
@@ -75,11 +75,6 @@
             runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
                                                   "}\n"
                                                   "// namespace"));
-  // Leave unknown comments.
-  EXPECT_EQ("namespace {\n"
-            "} // namespace // random text",
-            runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
-                                                  "} // random text"));
 }
 
 TEST(NamespaceCommentCheckTest, FixWrongComments) {
@@ -94,6 +89,11 @@
             "} // namespace",
             runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
                                                   "} // namespace asdf"));
+  // Remove unknown comments.
+  EXPECT_EQ("namespace {\n"
+            "} // namespace",
+            runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
+                                                  "} // random text"));
 }
 
 TEST(BracesAroundStatementsCheck, IfWithComments) {

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8078.21276.patch
Type: text/x-patch
Size: 4214 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150305/7e4a3da1/attachment.bin>


More information about the cfe-commits mailing list