Skip to content

Commit cc01a89

Browse files
committed
Refactor: fix errors if BOOST_JSON_STANDALONE is omitted
I want quick-lint-js' build system to work with Boost compiled outside quick-lint-js. In order to make this work, we need quick-lint-js to compile with BOOST_JSON_STANDALONE disabled. Disabling BOOST_JSON_STANDALONE causes boost::json APIs to use boost:: versions of things like string_view and error_code instead of std:: versions. Change quick-lint-js' uses of boost::json to work with either the boost:: versions or the std:: versions. The QLJS_BUILDING_QLJS_BOOST_JSON change is necessary to keep the simple build (which blindly builds src/*.cpp) working as-is. This commit should not change behavior.
1 parent 0e5c9a7 commit cc01a89

File tree

6 files changed

+71
-30
lines changed

6 files changed

+71
-30
lines changed

src/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ target_link_libraries(
282282
boost_json
283283
quick-lint-js-lib
284284
)
285+
target_compile_definitions(
286+
quick-lint-js-boost-json
287+
PRIVATE
288+
QLJS_BUILDING_QLJS_BOOST_JSON
289+
)
285290

286291
# quick-lint-js finds bugs in JavaScript programs.
287292
# Copyright (C) 2020 Matthew "strager" Glazar

src/boost-json.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
// Copyright (C) 2020 Matthew "strager" Glazar
22
// See end of file for extended copyright information.
33

4-
#if defined(BOOST_JSON_STANDALONE)
4+
#if defined(QLJS_BUILDING_QLJS_BOOST_JSON)
55

66
#include <boost/json/value.hpp>
77
#include <quick-lint-js/boost-json.h>
88

99
namespace quick_lint_js {
10+
::boost::json::string_view to_boost_string_view(string8_view sv) {
11+
std::string_view std_sv = to_string_view(sv);
12+
return ::boost::json::string_view(std_sv.data(), std_sv.size());
13+
}
14+
15+
#if QLJS_HAVE_CHAR8_T
16+
::boost::json::string_view to_boost_string_view(std::string_view sv) {
17+
return ::boost::json::string_view(sv.data(), sv.size());
18+
}
19+
#endif
20+
21+
#if !defined(BOOST_JSON_STANDALONE)
22+
std::string_view to_string_view(::boost::json::string_view sv) {
23+
return std::string_view(sv.data(), sv.size());
24+
}
25+
#endif
26+
27+
std::string_view to_string_view(const ::boost::json::string& s) {
28+
return std::string_view(s.data(), s.size());
29+
}
30+
1031
::boost::json::array* if_array(::boost::json::object& object,
1132
::boost::json::string_view key) {
1233
if (::boost::json::value* value = object.if_contains(key)) {

src/quick-lint-js/boost-json.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66

77
#include <boost/json/value.hpp>
88
#include <cstddef>
9+
#include <quick-lint-js/char8.h>
910
#include <quick-lint-js/narrow-cast.h>
1011
#include <type_traits>
1112

1213
namespace quick_lint_js {
14+
::boost::json::string_view to_boost_string_view(string8_view sv);
15+
#if QLJS_HAVE_CHAR8_T
16+
::boost::json::string_view to_boost_string_view(std::string_view sv);
17+
#endif
18+
19+
#if !defined(BOOST_JSON_STANDALONE)
20+
std::string_view to_string_view(::boost::json::string_view sv);
21+
#endif
22+
std::string_view to_string_view(const ::boost::json::string &s);
23+
1324
template <class... Keys>
1425
struct look_up_impl;
1526

test/parse-json.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#include <quick-lint-js/warning.h>
1414
#include <simdjson.h>
1515
#include <sstream>
16-
#include <system_error>
1716

1817
namespace quick_lint_js {
1918
::boost::json::value parse_boost_json(std::string_view json) {
20-
std::error_code error;
21-
::boost::json::value root = ::boost::json::parse(json, error);
19+
::boost::json::error_code error;
20+
::boost::json::value root =
21+
::boost::json::parse(to_boost_string_view(json), error);
2222
EXPECT_FALSE(error) << json;
2323
return root;
2424
}
@@ -32,8 +32,9 @@ ::boost::json::value parse_boost_json(string8_view json) {
3232
::boost::json::value simdjson_to_boost_json(
3333
::simdjson::ondemand::value &value) {
3434
std::string_view json = value.raw_json_token();
35-
std::error_code error;
36-
::boost::json::value result = ::boost::json::parse(json, error);
35+
::boost::json::error_code error;
36+
::boost::json::value result =
37+
::boost::json::parse(to_boost_string_view(json), error);
3738
EXPECT_FALSE(error);
3839
return result;
3940
}

test/test-json.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <boost/json/parse.hpp>
55
#include <boost/json/value.hpp>
66
#include <gtest/gtest.h>
7+
#include <quick-lint-js/boost-json.h>
78
#include <quick-lint-js/char8.h>
89
#include <quick-lint-js/json.h>
910
#include <quick-lint-js/narrow-cast.h>
@@ -72,11 +73,11 @@ TEST(test_json, ascii_characters_are_parsable_by_boost_json) {
7273
json.flush();
7374
SCOPED_TRACE(out_string8(json.get_flushed_string8()));
7475

75-
std::error_code error;
76-
::boost::json::value parsed =
77-
::boost::json::parse(to_string_view(json.get_flushed_string8()), error);
76+
::boost::json::error_code error;
77+
::boost::json::value parsed = ::boost::json::parse(
78+
to_boost_string_view(json.get_flushed_string8()), error);
7879
EXPECT_FALSE(error);
79-
EXPECT_EQ(parsed, to_string_view(string));
80+
EXPECT_EQ(parsed, to_boost_string_view(string));
8081
}
8182
}
8283
}

test/test-lsp-server.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <gmock/gmock.h>
1111
#include <gtest/gtest.h>
1212
#include <memory>
13+
#include <quick-lint-js/boost-json.h>
1314
#include <quick-lint-js/byte-buffer.h>
1415
#include <quick-lint-js/change-detecting-filesystem.h>
1516
#include <quick-lint-js/char8.h>
@@ -964,7 +965,7 @@ TEST_F(test_linting_lsp_server, editing_config_relints_many_open_js_files) {
964965
EXPECT_EQ(look_up(notification, "method"),
965966
"textDocument/publishDiagnostics");
966967
std::string uri(
967-
std::string_view(look_up(notification, "params", "uri").get_string()));
968+
to_string_view(look_up(notification, "params", "uri").get_string()));
968969
if (uri ==
969970
to_string(this->fs.file_uri_prefix_8() + u8"quick-lint-js.config")) {
970971
// Ignore.
@@ -1084,7 +1085,7 @@ TEST_F(test_linting_lsp_server, editing_config_relints_only_affected_js_files) {
10841085
EXPECT_EQ(look_up(notification, "method"),
10851086
"textDocument/publishDiagnostics");
10861087
std::string uri(
1087-
std::string_view(look_up(notification, "params", "uri").get_string()));
1088+
to_string_view(look_up(notification, "params", "uri").get_string()));
10881089
if (uri == to_string(this->fs.file_uri_prefix_8() +
10891090
u8"dir-a/quick-lint-js.config") ||
10901091
uri == to_string(this->fs.file_uri_prefix_8() +
@@ -1464,7 +1465,7 @@ TEST_F(test_linting_lsp_server, opening_js_file_with_unreadable_config_lints) {
14641465
EXPECT_EQ(look_up(showMessageMessage, "params", "type"),
14651466
lsp_warning_message_type);
14661467
EXPECT_EQ(look_up(showMessageMessage, "params", "message"),
1467-
std::string_view(this->config_file_load_error_message(
1468+
to_boost_string_view(this->config_file_load_error_message(
14681469
"test.js", "quick-lint-js.config")));
14691470
}
14701471

@@ -1522,11 +1523,11 @@ TEST_F(test_linting_lsp_server,
15221523
EXPECT_EQ(look_up(showMessageMessage, "method"), "window/showMessage");
15231524
EXPECT_EQ(look_up(showMessageMessage, "params", "type"),
15241525
lsp_warning_message_type);
1525-
EXPECT_EQ(
1526-
look_up(showMessageMessage, "params", "message"),
1527-
std::string_view("Problems found in the config file for "s +
1528-
this->fs.rooted("test.js").c_str() + " (" +
1529-
this->fs.rooted("quick-lint-js.config").c_str() + ")."));
1526+
EXPECT_EQ(look_up(showMessageMessage, "params", "message"),
1527+
to_boost_string_view(
1528+
"Problems found in the config file for "s +
1529+
this->fs.rooted("test.js").c_str() + " (" +
1530+
this->fs.rooted("quick-lint-js.config").c_str() + ")."));
15301531
}
15311532

15321533
TEST_F(test_linting_lsp_server, making_config_file_unreadable_relints) {
@@ -1595,7 +1596,7 @@ TEST_F(test_linting_lsp_server, making_config_file_unreadable_relints) {
15951596
EXPECT_EQ(look_up(showMessageMessage, "params", "type"),
15961597
lsp_warning_message_type);
15971598
EXPECT_EQ(look_up(showMessageMessage, "params", "message"),
1598-
std::string_view(this->config_file_load_error_message(
1599+
to_boost_string_view(this->config_file_load_error_message(
15991600
"test.js", "quick-lint-js.config")));
16001601
}
16011602

@@ -1620,9 +1621,9 @@ TEST_F(test_linting_lsp_server, opening_broken_config_file_shows_diagnostics) {
16201621
EXPECT_EQ(response["method"], "textDocument/publishDiagnostics");
16211622
EXPECT_FALSE(response.contains("error"));
16221623
// LSP PublishDiagnosticsParams:
1623-
EXPECT_EQ(
1624-
look_up(response, "params", "uri"),
1625-
to_string_view(this->fs.file_uri_prefix_8() + u8"quick-lint-js.config"));
1624+
EXPECT_EQ(look_up(response, "params", "uri"),
1625+
to_boost_string_view(this->fs.file_uri_prefix_8() +
1626+
u8"quick-lint-js.config"));
16261627
EXPECT_EQ(look_up(response, "params", "version"), 1);
16271628
::boost::json::array diagnostics =
16281629
look_up(response, "params", "diagnostics").as_array();
@@ -1678,9 +1679,9 @@ TEST_F(test_linting_lsp_server,
16781679
EXPECT_EQ(response["method"], "textDocument/publishDiagnostics");
16791680
EXPECT_FALSE(response.contains("error"));
16801681
// LSP PublishDiagnosticsParams:
1681-
EXPECT_EQ(
1682-
look_up(response, "params", "uri"),
1683-
to_string_view(this->fs.file_uri_prefix_8() + u8"quick-lint-js.config"));
1682+
EXPECT_EQ(look_up(response, "params", "uri"),
1683+
to_boost_string_view(this->fs.file_uri_prefix_8() +
1684+
u8"quick-lint-js.config"));
16841685
EXPECT_EQ(look_up(response, "params", "version"), 2);
16851686
::boost::json::array diagnostics =
16861687
look_up(response, "params", "diagnostics").as_array();
@@ -1907,8 +1908,8 @@ TEST_F(test_linting_lsp_server, showing_io_errors_shows_only_first) {
19071908
EXPECT_EQ(look_up(show_message_message, "method"), "window/showMessage");
19081909
EXPECT_EQ(look_up(show_message_message, "params", "type"),
19091910
lsp_warning_message_type);
1910-
std::string message(
1911-
look_up(show_message_message, "params", "message").as_string());
1911+
std::string message(to_string_view(
1912+
look_up(show_message_message, "params", "message").as_string()));
19121913
EXPECT_THAT(message, ::testing::HasSubstr("/banana"));
19131914
EXPECT_THAT(message, ::testing::Not(::testing::HasSubstr("orange")));
19141915
}
@@ -1932,8 +1933,8 @@ TEST_F(test_linting_lsp_server, showing_io_errors_shows_only_first_ever) {
19321933

19331934
ASSERT_EQ(this->client->messages.size(), 1);
19341935
::boost::json::value show_message_message = this->client->messages[0];
1935-
std::string message(
1936-
look_up(show_message_message, "params", "message").as_string());
1936+
std::string message(to_string_view(
1937+
look_up(show_message_message, "params", "message").as_string()));
19371938
EXPECT_THAT(message, ::testing::HasSubstr("/banana"));
19381939
EXPECT_THAT(message, ::testing::Not(::testing::HasSubstr("orange")));
19391940
}
@@ -1943,7 +1944,8 @@ void expect_error(::boost::json::value& response, int error_code,
19431944
EXPECT_FALSE(response.as_object().contains("method"));
19441945
EXPECT_EQ(look_up(response, "jsonrpc"), "2.0");
19451946
EXPECT_EQ(look_up(response, "error", "code"), error_code);
1946-
EXPECT_EQ(look_up(response, "error", "message"), error_message);
1947+
EXPECT_EQ(look_up(response, "error", "message"),
1948+
to_boost_string_view(error_message));
19471949
}
19481950

19491951
TEST_F(test_linting_lsp_server, invalid_json_in_request) {

0 commit comments

Comments
 (0)