diff --git a/src/frontend/cxx/frontend.cc b/src/frontend/cxx/frontend.cc index 89e6a894..9eace9f6 100644 --- a/src/frontend/cxx/frontend.cc +++ b/src/frontend/cxx/frontend.cc @@ -182,15 +182,15 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool { } if (toolchainId == "darwin" || toolchainId == "macos") { - auto macosToolchain = std::make_unique(preprocessor); - std::string host; -#ifdef __aarch64__ - host = "aarch64"; -#elif __x86_64__ + // on macOS we default to aarch64, since it is the most common + std::string host = "aarch64"; + +#if __x86_64__ host = "x86_64"; #endif - macosToolchain->setArch(cli.getSingle("-arch").value_or(host)); - toolchain = std::move(macosToolchain); + + toolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); } else if (toolchainId == "wasm32") { auto wasmToolchain = std::make_unique(preprocessor); @@ -219,17 +219,25 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool { toolchain = std::move(wasmToolchain); } else if (toolchainId == "linux") { - std::string host; + // on linux we default to x86_64, unless the host is aarch64 + std::string host = "x86_64"; + #ifdef __aarch64__ host = "aarch64"; -#elif __x86_64__ - host = "x86_64"; #endif - std::string arch = cli.getSingle("-arch").value_or(host); - toolchain = std::make_unique(preprocessor, arch); + toolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); } else if (toolchainId == "windows") { - auto windowsToolchain = std::make_unique(preprocessor); + // on linux we default to x86_64, unless the host is aarch64 + std::string host = "x86_64"; + +#ifdef __aarch64__ + host = "aarch64"; +#endif + + auto windowsToolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); if (auto paths = cli.get("-vctoolsdir"); !paths.empty()) { windowsToolchain->setVctoolsdir(paths.back()); diff --git a/src/lsp/cxx/lsp/cxx_document.cc b/src/lsp/cxx/lsp/cxx_document.cc index 9b8328f4..19c5232d 100644 --- a/src/lsp/cxx/lsp/cxx_document.cc +++ b/src/lsp/cxx/lsp/cxx_document.cc @@ -93,7 +93,7 @@ struct CxxDocument::Private { }; void CxxDocument::Private::configure() { - auto preprocesor = unit.preprocessor(); + auto preprocessor = unit.preprocessor(); auto toolchainId = cli.getSingle("-toolchain"); @@ -102,9 +102,15 @@ void CxxDocument::Private::configure() { } if (toolchainId == "darwin" || toolchainId == "macos") { - toolchain = std::make_unique(preprocesor); + std::string host = "aarch64"; +#ifdef __x86_64__ + host = "x86_64"; +#endif + + toolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); } else if (toolchainId == "wasm32") { - auto wasmToolchain = std::make_unique(preprocesor); + auto wasmToolchain = std::make_unique(preprocessor); fs::path app_dir; @@ -130,17 +136,22 @@ void CxxDocument::Private::configure() { toolchain = std::move(wasmToolchain); } else if (toolchainId == "linux") { - std::string host; + std::string host = "x86_64"; #ifdef __aarch64__ host = "aarch64"; -#elif __x86_64__ - host = "x86_64"; #endif - std::string arch = cli.getSingle("-arch").value_or(host); - toolchain = std::make_unique(preprocesor, arch); + toolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); + } else if (toolchainId == "windows") { - auto windowsToolchain = std::make_unique(preprocesor); + std::string host = "x86_64"; +#ifdef __aarch64__ + host = "aarch64"; +#endif + + auto windowsToolchain = std::make_unique( + preprocessor, cli.getSingle("-arch").value_or(host)); if (auto paths = cli.get("-vctoolsdir"); !paths.empty()) { windowsToolchain->setVctoolsdir(paths.back()); @@ -168,21 +179,21 @@ void CxxDocument::Private::configure() { } for (const auto& path : cli.get("-I")) { - preprocesor->addSystemIncludePath(path); + preprocessor->addSystemIncludePath(path); } for (const auto& macro : cli.get("-D")) { auto sep = macro.find_first_of("="); if (sep == std::string::npos) { - preprocesor->defineMacro(macro, "1"); + preprocessor->defineMacro(macro, "1"); } else { - preprocesor->defineMacro(macro.substr(0, sep), macro.substr(sep + 1)); + preprocessor->defineMacro(macro.substr(0, sep), macro.substr(sep + 1)); } } for (const auto& macro : cli.get("-U")) { - preprocesor->undefMacro(macro); + preprocessor->undefMacro(macro); } } diff --git a/src/parser/cxx/gcc_linux_toolchain.cc b/src/parser/cxx/gcc_linux_toolchain.cc index c7cd9b6a..6c472da3 100644 --- a/src/parser/cxx/gcc_linux_toolchain.cc +++ b/src/parser/cxx/gcc_linux_toolchain.cc @@ -19,6 +19,7 @@ // SOFTWARE. #include +#include #include #include @@ -29,6 +30,14 @@ namespace cxx { GCCLinuxToolchain::GCCLinuxToolchain(Preprocessor* preprocessor, std::string arch) : Toolchain(preprocessor), arch_(std::move(arch)) { + if (arch_ == "aarch64") { + memoryLayout()->setSizeOfLongDouble(8); + } else if (arch_ == "x86_64") { + memoryLayout()->setSizeOfLongDouble(16); + } else { + cxx_runtime_error(std::format("Unsupported architecture: {}", arch_)); + } + for (int version : {15, 14, 13, 12, 11, 10, 9}) { const auto path = fs::path( std::format("/usr/lib/gcc/{}-linux-gnu/{}/include", arch_, version)); diff --git a/src/parser/cxx/macos_toolchain.cc b/src/parser/cxx/macos_toolchain.cc index dfffb184..e1e8d1a9 100644 --- a/src/parser/cxx/macos_toolchain.cc +++ b/src/parser/cxx/macos_toolchain.cc @@ -19,6 +19,7 @@ // SOFTWARE. #include +#include #include #include @@ -26,8 +27,8 @@ namespace cxx { -MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor) - : Toolchain(preprocessor) { +MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor, std::string arch) + : Toolchain(preprocessor), arch_(std::move(arch)) { std::string xcodeContentsBasePath = "/Applications/Xcode.app/Contents"; platformPath_ = std::format( @@ -38,6 +39,14 @@ MacOSToolchain::MacOSToolchain(Preprocessor* preprocessor) toolchainPath_ = std::format("{}/Developer/Toolchains/XcodeDefault.xctoolchain", xcodeContentsBasePath); + + if (arch_ == "aarch64") { + memoryLayout()->setSizeOfLongDouble(8); + } else if (arch_ == "x86_64") { + memoryLayout()->setSizeOfLongDouble(16); + } else { + cxx_runtime_error(std::format("Unsupported architecture: {}", arch_)); + } } void MacOSToolchain::addSystemIncludePaths() { diff --git a/src/parser/cxx/macos_toolchain.h b/src/parser/cxx/macos_toolchain.h index 16e17973..41ac0c75 100644 --- a/src/parser/cxx/macos_toolchain.h +++ b/src/parser/cxx/macos_toolchain.h @@ -28,12 +28,10 @@ namespace cxx { class MacOSToolchain final : public Toolchain { public: - using Toolchain::Toolchain; - - explicit MacOSToolchain(Preprocessor* preprocessor); + explicit MacOSToolchain(Preprocessor* preprocessor, + std::string arch = "aarch64"); [[nodiscard]] auto arch() const -> std::string { return arch_; } - void setArch(std::string arch) { arch_ = std::move(arch); } void addSystemIncludePaths() override; void addSystemCppIncludePaths() override; @@ -42,7 +40,7 @@ class MacOSToolchain final : public Toolchain { private: std::string platformPath_; std::string toolchainPath_; - std::string arch_{"aarch64"}; + std::string arch_; }; } // namespace cxx diff --git a/src/parser/cxx/windows_toolchain.cc b/src/parser/cxx/windows_toolchain.cc index 8f743001..723f8b8a 100644 --- a/src/parser/cxx/windows_toolchain.cc +++ b/src/parser/cxx/windows_toolchain.cc @@ -21,6 +21,7 @@ #include // cxx +#include #include #include @@ -29,6 +30,15 @@ namespace cxx { +WindowsToolchain::WindowsToolchain(Preprocessor *preprocessor, + std::string arch = "x86_64") + : Toolchain(preprocessor), arch_(std::move(arch)) { + memoryLayout()->setSizeOfLong(4); + memoryLayout()->setSizeOfLongLong(8); + memoryLayout()->setSizeOfLongDouble(8); + memoryLayout()->setSizeOfPointer(8); +} + void WindowsToolchain::setVctoolsdir(std::string path) { vctoolsdir_ = std::move(path); } @@ -72,28 +82,6 @@ void WindowsToolchain::addSystemIncludePaths() { void WindowsToolchain::addSystemCppIncludePaths() {} void WindowsToolchain::addPredefinedMacros() { - // clang-format off - defineMacro("__cplusplus", "202101L"); - defineMacro("_WIN32", "1"); - defineMacro("_WIN64", "1"); - defineMacro("_MT", "1"); - defineMacro("_M_AMD64", "100"); - defineMacro("_M_X64", "100"); - defineMacro("_MSC_BUILD", "1"); - defineMacro("_MSC_EXTENSIONS", "1"); - defineMacro("_MSC_FULL_VER", "193000000"); - defineMacro("_MSC_VER", "1930"); - defineMacro("_MSVC_LANG", "201705L"); - defineMacro("_CPPRTTI", "1"); - defineMacro("_CPPUNWIND", "1"); - defineMacro("_WCHAR_T_DEFINED", "1"); - defineMacro("__BOOL_DEFINED", "1"); - - defineMacro("__int8", "char"); - defineMacro("__int16", "short"); - defineMacro("__int32", "int"); - defineMacro("__int64", "long long"); - defineMacro("__pragma(a)", ""); defineMacro("__declspec(a)", ""); defineMacro("__cdecl", ""); @@ -105,7 +93,24 @@ void WindowsToolchain::addPredefinedMacros() { defineMacro("__unaligned", ""); defineMacro("_Pragma(a)", ""); - // clang-format on + addCommonMacros(); + addCommonWindowsMacros(); + + if (language() == LanguageKind::kCXX) { + addCommonCxx26Macros(); + addWindowsCxx26Macros(); + } else { + addCommonC23Macros(); + addWindowsC23Macros(); + } + + if (arch_ == "aarch64") { + addWindowsAArch64Macros(); + } else if (arch_ == "x86_64") { + addWindowsX86_64Macros(); + } else { + cxx_runtime_error(std::format("Unsupported architecture: {}", arch_)); + } } } // namespace cxx diff --git a/src/parser/cxx/windows_toolchain.h b/src/parser/cxx/windows_toolchain.h index 9e2135d9..ea32e22b 100644 --- a/src/parser/cxx/windows_toolchain.h +++ b/src/parser/cxx/windows_toolchain.h @@ -28,7 +28,7 @@ namespace cxx { class WindowsToolchain final : public Toolchain { public: - using Toolchain::Toolchain; + explicit WindowsToolchain(Preprocessor* preprocessor, std::string arch); void setVctoolsdir(std::string path); void setWinsdkdir(std::string path); @@ -40,6 +40,7 @@ class WindowsToolchain final : public Toolchain { void addPredefinedMacros() override; private: + std::string arch_; std::string vctoolsdir_; std::string winsdkdir_; std::string winsdkversion_;