[clang] Add missing declarations of explicit template instantiations. (PR #86964)
https://github.com/tkoeppe created https://github.com/llvm/llvm-project/pull/86964 Found with -Wundefined-func-template. From 8576f816ce9873cf4212134d7cb9c985c4e04a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Thu, 28 Mar 2024 15:44:40 + Subject: [PATCH] Add missing declarations of explicit template instantiations. Found with -Wundefined-func-template. --- clang/lib/AST/Interp/ByteCodeStmtGen.h | 1 + 1 file changed, 1 insertion(+) diff --git a/clang/lib/AST/Interp/ByteCodeStmtGen.h b/clang/lib/AST/Interp/ByteCodeStmtGen.h index ab7a591fb798ee..d7e6e5042c2740 100644 --- a/clang/lib/AST/Interp/ByteCodeStmtGen.h +++ b/clang/lib/AST/Interp/ByteCodeStmtGen.h @@ -82,6 +82,7 @@ class ByteCodeStmtGen final : public ByteCodeExprGen { OptLabelTy DefaultLabel; }; +extern template class ByteCodeStmtGen; extern template class ByteCodeExprGen; } // namespace interp ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] Add missing declarations of explicit template instantiations. (PR #86964)
tkoeppe wrote: I don't have commit access and would welcome if someone else could merge this. https://github.com/llvm/llvm-project/pull/86964 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From d8a46761ef175194c7e7272313807d2ef25aa497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- .../checkers/modernize/use-nullptr.cpp| 22 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..c32524eee88bc8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,14 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + T x; +}; +void test_templated() { + pear p = { NULL }; tkoeppe wrote: Done now. A key insight was that the test defines `NULL` as `0`, so in order for the new check to kick in, we're using `__null` as the to-be-diagnosed spelling. https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,14 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + T x; +}; +void test_templated() { + pear p = { NULL }; tkoeppe wrote: It definitely does, but I was hoping that the absence would cause some kind of test failure! I.e. "first make sure you see the test fail"... Let me add a bogus expectation to confirm. https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From d6a67729477b178d2439dfd31539a82ab7041e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH 1/2] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- .../test/clang-tidy/checkers/modernize/use-nullptr.cpp| 8 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..1807d6bd56125b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,14 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + T x; +}; +void test_templated() { + pear p = { NULL }; + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; From 8fcc4b809cc9418473d362ef344c2fb0106f47ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 20:25:13 + Subject: [PATCH 2/2] Add bogus CHECK to see if this makes the test fail --- .../test/clang-tidy/checkers/modernize/use-nullptr.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 1807d6bd56125b..3c2e486538fe07 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -89,6 +89,7 @@ template struct pear { }; void test_templated() { pear p = { NULL }; + // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use fullptr dummy(p.x); } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 6e056a2aad2b9b7957c18c8dfb8f64a44c7c785f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- .../clang-tidy/checkers/modernize/use-nullptr.cpp | 11 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..df99a78efd0c80 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,17 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + pear() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + T x; +}; +void test_templated() { + pear p; + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
tkoeppe wrote: What happens next, do we need more approvals? @EugeneZelenko? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,29 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer tkoeppe wrote: That was the whole thing we were belaboring earlier -- this test defines `NULL` to `0` and thus isn't caught, but in production, it's defined as `__null`, where the new code catches it. We don't actually have code to detect "this was spelled as a macro", I'm afraid (right, @zygoloid?). https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 1149cbacb6b8376b6ed3d7651f0fa51360a67ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 ++- clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../checkers/modernize/use-nullptr.cpp| 25 +++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..2c36349da896cf 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,31 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. (We only check __null here, + // because in this test NULL is defined as 0, but real library implementations + // it is often defined as __null and the check will catch it.) + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-MESSAGES-NOT: :[[@LINE-1]] warning: use nullptr + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,29 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer tkoeppe wrote: Sure, done. https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe created https://github.com/llvm/llvm-project/pull/109169 Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) From d6a67729477b178d2439dfd31539a82ab7041e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- .../test/clang-tidy/checkers/modernize/use-nullptr.cpp| 8 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..1807d6bd56125b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,14 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + T x; +}; +void test_templated() { + pear p = { NULL }; + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), tkoeppe wrote: This is explained somewhat in the unit test: When `NULL` is defined to `__null`, then this change will catch both cases. But we don't have a direct check for "the origin was the macro `NULL`", I'm afraid. @zygoloid suggested this change as a reasonable, small change that will be effective in many real-world cases. I'd be happy to have a more general solution, but couldn't find one, and the present one will already do a lot of good. What do you think? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 07df4ed66b36fab2da9a2ae83ab85d8fcb39aa3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 12 - clang-tools-extra/docs/ReleaseNotes.rst | 4 +++ .../checkers/modernize/use-nullptr.cpp| 25 +++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..108717e151b577 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -35,10 +35,20 @@ AST_MATCHER(Type, sugaredNullptrType) { /// to null within. /// Finding sequences of explicit casts is necessary so that an entire sequence /// can be replaced instead of just the inner-most implicit cast. +/// +/// TODO/NOTE: The second "anyOf" below discards matches on a substituted type, +/// since we don't know if that would _always_ be a pointer type for all other +/// specializations, unless the expression was "__null", in which case we assume +/// that all specializations are expected to be for pointer types. Ideally this +/// would check for the "NULL" macro instead, but that'd be harder to express. +/// In practice, "NULL" is often defined as "__null", and this is a useful +/// condition. StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..2c36349da896cf 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,31 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. (We only check __null here, + // because in this test NULL is defined as 0, but real library implementations + // it is often defined as __null and the check will catch it.) + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-MESSAGES-NOT: :[[@LINE-1]] warning: use nullptr + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.or
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), tkoeppe wrote: Done, PTAL! https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
tkoeppe wrote: @EugeneZelenko Could you please have another look? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), tkoeppe wrote: Sure -- do you mean here in the implementation, or elsewhere? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), tkoeppe wrote: How about: ``` /// TODO/NOTE: The second "anyOf" below discards matches on a substituted type, /// since we don't know if that would _always_ be a pointer type for all other /// specializations, unless the expression was "__null", in which case we assume /// that all specializations are expected to be for pointer types. Ideally this /// would check for the NULL macro instead, but that'd be harder to express. In /// practice, NULL is often defined as __null, and this is a useful condition. ``` ? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 2f98433ec43d9285e1701cddd31a0d181f0bbcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 22 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..50989dd896d44e 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + `NULL`/`__null` (but not `0`) when used with a templated type. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..c32524eee88bc8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -183,6 +183,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + `NULL`/`__null` (but not `0`) when used with a templated type. tkoeppe wrote: Ah yes, will do! https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } tkoeppe wrote: Done, PTAL? https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 2e1e6b573aaee9a528c0a5af79c65f4cba94bcc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 23 +++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..66c1bf17e113a7 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,29 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-FIXES-NOT: warning: use nullptr + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 386d94da472178be9cdc6f5f3720be431d0744e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 24 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..a31d07089f2390 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,30 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-MESSAGES-NOT: warning: use nullptr + // CHECK-FIXES-NOT: y = nullptr; + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From a7ea1466bb42d701f834c984b3225120666ad4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 24 +++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..f9c8f81a04126b 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,30 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-MESSAGES-NOT: :[[@LINE-1]] warning: use nullptr + // CHECK-FIXES-NOT: y = nullptr; + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } tkoeppe wrote: PTAL https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 6077d493dfe2cbfa053a155c562475b3b41ed007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 22 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..9fb67d2ffd2afd 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -183,6 +183,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Improved :doc:`modernize-use-nullptr + ` check to also + recognize `NULL`/`__null` (but not `0`) when used with a templated type. + Removed checks ^^ diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..c32524eee88bc8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
tkoeppe wrote: Done! https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -183,6 +183,10 @@ Changes in existing checks ` check to remove `->`, when redundant `get()` is removed. +- Improved :doc:`modernize-use-nullptr tkoeppe wrote: Oh I see, I didn't realize there was such an ordering. Done. https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From 2b86607a6b3d2909c2e3757b3f9e4d5f3bf5997f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 22 +++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..da71cfbe2ffa95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + `NULL`/`__null` (but not `0`) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..c32524eee88bc8 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
@@ -84,6 +84,28 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } tkoeppe wrote: OK, I see, done. https://github.com/llvm/llvm-project/pull/109169 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] modernize-use-nullptr matches "NULL" in templates (PR #109169)
https://github.com/tkoeppe updated https://github.com/llvm/llvm-project/pull/109169 From d4e208f89ff04cfab366cdc6849bea25613090e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= Date: Wed, 18 Sep 2024 17:04:44 + Subject: [PATCH] [clang-tidy] Make modernize-use-nullptr matcher also match "NULL", but not "0", when it appears on a substituted type of a template specialization. Previously, any matches on a substituted type were excluded, but this meant that a situation like the following is not diagnosed: ```c++ template struct X { T val; X() { val = NULL; } // should diagnose }; ``` When the user says `NULL`, we expect that the destination type is always meant to be a pointer type, so this should be converted to `nullptr`. By contrast, we do not propose changing a literal `0` in that case, which appears as initializers of both pointer and integer specializations in reasonable real code. (If `NULL` is used erroneously in such a situation, it should be changed to `0` or `{}`.) --- .../clang-tidy/modernize/UseNullptrCheck.cpp | 4 +++- clang-tools-extra/docs/ReleaseNotes.rst | 4 .../checkers/modernize/use-nullptr.cpp| 23 +++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index 6a003a347badac..b2921690863b84 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -38,7 +38,9 @@ AST_MATCHER(Type, sugaredNullptrType) { StatementMatcher makeCastSequenceMatcher(llvm::ArrayRef NameList) { auto ImplicitCastToNull = implicitCastExpr( anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)), - unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType(, + anyOf(hasSourceExpression(gnuNullExpr()), +unless(hasImplicitDestinationType( +qualType(substTemplateTypeParmType(), unless(hasSourceExpression(hasType(sugaredNullptrType(, unless(hasImplicitDestinationType( qualType(matchers::matchesAnyListedTypeName(NameList); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index d032cef6b76164..b2bb7549f840cc 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -157,6 +157,10 @@ Changes in existing checks a false positive when only an implicit conversion happened inside an initializer list. +- Improved :doc:`modernize-use-nullptr + ` check to also recognize + ``NULL``/``__null`` (but not ``0``) when used with a templated type. + - Improved :doc:`modernize-use-std-print ` check to support replacing member function calls too. diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp index 7bc0925136aa86..cd576daa9287b9 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-nullptr.cpp @@ -84,6 +84,29 @@ void test_macro_expansion4() { #undef MY_NULL } +template struct pear { + // If you say __null (or NULL), we assume that T will always be a pointer + // type, so we suggest replacing it with nullptr. + void f() { x = __null; } + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr [modernize-use-nullptr] + // CHECK-FIXES: x = nullptr; + + // But if you say 0, we allow the possibility that T can be used with integral + // and pointer types, and "0" is an acceptable initializer (even if "{}" might + // be even better). + void g() { y = 0; } + // CHECK-MESSAGES-NOT: :[[@LINE-1]] warning: use nullptr + + T x; + T y; +}; +void test_templated() { + pear p; + p.f(); + p.g(); + dummy(p.x); +} + #define IS_EQ(x, y) if (x != y) return; void test_macro_args() { int i = 0; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits