Skip to content

Commit 15a8d29

Browse files
added option AllowNoNamespaceComments for google-readability-namespace-comments
new option AllowNoNamespaceComments added for google-readability-namespace-comments. When true, the check will allow that namespace comments are ommitted entirely. The check only fails if a namespace comment is present but does not match. Default is `false`.
1 parent e289cb5 commit 15a8d29

File tree

6 files changed

+176
-1
lines changed

6 files changed

+176
-1
lines changed

clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
2828
"namespace( +(((inline )|([a-zA-Z0-9_:]))+))?\\.? *(\\*/)?$",
2929
llvm::Regex::IgnoreCase),
3030
ShortNamespaceLines(Options.get("ShortNamespaceLines", 1U)),
31-
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)) {}
31+
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)),
32+
AllowNoNamespaceComments(Options.get("AllowNoNamespaceComments", false)) {}
3233

3334
void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
3435
Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
3536
Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
37+
Options.store(Opts, "AllowNoNamespaceComments", AllowNoNamespaceComments);
3638
}
3739

3840
void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -141,6 +143,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
141143

142144
SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
143145
std::string Message = "%0 not terminated with a closing comment";
146+
bool hasComment = false;
144147

145148
// Try to find existing namespace closing comment on the same line.
146149
if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
@@ -159,6 +162,8 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
159162
return;
160163
}
161164

165+
hasComment = true;
166+
162167
// Otherwise we need to fix the comment.
163168
NeedLineBreak = Comment.starts_with("/*");
164169
OldCommentRange =
@@ -184,6 +189,11 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
184189
ND->isAnonymousNamespace() ? "anonymous namespace"
185190
: ("namespace '" + *NamespaceNameAsWritten + "'");
186191

192+
// If no namespace comment is allowed
193+
if(!hasComment && AllowNoNamespaceComments) {
194+
return;
195+
}
196+
187197
std::string Fix(SpacesBeforeComments, ' ');
188198
Fix.append("// namespace");
189199
if (!ND->isAnonymousNamespace())

clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class NamespaceCommentCheck : public ClangTidyCheck {
3434
llvm::Regex NamespaceCommentPattern;
3535
const unsigned ShortNamespaceLines;
3636
const unsigned SpacesBeforeComments;
37+
const bool AllowNoNamespaceComments;
3738
llvm::SmallVector<SourceLocation, 4> Ends;
3839
};
3940

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ Changes in existing checks
402402
<clang-tidy/checks/readability/use-std-min-max>` check to use correct template
403403
type in ``std::min`` and ``std::max`` when operand is integer literal.
404404

405+
- Improved :doc:`google-readability-namespace-comments
406+
<clang-tidy/checks/google/readability-namespace-comments>` that permits
407+
omitting namespace comments entirely. With this option enabled, the check
408+
only fails if a namespace comment is present but does not match.
409+
410+
405411
Removed checks
406412
^^^^^^^^^^^^^^
407413

clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ Options
3939

4040
An unsigned integer specifying the number of spaces before the comment
4141
closing a namespace definition. Default is `1U`.
42+
43+
44+
.. option:: AllowNoNamespaceComments
45+
46+
When true, the check will allow that namespace comments are ommitted
47+
entirely. The check only fails if a namespace comment is present but does
48+
not match. Default is `false`.
49+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
2+
// RUN: -config='{CheckOptions: { \
3+
// RUN: google-readability-namespace-comments.AllowNoNamespaceComments: true, \
4+
// RUN: }}'
5+
6+
namespace n1::n2 {
7+
namespace /*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
8+
9+
// So that namespace is not empty.
10+
void f();
11+
12+
13+
}}
14+
15+
namespace n7::inline n8 {
16+
// make namespace above 10 lines
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
} // namespace n7::inline n8
28+
29+
namespace n9::inline n10 {
30+
// make namespace above 10 lines
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
} // namespace n9::n10
42+
// 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]
43+
44+
45+
namespace n11::n12 {
46+
// make namespace above 10 lines
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
// 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]
58+
} // namespace n1::n2
59+
// CHECK-FIXES: } // namespace n11::n12
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
2+
// RUN: -config='{CheckOptions: { \
3+
// RUN: google-readability-namespace-comments.AllowNoNamespaceComments: true, \
4+
// RUN: }}'
5+
6+
namespace n1 {
7+
namespace /* a comment */ n2 /* another comment */ {
8+
9+
10+
void f(); // So that the namespace isn't empty.
11+
12+
13+
}}
14+
15+
#define MACRO macro_expansion
16+
namespace MACRO {
17+
void f(); // So that the namespace isn't empty.
18+
// 1
19+
// 2
20+
// 3
21+
// 4
22+
// 5
23+
// 6
24+
// 7
25+
}
26+
27+
namespace macro_expansion {
28+
void ff(); // So that the namespace isn't empty.
29+
// 1
30+
// 2
31+
// 3
32+
// 4
33+
// 5
34+
// 6
35+
// 7
36+
}
37+
38+
namespace [[deprecated("foo")]] namespace_with_attr {
39+
inline namespace inline_namespace {
40+
void g();
41+
// 1
42+
// 2
43+
// 3
44+
// 4
45+
// 5
46+
// 6
47+
// 7
48+
}
49+
}
50+
51+
namespace [[]] {
52+
void hh();
53+
// 1
54+
// 2
55+
// 3
56+
// 4
57+
// 5
58+
// 6
59+
// 7
60+
}
61+
62+
namespace short1 {
63+
namespace short2 {
64+
// Namespaces covering 10 lines or fewer
65+
}
66+
}
67+
68+
namespace n3 {
69+
70+
71+
72+
73+
74+
75+
76+
77+
78+
} // namespace n3
79+
80+
namespace n4 {
81+
void hh();
82+
// 1
83+
// 2
84+
// 3
85+
// 4
86+
// 5
87+
// 6
88+
// 7
89+
// CHECK-MESSAGES: :[[@LINE+1]]:2: warning: namespace 'n4' ends with a comment that refers to a wrong namespace 'n5' [google-readability-namespace-comments]
90+
}; // namespace n5
91+
// CHECK-FIXES: } // namespace n4

0 commit comments

Comments
 (0)