3535 'MissingKindSpecifierRealLiterals'
3636]
3737
38+ jprd_files = []
3839
3940class FindFloatLiterals (ExpressionFinder ):
4041 """
@@ -170,17 +171,41 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
170171 ...
171172 """
172173 # sourcefile = subroutine.source.file
173- print (f"fix_subroutine: subroutine: { subroutine } | subroutine.source: { subroutine .source } | subroutine.source.file: { subroutine .source .file } " )
174+ print (f"fix_subroutine: subroutine: { subroutine } | subroutine.source: { subroutine .source } | subroutine.source.file: { subroutine .source .file } | { str (sourcefile .path )} " )
175+ orig_file = str (sourcefile .path ).replace ('source/ifs-source/' , '' )
176+ subdir = orig_file .split ('/' )[0 ]
177+ if orig_file in jprd_files :
178+ with open (f"files_to_commit_{ subdir } _jprd.txt" , "a" ) as myfile :
179+ myfile .write (f"{ orig_file } \n " )
180+ kind_spec = 'JPRD'
181+ else :
182+ with open (f"files_to_commit_{ subdir } _jprm.txt" , "a" ) as myfile :
183+ myfile .write (f"{ orig_file } \n " )
184+ kind_spec = 'JPRM'
174185 original_content = read_file (str (sourcefile .path ))
175186 content = original_content
176187 literal_nodes = FindFloatLiterals (with_ir_node = True ).visit (subroutine .body )
177188 content_new = None
189+ imports = FindNodes (ir .Import ).visit (subroutine .spec )
190+ imp_map = {}
191+ parkind1_available = False
192+ substitutions = 0
193+ for imp in imports :
194+ if imp .module .lower () == 'parkind1' :
195+ parkind1_available = True
196+ imp_map [imp ] = imp .clone (symbols = imp .symbols + (imp .symbols [0 ].clone (name = 'JPRQ' ),))
197+ # print(f"imp_map: {imp_map}")
198+ if not parkind1_available :
199+ with open (f"files_skipped_{ subdir } .txt" , "a" ) as myfile :
200+ myfile .write (f"{ orig_file } since parkind1 not avail\n " )
201+ print (f"no parkind1 in { str (sourcefile .path )} avail, thus early exit ..." )
202+ return
178203 for node , literals in literal_nodes :
179204 # print(f"node.source: {node.source.string} | {type(node.source.string)}")
180205 literal_map = {}
181206 for literal in literals :
182207 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' )
208+ literal_map [literal ] = FloatLiteral (value = literal .value , kind = kind_spec )
184209 if literal_map :
185210 # fixed_node = SubstituteExpressions(literal_map).visit(node)
186211 fixed_node = node .source .string
@@ -190,7 +215,7 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
190215 # else:
191216 # comment = None
192217 for key in literal_map :
193- fixed_node = re .sub (rf'{ re .escape (str (key ))} ' ,
218+ fixed_node = re .sub (rf'{ re .escape (str (key ))} (?!(_ { kind_spec } |_JP|[0-9]|[eEdD])) ' ,
194219 str (literal_map [key ]), fixed_node , flags = re .S )
195220 # indent = int((len(node.source.string) - len(node.source.string.lstrip(' ')))/2)
196221 # fixed_node_str = fgen(fixed_node, depth=indent)
@@ -199,12 +224,35 @@ def fix_subroutine(cls, subroutine, rule_report, config, sourcefile=None):
199224 fixed_node_str = str (fixed_node )
200225 # with open (f'loki_lint_{subroutine.name}_new_file_fixed_node_str.F90', 'w') as f:
201226 # f.write(fixed_node_str)
202- content_new = re .sub (rf'{ re .escape (node .source .string )} ' ,
227+ # content_new = re.sub(rf'{re.escape(node.source.string)}(?!_JPRB)(?<!JPRB)$',
228+ content_new , subs = re .subn (rf'{ re .escape (node .source .string )} (?!(_{ kind_spec } |_JP|[0-9]|[eEdD]))' ,
203229 fixed_node_str , content , flags = re .S )
230+ substitutions += subs
204231 content = content_new
205232 # with open (f'loki_lint_{subroutine.name}_new_file.F90', 'w') as f:
206233 # f.write(content_new)
207- if content_new is not None :
234+ if content_new is not None and subs > 0 :
235+ _subroutine = content_new .lower ().find (f'subroutine { subroutine .name .lower ()} ' )
236+ _function = content_new .lower ().find (f'function { subroutine .name .lower ()} ' )
237+ if _subroutine != - 1 :
238+ index = _subroutine
239+ if _function != - 1 :
240+ index = _function
241+ if index != - 1 :
242+ for node in imp_map :
243+ fixed_node = node .source .string
244+ # fixed_node = re.sub(rf'{re.escape(str(key))}',
245+ # str(imp_map[node]), fixed_node, flags = re.S)
246+ # fixed_node_str = f'{str(fixed_node)}, JPRQ'
247+ fixed_node_str = str (fixed_node )
248+ if kind_spec not in fixed_node_str .upper ():
249+ fixed_node_str = f'{ str (fixed_node )} , { kind_spec } '
250+ print (f"fixed_node_str: { fixed_node_str } " )
251+ content_new = re .sub (rf'{ re .escape (node .source .string )} ' ,
252+ fixed_node_str , content [index :], flags = re .S , count = 1 )
253+ # content[index::] = content_new
254+ # content = content[:index-1] + content_new
255+ content_new = content [:index ] + content_new
208256 diff = difflib .unified_diff (original_content .splitlines (), content_new .splitlines (),
209257 f'a/{ sourcefile .path } ' , f'b/{ sourcefile .path } ' , lineterm = '' )
210258 diff_str = '\n ' .join (list (diff ))
0 commit comments