Skip to content

[clang-tidy] create a check to ensure std:: qualified names used with C++ C standard headers #135678

@LegalizeAdulthood

Description

@LegalizeAdulthood

If you include <cctype> and don't import the namespace std, you should be using function names qualified by namespace std.

While it is a common implementation detail for the C++ style header to include the C header and do something like import the names from the global namespace into namespace std, the standard itself doesn't require that the C names be available in the global namespace.

This, after all, is the whole point of the C++ style headers like <cctype> -- to put the identifiers in namespace std and not pollute the global namespace.

However, because of the common implementation mechanism described above, it is too easy to drop the namespace qualifier and have the code "working by accident" instead of "working by design".

For such a check, there is the additional complication that the C style headers may have been included transitively and therefore the C style identifiers are indeed guaranteed to be in the global namespace.

Example:

#include <cctype>

bool is_upper_case(char c)
{
    return isupper(static_cast<unsigned char>(c)) != 0;  // warning issued here
    // fixit: insert `std::` before `isupper`
}

Example:

#include <cctype>

using namespace std;

bool is_upper_case(char c)
{
    return isupper(static_cast<unsigned char>(c)) != 0;  // no warning issued here
}

To implement this check, I think you'll need to enumerate all the symbols in namespace std that are provided by a C++ version of a C standard header. Maybe there's a better way, but I can't think of it off the top of my head.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions