Skip to content

Commit

Permalink
Remove C language from all the CMake project statements.
Browse files Browse the repository at this point in the history
Add a gtest::gtest alias for fetched-content gtest.
Update examples/{directory,interpreter}/main.cpp and examples/utils.hpp to use fmt.
  • Loading branch information
rturrado committed Mar 20, 2024
1 parent 1b7225d commit 87c778b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/directory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
18 changes: 9 additions & 9 deletions examples/directory/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "../utils.hpp"

#include <cstdio>
#include <iostream>
#include <fmt/core.h>
#include <fstream>
#include <ios>
#include <sstream> // ostringstream
#include <stdexcept>
#include <string>

// Include the generated file.
#include "directory.hpp"
Expand Down Expand Up @@ -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<uint8_t>(c));
count++;
}
std::printf("\n");
fmt::print("\n");
MARKER

// You can pull that through http://cbor.me and https://jsonformatter.org
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
9 changes: 4 additions & 5 deletions examples/interpreter/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "../utils.hpp"

#include <cstdio>
#include <iostream>
#include <fmt/core.h>
#include <sstream> // ostringstream
#include <stdexcept>
#include <stdexcept> // runtime_error
#include <string>

// Include the generated files.
#include "value.hpp"
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions examples/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#pragma once

#include <fmt/core.h>
#include <stdexcept> // 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)

#define ASSERT_RAISES(exc, x) \
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");
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 87c778b

Please sign in to comment.