Skip to content

Commit 1e49593

Browse files
committed
Another fix OpenMP-conditional lines
a white space after `!$` is required, otherwise it is a normal comment line
1 parent 6224391 commit 1e49593

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

fprettify/__init__.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
sys.stdin = utf8_reader(sys.stdin)
8989
sys.stdout = utf8_writer(sys.stdout)
9090

91-
from .fparse_utils import (VAR_DECL_RE, OMP_RE, OMP_DIR_RE,
91+
from .fparse_utils import (VAR_DECL_RE, OMP_COND_RE,
9292
InputStream, CharFilter,
9393
FprettifyException, FprettifyParseException, FprettifyInternalException,
9494
CPP_RE, NOTFORTRAN_LINE_RE, FYPP_LINE_RE, RE_FLAGS, STR_OPEN_RE)
@@ -1058,7 +1058,7 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_in
10581058

10591059
auto_align, auto_format, in_format_off_block = parse_fprettify_directives(
10601060
lines, comment_lines, in_format_off_block, orig_filename, stream.line_nr)
1061-
f_line, lines, is_omp, is_omp_conditional = preprocess_omp(
1061+
f_line, lines, is_omp_conditional = preprocess_omp(
10621062
f_line, lines)
10631063
f_line, lines, label = preprocess_labels(f_line, lines)
10641064

@@ -1129,7 +1129,7 @@ def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_in
11291129

11301130
# rm subsequent blank lines
11311131
skip_blank = EMPTY_RE.search(
1132-
f_line) and not any(comments) and not is_omp and not label
1132+
f_line) and not any(comments) and not is_omp_conditional and not label
11331133

11341134

11351135
def format_comments(lines, comments, strip_comments):
@@ -1182,13 +1182,12 @@ def parse_fprettify_directives(lines, comment_lines, in_format_off_block, filena
11821182
def preprocess_omp(f_line, lines):
11831183
"""convert omp conditional to normal fortran"""
11841184

1185-
is_omp = OMP_RE.search(f_line)
1186-
is_omp_conditional = bool(is_omp and not OMP_DIR_RE.search(f_line))
1185+
is_omp_conditional = bool(OMP_COND_RE.search(f_line))
11871186
if is_omp_conditional:
1188-
f_line = OMP_RE.sub(' ', f_line, count=1)
1189-
lines = [OMP_RE.sub(' ', l, count=1) for l in lines]
1187+
f_line = OMP_COND_RE.sub(' ', f_line, count=1)
1188+
lines = [OMP_COND_RE.sub(' ', l, count=1) for l in lines]
11901189

1191-
return [f_line, lines, is_omp, is_omp_conditional]
1190+
return [f_line, lines, is_omp_conditional]
11921191

11931192
def preprocess_labels(f_line, lines):
11941193
"""remove statement labels"""
@@ -1370,13 +1369,13 @@ def write_formatted_line(outfile, indent, lines, orig_lines, indent_special, lle
13701369
label_use = ''
13711370

13721371
if ind_use + line_length <= (llength+1): # llength (default 132) plus 1 newline char
1373-
outfile.write('!$' * is_omp_conditional + label_use +
1374-
' ' * (ind_use - 2 * is_omp_conditional - len(label_use) +
1372+
outfile.write('!$ ' * is_omp_conditional + label_use +
1373+
' ' * (ind_use - 3 * is_omp_conditional - len(label_use) +
13751374
len(line) - len(line.lstrip(' '))) +
13761375
line.lstrip(' '))
13771376
elif line_length <= (llength+1):
1378-
outfile.write('!$' * is_omp_conditional + label_use + ' ' *
1379-
((llength+1) - 2 * is_omp_conditional - len(label_use) -
1377+
outfile.write('!$ ' * is_omp_conditional + label_use + ' ' *
1378+
((llength+1) - 3 * is_omp_conditional - len(label_use) -
13801379
len(line.lstrip(' '))) + line.lstrip(' '))
13811380

13821381
log_message(LINESPLIT_MESSAGE+" (limit: "+str(llength)+")", "warning",

fprettify/fparse_utils.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
VAR_DECL_RE = re.compile(
3131
r"^ *(?P<type>integer(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type) *(?P<parameters>\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))? *(?P<attributes>(?: *, *[a-zA-Z_0-9]+(?: *\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))?)+)? *(?P<dpnt>::)?(?P<vars>[^\n]+)\n?", RE_FLAGS)
3232

33-
# FIXME: unify omp regular expressions
34-
OMP_DIR_RE = re.compile(r"^\s*(!\$omp)", RE_FLAGS)
35-
OMP_RE = re.compile(r"^\s*(!\$)", RE_FLAGS)
36-
OMP_SUBS_RE = re.compile(r"^\s*(!\$(omp)?)", RE_FLAGS)
33+
OMP_COND_RE = re.compile(r"^\s*(!\$ )", RE_FLAGS)
3734

3835
# supported preprocessors
3936
FYPP_LINE_STR = r"^(#!|#:|\$:|@:)"
@@ -179,9 +176,14 @@ def next_fortran_line(self):
179176
# convert OMP-conditional fortran statements into normal fortran statements
180177
# but remember to convert them back
181178

182-
what_omp = ''
183-
if OMP_RE.search(line) and not OMP_DIR_RE.search(line):
184-
what_omp = OMP_RE.search(line).group(1)
179+
what_omp = OMP_COND_RE.search(line)
180+
181+
if what_omp:
182+
what_omp = what_omp.group(1)
183+
else:
184+
what_omp = ''
185+
186+
if what_omp:
185187
line = line.replace(what_omp, '', 1)
186188
line_start = 0
187189

fprettify/tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,18 @@ def test_omp(self):
404404
" !$OMP PARALLEL DO\n"
405405
"b=4\n"
406406
"!$a=b\n"
407+
"!$ a=b\n"
407408
" !$ c=b\n"
409+
"!$acc parallel loop\n"
408410
"!$OMP END PARALLEL DO\n"
409411
"END PROGRAM")
410412
outstring = ("PROGRAM test_omp\n"
411413
" !$OMP PARALLEL DO\n"
412414
" b = 4\n"
415+
"!$a=b\n"
413416
"!$ a = b\n"
414417
"!$ c = b\n"
418+
"!$acc parallel loop\n"
415419
"!$OMP END PARALLEL DO\n"
416420
"END PROGRAM")
417421

0 commit comments

Comments
 (0)