Skip to content

Commit 6224391

Browse files
committed
Fix indentation of OpenMP directives
!$OMP lines are now consistently treated as comments !$ conditional Fortran code is handled as before: `!$` is moved to beginning of line and Fortran code is prettified fixes #35
1 parent 3fbf588 commit 6224391

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

fprettify/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,11 +1212,7 @@ def preprocess_line(f_line, lines, comments, filename, line_nr):
12121212
do_format = False
12131213
is_fypp = False
12141214

1215-
if OMP_DIR_RE.search(f_line):
1216-
# move '!$OMP' to line start, otherwise don't format omp directives
1217-
lines = ['!$OMP' + (len(l) - len(l.lstrip())) *
1218-
' ' + OMP_DIR_RE.sub('', l, count=1) for l in lines]
1219-
elif EMPTY_RE.search(f_line): # empty lines including comment lines
1215+
if EMPTY_RE.search(f_line): # empty lines including comment lines
12201216
if any(comments):
12211217
if lines[0].startswith(' '):
12221218
# indent comment lines only if they were not indented before.

fprettify/fparse_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ def next_fortran_line(self):
180180
# but remember to convert them back
181181

182182
what_omp = ''
183-
if OMP_SUBS_RE.search(line):
184-
what_omp = OMP_SUBS_RE.search(line).group(1)
183+
if OMP_RE.search(line) and not OMP_DIR_RE.search(line):
184+
what_omp = OMP_RE.search(line).group(1)
185185
line = line.replace(what_omp, '', 1)
186186
line_start = 0
187187

fprettify/tests/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,26 @@ def test_do(self):
398398

399399
self.assert_fprettify_result([], instring, instring)
400400

401+
def test_omp(self):
402+
"""test formatting of omp directives"""
403+
instring = ("PROGRAM test_omp\n"
404+
" !$OMP PARALLEL DO\n"
405+
"b=4\n"
406+
"!$a=b\n"
407+
" !$ c=b\n"
408+
"!$OMP END PARALLEL DO\n"
409+
"END PROGRAM")
410+
outstring = ("PROGRAM test_omp\n"
411+
" !$OMP PARALLEL DO\n"
412+
" b = 4\n"
413+
"!$ a = b\n"
414+
"!$ c = b\n"
415+
"!$OMP END PARALLEL DO\n"
416+
"END PROGRAM")
417+
418+
self.assert_fprettify_result([], instring, outstring)
419+
420+
401421
def addtestmethod(testcase, fpath, ffile):
402422
"""add a test method for each example."""
403423

0 commit comments

Comments
 (0)