4040#include < format>
4141
4242#include " ast_printer.h"
43+ #include " lsp_server.h"
4344#include " verify_diagnostics_client.h"
4445
4546// std
@@ -292,124 +293,6 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {
292293 return !diagnosticsClient.hasErrors ();
293294}
294295
295- auto startServer (const CLI& cli) -> bool {
296- Control control;
297- VerifyDiagnosticsClient diagnosticsClient;
298- TranslationUnit unit (&control, &diagnosticsClient);
299-
300- auto preprocesor = unit.preprocessor ();
301-
302- std::unique_ptr<Toolchain> toolchain;
303-
304- if (cli.opt_verify ) {
305- diagnosticsClient.setVerify (true );
306- preprocesor->setCommentHandler (&diagnosticsClient);
307- }
308-
309- auto toolchainId = cli.getSingle (" -toolchain" );
310-
311- if (!toolchainId) {
312- toolchainId = " wasm32" ;
313- }
314-
315- if (toolchainId == " darwin" || toolchainId == " macos" ) {
316- toolchain = std::make_unique<MacOSToolchain>(preprocesor);
317- } else if (toolchainId == " wasm32" ) {
318- auto wasmToolchain = std::make_unique<Wasm32WasiToolchain>(preprocesor);
319-
320- fs::path app_dir;
321-
322- #if __wasi__
323- app_dir = fs::path (" /usr/bin/" );
324- #elif !defined(CXX_NO_FILESYSTEM)
325- app_dir = std::filesystem::canonical (
326- std::filesystem::path (cli.app_name ).remove_filename ());
327- #elif __unix__ || __APPLE__
328- char * app_name = realpath (cli.app_name .c_str (), nullptr );
329- app_dir = fs::path (app_name).remove_filename ().string ();
330- std::free (app_name);
331- #endif
332-
333- wasmToolchain->setAppdir (app_dir.string ());
334-
335- if (auto paths = cli.get (" --sysroot" ); !paths.empty ()) {
336- wasmToolchain->setSysroot (paths.back ());
337- } else {
338- auto sysroot_dir = app_dir / std::string (" ../lib/wasi-sysroot" );
339- wasmToolchain->setSysroot (sysroot_dir.string ());
340- }
341-
342- toolchain = std::move (wasmToolchain);
343- } else if (toolchainId == " linux" ) {
344- std::string host;
345- #ifdef __aarch64__
346- host = " aarch64" ;
347- #elif __x86_64__
348- host = " x86_64" ;
349- #endif
350-
351- std::string arch = cli.getSingle (" -arch" ).value_or (host);
352- toolchain = std::make_unique<GCCLinuxToolchain>(preprocesor, arch);
353- } else if (toolchainId == " windows" ) {
354- auto windowsToolchain = std::make_unique<WindowsToolchain>(preprocesor);
355-
356- if (auto paths = cli.get (" -vctoolsdir" ); !paths.empty ()) {
357- windowsToolchain->setVctoolsdir (paths.back ());
358- }
359-
360- if (auto paths = cli.get (" -winsdkdir" ); !paths.empty ()) {
361- windowsToolchain->setWinsdkdir (paths.back ());
362- }
363-
364- if (auto versions = cli.get (" -winsdkversion" ); !versions.empty ()) {
365- windowsToolchain->setWinsdkversion (versions.back ());
366- }
367-
368- toolchain = std::move (windowsToolchain);
369- }
370-
371- if (toolchain) {
372- control.setMemoryLayout (toolchain->memoryLayout ());
373-
374- if (!cli.opt_nostdinc ) toolchain->addSystemIncludePaths ();
375-
376- if (!cli.opt_nostdincpp ) toolchain->addSystemCppIncludePaths ();
377-
378- toolchain->addPredefinedMacros ();
379- }
380-
381- for (const auto & path : cli.get (" -I" )) {
382- preprocesor->addSystemIncludePath (path);
383- }
384-
385- if (cli.opt_v ) {
386- std::cerr << std::format (" #include <...> search starts here:\n " );
387- const auto & paths = preprocesor->systemIncludePaths ();
388- for (auto it = rbegin (paths); it != rend (paths); ++it) {
389- std::cerr << std::format (" {}\n " , *it);
390- }
391- std::cerr << std::format (" End of search list.\n " );
392- }
393-
394- for (const auto & macro : cli.get (" -D" )) {
395- auto sep = macro.find_first_of (" =" );
396-
397- if (sep == std::string::npos) {
398- preprocesor->defineMacro (macro, " 1" );
399- } else {
400- preprocesor->defineMacro (macro.substr (0 , sep), macro.substr (sep + 1 ));
401- }
402- }
403-
404- for (const auto & macro : cli.get (" -U" )) {
405- preprocesor->undefMacro (macro);
406- }
407-
408- std::cerr << " Starting LSP server" << std::endl;
409-
410- return true ;
411- }
412-
413296} // namespace
414297
415298auto main (int argc, char * argv[]) -> int {
@@ -434,9 +317,7 @@ auto main(int argc, char* argv[]) -> int {
434317 int existStatus = EXIT_SUCCESS;
435318
436319 if (cli.opt_lsp ) {
437- if (!startServer (cli)) {
438- existStatus = EXIT_FAILURE;
439- }
320+ existStatus = lsp::startServer (cli);
440321 } else {
441322 for (const auto & fileName : inputFiles) {
442323 if (!runOnFile (cli, fileName)) {
0 commit comments