@@ -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-
105255class MissingImplicitNoneRule (GenericRule ):
106256 """
107257 ``IMPLICIT NONE`` must be present in all scoping units but may be omitted
0 commit comments