4

I have done some searching and have consulted a friend of mine, but also want to get the StackExchange's communities input. To preface I'm mainly a HW person who has been slammed into low-level firmware land, so forgive me for anything that seems as common knowledge. I have done C programming in the past, but it has been a while.

For a custom ASIC I am apart of we have many control/status registers that we will access through software/firmware (I will likely just call it SW from now on). For our verilog/SV/UVM testbenchs, we have a script that builds a verilog header `define file. In this case we have the following structure:

`define REG_NAME                        <addr of register>
`define REG_NAME__BITFIELD1             <bit vector i.e. 1:0>
`deinfe REG_NAME__BITFIELD2             <bit vector i.e. 4:2>

We use a double underscore to distinguish visually between the register name and bitfield. We then use this for doing things like read-modify-writes, masking out bits, etc. For the SW, I wrote a script to print out a .h file in C format. I wanted to keep a similar formatting:

#define REG_NAME                        <addr of register>
#define REG_NAME__BITFIELD1___MASK      0x00000003
#define REG_NAME__BITFIELD1___SHIFT     0
#deinfe REG_NAME__BITFIELD2___MASK      0x0000001c        
#deinfe REG_NAME__BITFIELD2___SHIFT     2

In this case I want to maintain the double underscore for the distinction between the REG and BIT_FIELD and the triple to distinguish the MASK/SHIFT/anything else I add.

I know based on the following SE questions: Use of double underscore in C How is __mro__ different from other double underscore names?

That prefixed underscores is not a good coding practice. Is it generally a bad coding practice to have double/triple underscores INSIDE of the define like this? I know that I could control the macro define to never start with or end with the double/triple underscore. It's honestly something that aids me quite a bit. I have a hard time picking out the differences between words when all single underscores are used. And with there being over 60k #defines based on registers, I would like to have a cleaner way to look at it. However if this is something that most SW engineers would barf at, I would change it as sooner or later (hopefully) someone who is a real SW engineer will be handling most of this (not that I mind doing SW, it's fun).

If this has been discussed, please feel free to forward the thread. I searched around for about 30-45 mins, but could not find anything that seemed to discuss this topic exactly.

Thanks!

Community
  • 1
  • 1
l Steveo l
  • 516
  • 3
  • 11
  • Since there is no functional change, this is entirely a matter of taste. – Siguza Apr 18 '17 at 00:24
  • 1
    Except as specified in the standard (such as the *initial* double-underscore stuff), identifiers are entirely up to you. If you want them to have __, ___, ZZZZ, or 55555 in the middle, go for it. – Lee Daniel Crocker Apr 18 '17 at 00:29
  • 1
    Format aspects aside, it is not coode practice; depending onm the font, it can be hard to distinguish one or two undercores and it looks just ugly. Why do you think you need two underscores? As it looks, the distinction with a single underscore is fine. An alternative would be to use mixed case like `REG_NAME_bitfield1`. – too honest for this site Apr 18 '17 at 01:06
  • @Olaf wouldn't the mixing of Upper and lower case really be about the same argument with regards to the coding practice? Although that's not a terrible idea. I really only want more than one underscore as a) it's easier for me to see, and b) it matches a bunch of the infrastructure we have on the Verilog/Hardware side of things. – l Steveo l Apr 18 '17 at 01:32
  • @lSteveol: Sure it is and I know coding standards which disallow it ("Macros must be all uppercase" - dogma, fullstop), but allow double, tribble, etc. underscores. It is all opinionated, which is exactly the reason the question is OT here. Nevertheless I think I have good arguments against the double underscores and my approach more looks like name-spacing (one could as well use Camel-Case for the suffix part). – too honest for this site Apr 18 '17 at 10:03

1 Answers1

5

In both C and C++, you (the application programmer) should avoid beginning identifiers with even a single underscore - all such identifiers are "reserved for the implementation" in at least some contexts; rather than remember the exact rules it's easiest to just stick to starting all your identifiers with a letter.

In C++ only, identifiers that contain two consecutive underscores (like your REG_NAME__BITFIELD1___MASK) are also reserved for the implementation.

So, as far as the standard is concerned, you can do what you want in C, but not in C++.

As a matter of style, I personally think your rationale for wanting to use two underscores in a row (lots of similar identifiers with structure in their names, make the structure more obvious) is legitimate, and I don't agree with the people in the linked question who say it's too hard to tell the difference between one and two underscores, when reading. I don't understand why you want three underscores in a row, though, and I do think it's too hard to tell the difference between two and three underscores in a row.

(Note: one of the old questions you linked to is about Python, which is a completely unrelated language; it cannot be used to draw conclusions about C or C++.)

(You can use identifiers defined by the implementation that begin with an underscore, as long as they are documented—e.g. __STDC__, _Bool, _IOLBF—just don't ever define them yourself, unless the documentation specifically tells you to define them (e.g. _POSIX_C_SOURCE). In complicated programs, the line between "the program" and "the implementation" can get blurry, so don't freak out if you see a program defining identifiers that begin with an underscore; there's a good chance that the authors know precisely what they are doing.)

zwol
  • 135,547
  • 38
  • 252
  • 361
  • The fact that C++ would in general have an issue with this is like cause for me to find an alternative approach. Thanks for the informative post. – l Steveo l Apr 18 '17 at 01:34
  • Also, yes I did accidentally link a python post. Had too many tabs open, sorry! – l Steveo l Apr 18 '17 at 01:35