| 1 |
!> Define tests for the `fpm_sources` module (parsing routines) |
| 2 |
module test_source_parsing |
| 3 |
use testsuite, only : new_unittest, unittest_t, error_t, test_failed |
| 4 |
use fpm_filesystem, only: get_temp_filename |
| 5 |
use fpm_source_parsing, only: parse_f_source, parse_c_source, parse_use_statement |
| 6 |
use fpm_model, only: srcfile_t, FPM_UNIT_PROGRAM, FPM_UNIT_MODULE, & |
| 7 |
FPM_UNIT_SUBMODULE, FPM_UNIT_SUBPROGRAM, FPM_UNIT_CSOURCE, & |
| 8 |
FPM_UNIT_CPPSOURCE, FPM_UNIT_NAME |
| 9 |
use fpm_strings, only: operator(.in.), lower, string_t |
| 10 |
use fpm_error, only: file_parse_error, fatal_error |
| 11 |
use fpm_manifest_preprocess, only: preprocess_config_t |
| 12 |
implicit none |
| 13 |
private |
| 14 |
|
| 15 |
public :: collect_source_parsing |
| 16 |
|
| 17 |
contains |
| 18 |
|
| 19 |
!> Collect all exported unit tests |
| 20 |
subroutine collect_source_parsing(testsuite) |
| 21 |
|
| 22 |
!> Collection of tests |
| 23 |
type(unittest_t), allocatable, intent(out) :: testsuite(:) |
| 24 |
|
| 25 |
testsuite = [ & |
| 26 |
& new_unittest("modules-used", test_modules_used), & |
| 27 |
& new_unittest("intrinsic-modules-used", test_intrinsic_modules_used), & |
| 28 |
& new_unittest("nonintrinsic-modules-used", test_nonintrinsic_modules_used), & |
| 29 |
& new_unittest("include-stmt", test_include_stmt), & |
| 30 |
& new_unittest("program", test_program), & |
| 31 |
& new_unittest("program-noheader", test_program_noheader), & |
| 32 |
& new_unittest("program-noheader-2", test_program_noheader_2), & |
| 33 |
& new_unittest("module", test_module), & |
| 34 |
& new_unittest("module-with-subprogram", test_module_with_subprogram), & |
| 35 |
& new_unittest("module-with-c-api", test_module_with_c_api), & |
| 36 |
& new_unittest("module-with-abstract-interface",test_module_with_abstract_interface), & |
| 37 |
& new_unittest("module-end-stmt", test_module_end_stmt), & |
| 38 |
& new_unittest("program-with-module", test_program_with_module), & |
| 39 |
& new_unittest("program-with-abstract-interface", test_program_with_abstract_interface), & |
| 40 |
& new_unittest("submodule", test_submodule), & |
| 41 |
& new_unittest("submodule-ancestor", test_submodule_ancestor), & |
| 42 |
& new_unittest("subprogram", test_subprogram), & |
| 43 |
& new_unittest("csource", test_csource), & |
| 44 |
& new_unittest("invalid-use-stmt", & |
| 45 |
test_invalid_use_stmt, should_fail=.true.), & |
| 46 |
& new_unittest("invalid-include-stmt", & |
| 47 |
test_invalid_include_stmt, should_fail=.true.), & |
| 48 |
& new_unittest("invalid-module", & |
| 49 |
test_invalid_module, should_fail=.true.), & |
| 50 |
& new_unittest("invalid-submodule", & |
| 51 |
test_invalid_submodule, should_fail=.true.), & |
| 52 |
& new_unittest("use-statement",test_use_statement), & |
| 53 |
& new_unittest("conditional-compilation", test_conditional_compilation), & |
| 54 |
& new_unittest("conditional-compilation-elif", test_conditional_compilation_elif), & |
| 55 |
& new_unittest("conditional-compilation-elif-else", test_conditional_compilation_elif_else), & |
| 56 |
& new_unittest("conditional-compilation_ifdef_else", test_conditional_compilation_ifdef_else), & |
| 57 |
& new_unittest("conditional-if-defined", test_conditional_if_defined), & |
| 58 |
& new_unittest("conditional-macro-comparison", test_conditional_macro_comparison), & |
| 59 |
& new_unittest("define-without-trailing-space", test_define_no_trailing_space), & |
| 60 |
& new_unittest("macro-case-sensitivity", test_macro_case_sensitivity), & |
| 61 |
& new_unittest("if-macro-truthy", test_if_macro_truthy), & |
| 62 |
& new_unittest("if-macro-truthy-from-define", test_if_macro_truthy_from_define) & |
| 63 |
] |
| 64 |
|
| 65 |
end subroutine collect_source_parsing |
| 66 |
|
| 67 |
|
| 68 |
!> Check parsing of module 'USE' statements |
| 69 |
subroutine test_modules_used(error) |
| 70 |
|
| 71 |
!> Error handling |
| 72 |
type(error_t), allocatable, intent(out) :: error |
| 73 |
|
| 74 |
integer :: unit |
| 75 |
character(:), allocatable :: temp_file |
| 76 |
type(srcfile_t), allocatable :: f_source |
| 77 |
|
| 78 |
allocate(temp_file, source=get_temp_filename()) |
| 79 |
|
| 80 |
open(file=temp_file, newunit=unit) |
| 81 |
write(unit, '(a)') & |
| 82 |
& 'program test', & |
| 83 |
& ' use module_one', & |
| 84 |
& ' use :: module_two', & |
| 85 |
& ' use module_three, only: a, b, c', & |
| 86 |
& ' use :: module_four, only: a => b', & |
| 87 |
& '! use module_not_used', & |
| 88 |
& ' implicit none', & |
| 89 |
& 'end program test' |
| 90 |
close(unit) |
| 91 |
|
| 92 |
f_source = parse_f_source(temp_file,error) |
| 93 |
if (allocated(error)) then |
| 94 |
return |
| 95 |
end if |
| 96 |
|
| 97 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 98 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM') |
| 99 |
return |
| 100 |
end if |
| 101 |
|
| 102 |
if (size(f_source%modules_provided) /= 0) then |
| 103 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 104 |
return |
| 105 |
end if |
| 106 |
|
| 107 |
if (size(f_source%modules_used) /= 4) then |
| 108 |
call test_failed(error,'Incorrect number of modules_used - expecting four') |
| 109 |
return |
| 110 |
end if |
| 111 |
|
| 112 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 113 |
call test_failed(error,'Missing module in modules_used') |
| 114 |
return |
| 115 |
end if |
| 116 |
|
| 117 |
if (.not.('module_two' .in. f_source%modules_used)) then |
| 118 |
call test_failed(error,'Missing module in modules_used') |
| 119 |
return |
| 120 |
end if |
| 121 |
|
| 122 |
if (.not.('module_three' .in. f_source%modules_used)) then |
| 123 |
call test_failed(error,'Missing module in modules_used') |
| 124 |
return |
| 125 |
end if |
| 126 |
|
| 127 |
if (.not.('module_four' .in. f_source%modules_used)) then |
| 128 |
call test_failed(error,'Missing module in modules_used') |
| 129 |
return |
| 130 |
end if |
| 131 |
|
| 132 |
if ('module_not_used' .in. f_source%modules_used) then |
| 133 |
call test_failed(error,'Commented module found in modules_used') |
| 134 |
return |
| 135 |
end if |
| 136 |
|
| 137 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 138 |
|
| 139 |
end subroutine test_modules_used |
| 140 |
|
| 141 |
|
| 142 |
!> Check that intrinsic modules are properly ignore |
| 143 |
subroutine test_intrinsic_modules_used(error) |
| 144 |
|
| 145 |
!> Error handling |
| 146 |
type(error_t), allocatable, intent(out) :: error |
| 147 |
|
| 148 |
integer :: unit |
| 149 |
character(:), allocatable :: temp_file |
| 150 |
type(srcfile_t), allocatable :: f_source |
| 151 |
|
| 152 |
allocate(temp_file, source=get_temp_filename()) |
| 153 |
|
| 154 |
open(file=temp_file, newunit=unit) |
| 155 |
write(unit, '(a)') & |
| 156 |
& 'program test', & |
| 157 |
& ' use iso_c_binding', & |
| 158 |
& ' use iso_fortran_env', & |
| 159 |
& ' use ieee_arithmetic', & |
| 160 |
& ' use ieee_exceptions', & |
| 161 |
& ' use ieee_features', & |
| 162 |
& ' implicit none', & |
| 163 |
& 'end program test' |
| 164 |
close(unit) |
| 165 |
|
| 166 |
f_source = parse_f_source(temp_file,error) |
| 167 |
if (allocated(error)) then |
| 168 |
return |
| 169 |
end if |
| 170 |
|
| 171 |
if (size(f_source%modules_provided) /= 0) then |
| 172 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 173 |
return |
| 174 |
end if |
| 175 |
|
| 176 |
if (size(f_source%modules_used) /= 0) then |
| 177 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 178 |
return |
| 179 |
end if |
| 180 |
|
| 181 |
if ('iso_c_binding' .in. f_source%modules_used) then |
| 182 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 183 |
return |
| 184 |
end if |
| 185 |
|
| 186 |
if ('iso_fortran_env' .in. f_source%modules_used) then |
| 187 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 188 |
return |
| 189 |
end if |
| 190 |
|
| 191 |
if ('ieee_arithmetic' .in. f_source%modules_used) then |
| 192 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 193 |
return |
| 194 |
end if |
| 195 |
|
| 196 |
if ('ieee_exceptions' .in. f_source%modules_used) then |
| 197 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 198 |
return |
| 199 |
end if |
| 200 |
|
| 201 |
if ('ieee_features' .in. f_source%modules_used) then |
| 202 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 203 |
return |
| 204 |
end if |
| 205 |
|
| 206 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 207 |
|
| 208 |
end subroutine test_intrinsic_modules_used |
| 209 |
|
| 210 |
|
| 211 |
!> Check that intrinsic module names are not ignored if declared non_intrinsic |
| 212 |
subroutine test_nonintrinsic_modules_used(error) |
| 213 |
|
| 214 |
!> Error handling |
| 215 |
type(error_t), allocatable, intent(out) :: error |
| 216 |
|
| 217 |
integer :: unit |
| 218 |
character(:), allocatable :: temp_file |
| 219 |
type(srcfile_t), allocatable :: f_source |
| 220 |
|
| 221 |
allocate(temp_file, source=get_temp_filename()) |
| 222 |
|
| 223 |
open(file=temp_file, newunit=unit) |
| 224 |
write(unit, '(a)') & |
| 225 |
& 'program test', & |
| 226 |
& ' use, non_intrinsic :: iso_c_binding', & |
| 227 |
& ' use, intrinsic :: iso_fortran_env', & |
| 228 |
& ' use, non_intrinsic :: ieee_arithmetic', & |
| 229 |
& ' use, non_intrinsic :: ieee_exceptions', & |
| 230 |
& ' use, non_intrinsic :: ieee_features', & |
| 231 |
& ' use, non_intrinsic :: my_module', & |
| 232 |
& ' implicit none', & |
| 233 |
& 'end program test' |
| 234 |
close(unit) |
| 235 |
|
| 236 |
f_source = parse_f_source(temp_file,error) |
| 237 |
if (allocated(error)) then |
| 238 |
return |
| 239 |
end if |
| 240 |
|
| 241 |
if (size(f_source%modules_provided) /= 0) then |
| 242 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 243 |
return |
| 244 |
end if |
| 245 |
|
| 246 |
if (size(f_source%modules_used) /= 5) then |
| 247 |
call test_failed(error,'Incorrect number of modules_used - expecting five') |
| 248 |
return |
| 249 |
end if |
| 250 |
|
| 251 |
if (.not. ('iso_c_binding' .in. f_source%modules_used)) then |
| 252 |
call test_failed(error,'Non-Intrinsic module found in modules_used') |
| 253 |
return |
| 254 |
end if |
| 255 |
|
| 256 |
if ('iso_fortran_env' .in. f_source%modules_used) then |
| 257 |
call test_failed(error,'Intrinsic module found in modules_used') |
| 258 |
return |
| 259 |
end if |
| 260 |
|
| 261 |
if (.not. ('ieee_arithmetic' .in. f_source%modules_used)) then |
| 262 |
call test_failed(error,'Non-Intrinsic module not found in modules_used') |
| 263 |
return |
| 264 |
end if |
| 265 |
|
| 266 |
if (.not. ('ieee_exceptions' .in. f_source%modules_used)) then |
| 267 |
call test_failed(error,'Non-Intrinsic module not found in modules_used') |
| 268 |
return |
| 269 |
end if |
| 270 |
|
| 271 |
if (.not. ('ieee_features' .in. f_source%modules_used)) then |
| 272 |
call test_failed(error,'Non-Intrinsic module not found in modules_used') |
| 273 |
return |
| 274 |
end if |
| 275 |
|
| 276 |
if (.not. ('my_module' .in. f_source%modules_used)) then |
| 277 |
call test_failed(error,'Non-Intrinsic module not found in modules_used') |
| 278 |
return |
| 279 |
end if |
| 280 |
|
| 281 |
end subroutine test_nonintrinsic_modules_used |
| 282 |
|
| 283 |
!> Check parsing of include statements |
| 284 |
subroutine test_include_stmt(error) |
| 285 |
|
| 286 |
!> Error handling |
| 287 |
type(error_t), allocatable, intent(out) :: error |
| 288 |
|
| 289 |
integer :: unit |
| 290 |
character(:), allocatable :: temp_file |
| 291 |
type(srcfile_t), allocatable :: f_source |
| 292 |
|
| 293 |
allocate(temp_file, source=get_temp_filename()) |
| 294 |
|
| 295 |
open(file=temp_file, newunit=unit) |
| 296 |
write(unit, '(a)') & |
| 297 |
& 'program test', & |
| 298 |
& ' implicit none', & |
| 299 |
& ' include "included_file.f90"', & |
| 300 |
& ' character(*) :: include_comments', & |
| 301 |
& ' include_comments = "some comments"', & |
| 302 |
& ' contains ', & |
| 303 |
& ' include"second_include.f90"', & |
| 304 |
& 'end program test' |
| 305 |
close(unit) |
| 306 |
|
| 307 |
f_source = parse_f_source(temp_file,error) |
| 308 |
if (allocated(error)) then |
| 309 |
return |
| 310 |
end if |
| 311 |
|
| 312 |
if (size(f_source%modules_provided) /= 0) then |
| 313 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 314 |
return |
| 315 |
end if |
| 316 |
|
| 317 |
if (size(f_source%modules_used) /= 0) then |
| 318 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 319 |
return |
| 320 |
end if |
| 321 |
|
| 322 |
if (size(f_source%include_dependencies) /= 2) then |
| 323 |
call test_failed(error,'Incorrect number of include_dependencies - expecting two') |
| 324 |
return |
| 325 |
end if |
| 326 |
|
| 327 |
if (.not.('included_file.f90' .in. f_source%include_dependencies)) then |
| 328 |
call test_failed(error,'Missing include file in include_dependencies') |
| 329 |
return |
| 330 |
end if |
| 331 |
|
| 332 |
if (.not.('second_include.f90' .in. f_source%include_dependencies)) then |
| 333 |
call test_failed(error,'Missing include file in include_dependencies') |
| 334 |
return |
| 335 |
end if |
| 336 |
|
| 337 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 338 |
|
| 339 |
end subroutine test_include_stmt |
| 340 |
|
| 341 |
!> Try to parse a simple fortran program |
| 342 |
subroutine test_program(error) |
| 343 |
|
| 344 |
!> Error handling |
| 345 |
type(error_t), allocatable, intent(out) :: error |
| 346 |
|
| 347 |
integer :: unit |
| 348 |
character(:), allocatable :: temp_file |
| 349 |
type(srcfile_t), allocatable :: f_source |
| 350 |
|
| 351 |
allocate(temp_file, source=get_temp_filename()) |
| 352 |
|
| 353 |
open(file=temp_file, newunit=unit) |
| 354 |
write(unit, '(a)') & |
| 355 |
& 'program my_program', & |
| 356 |
& 'use module_one', & |
| 357 |
& 'implicit none', & |
| 358 |
& 'integer :: module', & |
| 359 |
& 'module = 1', & |
| 360 |
& 'module= 1', & |
| 361 |
& 'module =1', & |
| 362 |
& 'module (i) =1', & |
| 363 |
& 'contains', & |
| 364 |
& 'subroutine f()', & |
| 365 |
& 'end subroutine f', & |
| 366 |
& 'end program my_program' |
| 367 |
close(unit) |
| 368 |
|
| 369 |
f_source = parse_f_source(temp_file,error) |
| 370 |
if (allocated(error)) then |
| 371 |
return |
| 372 |
end if |
| 373 |
|
| 374 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 375 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM') |
| 376 |
return |
| 377 |
end if |
| 378 |
|
| 379 |
if (size(f_source%modules_provided) /= 0) then |
| 380 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 381 |
return |
| 382 |
end if |
| 383 |
|
| 384 |
if (size(f_source%modules_used) /= 1) then |
| 385 |
call test_failed(error,'Incorrect number of modules_used - expecting one') |
| 386 |
return |
| 387 |
end if |
| 388 |
|
| 389 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 390 |
call test_failed(error,'Missing module in modules_used') |
| 391 |
return |
| 392 |
end if |
| 393 |
|
| 394 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 395 |
|
| 396 |
end subroutine test_program |
| 397 |
|
| 398 |
!> Try to parse a simple fortran program with no "program" header |
| 399 |
subroutine test_program_noheader(error) |
| 400 |
|
| 401 |
!> Error handling |
| 402 |
type(error_t), allocatable, intent(out) :: error |
| 403 |
|
| 404 |
integer :: unit |
| 405 |
character(:), allocatable :: temp_file |
| 406 |
type(srcfile_t), allocatable :: f_source |
| 407 |
|
| 408 |
allocate(temp_file, source=get_temp_filename()) |
| 409 |
|
| 410 |
open(file=temp_file, newunit=unit) |
| 411 |
write(unit, '(a)') & |
| 412 |
& 'use program_one', & |
| 413 |
& 'implicit none', & |
| 414 |
& 'integer :: module, program', & |
| 415 |
& 'module = 1', & |
| 416 |
& 'module= 1', & |
| 417 |
& 'module =1', & |
| 418 |
& 'module (i) =1', & |
| 419 |
& 'program = 123', & |
| 420 |
& 'contains', & |
| 421 |
& 'subroutine f()', & |
| 422 |
& 'end subroutine f', & |
| 423 |
& 'end program' |
| 424 |
close(unit) |
| 425 |
|
| 426 |
f_source = parse_f_source(temp_file,error) |
| 427 |
if (allocated(error)) then |
| 428 |
return |
| 429 |
end if |
| 430 |
|
| 431 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 432 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM, found '//& |
| 433 |
FPM_UNIT_NAME(f_source%unit_type)) |
| 434 |
return |
| 435 |
end if |
| 436 |
|
| 437 |
if (size(f_source%modules_provided) /= 0) then |
| 438 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 439 |
return |
| 440 |
end if |
| 441 |
|
| 442 |
if (size(f_source%modules_used) /= 1) then |
| 443 |
call test_failed(error,'Incorrect number of modules_used - expecting one') |
| 444 |
return |
| 445 |
end if |
| 446 |
|
| 447 |
if (.not.('program_one' .in. f_source%modules_used)) then |
| 448 |
call test_failed(error,'Missing module in modules_used') |
| 449 |
return |
| 450 |
end if |
| 451 |
|
| 452 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 453 |
|
| 454 |
end subroutine test_program_noheader |
| 455 |
|
| 456 |
!> Try to parse a simple fortran program with no "program" header |
| 457 |
subroutine test_program_noheader_2(error) |
| 458 |
|
| 459 |
!> Error handling |
| 460 |
type(error_t), allocatable, intent(out) :: error |
| 461 |
|
| 462 |
integer :: unit |
| 463 |
character(:), allocatable :: temp_file |
| 464 |
type(srcfile_t), allocatable :: f_source |
| 465 |
|
| 466 |
allocate(temp_file, source=get_temp_filename()) |
| 467 |
|
| 468 |
open(file=temp_file, newunit=unit) |
| 469 |
write(unit, '(a)') & |
| 470 |
& 'print *, "Hello World"', & |
| 471 |
& 'end program' |
| 472 |
close(unit) |
| 473 |
|
| 474 |
f_source = parse_f_source(temp_file,error) |
| 475 |
if (allocated(error)) then |
| 476 |
return |
| 477 |
end if |
| 478 |
|
| 479 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 480 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM, found '//& |
| 481 |
FPM_UNIT_NAME(f_source%unit_type)) |
| 482 |
return |
| 483 |
end if |
| 484 |
|
| 485 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 486 |
|
| 487 |
end subroutine test_program_noheader_2 |
| 488 |
|
| 489 |
!> Try to parse fortran module |
| 490 |
subroutine test_module(error) |
| 491 |
|
| 492 |
!> Error handling |
| 493 |
type(error_t), allocatable, intent(out) :: error |
| 494 |
|
| 495 |
integer :: unit |
| 496 |
character(:), allocatable :: temp_file |
| 497 |
type(srcfile_t), allocatable :: f_source |
| 498 |
|
| 499 |
allocate(temp_file, source=get_temp_filename()) |
| 500 |
|
| 501 |
open(file=temp_file, newunit=unit) |
| 502 |
write(unit, '(a)') & |
| 503 |
& '#define preprocesor_line_outside', & |
| 504 |
& 'module my_mod ! A trailing comment', & |
| 505 |
& 'use module_one', & |
| 506 |
& 'interface', & |
| 507 |
& ' module subroutine f() bind(C)', & |
| 508 |
& 'end interface', & |
| 509 |
& 'integer :: program', & |
| 510 |
& 'program = 1', & |
| 511 |
& 'program= 1', & |
| 512 |
& 'program =1', & |
| 513 |
& 'program (i) =1', & |
| 514 |
& 'contains', & |
| 515 |
& 'module subroutine&', & |
| 516 |
& ' e()', & |
| 517 |
& ' integer, parameter :: c = 1', & |
| 518 |
& ' integer :: & ', & |
| 519 |
& ' bind(c)', & |
| 520 |
& ' bind(c) = 1', & |
| 521 |
& 'end subroutine e', & |
| 522 |
& 'module subroutine f()', & |
| 523 |
& 'end subroutine f', & |
| 524 |
& 'module function g()', & |
| 525 |
& 'end function g', & |
| 526 |
& 'module integer function h()', & |
| 527 |
& 'end function h', & |
| 528 |
& 'module real function i()', & |
| 529 |
& 'string = " &', & |
| 530 |
& 'module name"', & |
| 531 |
& 'string = " &', & |
| 532 |
& 'module name !"', & |
| 533 |
& 'end function i', & |
| 534 |
& 'end module test', & |
| 535 |
& '! A trailing comment outside of module' |
| 536 |
close(unit) |
| 537 |
|
| 538 |
f_source = parse_f_source(temp_file,error) |
| 539 |
if (allocated(error)) then |
| 540 |
return |
| 541 |
end if |
| 542 |
|
| 543 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 544 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_MODULE') |
| 545 |
return |
| 546 |
end if |
| 547 |
|
| 548 |
if (size(f_source%modules_provided) /= 1) then |
| 549 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 550 |
return |
| 551 |
end if |
| 552 |
|
| 553 |
if (size(f_source%modules_used) /= 1) then |
| 554 |
call test_failed(error,'Incorrect number of modules_used - expecting one') |
| 555 |
return |
| 556 |
end if |
| 557 |
|
| 558 |
if (.not.('my_mod' .in. f_source%modules_provided)) then |
| 559 |
call test_failed(error,'Missing module in modules_provided') |
| 560 |
return |
| 561 |
end if |
| 562 |
|
| 563 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 564 |
call test_failed(error,'Missing module in modules_used') |
| 565 |
return |
| 566 |
end if |
| 567 |
|
| 568 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 569 |
|
| 570 |
end subroutine test_module |
| 571 |
|
| 572 |
|
| 573 |
!> Try to parse fortran module with subroutine outside of module |
| 574 |
!> (this should be detected as FPM_UNIT_SUBPROGRAM not FPM_UNIT_MODULE) |
| 575 |
subroutine test_module_with_subprogram(error) |
| 576 |
|
| 577 |
!> Error handling |
| 578 |
type(error_t), allocatable, intent(out) :: error |
| 579 |
|
| 580 |
integer :: unit |
| 581 |
character(:), allocatable :: temp_file |
| 582 |
type(srcfile_t), allocatable :: f_source |
| 583 |
|
| 584 |
allocate(temp_file, source=get_temp_filename()) |
| 585 |
|
| 586 |
open(file=temp_file, newunit=unit) |
| 587 |
write(unit, '(a)') & |
| 588 |
& 'module my_mod', & |
| 589 |
& 'contains', & |
| 590 |
& 'module subroutine f()', & |
| 591 |
& 'end subroutine f', & |
| 592 |
& 'module function g()', & |
| 593 |
& 'end function g', & |
| 594 |
& 'end module test',& |
| 595 |
& 'function h()', & |
| 596 |
& 'end function' |
| 597 |
close(unit) |
| 598 |
|
| 599 |
f_source = parse_f_source(temp_file,error) |
| 600 |
if (allocated(error)) then |
| 601 |
return |
| 602 |
end if |
| 603 |
|
| 604 |
if (f_source%unit_type /= FPM_UNIT_SUBPROGRAM) then |
| 605 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBPROGRAM') |
| 606 |
return |
| 607 |
end if |
| 608 |
|
| 609 |
if (size(f_source%modules_provided) /= 1) then |
| 610 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 611 |
return |
| 612 |
end if |
| 613 |
|
| 614 |
if (size(f_source%modules_used) /= 0) then |
| 615 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 616 |
return |
| 617 |
end if |
| 618 |
|
| 619 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 620 |
|
| 621 |
end subroutine test_module_with_subprogram |
| 622 |
|
| 623 |
|
| 624 |
!> Try to parse fortran modules without the full end module statement |
| 625 |
!> This should be detected as FPM_UNIT_SUBPROGRAM not FPM_UNIT_MODULE |
| 626 |
!> because we cannot guarantee if non-module subprograms are present |
| 627 |
subroutine test_module_end_stmt(error) |
| 628 |
|
| 629 |
!> Error handling |
| 630 |
type(error_t), allocatable, intent(out) :: error |
| 631 |
|
| 632 |
integer :: unit |
| 633 |
character(:), allocatable :: temp_file |
| 634 |
type(srcfile_t), allocatable :: f_source |
| 635 |
|
| 636 |
allocate(temp_file, source=get_temp_filename()) |
| 637 |
|
| 638 |
open(file=temp_file, newunit=unit) |
| 639 |
write(unit, '(a)') & |
| 640 |
& 'module mod1', & |
| 641 |
& 'contains', & |
| 642 |
& 'module subroutine f()', & |
| 643 |
& 'end subroutine f', & |
| 644 |
& 'module function g()', & |
| 645 |
& 'end function g', & |
| 646 |
& 'end', & |
| 647 |
& 'module mod2', & |
| 648 |
& 'contains', & |
| 649 |
& 'module subroutine f()', & |
| 650 |
& 'end subroutine f', & |
| 651 |
& 'module function g()', & |
| 652 |
& 'end function g', & |
| 653 |
& 'end module mod2' |
| 654 |
close(unit) |
| 655 |
|
| 656 |
f_source = parse_f_source(temp_file,error) |
| 657 |
if (allocated(error)) then |
| 658 |
return |
| 659 |
end if |
| 660 |
|
| 661 |
if (f_source%unit_type /= FPM_UNIT_SUBPROGRAM) then |
| 662 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBPROGRAM') |
| 663 |
return |
| 664 |
end if |
| 665 |
|
| 666 |
if (size(f_source%modules_provided) /= 2) then |
| 667 |
call test_failed(error,'Unexpected modules_provided - expecting two') |
| 668 |
return |
| 669 |
end if |
| 670 |
|
| 671 |
if (size(f_source%modules_used) /= 0) then |
| 672 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 673 |
return |
| 674 |
end if |
| 675 |
|
| 676 |
if (.not.('mod1' .in. f_source%modules_provided)) then |
| 677 |
call test_failed(error,'Missing module in modules_provided') |
| 678 |
return |
| 679 |
end if |
| 680 |
|
| 681 |
if (.not.('mod2' .in. f_source%modules_provided)) then |
| 682 |
call test_failed(error,'Missing module in modules_provided') |
| 683 |
return |
| 684 |
end if |
| 685 |
|
| 686 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 687 |
|
| 688 |
end subroutine test_module_end_stmt |
| 689 |
|
| 690 |
|
| 691 |
!> Try to parse fortran module with exported C-API via bind(c) |
| 692 |
!> (this should be detected as FPM_UNIT_SUBPROGRAM not FPM_UNIT_MODULE to prevent pruning) |
| 693 |
subroutine test_module_with_c_api(error) |
| 694 |
|
| 695 |
!> Error handling |
| 696 |
type(error_t), allocatable, intent(out) :: error |
| 697 |
|
| 698 |
integer :: unit |
| 699 |
character(:), allocatable :: temp_file |
| 700 |
type(srcfile_t), allocatable :: f_source |
| 701 |
|
| 702 |
allocate(temp_file, source=get_temp_filename()) |
| 703 |
|
| 704 |
open(file=temp_file, newunit=unit) |
| 705 |
write(unit, '(a)') & |
| 706 |
& 'module my_mod', & |
| 707 |
& 'contains', & |
| 708 |
& 'subroutine f() &', & |
| 709 |
& ' bind(C)', & |
| 710 |
& 'end subroutine f', & |
| 711 |
& 'module function g()', & |
| 712 |
& 'end function g', & |
| 713 |
& 'end module test' |
| 714 |
close(unit) |
| 715 |
|
| 716 |
f_source = parse_f_source(temp_file,error) |
| 717 |
if (allocated(error)) then |
| 718 |
return |
| 719 |
end if |
| 720 |
|
| 721 |
if (f_source%unit_type /= FPM_UNIT_SUBPROGRAM) then |
| 722 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBPROGRAM') |
| 723 |
return |
| 724 |
end if |
| 725 |
|
| 726 |
if (size(f_source%modules_provided) /= 1) then |
| 727 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 728 |
return |
| 729 |
end if |
| 730 |
|
| 731 |
if (size(f_source%modules_used) /= 0) then |
| 732 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 733 |
return |
| 734 |
end if |
| 735 |
|
| 736 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 737 |
|
| 738 |
end subroutine test_module_with_c_api |
| 739 |
|
| 740 |
!> Check parsing of module exporting an abstract interface |
| 741 |
!> See also https://github.com/fortran-lang/fpm/issues/1073 |
| 742 |
subroutine test_module_with_abstract_interface(error) |
| 743 |
type(error_t), allocatable, intent(out) :: error |
| 744 |
|
| 745 |
integer :: unit |
| 746 |
character(:), allocatable :: temp_file |
| 747 |
type(srcfile_t) :: f_source |
| 748 |
|
| 749 |
allocate(temp_file,source=get_temp_filename()) |
| 750 |
open(file=temp_file,newunit=unit) |
| 751 |
write(unit, '(A)') & |
| 752 |
& 'module foo', & |
| 753 |
& 'abstract interface', & |
| 754 |
& ' subroutine bar1()', & |
| 755 |
& ' end subroutine', & |
| 756 |
& ' subroutine bar2() bind(c)', & |
| 757 |
& ' end subroutine', & |
| 758 |
& 'end interface', & |
| 759 |
& 'end module foo' |
| 760 |
close(unit) |
| 761 |
|
| 762 |
f_source = parse_f_source(temp_file,error) |
| 763 |
if (allocated(error)) return |
| 764 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 765 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_MODULE') |
| 766 |
return |
| 767 |
end if |
| 768 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 769 |
end subroutine test_module_with_abstract_interface |
| 770 |
|
| 771 |
|
| 772 |
!> Try to parse combined fortran module and program |
| 773 |
!> Check that parsed unit type is FPM_UNIT_PROGRAM |
| 774 |
subroutine test_program_with_module(error) |
| 775 |
|
| 776 |
!> Error handling |
| 777 |
type(error_t), allocatable, intent(out) :: error |
| 778 |
|
| 779 |
integer :: unit |
| 780 |
character(:), allocatable :: temp_file |
| 781 |
type(srcfile_t), allocatable :: f_source |
| 782 |
|
| 783 |
allocate(temp_file, source=get_temp_filename()) |
| 784 |
|
| 785 |
open(file=temp_file, newunit=unit) |
| 786 |
write(unit, '(a)') & |
| 787 |
& 'module my_mod', & |
| 788 |
& 'use module_one', & |
| 789 |
& 'interface', & |
| 790 |
& ' module subroutine f()', & |
| 791 |
& 'end interface', & |
| 792 |
& 'contains', & |
| 793 |
& 'module procedure f()', & |
| 794 |
& 'end procedure f', & |
| 795 |
& 'end module test', & |
| 796 |
& 'program my_program', & |
| 797 |
& 'use my_mod', & |
| 798 |
& 'implicit none', & |
| 799 |
& 'end my_program' |
| 800 |
close(unit) |
| 801 |
|
| 802 |
f_source = parse_f_source(temp_file,error) |
| 803 |
if (allocated(error)) then |
| 804 |
return |
| 805 |
end if |
| 806 |
|
| 807 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 808 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM') |
| 809 |
return |
| 810 |
end if |
| 811 |
|
| 812 |
if (size(f_source%modules_provided) /= 1) then |
| 813 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 814 |
return |
| 815 |
end if |
| 816 |
|
| 817 |
if (.not.('my_mod' .in. f_source%modules_provided)) then |
| 818 |
call test_failed(error,'Missing module in modules_provided') |
| 819 |
return |
| 820 |
end if |
| 821 |
|
| 822 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 823 |
call test_failed(error,'Missing module in modules_used') |
| 824 |
return |
| 825 |
end if |
| 826 |
|
| 827 |
if (.not.('my_mod' .in. f_source%modules_used)) then |
| 828 |
call test_failed(error,'Missing module in modules_used') |
| 829 |
return |
| 830 |
end if |
| 831 |
|
| 832 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 833 |
|
| 834 |
end subroutine test_program_with_module |
| 835 |
|
| 836 |
!> Check parsing of interfaces within program unit |
| 837 |
!> See also https://github.com/fortran-lang/fpm/issues/1073 |
| 838 |
subroutine test_program_with_abstract_interface(error) |
| 839 |
|
| 840 |
!> Error handling |
| 841 |
type(error_t), allocatable, intent(out) :: error |
| 842 |
|
| 843 |
integer :: unit |
| 844 |
character(:), allocatable :: temp_file |
| 845 |
type(srcfile_t), allocatable :: f_source |
| 846 |
|
| 847 |
allocate(temp_file, source=get_temp_filename()) |
| 848 |
|
| 849 |
open(file=temp_file, newunit=unit) |
| 850 |
write(unit, '(a)') & |
| 851 |
& 'program my_program', & |
| 852 |
& 'implicit none', & |
| 853 |
& 'abstract interface', & |
| 854 |
& ' function cmpfunc(a,b) bind(c)', & |
| 855 |
& ' use, intrinsic :: iso_c_binding', & |
| 856 |
& ' type(c_ptr), intent(in), value :: a, b', & |
| 857 |
& ' integer(c_int) :: cmpfunc', & |
| 858 |
& ' end function', & |
| 859 |
& 'end interface', & |
| 860 |
& 'interface', & |
| 861 |
& ' subroutine qsort(ptr,count,size,comp) bind(c,name="qsort")', & |
| 862 |
& ' use, intrinsic :: iso_c_binding', & |
| 863 |
& ' type(c_ptr), value :: ptr', & |
| 864 |
& ' integer(c_size_t), value :: count, size', & |
| 865 |
& ' type(c_funptr), value :: comp', & |
| 866 |
& 'end interface', & |
| 867 |
& 'end program my_program' |
| 868 |
close(unit) |
| 869 |
|
| 870 |
f_source = parse_f_source(temp_file,error) |
| 871 |
if (allocated(error)) then |
| 872 |
return |
| 873 |
end if |
| 874 |
|
| 875 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 876 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_PROGRAM') |
| 877 |
return |
| 878 |
end if |
| 879 |
|
| 880 |
if (size(f_source%modules_provided) /= 0) then |
| 881 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 882 |
return |
| 883 |
end if |
| 884 |
|
| 885 |
! Intrinsic modules are not counted in `modules_used` (!) |
| 886 |
if (size(f_source%modules_used) /= 0) then |
| 887 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 888 |
return |
| 889 |
end if |
| 890 |
|
| 891 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 892 |
|
| 893 |
end subroutine test_program_with_abstract_interface |
| 894 |
|
| 895 |
!> Try to parse fortran submodule for ancestry |
| 896 |
subroutine test_submodule(error) |
| 897 |
|
| 898 |
!> Error handling |
| 899 |
type(error_t), allocatable, intent(out) :: error |
| 900 |
|
| 901 |
integer :: unit |
| 902 |
character(:), allocatable :: temp_file |
| 903 |
type(srcfile_t), allocatable :: f_source |
| 904 |
|
| 905 |
allocate(temp_file, source=get_temp_filename()) |
| 906 |
|
| 907 |
open(file=temp_file, newunit=unit) |
| 908 |
write(unit, '(a)') & |
| 909 |
& 'submodule (parent) child', & |
| 910 |
& 'use module_one', & |
| 911 |
& 'end submodule test' |
| 912 |
close(unit) |
| 913 |
|
| 914 |
f_source = parse_f_source(temp_file,error) |
| 915 |
if (allocated(error)) then |
| 916 |
return |
| 917 |
end if |
| 918 |
|
| 919 |
if (f_source%unit_type /= FPM_UNIT_SUBMODULE) then |
| 920 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBMODULE') |
| 921 |
return |
| 922 |
end if |
| 923 |
|
| 924 |
if (size(f_source%modules_provided) /= 1) then |
| 925 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 926 |
return |
| 927 |
end if |
| 928 |
|
| 929 |
if (size(f_source%modules_used) /= 2) then |
| 930 |
call test_failed(error,'Incorrect number of modules_used - expecting two') |
| 931 |
return |
| 932 |
end if |
| 933 |
|
| 934 |
if (.not.('child' .in. f_source%modules_provided)) then |
| 935 |
call test_failed(error,'Missing module in modules_provided') |
| 936 |
return |
| 937 |
end if |
| 938 |
|
| 939 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 940 |
call test_failed(error,'Missing module in modules_used') |
| 941 |
return |
| 942 |
end if |
| 943 |
|
| 944 |
if (.not.('parent' .in. f_source%modules_used)) then |
| 945 |
call test_failed(error,'Missing parent module in modules_used') |
| 946 |
return |
| 947 |
end if |
| 948 |
|
| 949 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 950 |
|
| 951 |
end subroutine test_submodule |
| 952 |
|
| 953 |
|
| 954 |
!> Try to parse fortran multi-level submodule for ancestry |
| 955 |
subroutine test_submodule_ancestor(error) |
| 956 |
|
| 957 |
!> Error handling |
| 958 |
type(error_t), allocatable, intent(out) :: error |
| 959 |
|
| 960 |
integer :: unit |
| 961 |
character(:), allocatable :: temp_file |
| 962 |
type(srcfile_t), allocatable :: f_source |
| 963 |
|
| 964 |
allocate(temp_file, source=get_temp_filename()) |
| 965 |
|
| 966 |
open(file=temp_file, newunit=unit) |
| 967 |
write(unit, '(a)') & |
| 968 |
& 'submodule (ancestor:parent) child', & |
| 969 |
& 'use module_one', & |
| 970 |
& 'end submodule test' |
| 971 |
close(unit) |
| 972 |
|
| 973 |
f_source = parse_f_source(temp_file,error) |
| 974 |
if (allocated(error)) then |
| 975 |
return |
| 976 |
end if |
| 977 |
|
| 978 |
if (f_source%unit_type /= FPM_UNIT_SUBMODULE) then |
| 979 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBMODULE') |
| 980 |
return |
| 981 |
end if |
| 982 |
|
| 983 |
if (size(f_source%modules_provided) /= 1) then |
| 984 |
call test_failed(error,'Unexpected modules_provided - expecting one') |
| 985 |
return |
| 986 |
end if |
| 987 |
|
| 988 |
if (size(f_source%modules_used) /= 2) then |
| 989 |
call test_failed(error,'Incorrect number of modules_used - expecting two') |
| 990 |
return |
| 991 |
end if |
| 992 |
|
| 993 |
if (.not.('child' .in. f_source%modules_provided)) then |
| 994 |
call test_failed(error,'Missing module in modules_provided') |
| 995 |
return |
| 996 |
end if |
| 997 |
|
| 998 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 999 |
call test_failed(error,'Missing module in modules_used') |
| 1000 |
return |
| 1001 |
end if |
| 1002 |
|
| 1003 |
if (.not.('parent' .in. f_source%modules_used)) then |
| 1004 |
call test_failed(error,'Missing parent module in modules_used') |
| 1005 |
return |
| 1006 |
end if |
| 1007 |
|
| 1008 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 1009 |
|
| 1010 |
end subroutine test_submodule_ancestor |
| 1011 |
|
| 1012 |
|
| 1013 |
!> Try to parse standard fortran sub-program (non-module) source |
| 1014 |
subroutine test_subprogram(error) |
| 1015 |
|
| 1016 |
!> Error handling |
| 1017 |
type(error_t), allocatable, intent(out) :: error |
| 1018 |
|
| 1019 |
integer :: unit |
| 1020 |
character(:), allocatable :: temp_file |
| 1021 |
type(srcfile_t), allocatable :: f_source |
| 1022 |
|
| 1023 |
allocate(temp_file, source=get_temp_filename()) |
| 1024 |
|
| 1025 |
open(file=temp_file, newunit=unit) |
| 1026 |
write(unit, '(a)') & |
| 1027 |
& 'subroutine my_sub(a)', & |
| 1028 |
& ' use module_one', & |
| 1029 |
& ' integer, intent(in) :: a', & |
| 1030 |
& 'end subroutine my_sub' |
| 1031 |
close(unit) |
| 1032 |
|
| 1033 |
f_source = parse_f_source(temp_file,error) |
| 1034 |
if (allocated(error)) then |
| 1035 |
return |
| 1036 |
end if |
| 1037 |
|
| 1038 |
if (f_source%unit_type /= FPM_UNIT_SUBPROGRAM) then |
| 1039 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_SUBPROGRAM') |
| 1040 |
return |
| 1041 |
end if |
| 1042 |
|
| 1043 |
if (size(f_source%modules_provided) /= 0) then |
| 1044 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 1045 |
return |
| 1046 |
end if |
| 1047 |
|
| 1048 |
if (size(f_source%modules_used) /= 1) then |
| 1049 |
call test_failed(error,'Incorrect number of modules_used - expecting one') |
| 1050 |
return |
| 1051 |
end if |
| 1052 |
|
| 1053 |
if (.not.('module_one' .in. f_source%modules_used)) then |
| 1054 |
call test_failed(error,'Missing module in modules_used') |
| 1055 |
return |
| 1056 |
end if |
| 1057 |
|
| 1058 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 1059 |
|
| 1060 |
end subroutine test_subprogram |
| 1061 |
|
| 1062 |
|
| 1063 |
!> Try to parse standard c source for includes |
| 1064 |
subroutine test_csource(error) |
| 1065 |
|
| 1066 |
!> Error handling |
| 1067 |
type(error_t), allocatable, intent(out) :: error |
| 1068 |
|
| 1069 |
integer :: unit |
| 1070 |
character(:), allocatable :: temp_file |
| 1071 |
type(srcfile_t), allocatable :: f_source |
| 1072 |
|
| 1073 |
allocate(temp_file, source=get_temp_filename()) |
| 1074 |
temp_file = temp_file//'.c' |
| 1075 |
|
| 1076 |
open(file=temp_file, newunit=unit) |
| 1077 |
write(unit, '(a)') & |
| 1078 |
& '#include "proto.h"', & |
| 1079 |
& 'void c_func(int a) {', & |
| 1080 |
& ' #include "function_body.c"', & |
| 1081 |
& ' return', & |
| 1082 |
& '}' |
| 1083 |
close(unit) |
| 1084 |
|
| 1085 |
f_source = parse_c_source(temp_file,error) |
| 1086 |
if (allocated(error)) then |
| 1087 |
return |
| 1088 |
end if |
| 1089 |
|
| 1090 |
if (f_source%unit_type /= FPM_UNIT_CSOURCE) then |
| 1091 |
call test_failed(error,'Wrong unit type detected - expecting FPM_UNIT_CSOURCE') |
| 1092 |
return |
| 1093 |
end if |
| 1094 |
|
| 1095 |
if (size(f_source%modules_provided) /= 0) then |
| 1096 |
call test_failed(error,'Unexpected modules_provided - expecting zero') |
| 1097 |
return |
| 1098 |
end if |
| 1099 |
|
| 1100 |
if (size(f_source%modules_used) /= 0) then |
| 1101 |
call test_failed(error,'Incorrect number of modules_used - expecting zero') |
| 1102 |
return |
| 1103 |
end if |
| 1104 |
|
| 1105 |
if (size(f_source%include_dependencies) /= 2) then |
| 1106 |
call test_failed(error,'Incorrect number of include_dependencies - expecting two') |
| 1107 |
return |
| 1108 |
end if |
| 1109 |
|
| 1110 |
if (allocated(f_source%link_libraries)) then |
| 1111 |
call test_failed(error,'Unexpected link_libraries - expecting unallocated') |
| 1112 |
return |
| 1113 |
end if |
| 1114 |
|
| 1115 |
if (size(f_source%parent_modules) /= 0) then |
| 1116 |
call test_failed(error,'Incorrect number of parent_modules - expecting zero') |
| 1117 |
return |
| 1118 |
end if |
| 1119 |
|
| 1120 |
if (.not.('proto.h' .in. f_source%include_dependencies)) then |
| 1121 |
call test_failed(error,'Missing file in include_dependencies') |
| 1122 |
return |
| 1123 |
end if |
| 1124 |
|
| 1125 |
if (.not.('function_body.c' .in. f_source%include_dependencies)) then |
| 1126 |
call test_failed(error,'Missing file in include_dependencies') |
| 1127 |
return |
| 1128 |
end if |
| 1129 |
|
| 1130 |
call f_source%test_serialization('srcfile_t: serialization', error) |
| 1131 |
|
| 1132 |
end subroutine test_csource |
| 1133 |
|
| 1134 |
!> Try to parse fortran program with invalid use statement |
| 1135 |
subroutine test_invalid_use_stmt(error) |
| 1136 |
|
| 1137 |
!> Error handling |
| 1138 |
type(error_t), allocatable, intent(out) :: error |
| 1139 |
|
| 1140 |
integer :: unit |
| 1141 |
character(:), allocatable :: temp_file |
| 1142 |
type(srcfile_t), allocatable :: f_source |
| 1143 |
|
| 1144 |
allocate(temp_file, source=get_temp_filename()) |
| 1145 |
|
| 1146 |
open(file=temp_file, newunit=unit) |
| 1147 |
write(unit, '(a)') & |
| 1148 |
& 'program test', & |
| 1149 |
& 'use module_one', & |
| 1150 |
& 'use :: ', & |
| 1151 |
& 'end program test' |
| 1152 |
close(unit) |
| 1153 |
|
| 1154 |
f_source = parse_f_source(temp_file,error) |
| 1155 |
if (allocated(error)) then |
| 1156 |
return |
| 1157 |
end if |
| 1158 |
|
| 1159 |
end subroutine test_invalid_use_stmt |
| 1160 |
|
| 1161 |
|
| 1162 |
!> Try to parse fortran program with invalid use statement |
| 1163 |
subroutine test_invalid_include_stmt(error) |
| 1164 |
|
| 1165 |
!> Error handling |
| 1166 |
type(error_t), allocatable, intent(out) :: error |
| 1167 |
|
| 1168 |
integer :: unit |
| 1169 |
character(:), allocatable :: temp_file |
| 1170 |
type(srcfile_t), allocatable :: f_source |
| 1171 |
|
| 1172 |
allocate(temp_file, source=get_temp_filename()) |
| 1173 |
|
| 1174 |
open(file=temp_file, newunit=unit) |
| 1175 |
write(unit, '(a)') & |
| 1176 |
& 'program test', & |
| 1177 |
& ' include "', & |
| 1178 |
& 'end program test' |
| 1179 |
close(unit) |
| 1180 |
|
| 1181 |
f_source = parse_f_source(temp_file,error) |
| 1182 |
if (allocated(error)) then |
| 1183 |
return |
| 1184 |
end if |
| 1185 |
|
| 1186 |
end subroutine test_invalid_include_stmt |
| 1187 |
|
| 1188 |
|
| 1189 |
!> Try to parse incorrect fortran module syntax |
| 1190 |
subroutine test_invalid_module(error) |
| 1191 |
|
| 1192 |
!> Error handling |
| 1193 |
type(error_t), allocatable, intent(out) :: error |
| 1194 |
|
| 1195 |
integer :: unit |
| 1196 |
character(:), allocatable :: temp_file |
| 1197 |
type(srcfile_t), allocatable :: f_source |
| 1198 |
|
| 1199 |
allocate(temp_file, source=get_temp_filename()) |
| 1200 |
|
| 1201 |
open(file=temp_file, newunit=unit) |
| 1202 |
write(unit, '(a)') & |
| 1203 |
& 'module ::my_mod', & |
| 1204 |
& 'end module test' |
| 1205 |
close(unit) |
| 1206 |
|
| 1207 |
f_source = parse_f_source(temp_file,error) |
| 1208 |
if (allocated(error)) then |
| 1209 |
return |
| 1210 |
end if |
| 1211 |
|
| 1212 |
end subroutine test_invalid_module |
| 1213 |
|
| 1214 |
|
| 1215 |
!> Try to parse incorrect fortran submodule syntax |
| 1216 |
subroutine test_invalid_submodule(error) |
| 1217 |
|
| 1218 |
!> Error handling |
| 1219 |
type(error_t), allocatable, intent(out) :: error |
| 1220 |
|
| 1221 |
integer :: unit |
| 1222 |
character(:), allocatable :: temp_file |
| 1223 |
type(srcfile_t), allocatable :: f_source |
| 1224 |
|
| 1225 |
allocate(temp_file, source=get_temp_filename()) |
| 1226 |
|
| 1227 |
open(file=temp_file, newunit=unit) |
| 1228 |
write(unit, '(a)') & |
| 1229 |
& 'submodule :: child', & |
| 1230 |
& 'end submodule test' |
| 1231 |
close(unit) |
| 1232 |
|
| 1233 |
f_source = parse_f_source(temp_file,error) |
| 1234 |
if (allocated(error)) then |
| 1235 |
return |
| 1236 |
end if |
| 1237 |
|
| 1238 |
write(*,*) '"',f_source%modules_used(1)%s,'"' |
| 1239 |
|
| 1240 |
end subroutine test_invalid_submodule |
| 1241 |
|
| 1242 |
!> Parse several USE statements |
| 1243 |
subroutine test_use_statement(error) |
| 1244 |
|
| 1245 |
!> Error handling |
| 1246 |
type(error_t), allocatable, intent(out) :: error |
| 1247 |
|
| 1248 |
character(*), parameter :: filename='test_use_statement' |
| 1249 |
character(:), allocatable :: line,module_name |
| 1250 |
|
| 1251 |
logical :: used,is_intrinsic |
| 1252 |
|
| 1253 |
line = 'use, intrinsic:: iso_fortran_env' |
| 1254 |
call parse_use_statement(filename,0,line,used,is_intrinsic,module_name,error) |
| 1255 |
if (allocated(error)) return |
| 1256 |
|
| 1257 |
if (.not. (used .and. & |
| 1258 |
is_intrinsic .and. & |
| 1259 |
module_name=='iso_fortran_env' .and. & |
| 1260 |
used)) then |
| 1261 |
call fatal_error(error,'USE statement failed parsing <'//line//'>') |
| 1262 |
return |
| 1263 |
endif |
| 1264 |
|
| 1265 |
line = 'use, non_intrinsic :: iso_fortran_env' |
| 1266 |
call parse_use_statement(filename,0,line,used,is_intrinsic,module_name,error) |
| 1267 |
if (allocated(error)) return |
| 1268 |
|
| 1269 |
if (.not. (used .and. & |
| 1270 |
(.not.is_intrinsic) .and. & |
| 1271 |
module_name=='iso_fortran_env' .and. & |
| 1272 |
used)) then |
| 1273 |
call fatal_error(error,'USE statement failed parsing <'//line//'>') |
| 1274 |
return |
| 1275 |
endif |
| 1276 |
|
| 1277 |
line = 'use, non_intrinsic :: my_fortran_module' |
| 1278 |
call parse_use_statement(filename,0,line,used,is_intrinsic,module_name,error) |
| 1279 |
if (allocated(error)) return |
| 1280 |
|
| 1281 |
if (.not. (used .and. & |
| 1282 |
(.not.is_intrinsic) .and. & |
| 1283 |
module_name=='my_fortran_module' .and. & |
| 1284 |
used)) then |
| 1285 |
call fatal_error(error,'USE statement failed parsing <'//line//'>') |
| 1286 |
return |
| 1287 |
endif |
| 1288 |
|
| 1289 |
line = 'use, intrinsic :: my_fortran_module' |
| 1290 |
call parse_use_statement(filename,0,line,used,is_intrinsic,module_name,error) |
| 1291 |
|
| 1292 |
! This is not an intrinsic module: should detect an error |
| 1293 |
if (.not. allocated(error)) then |
| 1294 |
call fatal_error(error,'Did not catch invalid intrinsic module in <'//line//'>') |
| 1295 |
return |
| 1296 |
else |
| 1297 |
deallocate(error) |
| 1298 |
endif |
| 1299 |
|
| 1300 |
end subroutine test_use_statement |
| 1301 |
|
| 1302 |
!> Test conditional compilation parsing with CPP preprocessing |
| 1303 |
subroutine test_conditional_compilation(error) |
| 1304 |
|
| 1305 |
!> Error handling |
| 1306 |
type(error_t), allocatable, intent(out) :: error |
| 1307 |
|
| 1308 |
type(srcfile_t) :: f_source |
| 1309 |
character(:), allocatable :: temp_file |
| 1310 |
integer :: unit |
| 1311 |
type(preprocess_config_t) :: cpp_config |
| 1312 |
|
| 1313 |
! Test 1: Without preprocessing, should include dependencies from #ifdef blocks |
| 1314 |
temp_file = get_temp_filename() |
| 1315 |
|
| 1316 |
open(file=temp_file, newunit=unit) |
| 1317 |
write(unit, '(a)') & |
| 1318 |
'module test_mod', & |
| 1319 |
'#ifdef SOME_FEATURE', & |
| 1320 |
' use nonexistent_module', & |
| 1321 |
'#endif', & |
| 1322 |
' implicit none', & |
| 1323 |
'contains', & |
| 1324 |
' subroutine test_sub()', & |
| 1325 |
' print *, "test"', & |
| 1326 |
' end subroutine', & |
| 1327 |
'end module test_mod' |
| 1328 |
close(unit) |
| 1329 |
|
| 1330 |
! Parse without preprocessing - should detect the use statement |
| 1331 |
f_source = parse_f_source(temp_file, error) |
| 1332 |
if (allocated(error)) return |
| 1333 |
|
| 1334 |
if (size(f_source%modules_used) /= 1) then |
| 1335 |
call test_failed(error, 'Expected 1 module dependency without preprocessing, got different count') |
| 1336 |
return |
| 1337 |
end if |
| 1338 |
|
| 1339 |
if (f_source%modules_used(1)%s /= 'nonexistent_module') then |
| 1340 |
call test_failed(error, 'Expected nonexistent_module, got: '//f_source%modules_used(1)%s) |
| 1341 |
return |
| 1342 |
end if |
| 1343 |
|
| 1344 |
! Test 2: With preprocessing enabled, should skip dependencies from #ifdef blocks |
| 1345 |
call cpp_config%new([string_t::]) |
| 1346 |
cpp_config%name = "cpp" |
| 1347 |
|
| 1348 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1349 |
if (allocated(error)) return |
| 1350 |
|
| 1351 |
if (size(f_source%modules_used) /= 0) then |
| 1352 |
call test_failed(error, 'Expected 0 module dependencies with preprocessing, got some dependencies') |
| 1353 |
return |
| 1354 |
end if |
| 1355 |
|
| 1356 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 1357 |
call test_failed(error, 'Expected module unit type') |
| 1358 |
return |
| 1359 |
end if |
| 1360 |
|
| 1361 |
if (size(f_source%modules_provided) /= 1) then |
| 1362 |
call test_failed(error, 'Expected 1 provided module') |
| 1363 |
return |
| 1364 |
end if |
| 1365 |
|
| 1366 |
if (f_source%modules_provided(1)%s /= 'test_mod') then |
| 1367 |
call test_failed(error, 'Expected test_mod, got: '//f_source%modules_provided(1)%s) |
| 1368 |
return |
| 1369 |
end if |
| 1370 |
|
| 1371 |
end subroutine test_conditional_compilation |
| 1372 |
!> Test conditional compilation parsing with CPP preprocessing |
| 1373 |
subroutine test_conditional_compilation_elif(error) |
| 1374 |
|
| 1375 |
!> Error handling |
| 1376 |
type(error_t), allocatable, intent(out) :: error |
| 1377 |
|
| 1378 |
type(srcfile_t) :: f_source |
| 1379 |
character(:), allocatable :: temp_file |
| 1380 |
integer :: unit |
| 1381 |
type(preprocess_config_t) :: cpp_config |
| 1382 |
|
| 1383 |
! Test 1: Without preprocessing, should include dependencies from #ifdef blocks |
| 1384 |
temp_file = get_temp_filename() |
| 1385 |
|
| 1386 |
open(file=temp_file, newunit=unit) |
| 1387 |
write(unit, '(a)') & |
| 1388 |
'module test_mod', & |
| 1389 |
'#ifdef SOME_FEATURE', & |
| 1390 |
' use nonexistent_module', & |
| 1391 |
'#elif defined(ANOTHER_FEATURE)', & |
| 1392 |
' use another_nonexistent_module', & |
| 1393 |
'#endif', & |
| 1394 |
' implicit none', & |
| 1395 |
'contains', & |
| 1396 |
' subroutine test_sub()', & |
| 1397 |
' print *, "test"', & |
| 1398 |
' end subroutine', & |
| 1399 |
'end module test_mod' |
| 1400 |
close(unit) |
| 1401 |
|
| 1402 |
! Parse without preprocessing - should detect the use statement |
| 1403 |
f_source = parse_f_source(temp_file, error) |
| 1404 |
if (allocated(error)) return |
| 1405 |
|
| 1406 |
if (size(f_source%modules_used) /= 2) then |
| 1407 |
call test_failed(error, 'Expected 2 module dependency without preprocessing, got different count') |
| 1408 |
return |
| 1409 |
end if |
| 1410 |
|
| 1411 |
if (f_source%modules_used(1)%s /= 'nonexistent_module') then |
| 1412 |
call test_failed(error, 'Expected nonexistent_module, got: '//f_source%modules_used(1)%s) |
| 1413 |
return |
| 1414 |
end if |
| 1415 |
|
| 1416 |
if (f_source%modules_used(2)%s /= 'another_nonexistent_module') then |
| 1417 |
call test_failed(error, 'Expected another_nonexistent_module, got: '//f_source%modules_used(2)%s) |
| 1418 |
return |
| 1419 |
end if |
| 1420 |
|
| 1421 |
! Test 2: With preprocessing enabled, should skip dependencies from #ifdef blocks |
| 1422 |
call cpp_config%new([string_t::]) |
| 1423 |
cpp_config%name = "cpp" |
| 1424 |
|
| 1425 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1426 |
if (allocated(error)) return |
| 1427 |
|
| 1428 |
if (size(f_source%modules_used) /= 0) then |
| 1429 |
call test_failed(error, 'Expected 0 module dependencies with preprocessing, got some dependencies') |
| 1430 |
return |
| 1431 |
end if |
| 1432 |
|
| 1433 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 1434 |
call test_failed(error, 'Expected module unit type') |
| 1435 |
return |
| 1436 |
end if |
| 1437 |
|
| 1438 |
if (size(f_source%modules_provided) /= 1) then |
| 1439 |
call test_failed(error, 'Expected 1 provided module') |
| 1440 |
return |
| 1441 |
end if |
| 1442 |
|
| 1443 |
if (f_source%modules_provided(1)%s /= 'test_mod') then |
| 1444 |
call test_failed(error, 'Expected test_mod, got: '//f_source%modules_provided(1)%s) |
| 1445 |
return |
| 1446 |
end if |
| 1447 |
|
| 1448 |
end subroutine test_conditional_compilation_elif |
| 1449 |
!> Test conditional compilation parsing with CPP preprocessing |
| 1450 |
subroutine test_conditional_compilation_elif_else(error) |
| 1451 |
|
| 1452 |
!> Error handling |
| 1453 |
type(error_t), allocatable, intent(out) :: error |
| 1454 |
|
| 1455 |
type(srcfile_t) :: f_source |
| 1456 |
character(:), allocatable :: temp_file |
| 1457 |
integer :: unit |
| 1458 |
type(preprocess_config_t) :: cpp_config |
| 1459 |
|
| 1460 |
! Test 1: Without preprocessing, should include dependencies from #ifdef blocks |
| 1461 |
temp_file = get_temp_filename() |
| 1462 |
|
| 1463 |
open(file=temp_file, newunit=unit) |
| 1464 |
write(unit, '(a)') & |
| 1465 |
'module test_mod', & |
| 1466 |
'#ifdef SOME_FEATURE', & |
| 1467 |
' use nonexistent_module', & |
| 1468 |
'#elif defined(ANOTHER_FEATURE)', & |
| 1469 |
' use another_nonexistent_module', & |
| 1470 |
'#else',& |
| 1471 |
' use a_third_module',& |
| 1472 |
'#endif', & |
| 1473 |
' implicit none', & |
| 1474 |
'contains', & |
| 1475 |
' subroutine test_sub()', & |
| 1476 |
' print *, "test"', & |
| 1477 |
' end subroutine', & |
| 1478 |
'end module test_mod' |
| 1479 |
close(unit) |
| 1480 |
|
| 1481 |
! Parse without preprocessing - should detect the use statement |
| 1482 |
f_source = parse_f_source(temp_file, error) |
| 1483 |
if (allocated(error)) return |
| 1484 |
|
| 1485 |
if (size(f_source%modules_used) /= 3) then |
| 1486 |
call test_failed(error, 'Expected 3 module dependency without preprocessing, got different count') |
| 1487 |
return |
| 1488 |
end if |
| 1489 |
|
| 1490 |
if (f_source%modules_used(1)%s /= 'nonexistent_module') then |
| 1491 |
call test_failed(error, 'Expected nonexistent_module, got: '//f_source%modules_used(1)%s) |
| 1492 |
return |
| 1493 |
end if |
| 1494 |
if (f_source%modules_used(2)%s /= 'another_nonexistent_module') then |
| 1495 |
call test_failed(error, 'Expected another_nonexistent_module, got: '//f_source%modules_used(2)%s) |
| 1496 |
return |
| 1497 |
end if |
| 1498 |
if (f_source%modules_used(3)%s /= 'a_third_module') then |
| 1499 |
call test_failed(error, 'Expected a_third_module, got: '//f_source%modules_used(3)%s) |
| 1500 |
return |
| 1501 |
end if |
| 1502 |
|
| 1503 |
! Test 2: With preprocessing enabled, should skip dependencies from #ifdef blocks |
| 1504 |
call cpp_config%new([string_t::]) |
| 1505 |
cpp_config%name = "cpp" |
| 1506 |
|
| 1507 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1508 |
if (allocated(error)) return |
| 1509 |
|
| 1510 |
if (size(f_source%modules_used) /= 1) then |
| 1511 |
call test_failed(error, 'Expected 1 module dependencies with preprocessing, got some dependencies') |
| 1512 |
return |
| 1513 |
end if |
| 1514 |
|
| 1515 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 1516 |
call test_failed(error, 'Expected module unit type') |
| 1517 |
return |
| 1518 |
end if |
| 1519 |
|
| 1520 |
if (size(f_source%modules_provided) /= 1) then |
| 1521 |
call test_failed(error, 'Expected 1 provided module') |
| 1522 |
return |
| 1523 |
end if |
| 1524 |
|
| 1525 |
if (f_source%modules_provided(1)%s /= 'test_mod') then |
| 1526 |
call test_failed(error, 'Expected test_mod, got: '//f_source%modules_provided(1)%s) |
| 1527 |
return |
| 1528 |
end if |
| 1529 |
|
| 1530 |
end subroutine test_conditional_compilation_elif_else |
| 1531 |
|
| 1532 |
subroutine test_conditional_compilation_ifdef_else(error) |
| 1533 |
|
| 1534 |
!> Error handling |
| 1535 |
type(error_t), allocatable, intent(out) :: error |
| 1536 |
|
| 1537 |
type(srcfile_t) :: f_source |
| 1538 |
character(:), allocatable :: temp_file |
| 1539 |
integer :: unit |
| 1540 |
type(preprocess_config_t) :: cpp_config |
| 1541 |
|
| 1542 |
! Test 1: Without preprocessing, should include dependencies from #ifdef blocks |
| 1543 |
temp_file = get_temp_filename() |
| 1544 |
|
| 1545 |
open(file=temp_file, newunit=unit) |
| 1546 |
write(unit, '(a)') & |
| 1547 |
'module test_mod', & |
| 1548 |
'#ifdef SOME_FEATURE', & |
| 1549 |
' use nonexistent_module', & |
| 1550 |
'#else',& |
| 1551 |
' use a_second_module',& |
| 1552 |
'#endif', & |
| 1553 |
' implicit none', & |
| 1554 |
'contains', & |
| 1555 |
' subroutine test_sub()', & |
| 1556 |
' print *, "test"', & |
| 1557 |
' end subroutine', & |
| 1558 |
'end module test_mod' |
| 1559 |
close(unit) |
| 1560 |
|
| 1561 |
! Parse without preprocessing - should detect the use statement |
| 1562 |
f_source = parse_f_source(temp_file, error) |
| 1563 |
if (allocated(error)) return |
| 1564 |
|
| 1565 |
if (size(f_source%modules_used) /= 2) then |
| 1566 |
call test_failed(error, 'Expected 2 module dependency without preprocessing, got different count') |
| 1567 |
return |
| 1568 |
end if |
| 1569 |
|
| 1570 |
if (f_source%modules_used(1)%s /= 'nonexistent_module') then |
| 1571 |
call test_failed(error, 'Expected nonexistent_module, got: '//f_source%modules_used(1)%s) |
| 1572 |
return |
| 1573 |
end if |
| 1574 |
|
| 1575 |
if (f_source%modules_used(2)%s /= 'a_second_module') then |
| 1576 |
call test_failed(error, 'Expected a_second_module, got: '//f_source%modules_used(2)%s) |
| 1577 |
return |
| 1578 |
end if |
| 1579 |
|
| 1580 |
! Test 2: With preprocessing enabled, should skip dependencies from #ifdef blocks |
| 1581 |
call cpp_config%new([string_t::]) |
| 1582 |
cpp_config%name = "cpp" |
| 1583 |
|
| 1584 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1585 |
if (allocated(error)) return |
| 1586 |
|
| 1587 |
if (size(f_source%modules_used) /= 1) then |
| 1588 |
call test_failed(error, 'Expected 1 module dependencies with preprocessing, got some dependencies') |
| 1589 |
return |
| 1590 |
end if |
| 1591 |
|
| 1592 |
if (f_source%unit_type /= FPM_UNIT_MODULE) then |
| 1593 |
call test_failed(error, 'Expected module unit type') |
| 1594 |
return |
| 1595 |
end if |
| 1596 |
|
| 1597 |
if (size(f_source%modules_provided) /= 1) then |
| 1598 |
call test_failed(error, 'Expected 1 provided module') |
| 1599 |
return |
| 1600 |
end if |
| 1601 |
|
| 1602 |
if (f_source%modules_provided(1)%s /= 'test_mod') then |
| 1603 |
call test_failed(error, 'Expected test_mod, got: '//f_source%modules_provided(1)%s) |
| 1604 |
return |
| 1605 |
end if |
| 1606 |
|
| 1607 |
end subroutine test_conditional_compilation_ifdef_else |
| 1608 |
|
| 1609 |
!> Test conditional compilation parsing with #if defined() syntax |
| 1610 |
subroutine test_conditional_if_defined(error) |
| 1611 |
|
| 1612 |
!> Error handling |
| 1613 |
type(error_t), allocatable, intent(out) :: error |
| 1614 |
|
| 1615 |
type(srcfile_t) :: f_source |
| 1616 |
character(:), allocatable :: temp_file |
| 1617 |
integer :: unit |
| 1618 |
type(preprocess_config_t) :: cpp_config |
| 1619 |
|
| 1620 |
temp_file = get_temp_filename() |
| 1621 |
|
| 1622 |
open(file=temp_file, newunit=unit) |
| 1623 |
write(unit, '(a)') & |
| 1624 |
'module test_mod', & |
| 1625 |
'#if defined(SOME_FEATURE)', & |
| 1626 |
' use some_module', & |
| 1627 |
'#endif', & |
| 1628 |
'#if !defined(OTHER_FEATURE)', & |
| 1629 |
' use other_module', & |
| 1630 |
'#endif', & |
| 1631 |
'#if SIMPLE_MACRO', & |
| 1632 |
' use third_module', & |
| 1633 |
'#endif', & |
| 1634 |
'#ifdef FOURTH_FEATURE', & |
| 1635 |
' use fourth_module', & |
| 1636 |
'#endif', & |
| 1637 |
'#ifndef FIFTH_FEATURE', & |
| 1638 |
' use fifth_module', & |
| 1639 |
' #ifdef NESTED_FEATURE', & |
| 1640 |
' use nested_module', & |
| 1641 |
' #endif', & |
| 1642 |
'#endif', & |
| 1643 |
' implicit none', & |
| 1644 |
'end module test_mod' |
| 1645 |
close(unit) |
| 1646 |
|
| 1647 |
! Without preprocessing - should detect all dependencies (including nested) |
| 1648 |
f_source = parse_f_source(temp_file, error) |
| 1649 |
if (allocated(error)) return |
| 1650 |
|
| 1651 |
if (size(f_source%modules_used) /= 6) then |
| 1652 |
call test_failed(error, 'Expected 6 module dependencies without preprocessing') |
| 1653 |
return |
| 1654 |
end if |
| 1655 |
|
| 1656 |
! With preprocessing - should skip all dependencies (no macros defined) |
| 1657 |
call cpp_config%new([string_t::]) |
| 1658 |
cpp_config%name = "cpp" |
| 1659 |
|
| 1660 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1661 |
if (allocated(error)) return |
| 1662 |
|
| 1663 |
! Should find 2 dependencies: !defined(OTHER_FEATURE) and #ifndef FIFTH_FEATURE are both true |
| 1664 |
! The nested #ifdef NESTED_FEATURE should be inactive since NESTED_FEATURE is not defined |
| 1665 |
if (size(f_source%modules_used) /= 2) then |
| 1666 |
if (size(f_source%modules_used) > 0) then |
| 1667 |
call test_failed(error, 'Expected 2 module dependencies with preprocessing, got ' // & |
| 1668 |
f_source%modules_used(1)%s // ' (and others)') |
| 1669 |
else |
| 1670 |
call test_failed(error, 'Expected 2 module dependencies with preprocessing, got 0') |
| 1671 |
end if |
| 1672 |
return |
| 1673 |
end if |
| 1674 |
|
| 1675 |
! Test with some macros defined - should find active dependencies |
| 1676 |
call cpp_config%new([string_t('SOME_FEATURE'), string_t('SIMPLE_MACRO')]) |
| 1677 |
cpp_config%name = "cpp" |
| 1678 |
|
| 1679 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1680 |
if (allocated(error)) return |
| 1681 |
|
| 1682 |
if (.not.('some_module' .in. f_source%modules_used)) then ! some_module |
| 1683 |
call test_failed(error, 'Expected "some_module" dependency with SOME_FEATURE and SIMPLE_MACRO defined') |
| 1684 |
return |
| 1685 |
end if |
| 1686 |
if (.not.('other_module' .in. f_source%modules_used)) then ! other_module |
| 1687 |
call test_failed(error, 'Expected "other_module" dependency with SOME_FEATURE and SIMPLE_MACRO defined') |
| 1688 |
return |
| 1689 |
end if |
| 1690 |
if (.not.('third_module' .in. f_source%modules_used)) then ! third_module |
| 1691 |
call test_failed(error, 'Expected "third_module" dependency with SOME_FEATURE and SIMPLE_MACRO defined') |
| 1692 |
return |
| 1693 |
end if |
| 1694 |
if (.not.('fifth_module' .in. f_source%modules_used)) then ! fifth_module |
| 1695 |
call test_failed(error, 'Expected "fifth_module" dependency with SOME_FEATURE and SIMPLE_MACRO defined') |
| 1696 |
return |
| 1697 |
end if |
| 1698 |
|
| 1699 |
if (size(f_source%modules_used) /= 4) then ! all modules |
| 1700 |
call test_failed(error, 'Expected 4 module dependencies with SOME_FEATURE and SIMPLE_MACRO defined') |
| 1701 |
return |
| 1702 |
end if |
| 1703 |
|
| 1704 |
! Test nested condition: define outer but not inner macro |
| 1705 |
call cpp_config%new([string_t('FIFTH_FEATURE')]) ! This makes #ifndef FIFTH_FEATURE inactive |
| 1706 |
cpp_config%name = "cpp" |
| 1707 |
|
| 1708 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1709 |
if (allocated(error)) return |
| 1710 |
|
| 1711 |
if (size(f_source%modules_used) /= 1) then |
| 1712 |
if (size(f_source%modules_used) > 0) then |
| 1713 |
call test_failed(error, 'Expected 1 module dependency with FIFTH_FEATURE defined, got: ' // & |
| 1714 |
f_source%modules_used(2)%s // ' (and maybe others)') |
| 1715 |
else |
| 1716 |
call test_failed(error, 'Expected 1 module dependency with FIFTH_FEATURE defined, got 0') |
| 1717 |
end if |
| 1718 |
return |
| 1719 |
end if |
| 1720 |
|
| 1721 |
! Test nested condition: define both outer condition (negative) and inner (positive) |
| 1722 |
call cpp_config%new([string_t('NESTED_FEATURE')]) ! FIFTH_FEATURE not defined, NESTED_FEATURE defined |
| 1723 |
cpp_config%name = "cpp" |
| 1724 |
|
| 1725 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1726 |
if (allocated(error)) return |
| 1727 |
|
| 1728 |
if (size(f_source%modules_used) /= 3) then |
| 1729 |
call test_failed(error, 'Expected 3 module dependencies with nested conditions active') |
| 1730 |
return |
| 1731 |
end if |
| 1732 |
|
| 1733 |
end subroutine test_conditional_if_defined |
| 1734 |
|
| 1735 |
!> Test conditional compilation with #if MACRO == VALUE and #if MACRO != VALUE |
| 1736 |
subroutine test_conditional_macro_comparison(error) |
| 1737 |
|
| 1738 |
!> Error handling |
| 1739 |
type(error_t), allocatable, intent(out) :: error |
| 1740 |
|
| 1741 |
type(srcfile_t) :: f_source |
| 1742 |
character(:), allocatable :: temp_file |
| 1743 |
integer :: unit |
| 1744 |
type(preprocess_config_t) :: cpp_config |
| 1745 |
|
| 1746 |
temp_file = get_temp_filename() |
| 1747 |
|
| 1748 |
! Test file with macro value comparisons and #define |
| 1749 |
open(file=temp_file, newunit=unit) |
| 1750 |
write(unit, '(a)') & |
| 1751 |
'module test_mod', & |
| 1752 |
'#define LOCAL_MACRO 1', & |
| 1753 |
'#if EXTERNAL_MACRO == 1', & |
| 1754 |
' use external_eq_module', & |
| 1755 |
'#endif', & |
| 1756 |
'#if EXTERNAL_MACRO != 1', & |
| 1757 |
' use external_neq_module', & |
| 1758 |
'#endif', & |
| 1759 |
'#if LOCAL_MACRO == 1', & |
| 1760 |
' use local_eq_module', & |
| 1761 |
'#endif', & |
| 1762 |
'#if LOCAL_MACRO != 1', & |
| 1763 |
' use local_neq_module', & |
| 1764 |
'#endif', & |
| 1765 |
'#if UNDEFINED_MACRO == 1', & |
| 1766 |
' use undefined_module', & |
| 1767 |
'#endif', & |
| 1768 |
' implicit none', & |
| 1769 |
'end module test_mod' |
| 1770 |
close(unit) |
| 1771 |
|
| 1772 |
! Test 1: With CPP enabled but no external macros defined |
| 1773 |
! LOCAL_MACRO is defined via #define, EXTERNAL_MACRO is not |
| 1774 |
call cpp_config%new([string_t::]) |
| 1775 |
cpp_config%name = "cpp" |
| 1776 |
|
| 1777 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1778 |
if (allocated(error)) return |
| 1779 |
|
| 1780 |
! Should find: local_eq_module (LOCAL_MACRO==1 is true) |
| 1781 |
! external_neq_module (EXTERNAL_MACRO!=1 is true since undefined) |
| 1782 |
! Should NOT find: external_eq_module, local_neq_module, undefined_module |
| 1783 |
if (.not.('local_eq_module' .in. f_source%modules_used)) then |
| 1784 |
call test_failed(error, 'Expected local_eq_module with #define LOCAL_MACRO 1') |
| 1785 |
return |
| 1786 |
end if |
| 1787 |
|
| 1788 |
if ('local_neq_module' .in. f_source%modules_used) then |
| 1789 |
call test_failed(error, 'Should not find local_neq_module when LOCAL_MACRO==1') |
| 1790 |
return |
| 1791 |
end if |
| 1792 |
|
| 1793 |
if ('external_eq_module' .in. f_source%modules_used) then |
| 1794 |
call test_failed(error, 'Should not find external_eq_module when EXTERNAL_MACRO is undefined') |
| 1795 |
return |
| 1796 |
end if |
| 1797 |
|
| 1798 |
if (.not.('external_neq_module' .in. f_source%modules_used)) then |
| 1799 |
call test_failed(error, 'Expected external_neq_module (UNDEFINED != 1 should be true)') |
| 1800 |
return |
| 1801 |
end if |
| 1802 |
|
| 1803 |
if ('undefined_module' .in. f_source%modules_used) then |
| 1804 |
call test_failed(error, 'Should not find undefined_module when UNDEFINED_MACRO is undefined') |
| 1805 |
return |
| 1806 |
end if |
| 1807 |
|
| 1808 |
! Test 2: With external macro defined to matching value |
| 1809 |
call cpp_config%new([string_t('EXTERNAL_MACRO=1')]) |
| 1810 |
cpp_config%name = "cpp" |
| 1811 |
|
| 1812 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1813 |
if (allocated(error)) return |
| 1814 |
|
| 1815 |
if (.not.('external_eq_module' .in. f_source%modules_used)) then |
| 1816 |
call test_failed(error, 'Expected external_eq_module with EXTERNAL_MACRO=1') |
| 1817 |
return |
| 1818 |
end if |
| 1819 |
|
| 1820 |
if ('external_neq_module' .in. f_source%modules_used) then |
| 1821 |
call test_failed(error, 'Should not find external_neq_module when EXTERNAL_MACRO==1') |
| 1822 |
return |
| 1823 |
end if |
| 1824 |
|
| 1825 |
! Test 3: With external macro defined to non-matching value |
| 1826 |
call cpp_config%new([string_t('EXTERNAL_MACRO=2')]) |
| 1827 |
cpp_config%name = "cpp" |
| 1828 |
|
| 1829 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1830 |
if (allocated(error)) return |
| 1831 |
|
| 1832 |
if ('external_eq_module' .in. f_source%modules_used) then |
| 1833 |
call test_failed(error, 'Should not find external_eq_module when EXTERNAL_MACRO==2') |
| 1834 |
return |
| 1835 |
end if |
| 1836 |
|
| 1837 |
if (.not.('external_neq_module' .in. f_source%modules_used)) then |
| 1838 |
call test_failed(error, 'Expected external_neq_module with EXTERNAL_MACRO=2 (!=1)') |
| 1839 |
return |
| 1840 |
end if |
| 1841 |
|
| 1842 |
end subroutine test_conditional_macro_comparison |
| 1843 |
|
| 1844 |
!> Test #define directive without trailing space (issue #1222) |
| 1845 |
!> This tests for out-of-bounds access when parsing #define at end of line |
| 1846 |
subroutine test_define_no_trailing_space(error) |
| 1847 |
|
| 1848 |
!> Error handling |
| 1849 |
type(error_t), allocatable, intent(out) :: error |
| 1850 |
|
| 1851 |
integer :: unit |
| 1852 |
character(:), allocatable :: temp_file |
| 1853 |
type(srcfile_t), allocatable :: f_source |
| 1854 |
type(preprocess_config_t) :: cpp_config |
| 1855 |
|
| 1856 |
allocate(temp_file, source=get_temp_filename()) |
| 1857 |
|
| 1858 |
open(file=temp_file, newunit=unit) |
| 1859 |
! Note: #define SIMPLE_FLAG has no trailing space - this triggered |
| 1860 |
! an out-of-bounds error before the fix |
| 1861 |
write(unit, '(a)') & |
| 1862 |
& '#define SIMPLE_FLAG', & |
| 1863 |
& '#ifdef SIMPLE_FLAG', & |
| 1864 |
& ' use flag_module', & |
| 1865 |
& '#endif', & |
| 1866 |
& 'program test', & |
| 1867 |
& ' implicit none', & |
| 1868 |
& 'end program test' |
| 1869 |
close(unit) |
| 1870 |
|
| 1871 |
call cpp_config%new([string_t::]) |
| 1872 |
cpp_config%name = "cpp" |
| 1873 |
|
| 1874 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1875 |
if (allocated(error)) return |
| 1876 |
|
| 1877 |
if (f_source%unit_type /= FPM_UNIT_PROGRAM) then |
| 1878 |
call test_failed(error, 'Wrong unit type detected - expecting FPM_UNIT_PROGRAM') |
| 1879 |
return |
| 1880 |
end if |
| 1881 |
|
| 1882 |
if (.not.('flag_module' .in. f_source%modules_used)) then |
| 1883 |
call test_failed(error, 'Expected flag_module with #define SIMPLE_FLAG (no trailing space)') |
| 1884 |
return |
| 1885 |
end if |
| 1886 |
|
| 1887 |
end subroutine test_define_no_trailing_space |
| 1888 |
|
| 1889 |
!> Test that CPP macros are case-sensitive (per CPP standard) |
| 1890 |
subroutine test_macro_case_sensitivity(error) |
| 1891 |
|
| 1892 |
!> Error handling |
| 1893 |
type(error_t), allocatable, intent(out) :: error |
| 1894 |
|
| 1895 |
integer :: unit |
| 1896 |
character(:), allocatable :: temp_file |
| 1897 |
type(srcfile_t), allocatable :: f_source |
| 1898 |
type(preprocess_config_t) :: cpp_config |
| 1899 |
|
| 1900 |
allocate(temp_file, source=get_temp_filename()) |
| 1901 |
|
| 1902 |
open(file=temp_file, newunit=unit) |
| 1903 |
write(unit, '(a)') & |
| 1904 |
& '! Test #ifdef (uses macro_in_list)', & |
| 1905 |
& '#ifdef MY_MACRO', & |
| 1906 |
& ' use ifdef_uppercase_module', & |
| 1907 |
& '#endif', & |
| 1908 |
& '#ifdef my_macro', & |
| 1909 |
& ' use ifdef_lowercase_module', & |
| 1910 |
& '#endif', & |
| 1911 |
& '#ifdef My_Macro', & |
| 1912 |
& ' use ifdef_mixedcase_module', & |
| 1913 |
& '#endif', & |
| 1914 |
& '! Test #if MACRO (uses macro_is_truthy -> get_macro_value)', & |
| 1915 |
& '#if MY_MACRO', & |
| 1916 |
& ' use if_uppercase_module', & |
| 1917 |
& '#endif', & |
| 1918 |
& '#if my_macro', & |
| 1919 |
& ' use if_lowercase_module', & |
| 1920 |
& '#endif', & |
| 1921 |
& '#if My_Macro', & |
| 1922 |
& ' use if_mixedcase_module', & |
| 1923 |
& '#endif', & |
| 1924 |
& 'program test', & |
| 1925 |
& ' implicit none', & |
| 1926 |
& 'end program test' |
| 1927 |
close(unit) |
| 1928 |
|
| 1929 |
! Test 1: Define MY_MACRO=1 (uppercase) - should only match uppercase for both #ifdef and #if |
| 1930 |
call cpp_config%new([string_t('MY_MACRO=1')]) |
| 1931 |
cpp_config%name = "cpp" |
| 1932 |
|
| 1933 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1934 |
if (allocated(error)) return |
| 1935 |
|
| 1936 |
! #ifdef tests (macro_in_list) |
| 1937 |
if (.not.('ifdef_uppercase_module' .in. f_source%modules_used)) then |
| 1938 |
call test_failed(error, '#ifdef: Expected ifdef_uppercase_module with MY_MACRO defined') |
| 1939 |
return |
| 1940 |
end if |
| 1941 |
if ('ifdef_lowercase_module' .in. f_source%modules_used) then |
| 1942 |
call test_failed(error, '#ifdef: Should not find ifdef_lowercase_module - case-sensitive') |
| 1943 |
return |
| 1944 |
end if |
| 1945 |
if ('ifdef_mixedcase_module' .in. f_source%modules_used) then |
| 1946 |
call test_failed(error, '#ifdef: Should not find ifdef_mixedcase_module - case-sensitive') |
| 1947 |
return |
| 1948 |
end if |
| 1949 |
|
| 1950 |
! #if tests (macro_is_truthy -> get_macro_value) |
| 1951 |
if (.not.('if_uppercase_module' .in. f_source%modules_used)) then |
| 1952 |
call test_failed(error, '#if: Expected if_uppercase_module with MY_MACRO=1 defined') |
| 1953 |
return |
| 1954 |
end if |
| 1955 |
if ('if_lowercase_module' .in. f_source%modules_used) then |
| 1956 |
call test_failed(error, '#if: Should not find if_lowercase_module - case-sensitive') |
| 1957 |
return |
| 1958 |
end if |
| 1959 |
if ('if_mixedcase_module' .in. f_source%modules_used) then |
| 1960 |
call test_failed(error, '#if: Should not find if_mixedcase_module - case-sensitive') |
| 1961 |
return |
| 1962 |
end if |
| 1963 |
|
| 1964 |
! Test 2: Define my_macro=1 (lowercase) - should only match lowercase |
| 1965 |
call cpp_config%new([string_t('my_macro=1')]) |
| 1966 |
cpp_config%name = "cpp" |
| 1967 |
|
| 1968 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 1969 |
if (allocated(error)) return |
| 1970 |
|
| 1971 |
! #ifdef tests |
| 1972 |
if ('ifdef_uppercase_module' .in. f_source%modules_used) then |
| 1973 |
call test_failed(error, '#ifdef: Should not find ifdef_uppercase_module - case-sensitive') |
| 1974 |
return |
| 1975 |
end if |
| 1976 |
if (.not.('ifdef_lowercase_module' .in. f_source%modules_used)) then |
| 1977 |
call test_failed(error, '#ifdef: Expected ifdef_lowercase_module with my_macro defined') |
| 1978 |
return |
| 1979 |
end if |
| 1980 |
|
| 1981 |
! #if tests |
| 1982 |
if ('if_uppercase_module' .in. f_source%modules_used) then |
| 1983 |
call test_failed(error, '#if: Should not find if_uppercase_module - case-sensitive') |
| 1984 |
return |
| 1985 |
end if |
| 1986 |
if (.not.('if_lowercase_module' .in. f_source%modules_used)) then |
| 1987 |
call test_failed(error, '#if: Expected if_lowercase_module with my_macro=1 defined') |
| 1988 |
return |
| 1989 |
end if |
| 1990 |
|
| 1991 |
end subroutine test_macro_case_sensitivity |
| 1992 |
|
| 1993 |
!> Test #if MACRO truthy evaluation per CPP semantics |
| 1994 |
!> Per CPP: undefined=0, MACRO=0 is false, MACRO=1 is true, MACRO=non-zero is true |
| 1995 |
subroutine test_if_macro_truthy(error) |
| 1996 |
|
| 1997 |
!> Error handling |
| 1998 |
type(error_t), allocatable, intent(out) :: error |
| 1999 |
|
| 2000 |
integer :: unit |
| 2001 |
character(:), allocatable :: temp_file |
| 2002 |
type(srcfile_t), allocatable :: f_source |
| 2003 |
type(preprocess_config_t) :: cpp_config |
| 2004 |
|
| 2005 |
allocate(temp_file, source=get_temp_filename()) |
| 2006 |
|
| 2007 |
! Test file with #if MACRO (not #ifdef, not #if defined()) |
| 2008 |
open(file=temp_file, newunit=unit) |
| 2009 |
write(unit, '(a)') & |
| 2010 |
& '#if UNDEFINED_MACRO', & |
| 2011 |
& ' use undefined_module', & |
| 2012 |
& '#endif', & |
| 2013 |
& '#if ZERO_MACRO', & |
| 2014 |
& ' use zero_module', & |
| 2015 |
& '#endif', & |
| 2016 |
& '#if ONE_MACRO', & |
| 2017 |
& ' use one_module', & |
| 2018 |
& '#endif', & |
| 2019 |
& '#if NONZERO_MACRO', & |
| 2020 |
& ' use nonzero_module', & |
| 2021 |
& '#endif', & |
| 2022 |
& '#if EMPTY_MACRO', & |
| 2023 |
& ' use empty_module', & |
| 2024 |
& '#endif', & |
| 2025 |
& 'program test', & |
| 2026 |
& ' implicit none', & |
| 2027 |
& 'end program test' |
| 2028 |
close(unit) |
| 2029 |
|
| 2030 |
! Test 1: No macros defined - only UNDEFINED should be tested (evaluates to 0/false) |
| 2031 |
call cpp_config%new([string_t::]) |
| 2032 |
cpp_config%name = "cpp" |
| 2033 |
|
| 2034 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 2035 |
if (allocated(error)) return |
| 2036 |
|
| 2037 |
if ('undefined_module' .in. f_source%modules_used) then |
| 2038 |
call test_failed(error, 'Undefined macro should evaluate to false in #if') |
| 2039 |
return |
| 2040 |
end if |
| 2041 |
|
| 2042 |
! Test 2: ZERO_MACRO=0 should be false, ONE_MACRO=1 should be true |
| 2043 |
call cpp_config%new([string_t('ZERO_MACRO=0'), string_t('ONE_MACRO=1')]) |
| 2044 |
cpp_config%name = "cpp" |
| 2045 |
|
| 2046 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 2047 |
if (allocated(error)) return |
| 2048 |
|
| 2049 |
if ('zero_module' .in. f_source%modules_used) then |
| 2050 |
call test_failed(error, 'MACRO=0 should evaluate to false in #if') |
| 2051 |
return |
| 2052 |
end if |
| 2053 |
|
| 2054 |
if (.not.('one_module' .in. f_source%modules_used)) then |
| 2055 |
call test_failed(error, 'MACRO=1 should evaluate to true in #if') |
| 2056 |
return |
| 2057 |
end if |
| 2058 |
|
| 2059 |
! Test 3: Non-zero values should be truthy |
| 2060 |
call cpp_config%new([string_t('NONZERO_MACRO=42')]) |
| 2061 |
cpp_config%name = "cpp" |
| 2062 |
|
| 2063 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 2064 |
if (allocated(error)) return |
| 2065 |
|
| 2066 |
if (.not.('nonzero_module' .in. f_source%modules_used)) then |
| 2067 |
call test_failed(error, 'MACRO=42 (non-zero) should evaluate to true in #if') |
| 2068 |
return |
| 2069 |
end if |
| 2070 |
|
| 2071 |
! Test 4: Empty macro (defined without value) - true, like -DMACRO on command line |
| 2072 |
call cpp_config%new([string_t('EMPTY_MACRO')]) |
| 2073 |
cpp_config%name = "cpp" |
| 2074 |
|
| 2075 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 2076 |
if (allocated(error)) return |
| 2077 |
|
| 2078 |
if (.not.('empty_module' .in. f_source%modules_used)) then |
| 2079 |
call test_failed(error, 'Empty macro should evaluate to true in #if (like -DMACRO)') |
| 2080 |
return |
| 2081 |
end if |
| 2082 |
|
| 2083 |
end subroutine test_if_macro_truthy |
| 2084 |
|
| 2085 |
!> Test all CPP conditional formats for macros defined inside source via #define |
| 2086 |
!> Formats: #if MACRO, #ifdef MACRO, #if defined(MACRO), #if defined MACRO, #if MACRO == val |
| 2087 |
subroutine test_if_macro_truthy_from_define(error) |
| 2088 |
|
| 2089 |
!> Error handling |
| 2090 |
type(error_t), allocatable, intent(out) :: error |
| 2091 |
|
| 2092 |
integer :: unit |
| 2093 |
character(:), allocatable :: temp_file |
| 2094 |
type(srcfile_t), allocatable :: f_source |
| 2095 |
type(preprocess_config_t) :: cpp_config |
| 2096 |
|
| 2097 |
allocate(temp_file, source=get_temp_filename()) |
| 2098 |
|
| 2099 |
! Test file with #define inside source, then various conditional formats |
| 2100 |
open(file=temp_file, newunit=unit) |
| 2101 |
write(unit, '(a)') & |
| 2102 |
& '#define ZERO_MACRO 0', & |
| 2103 |
& '#define ONE_MACRO 1', & |
| 2104 |
& '#define NONZERO_MACRO 42', & |
| 2105 |
& '#define EMPTY_MACRO', & |
| 2106 |
& '', & |
| 2107 |
& '! Test #if MACRO format', & |
| 2108 |
& '#if ZERO_MACRO', & |
| 2109 |
& ' use if_zero_module', & |
| 2110 |
& '#endif', & |
| 2111 |
& '#if ONE_MACRO', & |
| 2112 |
& ' use if_one_module', & |
| 2113 |
& '#endif', & |
| 2114 |
& '#if NONZERO_MACRO', & |
| 2115 |
& ' use if_nonzero_module', & |
| 2116 |
& '#endif', & |
| 2117 |
& '#if EMPTY_MACRO', & |
| 2118 |
& ' use if_empty_module', & |
| 2119 |
& '#endif', & |
| 2120 |
& '#if UNDEFINED_MACRO', & |
| 2121 |
& ' use if_undefined_module', & |
| 2122 |
& '#endif', & |
| 2123 |
& '', & |
| 2124 |
& '! Test #ifdef MACRO format', & |
| 2125 |
& '#ifdef ZERO_MACRO', & |
| 2126 |
& ' use ifdef_zero_module', & |
| 2127 |
& '#endif', & |
| 2128 |
& '#ifdef ONE_MACRO', & |
| 2129 |
& ' use ifdef_one_module', & |
| 2130 |
& '#endif', & |
| 2131 |
& '#ifdef EMPTY_MACRO', & |
| 2132 |
& ' use ifdef_empty_module', & |
| 2133 |
& '#endif', & |
| 2134 |
& '#ifdef UNDEFINED_MACRO', & |
| 2135 |
& ' use ifdef_undefined_module', & |
| 2136 |
& '#endif', & |
| 2137 |
& '', & |
| 2138 |
& '! Test #if defined(MACRO) format (with parentheses)', & |
| 2139 |
& '#if defined(ZERO_MACRO)', & |
| 2140 |
& ' use defined_paren_zero_module', & |
| 2141 |
& '#endif', & |
| 2142 |
& '#if defined(UNDEFINED_MACRO)', & |
| 2143 |
& ' use defined_paren_undefined_module', & |
| 2144 |
& '#endif', & |
| 2145 |
& '#if !defined(UNDEFINED_MACRO)', & |
| 2146 |
& ' use not_defined_paren_module', & |
| 2147 |
& '#endif', & |
| 2148 |
& '', & |
| 2149 |
& '! Test #if defined MACRO format (without parentheses)', & |
| 2150 |
& '#if defined ONE_MACRO', & |
| 2151 |
& ' use defined_noparen_one_module', & |
| 2152 |
& '#endif', & |
| 2153 |
& '#if !defined UNDEFINED_MACRO', & |
| 2154 |
& ' use not_defined_noparen_module', & |
| 2155 |
& '#endif', & |
| 2156 |
& '', & |
| 2157 |
& '! Test #if MACRO == value format', & |
| 2158 |
& '#if ZERO_MACRO == 0', & |
| 2159 |
& ' use eq_zero_module', & |
| 2160 |
& '#endif', & |
| 2161 |
& '#if ONE_MACRO == 1', & |
| 2162 |
& ' use eq_one_module', & |
| 2163 |
& '#endif', & |
| 2164 |
& '#if NONZERO_MACRO == 42', & |
| 2165 |
& ' use eq_nonzero_module', & |
| 2166 |
& '#endif', & |
| 2167 |
& '#if ONE_MACRO != 0', & |
| 2168 |
& ' use neq_one_module', & |
| 2169 |
& '#endif', & |
| 2170 |
& '#if ZERO_MACRO != 1', & |
| 2171 |
& ' use neq_zero_module', & |
| 2172 |
& '#endif', & |
| 2173 |
& '', & |
| 2174 |
& 'program test', & |
| 2175 |
& ' implicit none', & |
| 2176 |
& 'end program test' |
| 2177 |
close(unit) |
| 2178 |
|
| 2179 |
! Enable CPP but don't pass any external macros |
| 2180 |
call cpp_config%new([string_t::]) |
| 2181 |
cpp_config%name = "cpp" |
| 2182 |
|
| 2183 |
f_source = parse_f_source(temp_file, error, preprocess=cpp_config) |
| 2184 |
if (allocated(error)) return |
| 2185 |
|
| 2186 |
! === #if MACRO tests === |
| 2187 |
if ('if_zero_module' .in. f_source%modules_used) then |
| 2188 |
call test_failed(error, '#if MACRO: MACRO=0 should be false') |
| 2189 |
return |
| 2190 |
end if |
| 2191 |
if (.not.('if_one_module' .in. f_source%modules_used)) then |
| 2192 |
call test_failed(error, '#if MACRO: MACRO=1 should be true') |
| 2193 |
return |
| 2194 |
end if |
| 2195 |
if (.not.('if_nonzero_module' .in. f_source%modules_used)) then |
| 2196 |
call test_failed(error, '#if MACRO: MACRO=42 should be true') |
| 2197 |
return |
| 2198 |
end if |
| 2199 |
if ('if_empty_module' .in. f_source%modules_used) then |
| 2200 |
call test_failed(error, '#if MACRO: empty macro from #define should be false (CPP error)') |
| 2201 |
return |
| 2202 |
end if |
| 2203 |
if ('if_undefined_module' .in. f_source%modules_used) then |
| 2204 |
call test_failed(error, '#if MACRO: undefined should be false') |
| 2205 |
return |
| 2206 |
end if |
| 2207 |
|
| 2208 |
! === #ifdef MACRO tests === |
| 2209 |
if (.not.('ifdef_zero_module' .in. f_source%modules_used)) then |
| 2210 |
call test_failed(error, '#ifdef MACRO: MACRO=0 should be true (defined)') |
| 2211 |
return |
| 2212 |
end if |
| 2213 |
if (.not.('ifdef_one_module' .in. f_source%modules_used)) then |
| 2214 |
call test_failed(error, '#ifdef MACRO: MACRO=1 should be true') |
| 2215 |
return |
| 2216 |
end if |
| 2217 |
if (.not.('ifdef_empty_module' .in. f_source%modules_used)) then |
| 2218 |
call test_failed(error, '#ifdef MACRO: empty macro should be true') |
| 2219 |
return |
| 2220 |
end if |
| 2221 |
if ('ifdef_undefined_module' .in. f_source%modules_used) then |
| 2222 |
call test_failed(error, '#ifdef MACRO: undefined should be false') |
| 2223 |
return |
| 2224 |
end if |
| 2225 |
|
| 2226 |
! === #if defined(MACRO) tests === |
| 2227 |
if (.not.('defined_paren_zero_module' .in. f_source%modules_used)) then |
| 2228 |
call test_failed(error, '#if defined(MACRO): MACRO=0 should be true (defined)') |
| 2229 |
return |
| 2230 |
end if |
| 2231 |
if ('defined_paren_undefined_module' .in. f_source%modules_used) then |
| 2232 |
call test_failed(error, '#if defined(MACRO): undefined should be false') |
| 2233 |
return |
| 2234 |
end if |
| 2235 |
if (.not.('not_defined_paren_module' .in. f_source%modules_used)) then |
| 2236 |
call test_failed(error, '#if !defined(MACRO): undefined should be true') |
| 2237 |
return |
| 2238 |
end if |
| 2239 |
|
| 2240 |
! === #if defined MACRO tests (no parentheses) === |
| 2241 |
if (.not.('defined_noparen_one_module' .in. f_source%modules_used)) then |
| 2242 |
call test_failed(error, '#if defined MACRO: MACRO=1 should be true') |
| 2243 |
return |
| 2244 |
end if |
| 2245 |
if (.not.('not_defined_noparen_module' .in. f_source%modules_used)) then |
| 2246 |
call test_failed(error, '#if !defined MACRO: undefined should be true') |
| 2247 |
return |
| 2248 |
end if |
| 2249 |
|
| 2250 |
! === #if MACRO == value tests === |
| 2251 |
if (.not.('eq_zero_module' .in. f_source%modules_used)) then |
| 2252 |
call test_failed(error, '#if MACRO == 0: should be true when MACRO=0') |
| 2253 |
return |
| 2254 |
end if |
| 2255 |
if (.not.('eq_one_module' .in. f_source%modules_used)) then |
| 2256 |
call test_failed(error, '#if MACRO == 1: should be true when MACRO=1') |
| 2257 |
return |
| 2258 |
end if |
| 2259 |
if (.not.('eq_nonzero_module' .in. f_source%modules_used)) then |
| 2260 |
call test_failed(error, '#if MACRO == 42: should be true when MACRO=42') |
| 2261 |
return |
| 2262 |
end if |
| 2263 |
if (.not.('neq_one_module' .in. f_source%modules_used)) then |
| 2264 |
call test_failed(error, '#if MACRO != 0: should be true when MACRO=1') |
| 2265 |
return |
| 2266 |
end if |
| 2267 |
if (.not.('neq_zero_module' .in. f_source%modules_used)) then |
| 2268 |
call test_failed(error, '#if MACRO != 1: should be true when MACRO=0') |
| 2269 |
return |
| 2270 |
end if |
| 2271 |
|
| 2272 |
end subroutine test_if_macro_truthy_from_define |
| 2273 |
|
| 2274 |
|
| 2275 |
end module test_source_parsing |