77import argparse
88import re
99from unidiff import PatchSet
10+ import json
11+ import sys
12+
1013
1114if "ZEPHYR_BASE" not in os .environ :
1215 exit ("$ZEPHYR_BASE environment variable undefined." )
@@ -45,11 +48,89 @@ def parse_args():
4548 help = "Commit range in the form: a..b" )
4649 parser .add_argument ("-o" , "--output" , required = False ,
4750 help = "Print violation into a file" )
51+ parser .add_argument ("-s" , "--sarif" , required = False ,
52+ help = "Genrate sarif file" )
4853 return parser .parse_args ()
4954
55+ class CocciToSarif :
56+
57+ def __init__ (self , tags = []):
58+ self .tags = tags
59+ self .results = []
60+ self .rules = {}
61+
62+
63+ def add_rule (self , rule_id , short_description , full_description ,
64+ help_text , level , tags = []):
65+ self .rules [rule_id ] = {
66+ "id" : rule_id ,
67+ # This appears as the title on the list and individual issue view
68+ "shortDescription" : {"text" : short_description },
69+ # This appears as a sub heading on the individual issue view
70+ "fullDescription" : {"text" : full_description },
71+ # This appears on the individual issue view in an expandable box
72+ "help" : {
73+ "markdown" : full_description ,
74+ # This property is not used if markdown is provided, but is required
75+ "text" : "" ,
76+ },
77+ "defaultConfiguration" : {"level" : level },
78+ "properties" : {"tags" : tags },
79+ }
80+
81+ def add_result (self , rule_id , target_file , line , message ):
82+ result = {
83+ "ruleId" : rule_id ,
84+ # This appears in the line by line highlight on the individual issue view
85+ "message" : {"text" : message },
86+ "locations" : [
87+ {
88+ "physicalLocation" : {
89+ "artifactLocation" : {
90+ "uri" : target_file
91+ },
92+ "region" : {
93+ "startLine" : line
94+ },
95+ }
96+ }
97+ ],
98+ }
99+ self .results .append (result )
100+
101+ def get_sarif (self ):
102+ output = {
103+ "$schema" : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" ,
104+ "version" : "2.1.0" ,
105+ "runs" : [
106+ {
107+ "tool" : {
108+ "driver" : {
109+ "name" : "Cocci" ,
110+ "rules" : self .rules
111+ }
112+ },
113+ "results" : self .results ,
114+ }
115+ ],
116+ }
117+ return output
118+
50119
51120def main ():
52121 args = parse_args ()
122+
123+ if args .sarif :
124+ csarif = CocciToSarif ()
125+ csarif .add_rule (
126+ rule_id = "zephyr.rule_21.1" ,
127+ short_description = "Should not used a reserved identifier" ,
128+ full_description = "Should not used a reserved identifier" ,
129+ help_text = "Should not used a reserved identifier" ,
130+ level = "error" ,
131+ tags = ["cocci" , "zephyr" ]
132+ )
133+
53134 if not args .commits :
54135 exit ("missing commit range" )
55136
@@ -104,10 +185,17 @@ def main():
104185 for hunk in f :
105186 for line in hunk :
106187 if line .is_added :
107- violation = "{}:{}" . format ( f .path , line .target_line_no )
188+ violation = f" { f .path } : { line .target_line_no } "
108189 if violation in violations :
109190 numViolations += 1
110- if args .output :
191+
192+ if args .sarif :
193+ csarif .add_result (
194+ rule_id = "zephyr.rule_21.1" ,
195+ target_file = f .path ,
196+ line = line .target_line_no ,
197+ message = "\t \n " .join (violations [violation ]))
198+ elif args .output :
111199 with open (args .output , "a+" ) as fp :
112200 fp .write ("{}:{}\n " .format (
113201 violation , "\t \n " .join (
@@ -118,6 +206,11 @@ def main():
118206 violation , "\t \n " .join (
119207 violations [violation ])))
120208
209+ if args .sarif :
210+ csarif_output = csarif .get_sarif ()
211+ with open (args .sarif , "w" ) as fp :
212+ json .dump (csarif_output , fp , indent = 4 )
213+ print ("sarif file generated" )
121214 return numViolations
122215
123216
0 commit comments