2

In large codebases, it might be impossible for the maintainer/project-owner to review and audit every line of code. In C++, some identifiers are reserved (according to the standard) and prohibiting the definition of reserved identifiers seems reasonable.

According to Identifiers - cppreference.com:

  • the identifiers with a double underscore anywhere are reserved;
  • the identifiers that begin with an underscore followed by an uppercase letter are reserved;
  • the identifiers that begin with an underscore are reserved in the global namespace.

Is there tooling to enforce this guideline (prohibit the use of reserved identifiers)?

For instance, is it possible (through GCC, Clang, clang-tidy, or some other compiler or linter) to get code like the following snippet to error:

auto __destination = cv::Mat3f(1, 1);
cv::cvtColor(source, __destination, cv::ColorConversionCodes::COLOR_HSV2RGB);
auto result = __destination.at<cv::Vec3f>(0, 0);

I know I can either hack around this by testing some regular expressions against the codebase or even solve it decently by parsing the C++ code into a parse tree and then validating all identifiers, but I am interested in knowing whether or not there is already something that does this.

I would be interested in forbidding even the use of already defined variables starting with a double underscore as it could be a hack exploiting a GCC leaky abstraction, for instance.

Bernardo Sulzbach
  • 1,293
  • 10
  • 26

1 Answers1

4

Clang has this feature starting from version 13 with -Wreserved-identifier flag (link).

GCC doesn't implement such feature yet (as in March 2022).

Mariusz Jaskółka
  • 4,137
  • 2
  • 21
  • 47