Skip to content

Commit 0daa60d

Browse files
committed
Add test for -Wpass-global-variable
1 parent 19bb9f2 commit 0daa60d

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
!RUN: %python %S/test_errors.py %s %flang_fc1 -Werror
2+
module explicit_test_mod
3+
implicit none (type, external)
4+
integer :: i1
5+
integer :: i2(1)
6+
integer :: i3(3)
7+
integer, allocatable :: ia(:)
8+
9+
real :: x1, y1
10+
real :: x2, y2
11+
real :: z, z2
12+
common /xy1/ x1, y1(1)
13+
common /xy2/ x2(1), y2
14+
common /fm/ z(1)
15+
common /fm_bad/ z2(5)
16+
contains
17+
subroutine pass_int(i)
18+
integer, intent(inout) :: i
19+
end subroutine pass_int
20+
subroutine pass_int_1d(i)
21+
integer, intent(inout) :: i(*)
22+
end subroutine pass_int_1d
23+
subroutine pass_real(r)
24+
real, intent(inout) :: r
25+
end subroutine pass_real
26+
subroutine pass_real_1d(r)
27+
real, intent(inout) :: r(*)
28+
end subroutine pass_real_1d
29+
subroutine explicit_test(n)
30+
integer, intent(in) :: n
31+
32+
!WARNING: Passing global variable 'i1' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
33+
call pass_int(i1) !< warn: basic type
34+
call pass_int(i2(1)) !< ok: shape == [1]
35+
call pass_int(i2(n)) !< ok: shape == [1]
36+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
37+
call pass_int(i3(1)) !< warn: shape /= [1]
38+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
39+
call pass_int(i3(n)) !< warn: shape /= [1]
40+
!WARNING: Passing global variable 'i2' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
41+
call pass_int_1d(i2) !< warn: whole array is passed
42+
call pass_int_1d(i2(n:n+3)) !< ok: subrange of array
43+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
44+
call pass_int_1d(i3) !< warn: shape /= [1]
45+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
46+
call pass_int_1d(i3(n:n+3)) !< warn: shape /= [1]
47+
call pass_int(ia(1)) !< ok: allocatable
48+
call pass_int(ia(n)) !< ok: allocatable
49+
call pass_int_1d(ia) !< ok: allocatable
50+
call pass_int_1d(ia(n:n+3)) !< ok: allocatable
51+
52+
!WARNING: Passing global variable 'x1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
53+
call pass_real(x1) !< warn: x1 from common
54+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
55+
call pass_real_1d(y1) !< warn: y1 from common or offset /= 0
56+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
57+
call pass_real(y1(1)) !< warn: offset /= 0
58+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
59+
call pass_real(y1(n)) !< warn: offset /= 0
60+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
61+
call pass_real_1d(y1(n:n+3)) !< warn: offset /= 0
62+
63+
!WARNING: Passing global variable 'y2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
64+
call pass_real(y2) !< warn: offset /= 0
65+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
66+
call pass_real_1d(x2) !< warn: more than one variable in common block
67+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
68+
call pass_real(x2(1)) !< warn: more than one variable in common block
69+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
70+
call pass_real(x2(n)) !< warn: more than one variable in common block
71+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
72+
call pass_real_1d(x2(n:n+3)) !< warn: more than one variable in common block
73+
74+
!WARNING: Passing global variable 'z' from COMMON 'fm' as function argument [-Wpass-global-variable]
75+
call pass_real_1d(z) !< warn: z from common
76+
call pass_real(z(1)) !< ok: single element/begin of mem block
77+
call pass_real(z(n)) !< ok: single element/begin of mem block
78+
call pass_real_1d(z(n:n+3)) !< ok: mem block
79+
80+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
81+
call pass_real_1d(z2) !< warn: shape /= [1]
82+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
83+
call pass_real(z2(1)) !< warn: shape /= [1]
84+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
85+
call pass_real(z2(n)) !< warn: shape /= [1]
86+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
87+
call pass_real_1d(z2(n:n+3)) !< warn: shape /= [1]
88+
end subroutine explicit_test
89+
end module explicit_test_mod
90+
91+
subroutine module_test(n)
92+
use explicit_test_mod, only: i1, i2, i3, ia
93+
implicit none (type, external)
94+
integer, intent(in) :: n
95+
96+
external :: imp_pass_int, imp_pass_int_1d
97+
98+
!WARNING: Passing global variable 'i1' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
99+
call imp_pass_int(i1) !< warn: i1 from common
100+
call imp_pass_int(i2(1)) !< ok: single element/begin of mem block
101+
call imp_pass_int(i2(n)) !< ok: single element/begin of mem block
102+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
103+
call imp_pass_int(i3(1)) !< warn: shape /= [1]
104+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
105+
call imp_pass_int(i3(n)) !< warn: shape /= [1]
106+
call imp_pass_int(ia(1)) !< ok: allocatable
107+
call imp_pass_int(ia(n)) !< ok: allocatable
108+
109+
!WARNING: Passing global variable 'i2' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
110+
call imp_pass_int_1d(i2) !< warn: i2 from module
111+
call imp_pass_int_1d(i2(n:n+3)) !< ok: mem block
112+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
113+
call imp_pass_int_1d(i3) !< warn: i3 from module & shape /= [1]
114+
!WARNING: Passing global variable 'i3' from MODULE 'explicit_test_mod' as function argument [-Wpass-global-variable]
115+
call imp_pass_int_1d(i3(n:n+3)) !< warn: shape /= [1]
116+
call imp_pass_int_1d(ia) !< ok: allocatable
117+
call imp_pass_int_1d(ia(n:n+3)) !< ok: allocatable
118+
end subroutine module_test
119+
120+
subroutine implicit_test(n)
121+
implicit none (type, external)
122+
integer, intent(in) :: n
123+
real :: x1, y1
124+
real :: x2, y2
125+
real :: z, z2
126+
common /xy1/ x1, y1(1)
127+
common /xy2/ x2(1), y2
128+
common /fm/ z(1)
129+
common /fm_bad/ z2(5)
130+
131+
external :: imp_pass_real, imp_pass_real_1d
132+
133+
!WARNING: Passing global variable 'x1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
134+
call imp_pass_real(x1) !< warn: x1 from common
135+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
136+
call imp_pass_real_1d(y1) !< warn: y1 from common and offset /= 0
137+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
138+
call imp_pass_real(y1(1)) !< warn: offset /= 0
139+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
140+
call imp_pass_real(y1(n)) !< warn: offset /= 0
141+
!WARNING: Passing global variable 'y1' from COMMON 'xy1' as function argument [-Wpass-global-variable]
142+
call imp_pass_real_1d(y1(n:n+3)) !< warn: offset /= 0
143+
144+
!WARNING: Passing global variable 'y2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
145+
call imp_pass_real(y2) !< warn: y2 from common and offset /= 0
146+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
147+
call imp_pass_real_1d(x2) !< warn: x2 from common
148+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
149+
call imp_pass_real(x2(1)) !< warn: more than one variable in common
150+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
151+
call imp_pass_real(x2(n)) !< warn: more than one variable in common
152+
!WARNING: Passing global variable 'x2' from COMMON 'xy2' as function argument [-Wpass-global-variable]
153+
call imp_pass_real_1d(x2(n:n+3)) !< warn: more than one variable in common
154+
155+
!WARNING: Passing global variable 'z' from COMMON 'fm' as function argument [-Wpass-global-variable]
156+
call imp_pass_real_1d(z) !< warn: z from common
157+
call imp_pass_real(z(1)) !< ok: single element/begin of mem block
158+
call imp_pass_real(z(n)) !< ok: single element/begin of mem block
159+
call imp_pass_real_1d(z(n:n+3)) !< ok: mem block
160+
161+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
162+
call imp_pass_real_1d(z2) !< warn: z2 from common, shape /= [1]
163+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
164+
call imp_pass_real(z2(1)) !< warn: shape /= [1]
165+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
166+
call imp_pass_real(z2(n)) !< warn: shape /= [1]
167+
!WARNING: Passing global variable 'z2' from COMMON 'fm_bad' as function argument [-Wpass-global-variable]
168+
call imp_pass_real_1d(z2(n:n+3)) !< warn: shape /= [1]
169+
end subroutine implicit_test

0 commit comments

Comments
 (0)