Files
carbon-lang/toolchain/driver/driver.h
T
Chandler CarruthandJon Ross-Perkins 5b30888dd8 Fix clang runner to avoid leaking memory (#4972)
Sadly, the Clang driver unconditionally injects the `-disable-free` flag
to CC1 invocations. =/ This ends up with us leaking memory when invoking
Clang programmatically, for example in unit tests.

I've fixed this by post-processing the flags. The alternatives I see all
involve duplicating even more code from within Clang's internals into
our runner, and we already have a lot of that. I have left a TODO to try
and follow up with upstream about fixing this in a more sustainable way,
but wanted to get the testing in place anyways.

I've also threaded a flag through the various layers so that when we're
on the command line we can actually skip this and get the same compile
time benefits that Clang itself gets from disabling freeing all of the
internal data structures.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-02-20 00:25:18 +00:00

48 lines
1.8 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_H_
#define CARBON_TOOLCHAIN_DRIVER_DRIVER_H_
#include "common/command_line.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "toolchain/driver/driver_env.h"
#include "toolchain/driver/driver_subcommand.h"
namespace Carbon {
// Command line interface driver.
//
// Provides simple API to parse and run command lines for Carbon. It is
// generally expected to be used to implement command line tools for working
// with the language.
class Driver {
public:
// Constructs a driver with the provided environment. `input_stream` is
// optional; other parameters are required.
explicit Driver(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
const InstallPaths* installation, FILE* input_stream,
llvm::raw_pwrite_stream* output_stream,
llvm::raw_pwrite_stream* error_stream, bool fuzzing = false,
bool enable_leaking = false)
: driver_env_(std::move(fs), installation, input_stream, output_stream,
error_stream, fuzzing, enable_leaking) {}
// Parses the given arguments into both a subcommand to select the operation
// to perform and any arguments to that subcommand.
//
// Returns true if the operation succeeds. If the operation fails, returns
// false and any information about the failure is printed to the registered
// error stream (stderr by default).
auto RunCommand(llvm::ArrayRef<llvm::StringRef> args) -> DriverResult;
private:
DriverEnv driver_env_;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_DRIVER_DRIVER_H_