Files
carbon-lang/toolchain/check/check.h
T
Richard SmithandChandler Carruth 3776e464e0 Properly set up C++ include paths and similar environment settings when parsing imported C++. (#5767)
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>
2025-07-09 03:09:43 +00:00

84 lines
2.7 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_CHECK_CHECK_H_
#define CARBON_TOOLCHAIN_CHECK_CHECK_H_
#include "common/ostream.h"
#include "toolchain/base/shared_value_stores.h"
#include "toolchain/base/timings.h"
#include "toolchain/check/diagnostic_emitter.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/parse/tree_and_subtrees.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::Check {
// Checking information that's tracked per file. All members are caller-owned.
// Other than `timings`, members must be non-null.
struct Unit {
Diagnostics::Consumer* consumer;
SharedValueStores* value_stores;
// The `timings` may be null if nothing is to be recorded.
Timings* timings;
// The unit's SemIR, provided as empty and filled in by CheckParseTrees.
SemIR::File* sem_ir;
// Storage for the unit's Clang AST. The unique_ptr should start empty, and
// can be assigned as part of checking.
std::unique_ptr<clang::ASTUnit>* cpp_ast;
};
struct CheckParseTreesOptions {
// Options must be set individually, not through initialization.
explicit CheckParseTreesOptions() = default;
// Whether to import the prelude.
bool prelude_import = false;
// If set, enables verbose output.
llvm::raw_ostream* vlog_stream = nullptr;
// Whether fuzzing is being run. Used to disable features we don't want to
// fuzz.
bool fuzzing = false;
// Whether to include each unit in dumps. This is required when dumping
// (either of `dump_stream` or `raw_dump_stream`), and must have entries based
// on CheckIRId.
llvm::ArrayRef<bool> include_in_dumps = {};
// If set, SemIR will be dumped to this.
llvm::raw_ostream* dump_stream = nullptr;
// When dumping textual SemIR (or printing it to for verbose output), whether
// to use ranges.
enum class DumpSemIRRanges : int8_t {
IfPresent,
Only,
Ignore,
};
DumpSemIRRanges dump_sem_ir_ranges = DumpSemIRRanges::IfPresent;
// If set, raw SemIR will be dumped to this.
llvm::raw_ostream* raw_dump_stream = nullptr;
// When dumping raw SemIR, whether to include builtins.
bool dump_raw_sem_ir_builtins = false;
};
// Checks a group of parse trees. This will use imports to decide the order of
// checking.
auto CheckParseTrees(
llvm::MutableArrayRef<Unit> units,
llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::StringRef clang_path, llvm::StringRef target,
const CheckParseTreesOptions& options) -> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CHECK_H_