Explicitly declare that source files are UTF-8

Before this change, there was a chance that a text editor or compiler
would use the wrong character encoding. For text editors, this commit
adds “charset = utf-8” to .editorconfig. That will cause editors that
support EditorConfig files [1] to automatically use UTF-8 when reading
and writing files. For compilers, this commit adds compiler options that
guarantee that compilers will decode source code files using UTF-8. The
compiler options are known to work on MSVC, GCC and Clang. If we ever
want to support additional compilers, then we might have to edit the if
statement that this commit adds.

This commit does not eliminate the chance that a wrong character
encoding will be used. Someone could always use a text editor that
doesn’t support EditorConfig files or a compiler that doesn’t support
the compiler options that we use. This commit does, however, make an
encoding mismatch much less likely.

[1]: <https://editorconfig.org/#pre-installed>
This commit is contained in:
Jason Yundt 2024-07-07 08:51:54 -04:00
parent 308d82e276
commit dd757e9034
2 changed files with 7 additions and 0 deletions

View File

@ -5,6 +5,7 @@ indent_style = space
indent_size = 2 indent_size = 2
tab_width = 8 tab_width = 8
end_of_line = lf end_of_line = lf
charset = utf-8
spelling_language = en-US spelling_language = en-US
[*.md] [*.md]

View File

@ -28,6 +28,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if(MSVC)
add_compile_options(/source-charset:UTF-8)
else()
add_compile_options(-finput-charset=UTF-8)
endif()
if(FORCE_COLORED_OUTPUT) if(FORCE_COLORED_OUTPUT)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24)
set(CMAKE_COLOR_DIAGNOSTICS ON) set(CMAKE_COLOR_DIAGNOSTICS ON)