summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2017-03-02 08:28:55 +0000
committerAlexander Kornienko <alexfh@google.com>2017-03-02 08:28:55 +0000
commitd993e76a18b1ee309153c6c22abad0b0f03977f1 (patch)
tree5156bd6a3ae1fb6bf0c198e01d2929d9dca7b16d /clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp
parentb8ebcb519758a9acfb934965eaabf3091a2d39ec (diff)
downloadbcm5719-llvm-d993e76a18b1ee309153c6c22abad0b0f03977f1.tar.gz
bcm5719-llvm-d993e76a18b1ee309153c6c22abad0b0f03977f1.zip
[clang-tidy] Function names configurable for cppcoreguidelines-nomalloc - checker
Summary: Hello everybody, this is an incremental patch for the NoMalloc-Checker I wrote. It allows to configure the memory-management functions, that are checked, This might be helpful for a code base with custom functions in use, or non-standard functionality, like posix_memalign. Reviewers: aaron.ballman, hokein, alexfh Reviewed By: aaron.ballman, alexfh Subscribers: sbenza, nemanjai, JDevlieghere Tags: #clang-tools-extra Patch by Jonas Toth! Differential Revision: https://reviews.llvm.org/D28239 llvm-svn: 296734
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp
new file mode 100644
index 00000000000..89142fbd513
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-no-malloc-custom.cpp
@@ -0,0 +1,59 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-no-malloc %t \
+// RUN: -config='{CheckOptions: \
+// RUN: [{key: cppcoreguidelines-no-malloc.Allocations, value: "::malloc;::align_malloc;::calloc"},\
+// RUN: {key: cppcoreguidelines-no-malloc.Reallocations, value: "::realloc;::align_realloc"},\
+// RUN: {key: cppcoreguidelines-no-malloc.Deallocations, value: "::free;::align_free"}]}' \
+// RUN: --
+
+using size_t = __SIZE_TYPE__;
+
+void *malloc(size_t size);
+void *align_malloc(size_t size, unsigned short aligmnent);
+void *calloc(size_t num, size_t size);
+void *realloc(void *ptr, size_t size);
+void *align_realloc(void *ptr, size_t size, unsigned short alignment);
+void free(void *ptr);
+void *align_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]
+
+ int *aligned = (int *)align_malloc(20 * sizeof(int), 16);
+ // CHECK-MESSAGES: :[[@LINE-1]]:25: 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]
+
+ char *align_realloced = (char *)align_realloc(aligned, 50 * sizeof(int), 16);
+ // CHECK-MESSAGES: :[[@LINE-1]]:35: 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]
+
+ align_free(align_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