-
Notifications
You must be signed in to change notification settings - Fork 14.3k
added option google-readability-namespace-comments.AllowNoNamespaceComments
#124265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added option google-readability-namespace-comments.AllowNoNamespaceComments
#124265
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clang-tidy Author: Thorsten Klein (thorsten-klein) Changesnew option AllowNoNamespaceComments for Ref: #124264 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 Full diff: https://github.com/llvm/llvm-project/pull/124265.diff 5 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
index 120ec02e9ad7dc..fd306d5b5fb08b 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
@@ -28,11 +28,13 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
"namespace( +(((inline )|([a-zA-Z0-9_:]))+))?\\.? *(\\*/)?$",
llvm::Regex::IgnoreCase),
ShortNamespaceLines(Options.get("ShortNamespaceLines", 1U)),
- SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)) {}
+ SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)),
+ AllowNoNamespaceComments(Options.get("AllowNoNamespaceComments", false)) {}
void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
+ Options.store(Opts, "AllowNoNamespaceComments", AllowNoNamespaceComments);
}
void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -141,6 +143,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
SourceRange OldCommentRange(AfterRBrace, AfterRBrace);
std::string Message = "%0 not terminated with a closing comment";
+ bool hasComment = false;
// Try to find existing namespace closing comment on the same line.
if (Tok.is(tok::comment) && NextTokenIsOnSameLine) {
@@ -159,6 +162,8 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
+ hasComment = true;
+
// Otherwise we need to fix the comment.
NeedLineBreak = Comment.starts_with("/*");
OldCommentRange =
@@ -184,6 +189,11 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
ND->isAnonymousNamespace() ? "anonymous namespace"
: ("namespace '" + *NamespaceNameAsWritten + "'");
+ // If no namespace comment is allowed
+ if(!hasComment && AllowNoNamespaceComments) {
+ return;
+ }
+
std::string Fix(SpacesBeforeComments, ' ');
Fix.append("// namespace");
if (!ND->isAnonymousNamespace())
diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
index 7607d37b1b2fd8..1ecb37fdd8d5da 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h
@@ -34,6 +34,7 @@ class NamespaceCommentCheck : public ClangTidyCheck {
llvm::Regex NamespaceCommentPattern;
const unsigned ShortNamespaceLines;
const unsigned SpacesBeforeComments;
+ const bool AllowNoNamespaceComments;
llvm::SmallVector<SourceLocation, 4> Ends;
};
diff --git a/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst b/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
index be90260be73af3..f722800bebc460 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
@@ -39,3 +39,10 @@ Options
An unsigned integer specifying the number of spaces before the comment
closing a namespace definition. Default is `1U`.
+
+
+.. option:: AllowNoNamespaceComments
+
+ 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`.
+
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
new file mode 100644
index 00000000000000..036a100a1fbaf6
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
@@ -0,0 +1,59 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
+// RUN: -config='{CheckOptions: { \
+// RUN: google-readability-namespace-comments.AllowNoNamespaceComments: true, \
+// RUN: }}'
+
+namespace n1::n2 {
+namespace /*comment1*/n3/*comment2*/::/*comment3*/inline/*comment4*/n4/*comment5*/ {
+
+// So that namespace is not empty.
+void f();
+
+
+}}
+
+namespace n7::inline n8 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+} // namespace n7::inline n8
+
+namespace n9::inline n10 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+} // namespace n9::n10
+// 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]
+
+
+namespace n11::n12 {
+// make namespace above 10 lines
+
+
+
+
+
+
+
+
+
+
+// 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]
+} // namespace n1::n2
+// CHECK-FIXES: } // namespace n11::n12
diff --git a/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp
new file mode 100644
index 00000000000000..b0e6b4206103c9
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp
@@ -0,0 +1,91 @@
+// RUN: %check_clang_tidy %s google-readability-namespace-comments %t -- \
+// RUN: -config='{CheckOptions: { \
+// RUN: google-readability-namespace-comments.AllowNoNamespaceComments: true, \
+// RUN: }}'
+
+namespace n1 {
+namespace /* a comment */ n2 /* another comment */ {
+
+
+void f(); // So that the namespace isn't empty.
+
+
+}}
+
+#define MACRO macro_expansion
+namespace MACRO {
+void f(); // So that the namespace isn't empty.
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace macro_expansion {
+void ff(); // So that the namespace isn't empty.
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace [[deprecated("foo")]] namespace_with_attr {
+inline namespace inline_namespace {
+void g();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+}
+
+namespace [[]] {
+void hh();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+}
+
+namespace short1 {
+namespace short2 {
+// Namespaces covering 10 lines or fewer
+}
+}
+
+namespace n3 {
+
+
+
+
+
+
+
+
+
+} // namespace n3
+
+namespace n4 {
+void hh();
+// 1
+// 2
+// 3
+// 4
+// 5
+// 6
+// 7
+// CHECK-MESSAGES: :[[@LINE+1]]:2: warning: namespace 'n4' ends with a comment that refers to a wrong namespace 'n5' [google-readability-namespace-comments]
+}; // namespace n5
+// CHECK-FIXES: } // namespace n4
|
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
Please, update Release Notes in clang-tools-extra/docs/ReleaseNotes.rst |
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
70cb3de
to
15a8d29
Compare
Thanks a lot for your review @vbvictor! What do you think about the option's name? Is |
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
As for now I can not think of a better name, maybe others will suggest. |
15a8d29
to
608ceb0
Compare
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
608ceb0
to
bade51b
Compare
clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
Outdated
Show resolved
Hide resolved
bade51b
to
9f3bc50
Compare
I am not a native English speaker, just a personal feeling. I prefer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you organize test. It looks like copy from other tests and have some noise which has nothing to do with the function of the current PR.
Also, please rebase to main since release note is cleared
@thorsten-klein, gentle ping, do you still wish to work on this check? |
Thank you for your gentle ping! I have rebased the branch to master and renamed the option to Regarding the tests, I'm unsure which ones should be removed. After reviewing them, I believe all the tests make sense to ensure that enabling the option does not disrupt any existing functionality, since all other tests are executed with the option |
9f3bc50
to
b1bfe06
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose "organizing tests" means:
- remove redundant file "comments-missing-c++17.cpp". Check only your feature - omitted namespace - OK, wrong name - Bad.
In your case, I think It's even better to have a second RUN command with custom suffix in the original test file. Look at narrowing-conversions-ignoreconversionfromtypes-option.cpp for reference. This will avoid copy of original test files. - make your new namespaces smaller - I guess 2 lines will be enough because default value is 1
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
...tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-c++17.cpp
Outdated
Show resolved
Hide resolved
5cd0b1c
to
ae819b8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
a683a7b
to
4f44b92
Compare
clang-tools-extra/docs/clang-tidy/checks/llvm/namespace-comment.rst
Outdated
Show resolved
Hide resolved
…e-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 the specified namespace comment is different than expected. Default is `false`.
4f44b92
to
6bbb691
Compare
@thorsten-klein, by LLVM Developer Policy you should have public email for making a contribution. |
My e-mail address is now public 👍 |
@thorsten-klein Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing-nested-namespaces.cpp clang-tools-extra/test/clang-tidy/checkers/google/readability-namespace-comments-missing.cpp clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.h View the diff from clang-format here.diff --git a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
index 12e52d6af..d6844a95c 100644
--- a/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NamespaceCommentCheck.cpp
@@ -28,12 +28,14 @@ NamespaceCommentCheck::NamespaceCommentCheck(StringRef Name,
llvm::Regex::IgnoreCase),
ShortNamespaceLines(Options.get("ShortNamespaceLines", 1U)),
SpacesBeforeComments(Options.get("SpacesBeforeComments", 1U)),
- AllowOmittingNamespaceComments(Options.get("AllowOmittingNamespaceComments", false)) {}
+ AllowOmittingNamespaceComments(
+ Options.get("AllowOmittingNamespaceComments", false)) {}
void NamespaceCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "ShortNamespaceLines", ShortNamespaceLines);
Options.store(Opts, "SpacesBeforeComments", SpacesBeforeComments);
- Options.store(Opts, "AllowOmittingNamespaceComments", AllowOmittingNamespaceComments);
+ Options.store(Opts, "AllowOmittingNamespaceComments",
+ AllowOmittingNamespaceComments);
}
void NamespaceCommentCheck::registerMatchers(MatchFinder *Finder) {
@@ -189,7 +191,7 @@ void NamespaceCommentCheck::check(const MatchFinder::MatchResult &Result) {
: ("namespace '" + *NamespaceNameAsWritten + "'");
// If no namespace comment is allowed
- if(!hasComment && AllowOmittingNamespaceComments) {
+ if (!hasComment && AllowOmittingNamespaceComments) {
return;
}
|
@vbvictor it seems this was merged before the CI checks finished running, next time please wait for that ;) Could you or @thorsten-klein fix the broken autoformatter? |
I have no access to a computer today. I can fix tomorrow, if it has not been fixed before. |
I think I've waited for all checks to pass.. |
Actually, there is a failed job if you hover over a red cross next to one of the commits, my bad. |
What I saw is that some checks don't run automatically, but must be manually triggered (I guess by a maintainer). I did click on the button to run them and immediately after you merged the patch, good syncing :) I understand this might be done to save CI resources, but clang-format is a very lightweight job that should always run. |
Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to #124265.
Thanks for fixing quickly! |
…ck` (#143305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm/llvm-project#124265.
…omments` (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
…43305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm#124265.
…omments` (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
…43305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm#124265.
…omments` (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
…43305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm#124265.
…omments` (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
…43305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm#124265.
…omments` (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
…43305) Fixed formatting and codestyle issues in `namespace-comment-check` Follow up to llvm#124265.
new option AllowNoNamespaceComments for
google-readability-namespace-comments.AllowNoNamespaceComments
is added.Fixes #124264
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