Skip to content

Commit c975006

Browse files
committed
rebase intel-specific changes on top of PR fortran-lang#350
1 parent 6245d85 commit c975006

File tree

8 files changed

+59
-12
lines changed

8 files changed

+59
-12
lines changed

fortls/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ def locate_config(root: str) -> str | None:
507507
debug=True,
508508
pp_defs=pp_defs,
509509
include_dirs=include_dirs,
510+
pp_parse_intel=pp_parse_intel,
510511
)
511512
print("\n=========\nObject Tree\n=========\n")
512513
for obj in file_ast.get_scopes():

fortls/langserver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def serve_onChange(self, request: dict):
13241324
# tmp_file.ast.resolve_links(self.obj_tree, self.link_version)
13251325
elif file_obj.preproc:
13261326
file_obj.preprocess(
1327-
pp_defs=self.pp_defs,
1327+
pp_defs=self.pp_defs, pp_parse_intel=self.pp_parse_intel
13281328
)
13291329
self.pp_defs = {**self.pp_defs, **file_obj.pp_defs}
13301330

@@ -1400,6 +1400,7 @@ def update_workspace_file(
14001400
ast_new = file_obj.parse(
14011401
pp_defs=self.pp_defs,
14021402
include_dirs=self.include_dirs,
1403+
pp_parse_intel=self.pp_parse_intel,
14031404
)
14041405
# Add the included read in pp_defs from to the ones specified in the
14051406
# configuration file
@@ -1469,6 +1470,7 @@ def file_init(
14691470
file_ast = file_obj.parse(
14701471
pp_defs=pp_defs,
14711472
include_dirs=include_dirs,
1473+
pp_parse_intel=pp_parse_intel,
14721474
)
14731475
except:
14741476
log.error("Error while parsing file %s", filepath, exc_info=True)

fortls/parsers/internal/parser.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ def preprocess(
11801180
pp_defs: dict = None,
11811181
include_dirs: set = None,
11821182
debug: bool = False,
1183+
pp_parse_intel: bool = False,
11831184
) -> tuple[list, list]:
11841185
if pp_defs is None:
11851186
pp_defs = {}
@@ -1275,6 +1276,7 @@ def parse(
12751276
pp_defs=pp_defs,
12761277
include_dirs=include_dirs,
12771278
debug=debug,
1279+
pp_parse_intel=pp_parse_intel,
12781280
)
12791281
for pp_reg in pp_skips:
12801282
file_ast.start_ppif(pp_reg[0])
@@ -2051,6 +2053,35 @@ def replace_ops(expr: str):
20512053

20522054
return expr
20532055

2056+
def replace_intel_ops(expr: str):
2057+
expr = expr.replace("/=", " != ")
2058+
expr = expr.replace(".AND.", " && ")
2059+
expr = expr.replace(".LT.", " < ")
2060+
expr = expr.replace(".GT.", " > ")
2061+
expr = expr.replace(".EQ.", " == ")
2062+
expr = expr.replace(".LE.", " <= ")
2063+
expr = expr.replace(".GE.", " >= ")
2064+
expr = expr.replace(".NE.", " != ")
2065+
expr = expr.replace(".EQV.", " == ")
2066+
expr = expr.replace(".NEQV.", " != ")
2067+
expr = expr.replace(".NOT.", "!")
2068+
expr = expr.replace(".OR.", " || ")
2069+
expr = expr.replace(".XOR.", " != ") # admittedly a hack...
2070+
expr = expr.replace(".and.", " && ")
2071+
expr = expr.replace(".lt.", " < ")
2072+
expr = expr.replace(".gt.", " > ")
2073+
expr = expr.replace(".eq.", " == ")
2074+
expr = expr.replace(".le.", " <= ")
2075+
expr = expr.replace(".ge.", " >= ")
2076+
expr = expr.replace(".ne.", " != ")
2077+
expr = expr.replace(".eqv.", " == ")
2078+
expr = expr.replace(".neqv.", " != ")
2079+
expr = expr.replace(".not.", "!")
2080+
expr = expr.replace(".or.", " || ")
2081+
expr = expr.replace(".xor.", " != ") # admittedly a hack...
2082+
2083+
return expr
2084+
20542085
def replace_defined(line: str):
20552086
i0 = 0
20562087
out_line = ""
@@ -2083,6 +2114,9 @@ def replace_vars(line: str):
20832114
defs = {}
20842115

20852116
out_line = text
2117+
if pp_parse_intel:
2118+
out_line = replace_intel_ops(out_line)
2119+
20862120
out_line = replace_defined(out_line)
20872121
out_line = replace_vars(out_line)
20882122
try:
@@ -2119,13 +2153,13 @@ def replace_vars(line: str):
21192153
continue
21202154
# Handle conditional statements
21212155
match = FRegex.PP_REGEX.match(line)
2122-
if match and check_pp_prefix(match.group(1)):
2156+
if match and check_pp_prefix(match.group(1), pp_parse_intel):
21232157
output_file.append(line)
21242158
def_name = None
21252159
if_start = False
21262160
# Opening conditional statements
21272161
if match.group(2).lower() == "if ":
2128-
is_path = eval_pp_if(line[match.end(2) :], defs_tmp)
2162+
is_path = eval_pp_if(line[match.end(2) :], defs_tmp, pp_parse_intel)
21292163
if_start = True
21302164
elif match.group(2).lower() == "ifdef":
21312165
if_start = True
@@ -2161,7 +2195,7 @@ def replace_vars(line: str):
21612195
exc_continue = True
21622196
if pp_stack[-1][0] < 0:
21632197
pp_stack[-1][0] = i + 1
2164-
elif eval_pp_if(line[match.end(2) :], defs_tmp):
2198+
elif eval_pp_if(line[match.end(2) :], defs_tmp, pp_parse_intel):
21652199
pp_stack[-1][1] = i + 1
21662200
pp_skips.append(pp_stack.pop())
21672201
pp_stack_group[-1][1] = True

fortls/regex_patterns.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,20 @@ class FortranRegularExpressions:
124124
FREE_FORMAT_TEST: Pattern = compile(r"[ ]{1,4}[a-z]", I)
125125
# Preprocessor matching rules
126126
DEFINED: Pattern = compile(r"defined[ ]*\(?[ ]*([a-z_]\w*)[ ]*\)?", I)
127+
INTEL_FPP_PRE_STR = r"[c!*]DEC\$|[c!*]DIR\$|!MS\$"
128+
INTEL_FPP_PRE: Pattern = compile(f"{INTEL_FPP_PRE_STR}", I)
127129
PP_REGEX: Pattern = compile(
128-
r"[ ]*(#)[ ]*(if |ifdef|ifndef|else|elif|endif)",
130+
rf"[ ]*(#|{INTEL_FPP_PRE_STR})[ ]*(if |ifdef|ifndef|else|elif|endif)",
129131
I,
130132
)
131133
PP_DEF: Pattern = compile(
132-
r"[ ]*(#)[ ]*(define|undef|undefined)" r"[ ]+(\w+)(\([ ]*([ \w,]*?)[ ]*\))?",
134+
rf"[ ]*(#|{INTEL_FPP_PRE_STR})[ ]*(define|undef|undefined)"
135+
r"[ ]+(\w+)(\([ ]*([ \w,]*?)[ ]*\))?",
133136
I,
134137
)
135138
PP_DEF_TEST: Pattern = compile(r"(![ ]*)?defined[ ]*\([ ]*(\w*)[ ]*\)$", I)
136139
PP_INCLUDE: Pattern = compile(r"[ ]*#[ ]*include[ ]+([\"\w\.]*)", I)
137-
PP_ANY: Pattern = compile(r"(^[ ]*(?:#)[ ]*\w*:?\w+)")
140+
PP_ANY: Pattern = compile(rf"(^[ ]*(?:#|{INTEL_FPP_PRE_STR})[ ]*\w*:?\w+)")
138141
# Context matching rules
139142
CALL: Pattern = compile(r"[ ]*CALL[ ]+[\w%]*$", I)
140143
INT_STMNT: Pattern = compile(r"^[ ]*[a-z]*$", I)

test/test_preproc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ def check_return(result_array, checks):
4444
string += hover_req(file_path, 30, 23)
4545
file_path = root_dir / "preproc_spacing_arg_defs.F90"
4646
string += hover_req(file_path, 11, 20)
47-
string += hover_req(file_path, 20, 17)
48-
string += hover_req(file_path, 22, 13)
47+
string += hover_req(file_path, 24, 17)
48+
string += hover_req(file_path, 26, 13)
49+
string += hover_req(file_path, 26, 42)
4950
config = str(root_dir / ".pp_conf.json")
5051
errcode, results = run_request(string, ["--config", config])
5152
assert errcode == 0
@@ -75,6 +76,7 @@ def check_return(result_array, checks):
7576
"```fortran90\n#define MAYBEWRAP(PROCEDURE) PROCEDURE\n```",
7677
"```fortran90\nSUBROUTINE test_type_set_test()\n```",
7778
"```fortran90\n#define MACROARGS(x, y) x + y\n```",
79+
"```fortran90\nINTEGER(KIND=4), PARAMETER :: C_LONG = 4\n```",
7880
)
7981
assert len(ref_results) == len(results) - 1
8082
check_return(results[1:], ref_results)

test/test_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ def check_return(result_array):
198198
["test_str2", 13, 5],
199199
["test_sub", 6, 8],
200200
["test_type", 5, 5],
201-
["test_type_set_test", 6, 25],
201+
["test_type_set_test", 6, 29],
202202
["test_vis_mod", 2, 0],
203203
["the_test", 13, 15],
204-
["wrap_test_type_set_test", 6, 33],
204+
["wrap_test_type_set_test", 6, 37],
205205
)
206206
assert len(result_array) == len(objs)
207207
for i, obj in enumerate(objs):

test/test_source/pp/.pp_conf.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
"incl_suffixes": [".h"],
99
"include_dirs": ["include"],
1010
"pp_defs": { "HAVE_CONTIGUOUS": "" },
11+
"pp_parse_intel": true
1112
}

test/test_source/pp/preproc_spacing_arg_defs.F90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ program preprocessor_spacing_arg_defs
1616
type(test_type) :: the_test
1717
integer :: argtest
1818

19-
INTEGER (KIND=4), PARAMETER :: C_LONG = 4
19+
!DEC$ IF DEFINED(SPACING_TEST).AND.DEFINED(MACROARGS)
20+
INTEGER (KIND=4), PARAMETER :: C_LONG = 4
21+
!DEC$ ELSE
22+
INTEGER (KIND=4), PARAMETER :: C_LONG = 8
23+
!DEC$ ENDIF
2024

2125
call the_test%set_test()
2226

0 commit comments

Comments
 (0)