Browse Source
Remove locale-specific floating point from io
Remove locale-specific floating point from io
Use only C-locale functions wxString::ToCDouble and fmt::format{} functions so that we do not need to set the locale for file io operations This also introduces a cmake check that looks for certain banned functions (strtod, strtof and atof) that convert strings into doubles by the user locale or formatting specifiers like %f/%g that convert doubles into strings by the users locale. It will prevent compilation in this case. For the limited cases where we want to show the user a string in their locale, we have an override string "format:allow" that can be added to lines in order to allow them with %f/%g format specifierspull/18/head
13 changed files with 166 additions and 41 deletions
-
54cmake/CheckBannedFunctions.cmake
-
6common/CMakeLists.txt
-
33common/io/eagle/eagle_parser.cpp
-
8eeschema/sch_io/cadstar/cadstar_sch_archive_loader.cpp
-
3eeschema/sch_io/kicad_legacy/sch_io_kicad_legacy.cpp
-
4eeschema/sch_io/kicad_legacy/sch_io_kicad_legacy_lib_cache.cpp
-
4eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr.cpp
-
11eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_lib_cache.cpp
-
8pcbnew/pcb_io/altium/altium_pcb.cpp
-
20pcbnew/pcb_io/cadstar/cadstar_pcb_archive_loader.cpp
-
3pcbnew/pcb_io/fabmaster/import_fabmaster.cpp
-
49pcbnew/pcb_io/kicad_legacy/pcb_io_kicad_legacy.cpp
-
4pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr.cpp
@ -0,0 +1,54 @@ |
|||
# CheckBannedFunctions.cmake |
|||
# Usage: include(CheckBannedFunctions.cmake) |
|||
|
|||
# This script scans for banned functions in io directories and fails the build if found. |
|||
|
|||
# Set CMP0007 to NEW to ensure list variables are not stringified |
|||
cmake_policy(SET CMP0007 NEW) |
|||
|
|||
# Usage: check_banned_functions() after setting BANNED_DIRS |
|||
macro(check_banned_functions) |
|||
set(BANNED_FUNCTIONS "strtod" "strtof" "atof") |
|||
set(BANNED_SCAN_FILES) |
|||
|
|||
# Ensure BANNED_DIRS is set and not empty |
|||
if(NOT DEFINED BANNED_DIRS OR "${BANNED_DIRS}" STREQUAL "") |
|||
message(FATAL_ERROR "BANNED_DIRS variable is not set or empty.") |
|||
endif() |
|||
|
|||
# Convert space-separated to semicolon-separated list |
|||
string(REPLACE " " ";" BANNED_DIRS "${BANNED_DIRS}") |
|||
|
|||
foreach(dir IN LISTS BANNED_DIRS) |
|||
message(STATUS "Checking for banned functions in: ${dir}") |
|||
file(GLOB_RECURSE found_files |
|||
RELATIVE ${CMAKE_SOURCE_DIR} |
|||
${dir}/*.c |
|||
${dir}/*.cpp |
|||
${dir}/*.h |
|||
${dir}/*.hpp |
|||
) |
|||
list(APPEND BANNED_SCAN_FILES ${found_files}) |
|||
endforeach() |
|||
|
|||
foreach(file ${BANNED_SCAN_FILES}) |
|||
# Read file into lines using file(STRINGS) |
|||
file(STRINGS ${CMAKE_SOURCE_DIR}/${file} file_lines) |
|||
|
|||
foreach(line IN LISTS file_lines) |
|||
foreach(func ${BANNED_FUNCTIONS}) |
|||
if(line MATCHES "\\b${func}\\b") |
|||
message(FATAL_ERROR "Banned function '${func}' found in ${file}") |
|||
endif() |
|||
endforeach() |
|||
# Only error if the line does not end with //format:allow (allow spaces between // and format) |
|||
# we allow this for specific cases where the format specifier is needed because the |
|||
# string is shown to the user and needs to be formatted in their locale e.g. error messages |
|||
if(line MATCHES "%\\d*\\.?\\d*[fg]" AND NOT line MATCHES "format:allow") |
|||
message(FATAL_ERROR "Banned format specifier '%f' or '%g' found in ${file}") |
|||
endif() |
|||
endforeach() |
|||
endforeach() |
|||
endmacro() |
|||
|
|||
check_banned_functions() |
Write
Preview
Loading…
Cancel
Save
Reference in new issue