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

[torque-ls] Allow compilation of plain std::string inputs

This CL refactors the torque-compiler module slightly to allow
compilation of string inputs in addition to file path inputs. The
added functionality is then used to implement the first
'goto type defintion' unit test.

R=tebbi@chromium.org

Bug: v8:8880
Change-Id: I178a387abda6e319e66d41c50431cb139ac6e9f5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1503263
Commit-Queue: Simon Zünd <szuend@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60047}
parent c8057613
...@@ -34,6 +34,10 @@ struct LineAndColumn { ...@@ -34,6 +34,10 @@ struct LineAndColumn {
int column; int column;
static LineAndColumn Invalid() { return {-1, -1}; } static LineAndColumn Invalid() { return {-1, -1}; }
bool operator==(const LineAndColumn& other) const {
return line == other.line && column == other.column;
}
}; };
struct SourcePosition { struct SourcePosition {
...@@ -58,6 +62,10 @@ struct SourcePosition { ...@@ -58,6 +62,10 @@ struct SourcePosition {
if (pos.line == end.line && pos.column >= end.column) return false; if (pos.line == end.line && pos.column >= end.column) return false;
return true; return true;
} }
bool operator==(const SourcePosition& pos) const {
return source == pos.source && start == pos.start && end == pos.end;
}
}; };
DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId); DECLARE_CONTEXTUAL_VARIABLE(CurrentSourceFile, SourceId);
......
...@@ -45,16 +45,7 @@ void ReadAndParseTorqueFile(const std::string& path) { ...@@ -45,16 +45,7 @@ void ReadAndParseTorqueFile(const std::string& path) {
ParseTorque(*maybe_content); ParseTorque(*maybe_content);
} }
} // namespace void CompileCurrentAst(TorqueCompilerOptions options) {
void 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_;
for (const auto& path : files) ReadAndParseTorqueFile(path);
GlobalContext::Scope global_context(std::move(CurrentAst::Get())); GlobalContext::Scope global_context(std::move(CurrentAst::Get()));
if (options.verbose) GlobalContext::SetVerbose(); if (options.verbose) GlobalContext::SetVerbose();
if (options.collect_language_server_data) { if (options.collect_language_server_data) {
...@@ -92,6 +83,28 @@ void CompileTorque(std::vector<std::string> files, ...@@ -92,6 +83,28 @@ void CompileTorque(std::vector<std::string> files,
if (LintErrorStatus::HasLintErrors()) std::abort(); if (LintErrorStatus::HasLintErrors()) std::abort();
} }
} // namespace
void 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_;
ParseTorque(source);
CompileCurrentAst(options);
}
void 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_;
for (const auto& path : files) ReadAndParseTorqueFile(path);
CompileCurrentAst(options);
}
} // namespace torque } // namespace torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -19,12 +19,12 @@ struct TorqueCompilerOptions { ...@@ -19,12 +19,12 @@ struct TorqueCompilerOptions {
bool verbose; bool verbose;
bool collect_language_server_data; bool collect_language_server_data;
bool abort_on_lint_errors; bool abort_on_lint_errors;
static TorqueCompilerOptions Default() { return {"", false, false, false}; }
}; };
V8_EXPORT_PRIVATE void CompileTorque(const std::string& source,
TorqueCompilerOptions options);
void CompileTorque(std::vector<std::string> files, void CompileTorque(std::vector<std::string> files,
TorqueCompilerOptions = TorqueCompilerOptions::Default()); TorqueCompilerOptions options);
} // namespace torque } // namespace torque
} // namespace internal } // namespace internal
......
...@@ -200,6 +200,7 @@ v8_source_set("unittests_sources") { ...@@ -200,6 +200,7 @@ v8_source_set("unittests_sources") {
"torque/earley-parser-unittest.cc", "torque/earley-parser-unittest.cc",
"torque/ls-json-unittest.cc", "torque/ls-json-unittest.cc",
"torque/ls-message-unittest.cc", "torque/ls-message-unittest.cc",
"torque/ls-server-data-unittest.cc",
"torque/torque-unittest.cc", "torque/torque-unittest.cc",
"torque/torque-utils-unittest.cc", "torque/torque-utils-unittest.cc",
"unicode-unittest.cc", "unicode-unittest.cc",
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/torque/server-data.h"
#include "src/torque/torque-compiler.h"
#include "test/unittests/test-utils.h"
namespace v8 {
namespace internal {
namespace torque {
namespace {
struct TestCompiler {
SourceFileMap::Scope file_map_scope;
LanguageServerData::Scope server_data_scope;
void Compile(const std::string& source) {
TorqueCompilerOptions options;
options.abort_on_lint_errors = false;
options.output_directory = "";
options.verbose = false;
options.collect_language_server_data = true;
CompileTorque(source, options);
}
};
} // namespace
TEST(LanguageServer, GotoTypeDefinition) {
const std::string source =
"type void;\n"
"type never;\n"
"type T1 generates 'TNode<Object>';\n"
"type T2 generates 'TNode<Object>';\n"
"macro SomeMacro(a: T1, b: T2): T1 { return a; }";
TestCompiler compiler;
compiler.Compile(source);
// Find the definition for type 'T1' of argument 'a' on line 4.
const SourceId id = SourceFileMap::GetSourceId("<torque>");
auto maybe_position = LanguageServerData::FindDefinition(id, {4, 19});
ASSERT_TRUE(maybe_position.has_value());
EXPECT_EQ(*maybe_position, (SourcePosition{id, {2, 5}, {2, 7}}));
// Find the defintion for type 'T2' of argument 'b' on line 4.
maybe_position = LanguageServerData::FindDefinition(id, {4, 26});
ASSERT_TRUE(maybe_position.has_value());
EXPECT_EQ(*maybe_position, (SourcePosition{id, {3, 5}, {3, 7}}));
}
} // namespace torque
} // namespace internal
} // namespace v8
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