Files
carbon-lang/toolchain/driver/driver_subcommand.h
T
Richard Smith 6181259cf1 Language server: prelude support. (#7417)
Support multi-file compilation, and in particular imports of files from
the prelude, in `carbon language_server`.

In order to properly interface with `CompileDriver`, also switch over to
building a proper VFS from the documents we're given.

Assisted-by: Gemini via Antigravity
2026-06-26 20:16:28 +00:00

70 lines
2.5 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
#ifndef CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_
#define CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_
#include "common/command_line.h"
#include "common/ostream.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "toolchain/base/install_paths.h"
#include "toolchain/driver/driver_env.h"
namespace Carbon {
// A subcommand for the driver.
class DriverSubcommand {
public:
explicit DriverSubcommand(CommandLine::CommandInfo info) : info_(info) {}
// Adds the subcommand to the main command, assigning `selected_command` when
// the subcommand is in use.
auto AddTo(CommandLine::CommandBuilder& b,
DriverSubcommand** selected_subcommand) -> void {
b.AddSubcommand(
info_, [this, selected_subcommand](CommandLine::CommandBuilder& sub_b) {
BuildOptionsAndSetAction(sub_b, selected_subcommand);
});
}
// Adds command line options.
virtual auto BuildOptions(CommandLine::CommandBuilder& b) -> void = 0;
// Adds command line options and registers the `Run` method to be called.
//
// For more complex subcommands that need to control the action this method
// can be overridden and bypass the simple API above.
//
// TODO: This isn't the most elegant way to manage this. There is probably a
// better factoring / organization of the driver subcommand infrastructure
// that bakes in the needed flexibility here, but that was deferred for a
// future refactoring.
virtual auto BuildOptionsAndSetAction(CommandLine::CommandBuilder& b,
DriverSubcommand** selected_subcommand)
-> void {
BuildOptions(b);
b.Do([this, selected_subcommand] { *selected_subcommand = this; });
}
// Runs the command.
virtual auto Run(DriverEnv& driver_env) -> DriverResult = 0;
protected:
// Tests if fuzzing and if so diagnose and returns true.
//
// This should be used in subcommands to diagnose and exit early rather than
// entering them during fuzzing when they use external libraries that we can't
// keep fuzz-clean.
auto TestAndDiagnoseIfFuzzingExternalLibraries(DriverEnv& driver_env,
llvm::StringRef name) -> bool;
private:
// Subcommand information.
CommandLine::CommandInfo info_;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_SUBCOMMAND_H_