diff options
author | Matthias Gehre <M.Gehre@gmx.de> | 2015-10-26 21:56:02 +0000 |
---|---|---|
committer | Matthias Gehre <M.Gehre@gmx.de> | 2015-10-26 21:56:02 +0000 |
commit | f33319699dbf6f4a7b2f0e816b01d719706ea0b1 (patch) | |
tree | 8611000af3126e050cb9cf76d7fbcbb6bfb6cdb4 /clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp | |
parent | e9b500f722e1497ad83baacfe21057993ff44ed3 (diff) | |
download | bcm5719-llvm-f33319699dbf6f4a7b2f0e816b01d719706ea0b1.tar.gz bcm5719-llvm-f33319699dbf6f4a7b2f0e816b01d719706ea0b1.zip |
[clang-tidy] Add new check cppcoreguidelines-pro-bounds-array-to-pointer-decay
Summary:
This check flags all array to pointer decays.
Pointers should not be used as arrays. array_view is a bounds-checked,
safe alternative to using pointers to access arrays.
This rule is part of the "Bounds safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-bounds3-no-array-to-pointer-decay
Reviewers: alexfh, sbenza, bkramer, aaron.ballman
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D13640
llvm-svn: 251358
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp')
-rw-r--r-- | clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp new file mode 100644 index 00000000000..13fd8a22142 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -0,0 +1,41 @@ +// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t +#include <stddef.h> + +namespace gsl { +template <class T> +class array_view { +public: + template <class U, size_t N> + array_view(U (&arr)[N]); +}; +} + +void pointerfun(int *p); +void arrayfun(int p[]); +void arrayviewfun(gsl::array_view<int> &p); +size_t s(); + +void f() { + int a[5]; + pointerfun(a); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + pointerfun((int *)a); // OK, explicit cast + arrayfun(a); + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not implicitly decay an array into a pointer + + pointerfun(a + s() - 10); // Convert to &a[g() - 10]; + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: do not implicitly decay an array into a pointer + + gsl::array_view<int> av(a); + arrayviewfun(av); // OK + + int i = a[0]; // OK + pointerfun(&a[0]); // OK + + for (auto &e : a) // OK, iteration internally decays array to pointer + e = 1; +} + +const char *g() { + return "clang"; // OK, decay string literal to pointer +} |