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
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
-I build/_deps/nlohmann_json-src/include \
-I build/src/parser \
-DCXX_NO_FILESYSTEM \
-DCXX_NO_THREADS \
$i
done

Expand Down Expand Up @@ -218,6 +219,7 @@ jobs:
-I build.wasi/_deps/nlohmann_json-src/include \
-I build.wasi/src/parser \
-DCXX_NO_FILESYSTEM \
-DCXX_NO_THREADS \
$i
done

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ option(CXX_LIBCXX_WITH_CLANG "Link with libc++" OFF)
option(CXX_BUILD_TESTS "Build tests" ON)
option(CXX_INTERPROCEDURAL_OPTIMIZATION "Enable interprocedural optimization" OFF)

find_package(Threads)

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CXX_LIBCXX_WITH_CLANG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
Expand Down
14 changes: 4 additions & 10 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
{
"name": "install",
"configurePreset": "default",
"targets": [
"install"
]
"targets": ["install"]
},
{
"name": "build-emscripten",
Expand All @@ -87,9 +85,7 @@
{
"name": "install-emscripten",
"configurePreset": "emscripten",
"targets": [
"install"
]
"targets": ["install"]
},
{
"name": "build-wasi",
Expand All @@ -98,9 +94,7 @@
{
"name": "install-wasi",
"configurePreset": "wasi",
"targets": [
"install"
]
"targets": ["install"]
}
],
"testPresets": [
Expand Down Expand Up @@ -174,4 +168,4 @@
]
}
]
}
}
11 changes: 10 additions & 1 deletion src/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,17 @@ target_compile_definitions(cxx PRIVATE
CXX_VERSION="${CMAKE_PROJECT_VERSION}"
)

# if cmake founds the Threads package, link with it
if(Threads_FOUND)
target_link_libraries(cxx Threads::Threads)
target_compile_options(cxx PRIVATE -pthread)
else()
target_compile_definitions(cxx PRIVATE CXX_NO_THREADS)
endif()

if(EMSCRIPTEN)
target_link_options(cxx PUBLIC
target_link_options(cxx PRIVATE -pthread)
target_link_options(cxx PRIVATE
"SHELL:-s EXIT_RUNTIME=1"
"SHELL:-s WASM_BIGINT=1"
"SHELL:-s NODERAWFS=1"
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/cxx/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ std::vector<CLIOptionDescr> options{

{"-lsp", "Start Language Server", &CLI::opt_lsp},

{"-lsp-test", "Start Language Server in testing mode", &CLI::opt_lsp_test},

{"-j", "<n>", "Run <n> jobs in parallel.", CLIOptionDescrKind::kSeparated},

{"-v", "Show commands to run and use verbose output", &CLI::opt_v},

};
Expand Down
1 change: 1 addition & 0 deletions src/frontend/cxx/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CLI {
bool opt_v = false;
bool opt_emit_ast = false;
bool opt_lsp = false;
bool opt_lsp_test = false;

void parse(int& argc, char**& argv);

Expand Down
218 changes: 218 additions & 0 deletions src/frontend/cxx/cxx_document.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// Copyright (c) 2024 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "cxx_document.h"

#include <cxx/ast.h>
#include <cxx/ast_visitor.h>
#include <cxx/control.h>
#include <cxx/gcc_linux_toolchain.h>
#include <cxx/lexer.h>
#include <cxx/lsp/enums.h>
#include <cxx/lsp/requests.h>
#include <cxx/lsp/types.h>
#include <cxx/macos_toolchain.h>
#include <cxx/preprocessor.h>
#include <cxx/private/path.h>
#include <cxx/scope.h>
#include <cxx/symbol_printer.h>
#include <cxx/symbols.h>
#include <cxx/translation_unit.h>
#include <cxx/wasm32_wasi_toolchain.h>
#include <cxx/windows_toolchain.h>

namespace cxx::lsp {

namespace {

struct Diagnostics final : cxx::DiagnosticsClient {
json messages = json::array();
Vector<lsp::Diagnostic> diagnostics{messages};

void report(const cxx::Diagnostic& diag) override {
std::string_view fileName;
std::uint32_t line = 0;
std::uint32_t column = 0;

preprocessor()->getTokenStartPosition(diag.token(), &line, &column,
&fileName);

std::uint32_t endLine = 0;
std::uint32_t endColumn = 0;

preprocessor()->getTokenEndPosition(diag.token(), &endLine, &endColumn,
nullptr);

auto tmp = json::object();

auto d = diagnostics.emplace_back();

int s = std::max(int(line) - 1, 0);
int sc = std::max(int(column) - 1, 0);
int e = std::max(int(endLine) - 1, 0);
int ec = std::max(int(endColumn) - 1, 0);

d.message(diag.message());
d.range().start(lsp::Position(tmp).line(s).character(sc));
d.range().end(lsp::Position(tmp).line(e).character(ec));
}
};

} // namespace

struct CxxDocument::Private {
const CLI& cli;
long version;
Control control;
Diagnostics diagnosticsClient;
TranslationUnit unit{&control, &diagnosticsClient};
std::shared_ptr<Toolchain> toolchain;

Private(const CLI& cli, long version) : cli(cli), version(version) {}

void configure();
};

void CxxDocument::Private::configure() {
auto preprocesor = unit.preprocessor();

auto toolchainId = cli.getSingle("-toolchain");

if (!toolchainId) {
toolchainId = "wasm32";
}

if (toolchainId == "darwin" || toolchainId == "macos") {
toolchain = std::make_unique<MacOSToolchain>(preprocesor);
} else if (toolchainId == "wasm32") {
auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocesor);

fs::path app_dir;

#if __wasi__
app_dir = fs::path("/usr/bin/");
#elif !defined(CXX_NO_FILESYSTEM)
app_dir = std::filesystem::canonical(
std::filesystem::path(cli.app_name).remove_filename());
#elif __unix__ || __APPLE__
char* app_name = realpath(cli.app_name.c_str(), nullptr);
app_dir = fs::path(app_name).remove_filename().string();
std::free(app_name);
#endif

wasmToolchain->setAppdir(app_dir.string());

if (auto paths = cli.get("--sysroot"); !paths.empty()) {
wasmToolchain->setSysroot(paths.back());
} else {
auto sysroot_dir = app_dir / std::string("../lib/wasi-sysroot");
wasmToolchain->setSysroot(sysroot_dir.string());
}

toolchain = std::move(wasmToolchain);
} else if (toolchainId == "linux") {
std::string host;
#ifdef __aarch64__
host = "aarch64";
#elif __x86_64__
host = "x86_64";
#endif

std::string arch = cli.getSingle("-arch").value_or(host);
toolchain = std::make_unique<GCCLinuxToolchain>(preprocesor, arch);
} else if (toolchainId == "windows") {
auto windowsToolchain = std::make_unique<WindowsToolchain>(preprocesor);

if (auto paths = cli.get("-vctoolsdir"); !paths.empty()) {
windowsToolchain->setVctoolsdir(paths.back());
}

if (auto paths = cli.get("-winsdkdir"); !paths.empty()) {
windowsToolchain->setWinsdkdir(paths.back());
}

if (auto versions = cli.get("-winsdkversion"); !versions.empty()) {
windowsToolchain->setWinsdkversion(versions.back());
}

toolchain = std::move(windowsToolchain);
}

if (toolchain) {
control.setMemoryLayout(toolchain->memoryLayout());

if (!cli.opt_nostdinc) toolchain->addSystemIncludePaths();

if (!cli.opt_nostdincpp) toolchain->addSystemCppIncludePaths();

toolchain->addPredefinedMacros();
}

for (const auto& path : cli.get("-I")) {
preprocesor->addSystemIncludePath(path);
}

for (const auto& macro : cli.get("-D")) {
auto sep = macro.find_first_of("=");

if (sep == std::string::npos) {
preprocesor->defineMacro(macro, "1");
} else {
preprocesor->defineMacro(macro.substr(0, sep), macro.substr(sep + 1));
}
}

for (const auto& macro : cli.get("-U")) {
preprocesor->undefMacro(macro);
}
}

CxxDocument::CxxDocument(const CLI& cli, long version)
: d(std::make_unique<Private>(cli, version)) {}

void CxxDocument::parse(std::string source, std::string fileName) {
d->configure();

auto& unit = d->unit;
auto& cli = d->cli;

unit.setSource(std::move(source), fileName);

auto preprocessor = unit.preprocessor();
preprocessor->squeeze();

unit.parse(ParserConfiguration{
.checkTypes = cli.opt_fcheck,
.fuzzyTemplateResolution = true,
.staticAssert = cli.opt_fstatic_assert || cli.opt_fcheck,
.reflect = !cli.opt_fno_reflect,
.templates = cli.opt_ftemplates,
});
}

CxxDocument::~CxxDocument() {}

auto CxxDocument::version() const -> long { return d->version; }

auto CxxDocument::diagnostics() const -> Vector<Diagnostic> {
return Vector<Diagnostic>(d->diagnosticsClient.messages);
}

} // namespace cxx::lsp
47 changes: 47 additions & 0 deletions src/frontend/cxx/cxx_document.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2024 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <cxx/lsp/fwd.h>

#include <memory>
#include <string>

#include "cli.h"

namespace cxx::lsp {

class CxxDocument {
public:
explicit CxxDocument(const CLI& cli, long version);
~CxxDocument();

void parse(std::string source, std::string fileName);

[[nodiscard]] auto version() const -> long;
[[nodiscard]] auto diagnostics() const -> Vector<Diagnostic>;

private:
struct Private;
std::unique_ptr<Private> d;
};

} // namespace cxx::lsp
Loading