brintos

brintos / llvm-project-archived public Read only

0
0
Text · 13.0 KiB · 95e04d5 Raw
287 lines · cpp
1// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t2 3int a[] = {1, 2};4// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead5 6int b[1];7// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead8 9void foo() {10  int c[b[0]];11  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C VLA arrays, use 'std::vector' instead12 13  using d = decltype(c);14  d e;15  // Semi-FIXME: we do not diagnose these last two lines separately,16  // because we point at typeLoc.getBeginLoc(), which is the decl before that17  // (int c[b[0]];), which is already diagnosed.18}19 20template <typename T, int Size>21class array {22  T d[Size];23  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead24 25  int e[1];26  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead27};28 29array<int[4], 2> d;30// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead31 32using k = int[4];33// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not declare C-style arrays, use 'std::array' instead34 35k ak;36// no diagnostic expected here since no concrete C-style array type is written here37 38array<k, 2> dk;39// no diagnostic expected here since no concrete C-style array type is written here40 41array<decltype(ak), 3> ek;42// no diagnostic expected here since no concrete C-style array type is written here43 44template <typename T>45class unique_ptr {46  T *d;47 48  int e[1];49  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead50};51 52unique_ptr<int[]> d2;53// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead54 55using k2 = int[];56// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead57 58unique_ptr<k2> dk2;59 60// Some header61extern "C" {62 63int f[] = {1, 2};64 65int j[1];66 67inline void bar() {68  {69    int j[j[0]];70  }71}72 73extern "C++" {74int f3[] = {1, 2};75// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead76 77int j3[1];78// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead79 80struct Foo {81  int f3[3] = {1, 2};82  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead83 84  int j3[1];85  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead86};87}88 89struct Bar {90 91  int f[3] = {1, 2};92 93  int j[1];94};95}96 97const char name[] = "Some string";98// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]99 100void takeCharArray(const char name[]);101// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays]102 103namespace std {104  template<class T, class U>105  struct is_same { constexpr static bool value{false}; };106 107  template<class T>108  struct is_same<T, T> { constexpr static bool value{true}; };109 110  template<class T, class U>111  constexpr bool is_same_v = is_same<T, U>::value;112 113  template<class T> struct remove_const { typedef T type; };114  template<class T> struct remove_const<const T> { typedef T type; };115 116  template<class T>117  using remove_const_t = typename remove_const<T>::type;118 119  template<bool B, class T = void> struct enable_if {};120  template<class T> struct enable_if<true, T> { typedef T type; };121 122  template< bool B, class T = void >123  using enable_if_t = typename enable_if<B, T>::type;124}125 126// within below template decl, no array type findings are expected within the template parameter declarations since not a single C-style array type got written explicitly127template <typename T,128          bool = std::is_same_v<T, int>,129          bool = std::is_same<T, int>::value,130          bool = std::is_same_v<std::remove_const_t<T>, int>,131          bool = std::is_same<std::remove_const_t<T>, int>::value,132          bool = std::is_same_v<typename std::remove_const<T>::type, int>,133          bool = std::is_same<typename std::remove_const<T>::type, int>::value,134          std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool> = true,135          typename std::enable_if<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool>::type = true,136          typename = std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>)>,137          typename = typename std::remove_const<T>::type,138          typename = std::remove_const_t<T>>139class MyClassTemplate {140 public:141  // here, however, plenty of array type findings are expected for below template parameter declarations since C-style array types are written explicitly142  template <typename U = T,143            bool = std::is_same_v<U, int[]>,144            // CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]145            bool = std::is_same<U, int[10]>::value,146            // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]147            std::enable_if_t<not(std::is_same_v<std::remove_const_t<U>, int[]>) && not(std::is_same_v<typename std::remove_const<U>::type, char[10]>), bool> = true,148            // CHECK-MESSAGES: :[[@LINE-1]]:73: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]149            // CHECK-MESSAGES: :[[@LINE-2]]:140: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]150            typename = typename std::remove_const<int[10]>::type,151            // CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]152            typename = std::remove_const_t<int[]>>153            // CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]154    class MyInnerClassTemplate {155     public:156      MyInnerClassTemplate(const U&) {}157     private:158      U field[3];159      // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]160    };161 162    MyClassTemplate(const T&) {}163 164 private:165    T field[7];166    // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]167};168 169// an explicit instantiation170template171class MyClassTemplate<int[2]>;172// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]173 174using MyArrayType = int[3];175// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]176 177// another explicit instantiation178template179class MyClassTemplate<MyArrayType>;180// no diagnostic is expected here since no C-style array type got written here181 182// within below template decl, no array type findings are expected within the template parameter declarations since not a single C-style array type got written explicitly183template <typename T,184          bool = std::is_same_v<T, int>,185          bool = std::is_same<T, int>::value,186          bool = std::is_same_v<std::remove_const_t<T>, int>,187          bool = std::is_same<std::remove_const_t<T>, int>::value,188          bool = std::is_same_v<typename std::remove_const<T>::type, int>,189          bool = std::is_same<typename std::remove_const<T>::type, int>::value,190          std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool> = true,191          typename std::enable_if<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>), bool>::type = true,192          typename = std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int>) && not(std::is_same_v<typename std::remove_const<T>::type, char>)>,193          typename = typename std::remove_const<T>::type,194          typename = std::remove_const_t<T>>195void func(const T& param) {196  int array1[1];197  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]198 199  T array2[2];200  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]201 202  T value;203}204 205// here, however, plenty of array type findings are expected for below template parameter declarations since C-style array types are written explicitly206template <typename T = int[],207          // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]208          bool = std::is_same_v<T, int[]>,209          // CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]210          bool = std::is_same<T, int[10]>::value,211          // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]212          std::enable_if_t<not(std::is_same_v<std::remove_const_t<T>, int[]>) && not(std::is_same_v<typename std::remove_const<T>::type, char[10]>), bool> = true,213          // CHECK-MESSAGES: :[[@LINE-1]]:71: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]214          // CHECK-MESSAGES: :[[@LINE-2]]:138: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]215          typename = typename std::remove_const<int[10]>::type,216          // CHECK-MESSAGES: :[[@LINE-1]]:49: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]217          typename = std::remove_const_t<int[]>>218          // CHECK-MESSAGES: :[[@LINE-1]]:42: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]219void fun(const T& param) {220  int array3[3];221  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]222 223  T array4[4];224  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]225 226  T value;227}228 229template<typename T>230T some_constant{};231 232// explicit instantiations233template234int some_constant<int[5]>[5];235// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]236 237template238int some_constant<decltype(ak)>[4];239// no diagnostic is expected here since explicit instantiations aren't represented as `TypeLoc` in the AST and we hence cannot match them as such240 241MyArrayType mk;242// no diagnostic is expected here since no C-style array type got written here243 244// explicit specializations245template<>246int some_constant<int[7]>[7]{};247// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]248// CHECK-MESSAGES: :[[@LINE-2]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]249 250template<>251int some_constant<decltype(mk)>[3]{};252// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]253 254void testArrayInTemplateType() {255  int t[10];256  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]257 258  func(t);259  fun(t);260 261  func<decltype(t)>({});262  fun<decltype(t)>({});263 264  func<int[1]>({});265  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]266  fun<int[1]>({});267  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]268 269  MyClassTemplate var{t};270  MyClassTemplate<decltype(t)> var1{{}};271  MyClassTemplate<int[2]> var2{{}};272  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]273 274  decltype(var1)::MyInnerClassTemplate var3{t};275  decltype(var1)::MyInnerClassTemplate<decltype(t)> var4{{}};276  decltype(var1)::MyInnerClassTemplate<char[5]> var5{{}};277  // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]278 279  MyClassTemplate<decltype(t)>::MyInnerClassTemplate var6{t};280  MyClassTemplate<decltype(t)>::MyInnerClassTemplate<decltype(t)> var7{{}};281  MyClassTemplate<decltype(t)>::MyInnerClassTemplate<char[8]> var8{{}};282  // CHECK-MESSAGES: :[[@LINE-1]]:54: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]283  MyClassTemplate<int[9]>::MyInnerClassTemplate<char[9]> var9{{}};284  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]285  // CHECK-MESSAGES: :[[@LINE-2]]:49: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]286}287