@@ -1182,7 +1182,7 @@ def test_unknown_extension(tmpdir):
11821182 assert stderr == ''
11831183
11841184
1185- def test_multiple_define_rules (tmpdir ):
1185+ def test_rule_file_define_multiple (tmpdir ):
11861186 rule_file = os .path .join (tmpdir , 'rule_file.xml' )
11871187 with open (rule_file , 'wt' ) as f :
11881188 f .write ("""
@@ -1201,6 +1201,7 @@ def test_multiple_define_rules(tmpdir):
12011201 <message>
12021202 <severity>error</severity>
12031203 <id>ruleId2</id>
1204+ <summary>define2</summary>
12041205 </message>
12051206 </rule>
12061207</rules>""" )
@@ -1213,18 +1214,136 @@ def test_multiple_define_rules(tmpdir):
12131214void f() { }
12141215''' )
12151216
1216- exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1217+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), '-DDEF_3' , test_file ])
12171218 assert exitcode == 0 , stderr
12181219 lines = stdout .splitlines ()
12191220 assert lines == [
12201221 'Checking {} ...' .format (test_file ),
12211222 'Processing rule: DEF_1' ,
1222- 'Processing rule: DEF_2'
1223+ 'Processing rule: DEF_2' ,
1224+ 'Checking {}: DEF_3=1...' .format (test_file )
12231225 ]
12241226 lines = stderr .splitlines ()
12251227 assert lines == [
12261228 "{}:2:0: error: found 'DEF_1' [ruleId1]" .format (test_file ),
1227- "{}:3:0: error: found 'DEF_2' [ruleId2]" .format (test_file )
1229+ "{}:3:0: error: define2 [ruleId2]" .format (test_file )
1230+ ]
1231+
1232+
1233+ def test_rule_file_define (tmpdir ):
1234+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1235+ with open (rule_file , 'wt' ) as f :
1236+ f .write ("""
1237+ <rule>
1238+ <tokenlist>define</tokenlist>
1239+ <pattern>DEF_.</pattern>
1240+ </rule>
1241+ """ )
1242+
1243+ test_file = os .path .join (tmpdir , 'test.c' )
1244+ with open (test_file , 'wt' ) as f :
1245+ f .write ('''
1246+ #define DEF_1
1247+ #define DEF_2
1248+ void f() { }
1249+ ''' )
1250+
1251+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), '-DDEF_3' , test_file ])
1252+ assert exitcode == 0 , stdout
1253+ lines = stdout .splitlines ()
1254+ assert lines == [
1255+ 'Checking {} ...' .format (test_file ),
1256+ 'Processing rule: DEF_.' ,
1257+ 'Checking {}: DEF_3=1...' .format (test_file )
1258+ ]
1259+ lines = stderr .splitlines ()
1260+ assert lines == [
1261+ "{}:2:0: style: found 'DEF_1' [rule]" .format (test_file ),
1262+ "{}:3:0: style: found 'DEF_2' [rule]" .format (test_file )
1263+ ]
1264+
1265+
1266+ def test_rule_file_normal (tmpdir ):
1267+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1268+ with open (rule_file , 'wt' ) as f :
1269+ f .write ("""
1270+ <rule>
1271+ <pattern>int</pattern>
1272+ </rule>
1273+ """ )
1274+
1275+ test_file = os .path .join (tmpdir , 'test.c' )
1276+ with open (test_file , 'wt' ) as f :
1277+ f .write ('''
1278+ #define DEF_1
1279+ #define DEF_2
1280+ typedef int i32;
1281+ void f(i32) { }
1282+ ''' )
1283+
1284+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1285+ assert exitcode == 0 , stdout
1286+ lines = stdout .splitlines ()
1287+ assert lines == [
1288+ 'Checking {} ...' .format (test_file ),
1289+ 'Processing rule: int' ,
1290+ ]
1291+ lines = stderr .splitlines ()
1292+ assert lines == [
1293+ "{}:5:0: style: found 'int' [rule]" .format (test_file )
12281294 ]
12291295
1230- # TODO: test "raw" and "normal" rules
1296+
1297+ def test_rule_file_raw (tmpdir ):
1298+ rule_file = os .path .join (tmpdir , 'rule_file.xml' )
1299+ with open (rule_file , 'wt' ) as f :
1300+ f .write ("""
1301+ <rule>
1302+ <tokenlist>raw</tokenlist>
1303+ <pattern>i32</pattern>
1304+ </rule>
1305+ """ )
1306+
1307+ test_file = os .path .join (tmpdir , 'test.c' )
1308+ with open (test_file , 'wt' ) as f :
1309+ f .write ('''
1310+ #define DEF_1
1311+ #define DEF_2
1312+ typedef int i32;
1313+ void f(i32) { }
1314+ ''' )
1315+
1316+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule-file={}' .format (rule_file ), test_file ])
1317+ assert exitcode == 0 , stdout
1318+ lines = stdout .splitlines ()
1319+ assert lines == [
1320+ 'Checking {} ...' .format (test_file ),
1321+ 'Processing rule: i32' ,
1322+ ]
1323+ lines = stderr .splitlines ()
1324+ assert lines == [
1325+ "{}:4:0: style: found 'i32' [rule]" .format (test_file ),
1326+ "{}:5:0: style: found 'i32' [rule]" .format (test_file )
1327+ ]
1328+
1329+
1330+ def test_rule (tmpdir ):
1331+ test_file = os .path .join (tmpdir , 'test.c' )
1332+ with open (test_file , 'wt' ) as f :
1333+ f .write ('''
1334+ #define DEF_1
1335+ #define DEF_2
1336+ void f() { }
1337+ ''' )
1338+
1339+ exitcode , stdout , stderr = cppcheck (['--template=simple' , '--rule=f' , test_file ])
1340+ assert exitcode == 0 , stdout
1341+ lines = stdout .splitlines ()
1342+ assert lines == [
1343+ 'Checking {} ...' .format (test_file ),
1344+ 'Processing rule: f' ,
1345+ ]
1346+ lines = stderr .splitlines ()
1347+ assert lines == [
1348+ "{}:4:0: style: found 'f' [rule]" .format (test_file )
1349+ ]
0 commit comments