The rule “Macro names should comply with a naming convention” uses a default POSIX expression format “^[A-Z][A-Z0-9_]*$” to check against #define macro we have in our code. This POSIX expression doesn’t support the following #define macro format below. I know we can change the POSIX expression but don’t know what the expression should be like to support the following #defines.
Sorry I forgot to format the description above. Now it shows the actual #define that sonarqube reports as "Rename this macro to match the regular expression: “[1][A-Z0-9]*$”
Please note that your macros have incorrect names, according to the C++ standard:
In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.
— Each identifier that contains a double underscore __ or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use
This is why our default regex does not allow such names.
Thanks for the reply G Ann. I have another question. what if we have multiple underscores in between in the macros like in the examples below: #define A_B_C #define A_B_C_D_E #define A_B_D_E #define A_B_C_D_E_H #define A_B_C_D_E_F_G
I am looking for single expression to support the above macros and the previous one we discussed.
In fact, the C standard has equivalent requirements. I told you the C++ version that was easier for me to find, but ISO/IEC 9899:2011 (the C standard) says:
7.1.3 Reserved identifier
[…]
— All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
— All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
Hey @ganncamp, the above regex expression supports the underscores at the beginning and at the end but doesnt support the following type of identifier.
Also, my current POSIX expression is "[1][a-zA-Z0-9]*$" to support enum. what would be the single POSIX expression to support the following types of enum:
ABC_RX_RS_STATES
msg_decode_fast_
msg_decode_for_pi_
You’re using this regex: ^__[A-Z][A-Z0-9_]*__$. Let’s break it down:
^ - beginning of the string
__ two underscores
So right off, this regex requires two underscores at the beginning of the string. And then at the end:
__ two underscores
$ end of the string
There it’s requiring two underscores at the end. So if you don’t want to require pairs of underscores at the beginning and end, you might want to edit the regex.
Hey @ganncamp, I have some macros that has underscores at beginning and at the end. Also, I have other macros which only has underscores in the middle like #define A_BC_FC_F. So what would be a single POSIX expression rule for all these types?
@ganncamp I wouldnt be asking for help if I hadnt tried it out the things you suggested. The list you gave supports the underscores either at the beginning/end or in-between. I am trying to figure out the expression to support both of them. I already tried it out the following method below but forgot to mention in the comment. I added “|” as a or condition and came up with the following expression but didnt work.
**