From 87c778bb45bc256deb0c00e2246c29a1c2cddc59 Mon Sep 17 00:00:00 2001 From: rturrado <68099809+rturrado@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:53:33 +0100 Subject: [PATCH] Remove C language from all the CMake project statements. Add a gtest::gtest alias for fetched-content gtest. Update examples/{directory,interpreter}/main.cpp and examples/utils.hpp to use fmt. --- CMakeLists.txt | 2 +- examples/directory/CMakeLists.txt | 2 +- examples/directory/main.cpp | 18 +++++++++--------- examples/interpreter/CMakeLists.txt | 2 +- examples/interpreter/main.cpp | 9 ++++----- examples/utils.hpp | 11 +++++++---- test/CMakeLists.txt | 3 +++ 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 952b43b..d6fed49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ if(POLICY CMP0048) cmake_policy(SET CMP0048 NEW) endif() -project(tree-gen LANGUAGES C CXX) +project(tree-gen LANGUAGES CXX) # If tree-gen was already included elsewhere in the project, don't include it # again. There should be only one place for it and one version per project. diff --git a/examples/directory/CMakeLists.txt b/examples/directory/CMakeLists.txt index 0e96ec8..9cff71e 100644 --- a/examples/directory/CMakeLists.txt +++ b/examples/directory/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) -project(directory-example C CXX) +project(directory-example CXX) include(../../cmake/generate_tree.cmake) diff --git a/examples/directory/main.cpp b/examples/directory/main.cpp index 8b0fed5..910bf57 100644 --- a/examples/directory/main.cpp +++ b/examples/directory/main.cpp @@ -1,9 +1,10 @@ #include "../utils.hpp" -#include -#include +#include +#include +#include #include // ostringstream -#include +#include // Include the generated file. #include "directory.hpp" @@ -238,15 +239,15 @@ int main() { int count = 0; for (char c : cbor) { if (count == 16) { - std::printf("\n"); + fmt::print("\n"); count = 0; } else if (count > 0 && count % 4 == 0) { - std::printf(" "); + fmt::print(" "); } - std::printf("%02X ", (uint8_t)c); + fmt::print("{:02X} ", static_cast(c)); count++; } - std::printf("\n"); + fmt::print("\n"); MARKER // You can pull that through http://cbor.me and https://jsonformatter.org @@ -277,8 +278,7 @@ int main() { R"({"target":"...","name":"SomeUser"}}],"name":""}},"name":"evil link to C:"}}],"na)" R"(me":""}}}}]}})" ); - std::printf("%s", oss.str().c_str()); - std::printf("\n"); + fmt::print("{}\n", oss.str()); MARKER // Note that equality for two link edges is satisfied only if they point to diff --git a/examples/interpreter/CMakeLists.txt b/examples/interpreter/CMakeLists.txt index 3954d7c..8df0e07 100644 --- a/examples/interpreter/CMakeLists.txt +++ b/examples/interpreter/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) -project(interpreter-example C CXX) +project(interpreter-example CXX) include(../../cmake/generate_tree.cmake) diff --git a/examples/interpreter/main.cpp b/examples/interpreter/main.cpp index c9dd967..53668f5 100644 --- a/examples/interpreter/main.cpp +++ b/examples/interpreter/main.cpp @@ -1,9 +1,9 @@ #include "../utils.hpp" -#include -#include +#include #include // ostringstream -#include +#include // runtime_error +#include // Include the generated files. #include "value.hpp" @@ -201,8 +201,7 @@ int main() { R"(alue":"2","source_location":"test:1:5"}},"source_location":"test:1:1"}}],"source)" R"(_location":"test:1:1"}})" ); - std::printf("%s", oss.str().c_str()); - std::printf("\n"); + fmt::print("{}\n", oss.str()); MARKER // Note that we can still use these pointers despite ownership having been diff --git a/examples/utils.hpp b/examples/utils.hpp index 09550ab..151d94b 100644 --- a/examples/utils.hpp +++ b/examples/utils.hpp @@ -1,9 +1,12 @@ #pragma once +#include +#include // runtime_error + #define ASSERT(x) \ do { \ if (!(x)) { \ - throw std::runtime_error("assertion failed: " #x " is false at " __FILE__ ":" + std::to_string(__LINE__)); \ + throw std::runtime_error{ fmt::format("assertion failed: {} is false at {}:{}", #x, __FILE__, __LINE__) }; \ } \ } while (0) @@ -11,10 +14,10 @@ do { \ try { \ x; \ - throw std::runtime_error("assertion failed: no exception at " __FILE__ ":" + std::to_string(__LINE__)); \ + throw std::runtime_error{ fmt::format("assertion failed: no exception at {}:{}", __FILE__, __LINE__) } ; \ } catch (exc &e) { \ - std::cout << #exc << " exception message: " << e.what() << std::endl; \ + fmt::print("{} exception message: {}\n", #exc, e.what()); \ } \ } while (0) -#define MARKER std::cout << "###MARKER###" << std::endl; +#define MARKER fmt::print("###MARKER###\n"); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ed93629..69b8775 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) # Packages include(FetchContent) + +# gtest find_package(GTest) if(NOT GTest_FOUND) FetchContent_Declare(gtest @@ -10,6 +12,7 @@ if(NOT GTest_FOUND) ) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(gtest) + add_library(gtest::gtest ALIAS gtest) endif() include(GoogleTest)