1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
!> Define tests for the `fpm_sources` module (parsing routines)
module test_source_parsing
use testsuite, only : new_unittest, unittest_t, error_t, test_failed
use fpm_filesystem, only: get_temp_filename
use fpm_source_parsing, only: parse_f_source, parse_c_source
use fpm_model, only: srcfile_t, FPM_UNIT_PROGRAM, FPM_UNIT_MODULE, &
FPM_UNIT_SUBMODULE, FPM_UNIT_SUBPROGRAM, FPM_UNIT_CSOURCE
use fpm_strings, only: operator(.in.)
implicit none
private
public :: collect_source_parsing
contains
!> Collect all exported unit tests
subroutine collect_source_parsing(testsuite)
!> Collection of tests
type(unittest_t), allocatable, intent(out) :: testsuite(:)
testsuite = [ &
& new_unittest("modules-used", test_modules_used), &
& new_unittest("intrinsic-modules-used", test_intrinsic_modules_used), &
& new_unittest("include-stmt", test_include_stmt), &
& new_unittest("program", test_program), &
& new_unittest("module", test_module), &
& new_unittest("program-with-module", test_program_with_module), &
& new_unittest("submodule", test_submodule), &
& new_unittest("submodule-ancestor", test_submodule_ancestor), &
& new_unittest("subprogram", test_subprogram), &
& new_unittest("csource", test_csource), &
& new_unittest("invalid-use-stmt", &
test_invalid_use_stmt, should_fail=.true.), &
& new_unittest("invalid-include-stmt", &
test_invalid_include_stmt, should_fail=.true.), &
& new_unittest("invalid-module", &
test_invalid_module, should_fail=.true.), &
& new_unittest("invalid-submodule", &
test_invalid_submodule, should_fail=.true.) &
]
end subroutine collect_source_parsing
!> Check parsing of module 'USE' statements
subroutine test_modules_used(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program test', &
& ' use module_one', &
& ' use :: module_two', &
& ' use module_three, only: a, b, c', &
& ' use :: module_four, only: a => b', &
& '! use module_not_used', &
& ' implicit none', &
& 'end program test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM')
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 4) then
call test_failed(error,'Incorrect number of modules_used - expecting four')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('module_two' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('module_three' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('module_four' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if ('module_not_used' .in. f_source%modules_used) then
call test_failed(error,'Commented module found in modules_used')
return
end if
end subroutine test_modules_used
!> Check that intrinsic modules are properly ignore
subroutine test_intrinsic_modules_used(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program test', &
& ' use iso_c_binding', &
& ' use iso_fortran_env', &
& ' use ieee_arithmetic', &
& ' use ieee_exceptions', &
& ' use ieee_features', &
& ' implicit none', &
& 'end program test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 0) then
call test_failed(error,'Incorrect number of modules_used - expecting zero')
return
end if
if ('iso_c_binding' .in. f_source%modules_used) then
call test_failed(error,'Intrinsic module found in modules_used')
return
end if
if ('iso_fortran_env' .in. f_source%modules_used) then
call test_failed(error,'Intrinsic module found in modules_used')
return
end if
if ('ieee_arithmetic' .in. f_source%modules_used) then
call test_failed(error,'Intrinsic module found in modules_used')
return
end if
if ('ieee_exceptions' .in. f_source%modules_used) then
call test_failed(error,'Intrinsic module found in modules_used')
return
end if
if ('ieee_features' .in. f_source%modules_used) then
call test_failed(error,'Intrinsic module found in modules_used')
return
end if
end subroutine test_intrinsic_modules_used
!> Check parsing of include statements
subroutine test_include_stmt(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program test', &
& ' implicit none', &
& ' include "included_file.f90"', &
& ' character(*) :: include_comments', &
& ' include_comments = "some comments"', &
& ' contains ', &
& ' include"second_include.f90"', &
& 'end program test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 0) then
call test_failed(error,'Incorrect number of modules_used - expecting zero')
return
end if
if (size(f_source%include_dependencies) /= 2) then
call test_failed(error,'Incorrect number of include_dependencies - expecting two')
return
end if
if (.not.('included_file.f90' .in. f_source%include_dependencies)) then
call test_failed(error,'Missing include file in include_dependencies')
return
end if
if (.not.('second_include.f90' .in. f_source%include_dependencies)) then
call test_failed(error,'Missing include file in include_dependencies')
return
end if
end subroutine test_include_stmt
!> Try to parse a simple fortran program
subroutine test_program(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program my_program', &
& 'use module_one', &
& 'implicit none', &
& 'integer :: module', &
& 'module = 1', &
& 'module= 1', &
& 'module =1', &
& 'module (i) =1', &
& 'contains', &
& 'subroutine f()', &
& 'end subroutine f', &
& 'end program my_program'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM')
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 1) then
call test_failed(error,'Incorrect number of modules_used - expecting one')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
end subroutine test_program
!> Try to parse fortran module
subroutine test_module(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'module my_mod ! A trailing comment', &
& 'use module_one', &
& 'interface', &
& ' module subroutine f()', &
& 'end interface', &
& 'integer :: program', &
& 'program = 1', &
& 'program= 1', &
& 'program =1', &
& 'program (i) =1', &
& 'contains', &
& 'module subroutine&', &
& ' e()', &
& 'end subroutine e', &
& 'module subroutine f()', &
& 'end subroutine f', &
& 'module function g()', &
& 'end function g', &
& 'module integer function h()', &
& 'end function h', &
& 'module real function i()', &
& 'string = " &', &
& 'module name"', &
& 'string = " &', &
& 'module name !"', &
& 'end function i', &
& 'end module test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_MODULE) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_MODULE')
return
end if
if (size(f_source%modules_provided) /= 1) then
call test_failed(error,'Unexpected modules_provided - expecting one')
return
end if
if (size(f_source%modules_used) /= 1) then
call test_failed(error,'Incorrect number of modules_used - expecting one')
return
end if
if (.not.('my_mod' .in. f_source%modules_provided)) then
call test_failed(error,'Missing module in modules_provided')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
end subroutine test_module
!> Try to parse combined fortran module and program
!> Check that parsed unit type is FPM_UNIT_PROGRAM
subroutine test_program_with_module(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'module my_mod', &
& 'use module_one', &
& 'interface', &
& ' module subroutine f()', &
& 'end interface', &
& 'contains', &
& 'module procedure f()', &
& 'end procedure f', &
& 'end module test', &
& 'program my_program', &
& 'use my_mod', &
& 'implicit none', &
& 'end my_program'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM')
return
end if
if (size(f_source%modules_provided) /= 1) then
call test_failed(error,'Unexpected modules_provided - expecting one')
return
end if
if (.not.('my_mod' .in. f_source%modules_provided)) then
call test_failed(error,'Missing module in modules_provided')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('my_mod' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
end subroutine test_program_with_module
!> Try to parse fortran submodule for ancestry
subroutine test_submodule(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'submodule (parent) child', &
& 'use module_one', &
& 'end submodule test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_SUBMODULE) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBMODULE')
return
end if
if (size(f_source%modules_provided) /= 1) then
call test_failed(error,'Unexpected modules_provided - expecting one')
return
end if
if (size(f_source%modules_used) /= 2) then
call test_failed(error,'Incorrect number of modules_used - expecting two')
return
end if
if (.not.('child' .in. f_source%modules_provided)) then
call test_failed(error,'Missing module in modules_provided')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('parent' .in. f_source%modules_used)) then
call test_failed(error,'Missing parent module in modules_used')
return
end if
end subroutine test_submodule
!> Try to parse fortran multi-level submodule for ancestry
subroutine test_submodule_ancestor(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'submodule (ancestor:parent) child', &
& 'use module_one', &
& 'end submodule test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_SUBMODULE) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBMODULE')
return
end if
if (size(f_source%modules_provided) /= 1) then
call test_failed(error,'Unexpected modules_provided - expecting one')
return
end if
if (size(f_source%modules_used) /= 2) then
call test_failed(error,'Incorrect number of modules_used - expecting two')
return
end if
if (.not.('child' .in. f_source%modules_provided)) then
call test_failed(error,'Missing module in modules_provided')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
if (.not.('parent' .in. f_source%modules_used)) then
call test_failed(error,'Missing parent module in modules_used')
return
end if
end subroutine test_submodule_ancestor
!> Try to parse standard fortran sub-program (non-module) source
subroutine test_subprogram(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'subroutine my_sub(a)', &
& ' use module_one', &
& ' integer, intent(in) :: a', &
& 'end subroutine my_sub'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_SUBPROGRAM) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBPROGRAM')
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 1) then
call test_failed(error,'Incorrect number of modules_used - expecting one')
return
end if
if (.not.('module_one' .in. f_source%modules_used)) then
call test_failed(error,'Missing module in modules_used')
return
end if
end subroutine test_subprogram
!> Try to parse standard c source for includes
subroutine test_csource(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
temp_file = temp_file//'.c'
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& '#include "proto.h"', &
& 'void c_func(int a) {', &
& ' #include "function_body.c"', &
& ' return', &
& '}'
close(unit)
f_source = parse_c_source(temp_file,error)
if (allocated(error)) then
return
end if
if (f_source%unit_type /= FPM_UNIT_CSOURCE) then
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_CSOURCE')
return
end if
if (size(f_source%modules_provided) /= 0) then
call test_failed(error,'Unexpected modules_provided - expecting zero')
return
end if
if (size(f_source%modules_used) /= 0) then
call test_failed(error,'Incorrect number of modules_used - expecting zero')
return
end if
if (size(f_source%include_dependencies) /= 2) then
call test_failed(error,'Incorrect number of include_dependencies - expecting two')
return
end if
if (.not.('proto.h' .in. f_source%include_dependencies)) then
call test_failed(error,'Missing file in include_dependencies')
return
end if
if (.not.('function_body.c' .in. f_source%include_dependencies)) then
call test_failed(error,'Missing file in include_dependencies')
return
end if
end subroutine test_csource
!> Try to parse fortran program with invalid use statement
subroutine test_invalid_use_stmt(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program test', &
& 'use module_one', &
& 'use :: ', &
& 'end program test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
end subroutine test_invalid_use_stmt
!> Try to parse fortran program with invalid use statement
subroutine test_invalid_include_stmt(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'program test', &
& ' include "', &
& 'end program test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
end subroutine test_invalid_include_stmt
!> Try to parse incorrect fortran module syntax
subroutine test_invalid_module(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'module ::my_mod', &
& 'end module test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
end subroutine test_invalid_module
!> Try to parse incorrect fortran submodule syntax
subroutine test_invalid_submodule(error)
!> Error handling
type(error_t), allocatable, intent(out) :: error
integer :: unit
character(:), allocatable :: temp_file
type(srcfile_t), allocatable :: f_source
allocate(temp_file, source=get_temp_filename())
open(file=temp_file, newunit=unit)
write(unit, '(a)') &
& 'submodule :: child', &
& 'end submodule test'
close(unit)
f_source = parse_f_source(temp_file,error)
if (allocated(error)) then
return
end if
write(*,*) '"',f_source%modules_used(1)%s,'"'
end subroutine test_invalid_submodule
end module test_source_parsing
|