Skip to content

Extending file preprocessing #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
603 changes: 319 additions & 284 deletions fortls/parse_fortran.py

Large diffs are not rendered by default.

53 changes: 52 additions & 1 deletion test/test_preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,25 @@ def check_return(result_array, checks):
file_path = root_dir / "preproc.F90"
string += hover_req(file_path, 5, 8) # user defined type
string += hover_req(file_path, 7, 30) # variable
string += hover_req(file_path, 7, 40) # multi-lin variable
string += hover_req(file_path, 7, 40) # multi-line variable
string += hover_req(file_path, 8, 7) # function with if conditional
string += hover_req(file_path, 9, 7) # multiline function with if conditional
string += hover_req(file_path, 10, 15) # defined without ()
file_path = root_dir / "preproc_keywords.F90"
string += hover_req(file_path, 6, 2) # ignores PP across Fortran line continuations
file_path = root_dir / "preproc_ifdef.F90"
string += hover_req(file_path, 21, 15) # defined inside `if` block
string += hover_req(file_path, 22, 15) # defined inside `elif` block
string += hover_req(file_path, 23, 15) # defined inside `ifndef` block
file_path = root_dir / "preproc_use.F90"
string += hover_req(file_path, 7, 15) # Correct subroutine definition from mpi_f08
string += hover_req(file_path, 7, 30) # Correct type of MPI_COMM_WORLD
file_path = root_dir / "preproc_use_include.F90"
string += hover_req(file_path, 3, 15) # Correct subroutine definition from mpi_f08
string += hover_req(file_path, 3, 30) # Correct type of MPI_COMM_WORLD
string += hover_req(
file_path, 4, 15
) # Found definition of `omp_get_num_threads` function from omp_lib module
config = str(root_dir / ".pp_conf.json")
errcode, results = run_request(string, ["--config", config])
assert errcode == 0
Expand All @@ -49,6 +62,44 @@ def check_return(result_array, checks):
),
"```fortran90\n#define SUCCESS .true.\n```",
"```fortran90\nREAL, CONTIGUOUS, POINTER, DIMENSION(:) :: var1\n```",
"```fortran90\n#define CUSTOM_MACRO 0\n```",
"```fortran90\n#define SECOND_CUSTOM_MACRO 1\n```",
"```fortran90\n#define THIRD_CUSTOM_MACRO 0\n```",
(
"```fortran90"
"\nSUBROUTINE MPI_Comm_size(comm, size, ierror=ierror)"
"\n TYPE(MPI_Comm), INTENT(IN) :: comm"
"\n INTEGER(C_INT), INTENT(OUT) :: size"
"\n INTEGER(C_INT), OPTIONAL, INTENT(OUT) :: ierror"
"\n"
"```"
"\n-----\n"
"\n**Parameters:** "
"\n`comm` Communicator. "
"\n`size` Size of communicator. "
"\n`ierror` Error status."
),
"```fortran90\nTYPE(MPI_Comm) :: MPI_COMM_WORLD\n```",
(
"```fortran90"
"\nSUBROUTINE MPI_Comm_size(comm, size, ierror=ierror)"
"\n TYPE(MPI_Comm), INTENT(IN) :: comm"
"\n INTEGER(C_INT), INTENT(OUT) :: size"
"\n INTEGER(C_INT), OPTIONAL, INTENT(OUT) :: ierror"
"\n"
"```"
"\n-----\n"
"\n**Parameters:** "
"\n`comm` Communicator. "
"\n`size` Size of communicator. "
"\n`ierror` Error status."
),
"```fortran90\nTYPE(MPI_Comm) :: MPI_COMM_WORLD\n```",
(
"```fortran90"
"\nINTEGER FUNCTION omp_get_num_threads() RESULT(omp_get_num_threads)"
"\n```"
),
)
assert len(ref_results) == len(results) - 1
check_return(results[1:], ref_results)
2 changes: 1 addition & 1 deletion test/test_source/pp/.pp_conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"pp_suffixes": [".h", ".F90"],
"incl_suffixes": [".h"],
"include_dirs": ["include"],
"pp_defs": { "HAVE_CONTIGUOUS": "" }
"pp_defs": { "HAVE_CONTIGUOUS": "", "DUMMY": "" },
}
6 changes: 6 additions & 0 deletions test/test_source/pp/include/mpi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifdef USE_MPI
use mpi
#else
use mpi_f08
#endif
#include "omp.h"
1 change: 1 addition & 0 deletions test/test_source/pp/include/omp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use omp_lib
25 changes: 25 additions & 0 deletions test/test_source/pp/preproc_ifdef.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
program preprocessor
#if defined(DUMMY)
#define CUSTOM_MACRO 0
#else
#define CUSTOM_MACRO 1
#endif
#ifdef DUMMY_MACRO
#define SECOND_CUSTOM_MACRO 0
#elif defined DUMMY
#define SECOND_CUSTOM_MACRO 1
#else
#define SECOND_CUSTOM_MACRO 2
#endif

#ifndef DUMMY_MACRO
#define THIRD_CUSTOM_MACRO 0
#elif defined (SECOND_DUMMY_MACRO)
#define THIRD_CUSTOM_MACRO 1
#else
#define THIRD_CUSTOM_MACRO 2
#endif
print*, CUSTOM_MACRO
print*, SECOND_CUSTOM_MACRO
print*, THIRD_CUSTOM_MACRO
end program preprocessor
9 changes: 9 additions & 0 deletions test/test_source/pp/preproc_use.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
program preprocessor
#ifdef USE_MPI
use mpi
#else
use mpi_f08
#endif
integer :: comm_size, ierr
call MPI_Comm_size(MPI_COMM_WORLD, comm_size, ierr)
end program preprocessor
6 changes: 6 additions & 0 deletions test/test_source/pp/preproc_use_include.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
program preprocessor
#include "mpi.h"
integer :: comm_size, ierr
call MPI_Comm_size(MPI_COMM_WORLD, comm_size, ierr)
print*, omp_get_num_threads()
end program preprocessor
10 changes: 10 additions & 0 deletions test/test_source/pp/sources/mpi.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module mpi

integer, parameter :: MPI_COMM_WORLD = 0

contains

subroutine MPI_Comm_size(comm, size, ierr)
integer :: comm, size, ierr
end subroutine MPI_Comm_size
end module mpi
17 changes: 17 additions & 0 deletions test/test_source/pp/sources/mpi_f08.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module mpi_f08
use iso_c_binding

type :: MPI_Comm
integer :: MPI_VAL
end type MPI_Comm

type(MPI_Comm) :: MPI_COMM_WORLD

contains

subroutine MPI_Comm_size(comm, size, ierror)
type(MPI_Comm), intent(in) :: comm !< Communicator.
integer(C_INT), intent(out) :: size !< Size of communicator.
integer(C_INT), optional, intent(out) :: ierror !< Error status.
end subroutine MPI_Comm_size
end module mpi_f08