Commit 3bd49f9b authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Throw exception instead of aborting if something goes wrong

This CL enables exceptions for the Torque compiler and Torque language
server. Instead of aborting when something goes wrong during
compilation, a TorqueError is thrown, containing the error message
and a source position. The compiler executable still prints the error
and aborts, while the language server will pass this information
along to the client (not included in this CL).

R=danno@chromium.org

Bug: v8:8880
Change-Id: Iad83c46fb6a91c1babbc0ae7dbd94fbe4e7f1663
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1526003Reviewed-by: 's avatarDaniel Clifford <danno@chromium.org>
Commit-Queue: Simon Zünd <szuend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60512}
parent 7eaa0b0b
......@@ -3225,9 +3225,13 @@ v8_source_set("torque_base") {
":v8_shared_internal_headers",
]
configs = [ ":internal_config" ]
configs = [
":internal_config",
"//build/config/compiler:exceptions",
]
remove_configs = [ "//build/config/compiler:no_exceptions" ]
if (is_win && is_asan) {
remove_configs = [ "//build/config/sanitizers:default_sanitizer_flags" ]
remove_configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
}
}
......@@ -3249,9 +3253,13 @@ v8_source_set("torque_ls_base") {
":torque_base",
]
configs = [ ":internal_config" ]
configs = [
":internal_config",
"//build/config/compiler:exceptions",
]
remove_configs = [ "//build/config/compiler:no_exceptions" ]
if (is_win && is_asan) {
remove_configs = [ "//build/config/sanitizers:default_sanitizer_flags" ]
remove_configs += [ "//build/config/sanitizers:default_sanitizer_flags" ]
}
}
......
......@@ -180,13 +180,19 @@ class JsonGrammar : public Grammar {
Symbol file = {Rule({&value})};
};
JsonValue ParseJson(const std::string& input) {
JsonParserResult ParseJson(const std::string& input) {
// Torque needs a CurrentSourceFile scope during parsing.
// As JSON lives in memory only, a unknown file scope is created.
SourceFileMap::Scope source_map_scope;
CurrentSourceFile::Scope unkown_file(SourceFileMap::AddSource("<json>"));
return (*JsonGrammar().Parse(input)).Cast<JsonValue>();
JsonParserResult result;
try {
result.value = (*JsonGrammar().Parse(input)).Cast<JsonValue>();
} catch (TorqueError& error) {
result.error = error;
}
return result;
}
} // namespace ls
......
......@@ -6,14 +6,21 @@
#define V8_TORQUE_LS_JSON_PARSER_H_
#include "src/base/macros.h"
#include "src/base/optional.h"
#include "src/torque/ls/json.h"
#include "src/torque/utils.h"
namespace v8 {
namespace internal {
namespace torque {
namespace ls {
V8_EXPORT_PRIVATE JsonValue ParseJson(const std::string& input);
struct JsonParserResult {
base::Optional<TorqueError> error;
JsonValue value;
};
V8_EXPORT_PRIVATE JsonParserResult ParseJson(const std::string& input);
} // namespace ls
} // namespace torque
......
......@@ -26,6 +26,13 @@ struct JsonValue {
public:
enum { OBJECT, ARRAY, STRING, NUMBER, BOOL, IS_NULL } tag;
// JsonValues can only be moved, not copied.
JsonValue() V8_NOEXCEPT = default;
constexpr JsonValue(const JsonValue& other) = delete;
JsonValue(JsonValue&& other) V8_NOEXCEPT = default;
JsonValue& operator=(const JsonValue& other) = delete;
JsonValue& operator=(JsonValue&& other) V8_NOEXCEPT = default;
static JsonValue From(double number) {
JsonValue result;
result.tag = JsonValue::NUMBER;
......
......@@ -42,7 +42,7 @@ JsonValue ReadMessage() {
Logger::Log("[incoming] ", content, "\n\n");
return ParseJson(content);
return ParseJson(content).value;
}
void WriteMessage(JsonValue& message) {
......
......@@ -85,24 +85,36 @@ void CompileCurrentAst(TorqueCompilerOptions options) {
} // namespace
void CompileTorque(const std::string& source, TorqueCompilerOptions options) {
TorqueCompilerResult CompileTorque(const std::string& source,
TorqueCompilerOptions options) {
CurrentSourceFile::Scope no_file_scope(SourceFileMap::AddSource("<torque>"));
CurrentAst::Scope ast_scope_;
LintErrorStatus::Scope lint_error_status_scope_;
TorqueCompilerResult result;
try {
ParseTorque(source);
CompileCurrentAst(options);
} catch (TorqueError& error) {
result.error = error;
}
return result;
}
void CompileTorque(std::vector<std::string> files,
TorqueCompilerResult CompileTorque(std::vector<std::string> files,
TorqueCompilerOptions options) {
CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
CurrentAst::Scope ast_scope_;
LintErrorStatus::Scope lint_error_status_scope_;
TorqueCompilerResult result;
try {
for (const auto& path : files) ReadAndParseTorqueFile(path);
CompileCurrentAst(options);
} catch (TorqueError& error) {
result.error = error;
}
return result;
}
} // namespace torque
......
......@@ -21,9 +21,13 @@ struct TorqueCompilerOptions {
bool abort_on_lint_errors;
};
V8_EXPORT_PRIVATE void CompileTorque(const std::string& source,
TorqueCompilerOptions options);
void CompileTorque(std::vector<std::string> files,
struct TorqueCompilerResult {
base::Optional<TorqueError> error;
};
V8_EXPORT_PRIVATE TorqueCompilerResult
CompileTorque(const std::string& source, TorqueCompilerOptions options);
TorqueCompilerResult CompileTorque(std::vector<std::string> files,
TorqueCompilerOptions options);
} // namespace torque
......
......@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/torque/source-positions.h"
#include "src/torque/torque-compiler.h"
namespace v8 {
......@@ -36,7 +37,13 @@ int WrappedMain(int argc, const char** argv) {
options.collect_language_server_data = false;
options.abort_on_lint_errors = true;
CompileTorque(files, options);
TorqueCompilerResult result = CompileTorque(files, options);
if (result.error) {
TorqueError& error = *result.error;
if (error.position) std::cerr << PositionAsString(*error.position) << ": ";
std::cerr << "Torque error: " << error.message << "\n";
v8::base::OS::Abort();
}
return 0;
}
......
......@@ -120,11 +120,11 @@ std::string CurrentPositionAsString() {
DEFINE_CONTEXTUAL_VARIABLE(LintErrorStatus)
[[noreturn]] void ReportErrorString(const std::string& error,
bool print_position) {
if (print_position) std::cerr << CurrentPositionAsString() << ": ";
std::cerr << ": Torque error: " << error << "\n";
v8::base::OS::Abort();
[[noreturn]] void ThrowTorqueError(const std::string& message,
bool include_position) {
TorqueError error(message);
if (include_position) error.position = CurrentSourcePosition::Get();
throw error;
}
void LintError(const std::string& error) {
......
......@@ -14,6 +14,7 @@
#include "src/base/functional.h"
#include "src/base/optional.h"
#include "src/torque/contextual.h"
#include "src/torque/source-positions.h"
namespace v8 {
namespace internal {
......@@ -51,19 +52,26 @@ bool IsSnakeCase(const std::string& s);
bool IsValidNamespaceConstName(const std::string& s);
bool IsValidTypeName(const std::string& s);
[[noreturn]] void ReportErrorString(const std::string& error,
bool print_position);
struct TorqueError : public std::exception {
explicit TorqueError(const std::string& message) : message(message) {}
std::string message;
base::Optional<SourcePosition> position;
};
[[noreturn]] void ThrowTorqueError(const std::string& error,
bool include_position);
template <class... Args>
[[noreturn]] void ReportError(Args&&... args) {
std::stringstream s;
USE((s << std::forward<Args>(args))...);
ReportErrorString(s.str(), true);
ThrowTorqueError(s.str(), true);
}
template <class... Args>
[[noreturn]] void ReportErrorWithoutPosition(Args&&... args) {
std::stringstream s;
USE((s << std::forward<Args>(args))...);
ReportErrorString(s.str(), false);
ThrowTorqueError(s.str(), false);
}
std::string CapifyStringWithUnderscores(const std::string& camellified_string);
......
......@@ -5,7 +5,9 @@
#include "src/torque/ls/json-parser.h"
#include "src/torque/ls/json.h"
#include "src/torque/source-positions.h"
#include "src/torque/utils.h"
#include "test/unittests/test-utils.h"
#include "testing/gmock-support.h"
namespace v8 {
namespace internal {
......@@ -13,38 +15,38 @@ namespace torque {
namespace ls {
TEST(LanguageServerJson, TestJsonPrimitives) {
const JsonValue true_result = ParseJson("true");
const JsonValue true_result = ParseJson("true").value;
ASSERT_EQ(true_result.tag, JsonValue::BOOL);
EXPECT_EQ(true_result.ToBool(), true);
const JsonValue false_result = ParseJson("false");
const JsonValue false_result = ParseJson("false").value;
ASSERT_EQ(false_result.tag, JsonValue::BOOL);
EXPECT_EQ(false_result.ToBool(), false);
const JsonValue null_result = ParseJson("null");
const JsonValue null_result = ParseJson("null").value;
ASSERT_EQ(null_result.tag, JsonValue::IS_NULL);
const JsonValue number = ParseJson("42");
const JsonValue number = ParseJson("42").value;
ASSERT_EQ(number.tag, JsonValue::NUMBER);
EXPECT_EQ(number.ToNumber(), 42);
}
TEST(LanguageServerJson, TestJsonStrings) {
const JsonValue basic = ParseJson("\"basic\"");
const JsonValue basic = ParseJson("\"basic\"").value;
ASSERT_EQ(basic.tag, JsonValue::STRING);
EXPECT_EQ(basic.ToString(), "basic");
const JsonValue singleQuote = ParseJson("\"'\"");
const JsonValue singleQuote = ParseJson("\"'\"").value;
ASSERT_EQ(singleQuote.tag, JsonValue::STRING);
EXPECT_EQ(singleQuote.ToString(), "'");
}
TEST(LanguageServerJson, TestJsonArrays) {
const JsonValue empty_array = ParseJson("[]");
const JsonValue empty_array = ParseJson("[]").value;
ASSERT_EQ(empty_array.tag, JsonValue::ARRAY);
EXPECT_EQ(empty_array.ToArray().size(), (size_t)0);
const JsonValue number_array = ParseJson("[1, 2, 3, 4]");
const JsonValue number_array = ParseJson("[1, 2, 3, 4]").value;
ASSERT_EQ(number_array.tag, JsonValue::ARRAY);
const JsonArray& array = number_array.ToArray();
......@@ -52,7 +54,7 @@ TEST(LanguageServerJson, TestJsonArrays) {
ASSERT_EQ(array[1].tag, JsonValue::NUMBER);
EXPECT_EQ(array[1].ToNumber(), 2);
const JsonValue string_array_object = ParseJson("[\"a\", \"b\"]");
const JsonValue string_array_object = ParseJson("[\"a\", \"b\"]").value;
ASSERT_EQ(string_array_object.tag, JsonValue::ARRAY);
const JsonArray& string_array = string_array_object.ToArray();
......@@ -62,11 +64,12 @@ TEST(LanguageServerJson, TestJsonArrays) {
}
TEST(LanguageServerJson, TestJsonObjects) {
const JsonValue empty_object = ParseJson("{}");
const JsonValue empty_object = ParseJson("{}").value;
ASSERT_EQ(empty_object.tag, JsonValue::OBJECT);
EXPECT_EQ(empty_object.ToObject().size(), (size_t)0);
const JsonValue primitive_fields = ParseJson("{ \"flag\": true, \"id\": 5}");
const JsonValue primitive_fields =
ParseJson("{ \"flag\": true, \"id\": 5}").value;
EXPECT_EQ(primitive_fields.tag, JsonValue::OBJECT);
const JsonValue& flag = primitive_fields.ToObject().at("flag");
......@@ -78,7 +81,8 @@ TEST(LanguageServerJson, TestJsonObjects) {
EXPECT_EQ(id.ToNumber(), 5);
const JsonValue& complex_fields =
ParseJson("{ \"array\": [], \"object\": { \"name\": \"torque\" } }");
ParseJson("{ \"array\": [], \"object\": { \"name\": \"torque\" } }")
.value;
ASSERT_EQ(complex_fields.tag, JsonValue::OBJECT);
const JsonValue& array = complex_fields.ToObject().at("array");
......@@ -91,12 +95,26 @@ TEST(LanguageServerJson, TestJsonObjects) {
EXPECT_EQ(object.ToObject().at("name").ToString(), "torque");
}
TEST(LanguageServerJsonDeathTest, SyntaxError) {
ASSERT_DEATH(ParseJson("{]"), "Parser Error: unexpected token");
ASSERT_DEATH(ParseJson("{ noquoteskey: null }"),
"Lexer Error: unknown token");
// These tests currently fail on Windows as there seems to be a linking
// issue with exceptions enabled for Torque.
// TODO(szuend): Remove the OS check when errors are reported differently,
// or the issue is resolved.
#if !defined(V8_OS_WIN)
using ::testing::HasSubstr;
TEST(LanguageServerJson, ParserError) {
JsonParserResult result = ParseJson("{]");
ASSERT_TRUE(result.error.has_value());
EXPECT_THAT(result.error->message,
HasSubstr("Parser Error: unexpected token"));
}
TEST(LanguageServerJson, LexerError) {
JsonParserResult result = ParseJson("{ noquoteskey: null }");
ASSERT_TRUE(result.error.has_value());
EXPECT_THAT(result.error->message, HasSubstr("Lexer Error: unknown token"));
}
#endif
} // namespace ls
} // namespace torque
} // namespace internal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment