Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
[submodule "external/doctest"]
path = external/doctest
url = ../../doctest/doctest.git
[submodule "external/fmt"]
path = external/fmt
url = ../../fmtlib/fmt.git
9 changes: 0 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

add_library(fe INTERFACE)

check_include_file_cxx("format" CXX_FORMAT_SUPPORT)
message(STATUS "CXX_FORMAT_SUPPORT: ${CXX_FORMAT_SUPPORT}")
if(CXX_FORMAT_SUPPORT)
target_compile_definitions(fe INTERFACE FE_STD_FORMAT_SUPPORT)
else()
add_subdirectory(external/fmt)
target_link_libraries(fe INTERFACE fmt::fmt)
endif()

target_sources(fe
INTERFACE
FILE_SET headers
Expand Down
5 changes: 0 additions & 5 deletions cmake/fe-config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
@PACKAGE_INIT@

set(FE_STD_FORMAT_SUPPORT @CXX_FORMAT_SUPPORT@)
if(NOT FE_STD_FORMAT_SUPPORT)
include(${CMAKE_CURRENT_LIST_DIR}/../fmt/fmt-config.cmake)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake)
1 change: 0 additions & 1 deletion external/fmt
Submodule fmt deleted from 6b0082
6 changes: 3 additions & 3 deletions include/fe/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ struct Driver : public SymPool {
/// @name Diagnostics
///@{
// clang-format off
template<class... Args> static void note(Loc loc, format::format_string<Args...> fmt, Args&&... args) { std::cerr << loc << ": note: " << format::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void warn(Loc loc, format::format_string<Args...> fmt, Args&&... args) { ++num_warnings_; std::cerr << loc << ": warning: " << format::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void err (Loc loc, format::format_string<Args...> fmt, Args&&... args) { ++num_errors_; std::cerr << loc << ": error: " << format::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> static void note(Loc loc, std::format_string<Args...> fmt, Args&&... args) { std::cerr << loc << ": note: " << std::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void warn(Loc loc, std::format_string<Args...> fmt, Args&&... args) { ++num_warnings_; std::cerr << loc << ": warning: " << std::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void err (Loc loc, std::format_string<Args...> fmt, Args&&... args) { ++num_errors_; std::cerr << loc << ": error: " << std::format(fmt, std::forward<Args>(args)...) << std::endl; }
// clang-format on

unsigned num_errors() const { return num_errors_; }
Expand Down
45 changes: 15 additions & 30 deletions include/fe/format.h
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
#pragma once

#ifdef FE_STD_FORMAT_SUPPORT
# include <format>
#else
# include <fmt/format.h>
#endif
#include <format>

#include "fe/loc.h"
#include "fe/utf8.h"

namespace fe {

namespace format {
#ifdef FE_STD_FORMAT_SUPPORT
using namespace ::std;
#else
using namespace ::fmt;
#endif
} // namespace format

/// Make types that support ostream operators available for `std::format`.
/// Use like this:
/// ```
/// template<> struct std::formatter<T> : fe::ostream_formatter {};
/// ```
/// @sa [Stack Overflow](https://stackoverflow.com/a/75738462).
template<class Char>
struct basic_ostream_formatter : format::formatter<std::basic_string_view<Char>, Char> {
struct basic_ostream_formatter : std::formatter<std::basic_string_view<Char>, Char> {
template<class T, class O>
O format(const T& value, format::basic_format_context<O, Char>& ctx) const {
O format(const T& value, std::basic_format_context<O, Char>& ctx) const {
std::basic_stringstream<Char> ss;
ss << value;
#if defined(_LIBCPP_VERSION) && _LIBCPP_VERSION < 170000
return std::formatter<std::basic_string_view<Char>, Char>::format(ss.str(), ctx);
#else
return format::formatter<std::basic_string_view<Char>, Char>::format(ss.view(), ctx);
return std::formatter<std::basic_string_view<Char>, Char>::format(ss.view(), ctx);
#endif
}
};
Expand All @@ -45,10 +33,10 @@ using ostream_formatter = basic_ostream_formatter<char>;
/// Print to `std::cout`/`std::cerr` via `std::format`; the `*ln` variants conclude with `std::endl`.
///@{
// clang-format off
template<class... Args> void err (format::format_string<Args...> fmt, Args&&... args) { std::cerr << format::format(fmt, std::forward<Args>(args)...); }
template<class... Args> void out (format::format_string<Args...> fmt, Args&&... args) { std::cout << format::format(fmt, std::forward<Args>(args)...); }
template<class... Args> void errln(format::format_string<Args...> fmt, Args&&... args) { std::cerr << format::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void outln(format::format_string<Args...> fmt, Args&&... args) { std::cout << format::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void err (std::format_string<Args...> fmt, Args&&... args) { std::cerr << std::format(fmt, std::forward<Args>(args)...); }
template<class... Args> void out (std::format_string<Args...> fmt, Args&&... args) { std::cout << std::format(fmt, std::forward<Args>(args)...); }
template<class... Args> void errln(std::format_string<Args...> fmt, Args&&... args) { std::cerr << std::format(fmt, std::forward<Args>(args)...) << std::endl; }
template<class... Args> void outln(std::format_string<Args...> fmt, Args&&... args) { std::cout << std::format(fmt, std::forward<Args>(args)...) << std::endl; }
// clang-format on

/// Keeps track of indentation level during output
Expand Down Expand Up @@ -105,14 +93,11 @@ class Tab {
} // namespace fe

#ifndef DOXYGEN
template<>
struct fe::format::formatter<fe::Pos> : fe::ostream_formatter {};
template<>
struct fe::format::formatter<fe::Loc> : fe::ostream_formatter {};
template<>
struct fe::format::formatter<fe::Sym> : fe::ostream_formatter {};
template<>
struct fe::format::formatter<fe::Tab> : fe::ostream_formatter {};
template<>
struct fe::format::formatter<fe::utf8::Char32> : fe::ostream_formatter {};
// clang-format off
template<> struct std::formatter<fe::Pos> : fe::ostream_formatter {};
template<> struct std::formatter<fe::Loc> : fe::ostream_formatter {};
template<> struct std::formatter<fe::Sym> : fe::ostream_formatter {};
template<> struct std::formatter<fe::Tab> : fe::ostream_formatter {};
template<> struct std::formatter<fe::utf8::Char32> : fe::ostream_formatter {};
// clang-format on
#endif
4 changes: 2 additions & 2 deletions tests/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Tok {
};
};

template<> struct fe::format::formatter<Tok> : fe::ostream_formatter {};
template<> struct std::formatter<Tok> : fe::ostream_formatter {};

template<size_t K = 1> class Lexer : public fe::Lexer<K, Lexer<K>> {
public:
Expand Down Expand Up @@ -186,7 +186,7 @@ template<size_t K> void test_lexer() {
auto tb = lexer.lex();
auto tc = lexer.lex();
auto td = lexer.lex();
auto s = fe::format::format("{} {} {} {} {} {} {} {} {} {} {} {} {} {}", t1, t2, t3, t4, t5, t6, t7, t8, t9, t0, ta,
auto s = std::format("{} {} {} {} {} {} {} {} {} {} {} {} {} {}", t1, t2, t3, t4, t5, t6, t7, t8, t9, t0, ta,
tb, tc, td);
CHECK(s == "test abc def if while λ foo « n ; X » <end of file> <end of file>");

Expand Down
Loading