Skip to content

Commit 315b0ac

Browse files
Refactor identifier lookup in cpp/import.cpp (#6383)
Following up on the [comment](#6326 (comment)) from PR #6326, refactoring the identifier lookup to be only once, instead of both in `LookupMacro` and `ClangLookupName`. Part of #6303
1 parent 4a8efd8 commit 315b0ac

File tree

1 file changed

+40
-49
lines changed

1 file changed

+40
-49
lines changed

toolchain/check/cpp/import.cpp

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -537,32 +537,21 @@ static auto IsDeclInjectedClassName(Context& context,
537537
return true;
538538
}
539539

540-
// Returns a Clang DeclarationName for the given `NameId`.
541-
static auto GetDeclarationName(Context& context, SemIR::NameId name_id)
542-
-> std::optional<clang::DeclarationName> {
543-
std::optional<llvm::StringRef> name =
544-
context.names().GetAsStringIfIdentifier(name_id);
545-
if (!name) {
546-
// Special names never exist in C++ code.
547-
return std::nullopt;
548-
}
549-
550-
return clang::DeclarationName(
551-
context.clang_sema().getPreprocessor().getIdentifierInfo(*name));
552-
}
553-
554-
// Performs a qualified name lookup of the declaration name in the given scope.
540+
// Performs a qualified name lookup of the identifier in the given scope.
555541
// Returns the lookup result if lookup was successful.
556-
static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
557-
clang::DeclarationName name)
542+
static auto ClangLookupName(Context& context, SemIR::NameScopeId scope_id,
543+
clang::IdentifierInfo* identifier_name)
558544
-> std::optional<clang::LookupResult> {
545+
CARBON_CHECK(identifier_name, "Identifier name is empty");
559546
clang::Sema& sema = context.clang_sema();
560547

561548
// TODO: Map the LocId of the lookup to a clang SourceLocation and provide it
562549
// here so that clang's diagnostics can point into the carbon code that uses
563550
// the name.
564551
clang::LookupResult lookup(
565-
sema, clang::DeclarationNameInfo(name, clang::SourceLocation()),
552+
sema,
553+
clang::DeclarationNameInfo(clang::DeclarationName(identifier_name),
554+
clang::SourceLocation()),
566555
clang::Sema::LookupNameKind::LookupOrdinaryName);
567556

568557
bool found =
@@ -575,19 +564,6 @@ static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
575564
return lookup;
576565
}
577566

578-
// Looks up the given name in the Clang AST in a specific scope. Returns the
579-
// lookup result if lookup was successful.
580-
static auto ClangLookupName(Context& context, SemIR::NameScopeId scope_id,
581-
SemIR::NameId name_id)
582-
-> std::optional<clang::LookupResult> {
583-
auto declaration_name = GetDeclarationName(context, name_id);
584-
if (!declaration_name) {
585-
return std::nullopt;
586-
}
587-
588-
return ClangLookup(context, scope_id, *declaration_name);
589-
}
590-
591567
// Returns whether `decl` already mapped to an instruction.
592568
static auto IsClangDeclImported(Context& context, SemIR::ClangDeclKey key)
593569
-> bool {
@@ -2300,25 +2276,19 @@ static auto ImportMacro(Context& context, SemIR::LocId loc_id,
23002276
}
23012277

23022278
// Looks up a macro definition in the top-level `Cpp` scope. Returns nullptr if
2303-
// the macro is not found or the scope is not the top-level `Cpp` scope.
2279+
// the macro is not found or if it is a builtin macro, function-like macro or a
2280+
// macro used for header guards.
2281+
// TODO: Function-like and builtin macros are currently not supported and their
2282+
// support still needs to be clarified.
23042283
static auto LookupMacro(Context& context, SemIR::NameScopeId scope_id,
2305-
SemIR::NameId name_id) -> clang::MacroInfo* {
2306-
auto name_str_opt = context.names().GetAsStringIfIdentifier(name_id);
2307-
if (!name_str_opt || !IsTopCppScope(context, scope_id)) {
2308-
return nullptr;
2309-
}
2310-
2311-
clang::Preprocessor& preprocessor = context.clang_sema().getPreprocessor();
2312-
// TODO: Do the identifier lookup only once, rather than both here and in
2313-
// ClangLookupName.
2314-
clang::IdentifierInfo* identifier_info =
2315-
preprocessor.getIdentifierInfo(*name_str_opt);
2316-
2317-
if (!identifier_info) {
2284+
clang::IdentifierInfo* identifier_info)
2285+
-> clang::MacroInfo* {
2286+
if (!IsTopCppScope(context, scope_id)) {
23182287
return nullptr;
23192288
}
2320-
2321-
clang::MacroInfo* macro_info = preprocessor.getMacroInfo(identifier_info);
2289+
CARBON_CHECK(identifier_info, "Identifier info is empty");
2290+
clang::MacroInfo* macro_info =
2291+
context.clang_sema().getPreprocessor().getMacroInfo(identifier_info);
23222292
if (macro_info && !macro_info->isUsedForHeaderGuard() &&
23232293
!macro_info->isFunctionLike() && !macro_info->isBuiltinMacro()) {
23242294
return macro_info;
@@ -2327,6 +2297,20 @@ static auto LookupMacro(Context& context, SemIR::NameScopeId scope_id,
23272297
return nullptr;
23282298
}
23292299

2300+
// Gets the identifier info for a name. Returns `nullptr` if the name is not an
2301+
// identifier name.
2302+
static auto GetIdentifierInfo(Context& context, SemIR::NameId name_id)
2303+
-> clang::IdentifierInfo* {
2304+
std::optional<llvm::StringRef> string_name =
2305+
context.names().GetAsStringIfIdentifier(name_id);
2306+
if (!string_name) {
2307+
return nullptr;
2308+
}
2309+
clang::IdentifierInfo* identifier_info =
2310+
context.clang_sema().getPreprocessor().getIdentifierInfo(*string_name);
2311+
return identifier_info;
2312+
}
2313+
23302314
auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
23312315
SemIR::NameScopeId scope_id, SemIR::NameId name_id)
23322316
-> SemIR::ScopeLookupResult {
@@ -2339,10 +2323,17 @@ auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
23392323
if (IsIncompleteClass(context, scope_id)) {
23402324
return SemIR::ScopeLookupResult::MakeError();
23412325
}
2342-
if (clang::MacroInfo* macro_info = LookupMacro(context, scope_id, name_id)) {
2326+
2327+
clang::IdentifierInfo* identifier_info = GetIdentifierInfo(context, name_id);
2328+
if (!identifier_info) {
2329+
return SemIR::ScopeLookupResult::MakeNotFound();
2330+
}
2331+
2332+
if (clang::MacroInfo* macro_info =
2333+
LookupMacro(context, scope_id, identifier_info)) {
23432334
return ImportMacro(context, loc_id, scope_id, name_id, macro_info);
23442335
}
2345-
auto lookup = ClangLookupName(context, scope_id, name_id);
2336+
auto lookup = ClangLookupName(context, scope_id, identifier_info);
23462337
if (!lookup) {
23472338
return ImportBuiltinNameIntoScope(context, loc_id, scope_id, name_id);
23482339
}

0 commit comments

Comments
 (0)