xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/no-malloc-custom.cpp (revision e8a3ddafe063c970df9c23e803812369abde4c82)
189a1d03eSRichard // RUN: %check_clang_tidy %s cppcoreguidelines-no-malloc %t \
289a1d03eSRichard // RUN: -config='{CheckOptions: \
3*e8a3ddafSNathan James // RUN:  {cppcoreguidelines-no-malloc.Allocations: "::malloc;::align_malloc;::calloc",\
4*e8a3ddafSNathan James // RUN:   cppcoreguidelines-no-malloc.Reallocations: "::realloc;::align_realloc",\
5*e8a3ddafSNathan James // RUN:   cppcoreguidelines-no-malloc.Deallocations: "::free;::align_free"}}' \
689a1d03eSRichard // RUN: --
789a1d03eSRichard 
889a1d03eSRichard using size_t = __SIZE_TYPE__;
989a1d03eSRichard 
1089a1d03eSRichard void *malloc(size_t size);
1189a1d03eSRichard void *align_malloc(size_t size, unsigned short alignment);
1289a1d03eSRichard void *calloc(size_t num, size_t size);
1389a1d03eSRichard void *realloc(void *ptr, size_t size);
1489a1d03eSRichard void *align_realloc(void *ptr, size_t size, unsigned short alignment);
1589a1d03eSRichard void free(void *ptr);
1689a1d03eSRichard void *align_free(void *ptr);
1789a1d03eSRichard 
malloced_array()1889a1d03eSRichard void malloced_array() {
1989a1d03eSRichard   int *array0 = (int *)malloc(sizeof(int) * 20);
2089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
2189a1d03eSRichard 
2289a1d03eSRichard   int *zeroed = (int *)calloc(20, sizeof(int));
2389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
2489a1d03eSRichard 
2589a1d03eSRichard   int *aligned = (int *)align_malloc(20 * sizeof(int), 16);
2689a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
2789a1d03eSRichard 
2889a1d03eSRichard   // reallocation memory, std::vector shall be used
2989a1d03eSRichard   char *realloced = (char *)realloc(array0, 50 * sizeof(int));
3089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc]
3189a1d03eSRichard 
3289a1d03eSRichard   char *align_realloced = (char *)align_realloc(aligned, 50 * sizeof(int), 16);
3389a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not manage memory manually; consider std::vector or std::string [cppcoreguidelines-no-malloc]
3489a1d03eSRichard 
3589a1d03eSRichard   // freeing memory the bad way
3689a1d03eSRichard   free(realloced);
3789a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
3889a1d03eSRichard 
3989a1d03eSRichard   align_free(align_realloced);
4089a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
4189a1d03eSRichard 
4289a1d03eSRichard   // check if a call to malloc as function argument is found as well
4389a1d03eSRichard   free(malloc(20));
4489a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not manage memory manually; use RAII [cppcoreguidelines-no-malloc]
4589a1d03eSRichard   // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc]
4689a1d03eSRichard }
4789a1d03eSRichard 
4889a1d03eSRichard /// newing an array is still not good, but not relevant to this checker
newed_array()4989a1d03eSRichard void newed_array() {
5089a1d03eSRichard   int *new_array = new int[10]; // OK(1)
5189a1d03eSRichard }
5289a1d03eSRichard 
arbitrary_call()5389a1d03eSRichard void arbitrary_call() {
5489a1d03eSRichard   // we dont want every function to raise the warning even if malloc is in the name
5589a1d03eSRichard   malloced_array(); // OK(2)
5689a1d03eSRichard 
5789a1d03eSRichard   // completely unrelated function call to malloc
5889a1d03eSRichard   newed_array(); // OK(3)
5989a1d03eSRichard }
60