brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · b45183d Raw
104 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-sizeof-container %t -- -- -target x86_64-unknown-unknown2 3namespace std {4 5typedef unsigned int size_t;6 7template <typename T>8struct basic_string {9  size_t size() const;10};11 12template <typename T>13basic_string<T> operator+(const basic_string<T> &, const T *);14 15typedef basic_string<char> string;16 17template <typename T>18struct vector {19  size_t size() const;20};21 22// std::bitset<> is not a container. sizeof() is reasonable for it.23template <size_t N>24struct bitset {25  size_t size() const;26};27 28// std::array<> is, well, an array. sizeof() is reasonable for it.29template <typename T, size_t N>30struct array {31  size_t size() const;32};33 34class fake_container1 {35  size_t size() const; // non-public36};37 38struct fake_container2 {39  size_t size(); // non-const40};41 42}43 44using std::size_t;45 46#define ARRAYSIZE(a) \47  ((sizeof(a) / sizeof(*(a))) / static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))48 49#define ARRAYSIZE2(a) \50  (((sizeof(a)) / (sizeof(*(a)))) / static_cast<size_t>(!((sizeof(a)) % (sizeof(*(a))))))51 52struct string {53  std::size_t size() const;54};55 56template<typename T>57void g(T t) {58  (void)sizeof(t);59}60 61void f() {62  string s1;63  std::string s2;64  std::vector<int> v;65 66  int a = 42 + sizeof(s1);67// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: sizeof() doesn't return the size of the container; did you mean .size()? [bugprone-sizeof-container]68  a = 123 * sizeof(s2);69// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: sizeof() doesn't return the size70  a = 45 + sizeof(s2 + "asdf");71// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: sizeof() doesn't return the size72  a = sizeof(v);73// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: sizeof() doesn't return the size74  a = sizeof(std::vector<int>{});75// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: sizeof() doesn't return the size76 77  a = sizeof(a);78  a = sizeof(int);79  a = sizeof(std::string);80  a = sizeof(std::vector<int>);81 82  g(s1);83  g(s2);84  g(v);85 86  std::fake_container1 fake1;87  std::fake_container2 fake2;88  std::bitset<7> std_bitset;89  std::array<int, 3> std_array;90 91  a = sizeof(fake1);92  a = sizeof(fake2);93  a = sizeof(std_bitset);94  a = sizeof(std_array);95 96 97  std::string arr[3];98  a = ARRAYSIZE(arr);99  a = ARRAYSIZE2(arr);100  a = sizeof(arr) / sizeof(arr[0]);101 102  (void)a;103}104