Skip to content

Commit

Permalink
Merge pull request #1737 from xlsynth:cdleary/2024-11-20-dslx-path-in…
Browse files Browse the repository at this point in the history
…-lsp

PiperOrigin-RevId: 700494917
  • Loading branch information
copybara-github committed Nov 27, 2024
2 parents 96a1ab0 + 2d9ac63 commit 693ecc2
Show file tree
Hide file tree
Showing 80 changed files with 963 additions and 590 deletions.
1 change: 1 addition & 0 deletions xls/codegen/vast/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ cc_library(
"//xls/dslx:interp_bindings",
"//xls/dslx:interp_value",
"//xls/dslx:parse_and_typecheck",
"//xls/dslx:virtualizable_file_system",
"//xls/dslx:warning_collector",
"//xls/dslx:warning_kind",
"//xls/dslx/bytecode",
Expand Down
4 changes: 3 additions & 1 deletion xls/codegen/vast/dslx_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "xls/dslx/type_system/typecheck_function.h"
#include "xls/dslx/type_system/typecheck_invocation.h"
#include "xls/dslx/type_system/typecheck_module.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_collector.h"
#include "xls/dslx/warning_kind.h"
#include "xls/ir/bits.h"
Expand Down Expand Up @@ -792,7 +793,8 @@ absl::StatusOr<std::string> DslxBuilder::FormatModule() {
}
}
dslx::Comments comments = dslx::Comments::Create(comment_data);
return AutoFmt(*parsed_module.module, comments, /*text_width=*/100);
return AutoFmt(import_data.vfs(), *parsed_module.module, comments,
/*text_width=*/100);
}

} // namespace xls
1 change: 1 addition & 0 deletions xls/dev_tools/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,7 @@ cc_binary(
"//xls/dslx:create_import_data",
"//xls/dslx:import_data",
"//xls/dslx:mangle",
"//xls/dslx:virtualizable_file_system",
"//xls/dslx:warning_collector",
"//xls/dslx:warning_kind",
"//xls/dslx/frontend:ast",
Expand Down
1 change: 1 addition & 0 deletions xls/dev_tools/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "xls/dslx/type_system/type.h"
#include "xls/dslx/type_system/type_info.h"
#include "xls/dslx/type_system/typecheck_module.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_collector.h"
#include "xls/dslx/warning_kind.h"
#include "xls/ir/function.h"
Expand Down
30 changes: 23 additions & 7 deletions xls/dslx/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ bzl_library(
srcs = ["strip_comments.bzl"],
)

cc_library(
name = "virtualizable_file_system",
srcs = ["virtualizable_file_system.cc"],
hdrs = ["virtualizable_file_system.h"],
deps = [
"//xls/common/file:filesystem",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings:str_format",
],
)

cc_library(
name = "constexpr_evaluator",
srcs = ["constexpr_evaluator.cc"],
Expand Down Expand Up @@ -113,6 +125,7 @@ cc_library(
deps = [
":default_dslx_stdlib_path",
":import_data",
":virtualizable_file_system",
":warning_kind",
"//xls/dslx/bytecode:bytecode_cache",
"@com_google_absl//absl/memory",
Expand Down Expand Up @@ -317,8 +330,8 @@ cc_library(
":errors",
":import_record",
":interp_bindings",
":virtualizable_file_system",
":warning_kind",
"//xls/common/file:filesystem",
"//xls/common/status:ret_check",
"//xls/common/status:status_macros",
"//xls/dslx/bytecode:bytecode_cache_interface",
Expand Down Expand Up @@ -347,6 +360,7 @@ cc_library(
data = ["//xls/dslx/stdlib:x_files"],
deps = [
":import_data",
":virtualizable_file_system",
"//xls/common/config:xls_config",
"//xls/common/file:get_runfile_path",
"//xls/common/status:ret_check",
Expand Down Expand Up @@ -448,14 +462,13 @@ cc_library(
srcs = ["error_printer.cc"],
hdrs = ["error_printer.h"],
deps = [
":virtualizable_file_system",
":warning_collector",
"//xls/common/file:filesystem",
"//xls/common/status:ret_check",
"//xls/common/status:status_macros",
"//xls/dslx/frontend:pos",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_googlesource_code_re2//:re2",
Expand All @@ -467,11 +480,11 @@ cc_test(
srcs = ["error_printer_test.cc"],
deps = [
":error_printer",
":virtualizable_file_system",
"//xls/common:xls_gunit_main",
"//xls/common/file:temp_file",
"//xls/common/status:matchers",
"//xls/dslx/frontend:pos",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest",
],
Expand All @@ -484,7 +497,6 @@ cc_library(
deps = [
":import_data",
":warning_collector",
"//xls/common/file:filesystem",
"//xls/common/file:get_runfile_path",
"//xls/common/status:ret_check",
"//xls/common/status:status_macros",
Expand Down Expand Up @@ -514,6 +526,7 @@ cc_library(
hdrs = ["command_line_utils.h"],
deps = [
":error_printer",
":virtualizable_file_system",
"//xls/common/status:ret_check",
"//xls/dslx/frontend:bindings",
"//xls/dslx/frontend:pos",
Expand All @@ -534,6 +547,7 @@ cc_binary(
deps = [
":command_line_utils",
":default_dslx_stdlib_path",
":virtualizable_file_system",
":warning_kind",
"//xls/common:exit_status",
"//xls/common:init_xls",
Expand Down Expand Up @@ -586,9 +600,9 @@ cc_binary(
name = "highlight_main",
srcs = ["highlight_main.cc"],
deps = [
":virtualizable_file_system",
"//xls/common:exit_status",
"//xls/common:init_xls",
"//xls/common/file:filesystem",
"//xls/common/status:status_macros",
"//xls/dslx/frontend:builtins_metadata",
"//xls/dslx/frontend:pos",
Expand Down Expand Up @@ -620,9 +634,9 @@ cc_binary(
name = "strip_comments_main",
srcs = ["strip_comments_main.cc"],
deps = [
":virtualizable_file_system",
"//xls/common:exit_status",
"//xls/common:init_xls",
"//xls/common/file:filesystem",
"//xls/common/status:status_macros",
"//xls/dslx/frontend:pos",
"//xls/dslx/frontend:scanner",
Expand Down Expand Up @@ -767,6 +781,7 @@ cc_library(
":import_data",
":interp_value",
":parse_and_typecheck",
":virtualizable_file_system",
":warning_kind",
"//xls/common/status:status_macros",
"//xls/dslx/frontend:ast",
Expand Down Expand Up @@ -824,6 +839,7 @@ cc_binary(
":default_dslx_stdlib_path",
":import_data",
":parse_and_typecheck",
":virtualizable_file_system",
":warning_kind",
"//xls/common:exit_status",
"//xls/common:init_xls",
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/bytecode/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ cc_test(
"//xls/dslx:import_data",
"//xls/dslx:interp_value",
"//xls/dslx:parse_and_typecheck",
"//xls/dslx:virtualizable_file_system",
"//xls/dslx/frontend:ast",
"//xls/dslx/frontend:pos",
"//xls/dslx/frontend:proc_id",
Expand Down
9 changes: 3 additions & 6 deletions xls/dslx/bytecode/bytecode_interpreter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "xls/dslx/run_routines/run_routines.h"
#include "xls/dslx/type_system/parametric_env.h"
#include "xls/dslx/type_system/type_info.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/ir/bits.h"
#include "xls/ir/format_preference.h"
#include "xls/ir/format_strings.h"
Expand All @@ -68,12 +69,8 @@ absl::StatusOr<TypecheckedModule> ParseAndTypecheckOrPrintError(
absl::StatusOr<TypecheckedModule> tm_or =
ParseAndTypecheck(program, "test.x", "test", import_data);
if (!tm_or.ok()) {
TryPrintError(
tm_or.status(),
[&](std::string_view) -> absl::StatusOr<std::string> {
return std::string{program};
},
import_data->file_table());
UniformContentFilesystem vfs(program);
TryPrintError(tm_or.status(), import_data->file_table(), vfs);
}
return tm_or;
}
Expand Down
13 changes: 5 additions & 8 deletions xls/dslx/command_line_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <stdio.h> // NOLINT(modernize-deprecated-headers)
#include <unistd.h>

#include <functional>
#include <iostream>
#include <optional>
#include <string>
Expand All @@ -33,14 +32,12 @@
#include "xls/dslx/error_printer.h"
#include "xls/dslx/frontend/bindings.h"
#include "xls/dslx/frontend/pos.h"
#include "xls/dslx/virtualizable_file_system.h"

namespace xls::dslx {

bool TryPrintError(
const absl::Status& status,
const std::function<absl::StatusOr<std::string>(std::string_view)>&
get_file_contents,
FileTable& file_table) {
bool TryPrintError(const absl::Status& status, FileTable& file_table,
VirtualizableFilesystem& vfs) {
if (status.ok()) {
return false;
}
Expand All @@ -55,10 +52,10 @@ bool TryPrintError(
bool is_tty = isatty(fileno(stderr)) != 0;
absl::Status print_status = PrintPositionalError(
data.span, absl::StrFormat("%s: %s", data.error_type, data.message),
std::cerr, get_file_contents,
std::cerr,
is_tty ? PositionalErrorColor::kErrorColor
: PositionalErrorColor::kNoColor,
file_table);
file_table, vfs);
if (!print_status.ok()) {
LOG(ERROR) << "Could not print positional error: " << print_status;
}
Expand Down
9 changes: 3 additions & 6 deletions xls/dslx/command_line_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#ifndef XLS_DSLX_COMMAND_LINE_UTILS_H_
#define XLS_DSLX_COMMAND_LINE_UTILS_H_

#include <functional>
#include <string>
#include <string_view>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "xls/dslx/frontend/pos.h"
#include "xls/dslx/virtualizable_file_system.h"

namespace xls::dslx {

Expand All @@ -32,11 +32,8 @@ namespace xls::dslx {
// is not (i.e. because it does not have position information) false is
// returned, and the status should likely be propagated to the caller instead of
// squashed in some way.
bool TryPrintError(
const absl::Status& status,
const std::function<absl::StatusOr<std::string>(std::string_view)>&
get_file_contents,
FileTable& file_table);
bool TryPrintError(const absl::Status& status, FileTable& file_table,
VirtualizableFilesystem& vfs);

// Converts a path to a DSLX module into its corresponding module name; e.g.
//
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/cpp_transpiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ cc_binary(
"//xls/dslx:default_dslx_stdlib_path",
"//xls/dslx:import_data",
"//xls/dslx:parse_and_typecheck",
"//xls/dslx:virtualizable_file_system",
"//xls/dslx:warning_kind",
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/log",
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/cpp_transpiler/cpp_transpiler_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "xls/dslx/default_dslx_stdlib_path.h"
#include "xls/dslx/import_data.h"
#include "xls/dslx/parse_and_typecheck.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_kind.h"

ABSL_FLAG(std::string, output_header_path, "",
Expand Down
1 change: 1 addition & 0 deletions xls/dslx/create_import_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "xls/dslx/bytecode/bytecode_cache.h"
#include "xls/dslx/default_dslx_stdlib_path.h"
#include "xls/dslx/import_data.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_kind.h"

namespace xls::dslx {
Expand Down
2 changes: 1 addition & 1 deletion xls/dslx/create_import_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

#include "absl/types/span.h"
#include "xls/dslx/import_data.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_kind.h"

namespace xls::dslx {

// Creates an ImportData with the given stdlib and search paths and assigns a
Expand Down
12 changes: 8 additions & 4 deletions xls/dslx/dslx_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "xls/dslx/frontend/comment_data.h"
#include "xls/dslx/import_data.h"
#include "xls/dslx/parse_and_typecheck.h"
#include "xls/dslx/virtualizable_file_system.h"
#include "xls/dslx/warning_kind.h"

// Note: we attempt to keep our command line interface similar to clang-format.
Expand Down Expand Up @@ -73,21 +74,24 @@ absl::Status RealMain(std::string_view input_path,
}
std::filesystem::path path = input_path;

XLS_ASSIGN_OR_RETURN(std::string module_name, PathToName(path.c_str()));
XLS_ASSIGN_OR_RETURN(std::string contents, GetFileContents(path));

ImportData import_data =
CreateImportData(xls::kDefaultDslxStdlibPath,
/*additional_search_paths=*/{}, kNoWarningsSet,
std::make_unique<RealFilesystem>());

XLS_ASSIGN_OR_RETURN(std::string module_name, PathToName(path.c_str()));
XLS_ASSIGN_OR_RETURN(std::string contents,
import_data.vfs().GetFileContents(path));

std::string formatted;
if (mode == "autofmt") {
std::vector<CommentData> comments_vec;
XLS_ASSIGN_OR_RETURN(std::unique_ptr<Module> module,
ParseModule(contents, path.c_str(), module_name,
import_data.file_table(), &comments_vec));
Comments comments = Comments::Create(comments_vec);
XLS_ASSIGN_OR_RETURN(formatted, AutoFmt(*module, comments, contents));
XLS_ASSIGN_OR_RETURN(
formatted, AutoFmt(import_data.vfs(), *module, comments, contents));
} else if (mode == "typecheck") {
// Note: we don't flag any warnings in this binary as we're just formatting
// the text.
Expand Down
Loading

0 comments on commit 693ecc2

Please sign in to comment.