0

The wchar_t type is used extensively on Windows API and C++ standard library APIs derived from them therefore it's hard to change Windows code to use something else because you would have to cast/convert back and forth every time.

But on non-Windows wide characters are rarely used and UTF-8 encoding is preferred instead. Therefore having code that uses wchar_t outside Windows probably does something wrong and even if its intended it's better to use types that communicate the intent better eg. using std::u16string and char16_t when dealing with UTF-16 strings instead of wstring and using std::u32string and char32_t when the intent is storing Unicode codepoints.

Is there a GCC option to turn on a diagnostic project wide that warns or errors when it sees a wchar_t, therefore identifying potential sites for refactoring?

Calmarius
  • 18,570
  • 18
  • 110
  • 157
  • You can now [set UTF-8 as the locale on Windows](https://stackoverflow.com/a/63556337/995714). Microsoft has even recommended to use the `A` version of Win32 APIs again for portability, no need to deal with `wchar_t` anymore. And by statically linking the new Windows SDK you can have UTF-8 locale even in older Windows – phuclv May 04 '21 at 12:43
  • @phuclv The code base is large, decades old, and have to support certain old Windows versions for certain customers. So we can't switch it yet. – Calmarius May 04 '21 at 18:28

1 Answers1

0

That is a little work around and not dedicated to GCC and also will break your build but allows you to find where you use wchar_t. (And also breaks included third-party code more or less)

You can override the definition of wchar_t with the preprocessor which then leads to errors on the usage. In that way you can find the potential usages:

#define wchar_t void

wchar_t Foo() { }

int main()
{
    auto wchar_used = Foo();
}

Error message:

error: 'void wchar_used' has incomplete type
   10 |     auto wchar_used = Foo();
RoQuOTriX
  • 2,871
  • 14
  • 25