summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2016-12-13 16:38:18 +0000
committerAlexander Kornienko <alexfh@google.com>2016-12-13 16:38:18 +0000
commitebdfb9cf0cea0bd9ae91052b714397569d1c653a (patch)
tree98efe9b14d1949d674d80d0dc366e93ab2d63d76 /clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp
parent469fcd2afd56a0611715408d43af996d05dde25a (diff)
downloadbcm5719-llvm-ebdfb9cf0cea0bd9ae91052b714397569d1c653a.tar.gz
bcm5719-llvm-ebdfb9cf0cea0bd9ae91052b714397569d1c653a.zip
[Clang-tidy] check for malloc, realloc and free calls
Summary: This checker flags the use of C-style memory management functionality and notes about modern alternatives. In an earlier revision it tried to autofix some kind of patterns, but that was a bad idea. Since memory management can be so widespread in a program, manual updating is most likely necessary. Maybe for special cases, there could be later additions to this basic checker. This is the first checker I wrote and I never did something with clang (only compiling programs). So whenever I missed conventions or did plain retarded stuff, feel free to point it out! I am willing to fix them and write a better checker. I hope the patch does work, I never did this either. On a testapply in my repository it did, but I am pretty unconfident in my patching skills :) Reviewers: aaron.ballman, hokein, alexfh, malcolm.parsons Subscribers: cfe-commits, JDevlieghere, nemanjai, Eugene.Zelenko, Prazek, mgorny, modocache Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D26167 Patch by Jonas Toth! llvm-svn: 289546
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp
new file mode 100644
index 00000000000..8f840242a46
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc.cpp
@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-no-malloc %t
+
+using size_t = unsigned long;
+
+void *malloc(size_t size);
+void *calloc(size_t num, size_t size);
+void *realloc(void *ptr, size_t size);
+void free(void *ptr);
+
+void malloced_array() {
+ int *array0 = (int *)malloc(sizeof(int) * 20);
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
+
+ int *zeroed = (int *)calloc(20, sizeof(int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
+
+ // reallocation memory, std::vector shall be used
+ char *realloced = (char *)realloc(array0, 50 * sizeof(int));
+ // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc]
+
+ // freeing memory the bad way
+ free(realloced);
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
+
+ // check if a call to malloc as function argument is found as well
+ free(malloc(20));
+ // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
+ // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
+}
+
+/// newing an array is still not good, but not relevant to this checker
+void newed_array() {
+ int *new_array = new int[10]; // OK(1)
+}
+
+void arbitrary_call() {
+ // we dont want every function to raise the warning even if malloc is in the name
+ malloced_array(); // OK(2)
+
+ // completly unrelated function call to malloc
+ newed_array(); // OK(3)
+}
OpenPOWER on IntegriCloud