mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Stop using the clang tooling library to build an ASTUnit; that library is set up to process clang frontend arguments, assuming that something has already built frontend arguments from the compiler arguments. It is also too encapsulated and doesn't let us inspect and modify the compiler invocation before it's executed. Instead, build the AST unit directly in two phases: * FIrst, take a list of clang driver arguments and convert them into a list of compiler arguments, using `clang::createInvocation`. Internally, this uses the clang driver to build a frontend invocation, including building system-specific include paths as needed. * Then, directly build an ASTUnit from this compiler invocation. I've factored this so that we can split out the `createInvocation` step, with the intention that we may want to move it out of check and into the carbon driver with the rest of the driver-level argument handling, and we may want to customize some of the clang options before we invoke the clang frontend with that set of options. In order to make the invocation reusable, it no longer depends on the name of the carbon file importing the C++ code. In place of synthesizing a header file name as `<foo.carbon>.generated.cpp_imports.h`, we now insert line marker directives into the generated header so that errors in that header cause Clang to point a diagnostic back at the Carbon source file itself. This results in a minor improvement in the diagnostic output: we no longer refer to a nonexistent generated file. But the snippet still contains text that doesn't match the source code, so it remains imperfect. --------- Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
79 lines
3.0 KiB
C++
79 lines
3.0 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/language_server/language_server.h"
|
|
|
|
#include <memory>
|
|
|
|
#include "clang-tools-extra/clangd/LSPBinder.h"
|
|
#include "clang-tools-extra/clangd/Transport.h"
|
|
#include "clang-tools-extra/clangd/support/Logger.h"
|
|
#include "common/raw_string_ostream.h"
|
|
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
|
#include "toolchain/language_server/context.h"
|
|
#include "toolchain/language_server/incoming_messages.h"
|
|
#include "toolchain/language_server/outgoing_messages.h"
|
|
|
|
namespace Carbon::LanguageServer {
|
|
|
|
// An adapter for clangd's logging that sends most messages to `error_stream`.
|
|
// Verbose logging is only printed if `vlog_stream` is provided, and will be
|
|
// sent there.
|
|
class Logger : public clang::clangd::Logger {
|
|
public:
|
|
explicit Logger(llvm::raw_ostream* error_stream,
|
|
llvm::raw_ostream* vlog_stream)
|
|
: error_logger_(*error_stream, clang::clangd::Logger::Info),
|
|
vlog_logger_(vlog_stream
|
|
? std::make_unique<clang::clangd::StreamLogger>(
|
|
*vlog_stream, clang::clangd::Logger::Verbose)
|
|
: nullptr) {}
|
|
|
|
auto log(Level level, const char* format,
|
|
const llvm::formatv_object_base& message) -> void override {
|
|
if (level != clang::clangd::Logger::Verbose) {
|
|
error_logger_.log(level, format, message);
|
|
} else if (vlog_logger_) {
|
|
vlog_logger_->log(level, format, message);
|
|
}
|
|
}
|
|
|
|
private:
|
|
clang::clangd::StreamLogger error_logger_;
|
|
std::unique_ptr<clang::clangd::StreamLogger> vlog_logger_;
|
|
};
|
|
|
|
auto Run(const InstallPaths& installation, FILE* input_stream,
|
|
llvm::raw_ostream& output_stream, llvm::raw_ostream& error_stream,
|
|
llvm::raw_ostream* vlog_stream, Diagnostics::Consumer& consumer)
|
|
-> bool {
|
|
// The language server internally uses diagnostics for logging issues, but the
|
|
// clangd parts have their own logging system. We intercept that here.
|
|
Logger logger(&error_stream, vlog_stream);
|
|
clang::clangd::LoggingSession logging_session(logger);
|
|
|
|
// Set up the connection.
|
|
std::unique_ptr<clang::clangd::Transport> transport(
|
|
clang::clangd::newJSONTransport(input_stream, output_stream,
|
|
/*InMirror=*/nullptr,
|
|
/*Pretty=*/true));
|
|
OutgoingMessages outgoing(transport.get());
|
|
Context context(&installation, vlog_stream, &consumer, &outgoing);
|
|
IncomingMessages incoming(transport.get(), &context);
|
|
|
|
// Run the server loop.
|
|
llvm::Error err = transport->loop(incoming);
|
|
if (err) {
|
|
RawStringOstream out;
|
|
out << err;
|
|
CARBON_DIAGNOSTIC(LanguageServerTransportError, Error, "{0}", std::string);
|
|
Diagnostics::NoLocEmitter emitter(&consumer);
|
|
emitter.Emit(LanguageServerTransportError, out.TakeStr());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::LanguageServer
|