Skip to content

[Clang importer] Map imported names via the user-facing name #70414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,7 @@ class DarwinLegacyFilterDeclConsumer : public swift::VisibleDeclConsumer {
if (clangModule->Name == "MacTypes") {
if (!VD->hasName() || VD->getBaseName().isSpecial())
return true;
return llvm::StringSwitch<bool>(VD->getBaseIdentifier().str())
return llvm::StringSwitch<bool>(VD->getBaseName().userFacingName())
.Cases("OSErr", "OSStatus", "OptionBits", false)
.Cases("FourCharCode", "OSType", false)
.Case("Boolean", false)
Expand Down Expand Up @@ -4981,7 +4981,7 @@ const clang::CXXMethodDecl *getCalledBaseCxxMethod(FuncDecl *baseMember) {
if (auto *v = ce->getCalledValue()) {
if (v->getModuleContext() ==
baseMember->getASTContext().TheBuiltinModule &&
v->getBaseIdentifier().is("reinterpretCast")) {
v->getBaseName().userFacingName() == "reinterpretCast") {
returnExpr = ce->getArgs()->get(0).getExpr();
}
}
Expand Down Expand Up @@ -6914,7 +6914,7 @@ bool ClangImporter::isUnsafeCXXMethod(const FuncDecl *func) {
return false;
if (!func->hasName())
return false;
auto id = func->getBaseIdentifier().str();
auto id = func->getBaseName().userFacingName();
return id.startswith("__") && id.endswith("Unsafe");
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/DWARFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void ClangImporter::Implementation::lookupValueDWARF(
return;

SmallVector<clang::Decl *, 4> decls;
DWARFImporter->lookupValue(name.getBaseIdentifier().str(), llvm::None,
DWARFImporter->lookupValue(name.getBaseName().userFacingName(), llvm::None,
inModule.str(), decls);
for (auto *clangDecl : decls) {
auto *namedDecl = dyn_cast<clang::NamedDecl>(clangDecl);
Expand Down
50 changes: 24 additions & 26 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ applyPropertyOwnership(VarDecl *prop,
static bool isPrintLikeMethod(DeclName name, const DeclContext *dc) {
if (!name || name.isSpecial() || name.isSimpleName())
return false;
if (name.getBaseIdentifier().str() != "print")
if (name.getBaseName().userFacingName() != "print")
return false;
if (!dc->isTypeContext())
return false;
Expand Down Expand Up @@ -1188,7 +1188,7 @@ namespace {

auto *enumDecl = Impl.createDeclWithClangNode<EnumDecl>(
decl, AccessLevel::Public, Impl.importSourceLoc(decl->getBeginLoc()),
importedName.getDeclName().getBaseIdentifier(),
importedName.getBaseIdentifier(Impl.SwiftContext),
Impl.importSourceLoc(decl->getLocation()), llvm::None, nullptr, dc);
// TODO: we only have this for the sid effect of calling
// "FirstDeclAndLazyMembers.setInt(true)".
Expand Down Expand Up @@ -1222,7 +1222,7 @@ namespace {
ImportedName importedName;
llvm::Optional<ImportedName> correctSwiftName;
std::tie(importedName, correctSwiftName) = importFullName(decl);
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -1365,7 +1365,7 @@ namespace {
ImportedName importedName;
llvm::Optional<ImportedName> correctSwiftName;
std::tie(importedName, correctSwiftName) = importFullName(Decl);
auto Name = importedName.getDeclName().getBaseIdentifier();
auto Name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (Name.empty())
return nullptr;

Expand Down Expand Up @@ -1599,7 +1599,7 @@ namespace {
if (!dc)
return nullptr;

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);

// Create the enum declaration and record it.
ImportDiagnosticAdder addDiag(Impl, decl, decl->getLocation());
Expand Down Expand Up @@ -1959,7 +1959,7 @@ namespace {
if (unimported != constant && enumeratorDecl) {
ImportedName importedName =
Impl.importFullName(constant, getActiveSwiftVersion());
Identifier name = importedName.getDeclName().getBaseIdentifier();
Identifier name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty()) {
// Clear the existing declaration so we don't try to process it
// twice later.
Expand Down Expand Up @@ -2167,7 +2167,7 @@ namespace {
}

// Create the struct declaration and record it.
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
NominalTypeDecl *result = nullptr;
// Try to find an already-imported struct. This case happens any time
// there are nested structs. The "Parent" struct will import the "Child"
Expand Down Expand Up @@ -3016,7 +3016,7 @@ namespace {
std::tie(importedName, correctSwiftName) = importFullName(decl);
if (!importedName) return nullptr;

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -3087,7 +3087,7 @@ namespace {
std::tie(importedName, correctSwiftName) = importFullName(decl);
if (!importedName) return nullptr;

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);

auto dc =
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
Expand Down Expand Up @@ -3570,8 +3570,7 @@ namespace {

Identifier bodyName =
Impl.importFullName(param, Impl.CurrentVersion)
.getDeclName()
.getBaseIdentifier();
.getBaseIdentifier(Impl.SwiftContext);
auto paramInfo = Impl.createDeclWithClangNode<ParamDecl>(
param, AccessLevel::Private, SourceLoc(), SourceLoc(),
Identifier(), Impl.importSourceLoc(param->getLocation()),
Expand Down Expand Up @@ -3809,7 +3808,7 @@ namespace {
return nullptr;
}

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);

auto dc =
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
Expand Down Expand Up @@ -3897,7 +3896,7 @@ namespace {
std::tie(importedName, correctSwiftName) = importFullName(decl);
if (!importedName) return nullptr;

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
auto dc =
Impl.importDeclContextOf(decl, importedName.getEffectiveContext());
if (!dc)
Expand Down Expand Up @@ -3976,7 +3975,7 @@ namespace {
Decl *VisitClassTemplateDecl(const clang::ClassTemplateDecl *decl) {
ImportedName importedName;
std::tie(importedName, std::ignore) = importFullName(decl);
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -4042,7 +4041,7 @@ namespace {
// Don't import something that doesn't have a name.
if (importedName.getDeclName().isSpecial())
return nullptr;
auto Name = importedName.getDeclName().getBaseIdentifier();
auto Name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (Name.empty())
return nullptr;

Expand Down Expand Up @@ -4291,7 +4290,7 @@ namespace {

auto type = importedType.getType();
const auto access = getOverridableAccessLevel(dc);
auto ident = name.getDeclName().getBaseIdentifier();
auto ident = name.getBaseIdentifier(Impl.SwiftContext);
auto propDecl = Impl.createDeclWithClangNode<VarDecl>(decl, access,
/*IsStatic*/decl->isClassMethod(), VarDecl::Introducer::Var,
Impl.importSourceLoc(decl->getLocation()), ident, dc);
Expand Down Expand Up @@ -4987,7 +4986,7 @@ namespace {
return importCompatibilityTypeAlias(decl, importedName,
*correctSwiftName);

Identifier name = importedName.getDeclName().getBaseIdentifier();
Identifier name = importedName.getBaseIdentifier(Impl.SwiftContext);
bool hasKnownSwiftName = importedName.hasCustomName();

if (!decl->hasDefinition()) {
Expand Down Expand Up @@ -5150,7 +5149,7 @@ namespace {
return importCompatibilityTypeAlias(decl, importedName,
*correctSwiftName);

auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
bool hasKnownSwiftName = importedName.hasCustomName();

if (!decl->hasDefinition()) {
Expand Down Expand Up @@ -5358,7 +5357,7 @@ namespace {
ImportedName importedName;
llvm::Optional<ImportedName> correctSwiftName;
std::tie(importedName, correctSwiftName) = importFullName(decl);
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -5502,7 +5501,7 @@ namespace {

ImportedName importedName;
std::tie(importedName, std::ignore) = importFullName(decl);
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);

if (name.empty()) return nullptr;
Decl *importedDecl =
Expand Down Expand Up @@ -5742,7 +5741,7 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
// Create the type alias.
auto alias = Impl.createDeclWithClangNode<TypeAliasDecl>(
decl, AccessLevel::Public, Impl.importSourceLoc(decl->getBeginLoc()),
SourceLoc(), compatibilityName.getDeclName().getBaseIdentifier(),
SourceLoc(), compatibilityName.getBaseIdentifier(Impl.SwiftContext),
Impl.importSourceLoc(decl->getLocation()), /*generic params*/nullptr, dc);

auto *GTD = dyn_cast<GenericTypeDecl>(typeDecl);
Expand Down Expand Up @@ -5979,7 +5978,7 @@ Decl *SwiftDeclConverter::importEnumCase(const clang::EnumConstantDecl *decl,
ImportedName importedName;
llvm::Optional<ImportedName> correctSwiftName;
std::tie(importedName, correctSwiftName) = importFullName(decl);
auto name = importedName.getDeclName().getBaseIdentifier();
auto name = importedName.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -6036,7 +6035,7 @@ SwiftDeclConverter::importOptionConstant(const clang::EnumConstantDecl *decl,
ImportedName nameInfo;
llvm::Optional<ImportedName> correctSwiftName;
std::tie(nameInfo, correctSwiftName) = importFullName(decl);
Identifier name = nameInfo.getDeclName().getBaseIdentifier();
Identifier name = nameInfo.getBaseIdentifier(Impl.SwiftContext);
if (name.empty())
return nullptr;

Expand Down Expand Up @@ -6260,7 +6259,7 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
}

// Find the other accessor, if it exists.
auto propertyName = importedName.getDeclName().getBaseIdentifier();
auto propertyName = importedName.getBaseIdentifier(Impl.SwiftContext);
auto lookupTable =
Impl.findLookupTable(*getClangSubmoduleForDecl(accessor));
assert(lookupTable && "No lookup table?");
Expand Down Expand Up @@ -9448,8 +9447,7 @@ ClangImporter::Implementation::getSpecialTypedefKind(
Identifier
ClangImporter::getEnumConstantName(const clang::EnumConstantDecl *enumConstant){
return Impl.importFullName(enumConstant, Impl.CurrentVersion)
.getDeclName()
.getBaseIdentifier();
.getBaseIdentifier(Impl.SwiftContext);
}

// See swift/Basic/Statistic.h for declaration: this enables tracing
Expand Down
8 changes: 8 additions & 0 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,6 +2393,14 @@ bool ClangImporter::shouldIgnoreMacro(StringRef Name,
return ::shouldIgnoreMacro(Name, Macro, Impl.getClangPreprocessor());
}

Identifier ImportedName::getBaseIdentifier(ASTContext &ctx) const {
auto baseName = declName.getBaseName();
if (!baseName.isSpecial())
return baseName.getIdentifier();

return ctx.getIdentifier(baseName.userFacingName());
}

Identifier
NameImporter::importMacroName(const clang::IdentifierInfo *clangIdentifier,
const clang::MacroInfo *macro) {
Expand Down
4 changes: 4 additions & 0 deletions lib/ClangImporter/ImportName.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ class ImportedName {
return llvm::None;
}

/// Retrieve the base name as an identifier, including mapping special
/// names like 'init' or 'subscript' to identifiers.
Identifier getBaseIdentifier(ASTContext &ctx) const;

/// Whether this name was explicitly specified via a Clang
/// swift_name attribute.
bool hasCustomName() const { return info.hasCustomName; }
Expand Down
5 changes: 2 additions & 3 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2468,8 +2468,7 @@ static ParamDecl *getParameterInfo(ClangImporter::Implementation *impl,
const bool isParamTypeImplicitlyUnwrapped) {
// Figure out the name for this parameter.
Identifier bodyName = impl->importFullName(param, impl->CurrentVersion)
.getDeclName()
.getBaseIdentifier();
.getBaseIdentifier(impl->SwiftContext);

// It doesn't actually matter which DeclContext we use, so just use the
// imported header unit.
Expand Down Expand Up @@ -3284,7 +3283,7 @@ ImportedType ClangImporter::Implementation::importAccessorParamsAndReturnType(
} else {
const clang::ParmVarDecl *param = clangDecl->parameters().front();
ImportedName fullBodyName = importFullName(param, CurrentVersion);
Identifier bodyName = fullBodyName.getDeclName().getBaseIdentifier();
Identifier bodyName = fullBodyName.getBaseIdentifier(SwiftContext);
SourceLoc nameLoc = importSourceLoc(param->getLocation());
Identifier argLabel = functionName.getDeclName().getArgumentNames().front();
auto paramInfo
Expand Down
8 changes: 8 additions & 0 deletions test/ClangImporter/enum-renames.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck %s -verify

import enums_using_attributes

func testEvent(event: Event) {
if event == .`init` { print("Initialize") }
if event == .reset { print("Reset") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ Tags:
EnumKind: none
- Name: UnknownOptionsThanksToAPINotes
EnumKind: none
- Name: SystemEvent
SwiftName: Event
EnumExtensibility: open
Enumerators:
- Name: kEventInit
SwiftName: init
- Name: kEventReset
SwiftName: reset
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ typedef enum {
typedef enum EnumByBoth {
EnumByBothX
} EnumByBoth;


typedef enum {
kEventInit,
kEventReset
} SystemEvent;