xuyongchuan - 2025-05-21

I want to check whether macro definitions start and end with an underscore. I used the following rule file, but the detection is not working.
source code:
test.c

#include <stdio.h>
#define _pi_ 3.14

void func(){
    print("nothing to do");
}

int main()
{
    func();
    return 0;
}

c_file_rules.xml

<?xml version="1.0"?>
<rule>
    <pattern>extern</pattern>
    <message>
        <severity>warning</severity>
        <summary>extern keyword should not be used in .c file</summary>
    </message>
</rule>
<rule>
    <pattern><![CDATA[define\s+(_[a-zA-Z0-9_]+_)\s+]]></pattern>
    <message>
        <severity>warning</severity>
        <summary>macro definitions start and end with an underscore</summary>
    </message>
</rule>

detection result

[root@node26 cppcheck_tools]# cppcheck --rule-file=./c_file_rules.xml test.c
Checking test.c ...
Processing rule: extern
Processing rule: define\s+(_[a-zA-Z0-9_]+_)\s+