[clang-tools-extra] r219926 - [clang-tidy] Minor fixes for the NamespaceCommentCheck.

Alexander Kornienko alexfh at google.com
Thu Oct 16 08:11:54 PDT 2014


Author: alexfh
Date: Thu Oct 16 10:11:54 2014
New Revision: 219926

URL: http://llvm.org/viewvc/llvm-project?rev=219926&view=rev
Log:
[clang-tidy] Minor fixes for the NamespaceCommentCheck.

  * Make SmallVector size enough for all groups.
  * Allow trailing period in the comment.
  * Fix "// anonymous namespace qqq".


Modified:
    clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp
    clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityChecksTest.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp?rev=219926&r1=219925&r2=219926&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/NamespaceCommentCheck.cpp Thu Oct 16 10:11:54 2014
@@ -23,7 +23,7 @@ NamespaceCommentCheck::NamespaceCommentC
                                              ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       NamespaceCommentPattern("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
-                              "namespace( +([a-zA-Z0-9_]+))? *(\\*/)?$",
+                              "namespace( +([a-zA-Z0-9_]+))?\\.? *(\\*/)?$",
                               llvm::Regex::IgnoreCase),
       ShortNamespaceLines(Options.get("ShortNamespaceLines", 1u)),
       SpacesBeforeComments(Options.get("SpacesBeforeComments", 1u)) {}
@@ -85,13 +85,15 @@ void NamespaceCommentCheck::check(const
   // Try to find existing namespace closing comment on the same line.
   if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
     StringRef Comment(Sources.getCharacterData(Loc), Tok.getLength());
-    SmallVector<StringRef, 6> Groups;
+    SmallVector<StringRef, 7> Groups;
     if (NamespaceCommentPattern.match(Comment, &Groups)) {
-      StringRef NamespaceNameInComment = Groups.size() >= 6 ? Groups[5] : "";
+      StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
+      StringRef Anonymous = Groups.size() > 3 ? Groups[3] : "";
 
       // Check if the namespace in the comment is the same.
       if ((ND->isAnonymousNamespace() && NamespaceNameInComment.empty()) ||
-          ND->getNameAsString() == NamespaceNameInComment) {
+          (ND->getNameAsString() == NamespaceNameInComment &&
+           Anonymous.empty())) {
         // FIXME: Maybe we need a strict mode, where we always fix namespace
         // comments with different format.
         return;

Modified: clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityChecksTest.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityChecksTest.cpp?rev=219926&r1=219925&r2=219926&view=diff
==============================================================================
--- clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityChecksTest.cpp (original)
+++ clang-tools-extra/trunk/unittests/clang-tidy/ReadabilityChecksTest.cpp Thu Oct 16 10:11:54 2014
@@ -43,6 +43,14 @@ TEST(NamespaceCommentCheckTest, CheckExi
             "} // anonymous namespace",
             runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
                                                   "} // anonymous namespace"));
+  EXPECT_EQ("namespace {\n"
+            "} // Anonymous namespace.",
+            runCheckOnCode<NamespaceCommentCheck>("namespace {\n"
+                                                  "} // Anonymous namespace."));
+  EXPECT_EQ("namespace q {\n"
+            "} // namespace q",
+            runCheckOnCode<NamespaceCommentCheck>("namespace q {\n"
+                                                  "} // anonymous namespace q"));
   EXPECT_EQ(
       "namespace My_NameSpace123 {\n"
       "} // namespace My_NameSpace123",





More information about the cfe-commits mailing list