[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 closed 
https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 updated 
https://github.com/llvm/llvm-project/pull/95667

>From 5a4a4aac26a2a4078f07977b5101d3a1e22a3e0c Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 15 Jun 2024 16:24:55 +
Subject: [PATCH 1/2] [clang-tidy] fix false negatives for
 performance-inefficient-vector-operation

Fixes: #95596
Check will warn if the loop var type is not same as var init expr type
---
 .../InefficientVectorOperationCheck.cpp   |  6 ++--
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 +++
 .../inefficient-vector-operation.cpp  | 35 +++
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 20aea5a79fe9a..dc6e0cf9c7d12 100644
--- 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -105,9 +105,9 @@ void InefficientVectorOperationCheck::addMatcher(
   onImplicitObjectArgument(declRefExpr(to(TargetVarDecl
   .bind(AppendCallName);
   const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
-  const auto LoopVarInit =
-  declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0
- .bind(LoopInitVarName)));
+  const auto LoopVarInit = declStmt(hasSingleDecl(
+  varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)
+  .bind(LoopInitVarName)));
   const auto RefersToLoopVar = ignoringParenImpCasts(
   declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName);
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 7092d0b6fdb02..3bdd735f7dcf7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -387,6 +387,11 @@ Changes in existing checks
 - Improved :doc:`modernize-use-using `
   check by adding support for detection of typedefs declared on function level.
 
+- Improved :doc:`performance-inefficient-vector-operation
+  ` fixing false
+  negatives caused by different variable definition type and variable initial
+  value type in loop initialization expression.
+
 - Improved :doc:`performance-move-const-arg
   ` check by ignoring
   ``std::move()`` calls when their target is used as an rvalue.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
index c28592f4d6368..50424920d72b7 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer ) {
 B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector& t) {
+  {
+std::vector gh95596_0;
+// CHECK-FIXES: gh95596_0.reserve(10);
+for (unsigned i = 0; i < 10; ++i)
+  gh95596_0.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_1;
+// CHECK-FIXES: gh95596_1.reserve(10);
+for (int i = 0U; i < 10; ++i)
+  gh95596_1.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_2;
+// CHECK-FIXES: gh95596_2.reserve(10);
+for (unsigned i = 0U; i < 10; ++i)
+  gh95596_2.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_3;
+// CHECK-FIXES: gh95596_3.reserve(10U);
+for (int i = 0; i < 10U; ++i)
+  gh95596_3.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+}
+
+} // namespace gh95596
\ No newline at end of file

>From b21c8bb0d275b5e6708b8e5f04431232426b5d26 Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sun, 16 Jun 2024 06:14:18 +0800
Subject: [PATCH 2/2] Update
 
clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp

---
 .../checkers/performance/inefficient-vector-operation.cpp   | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
index 50424920d72b7..35091eb77c4c5 100644
--- 

[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Congcong Cai via cfe-commits


@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer ) {
 B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector& t) {
+  {
+std::vector gh95596_0;
+// CHECK-FIXES: gh95596_0.reserve(10);
+for (unsigned i = 0; i < 10; ++i)
+  gh95596_0.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_1;
+// CHECK-FIXES: gh95596_1.reserve(10);
+for (int i = 0U; i < 10; ++i)
+  gh95596_1.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_2;
+// CHECK-FIXES: gh95596_2.reserve(10);
+for (unsigned i = 0U; i < 10; ++i)
+  gh95596_2.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_3;
+// CHECK-FIXES: gh95596_3.reserve(10U);
+for (int i = 0; i < 10U; ++i)
+  gh95596_3.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+}
+
+} // namespace gh95596

HerrCai0907 wrote:

```suggestion
} // namespace gh95596

```

https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Piotr Zegar via cfe-commits

https://github.com/PiotrZSL approved this pull request.


https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Julian Schmidt via cfe-commits

https://github.com/5chmidti edited 
https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Julian Schmidt via cfe-commits

https://github.com/5chmidti approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Julian Schmidt via cfe-commits


@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer ) {
 B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector& t) {
+  {
+std::vector gh95596_0;
+// CHECK-FIXES: gh95596_0.reserve(10);
+for (unsigned i = 0; i < 10; ++i)
+  gh95596_0.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_1;
+// CHECK-FIXES: gh95596_1.reserve(10);
+for (int i = 0U; i < 10; ++i)
+  gh95596_1.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_2;
+// CHECK-FIXES: gh95596_2.reserve(10);
+for (unsigned i = 0U; i < 10; ++i)
+  gh95596_2.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_3;
+// CHECK-FIXES: gh95596_3.reserve(10U);
+for (int i = 0; i < 10U; ++i)
+  gh95596_3.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+}
+
+} // namespace gh95596

5chmidti wrote:

missing newline

https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Julian Schmidt via cfe-commits

https://github.com/5chmidti edited 
https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 edited 
https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)


Changes

Fixes: #95596
Check will warn if the loop var type is not same as var init expr type


---
Full diff: https://github.com/llvm/llvm-project/pull/95667.diff


3 Files Affected:

- (modified) 
clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp 
(+3-3) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5) 
- (modified) 
clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 (+35) 


``diff
diff --git 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 20aea5a79fe9a..dc6e0cf9c7d12 100644
--- 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -105,9 +105,9 @@ void InefficientVectorOperationCheck::addMatcher(
   onImplicitObjectArgument(declRefExpr(to(TargetVarDecl
   .bind(AppendCallName);
   const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
-  const auto LoopVarInit =
-  declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0
- .bind(LoopInitVarName)));
+  const auto LoopVarInit = declStmt(hasSingleDecl(
+  varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)
+  .bind(LoopInitVarName)));
   const auto RefersToLoopVar = ignoringParenImpCasts(
   declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName);
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 7092d0b6fdb02..3bdd735f7dcf7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -387,6 +387,11 @@ Changes in existing checks
 - Improved :doc:`modernize-use-using `
   check by adding support for detection of typedefs declared on function level.
 
+- Improved :doc:`performance-inefficient-vector-operation
+  ` fixing false
+  negatives caused by different variable definition type and variable initial
+  value type in loop initialization expression.
+
 - Improved :doc:`performance-move-const-arg
   ` check by ignoring
   ``std::move()`` calls when their target is used as an rvalue.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
index c28592f4d6368..50424920d72b7 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer ) {
 B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector& t) {
+  {
+std::vector gh95596_0;
+// CHECK-FIXES: gh95596_0.reserve(10);
+for (unsigned i = 0; i < 10; ++i)
+  gh95596_0.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_1;
+// CHECK-FIXES: gh95596_1.reserve(10);
+for (int i = 0U; i < 10; ++i)
+  gh95596_1.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_2;
+// CHECK-FIXES: gh95596_2.reserve(10);
+for (unsigned i = 0U; i < 10; ++i)
+  gh95596_2.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_3;
+// CHECK-FIXES: gh95596_3.reserve(10U);
+for (int i = 0; i < 10U; ++i)
+  gh95596_3.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+}
+
+} // namespace gh95596
\ No newline at end of file

``




https://github.com/llvm/llvm-project/pull/95667
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] fix false negatives for performance-inefficient-vector-operation (PR #95667)

2024-06-15 Thread Congcong Cai via cfe-commits

https://github.com/HerrCai0907 created 
https://github.com/llvm/llvm-project/pull/95667

Fixes: #95596
Check will warn if the loop var type is not same as var init expr type


>From 5a4a4aac26a2a4078f07977b5101d3a1e22a3e0c Mon Sep 17 00:00:00 2001
From: Congcong Cai 
Date: Sat, 15 Jun 2024 16:24:55 +
Subject: [PATCH] [clang-tidy] fix false negatives for
 performance-inefficient-vector-operation

Fixes: #95596
Check will warn if the loop var type is not same as var init expr type
---
 .../InefficientVectorOperationCheck.cpp   |  6 ++--
 clang-tools-extra/docs/ReleaseNotes.rst   |  5 +++
 .../inefficient-vector-operation.cpp  | 35 +++
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
index 20aea5a79fe9a..dc6e0cf9c7d12 100644
--- 
a/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/performance/InefficientVectorOperationCheck.cpp
@@ -105,9 +105,9 @@ void InefficientVectorOperationCheck::addMatcher(
   onImplicitObjectArgument(declRefExpr(to(TargetVarDecl
   .bind(AppendCallName);
   const auto AppendCall = expr(ignoringImplicit(AppendCallExpr));
-  const auto LoopVarInit =
-  declStmt(hasSingleDecl(varDecl(hasInitializer(integerLiteral(equals(0
- .bind(LoopInitVarName)));
+  const auto LoopVarInit = declStmt(hasSingleDecl(
+  varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)
+  .bind(LoopInitVarName)));
   const auto RefersToLoopVar = ignoringParenImpCasts(
   declRefExpr(to(varDecl(equalsBoundNode(LoopInitVarName);
 
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index 7092d0b6fdb02..3bdd735f7dcf7 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -387,6 +387,11 @@ Changes in existing checks
 - Improved :doc:`modernize-use-using `
   check by adding support for detection of typedefs declared on function level.
 
+- Improved :doc:`performance-inefficient-vector-operation
+  ` fixing false
+  negatives caused by different variable definition type and variable initial
+  value type in loop initialization expression.
+
 - Improved :doc:`performance-move-const-arg
   ` check by ignoring
   ``std::move()`` calls when their target is used as an rvalue.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
index c28592f4d6368..50424920d72b7 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/performance/inefficient-vector-operation.cpp
@@ -387,3 +387,38 @@ void foo(const StructWithFieldContainer ) {
 B.push_back(Number);
   }
 }
+
+namespace gh95596 {
+
+void f(std::vector& t) {
+  {
+std::vector gh95596_0;
+// CHECK-FIXES: gh95596_0.reserve(10);
+for (unsigned i = 0; i < 10; ++i)
+  gh95596_0.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_1;
+// CHECK-FIXES: gh95596_1.reserve(10);
+for (int i = 0U; i < 10; ++i)
+  gh95596_1.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_2;
+// CHECK-FIXES: gh95596_2.reserve(10);
+for (unsigned i = 0U; i < 10; ++i)
+  gh95596_2.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+  {
+std::vector gh95596_3;
+// CHECK-FIXES: gh95596_3.reserve(10U);
+for (int i = 0; i < 10U; ++i)
+  gh95596_3.push_back(i);
+  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside 
a loop; consider pre-allocating the container capacity before the loop
+  }
+}
+
+} // namespace gh95596
\ No newline at end of file

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits