xref: /llvm-project/clang/test/Misc/diag-unused-source-ranges.cpp (revision 64083172eea26e50c8b22b85697a825be8bda424)
1 // RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -Wunused -Wunused-template -Wunused-exception-parameter -Wunused-member-function -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s --strict-whitespace
2 #include "Inputs/diag-unused-source-ranges.h"
3 
4 #define CAT(a, b) a ## b
5 
6 // CHECK:      :{55:15-55:20}: warning: unused exception parameter 'param'
7 // CHECK-NEXT:   catch (int &param) {}
8 // CHECK-NEXT:               ^~~~~{{$}}
9 
10 // CHECK:      :{53:7-53:12}: warning: unused variable 'local'
11 // CHECK-NEXT:   int local = 0;
12 // CHECK-NEXT:       ^~~~~{{$}}
13 
14 // CHECK:      In file included from
15 // CHECK-NEXT: :{1:13-1:18}: warning: 'static' function 'thing' declared in header file should be declared 'static inline'
16 // CHECK-NEXT:   static void thing(void) {}
17 // CHECK-NEXT:               ^~~~~{{$}}
18 
19 namespace {
20 class A {
21   // CHECK:      :{[[@LINE+3]]:10-[[@LINE+3]]:14}: warning: member function 'func' is not needed
22   // CHECK-NEXT:   void func() {}
23   // CHECK-NEXT:        ^~~~{{$}}
func()24     void func() {}
25   // CHECK:      :{[[@LINE+3]]:32-[[@LINE+3]]:37}: warning: unused function template
26   // CHECK-NEXT:   void templ(T) {}
27   // CHECK-NEXT:        ^~~~~{{$}}
templ(T)28     template <typename T> void templ(T) {}
29   // CHECK:      :{[[@LINE+3]]:22-[[@LINE+3]]:32}: warning: member function 'templ<int>' is not needed
30   // CHECK-NEXT:   void templ<int>(int) {}
31   // CHECK-NEXT:        ^~~~~~~~~~{{$}}
templ(int)32     template <> void templ<int>(int) {}
33   // CHECK:      :{[[@LINE+3]]:22-[[@LINE+3]]:27}: warning: member function 'templ<float>' is not needed
34   // CHECK-NEXT:   void templ(float) {}
35   // CHECK-NEXT:        ^~~~~{{$}}
templ(float)36     template <> void templ(float) {}
37 
38   // CHECK:      :{[[@LINE+4]]:10-[[@LINE+4]]:13}: warning: unused function template
39   // CHECK-NEXT:   void foo() {
40   // CHECK-NEXT:        ^~~{{$}}
41     template <typename T>
foo()42     void foo() {
43       func();
44       templ(0);
45       templ(0.0f);
46       templ(0.0);
47     }
48 };
49 // CHECK:      :{[[@LINE+3]]:12-[[@LINE+3]]:23}: warning: unused function 'unused_func'
50 // CHECK-NEXT:   static int unused_func(int aaa, char bbb) {
51 // CHECK-NEXT:              ^~~~~~~~~~~{{$}}
unused_func(int aaa,char bbb)52 static int unused_func(int aaa, char bbb) {
53   int local = 0;
54   try{}
55   catch (int &param) {}
56   return 0;
57 }
58 
59 // CHECK:      :{[[@LINE+4]]:6-[[@LINE+4]]:16}: warning: unused function template
60 // CHECK-NEXT:   auto arrow_decl(T a, T b) ->
61 // CHECK-NEXT:        ^~~~~~~~~~{{$}}
62 template <typename T>
arrow_decl(T a,T b)63 auto arrow_decl(T a, T b) -> decltype(a + b) { thing(); return a + b; }
64 
65 // CHECK:      :{[[@LINE+4]]:6-[[@LINE+4]]:21}: warning: unused function 'arrow_decl<int>'
66 // CHECK-NEXT:   auto arrow_decl<int>(int a, int b) ->
67 // CHECK-NEXT:        ^~~~~~~~~~~~~~~{{$}}
68 template <>
arrow_decl(int a,int b)69 auto arrow_decl<int>(int a, int b) -> int { return a + b; }
70 
71 
72 // CHECK:      :{[[@LINE+4]]:10-[[@LINE+4]]:20}: warning: unused function template
73 // CHECK-NEXT:   static T func_templ(int bbb, T ccc) {
74 // CHECK-NEXT:            ^~~~~~~~~~{{$}}
75 template <typename T>
func_templ(int bbb,T ccc)76 static T func_templ(int bbb, T ccc) {
77   return ccc;
78 }
79 
80 // CHECK:      :{[[@LINE+3]]:17-[[@LINE+3]]:32}: warning: function 'func_templ<int>' is not needed
81 // CHECK-NEXT:   int func_templ<int>(int bbb, int ccc) {
82 // CHECK-NEXT:       ^~~~~~~~~~~~~~~{{$}}
func_templ(int bbb,int ccc)83 template <> int func_templ<int>(int bbb, int ccc) {
84   return bbb;
85 }
86 
87 // CHECK:      :{[[@LINE+3]]:35-[[@LINE+3]]:47}: warning: unused function template
88 // CHECK-NEXT:   static void never_called() {
89 // CHECK-NEXT:               ^~~~~~~~~~~~{{$}}
never_called()90 template <typename T> static void never_called() {
91   func_templ<int>(0, 0);
92 }
93 
94 // CHECK:      :{[[@LINE+3]]:22-[[@LINE+3]]:31}: warning: unused variable template
95 // CHECK-NEXT:   int var_templ =
96 // CHECK-NEXT:       ^~~~~~~~~{{$}}
97 template <int n> int var_templ = n * var_templ<n-1>;
98 // CHECK:      :{[[@LINE+3]]:17-[[@LINE+3]]:29}: warning: variable 'var_templ<0>' is not needed
99 // CHECK-NEXT:   int var_templ<0> =
100 // CHECK-NEXT:       ^~~~~~~~~~~~{{$}}
101 template <> int var_templ<0> = 1;
102 struct {
103 // CHECK:      :{[[@LINE+3]]:8-[[@LINE+3]]:11}: warning: unused member function 'fun'
104 // CHECK-NEXT:   void fun() {}
105 // CHECK-NEXT:        ^~~{{$}}
fun__anond93826520111::__anond93826520208106   void fun() {}
107 // CHECK:      :{[[@LINE+3]]:3-[[@LINE+3]]:8}: warning: unused variable 'var_x'
108 // CHECK-NEXT:   } var_x;
109 // CHECK-NEXT:     ^~~~~{{$}}
110 } var_x;
111 
112 // CHECK:      :{[[@LINE+5]]:12-[[@LINE+6]]:12}: warning: unused variable 'new_line'
113 // CHECK-NEXT:   static int CAT(new_,
114 // CHECK-NEXT:              ^~~~~~~~~{{$}}
115 // CHECK-NEXT:         line) =
116 // CHECK-NEXT:         ~~~~~{{$}}
117 static int CAT(new_,
118       line) = sizeof(var_templ<0>);
119 }
120 
121 // CHECK:      :{[[@LINE+3]]:15-[[@LINE+3]]:27}: warning: unused variable 'const_unused'
122 // CHECK-NEXT:   constexpr int const_unused = 1
123 // CHECK-NEXT:                 ^~~~~~~~~~~~{{$}}
124 constexpr int const_unused = 1;
125