Skip to content

Commit 47de71f

Browse files
thorsten-kleintomtor
authored andcommitted
added option google-readability-namespace-comments.AllowNoNamespaceComments (llvm#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 llvm#124264
1 parent 88d80a8 commit 47de71f

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-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/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ Changes in existing checks
191191
<clang-tidy/checks/concurrency/mt-unsafe>` check by fixing a false positive
192192
where ``strerror`` was flagged as MT-unsafe.
193193

194+
- Improved :doc:`google-readability-namespace-comments
195+
<clang-tidy/checks/google/readability-namespace-comments>` check by adding
196+
the option `AllowOmittingNamespaceComments` to accept if a namespace comment
197+
is omitted entirely.
198+
199+
- Improved :doc:`llvm-namespace-comment
200+
<clang-tidy/checks/llvm/namespace-comment>` check by adding the option
201+
`AllowOmittingNamespaceComments` to accept if a namespace comment is omitted
202+
entirely.
203+
194204
- Improved :doc:`misc-const-correctness
195205
<clang-tidy/checks/misc/const-correctness>` check by adding the option
196206
`AllowedTypes`, that excludes specified types from const-correctness

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:: 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. Default is `false`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
// accept if namespace comments are fully omitted
8+
namespace n1::n2 {
9+
namespace /*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
10+
void f();
11+
}}
12+
13+
namespace n5::inline n6 {
14+
void f();
15+
}
16+
17+
namespace n7::inline n8 {
18+
void f();
19+
}
20+
21+
// accept if namespace comments are partly omitted (e.g. only for nested namespace)
22+
namespace n1::n2 {
23+
namespace n3::n4 {
24+
void f();
25+
}
26+
} // namespace n1::n2
27+
28+
// fail if namespace comment is different than expected
29+
namespace n9::inline n10 {
30+
void f();
31+
} // namespace n9::n10
32+
// 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]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// accept if namespace comments are fully omitted
8+
namespace n1 {
9+
namespace /* a comment */ n2 /* another comment */ {
10+
void f();
11+
}}
12+
13+
#define MACRO macro_expansion
14+
namespace MACRO {
15+
void f();
16+
}
17+
18+
namespace [[deprecated("foo")]] namespace_with_attr {
19+
inline namespace inline_namespace {
20+
void f();
21+
}
22+
}
23+
24+
namespace [[]] {
25+
void f();
26+
}
27+
28+
// accept if namespace comments are partly omitted (e.g. only for nested namespace)
29+
namespace n3 {
30+
namespace n4 {
31+
void f();
32+
} // n4
33+
}
34+
35+
// fail if namespace comment is different than expected
36+
namespace n1 {
37+
void f();
38+
} // namespace n2
39+
// CHECK-MESSAGES: :[[@LINE-1]]:2: warning: namespace 'n1' ends with a comment that refers to a wrong namespace 'n2' [google-readability-namespace-comments]
40+

0 commit comments

Comments
 (0)