Skip to content

Commit 5568d38

Browse files
committed
tmp
1 parent c5b2882 commit 5568d38

File tree

4 files changed

+172
-5
lines changed

4 files changed

+172
-5
lines changed

lint_rules/lint_rules/ifs_arpege_coding_standards.py

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,162 @@ def check_subroutine(cls, subroutine, rule_report, config, **kwargs):
7171
if literal.kind is None:
7272
rule_report.add(f'Real/Float literal without kind specifier "{literal}"', node)
7373

74+
@classmethod
75+
def fix_subroutinei_test_2(cls, subroutine, rule_report, config, sourcefile=None):
76+
"""
77+
...
78+
"""
79+
for node, literals in literal_nodes:
80+
literal_map = {}
81+
for literal in literals:
82+
if literal.kind is None and 'e' not in literal.value.lower() and 'd' not in literal.value.lower():
83+
literal_map[literal] = FloatLiteral(value=literal.value, kind='JPRB')
84+
if literal_map:
85+
# fixed_node = SubstituteExpressions(literal_map).visit(node)
86+
# for key in literal_map:
87+
# fixed_node = re.sub(rf'{re.escape()}',
88+
# , content, flags = re.S)
89+
indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
90+
fixed_node_str = fgen(fixed_node, depth=indent)
91+
with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
92+
f.write(fixed_node_str)
93+
content_new = re.sub(rf'{re.escape(node.source.string)}',
94+
fixed_node_str, content, flags = re.S)
95+
content = content_new
96+
with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
97+
f.write(content_new)
98+
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
99+
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
100+
diff_str = '\n'.join(list(diff))
101+
# print(f"---{sourcefile.path}------")
102+
# print(diff_str)
103+
# print(f"--------------------------")
104+
with open (f'loki_lint_{subroutine.name}.approach_2.patch', 'w') as f:
105+
f.write(diff_str)
106+
f.write('\n')
107+
108+
@classmethod
109+
def fix_subroutine_test(cls, subroutine, rule_report, config, sourcefile=None):
110+
"""
111+
...
112+
"""
113+
# sourcefile = subroutine.source.file
114+
print(f"fix_subroutine: subroutine: {subroutine} | subroutine.source: {subroutine.source} | subroutine.source.file: {subroutine.source.file}")
115+
original_content = read_file(str(sourcefile.path))
116+
content = original_content
117+
literals = FindFloatLiterals(with_ir_node=False).visit(subroutine.body)
118+
literal_map = {}
119+
for literal in literals:
120+
if literal.kind is None:
121+
literal_map[literal] = FloatLiteral(value=literal.value, kind='JPRB')
122+
# content_new = content
123+
if literal_map:
124+
for key in literal_map:
125+
# print(f"replace ")
126+
# content_new = re.sub(rf'{re.escape(str(key))}', rf'{re.escape(str(literal_map[key]))}', content, flags = re.S)
127+
content_new = re.sub(rf'{re.escape(str(key))}', str(literal_map[key]), content, flags = re.S)
128+
content = content_new
129+
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
130+
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
131+
diff_str = '\n'.join(list(diff))
132+
# print(f"---{sourcefile.path}------")
133+
# print(diff_str)
134+
# print(f"--------------------------")
135+
with open (f'loki_lint_{subroutine.name}.approach_2.patch', 'w') as f:
136+
f.write(diff_str)
137+
f.write('\n')
138+
139+
"""
140+
for node, literals in literal_nodes:
141+
literal_map = {}
142+
for literal in literals:
143+
if literal.kind is None:
144+
literal_map[literal] = FloatLiteral(value=literal.value, kind='JPRB')
145+
if literal_map:
146+
# fixed_node = SubstituteExpressions(literal_map).visit(node)
147+
# indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
148+
# fixed_node_str = fgen(fixed_node, depth=indent)
149+
# with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
150+
#  f.write(fixed_node_str)
151+
# content_new = re.sub(rf'{re.escape(node.source.string)}',
152+
# fixed_node_str, content, flags = re.S)
153+
# content = content_new
154+
with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
155+
f.write(content_new)
156+
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
157+
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
158+
diff_str = '\n'.join(list(diff))
159+
# print(f"---{sourcefile.path}------")
160+
# print(diff_str)
161+
# print(f"--------------------------")
162+
with open (f'loki_lint_{subroutine.name}.approach_2.patch', 'w') as f:
163+
f.write(diff_str)
164+
f.write('\n')
165+
"""
166+
74167
@classmethod
75168
def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
76169
"""
77170
...
78171
"""
172+
# sourcefile = subroutine.source.file
173+
print(f"fix_subroutine: subroutine: {subroutine} | subroutine.source: {subroutine.source} | subroutine.source.file: {subroutine.source.file}")
174+
original_content = read_file(str(sourcefile.path))
175+
content = original_content
176+
literal_nodes = FindFloatLiterals(with_ir_node=True).visit(subroutine.body)
177+
content_new = None
178+
for node, literals in literal_nodes:
179+
# print(f"node.source: {node.source.string} | {type(node.source.string)}")
180+
literal_map = {}
181+
for literal in literals:
182+
if literal.kind is None and 'e' not in str(literal.value).lower() and 'd' not in str(literal.value).lower():
183+
literal_map[literal] = FloatLiteral(value=literal.value, kind='JPRB')
184+
if literal_map:
185+
# fixed_node = SubstituteExpressions(literal_map).visit(node)
186+
fixed_node = node.source.string
187+
# if hasattr(node, 'comment') and node.comment is not None:
188+
# comment = node.comment
189+
# fixed_node._update(comment=None)
190+
# else:
191+
# comment = None
192+
for key in literal_map:
193+
fixed_node = re.sub(rf'{re.escape(str(key))}',
194+
str(literal_map[key]), fixed_node, flags = re.S)
195+
# indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
196+
# fixed_node_str = fgen(fixed_node, depth=indent)
197+
# if comment is not None:
198+
# fixed_node._update(comment=comment)
199+
fixed_node_str = str(fixed_node)
200+
# with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
201+
# f.write(fixed_node_str)
202+
content_new = re.sub(rf'{re.escape(node.source.string)}',
203+
fixed_node_str, content, flags = re.S)
204+
content = content_new
205+
# with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
206+
# f.write(content_new)
207+
if content_new is not None:
208+
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
209+
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
210+
diff_str = '\n'.join(list(diff))
211+
# print(f"---{sourcefile.path}------")
212+
# print(diff_str)
213+
# print(f"--------------------------")
214+
with open (f'loki_lint_{subroutine.name}.patch', 'w') as f:
215+
f.write(diff_str)
216+
f.write('\n')
217+
218+
@classmethod
219+
def fix_subroutine_working(cls, subroutine, rule_report, config, sourcefile=None):
220+
"""
221+
...
222+
"""
223+
# sourcefile = subroutine.source.file
224+
print(f"fix_subroutine: subroutine: {subroutine} | subroutine.source: {subroutine.source} | subroutine.source.file: {subroutine.source.file}")
79225
original_content = read_file(str(sourcefile.path))
80226
content = original_content
81227
literal_nodes = FindFloatLiterals(with_ir_node=True).visit(subroutine.body)
82228
for node, literals in literal_nodes:
229+
# print(f"node.source: {node.source.string} | {type(node.source.string)}")
83230
literal_map = {}
84231
for literal in literals:
85232
if literal.kind is None:
@@ -88,9 +235,13 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
88235
fixed_node = SubstituteExpressions(literal_map).visit(node)
89236
indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
90237
fixed_node_str = fgen(fixed_node, depth=indent)
238+
with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
239+
f.write(fixed_node_str)
91240
content_new = re.sub(rf'{re.escape(node.source.string)}',
92241
fixed_node_str, content, flags = re.S)
93242
content = content_new
243+
with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
244+
f.write(content_new)
94245
diff = difflib.unified_diff(original_content.splitlines(), content_new.splitlines(),
95246
f'a/{sourcefile.path}', f'b/{sourcefile.path}', lineterm='')
96247
diff_str = '\n'.join(list(diff))
@@ -101,7 +252,6 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
101252
f.write(diff_str)
102253
f.write('\n')
103254

104-
105255
class MissingImplicitNoneRule(GenericRule):
106256
"""
107257
``IMPLICIT NONE`` must be present in all scoping units but may be omitted

loki/expression/mappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def map_quotient(self, expr, enclosing_prec, *args, **kwargs):
173173
numerator = self.rec_with_force_parens_around(expr.numerator, PREC_PRODUCT, *args, **kwargs)
174174
kwargs['force_parens_around'] = self.multiplicative_primitives
175175
denominator = self.rec_with_force_parens_around(expr.denominator, PREC_PRODUCT, *args, **kwargs)
176-
return self.parenthesize_if_needed(self.format('%s / %s', numerator, denominator),
176+
return self.parenthesize_if_needed(self.format('%s/%s', numerator, denominator),
177177
enclosing_prec, PREC_PRODUCT)
178178

179179
def map_parenthesised_add(self, expr, enclosing_prec, *args, **kwargs):
@@ -206,7 +206,7 @@ def map_inline_do(self, expr, enclosing_prec, *args, **kwargs):
206206

207207
def map_array_subscript(self, expr, enclosing_prec, *args, **kwargs):
208208
name_str = self.rec(expr.aggregate, PREC_NONE, *args, **kwargs)
209-
index_str = self.join_rec(', ', expr.index_tuple, PREC_NONE, *args, **kwargs)
209+
index_str = self.join_rec(',', expr.index_tuple, PREC_NONE, *args, **kwargs)
210210
return f'{name_str}({index_str})'
211211

212212
map_string_subscript = map_array_subscript

loki/frontend/tests/test_frontends.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from loki import (
2323
Module, Subroutine, FindVariables, BasicType, config, Sourcefile,
2424
RawSource, RegexParserClass, ProcedureType, DerivedType,
25-
PreprocessorDirective, config_override
25+
PreprocessorDirective, config_override, FindLiterals
2626
)
2727
from loki.build import jit_compile, clean_test
2828
from loki.expression import symbols as sym
@@ -53,7 +53,23 @@ def fixture_reset_regex_frontend_timeout():
5353
yield
5454
config['regex-frontend-timeout'] = original_timeout
5555

56+
@pytest.mark.parametrize('frontend', available_frontends())
57+
def test_literals_kind(frontend):
58+
"""
59+
"""
5660

61+
fcode = """
62+
SUBROUTINE SOME_SUBROUTINE()
63+
REAL :: A
64+
A = 0.d0
65+
END SUBROUTINE
66+
""".strip()
67+
routine = Subroutine.from_source(fcode, frontend=frontend)
68+
literals = FindLiterals().visit(routine.body)
69+
# print(f"literals: {literals}")
70+
for literal in literals:
71+
print(f"literal: '{literal}' | {literal.kind}")
72+
5773
@pytest.mark.parametrize('frontend', available_frontends())
5874
def test_check_alloc_opts(here, frontend):
5975
"""

loki/ir/pprint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ def format_line(self, *items, comment=None, no_wrap=False, no_indent=False):
150150
required to observe the line width limit.
151151
:rtype: str
152152
"""
153-
if not no_indent:
153+
# print(f"items: {items}")
154+
if not no_indent and items != ('',):
154155
items = [self.indent, *items]
155156
if no_wrap:
156157
# Simply concatenate items and append the comment

0 commit comments

Comments
 (0)