[PATCH] D60139: [clang-tidy] Add misc-placement-new-target-size check
Eugene.Zelenko added inline comments. Comment at: docs/clang-tidy/checks/misc-placement-new-target-size.rst:5 +== + + Eugene.Zelenko wrote: > Unnecessary empty line. Somehow empty line is still there. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60139/new/ https://reviews.llvm.org/D60139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D60139: [clang-tidy] Add misc-placement-new-target-size check
DennisL updated this revision to Diff 193471. DennisL added a comment. Simplify logic CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60139/new/ https://reviews.llvm.org/D60139 Files: clang-tidy/misc/CMakeLists.txt clang-tidy/misc/MiscTidyModule.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/misc-placement-new-target-size.rst test/clang-tidy/misc-placement-new-target-size.cpp Index: test/clang-tidy/misc-placement-new-target-size.cpp === --- /dev/null +++ test/clang-tidy/misc-placement-new-target-size.cpp @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s misc-placement-new-target-size %t + +using size_type = unsigned long; +void *operator new(size_type, void *); + +void f() { + struct Dummy { +int a; +int b; + }; + int *ptr = new int; + new (ptr) Dummy; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: placement new has insufficient target size [misc-placement-new-target-size] + delete ptr; +} Index: docs/clang-tidy/checks/misc-placement-new-target-size.rst === --- /dev/null +++ docs/clang-tidy/checks/misc-placement-new-target-size.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - misc-placement-new-target-size + +misc-placement-new-target-size +== + + +Finds placement-new calls where the size of the pointee type of the placement +parameter is smaller than the size of the constructed type and the pointer is +implicitly cast to ``void *``. + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -184,6 +184,7 @@ misc-new-delete-overloads misc-non-copyable-objects misc-non-private-member-variables-in-classes + misc-placement-new-target-size misc-redundant-expression misc-static-assert misc-throw-by-value-catch-by-reference Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -130,6 +130,13 @@ ` now supports `OverrideSpelling` and `FinalSpelling` options. +- New :doc:`misc-placement-new-target-size + ` check. + + Finds placement-new calls where the size of the pointee type of the placement + parameter is smaller than the size of the constructed type and the pointer is + implicitly cast to ``void *``. + - New :doc:`openmp-exception-escape ` check. Index: clang-tidy/misc/PlacementNewTargetSizeCheck.h === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.h @@ -0,0 +1,37 @@ +//===--- PlacementNewTargetSizeCheck.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_MISC_PLACEMENTNEWTARGETSIZECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// This checker finds placement new statements that construct objects into +/// allocated space where the pointer to that allocated space is of a type +/// smaller than the constructed object and the pointer is implicitly cast to +/// void *. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-placement-new-target-size.html +class PlacementNewTargetSizeCheck : public ClangTidyCheck { +public: + PlacementNewTargetSizeCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H Index: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.cpp @@ -0,0 +1,78 @@ +//===--- PlacementNewTargetSizeCheck.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 "PlacementNewTargetSizeCheck.h" +#include "
[PATCH] D60139: [clang-tidy] Add misc-placement-new-target-size check
DennisL updated this revision to Diff 193454. DennisL added a comment. Remove debug output CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60139/new/ https://reviews.llvm.org/D60139 Files: clang-tidy/misc/CMakeLists.txt clang-tidy/misc/MiscTidyModule.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/misc-placement-new-target-size.rst test/clang-tidy/misc-placement-new-target-size.cpp Index: test/clang-tidy/misc-placement-new-target-size.cpp === --- /dev/null +++ test/clang-tidy/misc-placement-new-target-size.cpp @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s misc-placement-new-target-size %t + +using size_type = unsigned long; +void *operator new(size_type, void *); + +void f() { + struct Dummy { +int a; +int b; + }; + int *ptr = new int; + new (ptr) Dummy; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: placement new has insufficient target size [misc-placement-new-target-size] + delete ptr; +} Index: docs/clang-tidy/checks/misc-placement-new-target-size.rst === --- /dev/null +++ docs/clang-tidy/checks/misc-placement-new-target-size.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - misc-placement-new-target-size + +misc-placement-new-target-size +== + + +Finds placement-new calls where the size of the pointee type of the placement +parameter is smaller than the size of the constructed type and the pointer is +implicitly cast to ``void *``. + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -184,6 +184,7 @@ misc-new-delete-overloads misc-non-copyable-objects misc-non-private-member-variables-in-classes + misc-placement-new-target-size misc-redundant-expression misc-static-assert misc-throw-by-value-catch-by-reference Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -130,6 +130,13 @@ ` now supports `OverrideSpelling` and `FinalSpelling` options. +- New :doc:`misc-placement-new-target-size + ` check. + + Finds placement-new calls where the size of the pointee type of the placement + parameter is smaller than the size of the constructed type and the pointer is + implicitly cast to ``void *``. + - New :doc:`openmp-exception-escape ` check. Index: clang-tidy/misc/PlacementNewTargetSizeCheck.h === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.h @@ -0,0 +1,37 @@ +//===--- PlacementNewTargetSizeCheck.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_MISC_PLACEMENTNEWTARGETSIZECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// This checker finds placement new statements that construct objects into +/// allocated space where the pointer to that allocated space is of a type +/// smaller than the constructed object and the pointer is implicitly cast to +/// void *. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-placement-new-target-size.html +class PlacementNewTargetSizeCheck : public ClangTidyCheck { +public: + PlacementNewTargetSizeCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H Index: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.cpp @@ -0,0 +1,78 @@ +//===--- PlacementNewTargetSizeCheck.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 "PlacementNewTargetSizeCheck.h" +#incl
[PATCH] D60139: [clang-tidy] Add misc-placement-new-target-size check
DennisL updated this revision to Diff 193449. DennisL marked 13 inline comments as done. DennisL added a comment. Updated patch to address reviewer feedback CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60139/new/ https://reviews.llvm.org/D60139 Files: clang-tidy/misc/CMakeLists.txt clang-tidy/misc/MiscTidyModule.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.cpp clang-tidy/misc/PlacementNewTargetSizeCheck.h docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/misc-placement-new-target-size.rst test/clang-tidy/misc-placement-new-target-size.cpp Index: test/clang-tidy/misc-placement-new-target-size.cpp === --- /dev/null +++ test/clang-tidy/misc-placement-new-target-size.cpp @@ -0,0 +1,15 @@ +// RUN: %check_clang_tidy %s misc-placement-new-target-size %t + +using size_type = unsigned long; +void *operator new(size_type, void *); + +void f() { + struct Dummy { +int a; +int b; + }; + int *ptr = new int; + new (ptr) Dummy; + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: placement new has insufficient target size [misc-placement-new-target-size] + delete ptr; +} Index: docs/clang-tidy/checks/misc-placement-new-target-size.rst === --- /dev/null +++ docs/clang-tidy/checks/misc-placement-new-target-size.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - misc-placement-new-target-size + +misc-placement-new-target-size +== + + +Finds placement-new calls where the size of the pointee type of the placement +parameter is smaller than the size of the constructed type and the pointer is +implicitly cast to ``void *``. + Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -184,6 +184,7 @@ misc-new-delete-overloads misc-non-copyable-objects misc-non-private-member-variables-in-classes + misc-placement-new-target-size misc-redundant-expression misc-static-assert misc-throw-by-value-catch-by-reference Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -130,6 +130,13 @@ ` now supports `OverrideSpelling` and `FinalSpelling` options. +- New :doc:`misc-placement-new-target-size + ` check. + + Finds placement-new calls where the size of the pointee type of the placement + parameter is smaller than the size of the constructed type and the pointer is + implicitly cast to ``void *``. + - New :doc:`openmp-exception-escape ` check. Index: clang-tidy/misc/PlacementNewTargetSizeCheck.h === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.h @@ -0,0 +1,37 @@ +//===--- PlacementNewTargetSizeCheck.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_MISC_PLACEMENTNEWTARGETSIZECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// This checker finds placement new statements that construct objects into +/// allocated space where the pointer to that allocated space is of a type +/// smaller than the constructed object and the pointer is implicitly cast to +/// void *. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-placement-new-target-size.html +class PlacementNewTargetSizeCheck : public ClangTidyCheck { +public: + PlacementNewTargetSizeCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PLACEMENTNEWTARGETSIZECHECK_H Index: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp === --- /dev/null +++ clang-tidy/misc/PlacementNewTargetSizeCheck.cpp @@ -0,0 +1,79 @@ +//===--- PlacementNewTargetSizeCheck.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 +// +//===
[PATCH] D60139: [clang-tidy] Add misc-placement-new-target-size check
Eugene.Zelenko added inline comments. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:10 +#include + +#include "PlacementNewTargetSizeCheck.h" Unnecessary empty line. Please run Clang-format after fixing. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:23 +namespace { +CharUnits getSizeOfType(clang::ASTContext *const Context, const QualType& type) { + CharUnits TypeSize; Please use static instead of anonymous namespaces. See LLVM Coding Guidelines. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:26 + if (type->isRecordType()) { +const auto *CXXRecordDecl = type->getAsRecordDecl(); +assert(CXXRecordDecl && "type->getAsRecordDecl() failed"); Please don't use auto where type could not be easily deduced. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:28 +assert(CXXRecordDecl && "type->getAsRecordDecl() failed"); +const auto &CXXDeclLayout = +CXXRecordDecl->getASTContext().getASTRecordLayout(CXXRecordDecl); Please don't use auto where type could not be easily deduced. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:47 +const MatchFinder::MatchResult &Result) { + const CXXNewExpr *Alloc = Result.Nodes.getNodeAs("Alloc"); + assert(Alloc && "Matched node bound by 'Alloc' shoud be a 'CXXNewExpr'"); auto could be used here, because of cast. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:50 + + auto numPlacementArgs = Alloc->getNumPlacementArgs(); + if (0 == numPlacementArgs) { Please don't use auto where type could not be easily deduced. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:55 + + auto type = Alloc->getAllocatedType(); + CharUnits TypeSize = getSizeOfType(Result.Context, type); Please don't use auto where type could not be easily deduced. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:62 + // get ptr type of implicit cast + const ImplicitCastExpr *Cast = Result.Nodes.getNodeAs("Cast"); + auto CastType = Cast->getSubExpr()->getType(); auto could be used here, because of cast. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:63 + const ImplicitCastExpr *Cast = Result.Nodes.getNodeAs("Cast"); + auto CastType = Cast->getSubExpr()->getType(); + if (CastType->isPointerType()) { Please don't use auto where type could not be easily deduced. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:65 + if (CastType->isPointerType()) { + CharUnits CastSize = getSizeOfType(Result.Context, CastType->getPointeeType()); +if ((TypeSize - CastSize).isPositive()) { Please run Clang-format. Comment at: clang-tidy/misc/PlacementNewTargetSizeCheck.cpp:73 + } + +} Unnecessary empty line. Comment at: docs/ReleaseNotes.rst:135 + ` check. + - New :doc:`openmp-exception-escape Please add one sentence description. Should be same as in documentation. Comment at: docs/clang-tidy/checks/misc-placement-new-target-size.rst:5 +== + + Unnecessary empty line. Repository: rCTE Clang Tools Extra CHANGES SINCE LAST ACTION https://reviews.llvm.org/D60139/new/ https://reviews.llvm.org/D60139 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits