Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warnings with MacOS Clang 14.0.3 #6467

Merged
merged 12 commits into from
Apr 6, 2023
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-----------

### Internals
* None.
* Fix compiler warnings with MacOS Clang 14.0.3 ([PR #6467](https://github.com/realm/realm-core/pull/6467))

----------------------------------------------

Expand Down
42 changes: 38 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.15)
message(STATUS "CMake version: ${CMAKE_VERSION}")

set(CMAKE_BUILD_TYPE Debug CACHE STRING "")
project(RealmCore)
Expand Down Expand Up @@ -43,20 +44,53 @@ elseif(APPLE)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
endif()

function(add_cxx_flag_if_supported flag)
# Sets/clears the {FLAG} variable if the compiler flag is supported or not
# The compiler flag check is only performed once and the value is cached as the out variable name
function(check_if_cxx_compiler_flag_supported flag out_var)
if(flag MATCHES "^-Wno-")
# Compilers ignore unknown -Wno-foo flags, so look for -Wfoo instead.
string(REPLACE "-Wno-" "-W" check_flag ${flag})
else()
set(check_flag ${flag})
endif()

check_cxx_compiler_flag(${check_flag} HAVE${check_flag})
if(HAVE${check_flag})
check_cxx_compiler_flag(${check_flag} ${out_var})
endfunction()

function(add_cxx_flag_if_supported flag)
check_if_cxx_compiler_flag_supported(${flag} HAVE${flag})
if(HAVE${flag})
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:${flag}>)
endif()
endfunction()

# Usage: append_source_file_compile_options(FILES <file1>[ <file2>...] FLAGS <flag1>[ <flag2>...])
function(append_source_file_compile_options)
set(options)
set(args)
set(list_args FILES FLAGS)
cmake_parse_arguments(append_options "${options}" "${args}" "${list_args}" ${ARGN})
if (NOT append_options_FILES)
message(FATAL_ERROR "Missing FILES argument for append_source_file_compile_options")
endif()
if (NOT append_options_FLAGS)
message(FATAL_ERROR "Missing FLAGS argument for append_source_file_compile_options")
endif()
foreach(FLAG_ IN LISTS append_options_FLAGS)
check_if_cxx_compiler_flag_supported(${FLAG_} HAVE${FLAG_})
if (HAVE${FLAG_})
foreach(SRC_ IN LISTS append_options_FILES)
get_source_file_property(OLD_OPTS ${SRC_} COMPILE_OPTIONS)
if (NOT OLD_OPTS)
set_source_files_properties(${SRC_} PROPERTIES COMPILE_OPTIONS "${FLAG_}")
elseif(NOT "${FLAG_}" IN_LIST OLD_OPTS)
set_source_files_properties(${SRC_} PROPERTIES COMPILE_OPTIONS "${OLD_OPTS};${FLAG_}")
endif()
endforeach()
endif()
endforeach()
endfunction()

function(use_faster_linker)
# If a linker has already been set, don't override.
if ("${CMAKE_EXE_LINKER_FLAGS}" MATCHES "-fuse-ld=")
Expand Down Expand Up @@ -300,7 +334,7 @@ elseif(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
endif()
set(CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS ON CACHE BOOL "Enable Catch2's optional standard library stringmakers")

add_subdirectory(external/catch EXCLUDE_FROM_ALL)
add_subdirectory(external EXCLUDE_FROM_ALL)

# Cmake does not let us easily set the deployment target per-target, but Catch2
# can use modern features because we only run tests on recent OS versions.
Expand Down
4 changes: 4 additions & 0 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.15)

add_cxx_flag_if_supported(-Wno-unused-but-set-variable)
add_subdirectory(catch)
13 changes: 10 additions & 3 deletions src/realm/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ endif()
set(REALM_PARSER_SOURCES
driver.cpp
keypath_mapping.cpp
) # REALM_PARSER_SOURCES

set(REALM_PARSER_GENERATED
generated/query_flex.cpp
generated/query_bison.cpp
) # REALM_PARSER_SOURCES
Expand All @@ -56,13 +59,17 @@ set(REALM_PARSER_EXTRAS
query_flex.ll
)

# Add custom warning flags for generated parser files if the flag is supported
append_source_file_compile_options(FILES ${REALM_PARSER_GENERATED} FLAGS -Wno-unused-but-set-variable)

# Append additional compile options to the generated/query_flex.cpp file
if(MSVC)
set_source_files_properties(generated/query_flex.cpp PROPERTIES COMPILE_OPTIONS "/wd4018")
append_source_file_compile_options(FILES generated/query_flex.cpp FLAGS /wd4018)
else()
set_source_files_properties(generated/query_flex.cpp PROPERTIES COMPILE_OPTIONS "-Wno-unreachable-code;-Wno-sign-compare")
append_source_file_compile_options(FILES generated/query_flex.cpp FLAGS -Wno-unreachable-code -Wno-sign-compare)
endif()

add_library(QueryParser STATIC ${REALM_PARSER_SOURCES} ${REALM_PARSER_HEADERS} ${REALM_PARSER_EXTRAS})
add_library(QueryParser STATIC ${REALM_PARSER_SOURCES} ${REALM_PARSER_GENERATED} ${REALM_PARSER_HEADERS} ${REALM_PARSER_EXTRAS})
add_library(realm-parser ALIAS QueryParser)
add_library(Realm::QueryParser ALIAS QueryParser)
target_link_libraries(QueryParser PUBLIC Storage)
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ set(CORE_TESTS ${CORE_TEST_SOURCES} ${LARGE_TEST_SOURCES} ${FUZZY_TEST_SOURCES})
# FIXME: Benchmarks

if (MSVC)
set_source_files_properties(test_query.cpp test_query_big.cpp PROPERTIES COMPILE_FLAGS /bigobj)
append_source_file_compile_options(FILES test_query.cpp test_query_big.cpp FLAGS /bigobj)
endif()

# Resources required for running the tests
Expand Down
6 changes: 3 additions & 3 deletions test/test_lang_bind_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3986,7 +3986,7 @@ struct HandoverControl {
while (m_handover != nullptr)
m_changed.wait(lg);
// std::cout << " -- put " << h << std::endl;
m_handover = move(h);
m_handover = std::move(h);
m_changed.notify_all();
}
void get(std::unique_ptr<T>& h)
Expand All @@ -3996,7 +3996,7 @@ struct HandoverControl {
while (m_handover == nullptr)
m_changed.wait(lg);
// std::cout << " -- get " << m_handover << std::endl;
h = move(m_handover);
h = std::move(m_handover);
m_handover = nullptr;
m_changed.notify_all();
}
Expand All @@ -4005,7 +4005,7 @@ struct HandoverControl {
LockGuard lg(m_lock);
if (m_handover == nullptr)
return false;
h = move(m_handover);
h = std::move(m_handover);
m_handover = nullptr;
m_changed.notify_all();
return true;
Expand Down
2 changes: 0 additions & 2 deletions test/test_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2904,7 +2904,6 @@ TEST_IF(Query_StrIndex3, TEST_DURATION > 0)

std::vector<ObjKey> vec;

size_t n = 0;
#if defined REALM_DEBUG || REALM_ANDROID
for (int i = 0; i < 4; i++) {
#else
Expand All @@ -2924,7 +2923,6 @@ TEST_IF(Query_StrIndex3, TEST_DURATION > 0)
ObjKey key =
ttt.create_object().set_all(0, longstrings ? "AAAAAAAAAAAAAAAAAAAAAAAA" : "AA").get_key();
if (!longstrings) {
n++;
vec.push_back(key);
}
}
Expand Down
3 changes: 0 additions & 3 deletions test/test_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1841,13 +1841,10 @@ TEST(Shared_StringIndexBug3)
}

Random random(random_int<unsigned long>()); // Seed from slow global generator
size_t transactions = 0;
std::vector<ObjKey> keys;
for (size_t n = 0; n < 100; ++n) {
const uint64_t action = random.draw_int_mod(1000);

transactions++;

if (action <= 500) {
// delete random user
auto tr = db->start_write();
Expand Down