diff --git a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt --- a/clang-tools-extra/clang-tidy/performance/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/performance/CMakeLists.txt @@ -13,6 +13,7 @@ MoveConstArgCheck.cpp MoveConstructorInitCheck.cpp NoAutomaticMoveCheck.cpp + NoIntToPtrCheck.cpp NoexceptMoveConstructorCheck.cpp PerformanceTidyModule.cpp TriviallyDestructibleCheck.cpp diff --git a/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.h b/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.h new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.h @@ -0,0 +1,34 @@ +//===--- NoIntToPtrCheck.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_PERFORMANCE_NOINTTOPTRCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOINTTOPTRCHECK_H + +#include "../ClangTidyCheck.h" + +namespace clang { +namespace tidy { +namespace performance { + +/// Diagnoses every integer to pointer cast. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/performance-no-inttoptr.html +class NoIntToPtrCheck : public ClangTidyCheck { +public: + NoIntToPtrCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace performance +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_NOINTTOPTRCHECK_H diff --git a/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/clang-tidy/performance/NoIntToPtrCheck.cpp @@ -0,0 +1,33 @@ +//===--- NoIntToPtrCheck.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 "NoIntToPtrCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace performance { + +void NoIntToPtrCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(castExpr(hasCastKind(CK_IntegralToPointer), + unless(hasSourceExpression(integerLiteral()))) + .bind("x"), + this); +} + +void NoIntToPtrCheck::check(const MatchFinder::MatchResult &Result) { + const auto *MatchedCast = Result.Nodes.getNodeAs("x"); + diag(MatchedCast->getBeginLoc(), "avoid integer to pointer casts"); +} + +} // namespace performance +} // namespace tidy +} // namespace clang diff --git a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp --- a/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp @@ -18,6 +18,7 @@ #include "MoveConstArgCheck.h" #include "MoveConstructorInitCheck.h" #include "NoAutomaticMoveCheck.h" +#include "NoIntToPtrCheck.h" #include "NoexceptMoveConstructorCheck.h" #include "TriviallyDestructibleCheck.h" #include "TypePromotionInMathFnCheck.h" @@ -49,6 +50,7 @@ "performance-move-constructor-init"); CheckFactories.registerCheck( "performance-no-automatic-move"); + CheckFactories.registerCheck("performance-no-inttoptr"); CheckFactories.registerCheck( "performance-noexcept-move-constructor"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -135,6 +135,11 @@ Alias to the :doc:`bugprone-signal-handler ` check. +- New :doc:`performance-no-inttoptr + ` check. + + Diagnoses every integer to pointer cast. + - New :doc:`readability-function-cognitive-complexity ` check. @@ -156,8 +161,8 @@ Now renames overridden virtual methods if the method they override has a style violation. - - Added support for specifying the style of scoped ``enum`` constants. If + + Added support for specifying the style of scoped ``enum`` constants. If unspecified, will fall back to the style for regular ``enum`` constants. Added an option `IgnoredRegexp` per identifier type to suppress identifier diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -12,30 +12,30 @@ .. csv-table:: :header: "Name", "Offers fixes" - `abseil-duration-addition `_, "Yes" - `abseil-duration-comparison `_, "Yes" - `abseil-duration-conversion-cast `_, "Yes" - `abseil-duration-division `_, "Yes" - `abseil-duration-factory-float `_, "Yes" - `abseil-duration-factory-scale `_, "Yes" - `abseil-duration-subtraction `_, "Yes" - `abseil-duration-unnecessary-conversion `_, "Yes" - `abseil-faster-strsplit-delimiter `_, "Yes" + `abseil-duration-addition `_, + `abseil-duration-comparison `_, + `abseil-duration-conversion-cast `_, + `abseil-duration-division `_, + `abseil-duration-factory-float `_, + `abseil-duration-factory-scale `_, + `abseil-duration-subtraction `_, + `abseil-duration-unnecessary-conversion `_, + `abseil-faster-strsplit-delimiter `_, `abseil-no-internal-dependencies `_, `abseil-no-namespace `_, - `abseil-redundant-strcat-calls `_, "Yes" - `abseil-str-cat-append `_, "Yes" - `abseil-string-find-startswith `_, "Yes" - `abseil-string-find-str-contains `_, "Yes" - `abseil-time-comparison `_, "Yes" - `abseil-time-subtraction `_, "Yes" - `abseil-upgrade-duration-conversions `_, "Yes" + `abseil-redundant-strcat-calls `_, + `abseil-str-cat-append `_, + `abseil-string-find-startswith `_, + `abseil-string-find-str-contains `_, + `abseil-time-comparison `_, + `abseil-time-subtraction `_, + `abseil-upgrade-duration-conversions `_, `altera-kernel-name-restriction `_, `altera-struct-pack-align `_, - `android-cloexec-accept `_, "Yes" + `android-cloexec-accept `_, `android-cloexec-accept4 `_, - `android-cloexec-creat `_, "Yes" - `android-cloexec-dup `_, "Yes" + `android-cloexec-creat `_, + `android-cloexec-dup `_, `android-cloexec-epoll-create `_, `android-cloexec-epoll-create1 `_, `android-cloexec-fopen `_, @@ -43,66 +43,66 @@ `android-cloexec-inotify-init1 `_, `android-cloexec-memfd-create `_, `android-cloexec-open `_, - `android-cloexec-pipe `_, "Yes" + `android-cloexec-pipe `_, `android-cloexec-pipe2 `_, `android-cloexec-socket `_, `android-comparison-in-temp-failure-retry `_, - `boost-use-to-string `_, "Yes" - `bugprone-argument-comment `_, "Yes" + `boost-use-to-string `_, + `bugprone-argument-comment `_, `bugprone-assert-side-effect `_, `bugprone-bad-signal-to-kill-thread `_, - `bugprone-bool-pointer-implicit-conversion `_, "Yes" + `bugprone-bool-pointer-implicit-conversion `_, `bugprone-branch-clone `_, - `bugprone-copy-constructor-init `_, "Yes" + `bugprone-copy-constructor-init `_, `bugprone-dangling-handle `_, `bugprone-dynamic-static-initializers `_, `bugprone-exception-escape `_, `bugprone-fold-init-type `_, `bugprone-forward-declaration-namespace `_, `bugprone-forwarding-reference-overload `_, - `bugprone-inaccurate-erase `_, "Yes" + `bugprone-inaccurate-erase `_, `bugprone-incorrect-roundings `_, `bugprone-infinite-loop `_, `bugprone-integer-division `_, `bugprone-lambda-function-name `_, - `bugprone-macro-parentheses `_, "Yes" + `bugprone-macro-parentheses `_, `bugprone-macro-repeated-side-effects `_, - `bugprone-misplaced-operator-in-strlen-in-alloc `_, "Yes" - `bugprone-misplaced-pointer-arithmetic-in-alloc `_, "Yes" + `bugprone-misplaced-operator-in-strlen-in-alloc `_, + `bugprone-misplaced-pointer-arithmetic-in-alloc `_, `bugprone-misplaced-widening-cast `_, - `bugprone-move-forwarding-reference `_, "Yes" + `bugprone-move-forwarding-reference `_, `bugprone-multiple-statement-macro `_, `bugprone-no-escape `_, - `bugprone-not-null-terminated-result `_, "Yes" - `bugprone-parent-virtual-call `_, "Yes" - `bugprone-posix-return `_, "Yes" - `bugprone-redundant-branch-condition `_, "Yes" - `bugprone-reserved-identifier `_, "Yes" + `bugprone-not-null-terminated-result `_, + `bugprone-parent-virtual-call `_, + `bugprone-posix-return `_, + `bugprone-redundant-branch-condition `_, + `bugprone-reserved-identifier `_, `bugprone-signal-handler `_, `bugprone-signed-char-misuse `_, `bugprone-sizeof-container `_, `bugprone-sizeof-expression `_, `bugprone-spuriously-wake-up-functions `_, - `bugprone-string-constructor `_, "Yes" - `bugprone-string-integer-assignment `_, "Yes" + `bugprone-string-constructor `_, + `bugprone-string-integer-assignment `_, `bugprone-string-literal-with-embedded-nul `_, `bugprone-suspicious-enum-usage `_, `bugprone-suspicious-include `_, - `bugprone-suspicious-memset-usage `_, "Yes" + `bugprone-suspicious-memset-usage `_, `bugprone-suspicious-missing-comma `_, - `bugprone-suspicious-semicolon `_, "Yes" - `bugprone-suspicious-string-compare `_, "Yes" - `bugprone-swapped-arguments `_, "Yes" - `bugprone-terminating-continue `_, "Yes" + `bugprone-suspicious-semicolon `_, + `bugprone-suspicious-string-compare `_, + `bugprone-swapped-arguments `_, + `bugprone-terminating-continue `_, `bugprone-throw-keyword-missing `_, `bugprone-too-small-loop-variable `_, `bugprone-undefined-memory-manipulation `_, `bugprone-undelegated-constructor `_, `bugprone-unhandled-self-assignment `_, - `bugprone-unused-raii `_, "Yes" + `bugprone-unused-raii `_, `bugprone-unused-return-value `_, `bugprone-use-after-move `_, - `bugprone-virtual-near-miss `_, "Yes" + `bugprone-virtual-near-miss `_, `cert-dcl21-cpp `_, `cert-dcl50-cpp `_, `cert-dcl58-cpp `_, @@ -117,7 +117,6 @@ `cert-msc51-cpp `_, `cert-oop57-cpp `_, `cert-oop58-cpp `_, - `cert-sig30-c `_, `clang-analyzer-core.DynamicTypePropagation `_, `clang-analyzer-core.uninitialized.CapturedBlockVariable `_, `clang-analyzer-cplusplus.InnerPointer `_, @@ -141,28 +140,29 @@ `concurrency-mt-unsafe `_, `cppcoreguidelines-avoid-goto `_, `cppcoreguidelines-avoid-non-const-global-variables `_, - `cppcoreguidelines-init-variables `_, "Yes" + `cppcoreguidelines-init-variables `_, `cppcoreguidelines-interfaces-global-init `_, `cppcoreguidelines-macro-usage `_, `cppcoreguidelines-narrowing-conversions `_, `cppcoreguidelines-no-malloc `_, `cppcoreguidelines-owning-memory `_, + `cppcoreguidelines-prefer-member-initializer `_, `cppcoreguidelines-pro-bounds-array-to-pointer-decay `_, - `cppcoreguidelines-pro-bounds-constant-array-index `_, "Yes" + `cppcoreguidelines-pro-bounds-constant-array-index `_, `cppcoreguidelines-pro-bounds-pointer-arithmetic `_, `cppcoreguidelines-pro-type-const-cast `_, - `cppcoreguidelines-pro-type-cstyle-cast `_, "Yes" - `cppcoreguidelines-pro-type-member-init `_, "Yes" + `cppcoreguidelines-pro-type-cstyle-cast `_, + `cppcoreguidelines-pro-type-member-init `_, `cppcoreguidelines-pro-type-reinterpret-cast `_, - `cppcoreguidelines-pro-type-static-cast-downcast `_, "Yes" + `cppcoreguidelines-pro-type-static-cast-downcast `_, `cppcoreguidelines-pro-type-union-access `_, `cppcoreguidelines-pro-type-vararg `_, `cppcoreguidelines-slicing `_, `cppcoreguidelines-special-member-functions `_, `darwin-avoid-spinlock `_, - `darwin-dispatch-once-nonstatic `_, "Yes" + `darwin-dispatch-once-nonstatic `_, `fuchsia-default-arguments-calls `_, - `fuchsia-default-arguments-declarations `_, "Yes" + `fuchsia-default-arguments-declarations `_, `fuchsia-multiple-inheritance `_, `fuchsia-overloaded-operator `_, `fuchsia-statically-constructed-objects `_, @@ -172,7 +172,7 @@ `google-build-namespaces `_, `google-build-using-namespace `_, `google-default-arguments `_, - `google-explicit-constructor `_, "Yes" + `google-explicit-constructor `_, `google-global-names-in-headers `_, `google-objc-avoid-nsobject-new `_, `google-objc-avoid-throwing-exception `_, @@ -183,8 +183,7 @@ `google-readability-todo `_, `google-runtime-int `_, `google-runtime-operator `_, - `google-runtime-references `_, - `google-upgrade-googletest-case `_, "Yes" + `google-upgrade-googletest-case `_, `hicpp-avoid-goto `_, `hicpp-exception-baseclass `_, `hicpp-multiway-paths-covered `_, @@ -192,123 +191,124 @@ `hicpp-signed-bitwise `_, `linuxkernel-must-use-errs `_, `llvm-header-guard `_, - `llvm-include-order `_, "Yes" + `llvm-include-order `_, `llvm-namespace-comment `_, - `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, "Yes" - `llvm-prefer-register-over-unsigned `_, "Yes" - `llvm-twine-local `_, "Yes" + `llvm-prefer-isa-or-dyn-cast-in-conditionals `_, + `llvm-prefer-register-over-unsigned `_, + `llvm-twine-local `_, `llvmlibc-callee-namespace `_, `llvmlibc-implementation-in-namespace `_, - `llvmlibc-restrict-system-libc-headers `_, "Yes" - `misc-definitions-in-headers `_, "Yes" + `llvmlibc-restrict-system-libc-headers `_, + `misc-definitions-in-headers `_, `misc-misplaced-const `_, `misc-new-delete-overloads `_, `misc-no-recursion `_, `misc-non-copyable-objects `_, `misc-non-private-member-variables-in-classes `_, - `misc-redundant-expression `_, "Yes" - `misc-static-assert `_, "Yes" + `misc-redundant-expression `_, + `misc-static-assert `_, `misc-throw-by-value-catch-by-reference `_, `misc-unconventional-assign-operator `_, - `misc-uniqueptr-reset-release `_, "Yes" - `misc-unused-alias-decls `_, "Yes" - `misc-unused-parameters `_, "Yes" - `misc-unused-using-decls `_, "Yes" - `modernize-avoid-bind `_, "Yes" + `misc-uniqueptr-reset-release `_, + `misc-unused-alias-decls `_, + `misc-unused-parameters `_, + `misc-unused-using-decls `_, + `modernize-avoid-bind `_, `modernize-avoid-c-arrays `_, - `modernize-concat-nested-namespaces `_, "Yes" - `modernize-deprecated-headers `_, "Yes" - `modernize-deprecated-ios-base-aliases `_, "Yes" - `modernize-loop-convert `_, "Yes" - `modernize-make-shared `_, "Yes" - `modernize-make-unique `_, "Yes" - `modernize-pass-by-value `_, "Yes" - `modernize-raw-string-literal `_, "Yes" - `modernize-redundant-void-arg `_, "Yes" - `modernize-replace-auto-ptr `_, "Yes" - `modernize-replace-disallow-copy-and-assign-macro `_, "Yes" - `modernize-replace-random-shuffle `_, "Yes" - `modernize-return-braced-init-list `_, "Yes" - `modernize-shrink-to-fit `_, "Yes" - `modernize-unary-static-assert `_, "Yes" - `modernize-use-auto `_, "Yes" - `modernize-use-bool-literals `_, "Yes" - `modernize-use-default-member-init `_, "Yes" - `modernize-use-emplace `_, "Yes" - `modernize-use-equals-default `_, "Yes" - `modernize-use-equals-delete `_, "Yes" - `modernize-use-nodiscard `_, "Yes" - `modernize-use-noexcept `_, "Yes" - `modernize-use-nullptr `_, "Yes" - `modernize-use-override `_, "Yes" - `modernize-use-trailing-return-type `_, "Yes" - `modernize-use-transparent-functors `_, "Yes" - `modernize-use-uncaught-exceptions `_, "Yes" - `modernize-use-using `_, "Yes" - `mpi-buffer-deref `_, "Yes" - `mpi-type-mismatch `_, "Yes" + `modernize-concat-nested-namespaces `_, + `modernize-deprecated-headers `_, + `modernize-deprecated-ios-base-aliases `_, + `modernize-loop-convert `_, + `modernize-make-shared `_, + `modernize-make-unique `_, + `modernize-pass-by-value `_, + `modernize-raw-string-literal `_, + `modernize-redundant-void-arg `_, + `modernize-replace-auto-ptr `_, + `modernize-replace-disallow-copy-and-assign-macro `_, + `modernize-replace-random-shuffle `_, + `modernize-return-braced-init-list `_, + `modernize-shrink-to-fit `_, + `modernize-unary-static-assert `_, + `modernize-use-auto `_, + `modernize-use-bool-literals `_, + `modernize-use-default-member-init `_, + `modernize-use-emplace `_, + `modernize-use-equals-default `_, + `modernize-use-equals-delete `_, + `modernize-use-nodiscard `_, + `modernize-use-noexcept `_, + `modernize-use-nullptr `_, + `modernize-use-override `_, + `modernize-use-trailing-return-type `_, + `modernize-use-transparent-functors `_, + `modernize-use-uncaught-exceptions `_, + `modernize-use-using `_, + `mpi-buffer-deref `_, + `mpi-type-mismatch `_, `objc-avoid-nserror-init `_, `objc-dealloc-in-category `_, `objc-forbidden-subclassing `_, `objc-missing-hash `_, - `objc-nsinvocation-argument-lifetime `_, "Yes" - `objc-property-declaration `_, "Yes" - `objc-super-self `_, "Yes" + `objc-nsinvocation-argument-lifetime `_, + `objc-property-declaration `_, + `objc-super-self `_, `openmp-exception-escape `_, `openmp-use-default-none `_, - `performance-faster-string-find `_, "Yes" - `performance-for-range-copy `_, "Yes" + `performance-faster-string-find `_, + `performance-for-range-copy `_, `performance-implicit-conversion-in-loop `_, - `performance-inefficient-algorithm `_, "Yes" + `performance-inefficient-algorithm `_, `performance-inefficient-string-concatenation `_, - `performance-inefficient-vector-operation `_, "Yes" - `performance-move-const-arg `_, "Yes" - `performance-move-constructor-init `_, "Yes" + `performance-inefficient-vector-operation `_, + `performance-move-const-arg `_, + `performance-move-constructor-init `_, `performance-no-automatic-move `_, - `performance-noexcept-move-constructor `_, "Yes" - `performance-trivially-destructible `_, "Yes" - `performance-type-promotion-in-math-fn `_, "Yes" + `performance-no-inttoptr `_, + `performance-noexcept-move-constructor `_, + `performance-trivially-destructible `_, + `performance-type-promotion-in-math-fn `_, `performance-unnecessary-copy-initialization `_, - `performance-unnecessary-value-param `_, "Yes" - `portability-restrict-system-includes `_, "Yes" + `performance-unnecessary-value-param `_, + `portability-restrict-system-includes `_, `portability-simd-intrinsics `_, `readability-avoid-const-params-in-decls `_, - `readability-braces-around-statements `_, "Yes" - `readability-const-return-type `_, "Yes" - `readability-container-size-empty `_, "Yes" + `readability-braces-around-statements `_, + `readability-const-return-type `_, + `readability-container-size-empty `_, `readability-convert-member-functions-to-static `_, - `readability-delete-null-pointer `_, "Yes" + `readability-delete-null-pointer `_, `readability-deleted-default `_, - `readability-else-after-return `_, "Yes" + `readability-else-after-return `_, `readability-function-cognitive-complexity `_, `readability-function-size `_, - `readability-identifier-naming `_, "Yes" - `readability-implicit-bool-conversion `_, "Yes" - `readability-inconsistent-declaration-parameter-name `_, "Yes" - `readability-isolate-declaration `_, "Yes" + `readability-identifier-naming `_, + `readability-implicit-bool-conversion `_, + `readability-inconsistent-declaration-parameter-name `_, + `readability-isolate-declaration `_, `readability-magic-numbers `_, - `readability-make-member-function-const `_, "Yes" + `readability-make-member-function-const `_, `readability-misleading-indentation `_, - `readability-misplaced-array-index `_, "Yes" - `readability-named-parameter `_, "Yes" - `readability-non-const-parameter `_, "Yes" - `readability-qualified-auto `_, "Yes" - `readability-redundant-access-specifiers `_, "Yes" - `readability-redundant-control-flow `_, "Yes" - `readability-redundant-declaration `_, "Yes" - `readability-redundant-function-ptr-dereference `_, "Yes" - `readability-redundant-member-init `_, "Yes" + `readability-misplaced-array-index `_, + `readability-named-parameter `_, + `readability-non-const-parameter `_, + `readability-qualified-auto `_, + `readability-redundant-access-specifiers `_, + `readability-redundant-control-flow `_, + `readability-redundant-declaration `_, + `readability-redundant-function-ptr-dereference `_, + `readability-redundant-member-init `_, `readability-redundant-preprocessor `_, - `readability-redundant-smartptr-get `_, "Yes" - `readability-redundant-string-cstr `_, "Yes" - `readability-redundant-string-init `_, "Yes" - `readability-simplify-boolean-expr `_, "Yes" - `readability-simplify-subscript-expr `_, "Yes" - `readability-static-accessed-through-instance `_, "Yes" - `readability-static-definition-in-anonymous-namespace `_, "Yes" - `readability-string-compare `_, "Yes" - `readability-uniqueptr-delete-release `_, "Yes" - `readability-uppercase-literal-suffix `_, "Yes" + `readability-redundant-smartptr-get `_, + `readability-redundant-string-cstr `_, + `readability-redundant-string-init `_, + `readability-simplify-boolean-expr `_, + `readability-simplify-subscript-expr `_, + `readability-static-accessed-through-instance `_, + `readability-static-definition-in-anonymous-namespace `_, + `readability-string-compare `_, + `readability-uniqueptr-delete-release `_, + `readability-uppercase-literal-suffix `_, `readability-use-anyofallof `_, `zircon-temporary-objects `_, @@ -318,10 +318,10 @@ `cert-con36-c `_, `bugprone-spuriously-wake-up-functions `_, `cert-con54-cpp `_, `bugprone-spuriously-wake-up-functions `_, - `cert-dcl03-c `_, `misc-static-assert `_, "Yes" - `cert-dcl16-c `_, `readability-uppercase-literal-suffix `_, "Yes" - `cert-dcl37-c `_, `bugprone-reserved-identifier `_, "Yes" - `cert-dcl51-cpp `_, `bugprone-reserved-identifier `_, "Yes" + `cert-dcl03-c `_, `misc-static-assert `_, + `cert-dcl16-c `_, `readability-uppercase-literal-suffix `_, + `cert-dcl37-c `_, `bugprone-reserved-identifier `_, + `cert-dcl51-cpp `_, `bugprone-reserved-identifier `_, `cert-dcl54-cpp `_, `misc-new-delete-overloads `_, `cert-dcl59-cpp `_, `google-build-namespaces `_, `cert-err09-cpp `_, `misc-throw-by-value-catch-by-reference `_, @@ -329,9 +329,10 @@ `cert-fio38-c `_, `misc-non-copyable-objects `_, `cert-msc30-c `_, `cert-msc50-cpp `_, `cert-msc32-c `_, `cert-msc51-cpp `_, - `cert-oop11-cpp `_, `performance-move-constructor-init `_, "Yes" + `cert-oop11-cpp `_, `performance-move-constructor-init `_, `cert-oop54-cpp `_, `bugprone-unhandled-self-assignment `_, `cert-pos44-c `_, `bugprone-bad-signal-to-kill-thread `_, + `cert-sig30-c `_, `bugprone-signal-handler `_, `cert-str34-c `_, `bugprone-signed-char-misuse `_, `clang-analyzer-core.CallAndMessage `_, `Clang Static Analyzer `_, `clang-analyzer-core.DivideZero `_, `Clang Static Analyzer `_, @@ -400,36 +401,36 @@ `cppcoreguidelines-avoid-c-arrays `_, `modernize-avoid-c-arrays `_, `cppcoreguidelines-avoid-magic-numbers `_, `readability-magic-numbers `_, `cppcoreguidelines-c-copy-assignment-signature `_, `misc-unconventional-assign-operator `_, - `cppcoreguidelines-explicit-virtual-functions `_, `modernize-use-override `_, "Yes" + `cppcoreguidelines-explicit-virtual-functions `_, `modernize-use-override `_, `cppcoreguidelines-non-private-member-variables-in-classes `_, `misc-non-private-member-variables-in-classes `_, `fuchsia-header-anon-namespaces `_, `google-build-namespaces `_, - `google-readability-braces-around-statements `_, `readability-braces-around-statements `_, "Yes" + `google-readability-braces-around-statements `_, `readability-braces-around-statements `_, `google-readability-function-size `_, `readability-function-size `_, `google-readability-namespace-comments `_, `llvm-namespace-comment `_, `hicpp-avoid-c-arrays `_, `modernize-avoid-c-arrays `_, - `hicpp-braces-around-statements `_, `readability-braces-around-statements `_, "Yes" - `hicpp-deprecated-headers `_, `modernize-deprecated-headers `_, "Yes" - `hicpp-explicit-conversions `_, `google-explicit-constructor `_, "Yes" + `hicpp-braces-around-statements `_, `readability-braces-around-statements `_, + `hicpp-deprecated-headers `_, `modernize-deprecated-headers `_, + `hicpp-explicit-conversions `_, `google-explicit-constructor `_, `hicpp-function-size `_, `readability-function-size `_, `hicpp-invalid-access-moved `_, `bugprone-use-after-move `_, - `hicpp-member-init `_, `cppcoreguidelines-pro-type-member-init `_, "Yes" - `hicpp-move-const-arg `_, `performance-move-const-arg `_, "Yes" - `hicpp-named-parameter `_, `readability-named-parameter `_, "Yes" + `hicpp-member-init `_, `cppcoreguidelines-pro-type-member-init `_, + `hicpp-move-const-arg `_, `performance-move-const-arg `_, + `hicpp-named-parameter `_, `readability-named-parameter `_, `hicpp-new-delete-operators `_, `misc-new-delete-overloads `_, `hicpp-no-array-decay `_, `cppcoreguidelines-pro-bounds-array-to-pointer-decay `_, `hicpp-no-malloc `_, `cppcoreguidelines-no-malloc `_, - `hicpp-noexcept-move `_, `performance-noexcept-move-constructor `_, "Yes" + `hicpp-noexcept-move `_, `performance-noexcept-move-constructor `_, `hicpp-special-member-functions `_, `cppcoreguidelines-special-member-functions `_, - `hicpp-static-assert `_, `misc-static-assert `_, "Yes" + `hicpp-static-assert `_, `misc-static-assert `_, `hicpp-undelegated-constructor `_, `bugprone-undelegated-constructor `_, - `hicpp-uppercase-literal-suffix `_, `readability-uppercase-literal-suffix `_, "Yes" - `hicpp-use-auto `_, `modernize-use-auto `_, "Yes" - `hicpp-use-emplace `_, `modernize-use-emplace `_, "Yes" - `hicpp-use-equals-default `_, `modernize-use-equals-default `_, "Yes" - `hicpp-use-equals-delete `_, `modernize-use-equals-delete `_, "Yes" - `hicpp-use-noexcept `_, `modernize-use-noexcept `_, "Yes" - `hicpp-use-nullptr `_, `modernize-use-nullptr `_, "Yes" - `hicpp-use-override `_, `modernize-use-override `_, "Yes" + `hicpp-uppercase-literal-suffix `_, `readability-uppercase-literal-suffix `_, + `hicpp-use-auto `_, `modernize-use-auto `_, + `hicpp-use-emplace `_, `modernize-use-emplace `_, + `hicpp-use-equals-default `_, `modernize-use-equals-default `_, + `hicpp-use-equals-delete `_, `modernize-use-equals-delete `_, + `hicpp-use-noexcept `_, `modernize-use-noexcept `_, + `hicpp-use-nullptr `_, `modernize-use-nullptr `_, + `hicpp-use-override `_, `modernize-use-override `_, `hicpp-vararg `_, `cppcoreguidelines-pro-type-vararg `_, - `llvm-else-after-return `_, `readability-else-after-return `_, "Yes" - `llvm-qualified-auto `_, `readability-qualified-auto `_, "Yes" + `llvm-else-after-return `_, `readability-else-after-return `_, + `llvm-qualified-auto `_, `readability-qualified-auto `_, diff --git a/clang-tools-extra/docs/clang-tidy/checks/performance-no-inttoptr.rst b/clang-tools-extra/docs/clang-tidy/checks/performance-no-inttoptr.rst new file mode 100644 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/performance-no-inttoptr.rst @@ -0,0 +1,45 @@ +.. title:: clang-tidy - performance-no-inttoptr + +performance-no-inttoptr +======================= + +Diagnoses every integer to pointer cast. + +While casting an (integral) pointer to an integer is obvious - you just get +the integral value of the pointer, casting an integer to an (integral) pointer +is deceivingly different. While you will get a pointer with that integral value, +if you got that integral value via a pointer-to-integer cast originally, +the new pointer will lack the provenance information from the original pointer. + +So while (integral) pointer to integer casts are effectively no-ops, +and are transparent to the optimizer, integer to (integral) pointer casts +are *NOT* transparent, and may conceal information from optimizer. + +While that may be the intention, it is not always so. For example, +let's take a look at a routine to align the pointer up to the multiple of 16: +The obvious, naive implementation for that is: + +.. code-block:: c++ + + char* src(char* maybe_underbiased_ptr) { + uintptr_t maybe_underbiased_intptr = (uintptr_t)maybe_underbiased_ptr; + uintptr_t aligned_biased_intptr = maybe_underbiased_intptr + 15; + uintptr_t aligned_intptr = aligned_biased_intptr & (~15); + return (char*)aligned_intptr; // warning: avoid integer to pointer casts [performance-no-inttoptr] + } + +The check will rightfully diagnose that cast. + +But when provenance concealment is not the goal of the code, but an accident, +this example can be rewritten as follows, without using integer to pointer cast: + +.. code-block:: c++ + + char* + tgt(char* maybe_underbiased_ptr) { + uintptr_t maybe_underbiased_intptr = (uintptr_t)maybe_underbiased_ptr; + uintptr_t aligned_biased_intptr = maybe_underbiased_intptr + 15; + uintptr_t aligned_intptr = aligned_biased_intptr & (~15); + uintptr_t bias = aligned_intptr - maybe_underbiased_intptr; + return maybe_underbiased_ptr + bias; + } diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.c b/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.c new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.c @@ -0,0 +1,66 @@ +// RUN: %check_clang_tidy %s performance-no-inttoptr %t + +void *t0(char x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t1(signed char x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t2(unsigned char x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +void *t3(short x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t4(unsigned short x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t5(signed short x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +void *t6(int x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t7(unsigned int x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t8(signed int x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +void *t9(long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t10(unsigned long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t11(signed long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +void *t12(long long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t13(unsigned long long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} +void *t14(signed long long x) { + return x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/performance-no-inttoptr.cpp @@ -0,0 +1,22 @@ +// RUN: %check_clang_tidy %s performance-no-inttoptr %t + +// can't implicitly cast int to a pointer. +// can't use static_cast<>() to cast integer to a pointer. +// can't use dynamic_cast<>() to cast integer to a pointer. +// can't use const_cast<>() to cast integer to a pointer. + +void *t0(long long int x) { + return (void *)x; + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +void *t1(int x) { + return reinterpret_cast(x); + // CHECK-MESSAGES: [[@LINE-1]]:10: warning: avoid integer to pointer casts [performance-no-inttoptr] +} + +// Don't diagnose casts from integer literals. +// It's a widely-used technique in embedded/microcontroller/hardware interfacing. +void *t3(long long int x) { + return (void *)0xFEEDFACE; +}