Skip to content

Commit ae819b8

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 ae819b8

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,10 @@ 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:: AllowOmittingNamespaceComments
44+
45+
When `true`, the check will accept if no namespace comment is present.
46+
The check will only fail if the specified namespace comment is different
47+
than expected. Defaults to `false`.
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -std=c++20 \
2+
// RUN: '-config={CheckOptions: { \
3+
// RUN: google-readability-namespace-comments.AllowOmittingNamespaceComments: true, \
4+
// RUN: google-readability-namespace-comments.ShortNamespaceLines: 0, \
5+
// RUN: }}'
6+
7+
namespace n1::n2 {
8+
namespace /*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
9+
void f();
10+
}}
11+
12+
namespace n7::inline n8 {
13+
void f();
14+
}
15+
16+
namespace n9::inline n10 {
17+
void f();
18+
}
19+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: google-readability-namespace-comments.ShortNamespaceLines: 0, \
5+
// RUN: }}'
6+
7+
namespace n1 {
8+
namespace /* a comment */ n2 /* another comment */ {
9+
void f();
10+
}}
11+
12+
#define MACRO macro_expansion
13+
namespace MACRO {
14+
void f();
15+
}
16+
17+
namespace [[deprecated("foo")]] namespace_with_attr {
18+
inline namespace inline_namespace {
19+
void g();
20+
}
21+
}
22+
23+
namespace [[]] {
24+
void hh();
25+
}
26+
27+

0 commit comments

Comments
 (0)