Skip to content

Commit b1bfe06

Browse files
added option AllowNoNamespaceComments for google-readability-namespace-comments
new option AllowOmittingNamespaceComments for google-readability-namespace-comments is added. When true, the check will accept if no namespace comment is present. The check will only fail if a namespace comment is specified which is different than expeced. Defaults to `false`.
1 parent a564425 commit b1bfe06

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-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
@@ -27,11 +27,13 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
2727
"namespace( +(((inline )|([a-zA-Z0-9_:]))+))?\\.? *(\\*/)?$",
2828
llvm::Regex::IgnoreCase),
2929
ShortNamespaceLines(Options.get("ShortNamespaceLines", 1U)),
30-
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)) {}
30+
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)),
31+
AllowOmittingNamespaceComments(Options.get("AllowOmittingNamespaceComments", false)) {}
3132

3233
void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
3334
Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
3435
Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
36+
Options.store(Opts, "AllowOmittingNamespaceComments", AllowOmittingNamespaceComments);
3537
}
3638

3739
void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -140,6 +142,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
140142

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

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

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

191+
// If no namespace comment is allowed
192+
if(!hasComment && AllowOmittingNamespaceComments) {
193+
return;
194+
}
195+
186196
std::string Fix(SpacesBeforeComments, ' ');
187197
Fix.append("// namespace");
188198
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 AllowOmittingNamespaceComments;
3738
llvm::SmallVector<SourceLocation, 4> Ends;
3839
};
3940

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:: AllowOmittingNamespaceComments
45+
46+
When true, the check will accept if no namespace comment is present.
47+
The check will only fail if a namespace comment is specified which is
48+
different than expeced. Defaults to `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.AllowOmittingNamespaceComments: 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.AllowOmittingNamespaceComments: 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)