summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp
diff options
context:
space:
mode:
authorJonas Toth <jonas.toth@gmail.com>2017-10-18 16:14:15 +0000
committerJonas Toth <jonas.toth@gmail.com>2017-10-18 16:14:15 +0000
commitc9aea86e6af2bc1f7414f69f31428cf49273bf62 (patch)
treeeb9d59a48d895e2799a488c3b416a79a7d66bc89 /clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp
parent13ce95b77f54aa4a7ff01a46a5faaa85001755a6 (diff)
downloadbcm5719-llvm-c9aea86e6af2bc1f7414f69f31428cf49273bf62.tar.gz
bcm5719-llvm-c9aea86e6af2bc1f7414f69f31428cf49273bf62.zip
[clang-tidy] introduce legacy resource functions to 'cppcoreguidelines-owning-memory'
Summary: This patch introduces support for legacy C-style resource functions that must obey the 'owner<>' semantics. - added legacy creators like malloc,fopen,... - added legacy consumers like free,fclose,... This helps codes that mostly benefit from owner: Legacy, C-Style code that isn't feasable to port directly to RAII but needs a step in between to identify actual resource management and just using the resources. Reviewers: aaron.ballman, alexfh, hokein Reviewed By: aaron.ballman Subscribers: nemanjai, JDevlieghere, xazax.hun, kbarton Differential Revision: https://reviews.llvm.org/D38396 llvm-svn: 316092
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp
new file mode 100644
index 00000000000..929b33d9987
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-owning-memory-containers.cpp
@@ -0,0 +1,61 @@
+// RUN: %check_clang_tidy %s cppcoreguidelines-owning-memory %t
+
+namespace gsl {
+template <typename T>
+using owner = T;
+}
+
+namespace std {
+
+// Not actually a vector, but more a dynamic, fixed size array. Just to demonstrate
+// functionality or the lack of the same.
+template <typename T>
+class vector {
+public:
+ vector(unsigned long size, T val) : data{new T[size]}, size{size} {
+ for (unsigned long i = 0ul; i < size; ++i) {
+ data[i] = val;
+ }
+ }
+
+ T *begin() { return data; }
+ T *end() { return &data[size]; }
+ T &operator[](unsigned long index) { return data[index]; }
+
+private:
+ T *data;
+ unsigned long size;
+};
+
+} // namespace std
+
+// All of the following codesnippets should be valid with appropriate 'owner<>' anaylsis,
+// but currently the type information of 'gsl::owner<>' gets lost in typededuction.
+int main() {
+ std::vector<gsl::owner<int *>> OwnerStdVector(100, nullptr);
+
+ // Rangebased looping in resource vector.
+ for (auto *Element : OwnerStdVector) {
+ Element = new int(42);
+ // CHECK-MESSAGES: [[@LINE-1]]:5: warning: assigning newly created 'gsl::owner<>' to non-owner 'int *'
+ }
+ for (auto *Element : OwnerStdVector) {
+ delete Element;
+ // CHECK-MESSAGES: [[@LINE-1]]:5: warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead
+ // CHECK-MESSAGES: [[@LINE-3]]:8: note: variable declared here
+ }
+
+ // Indexbased looping in resource vector.
+ for (int i = 0; i < 100; ++i) {
+ OwnerStdVector[i] = new int(42);
+ // CHECK-MESSAGES: [[@LINE-1]]:5: warning: assigning newly created 'gsl::owner<>' to non-owner 'int *'
+ }
+ for (int i = 0; i < 100; ++i) {
+ delete OwnerStdVector[i];
+ // CHECK-MESSAGES: [[@LINE-1]]:5: warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead
+ // A note gets emitted here pointing to the return value of the operator[] from the
+ // vector implementation. Maybe this is considered misleading.
+ }
+
+ return 0;
+}
OpenPOWER on IntegriCloud