Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions toolchain/check/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cc_library(
"global_init.cpp",
"impl_lookup.cpp",
"import.cpp",
"import_cpp.cpp",
"import_ref.cpp",
"inst_block_stack.cpp",
"literal.cpp",
Expand All @@ -52,6 +53,7 @@ cc_library(
"global_init.h",
"impl_lookup.h",
"import.h",
"import_cpp.h",
"import_ref.h",
"inst_block_stack.h",
"keyword_modifier_set.h",
Expand Down Expand Up @@ -88,6 +90,8 @@ cc_library(
"//toolchain/sem_ir:formatter",
"//toolchain/sem_ir:inst",
"//toolchain/sem_ir:typed_insts",
"@llvm-project//clang:frontend",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:Support",
],
)
Expand Down
20 changes: 15 additions & 5 deletions toolchain/check/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,20 @@ static auto TrackImport(Map<ImportKey, UnitAndImports*>& api_map,

IdentifierId file_package_id =
packaging ? packaging->names.package_id : IdentifierId::Invalid;
auto import_key = GetImportKey(unit_info, file_package_id, import);
const auto import_key = GetImportKey(unit_info, file_package_id, import);
const auto& [import_package_name, import_library_name] = import_key;

// True if the import has `Main` as the package name, even if it comes from
// the file's packaging (diagnostics may differentiate).
bool is_explicit_main = import_key.first == MainPackageName;
bool is_explicit_main = import_package_name == MainPackageName;

bool is_cpp =
import_package_name == CppPackageName && !import_library_name.empty();

if (is_cpp) {
unit_info.cpp_imports.push_back(import);
return;
}

// Explicit imports need more validation than implicit ones. We try to do
// these in an order of imports that should be removed, followed by imports
Expand Down Expand Up @@ -185,7 +194,7 @@ static auto TrackImport(Map<ImportKey, UnitAndImports*>& api_map,
} else {
// The imported api is missing.
package_imports.has_load_error = true;
if (!explicit_import_map && import_key.first == CppPackageName) {
if (!explicit_import_map && import_package_name == CppPackageName) {
// Don't diagnose the implicit import in `impl package Cpp`, because we'll
// have diagnosed the use of `Cpp` in the declaration.
return;
Expand Down Expand Up @@ -295,6 +304,7 @@ static auto BuildApiMapAndDiagnosePackaging(
}

auto CheckParseTrees(llvm::MutableArrayRef<Unit> units, bool prelude_import,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::raw_ostream* vlog_stream) -> void {
// UnitAndImports is big due to its SmallVectors, so we default to 0 on the
// stack.
Expand Down Expand Up @@ -350,7 +360,7 @@ auto CheckParseTrees(llvm::MutableArrayRef<Unit> units, bool prelude_import,
for (int check_index = 0;
check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
auto* unit_info = ready_to_check[check_index];
CheckUnit(unit_info, units.size(), vlog_stream).Run();
CheckUnit(unit_info, units.size(), fs, vlog_stream).Run();
for (auto* incoming_import : unit_info->incoming_imports) {
--incoming_import->imports_remaining;
if (incoming_import->imports_remaining == 0) {
Expand Down Expand Up @@ -397,7 +407,7 @@ auto CheckParseTrees(llvm::MutableArrayRef<Unit> units, bool prelude_import,
// incomplete imports.
for (auto& unit_info : unit_infos) {
if (unit_info.imports_remaining > 0) {
CheckUnit(&unit_info, units.size(), vlog_stream).Run();
CheckUnit(&unit_info, units.size(), fs, vlog_stream).Run();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions toolchain/check/check.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct Unit {
// Checks a group of parse trees. This will use imports to decide the order of
// checking.
auto CheckParseTrees(llvm::MutableArrayRef<Unit> units, bool prelude_import,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::raw_ostream* vlog_stream) -> void;

} // namespace Carbon::Check
Expand Down
40 changes: 40 additions & 0 deletions toolchain/check/check_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

#include "toolchain/check/check_unit.h"

#include <string>

#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/base/pretty_stack_trace_function.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/impl.h"
#include "toolchain/check/import.h"
#include "toolchain/check/import_cpp.h"
#include "toolchain/check/import_ref.h"
#include "toolchain/check/node_id_traversal.h"

Expand All @@ -29,9 +35,11 @@ static auto GetImportedIRCount(UnitAndImports* unit_and_imports) -> int {
}

CheckUnit::CheckUnit(UnitAndImports* unit_and_imports, int total_ir_count,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::raw_ostream* vlog_stream)
: unit_and_imports_(unit_and_imports),
total_ir_count_(total_ir_count),
fs_(std::move(fs)),
vlog_stream_(vlog_stream),
emitter_(*unit_and_imports_->unit->sem_ir_converter,
unit_and_imports_->err_tracker),
Expand Down Expand Up @@ -130,6 +138,7 @@ auto CheckUnit::InitPackageScopeAndImports() -> void {
ImportCurrentPackage(package_inst_id, namespace_type_id);
CARBON_CHECK(context_.scope_stack().PeekIndex() == ScopeIndex::Package);
ImportOtherPackages(namespace_type_id);
ImportCppPackages();
}

auto CheckUnit::CollectDirectImports(
Expand Down Expand Up @@ -325,6 +334,37 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
}
}

auto CheckUnit::ImportCppPackages() -> void {
for (const auto& import : unit_and_imports_->cpp_imports) {
llvm::StringRef cpp_file_path =
unit_and_imports_->unit->value_stores->string_literal_values().Get(
import.library_id);

auto file = fs_->openFileForRead(cpp_file_path);
if (!file) {
CARBON_DIAGNOSTIC(CppInteropFileNotFound, Error,
"file '{0}' couldn't be opened for reading: {1}",
std::string, std::string);
emitter_.Emit(import.node_id, CppInteropFileNotFound, cpp_file_path.str(),
file.getError().message());
continue;
}

llvm::vfs::File& file_ref = *file.get();
auto buffer = file_ref.getBuffer(cpp_file_path);
if (!buffer) {
CARBON_DIAGNOSTIC(CppInteropFailedAccessingFileBuffer, Error,
"failed accessing buffer of file '{0}': {1}",
std::string, std::string);
emitter_.Emit(import.node_id, CppInteropFailedAccessingFileBuffer,
cpp_file_path.str(), buffer.getError().message());
continue;
}
ImportCppFile(context_, import.node_id, cpp_file_path,
buffer.get()->getBuffer());
}
}

// Loops over all nodes in the tree. On some errors, this may return early,
// for example if an unrecoverable state is encountered.
// NOLINTNEXTLINE(readability-function-size)
Expand Down
10 changes: 9 additions & 1 deletion toolchain/check/check_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ struct UnitAndImports {
// A map of the package names to the outgoing imports above.
Map<IdentifierId, int32_t> package_imports_map;

// List of the `import Cpp` imports.
llvm::SmallVector<Parse::Tree::PackagingNames> cpp_imports;

// The remaining number of imports which must be checked before this unit can
// be processed.
int32_t imports_remaining = 0;
Expand All @@ -98,6 +101,7 @@ struct UnitAndImports {
class CheckUnit {
public:
explicit CheckUnit(UnitAndImports* unit_and_imports, int total_ir_count,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::raw_ostream* vlog_stream);

// Produces and checks the IR for the provided unit.
Expand Down Expand Up @@ -125,9 +129,12 @@ class CheckUnit {
auto ImportCurrentPackage(SemIR::InstId package_inst_id,
SemIR::TypeId namespace_type_id) -> void;

// Imports all other packages (excluding the current package).
// Imports all other Carbon packages (excluding the current package).
auto ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void;

// Imports all C++ packages.
auto ImportCppPackages() -> void;

// Checks that each required definition is available. If the definition can be
// generated by resolving a specific, does so, otherwise emits a diagnostic
// for each declaration in context.definitions_required() that doesn't have a
Expand All @@ -142,6 +149,7 @@ class CheckUnit {
UnitAndImports* unit_and_imports_;
// The number of IRs being checked in total.
int total_ir_count_;
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
llvm::raw_ostream* vlog_stream_;

Context::DiagnosticEmitter emitter_;
Expand Down
57 changes: 57 additions & 0 deletions toolchain/check/import_cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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/check/import_cpp.h"

#include <memory>
#include <string>

#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "toolchain/check/context.h"
#include "toolchain/check/diagnostic_helpers.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/diagnostics/format_providers.h"

namespace Carbon::Check {

auto ImportCppFile(Context& context, SemIRLoc loc, llvm::StringRef file_path,
llvm::StringRef code) -> void {
std::string diagnostics_str;
llvm::raw_string_ostream diagnostics_stream(diagnostics_str);

llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options(
new clang::DiagnosticOptions());
clang::TextDiagnosticPrinter diagnostics_consumer(diagnostics_stream,
diagnostic_options.get());
// TODO: Share compilation flags with ClangRunner.
auto ast = clang::tooling::buildASTFromCodeWithArgs(
code, {}, file_path, "clang-tool",
std::make_shared<clang::PCHContainerOperations>(),
clang::tooling::getClangStripDependencyFileAdjuster(),
clang::tooling::FileContentMappings(), &diagnostics_consumer);
// TODO: Implement and use a DynamicRecursiveASTVisitor to traverse the AST.
int num_errors = diagnostics_consumer.getNumErrors();
int num_warnings = diagnostics_consumer.getNumWarnings();
if (num_errors > 0) {
// TODO: Remove the warnings part when there are no warnings.
CARBON_DIAGNOSTIC(
CppInteropCodeErrors, Error,
"{0} error{0:s} and {1} warning{1:s} in C++ file '{2}':\n{3}",
IntAsSelect, IntAsSelect, std::string, std::string);
context.emitter().Emit(loc, CppInteropCodeErrors, num_errors, num_warnings,
file_path.str(), diagnostics_str);
} else if (num_warnings > 0) {
CARBON_DIAGNOSTIC(CppInteropCodeWarnings, Warning,
"{0} warning{0:s} in C++ file '{1}':\n{2}", IntAsSelect,
std::string, std::string);
context.emitter().Emit(loc, CppInteropCodeWarnings, num_warnings,
file_path.str(), diagnostics_str);
}
}

} // namespace Carbon::Check
20 changes: 20 additions & 0 deletions toolchain/check/import_cpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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_IMPORT_CPP_H_
#define CARBON_TOOLCHAIN_CHECK_IMPORT_CPP_H_

#include "llvm/ADT/StringRef.h"
#include "toolchain/check/context.h"
#include "toolchain/check/diagnostic_helpers.h"

namespace Carbon::Check {

// Parses the C++ code and report errors and warnings.
auto ImportCppFile(Context& context, SemIRLoc loc, llvm::StringRef file_path,
llvm::StringRef code) -> void;

} // namespace Carbon::Check

#endif // CARBON_TOOLCHAIN_CHECK_IMPORT_CPP_H_
Loading
Loading