[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)

Thorsten Klein via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 24 04:54:45 PST 2025


https://github.com/thorsten-klein created https://github.com/llvm/llvm-project/pull/124265

new option AllowNoNamespaceComments for `google-readability-namespace-comments.AllowNoNamespaceComments` is added.

Ref: #124264

When true, the check will allow that no namespace comment is present. If a namespace comment is added but it is not matching, the check will fail. Default is `false`

>From 70cb3de61ae28220c556522249d1db9b93eaec85 Mon Sep 17 00:00:00 2001
From: "Klein, Thorsten (GDE-EDSI1)" <Thorsten.Klein at bshg.com>
Date: Fri, 24 Jan 2025 13:46:24 +0100
Subject: [PATCH] added option AllowNoNamespaceComments for
 google-readability-namespace-comments

new option AllowNoNamespaceComments for
google-readability-namespace-comments is added.

When true, the check will allow that no namespace comment is present. If
a namespace comment is added but it is not matching, the check will
fail. Default is `false`
---
 .../readability/NamespaceCommentCheck.cpp     | 12 ++-
 .../readability/NamespaceCommentCheck.h       |  1 +
 .../checks/llvm/namespace-comment.rst         |  7 ++
 ...ility-namespace-comments-missing-c++17.cpp | 59 ++++++++++++
 ...readability-namespace-comments-missing.cpp | 91 +++++++++++++++++++
 5 files changed, 169 insertions(+), 1 deletion(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
 create mode 100644 clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp

diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
index 120ec02e9ad7dc..fd306d5b5fb08b 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
@@ -28,11 +28,13 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
           "namespace( +(((inline )|([a-zA-Z0-9_:]))+))?\\.? *(\\*/)?$",
           llvm::Regex::IgnoreCase),
       ShortNamespaceLines(Options.get("ShortNamespaceLines", 1U)),
-      SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)) {}
+      SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)),
+      AllowNoNamespaceComments(Options.get("AllowNoNamespaceComments", false)) {}
 
 void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
   Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
+  Options.store(Opts, "AllowNoNamespaceComments", AllowNoNamespaceComments);
 }
 
 void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -141,6 +143,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
 
   SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
   std::string Message = "%0 not terminated with a closing comment";
+  bool hasComment = false;
 
   // Try to find existing namespace closing comment on the same line.
   if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
@@ -159,6 +162,8 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
         return;
       }
 
+      hasComment = true;
+
       // Otherwise we need to fix the comment.
       NeedLineBreak = Comment.starts_with("/*");
       OldCommentRange =
@@ -184,6 +189,11 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
       ND->isAnonymousNamespace() ? "anonymous namespace"
                                  : ("namespace '" + *NamespaceNameAsWritten + "'");
 
+  // If no namespace comment is allowed
+  if(!hasComment && AllowNoNamespaceComments) {
+    return;
+  }
+
   std::string Fix(SpacesBeforeComments, ' ');
   Fix.append("// namespace");
   if (!ND->isAnonymousNamespace())
diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
index 7607d37b1b2fd8..1ecb37fdd8d5da 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
@@ -34,6 +34,7 @@ class NamespaceCommentCheck : public ClangTidyCheck {
   llvm::Regex NamespaceCommentPattern;
   const unsigned ShortNamespaceLines;
   const unsigned SpacesBeforeComments;
+  const bool AllowNoNamespaceComments;
   llvm::SmallVector<SourceLocation, 4> Ends;
 };
 
diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
index be90260be73af3..f722800bebc460 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
@@ -39,3 +39,10 @@ Options
 
    An unsigned integer specifying the number of spaces before the comment
    closing a namespace definition. Default is `1U`.
+
+
+.. option:: AllowNoNamespaceComments
+
+   When true, the check will allow that no namespace comment is present.
+   If a namespace comment is added but it is not matching, the check will fail. Default is `false`.
+
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
new file mode 100644
index 00000000000000..036a100a1fbaf6
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
@@ -0,0 +1,59 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     google-readability-namespace-comments.AllowNoNamespaceComments: true, \
+// RUN:   }}'
+
+namespace n1::n2 {
+namespace /*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
+
+// So that namespace is not empty.
+void f();
+
+
+}}
+
+namespace n7::inline n8 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+} // namespace n7::inline n8
+
+namespace n9::inline n10 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+} // namespace n9::n10
+// CHECK-MESSAGES: :[[@LINE-1]]:2: warning: namespace 'n9::inline n10' ends with a comment that refers to a wrong namespace 'n9::n10' [google-readability-namespace-comments]
+
+
+namespace n11::n12 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+// CHECK-MESSAGES: :[[@LINE-1]]:2: warning: namespace 'n11::n12' ends with a comment that refers to a wrong namespace 'n1::n2' [google-readability-namespace-comments]
+} // namespace n1::n2
+// CHECK-FIXES: }  // namespace n11::n12
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp
new file mode 100644
index 00000000000000..b0e6b4206103c9
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp
@@ -0,0 +1,91 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     google-readability-namespace-comments.AllowNoNamespaceComments: true, \
+// RUN:   }}'
+
+namespace n1 {
+namespace /* a comment */ n2 /* another comment */ {
+
+
+void f(); // So that the namespace isn't empty.
+
+
+}}
+
+#define MACRO macro_expansion
+namespace MACRO {
+void f(); // So that the namespace isn't empty.
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace macro_expansion {
+void ff(); // So that the namespace isn't empty.
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace [[deprecated("foo")]] namespace_with_attr {
+inline namespace inline_namespace {
+void g();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+}
+
+namespace [[]] {
+void hh();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace short1 {
+namespace short2 {
+// Namespaces covering 10 lines or fewer
+}
+}
+
+namespace n3 {
+
+
+
+
+
+
+
+
+
+} // namespace n3
+
+namespace n4 {
+void hh();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+// CHECK-MESSAGES: :[[@LINE+1]]:2: warning: namespace 'n4' ends with a comment that refers to a wrong namespace 'n5' [google-readability-namespace-comments]
+}; // namespace n5
+// CHECK-FIXES: }  // namespace n4



More information about the cfe-commits mailing list