Skip to content

Commit 9ab0d33

Browse files
authored
Merge pull request #11682 from lucasssvaz/codeql
feat(codeql): Add CPP analysis
2 parents d26d7a3 + 3084758 commit 9ab0d33

File tree

2 files changed

+183
-1
lines changed

2 files changed

+183
-1
lines changed

.github/scripts/process_sarif.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python3
2+
3+
# This script is used to process the SARIF file generated by CodeQL and
4+
# to rename files back to .ino and adjust line numbers to match the original .ino files.
5+
6+
import json
7+
import sys
8+
import os
9+
10+
def process_artifact_location(artifact_location, renamed_files):
11+
"""
12+
Process a single artifact location to rename .cpp files back to .ino
13+
"""
14+
if 'uri' in artifact_location:
15+
uri = artifact_location['uri']
16+
if uri in renamed_files:
17+
print(f"Renaming file: {uri} -> {renamed_files[uri]}")
18+
artifact_location['uri'] = renamed_files[uri]
19+
return True
20+
return False
21+
22+
def process_region(region):
23+
"""
24+
Adjust line numbers in a region by decreasing them by 1
25+
"""
26+
if 'startLine' in region:
27+
region['startLine'] = max(1, region['startLine'] - 1)
28+
if 'endLine' in region:
29+
region['endLine'] = max(1, region['endLine'] - 1)
30+
31+
def process_physical_location(physical_location, renamed_files):
32+
"""
33+
Process a physical location to rename files and adjust line numbers
34+
"""
35+
file_renamed = False
36+
37+
if 'artifactLocation' in physical_location:
38+
if process_artifact_location(physical_location['artifactLocation'], renamed_files):
39+
file_renamed = True
40+
41+
# Adjust line numbers if the file was renamed
42+
if file_renamed and 'region' in physical_location:
43+
process_region(physical_location['region'])
44+
45+
return file_renamed
46+
47+
48+
def process_sarif_file(sarif_file, renamed_files_file):
49+
"""
50+
Process SARIF file to rename files back to .ino and adjust line numbers
51+
"""
52+
# Read the renamed files mapping
53+
with open(renamed_files_file, 'r') as f:
54+
renamed_files = json.load(f)
55+
56+
print(f"Loaded {len(renamed_files)} file mappings:")
57+
for cpp_file, ino_file in renamed_files.items():
58+
print(f" {cpp_file} -> {ino_file}")
59+
60+
61+
# Read the SARIF file
62+
with open(sarif_file, 'r') as f:
63+
sarif_data = json.load(f)
64+
65+
files_processed = 0
66+
67+
# Process each run
68+
if 'runs' in sarif_data:
69+
for run in sarif_data['runs']:
70+
# Process results
71+
if 'results' in run:
72+
for result in run['results']:
73+
# Process all locations in the result
74+
if 'locations' in result:
75+
for location in result['locations']:
76+
if 'physicalLocation' in location:
77+
if process_physical_location(location['physicalLocation'], renamed_files):
78+
files_processed += 1
79+
80+
# Process related locations if they exist
81+
if 'relatedLocations' in result:
82+
for location in result['relatedLocations']:
83+
if 'physicalLocation' in location:
84+
if process_physical_location(location['physicalLocation'], renamed_files):
85+
files_processed += 1
86+
87+
# Process artifacts if they exist
88+
if 'artifacts' in run:
89+
for artifact in run['artifacts']:
90+
if 'location' in artifact and 'uri' in artifact['location']:
91+
uri = artifact['location']['uri']
92+
if uri in renamed_files:
93+
artifact['location']['uri'] = renamed_files[uri]
94+
files_processed += 1
95+
96+
print(f"Processed {files_processed} file references")
97+
98+
# Write the processed SARIF file
99+
with open(sarif_file, 'w') as f:
100+
json.dump(sarif_data, f, indent=2)
101+
102+
def main():
103+
if len(sys.argv) != 3:
104+
print("Usage: python3 sarif_nobuild.py <sarif_file> <renamed_files_file>")
105+
sys.exit(1)
106+
107+
sarif_file = sys.argv[1]
108+
renamed_files_file = sys.argv[2]
109+
110+
# Check if files exist
111+
if not os.path.exists(sarif_file):
112+
print(f"SARIF file not found: {sarif_file}")
113+
sys.exit(1)
114+
115+
if not os.path.exists(renamed_files_file):
116+
print(f"Renamed files mapping not found: {renamed_files_file}")
117+
sys.exit(1)
118+
119+
try:
120+
process_sarif_file(sarif_file, renamed_files_file)
121+
print("SARIF file processed successfully")
122+
except Exception as e:
123+
print(f"Error processing SARIF file: {e}")
124+
import traceback
125+
traceback.print_exc()
126+
sys.exit(1)
127+
128+
if __name__ == "__main__":
129+
main()

.github/workflows/codeql.yml

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ on:
77
- master
88
pull_request:
99
paths:
10+
- "**/*.c"
11+
- "**/*.cpp"
12+
- "**/*.h"
13+
- "**/*.ino"
1014
- "**/*.py"
1115
- ".github/workflows/*.yml"
1216
- ".github/workflows/*.yaml"
@@ -17,19 +21,68 @@ jobs:
1721
runs-on: ubuntu-latest
1822
strategy:
1923
matrix:
20-
language: [python, actions]
24+
language: [python, actions, cpp]
2125

2226
steps:
2327
- name: Checkout repository
2428
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2529

30+
- name: Process .ino files
31+
if: matrix.language == 'cpp'
32+
run: |
33+
# Create a mapping file to track renamed files
34+
echo "{}" > renamed_files.json
35+
36+
# Find all .ino files and process them
37+
find . -name "*.ino" -type f | while read -r file; do
38+
echo "Processing $file"
39+
40+
# Get the relative path from repository root
41+
rel_path=$(realpath --relative-to=. "$file")
42+
cpp_path="${rel_path%.ino}.cpp"
43+
44+
# Create new .cpp file with Arduino.h include
45+
echo "#include <Arduino.h>" > "$cpp_path"
46+
47+
# Append the original content
48+
cat "$file" >> "$cpp_path"
49+
50+
# Update the mapping file
51+
jq --arg ino "$rel_path" --arg cpp "$cpp_path" '. += {($cpp): $ino}' renamed_files.json > temp.json && mv temp.json renamed_files.json
52+
53+
# Remove the original .ino file
54+
rm "$file"
55+
56+
echo "Converted $file to $cpp_path"
57+
done
58+
59+
echo "Renamed files mapping:"
60+
cat renamed_files.json
61+
2662
- name: Initialize CodeQL
2763
uses: github/codeql-action/init@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
2864
with:
65+
build-mode: none
2966
languages: ${{ matrix.language }}
3067
config-file: ./.github/codeql/codeql-config.yml
3168

3269
- name: Run CodeQL Analysis
3370
uses: github/codeql-action/analyze@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
3471
with:
3572
category: "/language:${{ matrix.language }}"
73+
output: sarif-results
74+
upload: failure-only
75+
76+
- name: Process SARIF file
77+
if: matrix.language == 'cpp'
78+
run: |
79+
sarif_file="sarif-results/${{ matrix.language }}.sarif"
80+
81+
# Run the Python script to process the SARIF file
82+
python3 .github/scripts/process_sarif.py "$sarif_file" "renamed_files.json"
83+
84+
- name: Upload SARIF file
85+
uses: github/codeql-action/upload-sarif@181d5eefc20863364f96762470ba6f862bdef56b # v3.29.2
86+
with:
87+
sarif_file: sarif-results/${{ matrix.language }}.sarif
88+
category: "/language:${{ matrix.language }}"

0 commit comments

Comments
 (0)