philnik created this revision.
philnik added reviewers: aaron.ballman, JonasToth.
Herald added subscribers: carlosgalvezp, xazax.hun, mgorny.
Herald added a project: All.
philnik requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120814

Files:
  clang-tools-extra/clang-tidy/performance/CMakeLists.txt
  clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
  clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.cpp
  clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/performance-use-preincrement.rst
  clang-tools-extra/test/clang-tidy/checkers/performance-use-preincrement.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/performance-use-preincrement.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/performance-use-preincrement.cpp
@@ -0,0 +1,23 @@
+// RUN: %check_clang_tidy %s performance-use-preincrement %t
+
+void positivePostincrement() {
+  int I = 0;
+  I++;
+  // CHECK-MESSAGES: [[@LINE-1]]:4: warning: Preincrement could be used instead of postincrement. [performance-use-preincrement]
+}
+
+void negativePreincrement() {
+  int I = 0;
+  ++I;
+}
+
+void negativeUsedPostincrement() {
+  int I = 0;
+  int J = I++;
+  ++J;
+  J = I++;
+}
+
+int negativeReturn(int &I) {
+  return I++;
+}
Index: clang-tools-extra/docs/clang-tidy/checks/performance-use-preincrement.rst
===================================================================
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/performance-use-preincrement.rst
@@ -0,0 +1,7 @@
+.. title:: clang-tidy - performance-use-preincrement
+
+performance-use-preincrement
+============================
+
+Checks that preincrement is used when there is no need for postincrement.
+This can lead to improved performance with iterators that are not trivially copyable.
Index: clang-tools-extra/docs/clang-tidy/checks/list.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/list.rst
+++ clang-tools-extra/docs/clang-tidy/checks/list.rst
@@ -84,7 +84,7 @@
    `bugprone-posix-return <bugprone-posix-return.html>`_, "Yes"
    `bugprone-redundant-branch-condition <bugprone-redundant-branch-condition.html>`_, "Yes"
    `bugprone-reserved-identifier <bugprone-reserved-identifier.html>`_, "Yes"
-   `bugprone-shared-ptr-array-mismatch <bugprone-shared-ptr-array-mismatch.html>`_, "Yes"
+   `bugprone-shared-ptr-array-mismatch <bugprone-shared-ptr-array-mismatch.html>`_,
    `bugprone-signal-handler <bugprone-signal-handler.html>`_,
    `bugprone-signed-char-misuse <bugprone-signed-char-misuse.html>`_,
    `bugprone-sizeof-container <bugprone-sizeof-container.html>`_,
@@ -286,6 +286,7 @@
    `performance-type-promotion-in-math-fn <performance-type-promotion-in-math-fn.html>`_, "Yes"
    `performance-unnecessary-copy-initialization <performance-unnecessary-copy-initialization.html>`_, "Yes"
    `performance-unnecessary-value-param <performance-unnecessary-value-param.html>`_, "Yes"
+   `performance-use-preincrement <performance-use-preincrement.html>`_, "Yes"
    `portability-restrict-system-includes <portability-restrict-system-includes.html>`_, "Yes"
    `portability-simd-intrinsics <portability-simd-intrinsics.html>`_,
    `readability-avoid-const-params-in-decls <readability-avoid-const-params-in-decls.html>`_, "Yes"
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -103,6 +103,11 @@
 
   Finds initializations of C++ shared pointers to non-array type that are initialized with an array.
 
+- New :doc:`performance-use-preincrement
+  <clang-tidy/checks/performance-use-preincrement>` check.
+
+  FIXME: add release notes.
+
 New check aliases
 ^^^^^^^^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.h
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.h
@@ -0,0 +1,34 @@
+//===--- UsePreincrementCheck.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_USEPREINCREMENTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_PERFORMANCE_USEPREINCREMENTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang {
+namespace tidy {
+namespace performance {
+
+/// Checks that preincrement is used when there is no need for postincrement
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/performance-use-preincrement.html
+class UsePreincrementCheck : public ClangTidyCheck {
+public:
+  UsePreincrementCheck(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_USEPREINCREMENTCHECK_H
Index: clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/clang-tidy/performance/UsePreincrementCheck.cpp
@@ -0,0 +1,46 @@
+//===--- UsePreincrementCheck.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 "UsePreincrementCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace performance {
+
+void UsePreincrementCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      unaryOperator(hasOperatorName("++"), hasParent(compoundStmt()))
+          .bind("unaryOperator"),
+      this);
+}
+
+void UsePreincrementCheck::check(const MatchFinder::MatchResult &Result) {
+  const auto *Operator = Result.Nodes.getNodeAs<UnaryOperator>("unaryOperator");
+  if (Operator->isPrefix())
+    return;
+
+  auto PostIncRange =
+      CharSourceRange::getTokenRange(Operator->getSourceRange());
+  auto ValRange =
+      CharSourceRange::getTokenRange(Operator->getSubExpr()->getSourceRange());
+
+  auto Diag =
+      diag(Operator->getOperatorLoc(),
+           "Preincrement could be used instead of postincrement.")
+      << FixItHint::CreateInsertion(ValRange.getBegin().getLocWithOffset(-2),
+                                    "++")
+      << FixItHint::CreateRemoval(PostIncRange.getEnd().getLocWithOffset(1));
+}
+
+} // namespace performance
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
===================================================================
--- clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
+++ clang-tools-extra/clang-tidy/performance/PerformanceTidyModule.cpp
@@ -24,6 +24,7 @@
 #include "TypePromotionInMathFnCheck.h"
 #include "UnnecessaryCopyInitialization.h"
 #include "UnnecessaryValueParamCheck.h"
+#include "UsePreincrementCheck.h"
 
 namespace clang {
 namespace tidy {
@@ -61,6 +62,8 @@
         "performance-unnecessary-copy-initialization");
     CheckFactories.registerCheck<UnnecessaryValueParamCheck>(
         "performance-unnecessary-value-param");
+    CheckFactories.registerCheck<UsePreincrementCheck>(
+        "performance-use-preincrement");
   }
 };
 
Index: clang-tools-extra/clang-tidy/performance/CMakeLists.txt
===================================================================
--- clang-tools-extra/clang-tidy/performance/CMakeLists.txt
+++ clang-tools-extra/clang-tidy/performance/CMakeLists.txt
@@ -20,6 +20,7 @@
   TypePromotionInMathFnCheck.cpp
   UnnecessaryCopyInitialization.cpp
   UnnecessaryValueParamCheck.cpp
+  UsePreincrementCheck.cpp
 
   LINK_LIBS
   clangTidy
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D120814: [clang-ti... Nikolas Klauser via Phabricator via cfe-commits

Reply via email to