Skip to content

Commit bade51b

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 bade51b

File tree

6 files changed

+169
-1
lines changed

6 files changed

+169
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ Changes in existing checks
287287
<clang-tidy/checks/cppcoreguidelines/pro-type-union-access>` check to
288288
report a location even when the member location is not valid.
289289

290+
- Improved :doc:`google-readability-namespace-comments
291+
<clang-tidy/checks/google/readability-namespace-comments>` check to
292+
add an option ``AllowNoNamespaceComments``, that permits
293+
omitting namespace comments entirely.
294+
290295
- Improved :doc:`misc-definitions-in-headers
291296
<clang-tidy/checks/misc/definitions-in-headers>` check by rewording the
292297
diagnostic note that suggests adding ``inline``.

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

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

4040
An unsigned integer specifying the number of spaces before the comment
4141
closing a namespace definition. Default is `1U`.
42+
43+
.. option:: AllowNoNamespaceComments
44+
45+
When `true`, the check will allow that namespace comments are omitted
46+
entirely. The check only fails if a namespace comment is present but does
47+
not match namespace definition. Default is `false`.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
void hh();
19+
20+
// 1
21+
// 2
22+
// 3
23+
// 4
24+
// 5
25+
// 6
26+
// 7
27+
} // namespace n7::inline n8
28+
29+
namespace n9::inline n10 {
30+
// make namespace above 10 lines
31+
void hh();
32+
// 1
33+
// 2
34+
// 3
35+
// 4
36+
// 5
37+
// 6
38+
// 7
39+
} // namespace n9::n10
40+
// 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]
41+
42+
43+
namespace n11::n12 {
44+
// make namespace above 10 lines
45+
void hh();
46+
// 1
47+
// 2
48+
// 3
49+
// 4
50+
// 5
51+
// 6
52+
// 7
53+
// 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]
54+
}; // namespace n1::n2
55+
// 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)