44import sys
55import os
66
7+ def process_artifact_location (artifact_location , renamed_files ):
8+ """
9+ Process a single artifact location to rename .cpp files back to .ino
10+ """
11+ if 'uri' in artifact_location :
12+ uri = artifact_location ['uri' ]
13+ if uri in renamed_files :
14+ print (f"Renaming file: { uri } -> { renamed_files [uri ]} " )
15+ artifact_location ['uri' ] = renamed_files [uri ]
16+ return True
17+ return False
18+
19+ def process_region (region ):
20+ """
21+ Adjust line numbers in a region by decreasing them by 1
22+ """
23+ if 'startLine' in region :
24+ region ['startLine' ] = max (1 , region ['startLine' ] - 1 )
25+ if 'endLine' in region :
26+ region ['endLine' ] = max (1 , region ['endLine' ] - 1 )
27+
28+ def process_physical_location (physical_location , renamed_files ):
29+ """
30+ Process a physical location to rename files and adjust line numbers
31+ """
32+ file_renamed = False
33+
34+ if 'artifactLocation' in physical_location :
35+ if process_artifact_location (physical_location ['artifactLocation' ], renamed_files ):
36+ file_renamed = True
37+
38+ # Adjust line numbers if the file was renamed
39+ if file_renamed and 'region' in physical_location :
40+ process_region (physical_location ['region' ])
41+
42+ return file_renamed
43+
744def process_sarif_file (sarif_file , renamed_files_file ):
845 """
946 Process SARIF file to rename files back to .ino and adjust line numbers
@@ -12,38 +49,46 @@ def process_sarif_file(sarif_file, renamed_files_file):
1249 with open (renamed_files_file , 'r' ) as f :
1350 renamed_files = json .load (f )
1451
52+ print (f"Loaded { len (renamed_files )} file mappings:" )
53+ for cpp_file , ino_file in renamed_files .items ():
54+ print (f" { cpp_file } -> { ino_file } " )
55+
1556 # Read the SARIF file
1657 with open (sarif_file , 'r' ) as f :
1758 sarif_data = json .load (f )
1859
19- # Process each result
20- if 'runs' in sarif_data and len (sarif_data ['runs' ]) > 0 :
21- results = sarif_data ['runs' ][0 ].get ('results' , [])
22-
23- for result in results :
24- if 'locations' in result and len (result ['locations' ]) > 0 :
25- location = result ['locations' ][0 ]
26-
27- if 'physicalLocation' in location :
28- physical_location = location ['physicalLocation' ]
29-
30- if 'artifactLocation' in physical_location :
31- uri = physical_location ['artifactLocation' ].get ('uri' )
32-
33- # Check if this file was originally a .ino file
60+ files_processed = 0
61+
62+ # Process each run
63+ if 'runs' in sarif_data :
64+ for run in sarif_data ['runs' ]:
65+ # Process results
66+ if 'results' in run :
67+ for result in run ['results' ]:
68+ # Process all locations in the result
69+ if 'locations' in result :
70+ for location in result ['locations' ]:
71+ if 'physicalLocation' in location :
72+ if process_physical_location (location ['physicalLocation' ], renamed_files ):
73+ files_processed += 1
74+
75+ # Process related locations if they exist
76+ if 'relatedLocations' in result :
77+ for location in result ['relatedLocations' ]:
78+ if 'physicalLocation' in location :
79+ if process_physical_location (location ['physicalLocation' ], renamed_files ):
80+ files_processed += 1
81+
82+ # Process artifacts if they exist
83+ if 'artifacts' in run :
84+ for artifact in run ['artifacts' ]:
85+ if 'location' in artifact and 'uri' in artifact ['location' ]:
86+ uri = artifact ['location' ]['uri' ]
3487 if uri in renamed_files :
35- # Rename the file back to .ino
36- physical_location ['artifactLocation' ]['uri' ] = renamed_files [uri ]
37-
38- # Adjust line numbers if they exist
39- if 'region' in physical_location :
40- region = physical_location ['region' ]
41-
42- if 'startLine' in region :
43- region ['startLine' ] = max (1 , region ['startLine' ] - 1 )
88+ artifact ['location' ]['uri' ] = renamed_files [uri ]
89+ files_processed += 1
4490
45- if 'endLine' in region :
46- region ['endLine' ] = max (1 , region ['endLine' ] - 1 )
91+ print (f"Processed { files_processed } file references" )
4792
4893 # Write the processed SARIF file
4994 with open (sarif_file , 'w' ) as f :
@@ -71,6 +116,8 @@ def main():
71116 print ("SARIF file processed successfully" )
72117 except Exception as e :
73118 print (f"Error processing SARIF file: { e } " )
119+ import traceback
120+ traceback .print_exc ()
74121 sys .exit (1 )
75122
76123if __name__ == "__main__" :
0 commit comments