[clang-tools-extra] [clang-tidy] Add bugprone-reset-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/3] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,33 @@ +.. title:: clang-tidy - bugprone-reset-call + +bugprone-reset-call +=== + +Finds calls to ``reset()`` method on smart pointers where the pointee type vbvictor wrote: Done, also removed parentheses in ``reset`` to follow general style https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
vbvictor wrote: > 1. Consider TK_IgnoreUnlessSpelledInSource, may simplify matchers > 2. Name is too generic, consider: > > * bugprone-smartptr-reset-pointee-reset > * bugprone-smartptr-reset-call > * bugprone-smartptr-pointee-reset > * bugprone-smartptr-reset-ambiguous-call (my prefered) > > Or any simillar. 1. Could not find a way to use TK_IgnoreUnlessSpelledInSource because code in function `template void TemplatePositiveTest() {}` is implicitly instantiated for different template types, so it is being ignored by matchers with TK_IgnoreUnlessSpelledInSource. 2. Renamed check to bugprone-smartptr-reset-ambiguous-call. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-reset-call check (PR #121291)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-reset-call check (PR #121291)
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/121291 Add new clang-tidy check that finds potentially erroneous calls to ``reset()`` method on smart pointers when the pointee type also has a ``reset()`` method. It's easy to make typo and delete object because the difference between ``.`` and ``->`` is really small. Sometimes IDE's autocomplete will change ``->`` to ``.`` automatically. For example, developer wrote ``ptr->res`` but after _Tab_ it became ``ptr.reset()``. Small example: ```cpp struct Resettable { void reset() { /* Own reset logic */ } }; auto ptr = std::make_unique(); ptr->reset(); // Calls underlying reset method ptr.reset(); // Makes the pointer null // After Fix-its (*ptr).reset(); // Clearly calls underlying reset method ptr = nullptr; // Clearly makes the pointer null ``` >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr
[clang-tools-extra] [clang-tidy] Add bugprone-reset-call check (PR #121291)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor closed https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
vbvictor wrote: > I think that this check is one that we don't want to emit fixes for, at least > not in the main diagnostic, but maybe as a fixit attached to a note > What do others think? https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor reopened https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,34 @@ +//===--- SmartptrResetAmbiguousCallCheck.h - clang-tidy -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SMARTPTRRESETAMBIGUOUSCALLCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SMARTPTRRESETAMBIGUOUSCALLCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::bugprone { + +/// Finds potentially erroneous calls to 'reset' method on smart pointers when +/// the pointee type also has a 'reset' method +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/smartptr-reset-ambiguous-call.html +class SmartptrResetAmbiguousCallCheck : public ClangTidyCheck { +public: + SmartptrResetAmbiguousCallCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { +return LangOpts.CPlusPlus; vbvictor wrote: As for now, this check only matches `::std::unique_ptr` and `::std::shared_ptr`, so this comment is valid. In the future, I think a list of smartptr-like classes should be an option for check. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
vbvictor wrote: Ping https://github.com/llvm/llvm-project/pull/123413 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 618f4a1707c1b62693c0e878040997154e7e35d6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/7] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef6..aee4a3b789863c0 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001f..3b7aba7c4be384e 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3bddeeda06e06b9..f57e951afc1ce05 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -101,6 +101,11 @@ Changes in existing checks ` check to allow specifying additional C++ member functions to match. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst b/clang-tools-extra/docs/
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/123413 >From 26c73cba1157bc538eb69c4ce11bba79c21ec3c6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH 1/4] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 +-- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a9..d1902b658061b11 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit = Result.Nodes.getNodeAs("int"); -if (Lit->getValue().ugt(Str->getLength())) { +const auto *Length = Result.Nodes.getNodeAs("length"); +if (Length->getValue().ugt(Str->getLength())) { diag(Loc, "length is bigger than string literal size"); + return; +} +if (const auto *Pos = Result.Nodes.getNodeAs("pos")) { + if (Pos->getValue().uge(Str->getLength())) { +diag(Loc, "position of the first chara
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 42e03bb9cc9bd815476b0a3f06ac5f58826e3708 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Fri, 31 Jan 2025 19:29:05 +0300 Subject: [PATCH] [clang-tidy] add new check bugprone-reset-ambiguous-call --- .../bugprone/BugproneTidyModule.cpp | 3 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../SmartptrResetAmbiguousCallCheck.cpp | 146 +++ .../SmartptrResetAmbiguousCallCheck.h | 37 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../smartptr-reset-ambiguous-call.rst | 47 .../docs/clang-tidy/checks/list.rst | 1 + ...r-reset-ambiguous-call-custom-pointers.cpp | 72 ++ .../smartptr-reset-ambiguous-call.cpp | 239 ++ 9 files changed, 552 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/smartptr-reset-ambiguous-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/smartptr-reset-ambiguous-call-custom-pointers.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/smartptr-reset-ambiguous-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index c5f0b5b28418f8..a01d0e384ab737 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -64,6 +64,7 @@ #include "SignedCharMisuseCheck.h" #include "SizeofContainerCheck.h" #include "SizeofExpressionCheck.h" +#include "SmartptrResetAmbiguousCallCheck.h" #include "SpuriouslyWakeUpFunctionsCheck.h" #include "StandaloneEmptyCheck.h" #include "StringConstructorCheck.h" @@ -207,6 +208,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-sizeof-container"); CheckFactories.registerCheck( "bugprone-sizeof-expression"); +CheckFactories.registerCheck( +"bugprone-smartptr-reset-ambiguous-call"); CheckFactories.registerCheck( "bugprone-spuriously-wake-up-functions"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index e8309c68b7fcaa..7fd6f4994f5375 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -65,6 +65,7 @@ add_clang_library(clangTidyBugproneModule STATIC SizeofContainerCheck.cpp SizeofExpressionCheck.cpp SmartPtrArrayMismatchCheck.cpp + SmartptrResetAmbiguousCallCheck.cpp SpuriouslyWakeUpFunctionsCheck.cpp StandaloneEmptyCheck.cpp StringConstructorCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp new file mode 100644 index 00..326a5665179d79 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp @@ -0,0 +1,146 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultParameters) { + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + + return true; +} + +const auto DefaultSmartPointers = "::std::shared_ptr;::std::unique_ptr"; +} // namespace + +SmartptrResetAmbiguousCallCheck::SmartptrResetAmbiguousCallCheck( +StringRef Name, ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + SmartPointers(utils::options::parseStringList( + Options.get("SmartPointers", DefaultSmartPointers))) {} + +void SmartptrResetAmbiguousCallCheck::storeOptions( +ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "SmartPointers", +utils::options::seria
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 42e03bb9cc9bd815476b0a3f06ac5f58826e3708 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Fri, 31 Jan 2025 19:29:05 +0300 Subject: [PATCH 1/2] [clang-tidy] add new check bugprone-reset-ambiguous-call --- .../bugprone/BugproneTidyModule.cpp | 3 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../SmartptrResetAmbiguousCallCheck.cpp | 146 +++ .../SmartptrResetAmbiguousCallCheck.h | 37 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../smartptr-reset-ambiguous-call.rst | 47 .../docs/clang-tidy/checks/list.rst | 1 + ...r-reset-ambiguous-call-custom-pointers.cpp | 72 ++ .../smartptr-reset-ambiguous-call.cpp | 239 ++ 9 files changed, 552 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/smartptr-reset-ambiguous-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/smartptr-reset-ambiguous-call-custom-pointers.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/smartptr-reset-ambiguous-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index c5f0b5b28418f8..a01d0e384ab737 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -64,6 +64,7 @@ #include "SignedCharMisuseCheck.h" #include "SizeofContainerCheck.h" #include "SizeofExpressionCheck.h" +#include "SmartptrResetAmbiguousCallCheck.h" #include "SpuriouslyWakeUpFunctionsCheck.h" #include "StandaloneEmptyCheck.h" #include "StringConstructorCheck.h" @@ -207,6 +208,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-sizeof-container"); CheckFactories.registerCheck( "bugprone-sizeof-expression"); +CheckFactories.registerCheck( +"bugprone-smartptr-reset-ambiguous-call"); CheckFactories.registerCheck( "bugprone-spuriously-wake-up-functions"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index e8309c68b7fcaa..7fd6f4994f5375 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -65,6 +65,7 @@ add_clang_library(clangTidyBugproneModule STATIC SizeofContainerCheck.cpp SizeofExpressionCheck.cpp SmartPtrArrayMismatchCheck.cpp + SmartptrResetAmbiguousCallCheck.cpp SpuriouslyWakeUpFunctionsCheck.cpp StandaloneEmptyCheck.cpp StringConstructorCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp new file mode 100644 index 00..326a5665179d79 --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/SmartptrResetAmbiguousCallCheck.cpp @@ -0,0 +1,146 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultParameters) { + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + + return true; +} + +const auto DefaultSmartPointers = "::std::shared_ptr;::std::unique_ptr"; +} // namespace + +SmartptrResetAmbiguousCallCheck::SmartptrResetAmbiguousCallCheck( +StringRef Name, ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + SmartPointers(utils::options::parseStringList( + Options.get("SmartPointers", DefaultSmartPointers))) {} + +void SmartptrResetAmbiguousCallCheck::storeOptions( +ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "SmartPointers", +utils::options::s
[clang-tools-extra] [clang-tidy] add new modernize-use-scoped-lock check (PR #126434)
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/126434 Add new clang-tidy that finds uses of `std::lock_guard` and suggests replacing them with C++17's more flexible and safer alternative `std::scoped_lock`. Here is a small description of how it works for better understanding of the code: Two separate AST matchers are registered: - The first one matches declarations of `std::lock_guard` that are single in their scope (only one `std::lock_guard` in `CompoundStmt`). It's an easy case, we can emit warning right away. - The second one matches `CompoundStmt`'s that have multiple `std::lock_guard` declarations, which means that we may have consecutive declarations of `std::lock_guard` that can be replaced by a single `std::scoped_lock`. In order to ensure that declarations are consecutive, we need to loop over `Stmt`'s in `CompoundStmt`. Here is a small example: ```cpp { std::mutex m1, m2; std::lock(m1, m2); std::lock_guard l1(m, std::adopt_lock); // first declaration of 'std::lock_guard' std::lock_guard l2(m, std::adopt_lock); // second declaration of 'std::lock_guard' that can be merged with first using 'scoped_lock' } ``` If there is an easier way to find consecutive declarations of `std::lock_guard` in AST matchers, I'm happy to learn and adjust my code. This PR closes https://github.com/llvm/llvm-project/issues/107839. >From 92588a7eb3f87e74887e94f88d3402ec25c6ee53 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 9 Feb 2025 22:34:26 +0300 Subject: [PATCH] [clang-tidy] add modernize-use-scoped-lock check --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseScopedLockCheck.cpp | 234 ++ .../clang-tidy/modernize/UseScopedLockCheck.h | 50 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 1 + .../checks/modernize/use-scoped-lock.rst | 81 + .../use-scoped-lock-only-warn-on-multiple.cpp | 122 .../checkers/modernize/use-scoped-lock.cpp| 290 ++ 9 files changed, 788 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-scoped-lock.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-only-warn-on-multiple.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index bab1167fb15ff20..619a27b2f9bb6b2 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -42,6 +42,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseNullptrCheck.cpp UseOverrideCheck.cpp UseRangesCheck.cpp + UseScopedLockCheck.cpp UseStartsEndsWithCheck.cpp UseStdFormatCheck.cpp UseStdNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index fc46c72982fdce8..b2d4ddd6675022a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -43,6 +43,7 @@ #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" #include "UseRangesCheck.h" +#include "UseScopedLockCheck.h" #include "UseStartsEndsWithCheck.h" #include "UseStdFormatCheck.h" #include "UseStdNumbersCheck.h" @@ -80,6 +81,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck( "modernize-use-integer-sign-comparison"); CheckFactories.registerCheck("modernize-use-ranges"); +CheckFactories.registerCheck( +"modernize-use-scoped-lock"); CheckFactories.registerCheck( "modernize-use-starts-ends-with"); CheckFactories.registerCheck("modernize-use-std-format"); diff --git a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp new file mode 100644 index 000..af2fea5ad310e6d --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp @@ -0,0 +1,234 @@ +//===--- UseScopedLockCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseScopedLockCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/
[clang-tools-extra] [clang-tidy] add new check: modernize-use-scoped-lock (PR #126434)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/126434 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add new check: modernize-use-scoped-lock (PR #126434)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/126434 >From 92588a7eb3f87e74887e94f88d3402ec25c6ee53 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 9 Feb 2025 22:34:26 +0300 Subject: [PATCH 1/2] [clang-tidy] add modernize-use-scoped-lock check --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseScopedLockCheck.cpp | 234 ++ .../clang-tidy/modernize/UseScopedLockCheck.h | 50 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 1 + .../checks/modernize/use-scoped-lock.rst | 81 + .../use-scoped-lock-only-warn-on-multiple.cpp | 122 .../checkers/modernize/use-scoped-lock.cpp| 290 ++ 9 files changed, 788 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-scoped-lock.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-only-warn-on-multiple.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index bab1167fb15ff20..619a27b2f9bb6b2 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -42,6 +42,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseNullptrCheck.cpp UseOverrideCheck.cpp UseRangesCheck.cpp + UseScopedLockCheck.cpp UseStartsEndsWithCheck.cpp UseStdFormatCheck.cpp UseStdNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index fc46c72982fdce8..b2d4ddd6675022a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -43,6 +43,7 @@ #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" #include "UseRangesCheck.h" +#include "UseScopedLockCheck.h" #include "UseStartsEndsWithCheck.h" #include "UseStdFormatCheck.h" #include "UseStdNumbersCheck.h" @@ -80,6 +81,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck( "modernize-use-integer-sign-comparison"); CheckFactories.registerCheck("modernize-use-ranges"); +CheckFactories.registerCheck( +"modernize-use-scoped-lock"); CheckFactories.registerCheck( "modernize-use-starts-ends-with"); CheckFactories.registerCheck("modernize-use-std-format"); diff --git a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp new file mode 100644 index 000..af2fea5ad310e6d --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp @@ -0,0 +1,234 @@ +//===--- UseScopedLockCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseScopedLockCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/Twine.h" +#include +#include + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +namespace { + +bool isLockGuard(const QualType &Type) { + if (const auto *RecordTy = Type->getAs()) { +if (const auto *RecordDecl = RecordTy->getDecl()) { + return RecordDecl->getQualifiedNameAsString() == "std::lock_guard"; +} + } + + if (const auto *TemplateSpecType = + Type->getAs()) { +if (const auto *TemplateDecl = +TemplateSpecType->getTemplateName().getAsTemplateDecl()) { + return TemplateDecl->getQualifiedNameAsString() == "std::lock_guard"; +} + } + + return false; +} + +std::vector getLockGuardsFromDecl(const DeclStmt *DS) { + std::vector LockGuards; + + for (const auto *Decl : DS->decls()) { +if (const auto *VD = dyn_cast(Decl)) { + const QualType Type = VD->getType().getCanonicalType(); + if (isLockGuard(Type)) { +LockGuards.push_back(VD); + } +} + } + + return LockGuards; +} + +// Scans through the statements in a block and groups consecutive +// 'std::lock_guard' variable declarations toget
[clang-tools-extra] [clang-tidy] add new check: modernize-use-scoped-lock (PR #126434)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/126434 >From 92588a7eb3f87e74887e94f88d3402ec25c6ee53 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 9 Feb 2025 22:34:26 +0300 Subject: [PATCH 1/3] [clang-tidy] add modernize-use-scoped-lock check --- .../clang-tidy/modernize/CMakeLists.txt | 1 + .../modernize/ModernizeTidyModule.cpp | 3 + .../modernize/UseScopedLockCheck.cpp | 234 ++ .../clang-tidy/modernize/UseScopedLockCheck.h | 50 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../docs/clang-tidy/checks/list.rst | 1 + .../checks/modernize/use-scoped-lock.rst | 81 + .../use-scoped-lock-only-warn-on-multiple.cpp | 122 .../checkers/modernize/use-scoped-lock.cpp| 290 ++ 9 files changed, 788 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/modernize/use-scoped-lock.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock-only-warn-on-multiple.cpp create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize/use-scoped-lock.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt index bab1167fb15ff20..619a27b2f9bb6b2 100644 --- a/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/modernize/CMakeLists.txt @@ -42,6 +42,7 @@ add_clang_library(clangTidyModernizeModule STATIC UseNullptrCheck.cpp UseOverrideCheck.cpp UseRangesCheck.cpp + UseScopedLockCheck.cpp UseStartsEndsWithCheck.cpp UseStdFormatCheck.cpp UseStdNumbersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp index fc46c72982fdce8..b2d4ddd6675022a 100644 --- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp @@ -43,6 +43,7 @@ #include "UseNullptrCheck.h" #include "UseOverrideCheck.h" #include "UseRangesCheck.h" +#include "UseScopedLockCheck.h" #include "UseStartsEndsWithCheck.h" #include "UseStdFormatCheck.h" #include "UseStdNumbersCheck.h" @@ -80,6 +81,8 @@ class ModernizeModule : public ClangTidyModule { CheckFactories.registerCheck( "modernize-use-integer-sign-comparison"); CheckFactories.registerCheck("modernize-use-ranges"); +CheckFactories.registerCheck( +"modernize-use-scoped-lock"); CheckFactories.registerCheck( "modernize-use-starts-ends-with"); CheckFactories.registerCheck("modernize-use-std-format"); diff --git a/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp new file mode 100644 index 000..af2fea5ad310e6d --- /dev/null +++ b/clang-tools-extra/clang-tidy/modernize/UseScopedLockCheck.cpp @@ -0,0 +1,234 @@ +//===--- UseScopedLockCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseScopedLockCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/AST/Stmt.h" +#include "clang/AST/Type.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Lex/Lexer.h" +#include "llvm/ADT/Twine.h" +#include +#include + +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { + +namespace { + +bool isLockGuard(const QualType &Type) { + if (const auto *RecordTy = Type->getAs()) { +if (const auto *RecordDecl = RecordTy->getDecl()) { + return RecordDecl->getQualifiedNameAsString() == "std::lock_guard"; +} + } + + if (const auto *TemplateSpecType = + Type->getAs()) { +if (const auto *TemplateDecl = +TemplateSpecType->getTemplateName().getAsTemplateDecl()) { + return TemplateDecl->getQualifiedNameAsString() == "std::lock_guard"; +} + } + + return false; +} + +std::vector getLockGuardsFromDecl(const DeclStmt *DS) { + std::vector LockGuards; + + for (const auto *Decl : DS->decls()) { +if (const auto *VD = dyn_cast(Decl)) { + const QualType Type = VD->getType().getCanonicalType(); + if (isLockGuard(Type)) { +LockGuards.push_back(VD); + } +} + } + + return LockGuards; +} + +// Scans through the statements in a block and groups consecutive +// 'std::lock_guard' variable declarations toget
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
vbvictor wrote: Ping https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
vbvictor wrote: Ping https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
vbvictor wrote: There are some general improvements can be made: - Create a separate test file `use-cpp-style-comments-doxygen.cpp` and leave `use-cpp-style-comments.cpp` as it was before. With this, we can make sure that check runs good both with and without option. - Add documentation about option `ExcludeDoxygenStyleComments` in clang-tools-extra/docs/clang-tidy/checks/readability/use-cpp-style-comments.rst. You can use others docs with described options as reference. https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
vbvictor wrote: Ping, @HerrCai0907, Could you please address https://github.com/llvm/llvm-project/pull/123413#issuecomment-2622200842. Thank you in advance. https://github.com/llvm/llvm-project/pull/123413 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
vbvictor wrote: Ping https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 618f4a1707c1b62693c0e878040997154e7e35d6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/9] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3bddeeda06e06b..f57e951afc1ce0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -101,6 +101,11 @@ Changes in existing checks ` check to allow specifying additional C++ member functions to match. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst b/clang-tools-extra/docs/clang-
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +199,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes + + A semicolon-separated list of names of types that will be excluded from + const-correctness checking. Regular expressions are accepted, e.g. + `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` + and `reference`. If a name in the list contains the sequence `::`, it is + matched against the qualified type name (i.e. ``namespace::Type``), + otherwise it is matched against only the type name (i.e. ``Type``). + Default is empty. vbvictor wrote: Done. https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 618f4a1707c1b62693c0e878040997154e7e35d6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/8] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 3bddeeda06e06b..f57e951afc1ce0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -101,6 +101,11 @@ Changes in existing checks ` check to allow specifying additional C++ member functions to match. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + Removed checks ^^ diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/const-correctness.rst b/clang-tools-extra/docs/clang-
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/123413 >From f5f0ba142a2eb71ce781faf4e15fcd225bec9ca8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH 1/2] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 +-- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a..d1902b658061b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit = Result.Nodes.getNodeAs("int"); -if (Lit->getValue().ugt(Str->getLength())) { +const auto *Length = Result.Nodes.getNodeAs("length"); +if (Length->getValue().ugt(Str->getLength())) { diag(Loc, "length is bigger than string literal size"); + return; +} +if (const auto *Pos = Result.Nodes.getNodeAs("pos")) { + if (Pos->getValue().uge(Str->getLength())) { +diag(Loc, "position of the first charact
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
vbvictor wrote: > please update documents in > clang-tools-extra/docs/clang-tidy/checks/bugprone/string-constructor.rst also Added new error description for "invalid character position argument" and provided more examples to existing cases. https://github.com/llvm/llvm-project/pull/123413 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -0,0 +1,127 @@ +//===--- UseCppStyleCommentsCheck.cpp - clang-tidy-===// + +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseCppStyleCommentsCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" +#include "clang/Lex/Preprocessor.h" +#include +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { +class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler { +public: + CStyleCommentHandler(UseCppStyleCommentsCheck &Check) + : Check(Check), +CStyleCommentMatch( +"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$") { + } + + std::string convertToCppStyleComment(const SourceManager &SM, + const SourceRange &Range) { +StringRef CommentText = Lexer::getSourceText( +CharSourceRange::getTokenRange(Range), SM, LangOptions()); + +std::string InnerText = CommentText.str(); +InnerText.erase(0, 2); +InnerText.erase(InnerText.size() - 2, 2); + +std::string Result; +std::istringstream Stream(InnerText); +std::string Line; + +if (std::getline(Stream, Line)) { + size_t startPos = Line.find_first_not_of(" \t"); + if (startPos != std::string::npos) { +Line = Line.substr(startPos); + } else { +Line.clear(); + } + Result += "// " + Line; +} + +while (std::getline(Stream, Line)) { + size_t startPos = Line.find_first_not_of(" \t"); vbvictor wrote: ```suggestion size_t StartPos = Line.find_first_not_of(" \t"); ``` https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
vbvictor wrote: Ping @PiotrZSL @5chmidti https://github.com/llvm/llvm-project/pull/123413 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
@@ -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`. vbvictor wrote: Please, wrap line to 80 characters. https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
@@ -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`. vbvictor wrote: Please, wrap line to 80 characters https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -0,0 +1,40 @@ +//===--- UseCppStyleCommentsCheck.h - clang-tidy---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H + +#include "../ClangTidyCheck.h" +#include vbvictor wrote: Please, remove unused include. https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -0,0 +1,127 @@ +//===--- UseCppStyleCommentsCheck.cpp - clang-tidy-===// + +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseCppStyleCommentsCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" +#include "clang/Lex/Preprocessor.h" +#include vbvictor wrote: Please, remove unused include. https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -0,0 +1,127 @@ +//===--- UseCppStyleCommentsCheck.cpp - clang-tidy-===// + +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "UseCppStyleCommentsCheck.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" +#include "clang/Lex/Preprocessor.h" +#include +using namespace clang::ast_matchers; + +namespace clang::tidy::modernize { +class UseCppStyleCommentsCheck::CStyleCommentHandler : public CommentHandler { +public: + CStyleCommentHandler(UseCppStyleCommentsCheck &Check) + : Check(Check), +CStyleCommentMatch( +"^[ \t]*/\\*+[ \t\r\n]*(.*[ \t\r\n]*)*[ \t\r\n]*\\*+/[ \t\r\n]*$") { + } + + std::string convertToCppStyleComment(const SourceManager &SM, + const SourceRange &Range) { +StringRef CommentText = Lexer::getSourceText( +CharSourceRange::getTokenRange(Range), SM, LangOptions()); + +std::string InnerText = CommentText.str(); +InnerText.erase(0, 2); +InnerText.erase(InnerText.size() - 2, 2); + +std::string Result; +std::istringstream Stream(InnerText); +std::string Line; + +if (std::getline(Stream, Line)) { + size_t startPos = Line.find_first_not_of(" \t"); vbvictor wrote: Please, use CamelCase ```suggestion size_t StartPos = Line.find_first_not_of(" \t"); ``` https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
vbvictor wrote: Thank you for working on this check! Please, read comments on previous pull request https://github.com/llvm/llvm-project/pull/99713. I think there could be some improvements done based on comments in that PR. For moving this check to `readability` section script `clang-tools-extra/clang-tidy/rename_check.py` can be useful. https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
@@ -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`. vbvictor wrote: Nit: I am not a native speaker but I suggest there could be an easier to understand wording: ```suggestion If a namespace comment is present but does not match namespace definition, the check will fail. Default is `false`. ``` https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/7] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,150 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + vbvictor wrote: Yes, removed useless condition. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,150 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "../utils/OptionsUtils.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultParameters) { + if (Node.param_empty()) vbvictor wrote: Yes, removed useless condition. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
https://github.com/vbvictor deleted https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
vbvictor wrote: Please, update Release Notes in clang-tools-extra/docs/ReleaseNotes.rst https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
vbvictor wrote: > What do you think about the option's name? Is `AllowNoNamespaceComments` > fine for you or shall I rename to `AllowOmittingNamespaceComments` (or any > other suggestion)? As for now I can not think of a better name, maybe others will suggest. https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] added option `google-readability-namespace-comments.AllowNoNamespaceComments` (PR #124265)
@@ -39,3 +39,11 @@ 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 namespace comments are ommitted + entirely. The check only fails if a namespace comment is present but does + not match. Default is `false`. vbvictor wrote: Nit: I think it will make easier to understand what it tries to match. The same should be corrected in clang-tools-extra/docs/ReleaseNotes.rst if you wish to apply this fix. ```suggestion not match namespace definition. Default is `false`. ``` https://github.com/llvm/llvm-project/pull/124265 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/5] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/6] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,47 @@ +.. title:: clang-tidy - bugprone-smartptr-reset-ambiguous-call + +bugprone-smartptr-reset-ambiguous-call +== + +Finds potentially erroneous calls to ``reset`` method on +smart pointers when the pointee type also has a ``reset`` method. +Having a ``reset`` method in both classes makes it easy to accidentally +make the pointer null when intending to reset the underlying object. + +.. code-block:: c++ + + struct Resettable { +void reset() { /* Own reset logic */ } + }; + + auto ptr = std::make_unique(); + + ptr->reset(); // Calls underlying reset method + ptr.reset(); // Makes the pointer null + +Both calls are valid C++ code, but the second one might not be +what the developer intended, as it destroys the pointed-to object +rather than resetting its state. +It's easy to make such a typo because the difference +between ``.`` and ``->`` is really small. + +The recommended approach is to make the intent explicit by +using either member access or direct assignment: + +.. code-block:: c++ + + std::unique_ptr ptr = std::make_unique(); + + (*ptr).reset(); // Clearly calls underlying reset method + ptr = nullptr; // Clearly makes the pointer null + +The default smart pointers that are considered are ``std::unique_ptr``, +``std::shared_ptr``. To specify other smart pointers or +other classes use the :option:`SmartPointers` option. + +Options +--- + +.. option:: SmartPointers + +Semicolon-separated list of class names of custom smart pointers. vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
vbvictor wrote: Thank you everyone for the feedback! @PiotrZSL I tried using TK_IgnoreUnlessSpelledInSource with `cxxDependentScopeMemberExpr` ast-matcher but failed to write a working matcher case with nested template parameters: ```cpp template void TemplatePositiveTest() { std::unique_ptr u_ptr; u_ptr.reset(); u_ptr->reset(); } void instantiate() { TemplatePositiveTest>(); } ``` As for now, I'm thinking about leaving it as is and make an NFC change in the future when I get more familiar with matchers. Apart from TK_IgnoreUnlessSpelledInSource I fixed your comments. @5chmidti I fixed all your comments and added better _warning_ and _note_ messages: ```cpp s.reset(); // warning: be explicit when calling 'reset()' on a smart pointer with a pointee that has a 'reset()' method // note: assign the pointer to 'nullptr' s->reset(); // warning: be explicit when calling 'reset()' on a pointee of a smart pointer // note: use dereference to call 'reset' method of the pointee ``` @EugeneZelenko Fixed all pr comments. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/123413 This PR add diagnostics for 3-parameter `std::basic_string(const char* t, size_type pos, size_type count)` constructor in bugprone-string-constructor check: ```cpp std::string r1("test", 1, 0); // constructor creating an empty string std::string r2("test", 0, -4); // negative value used as length parameter // more examples in test file ``` Fixes false-positives reported in https://github.com/llvm/llvm-project/issues/123198. >From f5f0ba142a2eb71ce781faf4e15fcd225bec9ca8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 +-- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a..d1902b658061b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit =
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
vbvictor wrote: Ping https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
vbvictor wrote: @EugeneZelenko Ping I would also need assistance in merging this PR since I don't have write permissions. https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 841cfec5a0ab4ce5ce64e71facfb7becaf4340e8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/5] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index fa3a8e577a33ad..16be70ca944e95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,11 @@ Changes in existing checks ` check to report a location even when the member location is not valid. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + - Improved :doc:`misc-definitions-in-headers ` check by rewording the diagnostic note that suggests adding ``inline``. diff --git a/clang
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/8] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,48 @@ +.. title:: clang-tidy - bugprone-smartptr-reset-ambiguous-call + +bugprone-smartptr-reset-ambiguous-call +== + +Finds potentially erroneous calls to ``reset`` method on vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 841cfec5a0ab4ce5ce64e71facfb7becaf4340e8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/6] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index fa3a8e577a33ad..16be70ca944e95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,11 @@ Changes in existing checks ` check to report a location even when the member location is not valid. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + - Improved :doc:`misc-definitions-in-headers ` check by rewording the diagnostic note that suggests adding ``inline``. diff --git a/clang
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,12 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') vbvictor wrote: Placed default in last sentence. https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
vbvictor wrote: > Please also fix preceding option defaults. Fixed preceding option defaults https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
vbvictor wrote: > Can anyone please help me? Why is Test Documentation build is failing? I am > in no clue. Restore clang-tools-extra/clang-tidy/modernize/CMakeLists.txt and clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp as they were before (with no changes to them). https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -0,0 +1,40 @@ +//===--- UseCppStyleCommentsCheck.h - clang-tidy---===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_CPP_STYLE_COMMENTS_CHECK_H + +#include "../ClangTidyCheck.h" +#include vbvictor wrote: Thank you for pointing out, I didn't see usage in header file https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,12 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') vbvictor wrote: In this file all the options has default in its names. I assumed that I should stick to the style of the file. Maybe It's better to leave this as is and create a new PR with fixes for all options, what do you think? https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 841cfec5a0ab4ce5ce64e71facfb7becaf4340e8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/7] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index fa3a8e577a33ad..16be70ca944e95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,11 @@ Changes in existing checks ` check to report a location even when the member location is not valid. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + - Improved :doc:`misc-definitions-in-headers ` check by rewording the diagnostic note that suggests adding ``inline``. diff --git a/clang
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
vbvictor wrote: As @PiotrZSL mentioned in the issue, adding an option called `ExcludeDoxygenStyleComments` would be very helpful (or even necessary) before releasing this check. Otherwise, it could create a lot of trouble for projects that use doxygen for their documentation (including LLVM itself). Doxygen comments typically look like this: ```cpp /** * ... text ... */ void foo() ``` With this option enabled, I think we should exclude the following cases: ```cpp /** * ... text ... */ /*! * ... text ... */ /*** * ... text ***/ ``` Basically, they are comments that start with `/*` followed by more than one `*` or `/*!` More detailed documentation can be found in https://www.doxygen.nl/manual/docblocks.html. This option can also be tested on LLVM codebase for false positives. https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
@@ -31,6 +31,6 @@ Options A semicolon-separated list of names of types allowed to be copied in each iteration. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default vbvictor wrote: @nicovank, yes, I can make this change. As far as I understand only _Ref_, _ref_, _Reference_ and _reference_ should be changed but not _[Rr]ef(erence)?$_. It got a little tricky for me since _Ref_ only refers to a suffix of potential c++ type, so it doesn't fall in double back-ticks category. https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
@@ -146,6 +147,8 @@ class ReadabilityModule : public ClangTidyModule { "readability-static-definition-in-anonymous-namespace"); CheckFactories.registerCheck( "readability-string-compare"); +CheckFactories.registerCheck( vbvictor wrote: Please sort alphabetically https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Replace /* ... */ single-line comments with // ... comments (PR #124319)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/124319 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122957 >From 9c49e1e558c43d13872afe0a10345e3510afebdf Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:30:03 +0300 Subject: [PATCH 1/3] [clang-tidy] Fix: typos in 'AllowedTypes' option in various checks --- .../docs/clang-tidy/checks/performance/for-range-copy.rst | 2 +- .../checks/performance/unnecessary-copy-initialization.rst | 2 +- .../clang-tidy/checks/performance/unnecessary-value-param.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst index 01fde9580e2a08..9460802bf4eab4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst @@ -32,5 +32,5 @@ Options iteration. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched - against the qualified typename (i.e. `namespace::Type`, otherwise it is + against the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst index 837283811ddcce..4ad9dac3e38f66 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst @@ -45,7 +45,7 @@ Options copying. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched - against the qualified typename (i.e. `namespace::Type`, otherwise it is + against the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). .. option:: ExcludedContainerTypes diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst index cc5e1ae73508c5..330ef960e9a3bb 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst @@ -68,5 +68,5 @@ Options Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched against - the qualified typename (i.e. `namespace::Type`, otherwise it is matched + the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). >From be68e91708bd122e74b03d559796f22a94628155 Mon Sep 17 00:00:00 2001 From: Baranov Victor Date: Wed, 15 Jan 2025 20:31:44 +0300 Subject: [PATCH 2/3] [clang-tidy] more fixes to docs --- .../docs/clang-tidy/checks/performance/for-range-copy.rst | 6 +++--- .../checks/performance/unnecessary-copy-initialization.rst | 6 +++--- .../checks/performance/unnecessary-value-param.rst | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst index 9460802bf4eab4..cd1b8daacee107 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst @@ -31,6 +31,6 @@ Options A semicolon-separated list of names of types allowed to be copied in each iteration. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default - is empty. If a name in the list contains the sequence `::` it is matched - against the qualified typename (i.e. `namespace::Type`), otherwise it is - matched against only the type name (i.e. `Type`). + is empty. If a name in the list contains the sequence `::`, it is matched + against the qualified type name (i.e. ``namespace::Type``), otherwise it is + matched against only the type name (i.e. ``Type``). diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst index 4ad9dac3e38f66..45c91cd56218df 100644 --- a/clang-tools-extra/docs
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
@@ -31,6 +31,6 @@ Options A semicolon-separated list of names of types allowed to be copied in each iteration. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default vbvictor wrote: Corrected to double-ticks plus found one more file with same errors: `clang-tools-extra/docs/clang-tidy/checks/bugprone/assert-side-effect.rst` https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/123413 >From f5f0ba142a2eb71ce781faf4e15fcd225bec9ca8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH 1/3] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 +-- clang-tools-extra/docs/ReleaseNotes.rst | 5 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 80 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a..d1902b658061b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit = Result.Nodes.getNodeAs("int"); -if (Lit->getValue().ugt(Str->getLength())) { +const auto *Length = Result.Nodes.getNodeAs("length"); +if (Length->getValue().ugt(Str->getLength())) { diag(Loc, "length is bigger than string literal size"); + return; +} +if (const auto *Pos = Result.Nodes.getNodeAs("pos")) { + if (Pos->getValue().uge(Str->getLength())) { +diag(Loc, "position of the first charact
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/123413 >From bc85c10a7d316630843266779cb1465b02d3dbf6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH 1/4] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 - clang-tools-extra/docs/ReleaseNotes.rst | 182 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 257 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a..d1902b658061b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit = Result.Nodes.getNodeAs("int"); -if (Lit->getValue().ugt(Str->getLength())) { +const auto *Length = Result.Nodes.getNodeAs("length"); +if (Length->getValue().ugt(Str->getLength())) { diag(Loc, "length is bigger than string literal size"); + return; +} +if (const auto *Pos = Result.Nodes.getNodeAs("pos")) { + if (Pos->getValue().uge(Str->getLength())) { +diag(Loc, "position of the first character
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
vbvictor wrote: @HerrCai0907, sorry to bother you, but can you help merge this pr if it is okay to have only one review (from you). I can keep pinging others but don't understand how many people need to watch it. Some other pull requests were merged with some reviewers still being in "awaiting" status. https://github.com/llvm/llvm-project/pull/123413 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Added support for 3-argument std::string ctor in bugprone-string-constructor check (PR #123413)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/123413 >From bc85c10a7d316630843266779cb1465b02d3dbf6 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sat, 18 Jan 2025 00:49:29 +0300 Subject: [PATCH 1/5] [clang-tidy] Added support for 3-argument string ctor --- .../bugprone/StringConstructorCheck.cpp | 50 - clang-tools-extra/docs/ReleaseNotes.rst | 182 ++ .../checkers/bugprone/string-constructor.cpp | 30 +++ 3 files changed, 257 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp index 8ae4351ac2830a..d1902b658061b1 100644 --- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp @@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( cxxConstructExpr( hasDeclaration(cxxMethodDecl(hasName("basic_string"))), - hasArgument(0, hasType(qualType(isInteger(, + argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger(, hasArgument(1, hasType(qualType(isInteger(, anyOf( // Detect the expression: string('x', 40); @@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { cxxConstructExpr( hasDeclaration(cxxConstructorDecl(ofClass( cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), - hasArgument(0, hasType(CharPtrType)), + argumentCountIs(2), hasArgument(0, hasType(CharPtrType)), hasArgument(1, hasType(isInteger())), anyOf( // Detect the expression: string("...", 0); @@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) { // Detect the expression: string("lit", 5) allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), hasArgument(1, ignoringParenImpCasts( - integerLiteral().bind("int")) + integerLiteral().bind("length")) + .bind("constructor"), + this); + + // Check the literal string constructor with char pointer, start position and + // length parameters. [i.e. string (const char* s, size_t pos, size_t count);] + Finder->addMatcher( + cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(ofClass( + cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)), + argumentCountIs(3), hasArgument(0, hasType(CharPtrType)), + hasArgument(1, hasType(qualType(isInteger(, + hasArgument(2, hasType(qualType(isInteger(, + anyOf( + // Detect the expression: string("...", 1, 0); + hasArgument(2, ZeroExpr.bind("empty-string")), + // Detect the expression: string("...", -4, 1); + hasArgument(1, NegativeExpr.bind("negative-pos")), + // Detect the expression: string("...", 0, -4); + hasArgument(2, NegativeExpr.bind("negative-length")), + // Detect the expression: string("lit", 0, 0x1234567); + hasArgument(2, LargeLengthExpr.bind("large-length")), + // Detect the expression: string("lit", 1, 5) + allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")), +hasArgument( +1, ignoringParenImpCasts(integerLiteral().bind("pos"))), +hasArgument(2, ignoringParenImpCasts( + integerLiteral().bind("length")) .bind("constructor"), this); @@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) { diag(Loc, "constructor creating an empty string"); } else if (Result.Nodes.getNodeAs("negative-length")) { diag(Loc, "negative value used as length parameter"); + } else if (Result.Nodes.getNodeAs("negative-pos")) { +diag(Loc, "negative value used as position of the " + "first character parameter"); } else if (Result.Nodes.getNodeAs("large-length")) { if (WarnOnLargeLength) diag(Loc, "suspicious large length parameter"); } else if (Result.Nodes.getNodeAs("literal-with-length")) { const auto *Str = Result.Nodes.getNodeAs("str"); -const auto *Lit = Result.Nodes.getNodeAs("int"); -if (Lit->getValue().ugt(Str->getLength())) { +const auto *Length = Result.Nodes.getNodeAs("length"); +if (Length->getValue().ugt(Str->getLength())) { diag(Loc, "length is bigger than string literal size"); + return; +} +if (const auto *Pos = Result.Nodes.getNodeAs("pos")) { + if (Pos->getValue().uge(Str->getLength())) { +diag(Loc, "position of the first character
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/122957 Added right parenthesis to match left one. >From 9c49e1e558c43d13872afe0a10345e3510afebdf Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:30:03 +0300 Subject: [PATCH] [clang-tidy] Fix: typos in 'AllowedTypes' option in various checks --- .../docs/clang-tidy/checks/performance/for-range-copy.rst | 2 +- .../checks/performance/unnecessary-copy-initialization.rst | 2 +- .../clang-tidy/checks/performance/unnecessary-value-param.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst index 01fde9580e2a08..9460802bf4eab4 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/for-range-copy.rst @@ -32,5 +32,5 @@ Options iteration. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched - against the qualified typename (i.e. `namespace::Type`, otherwise it is + against the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst index 837283811ddcce..4ad9dac3e38f66 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-copy-initialization.rst @@ -45,7 +45,7 @@ Options copying. Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched - against the qualified typename (i.e. `namespace::Type`, otherwise it is + against the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). .. option:: ExcludedContainerTypes diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst index cc5e1ae73508c5..330ef960e9a3bb 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/performance/unnecessary-value-param.rst @@ -68,5 +68,5 @@ Options Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type with suffix `Ref`, `ref`, `Reference` and `reference`. The default is empty. If a name in the list contains the sequence `::` it is matched against - the qualified typename (i.e. `namespace::Type`, otherwise it is matched + the qualified typename (i.e. `namespace::Type`), otherwise it is matched against only the type name (i.e. `Type`). ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
vbvictor wrote: @HerrCai0907, Could you look at this PR, please. I have seen you doing some refactor/cleanups lately. https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Refactor: removed typos in 'AllowedTypes' option in various checks (PR #122957)
https://github.com/vbvictor edited https://github.com/llvm/llvm-project/pull/122957 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') + + A semicolon-separated list of names of types that + will be excluded from const-correctness checking. + Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type + with suffix `Ref`, `ref`, `Reference` and `reference`. + If a name in the list contains the sequence `::` it is matched against + the qualified typename (i.e. `namespace::Type`), otherwise it is matched vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') + + A semicolon-separated list of names of types that + will be excluded from const-correctness checking. + Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type + with suffix `Ref`, `ref`, `Reference` and `reference`. + If a name in the list contains the sequence `::` it is matched against vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') + + A semicolon-separated list of names of types that + will be excluded from const-correctness checking. + Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type + with suffix `Ref`, `ref`, `Reference` and `reference`. + If a name in the list contains the sequence `::` it is matched against + the qualified typename (i.e. `namespace::Type`), otherwise it is matched + against only the type name (i.e. `Type`). vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/122951 >From 841cfec5a0ab4ce5ce64e71facfb7becaf4340e8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH 1/2] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index fa3a8e577a33ad..16be70ca944e95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,11 @@ Changes in existing checks ` check to report a location even when the member location is not valid. +- Improved :doc:`misc-const-correctness + ` check by adding + the option ``AllowedTypes``, that excludes specified types + from const-correctness checking. + - Improved :doc:`misc-definitions-in-headers ` check by rewording the diagnostic note that suggests adding ``inline``. diff --git a/clang
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') + + A semicolon-separated list of names of types that + will be excluded from const-correctness checking. + Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type + with suffix `Ref`, `ref`, `Reference` and `reference`. + If a name in the list contains the sequence `::` it is matched against + the qualified typename (i.e. `namespace::Type`), otherwise it is matched vbvictor wrote: I must note that files `for-range-copy.rst`, `unnecessary-copy-initialization.rst`, `unnecessary-value-param.rst` have the same errors in description of AllowedTypes option. Should I also make these corrections in https://github.com/llvm/llvm-project/pull/122957? https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,134 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void SmartptrResetAmbiguousCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +everyArgumentMatches(cxxDefaultArgExpr()), +on(expr(hasType(SmartptrWithBugproneReset + .bind("smartptrResetCall"), + this); + + // Find a->reset() calls + Finder->addMatcher( + cxxMemberCallExpr( + callee(memberExpr( + member(ResetMethod), + hasObjectExpression( + cxxOperatorCallExpr( + hasOverloadedOperatorName("->"), + hasArgument( + 0, expr(hasType( + classTemplateSpecializationDecl(IsSmartptr) + .bind("OpCall", + everyArgumentMatches(cxxDefaultArgExpr())) + .bind("objectResetCall"), + this); +} + +void SmartptrResetAmbiguousCallCheck::check( +const MatchFinder::MatchResult &Result) { + const auto *SmartptrResetCall = + Result.Nodes.getNodeAs("smartptrResetCall"); + const auto *ObjectResetCall = + Result.Nodes.getNodeAs("objectResetCall"); vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,34 @@ +.. title:: clang-tidy - bugprone-smartptr-reset-ambiguous-call + +bugprone-smartptr-reset-ambiguous-call +== + +Finds potentially erroneous calls to ``reset`` method on +smart pointers when the pointee type also has a ``reset`` method. +Having ``reset`` method in both classes makes it easy to accidentally vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,34 @@ +.. title:: clang-tidy - bugprone-smartptr-reset-ambiguous-call + +bugprone-smartptr-reset-ambiguous-call +== + +Finds potentially erroneous calls to ``reset`` method on +smart pointers when the pointee type also has a ``reset`` method. +Having ``reset`` method in both classes makes it easy to accidentally +make the pointer null when intending to reset the underlying object. + +.. code-block:: c++ + + struct Resettable { +void reset() { /* Own reset logic */ } + }; + + auto ptr = std::make_unique(); + + ptr->reset(); // Calls underlying reset method + ptr.reset(); // Makes the pointer null + +Both calls are valid C++ code, but the second one might not be +what the developer intended, as it destroys the pointed-to object +rather than resetting its state. +It's easy to make such typo because the difference between ``.`` and ``->`` is really small. + +The recommended approach is to make the intent explicit by using either member access or direct assignment: vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,34 @@ +//===--- SmartptrResetAmbiguousCallCheck.h - clang-tidy -*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SMARTPTRRESETAMBIGUOUSCALLCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SMARTPTRRESETAMBIGUOUSCALLCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang::tidy::bugprone { + +/// Finds potentially erroneous calls to 'reset' method on smart pointers when +/// the pointee type also has a 'reset' method +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/smartptr-reset-ambiguous-call.html +class SmartptrResetAmbiguousCallCheck : public ClangTidyCheck { +public: + SmartptrResetAmbiguousCallCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { +return LangOpts.CPlusPlus; vbvictor wrote: I added an option to specify custom smart pointers. Now `boost::shared_ptr` can be valid pointer for this check, so I did not change to `CPlusPlus11`. https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,134 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void SmartptrResetAmbiguousCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +everyArgumentMatches(cxxDefaultArgExpr()), +on(expr(hasType(SmartptrWithBugproneReset + .bind("smartptrResetCall"), + this); + + // Find a->reset() calls + Finder->addMatcher( + cxxMemberCallExpr( + callee(memberExpr( + member(ResetMethod), + hasObjectExpression( + cxxOperatorCallExpr( + hasOverloadedOperatorName("->"), + hasArgument( + 0, expr(hasType( + classTemplateSpecializationDecl(IsSmartptr) + .bind("OpCall", + everyArgumentMatches(cxxDefaultArgExpr())) + .bind("objectResetCall"), + this); +} + +void SmartptrResetAmbiguousCallCheck::check( +const MatchFinder::MatchResult &Result) { + const auto *SmartptrResetCall = + Result.Nodes.getNodeAs("smartptrResetCall"); + const auto *ObjectResetCall = + Result.Nodes.getNodeAs("objectResetCall"); + + if (SmartptrResetCall) { +const auto *Member = cast(SmartptrResetCall->getCallee()); + +auto DiagBuilder = diag(SmartptrResetCall->getBeginLoc(), +"bugprone 'reset()' call on smart pointer"); + +DiagBuilder << FixItHint::CreateReplacement( +SourceRange(Member->getOperatorLoc(), +Member->getOperatorLoc().getLocWithOffset(0)), +" ="); + +DiagBuilder << FixItHint::CreateReplacement( +SourceRange(Member->getMemberLoc(), SmartptrResetCall->getEndLoc()), +" nullptr"); + } + + if (ObjectResetCall) { +const auto *Arrow = Result.Nodes.getNodeAs("OpCall"); + +const auto SmartptrSourceRange = vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,34 @@ +.. title:: clang-tidy - bugprone-smartptr-reset-ambiguous-call + +bugprone-smartptr-reset-ambiguous-call +== + +Finds potentially erroneous calls to ``reset`` method on +smart pointers when the pointee type also has a ``reset`` method. +Having ``reset`` method in both classes makes it easy to accidentally +make the pointer null when intending to reset the underlying object. + +.. code-block:: c++ + + struct Resettable { +void reset() { /* Own reset logic */ } + }; + + auto ptr = std::make_unique(); + + ptr->reset(); // Calls underlying reset method + ptr.reset(); // Makes the pointer null + +Both calls are valid C++ code, but the second one might not be +what the developer intended, as it destroys the pointed-to object +rather than resetting its state. +It's easy to make such typo because the difference between ``.`` and ``->`` is really small. vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -144,6 +145,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck( +"bugprone-smartptr-reset-ambiguous-call"); vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/121291 >From 37dce6a7ed0cca2e9819c24f4d176c43e3c9f2ac Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 29 Dec 2024 15:32:22 +0300 Subject: [PATCH 1/4] [clang-tidy] Add bugprone-reset-call check --- .../bugprone/BugproneTidyModule.cpp | 2 + .../clang-tidy/bugprone/CMakeLists.txt| 1 + .../clang-tidy/bugprone/ResetCallCheck.cpp| 133 +++ .../clang-tidy/bugprone/ResetCallCheck.h | 34 +++ clang-tools-extra/docs/ReleaseNotes.rst | 6 + .../clang-tidy/checks/bugprone/reset-call.rst | 33 +++ .../docs/clang-tidy/checks/list.rst | 1 + .../checkers/bugprone/reset-call.cpp | 215 ++ 8 files changed, 425 insertions(+) create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp create mode 100644 clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.h create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/reset-call.rst create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/reset-call.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 33ac65e715ce81..645958e47e22a5 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -57,6 +57,7 @@ #include "PosixReturnCheck.h" #include "RedundantBranchConditionCheck.h" #include "ReservedIdentifierCheck.h" +#include "ResetCallCheck.h" #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" @@ -144,6 +145,7 @@ class BugproneModule : public ClangTidyModule { "bugprone-inaccurate-erase"); CheckFactories.registerCheck( "bugprone-incorrect-enable-if"); +CheckFactories.registerCheck("bugprone-reset-call"); CheckFactories.registerCheck( "bugprone-return-const-ref-from-parameter"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index 13adad7c3dadbd..17ab5b27ec5550 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -52,6 +52,7 @@ add_clang_library(clangTidyBugproneModule STATIC PosixReturnCheck.cpp RedundantBranchConditionCheck.cpp ReservedIdentifierCheck.cpp + ResetCallCheck.cpp ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp diff --git a/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp new file mode 100644 index 00..305ac8d51adf3e --- /dev/null +++ b/clang-tools-extra/clang-tidy/bugprone/ResetCallCheck.cpp @@ -0,0 +1,133 @@ +//===--- ResetCallCheck.cpp - clang-tidy --===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "ResetCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void ResetCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +
[clang-tools-extra] [clang-tidy] Add bugprone-smartptr-reset-ambiguous-call check (PR #121291)
@@ -0,0 +1,134 @@ +//===--- SmartptrResetAmbiguousCallCheck.cpp - clang-tidy -===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// + +#include "SmartptrResetAmbiguousCallCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang::tidy::bugprone { + +namespace { + +AST_MATCHER_P(CallExpr, everyArgumentMatches, + ast_matchers::internal::Matcher, InnerMatcher) { + if (Node.getNumArgs() == 0) +return true; + + for (const auto *Arg : Node.arguments()) { +if (!InnerMatcher.matches(*Arg, Finder, Builder)) + return false; + } + return true; +} + +AST_MATCHER(CXXMethodDecl, hasOnlyDefaultArgs) { + if (Node.param_empty()) +return true; + + for (const auto *Param : Node.parameters()) { +if (!Param->hasDefaultArg()) + return false; + } + return true; +} + +} // namespace + +void SmartptrResetAmbiguousCallCheck::registerMatchers(MatchFinder *Finder) { + const auto IsSmartptr = hasAnyName("::std::unique_ptr", "::std::shared_ptr"); + + const auto ResetMethod = + cxxMethodDecl(hasName("reset"), hasOnlyDefaultArgs()); + + const auto TypeWithReset = + anyOf(cxxRecordDecl(hasMethod(ResetMethod)), +classTemplateSpecializationDecl( +hasSpecializedTemplate(classTemplateDecl(has(ResetMethod); + + const auto SmartptrWithBugproneReset = classTemplateSpecializationDecl( + IsSmartptr, + hasTemplateArgument( + 0, templateArgument(refersToType(hasUnqualifiedDesugaredType( + recordType(hasDeclaration(TypeWithReset))); + + // Find a.reset() calls + Finder->addMatcher( + cxxMemberCallExpr(callee(memberExpr(member(hasName("reset", +everyArgumentMatches(cxxDefaultArgExpr()), +on(expr(hasType(SmartptrWithBugproneReset + .bind("smartptrResetCall"), + this); + + // Find a->reset() calls + Finder->addMatcher( + cxxMemberCallExpr( + callee(memberExpr( + member(ResetMethod), + hasObjectExpression( + cxxOperatorCallExpr( + hasOverloadedOperatorName("->"), + hasArgument( + 0, expr(hasType( + classTemplateSpecializationDecl(IsSmartptr) + .bind("OpCall", + everyArgumentMatches(cxxDefaultArgExpr())) + .bind("objectResetCall"), + this); +} + +void SmartptrResetAmbiguousCallCheck::check( +const MatchFinder::MatchResult &Result) { + const auto *SmartptrResetCall = + Result.Nodes.getNodeAs("smartptrResetCall"); + const auto *ObjectResetCall = + Result.Nodes.getNodeAs("objectResetCall"); + + if (SmartptrResetCall) { +const auto *Member = cast(SmartptrResetCall->getCallee()); + +auto DiagBuilder = diag(SmartptrResetCall->getBeginLoc(), +"bugprone 'reset()' call on smart pointer"); + +DiagBuilder << FixItHint::CreateReplacement( +SourceRange(Member->getOperatorLoc(), +Member->getOperatorLoc().getLocWithOffset(0)), +" ="); + +DiagBuilder << FixItHint::CreateReplacement( +SourceRange(Member->getMemberLoc(), SmartptrResetCall->getEndLoc()), +" nullptr"); + } vbvictor wrote: Done https://github.com/llvm/llvm-project/pull/121291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add AllowedTypes to misc-const-correctness (PR #122951)
https://github.com/vbvictor created https://github.com/llvm/llvm-project/pull/122951 This PR add option `AllowedTypes` which allow users to specify types they want to exclude from const-correctness check. Small real-world example: ```cpp #include int main() { std::mutex m; std::lock_guard l(m); // we want to ignore it since std::lock_guard is already immutable. } ``` Closes issue https://github.com/llvm/llvm-project/issues/122592 >From 841cfec5a0ab4ce5ce64e71facfb7becaf4340e8 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Tue, 14 Jan 2025 22:05:43 +0300 Subject: [PATCH] [clang-tidy] add AllowedTypes to misc-const-correctness --- .../clang-tidy/misc/ConstCorrectnessCheck.cpp | 18 +- .../clang-tidy/misc/ConstCorrectnessCheck.h | 2 + clang-tools-extra/docs/ReleaseNotes.rst | 5 + .../checks/misc/const-correctness.rst | 10 + .../misc/const-correctness-allowed-types.cpp | 180 ++ 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-allowed-types.cpp diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp index 71a4cee4bdc6ef..aee4a3b789863c 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp @@ -8,6 +8,8 @@ #include "ConstCorrectnessCheck.h" #include "../utils/FixItHintUtils.h" +#include "../utils/Matchers.h" +#include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/ASTMatchers/ASTMatchers.h" @@ -41,7 +43,9 @@ ConstCorrectnessCheck::ConstCorrectnessCheck(StringRef Name, TransformValues(Options.get("TransformValues", true)), TransformReferences(Options.get("TransformReferences", true)), TransformPointersAsValues( - Options.get("TransformPointersAsValues", false)) { + Options.get("TransformPointersAsValues", false)), + AllowedTypes( + utils::options::parseStringList(Options.get("AllowedTypes", ""))) { if (AnalyzeValues == false && AnalyzeReferences == false) this->configurationDiag( "The check 'misc-const-correctness' will not " @@ -57,6 +61,9 @@ void ConstCorrectnessCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { Options.store(Opts, "TransformValues", TransformValues); Options.store(Opts, "TransformReferences", TransformReferences); Options.store(Opts, "TransformPointersAsValues", TransformPointersAsValues); + + Options.store(Opts, "AllowedTypes", +utils::options::serializeStringList(AllowedTypes)); } void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { @@ -73,6 +80,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType(), hasType(referenceType(pointee(substTemplateTypeParmType(); + const auto AllowedType = hasType(qualType(anyOf( + hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))), + pointerType(pointee(hasDeclaration( + namedDecl(matchers::matchesAnyListedName(AllowedTypes; + const auto AutoTemplateType = varDecl( anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType(, hasType(pointerType(pointee(autoType()); @@ -87,7 +100,8 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) { unless(anyOf(ConstType, ConstReference, TemplateType, hasInitializer(isInstantiationDependent()), AutoTemplateType, RValueReference, FunctionPointerRef, - hasType(cxxRecordDecl(isLambda())), isImplicit(; + hasType(cxxRecordDecl(isLambda())), isImplicit(), + AllowedType))); // Match the function scope for which the analysis of all local variables // shall be run. diff --git a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h index bba060e555d001..3b7aba7c4be384 100644 --- a/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.h @@ -45,6 +45,8 @@ class ConstCorrectnessCheck : public ClangTidyCheck { const bool TransformValues; const bool TransformReferences; const bool TransformPointersAsValues; + + const std::vector AllowedTypes; }; } // namespace clang::tidy::misc diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index fa3a8e577a33ad..16be70ca944e95 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -260,6 +260,11 @@ Changes in existing checks ` check to report a locati
[clang-tools-extra] [clang-tidy] add AllowedTypes option to misc-const-correctness (PR #122951)
@@ -196,3 +196,13 @@ Options // The following pointer may not become a 'int *const'. int *changing_pointee = &value; changing_pointee = &result; + +.. option:: AllowedTypes (default = '') + + A semicolon-separated list of names of types that + will be excluded from const-correctness checking. + Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type + with suffix `Ref`, `ref`, `Reference` and `reference`. + If a name in the list contains the sequence `::`, it is matched against + the qualified type name (i.e. `namespace::Type`), otherwise it is matched + against only the type name (i.e. `Type`). vbvictor wrote: Should I also make similar change in the previous line? \`namespace::Type\` -> \`\`namespace::Type\`\` https://github.com/llvm/llvm-project/pull/122951 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits