Author: Thorsten Klein
Date: 2025-06-08T13:19:40+03:00
New Revision: 00eb22fff99559cc97fc9b24a9dbe86b89e9beae

URL: 
https://github.com/llvm/llvm-project/commit/00eb22fff99559cc97fc9b24a9dbe86b89e9beae
DIFF: 
https://github.com/llvm/llvm-project/commit/00eb22fff99559cc97fc9b24a9dbe86b89e9beae.diff

LOG: added option 
`google-readability-namespace-comments.AllowNoNamespaceComments` (#124265)

New option AllowNoNamespaceComments for
`google-readability-namespace-comments.AllowNoNamespaceComments` 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`

Fixes #124264

Added: 
    
clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-nested-namespaces.cpp
    
clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp

Modified: 
    clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
    clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst

Removed: 
    


################################################################################
diff  --git 
a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
index 64dc941569a96..12e52d6afad56 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
@@ -27,11 +27,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)),
+      
AllowOmittingNamespaceComments(Options.get("AllowOmittingNamespaceComments", 
false)) {}
 
 void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
   Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
+  Options.store(Opts, "AllowOmittingNamespaceComments", 
AllowOmittingNamespaceComments);
 }
 
 void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -140,6 +142,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) {
@@ -158,6 +161,8 @@ void NamespaceCommentCheck::check(const 
MatchFinder::MatchResult &Result) {
         return;
       }
 
+      hasComment = true;
+
       // Otherwise we need to fix the comment.
       NeedLineBreak = Comment.starts_with("/*");
       OldCommentRange =
@@ -183,6 +188,11 @@ void NamespaceCommentCheck::check(const 
MatchFinder::MatchResult &Result) {
       ND->isAnonymousNamespace() ? "anonymous namespace"
                                  : ("namespace '" + *NamespaceNameAsWritten + 
"'");
 
+  // If no namespace comment is allowed
+  if(!hasComment && AllowOmittingNamespaceComments) {
+    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 7607d37b1b2fd..8edd77213f779 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 AllowOmittingNamespaceComments;
   llvm::SmallVector<SourceLocation, 4> Ends;
 };
 

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 97f66a18c7c99..19ccd1790e757 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -191,6 +191,16 @@ Changes in existing checks
   <clang-tidy/checks/concurrency/mt-unsafe>` check by fixing a false positive
   where ``strerror`` was flagged as MT-unsafe.
 
+- Improved :doc:`google-readability-namespace-comments
+  <clang-tidy/checks/google/readability-namespace-comments>` check by adding
+  the option `AllowOmittingNamespaceComments` to accept if a namespace comment
+  is omitted entirely.
+
+- Improved :doc:`llvm-namespace-comment
+  <clang-tidy/checks/llvm/namespace-comment>` check by adding the option
+  `AllowOmittingNamespaceComments` to accept if a namespace comment is omitted
+  entirely.
+
 - Improved :doc:`misc-const-correctness
   <clang-tidy/checks/misc/const-correctness>` check by adding the option
   `AllowedTypes`, that excludes specified types from const-correctness

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 be90260be73af..23673c9b4cfd4 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,9 @@ Options
 
    An unsigned integer specifying the number of spaces before the comment
    closing a namespace definition. Default is `1U`.
+
+.. option:: AllowOmittingNamespaceComments
+
+   When `true`, the check will accept if no namespace comment is present.
+   The check will only fail if the specified namespace comment is 
diff erent
+   than expected. Default is `false`.

diff  --git 
a/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-nested-namespaces.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-nested-namespaces.cpp
new file mode 100644
index 0000000000000..de42df30cb796
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-nested-namespaces.cpp
@@ -0,0 +1,32 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t 
-std=c++20 \
+// RUN:   '-config={CheckOptions: { \
+// RUN:     
google-readability-namespace-comments.AllowOmittingNamespaceComments: true, \
+// RUN:     google-readability-namespace-comments.ShortNamespaceLines: 0, \
+// RUN:   }}'
+
+// accept if namespace comments are fully omitted
+namespace n1::n2 {
+namespace 
/*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
+void f();
+}}
+
+namespace n5::inline n6 {
+void f();
+}
+
+namespace n7::inline n8 {
+void f();
+}
+
+// accept if namespace comments are partly omitted (e.g. only for nested 
namespace)
+namespace n1::n2 {
+namespace n3::n4 {
+void f();
+}
+} // namespace n1::n2
+
+// fail if namespace comment is 
diff erent than expected
+namespace n9::inline n10 {
+void f();
+} // 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]

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 0000000000000..019add1ed06d2
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp
@@ -0,0 +1,40 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t \
+// RUN:   -config='{CheckOptions: { \
+// RUN:     
google-readability-namespace-comments.AllowOmittingNamespaceComments: true, \
+// RUN:     google-readability-namespace-comments.ShortNamespaceLines: 0, \
+// RUN:   }}'
+
+// accept if namespace comments are fully omitted
+namespace n1 {
+namespace /* a comment */ n2 /* another comment */ {
+void f();
+}}
+
+#define MACRO macro_expansion
+namespace MACRO {
+void f();
+}
+
+namespace [[deprecated("foo")]] namespace_with_attr {
+inline namespace inline_namespace {
+void f();
+}
+}
+
+namespace [[]] {
+void f();
+}
+
+// accept if namespace comments are partly omitted (e.g. only for nested 
namespace)
+namespace n3 {
+namespace n4 {
+void f();
+} // n4
+}
+
+// fail if namespace comment is 
diff erent than expected
+namespace n1 {
+void f();
+} // namespace n2
+// CHECK-MESSAGES: :[[@LINE-1]]:2: warning: namespace 'n1' ends with a comment 
that refers to a wrong namespace 'n2' [google-readability-namespace-comments]
+


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

Reply via email to