Skip to content

Commit

Permalink
Treat special names inside Cpp as not existing
Browse files Browse the repository at this point in the history
  • Loading branch information
bricknerb committed Mar 3, 2025
1 parent 4cf320d commit 16867c9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
24 changes: 10 additions & 14 deletions toolchain/check/import_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,15 @@ auto ImportCppFiles(Context& context, llvm::StringRef importing_file_path,
// Look ups the given name in the Clang AST. Returns the lookup result if lookup
// was successful.
static auto ClangLookup(Context& context, SemIR::LocId loc_id,
llvm::StringRef name)
SemIR::NameId name_id)
-> std::optional<clang::LookupResult> {
std::optional<llvm::StringRef> name =
context.names().GetAsStringIfIdentifier(name_id);
if (!name) {
// Special names never exist in C++ code.
return std::nullopt;
}

clang::ASTUnit* ast = context.sem_ir().cpp_ast();
CARBON_CHECK(ast);
clang::Sema& sema = ast->getSema();
Expand All @@ -160,7 +167,7 @@ static auto ClangLookup(Context& context, SemIR::LocId loc_id,
sema,
clang::DeclarationNameInfo(
clang::DeclarationName(
sema.getPreprocessor().getIdentifierInfo(name)),
sema.getPreprocessor().getIdentifierInfo(*name)),
clang::SourceLocation()),
clang::Sema::LookupNameKind::LookupOrdinaryName);

Expand Down Expand Up @@ -263,18 +270,7 @@ static auto ImportNameDecl(Context& context, SemIR::LocId loc_id,
auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
SemIR::NameScopeId scope_id, SemIR::NameId name_id)
-> SemIR::InstId {
std::optional<llvm::StringRef> name =
context.names().GetAsStringIfIdentifier(name_id);
if (!name) {
// Special names never exist in C++ code.
CARBON_DIAGNOSTIC(CppNonIdentifierName, Error,
"Using non-identifier name `{0}` in `Cpp`",
SemIR::NameId);
context.emitter().Emit(loc_id, CppNonIdentifierName, name_id);
return SemIR::ErrorInst::SingletonInstId;
}

auto lookup = ClangLookup(context, loc_id, *name);
auto lookup = ClangLookup(context, loc_id, name_id);
if (!lookup) {
return SemIR::InstId::None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ library "[[@TEST_NAME]]";
import Cpp library "function_special_name_decl.h";

fn MyF() {
// CHECK:STDERR: fail_import_function_special_name_decl.carbon:[[@LINE+4]]:3: error: Using non-identifier name `base` in `Cpp` [CppNonIdentifierName]
// CHECK:STDERR: fail_import_function_special_name_decl.carbon:[[@LINE+4]]:3: error: member name `base` not found in `Cpp` [MemberNameNotFoundInScope]
// CHECK:STDERR: Cpp.base();
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR:
Expand Down Expand Up @@ -275,9 +275,7 @@ fn F() {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
// CHECK:STDOUT: .base = <error>
// CHECK:STDOUT: }
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand Down
1 change: 0 additions & 1 deletion toolchain/diagnostics/diagnostic_kind.def
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ CARBON_DIAGNOSTIC_KIND(QualifiedDeclInIncompleteClassScope)
CARBON_DIAGNOSTIC_KIND(QualifiedDeclInUndefinedInterfaceScope)

// Name lookup.
CARBON_DIAGNOSTIC_KIND(CppNonIdentifierName)
CARBON_DIAGNOSTIC_KIND(FromExtendHere)
CARBON_DIAGNOSTIC_KIND(InCppNameLookup)
CARBON_DIAGNOSTIC_KIND(InNameLookup)
Expand Down

0 comments on commit 16867c9

Please sign in to comment.