-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy][NFC] add '.clang-tidy' config for clang-tidy project #147793
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
Conversation
|
@llvm/pr-subscribers-clang-tidy Author: Baranov Victor (vbvictor) ChangesAdded Disabled checks will be enabled in future PRs after fixing their warnings. Full diff: https://github.com/llvm/llvm-project/pull/147793.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
new file mode 100644
index 0000000000000..45d1929b6b742
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -0,0 +1,11 @@
+InheritParentConfig: true
+Checks: [
+ bugprone-*
+ -bugprone-assignment-in-if-condition,
+ -bugprone-branch-clone,
+ -bugprone-easily-swappable-parameters,
+ -bugprone-narrowing-conversions,
+ -bugprone-suspicious-stringview-data-usage,
+ -bugprone-unchecked-optional-access,
+ -bugprone-unused-return-value,
+]
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 28e8fe002d575..6565fa3f7c85b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -129,13 +129,10 @@ void CrtpConstructorAccessibilityCheck::check(
<< HintFriend;
}
- auto WithFriendHintIfNeeded =
- [&](const DiagnosticBuilder &Diag,
- bool NeedsFriend) -> const DiagnosticBuilder & {
+ auto WithFriendHintIfNeeded = [&](const DiagnosticBuilder &Diag,
+ bool NeedsFriend) {
if (NeedsFriend)
Diag << HintFriend;
-
- return Diag;
};
if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 88d2f2c388d07..88e048e65d4e8 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -370,16 +370,16 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
<< E->getSourceRange();
} else if (Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type");
- if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy))
- SizeofArgTy = member->getPointeeType().getTypePtr();
+ if (const auto *Member = dyn_cast<MemberPointerType>(SizeofArgTy))
+ SizeofArgTy = Member->getPointeeType().getTypePtr();
const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>("sizeof-expr");
- if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) {
+ if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
// check if the array element size is larger than one. If true,
// the size of the array is higher than the number of elements
- CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType());
- if (!sSize.isOne()) {
+ CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
+ if (!SSize.isOne()) {
diag(SzOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof' in the loop")
<< SzOfExpr->getSourceRange();
|
|
@llvm/pr-subscribers-clang-tools-extra Author: Baranov Victor (vbvictor) ChangesAdded Disabled checks will be enabled in future PRs after fixing their warnings. Full diff: https://github.com/llvm/llvm-project/pull/147793.diff 3 Files Affected:
diff --git a/clang-tools-extra/clang-tidy/.clang-tidy b/clang-tools-extra/clang-tidy/.clang-tidy
new file mode 100644
index 0000000000000..45d1929b6b742
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/.clang-tidy
@@ -0,0 +1,11 @@
+InheritParentConfig: true
+Checks: [
+ bugprone-*
+ -bugprone-assignment-in-if-condition,
+ -bugprone-branch-clone,
+ -bugprone-easily-swappable-parameters,
+ -bugprone-narrowing-conversions,
+ -bugprone-suspicious-stringview-data-usage,
+ -bugprone-unchecked-optional-access,
+ -bugprone-unused-return-value,
+]
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 28e8fe002d575..6565fa3f7c85b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -129,13 +129,10 @@ void CrtpConstructorAccessibilityCheck::check(
<< HintFriend;
}
- auto WithFriendHintIfNeeded =
- [&](const DiagnosticBuilder &Diag,
- bool NeedsFriend) -> const DiagnosticBuilder & {
+ auto WithFriendHintIfNeeded = [&](const DiagnosticBuilder &Diag,
+ bool NeedsFriend) {
if (NeedsFriend)
Diag << HintFriend;
-
- return Diag;
};
if (!CRTPDeclaration->hasUserDeclaredConstructor()) {
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 88d2f2c388d07..88e048e65d4e8 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -370,16 +370,16 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
<< E->getSourceRange();
} else if (Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type");
- if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy))
- SizeofArgTy = member->getPointeeType().getTypePtr();
+ if (const auto *Member = dyn_cast<MemberPointerType>(SizeofArgTy))
+ SizeofArgTy = Member->getPointeeType().getTypePtr();
const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>("sizeof-expr");
- if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) {
+ if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
// check if the array element size is larger than one. If true,
// the size of the array is higher than the number of elements
- CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType());
- if (!sSize.isOne()) {
+ CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
+ if (!SSize.isOne()) {
diag(SzOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof' in the loop")
<< SzOfExpr->getSourceRange();
|
|
Added all people that participated in RFC and regular clang-tidy reviewers to see this change. |
|
How about adding |
Yes, I will add more and more checks to config. Just started with
Noted, will check that. |
Yes, one go is straightforward. However, if preliminary code fixes will be necessary, they should go into separate pull request. |
86802f2 to
a0fd1b0
Compare
|
Results of running with config (disabled checks that doesn't align with our style) report.html.txt (delete |
7dbf98f to
4b061f9
Compare
EugeneZelenko
left a comment
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.
But please wait for other reviewers.
|
Will be good idea to similar Clang-Tidy configuration over other parts of LLVM code base. May be some check warnings will be easy to fix and enforce. I did such cleanups in past (by quite remote :-(). |
PiotrZSL
left a comment
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
carlosgalvezp
left a comment
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!
|
Based on carlosgalvezp's comment in #147902 (review) changed |
|
LGTM, I see there was a surprising amount of cruft we've accumulated |
5chmidti
left a comment
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.
Thanks for driving this. Let's see what checks we can enable in the follow-ups.
|
Thank you for driving this, LGTM! One last thing I'm missing: can you update the clang-tidy contributors guide, to explain that we expect people to run clang-tidy as well (and what command to run)? Since it's not currently enforced in CI. |
I will add it in a separate PR. Don't feel right to make kind-of unrelated changes after most of the people gave their consent. |
Ok! |
…ontributing.rst (#148547) Follow up to #147793. _Originally suggested by carlosgalvezp in #147793 (comment)
…hanges in Contributing.rst (#148547) Follow up to llvm/llvm-project#147793. _Originally suggested by carlosgalvezp in llvm/llvm-project#147793 (comment)
**KEY POINTS** - MVP workflow to automatically lint C++ files, located **only** in `clang-tools-extra/clang-tidy`. It's chosen this way as `clang-tools-extra/clang-tidy` is almost 100% `clang-tidy` complaint, thus we would automatically enforce a [high quality standard for clang-tidy source code](https://discourse.llvm.org/t/rfc-create-hardened-clang-tidy-config-for-clang-tidy-directory/87247). (#147793) - Implementation is very similar to code-format job, but without the ability to run `code-lint-helper.py` locally. **FOUND ISSUES** + open questions - Speed: it takes ~ 1m40sec to download and unpack `clang-tidy` plus additional ~4 mins to configure and CodeGen targets. I see that `premerge.yaml` runs on special `llvm-premerge-linux-runners` runners which can use `sccache` for speed. Can we use the same runners for this job? Exact timings can be found [here](https://github.com/llvm/llvm-project/actions/runs/17135749067/job/48611150680?pr=154223). **TO DO** - Place common components from `code-lint-helper.py` and `code-format-helper.py` into a separate file and reuse it in both CI's. - Compute CodeGen targets based on `projects_to_build`, for now `clang-tablegen-targets` is hardcoded for `clang-tools-extra/`. - Automatically generate and upload `.yaml` for `clang-apply-replacements` - Create an RFC with a plan how to enable `clang-tidy` in other projects so that Maintainers of LLVM components could choose if they want `clang-tidy` or not. - Add more linters like `pylint`, `ruff` in the future.
**KEY POINTS** - MVP workflow to automatically lint C++ files, located **only** in `clang-tools-extra/clang-tidy`. It's chosen this way as `clang-tools-extra/clang-tidy` is almost 100% `clang-tidy` complaint, thus we would automatically enforce a [high quality standard for clang-tidy source code](https://discourse.llvm.org/t/rfc-create-hardened-clang-tidy-config-for-clang-tidy-directory/87247). (llvm/llvm-project#147793) - Implementation is very similar to code-format job, but without the ability to run `code-lint-helper.py` locally. **FOUND ISSUES** + open questions - Speed: it takes ~ 1m40sec to download and unpack `clang-tidy` plus additional ~4 mins to configure and CodeGen targets. I see that `premerge.yaml` runs on special `llvm-premerge-linux-runners` runners which can use `sccache` for speed. Can we use the same runners for this job? Exact timings can be found [here](https://github.com/llvm/llvm-project/actions/runs/17135749067/job/48611150680?pr=154223). **TO DO** - Place common components from `code-lint-helper.py` and `code-format-helper.py` into a separate file and reuse it in both CI's. - Compute CodeGen targets based on `projects_to_build`, for now `clang-tablegen-targets` is hardcoded for `clang-tools-extra/`. - Automatically generate and upload `.yaml` for `clang-apply-replacements` - Create an RFC with a plan how to enable `clang-tidy` in other projects so that Maintainers of LLVM components could choose if they want `clang-tidy` or not. - Add more linters like `pylint`, `ruff` in the future.
Added
.clang-tidyconfig as discussed in RFC.Added
bugprone,readability,modernize,performancechecks that didn't create many warnings.Fixed minor warnings to make
/clang-tidydirectory complaint withclang-tidy-20.Disabled checks will be enabled in future PRs after fixing their warnings.