Avoiding identifiers reserved in C++ but allow those reserved in C

There is this rule https://rules.sonarsource.com/cpp/RSPEC-978 Which finds uses where code declares an identifier matching these rules:

  • defined
  • C standard library function names
  • identifiers that contain two consecutive underscores
  • identifiers that begin with an underscore, followed by an uppercase letter
  • identifiers in the global namespace that start with an underscore

Which makes sense, but our code has some cases that violate the second of these points. I’m not sure if the source material is suggesting that function names in the C standard library shouldn’t be used in C++ code, so having that cause in the same rule that detects usage of names that are reserved in C++ directly seems stricter than it needs to be.

Alternatively, because compatibility with C is important, maybe it would make sense to find declarations using names of functions that are defined in the C standard library in the global namespace.

I think it would make sense to replace the existing rule with these:

  • Reserved identifiers in C++ should not be used
  • The names of functions defined in the C standard library should not be used as identifiers
  • The names of functions defined in the C standard library should not be used as identifiers in the global scope

There is a relevant clang tidy check for this issue here clang-tidy - bugprone-reserved-identifier — Extra Clang Tools 15.0.0git documentation I think it would be good for SonarQube to have rules that let you match the capabilities of clang tidy.