From dd757e90349cb869d60700cb54b95b1dad3a9113 Mon Sep 17 00:00:00 2001 From: Jason Yundt Date: Sun, 7 Jul 2024 08:51:54 -0400 Subject: [PATCH] Explicitly declare that source files are UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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]: --- .editorconfig | 1 + CMakeLists.txt | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/.editorconfig b/.editorconfig index c7a5f17e..acd10aaf 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,6 +5,7 @@ indent_style = space indent_size = 2 tab_width = 8 end_of_line = lf +charset = utf-8 spelling_language = en-US [*.md] diff --git a/CMakeLists.txt b/CMakeLists.txt index a1ee6fd5..bf5e8fd1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,12 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 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(CMAKE_VERSION VERSION_GREATER_EQUAL 3.24) set(CMAKE_COLOR_DIAGNOSTICS ON)