summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp
diff options
context:
space:
mode:
authorHaojian Wu <hokein@google.com>2019-09-16 08:54:10 +0000
committerHaojian Wu <hokein@google.com>2019-09-16 08:54:10 +0000
commitad7a7cea89717daf6b56f35e4bc0b95afe0498a2 (patch)
tree0a1c2b56486350046143365ccb0f5672639793d7 /clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp
parentb026b3e53d67c258f8b750e3fc44fa1609185dee (diff)
downloadbcm5719-llvm-ad7a7cea89717daf6b56f35e4bc0b95afe0498a2.tar.gz
bcm5719-llvm-ad7a7cea89717daf6b56f35e4bc0b95afe0498a2.zip
[clang-tidy] performance-inefficient-vector-operation: Support proto repeated field
Summary: Finds calls that add element to protobuf repeated field in a loop without calling Reserve() before the loop. Calling Reserve() first can avoid unnecessary memory reallocations. A new option EnableProto is added to guard this feature. Patch by Cong Liu! Reviewers: gribozavr, alexfh, hokein, aaron.ballman Reviewed By: hokein Subscribers: lebedev.ri, xazax.hun, Eugene.Zelenko, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D67135 llvm-svn: 371963
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp88
1 files changed, 86 insertions, 2 deletions
diff --git a/clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp b/clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp
index d5e38eb71f8..ffac24c2b9a 100644
--- a/clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp
+++ b/clang-tools-extra/test/clang-tidy/performance-inefficient-vector-operation.cpp
@@ -1,4 +1,7 @@
-// RUN: %check_clang_tidy %s performance-inefficient-vector-operation %t -- -format-style=llvm
+// RUN: %check_clang_tidy %s performance-inefficient-vector-operation %t -- \
+// RUN: -format-style=llvm \
+// RUN: -config='{CheckOptions: \
+// RUN: [{key: performance-inefficient-vector-operation.EnableProto, value: 1}]}'
namespace std {
@@ -62,13 +65,35 @@ class Bar {
int Op(int);
+namespace proto2 {
+class MessageLite {};
+class Message : public MessageLite {};
+} // namespace proto2
+
+class FooProto : public proto2::Message {
+ public:
+ int *add_x(); // repeated int x;
+ void add_x(int x);
+ void mutable_x();
+ void mutable_y();
+ int add_z() const; // optional int add_z;
+};
+
+class BarProto : public proto2::Message {
+ public:
+ int *add_x();
+ void add_x(int x);
+ void mutable_x();
+ void mutable_y();
+};
+
void f(std::vector<int>& t) {
{
std::vector<int> v0;
// CHECK-FIXES: v0.reserve(10);
for (int i = 0; i < 10; ++i)
v0.push_back(i);
- // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the vector capacity before the loop
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop
}
{
std::vector<int> v1;
@@ -162,6 +187,15 @@ void f(std::vector<int>& t) {
}
}
+ {
+ FooProto foo;
+ // CHECK-FIXES: foo.mutable_x()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x(i);
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: 'add_x' is called inside a loop; consider pre-allocating the container capacity before the loop
+ }
+ }
+
// ---- Non-fixed Cases ----
{
std::vector<int> z0;
@@ -274,4 +308,54 @@ void f(std::vector<int>& t) {
z12.push_back(e);
}
}
+
+ {
+ FooProto foo;
+ foo.mutable_x();
+ // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x(i);
+ }
+ }
+ {
+ FooProto foo;
+ // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x(i);
+ foo.add_x(i);
+ }
+ }
+ {
+ FooProto foo;
+ // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ foo.add_x(-1);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x(i);
+ }
+ }
+ {
+ FooProto foo;
+ BarProto bar;
+ bar.mutable_x();
+ // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x();
+ bar.add_x();
+ }
+ }
+ {
+ FooProto foo;
+ foo.mutable_y();
+ // CHECK-FIXES-NOT: foo.mutable_x()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_x(i);
+ }
+ }
+ {
+ FooProto foo;
+ // CHECK-FIXES-NOT: foo.mutable_z()->Reserve(5);
+ for (int i = 0; i < 5; i++) {
+ foo.add_z();
+ }
+ }
}
OpenPOWER on IntegriCloud