Change POSIX expression to support #define macro of different types

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.

#define __ABC_D__
#define __CEL_INTERFACE_E__
#define __CRD_INTERFACE_T__
#define __CWD_TYPES_H__


Welcome to the community!

I’m confused. It seems to me that the default regular expression perfectly matches the defines you’ve listed.

What makes you think it doesn’t?


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]*$”

  1. A-Z ↩︎

1 Like


If you simply want to allow underscores at the beginning it would be:

If you want to require two underscores at the beginning, then the easiest thing is:

And to require head & tail:


Hello @Abhayak,

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.

1 Like

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.

Hello Loic, I am analyzing on C code.


I gave you 3 regex above with descriptions. Have you tried them?


Yes I tried them and they worked. Thanks!

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.

Thanks Loic for the information.

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:


  1. A-Z ↩︎


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?


Maybe check the list I already gave you?


@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.

^[A-Z][A-Z0-9_]*$ | [1][A-Z0-9_]*$


  1. A-Z ↩︎