================
@@ -0,0 +1,482 @@
+// RUN: %check_clang_tidy %s misc-const-correctness %t -- \
+// RUN:   -config="{CheckOptions: {\
+// RUN:     misc-const-correctness.AnalyzeParameters: true, \
+// RUN:     misc-const-correctness.WarnPointersAsValues: true, \
+// RUN:     misc-const-correctness.TransformPointersAsValues: true \
+// RUN:   }}" -- -I %S/Inputs/const-correctness -fno-delayed-template-parsing
+
+struct Bar {
+  void const_method() const;
+  void mutating_method();
+  int value;
+};
+
+void ref_param_const(Bar& b);
+// CHECK-FIXES: void ref_param_const(Bar const& b);
+
+void ref_param_const(Bar& b) {
+  // CHECK-MESSAGES: [[@LINE-1]]:22: warning: variable 'b' of type 'Bar &' can 
be declared 'const'
+  // CHECK-FIXES: void ref_param_const(Bar const& b) {
+  b.const_method();
+}
+
+void ref_param_already_const(const Bar& f) {
+  f.const_method();
+}
+
+void ref_param_mutated(Bar& f) {
+  f.mutating_method();
+}
+
+void ref_param_member_modified(Bar& b) {
+  b.value = 42;
+}
+
+void pointer_param_read_only(Bar* b) {
+  // CHECK-MESSAGES: [[@LINE-1]]:30: warning: pointee of variable 'b' of type 
'Bar *' can be declared 'const'
+  // CHECK-MESSAGES: [[@LINE-2]]:30: warning: variable 'b' of type 'Bar *' can 
be declared 'const'
+  // CHECK-FIXES: void pointer_param_read_only(Bar const* const b) {
+  b->const_method();
+}
+
+void pointer_param_mutated_pointee(Bar* b) {
+  b->mutating_method();
+}
+
+void pointer_param_mutated_pointer(Bar* b) {
+  // CHECK-MESSAGES: [[@LINE-1]]:36: warning: pointee of variable 'b' of type 
'Bar *' can be declared 'const'
+  // CHECK-FIXES: void pointer_param_mutated_pointer(Bar const* b) {
+  b = nullptr;
+}
+
+void value_param_int(int x) {
+  int y = x + 1;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'y' of type 'int' can be 
declared 'const'
+  // CHECK-FIXES: int const y = x + 1;
+}
+
+void value_param_struct(Bar b) {
+  b.const_method();
+}
+
+void multiple_params_mixed(int x, Bar& b, Bar& f) {
+  // CHECK-MESSAGES: [[@LINE-1]]:35: warning: variable 'b' of type 'Bar &' can 
be declared 'const'
+  // CHECK-FIXES: void multiple_params_mixed(int x, Bar const& b, Bar& f) {
+  int y = x;
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: variable 'y' of type 'int' can be 
declared 'const'
+  // CHECK-FIXES: int const y = x;
+  b.const_method();
+  f.mutating_method();
+}
+
+void rvalue_ref_param(Bar&& b) {
+  b.const_method();
+}
+
+void pass_to_const_ref(const Bar& b);
+void pass_to_nonconst_ref(Bar& b);
+
+void param_passed_to_const_ref(Bar& b) {
+  // CHECK-MESSAGES: [[@LINE-1]]:32: warning: variable 'b' of type 'Bar &' can 
be declared 'const'
+  // CHECK-FIXES: void param_passed_to_const_ref(Bar const& b) {
+  pass_to_const_ref(b);
+}
+
+void param_passed_to_nonconst_ref(Bar& b) {
+  pass_to_nonconst_ref(b);
+}
+
+template<typename T>
+void template_param(T& t) {
+  t.const_method();
+}
+
+template<typename T>
+void forwarding_ref(T&& t) {
+  t.mutating_method();
+}
+
+template<typename T>
+void specialized_func(T& t) {
+  t.const_method();
+}
+
+template<>
+void specialized_func<Bar>(Bar& b) {
+  b.const_method();
+}
+
+template<int N>
+void non_type_template_param(Bar& b) {
+  b.const_method();
+}
+
+template<typename... Args>
+void variadic_template(Args&... args) {
+  (args.const_method(), ...);
+}
+
+template<typename First, typename... Rest>
+void variadic_first_param(First& first, Rest&... rest) {
+  first.const_method();
+}
+
+template<typename T>
+struct is_bar {
+  static constexpr bool value = false;
+};
+
+template<>
+struct is_bar<Bar> {
+  static constexpr bool value = true;
+};
+
+template<typename T, typename = void>
+struct enable_if {};
+
+template<typename T>
+struct enable_if<T, typename T::type> {
+  using type = typename T::type;
+};
+
+struct true_type { using type = void; };
+struct false_type {};
+
+template<bool B>
+struct bool_constant : false_type {};
+
+template<>
+struct bool_constant<true> : true_type {};
+
+template<typename T>
+void sfinae_func(T& t, typename 
enable_if<bool_constant<is_bar<T>::value>>::type* = nullptr) {
+  t.const_method();
+}
+
+void instantiate() {
+  int a = 42;
+  Bar b;
+  template_param(b);
+  forwarding_ref(b);
+  specialized_func(b);
+  non_type_template_param<2>(b);
+  variadic_template(b, b);
+  variadic_first_param(b, a);
+  sfinae_func(b);
+}
+
+// Leave this for futher reference if const-correctness is implemented on 
template functions/methods 
----------------
localspook wrote:

```suggestion
// Leave this for further reference if const-correctness is implemented on 
template functions/methods 
```

https://github.com/llvm/llvm-project/pull/171215
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to