[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
PiotrZSL updated this revision to Diff 548554. PiotrZSL marked an inline comment as done. PiotrZSL added a comment. Rebase, add support for integer returnign functions, fixes in tests. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 Files: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.h clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/bugprone/allocation-bool-conversion.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp === --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp @@ -0,0 +1,133 @@ +// RUN: %check_clang_tidy %s bugprone-allocation-bool-conversion %t -- -config="{CheckOptions: { \ +// RUN: bugprone-allocation-bool-conversion.PointerReturningAllocators: 'ptr_custom',\ +// RUN: bugprone-allocation-bool-conversion.IntegerReturningAllocators: 'int_custom'}}" + +typedef __SIZE_TYPE__ size_t; + +void takeBool(bool); +void* operator new(size_t count); +void *malloc(size_t size); +int open(const char *pathname, int flags); + +template +struct Allocator { + typedef T* pointer; + pointer allocate(size_t n, const void* hint = 0); +}; + +void* ptr_custom(); +int int_custom(); +void* negative(); +int negativeInt(); + +static Allocator allocator; + +void testImplicit() { + takeBool(negative()); + takeBool(negativeInt()); + + takeBool(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(new bool); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(operator new(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(malloc(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(open("file", 0)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'open' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(allocator.allocate(1U)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'allocate' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(ptr_custom()); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'ptr_custom' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(int_custom()); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'int_custom' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + + bool value; + + value = negative(); + value = negativeInt(); + + value = new int; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = new bool; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = operator new(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = malloc(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = open("file", 0); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'open' call is being used as a boolean value, which may lead to unintended behavior or resource leaks
[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
PiotrZSL planned changes to this revision. PiotrZSL added a comment. TODO: Fix CI, Add support for allocators that return 'integer', hardcode most of functions, change config into AdditionalAllocationFunctions Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
PiotrZSL added inline comments. Comment at: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp:25 + "malloc;calloc;realloc;strdup;fopen;fdopen;freopen;" + "opendir;fdopendir;popen;mmap;allocate"))) {} + Eugene.Zelenko wrote: > POSIX `open`, `openat`, `creat`, `pthread_create` should be added too. Those return integers, not pointers. Check is for pointers only, but probably could be extended to integers if we go into this direction. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
PiotrZSL updated this revision to Diff 547587. PiotrZSL marked an inline comment as done. PiotrZSL added a comment. Formating + fix tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 Files: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.h clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/bugprone/allocation-bool-conversion.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp === --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp @@ -0,0 +1,99 @@ +// RUN: %check_clang_tidy %s bugprone-allocation-bool-conversion %t -- -config="{CheckOptions: { bugprone-allocation-bool-conversion.portability-restrict-system-includes.AllocationFunctions: 'malloc;allocate;custom' }}" + +typedef __SIZE_TYPE__ size_t; + +void takeBool(bool); +void* operator new(size_t count); +void *malloc(size_t size); + +template +struct Allocator { + typedef T* pointer; + pointer allocate(size_t n, const void* hint = 0); +}; + +void* custom(); +void* negative(); + +static Allocator allocator; + +void testImplicit() { + takeBool(negative()); + + takeBool(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(new bool); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(operator new(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(malloc(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(allocator.allocate(1U)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'allocate' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + + takeBool(custom()); + + bool value; + + value = negative(); + + value = new int; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = new bool; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = operator new(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = malloc(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = allocator.allocate(1U); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'allocate' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = custom(); +} + +void testExplicit() { + takeBool(static_cast(negative())); + + takeBool(static_cast(new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(static_cast(new bool)); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(static_cast(operator new(10))); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(static_cast(malloc(10))); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the
[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
Eugene.Zelenko added inline comments. Comment at: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp:25 + "malloc;calloc;realloc;strdup;fopen;fdopen;freopen;" + "opendir;fdopendir;popen;mmap;allocate"))) {} + POSIX `open`, `openat`, `creat`, `pthread_create` should be added too. Comment at: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp:60 + "result of the 'new' expression is being used as a boolean value, " + "which " + "may lead to unintended behavior or resource leaks"); Should be merged together. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D157188: [clang-tidy] Add bugprone-allocation-bool-conversion
PiotrZSL updated this revision to Diff 547523. PiotrZSL retitled this revision from "[clang-tidy] Add bugprone-new-bool-conversion" to "[clang-tidy] Add bugprone-allocation-bool-conversion". PiotrZSL edited the summary of this revision. PiotrZSL added a comment. Change check anme, add configuration option. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D157188/new/ https://reviews.llvm.org/D157188 Files: clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.cpp clang-tools-extra/clang-tidy/bugprone/AllocationBoolConversionCheck.h clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/bugprone/allocation-bool-conversion.rst clang-tools-extra/docs/clang-tidy/checks/list.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp Index: clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp === --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/bugprone/allocation-bool-conversion.cpp @@ -0,0 +1,97 @@ +// RUN: %check_clang_tidy %s bugprone-allocation-bool-conversion %t -- -config="{CheckOptions: { bugprone-allocation-bool-conversion.portability-restrict-system-includes.AllocationFunctions: 'malloc;allocate;custom' }}" + +void takeBool(bool); +void* operator new(unsigned long count); +void *malloc(unsigned long size); + +template +struct Allocator { + typedef T* pointer; + pointer allocate(unsigned long n, const void* hint = 0); +}; + +void* custom(); +void* negative(); + +static Allocator allocator; + +void testImplicit() { + takeBool(negative()); + + takeBool(new int); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(new bool); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(operator new(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(malloc(10)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(allocator.allocate(1U)); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: result of the 'allocate' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + + takeBool(custom()); + + bool value; + + value = negative(); + + value = new int; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = new bool; + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = operator new(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = malloc(10); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'malloc' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = allocator.allocate(1U); + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: result of the 'allocate' call is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + value = custom(); +} + +void testExplicit() { + takeBool(static_cast(negative())); + + takeBool(static_cast(new int)); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(static_cast(new bool)); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'new' expression is being used as a boolean value, which may lead to unintended behavior or resource leaks [bugprone-allocation-bool-conversion] + takeBool(static_cast(operator new(10))); + // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: result of the 'operator new' call is being used as a boolean value, which may lead to unintended