Skip to content
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

Support selected Intel-specific preprocessor macro expansions #341

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
05f93a7
allow for whitespace in PP directives, better def arg regex
emanspeaks Dec 11, 2023
f297da5
add draft of unit test
emanspeaks Jan 3, 2024
d23088d
Merge branch 'master' into ifort-fpp-fixes
emanspeaks Jan 3, 2024
37752c8
revert changes to try to fix unit tests
emanspeaks Jan 3, 2024
4367f32
everything working but the def regex
emanspeaks Jan 3, 2024
1b3735e
working regex that is at least backwards compatible
emanspeaks Jan 3, 2024
3ddeb00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 3, 2024
6e63f04
working version of macro expansion w/ tests
emanspeaks Jan 3, 2024
e1ce5eb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 3, 2024
0b6fbaa
housekeeping
emanspeaks Jan 3, 2024
ccb4d37
replace \s* with [ ]*
emanspeaks Jan 3, 2024
b7c3572
attempt to remedy regex warnings
emanspeaks Jan 4, 2024
efed76f
second attempt to fix regex warnings
emanspeaks Jan 4, 2024
c62ef0f
remove ambiguous define arg matching
emanspeaks Jan 4, 2024
107180d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 4, 2024
0ecc55d
additional support for Intel FPP
emanspeaks Jan 7, 2024
0de9e0e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 7, 2024
1ea9700
fix issue with json schema
emanspeaks Jan 7, 2024
755370e
addl logical operators for intel fpp
emanspeaks Jan 7, 2024
9b67c67
parse intel fpp operators separately
emanspeaks Jan 7, 2024
ad4de4c
init commit of cherry-picked changes from #341
emanspeaks Jan 21, 2024
5cf36e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 21, 2024
6245d85
Merge branch 'generic-macro-expansions' into ifort-fpp-fixes
emanspeaks Jan 21, 2024
c975006
rebase intel-specific changes on top of PR #350
emanspeaks Jan 21, 2024
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
8 changes: 7 additions & 1 deletion fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,17 @@ def get_definition(
return None
# Search in Preprocessor defined variables
if def_name in def_file.pp_defs:
def_value = def_file.pp_defs.get(def_name)
def_arg_str = ""
if isinstance(def_value, tuple):
def_arg_str, def_value = def_value
def_arg_str = f"({def_arg_str})"

var = Variable(
def_file.ast,
def_line + 1,
def_name,
f"#define {def_name} {def_file.pp_defs.get(def_name)}",
f"#define {def_name}{def_arg_str} {def_value}",
[],
)
return var
Expand Down
56 changes: 50 additions & 6 deletions fortls/parsers/internal/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2098,10 +2098,11 @@
if def_cont_name is not None:
output_file.append("")
if line.rstrip()[-1] != "\\":
defs_tmp[def_cont_name] += line.strip()
append_multiline_macro(defs_tmp, def_cont_name, line.strip())
def_cont_name = None
else:
defs_tmp[def_cont_name] += line[0:-1].strip()
append_multiline_macro(defs_tmp, def_cont_name, line[0:-1].strip())

Check warning on line 2104 in fortls/parsers/internal/parser.py

View check run for this annotation

Codecov / codecov/patch

fortls/parsers/internal/parser.py#L2104

Added line #L2104 was not covered by tests

continue
# Handle conditional statements
match = FRegex.PP_REGEX.match(line)
Expand Down Expand Up @@ -2206,15 +2207,24 @@
# def_name += match.group(3)
if (match.group(1) == "define") and (def_name not in defs_tmp):
eq_ind = line[match.end(0) :].find(" ")
if eq_ind < 0:
eq_ind = line[match.end(0) :].find("\t")

if eq_ind >= 0:
# Handle multiline macros
if line.rstrip()[-1] == "\\":
defs_tmp[def_name] = line[match.end(0) + eq_ind : -1].strip()
def_value = line[match.end(0) + eq_ind : -1].strip()
def_cont_name = def_name
else:
defs_tmp[def_name] = line[match.end(0) + eq_ind :].strip()
def_value = line[match.end(0) + eq_ind :].strip()
else:
defs_tmp[def_name] = "True"
def_value = "True"

# are there arguments to parse?
if match.group(3):
def_value = (match.group(4), def_value)

defs_tmp[def_name] = def_value
elif (match.group(1) == "undef") and (def_name in defs_tmp):
defs_tmp.pop(def_name, None)
log.debug(f"{line.strip()} !!! Define statement({i + 1})")
Expand Down Expand Up @@ -2265,8 +2275,16 @@
continue
def_regex = def_regexes.get(def_tmp)
if def_regex is None:
def_regex = re.compile(rf"\b{def_tmp}\b")
if isinstance(value, tuple):
def_regex = expand_def_func_macro(def_tmp, value)
else:
def_regex = re.compile(rf"\b{def_tmp}\b")

def_regexes[def_tmp] = def_regex

if isinstance(def_regex, tuple):
def_regex, value = def_regex

line_new, nsubs = def_regex.subn(value, line)
if nsubs > 0:
log.debug(
Expand All @@ -2275,3 +2293,29 @@
line = line_new
output_file.append(line)
return output_file, pp_skips, pp_defines, defs_tmp


def expand_def_func_macro(def_name: str, def_value: tuple[str, str]):
def_args, sub = def_value
def_args = def_args.split(",")
regex = re.compile(rf"\b{def_name}\s*\({','.join(['(.*)']*len(def_args))}\)")

for i, arg in enumerate(def_args):
arg = arg.strip()
sub = re.sub(rf"\b({arg})\b", rf"\\{i + 1}", sub)

return regex, sub


def append_multiline_macro(pp_defs: dict, def_name: str, line: str):
def_value = pp_defs[def_name]
def_args = None
if isinstance(def_value, tuple):
def_args, def_value = def_value

def_value += line

if def_args is not None:
def_value = (def_args, def_value)

pp_defs[def_name] = def_value
11 changes: 7 additions & 4 deletions fortls/regex_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,14 @@
FREE_FORMAT_TEST: Pattern = compile(r"[ ]{1,4}[a-z]", I)
# Preprocessor matching rules
DEFINED: Pattern = compile(r"defined[ ]*\(?[ ]*([a-z_]\w*)[ ]*\)?", I)
PP_REGEX: Pattern = compile(r"#(if |ifdef|ifndef|else|elif|endif)")
PP_DEF: Pattern = compile(r"#(define|undef)[ ]*([\w]+)(\((\w+(,[ ]*)?)+\))?", I)
PP_REGEX: Pattern = compile(r"[ ]*#[ ]*(if |ifdef|ifndef|else|elif|endif)")
PP_DEF: Pattern = compile(
r"[ ]*#[ ]*(define|undef)[ ]+(\w+)(\([ ]*([\w][ \w,]*)*[ ]*\))?",
Fixed Show fixed Hide fixed
I,
)
PP_DEF_TEST: Pattern = compile(r"(![ ]*)?defined[ ]*\([ ]*(\w*)[ ]*\)$", I)
PP_INCLUDE: Pattern = compile(r"#include[ ]*([\"\w\.]*)", I)
PP_ANY: Pattern = compile(r"(^#:?\w+)")
PP_INCLUDE: Pattern = compile(r"[ ]*#[ ]*include[ ]+([\"\w\.]*)", I)
PP_ANY: Pattern = compile(r"(^[ ]*#[ ]*\w*:?\w+)")
# Context matching rules
CALL: Pattern = compile(r"[ ]*CALL[ ]+[\w%]*$", I)
INT_STMNT: Pattern = compile(r"^[ ]*[a-z]*$", I)
Expand Down
3 changes: 2 additions & 1 deletion test/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def test_version_update_pypi():
s = LangServer(conn=JSONRPC2Connection(ReadWriter(stdin, stdout)), settings=args)
s.root_path = (Path(__file__).parent / "test_source").resolve()
did_update = s._update_version_pypi(test=True)
assert did_update
isconda = os.path.exists(os.path.join(sys.prefix, "conda-meta"))
assert not did_update if isconda else did_update

s.disable_autoupdate = True
did_update = s._update_version_pypi()
Expand Down
28 changes: 24 additions & 4 deletions test/test_preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def check_return(result_array, checks):
string += hover_req(file_path, 30, 23)
file_path = root_dir / "preproc_if_elif_skip.F90"
string += hover_req(file_path, 30, 23)
file_path = root_dir / "preproc_spacing_arg_defs.F90"
string += hover_req(file_path, 11, 20)
string += hover_req(file_path, 18, 17)
string += hover_req(file_path, 20, 13)
config = str(root_dir / ".pp_conf.json")
errcode, results = run_request(string, ["--config", config])
assert errcode == 0
Expand All @@ -52,12 +56,12 @@ def check_return(result_array, checks):
"```fortran90\n#define PETSC_ERR_INT_OVERFLOW 84\n```",
"```fortran90\n#define varVar 55\n```",
(
"```fortran90\n#define ewrite if (priority <= 3) write((priority),"
" format)\n```"
"```fortran90\n#define ewrite(priority, format)"
" if (priority <= 3) write((priority), format)\n```"
),
(
"```fortran90\n#define ewrite2 if (priority <= 3) write((priority),"
" format)\n```"
"```fortran90\n#define ewrite2(priority, format)"
" if (priority <= 3) write((priority), format)\n```"
),
"```fortran90\n#define SUCCESS .true.\n```",
"```fortran90\nREAL, CONTIGUOUS, POINTER, DIMENSION(:) :: var1\n```",
Expand All @@ -68,6 +72,22 @@ def check_return(result_array, checks):
"```fortran90\nINTEGER, PARAMETER :: res = 0+1+0+0\n```",
"```fortran90\nINTEGER, PARAMETER :: res = 0+0+0+1\n```",
"```fortran90\nINTEGER, PARAMETER :: res = 1+0+0+0\n```",
"```fortran90\n#define MAYBEWRAP(PROCEDURE) PROCEDURE\n```",
"```fortran90\nSUBROUTINE test_type_set_test()\n```",
"```fortran90\n#define MACROARGS(x , y ) x + y\n```",
)
assert len(ref_results) == len(results) - 1
check_return(results[1:], ref_results)


# def test_FortranFile_pp():
# from fortls import FortranFile

# root_dir = test_dir / "pp"
# # file_path = root_dir / "preproc.F90"
# file_path = root_dir / "preproc_spacing_arg_defs.F90"
# ff = FortranFile(file_path)
# ff.load_from_disk()
# include_dirs = {root_dir / "include"}
# file_ast = ff.parse(include_dirs=include_dirs, debug=True)
# assert file_ast
5 changes: 5 additions & 0 deletions test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def test_workspace_symbols():
def check_return(result_array):
# Expected objects
objs = (
["argtest", 13, 16],
["test", 6, 7],
["test_abstract", 2, 0],
["test_associate_block", 2, 0],
Expand All @@ -196,7 +197,11 @@ def check_return(result_array):
["test_str1", 13, 5],
["test_str2", 13, 5],
["test_sub", 6, 8],
["test_type", 5, 5],
["test_type_set_test", 6, 23],
["test_vis_mod", 2, 0],
["the_test", 13, 15],
["wrap_test_type_set_test", 6, 31],
)
assert len(result_array) == len(objs)
for i, obj in enumerate(objs):
Expand Down
22 changes: 22 additions & 0 deletions test/test_source/pp/include/indent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
!! sample code adapted from json-fortran/json_macros.inc

# define SPACING_TEST
# define FILE_ENCODING ,encoding='UTF-8'

# ifdef __GFORTRAN__
! gfortran uses cpp in old-school compatibility mode so
! the # stringify and ## concatenate operators don't work
! but we can use C/C++ style comment to ensure PROCEDURE is
! correctly tokenized and prepended with 'wrap_' when the
! macro is expanded
# define MAYBEWRAP(PROCEDURE) PROCEDURE , wrap_/**/PROCEDURE
# else
! Intel's fpp does support the more contemporary ## concatenation
! operator, but doesn't treat the C/C++ comments the same way.
! If you use the gfortran approach and pass the -noB switch to
! fpp, the macro will expand, but with a space between wrap_ and
! whatever PROCEDURE expands to
# define MAYBEWRAP(PROCEDURE) PROCEDURE
# endif

# define MACROARGS( x , y ) x + y
40 changes: 40 additions & 0 deletions test/test_source/pp/preproc_spacing_arg_defs.F90
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
program preprocessor_spacing_arg_defs
implicit none

#include "indent.h"

type :: test_type
private
integer, public :: test_int

contains
generic, public :: set_test => MAYBEWRAP(test_type_set_test)
procedure :: MAYBEWRAP(test_type_set_test)

end type test_type

type(test_type) :: the_test
integer :: argtest

call the_test%set_test()

argtest = MACROARGS(the_test%test_int, 4)

contains
subroutine test_type_set_test(me)
implicit none

class(test_type), intent(inout) :: me

me%test_int = 3
end subroutine test_type_set_test

subroutine wrap_test_type_set_test(me)
implicit none

class(test_type), intent(inout) :: me

me%test_int = 5
end subroutine wrap_test_type_set_test

end program preprocessor_spacing_arg_defs