Files
carbon-lang/toolchain/sem_ir/read_only_ast_source.h
T
Richard Smith e476a2bd3b Fix exporting of names to C++ after a lookup failure. (#7933)
If Clang encounters a lookup failure, it caches that result, creating
problems with `-share-cpp-ast` where a lookup failure in one file
effectively poisons the same name when looked up in a different file.

Also, if Clang triggers loading a complete name table (which happens,
for example, during typo correction, but Clang could do that any time it
wants to), it clears out the "has external visible results" flag, which
means that introducing new names in Carbon and then attempting to use
them from C++ after that point does not work.

Fix both of these problems by re-enabling the "has external visible
results" flag on callbacks that indicate Clang just cleared it, and by
not caching failed name lookups into Carbon scopes in Clang's lookup
table. (They still get cached in *our* lookup table.)

Assisted-by: Gemini via Antigravity
2026-10-13 18:43:50 +00:00

53 lines
1.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_SEM_IR_READ_ONLY_AST_SOURCE_H_
#define CARBON_TOOLCHAIN_SEM_IR_READ_ONLY_AST_SOURCE_H_
#include <optional>
#include "clang/Sema/ExternalSemaSource.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::SemIR {
class ReadOnlyASTSource : public clang::ExternalSemaSource {
public:
explicit ReadOnlyASTSource(const File& sem_ir) : sem_ir_(sem_ir) {}
auto completeVisibleDeclsMap(const clang::DeclContext* decl_context)
-> void override;
auto layoutRecordType(
const clang::RecordDecl* record_decl, uint64_t& size, uint64_t& alignment,
llvm::DenseMap<const clang::FieldDecl*, uint64_t>& field_offsets,
llvm::DenseMap<const clang::CXXRecordDecl*, clang::CharUnits>&
base_offsets,
llvm::DenseMap<const clang::CXXRecordDecl*, clang::CharUnits>&
vbase_offsets) -> bool override;
auto isA(const void* class_id) const -> bool override {
return class_id == &id || ExternalSemaSource::isA(class_id);
}
static auto classof(const ExternalASTSource* s) -> bool {
return s->isA(&id);
}
protected:
// If `decl_context` was created by exporting a Carbon declaration to C++,
// returns its `ClangDecl`. Otherwise returns `std::nullopt`.
auto GetCarbonOwnedDecl(const clang::DeclContext* decl_context) const
-> std::optional<ClangDecl>;
private:
// For LLVM RTTI.
static char id;
const File& sem_ir_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_READ_ONLY_AST_SOURCE_H_