Skip to content

[cxx-interop] Import more C++ source locations into Swift #77204

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
Oct 31, 2024
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
8 changes: 6 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5502,8 +5502,12 @@ SourceRange GenericTypeParamDecl::getSourceRange() const {
if (const auto specifierLoc = getSpecifierLoc())
startLoc = specifierLoc;

if (!getInherited().empty())
endLoc = getInherited().getEndLoc();
if (!getInherited().empty()) {
if (getInherited().getEndLoc().isValid())
endLoc = getInherited().getEndLoc();
else
assert(startLoc.isInvalid() || this->hasClangNode());
}

return {startLoc, endLoc};
}
Expand Down
16 changes: 8 additions & 8 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "swift/Basic/Defer.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/SourceLoc.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Basic/Version.h"
#include "swift/ClangImporter/ClangImporterRequests.h"
Expand Down Expand Up @@ -2860,14 +2861,12 @@ ClangImporter::Implementation::exportSourceLoc(SourceLoc loc) {

SourceLoc
ClangImporter::Implementation::importSourceLoc(clang::SourceLocation loc) {
// FIXME: Implement!
return SourceLoc();
return BuffersForDiagnostics.resolveSourceLocation(Instance->getSourceManager(), loc);
}

SourceRange
ClangImporter::Implementation::importSourceRange(clang::SourceRange loc) {
// FIXME: Implement!
return SourceRange();
ClangImporter::Implementation::importSourceRange(clang::SourceRange range) {
return SourceRange(importSourceLoc(range.getBegin()), importSourceLoc(range.getEnd()));
}

#pragma mark Importing names
Expand Down Expand Up @@ -5981,9 +5980,10 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
if (auto typeAlias = dyn_cast<TypeAliasDecl>(decl)) {
auto rawMemory = allocateMemoryForDecl<TypeAliasDecl>(
typeAlias->getASTContext(), sizeof(TypeAliasDecl), false);
auto out = new (rawMemory) TypeAliasDecl(
typeAlias->getLoc(), typeAlias->getEqualLoc(), typeAlias->getName(),
typeAlias->getNameLoc(), typeAlias->getGenericParams(), newContext);
auto out = new (rawMemory)
TypeAliasDecl(typeAlias->getStartLoc(), typeAlias->getEqualLoc(),
typeAlias->getName(), typeAlias->getNameLoc(),
typeAlias->getGenericParams(), newContext);
out->setUnderlyingType(typeAlias->getUnderlyingType());
out->copyFormalAccessFrom(typeAlias);
return out;
Expand Down
15 changes: 10 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/SourceLoc.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/StringExtras.h"
#include "swift/Basic/Version.h"
Expand Down Expand Up @@ -1661,6 +1662,8 @@ namespace {
// Create the wrapper struct.
errorWrapper =
new (C) StructDecl(loc, name, loc, std::nullopt, nullptr, dc);
SourceLoc end = Impl.importSourceLoc(decl->getEndLoc());
errorWrapper->setBraces(SourceRange(loc, end));
errorWrapper->setAccess(AccessLevel::Public);
errorWrapper->getAttrs().add(
new (Impl.SwiftContext) FrozenAttr(/*IsImplicit*/true));
Expand Down Expand Up @@ -3606,9 +3609,9 @@ namespace {
auto *typeParam = Impl.createDeclWithClangNode<GenericTypeParamDecl>(
param, AccessLevel::Public, dc,
Impl.SwiftContext.getIdentifier(param->getName()),
/*nameLoc*/ SourceLoc(), /*specifierLoc*/ SourceLoc(),
/*depth*/ 0, /*index*/ i,
GenericTypeParamKind::Type);
/*nameLoc*/ Impl.importSourceLoc(param->getLocation()),
/*specifierLoc*/ SourceLoc(),
/*depth*/ 0, /*index*/ i, GenericTypeParamKind::Type);
templateParams.push_back(typeParam);
(void)++i;
}
Expand Down Expand Up @@ -6468,7 +6471,8 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer(
}

auto result = Impl.createDeclWithClangNode<ConstructorDecl>(
decl, AccessLevel::Public, name, /*NameLoc=*/SourceLoc(), failable,
decl, AccessLevel::Public, name,
Impl.importSourceLoc(decl->getLocation()), failable,
/*FailabilityLoc=*/SourceLoc(),
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(), /*ThrownType=*/TypeLoc(),
Expand Down Expand Up @@ -6983,7 +6987,8 @@ ConstructorDecl *SwiftDeclConverter::importConstructor(
assert(!importedName.getAsyncInfo());
auto result = Impl.createDeclWithClangNode<ConstructorDecl>(
objcMethod, AccessLevel::Public, importedName.getDeclName(),
/*NameLoc=*/SourceLoc(), failability, /*FailabilityLoc=*/SourceLoc(),
/*NameLoc=*/Impl.importSourceLoc(objcMethod->getLocation()), failability,
/*FailabilityLoc=*/SourceLoc(),
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
/*Throws=*/importedName.getErrorInfo().has_value(),
/*ThrowsLoc=*/SourceLoc(), /*ThrownType=*/TypeLoc(), bodyParams,
Expand Down
10 changes: 9 additions & 1 deletion lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2766,7 +2766,15 @@ ParameterList *ClangImporter::Implementation::importFunctionParameterList(
}

// Form the parameter list.
return ParameterList::create(SwiftContext, parameters);
// Estimate locations for the begin and end of parameter list.
auto begin = clangDecl->getLocation();
auto end = begin;
if (!params.empty()) {
begin = params.front()->getBeginLoc();
end = params.back()->getEndLoc();
}
return ParameterList::create(SwiftContext, importSourceLoc(begin), parameters,
importSourceLoc(end));
}

static bool isObjCMethodResultAudited(const clang::Decl *decl) {
Expand Down
16 changes: 16 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include "ImportName.h"
#include "SwiftLookupTable.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/Module.h"
#include "swift/AST/RequirementSignature.h"
#include "swift/AST/Type.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/SourceLoc.h"
#include "swift/Basic/StringExtras.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/ClangImporter/ClangModule.h"
Expand Down Expand Up @@ -1715,6 +1717,20 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
if (auto ASD = dyn_cast<AbstractStorageDecl>(D))
ASD->setSetterAccess(access);

if constexpr (std::is_base_of_v<NominalTypeDecl, DeclTy>) {
// Estimate brace locations.
auto begin = ClangN.getAsDecl()->getBeginLoc();
auto end = ClangN.getAsDecl()->getEndLoc();
SourceRange range;
if (begin.isValid() && end.isValid() && D->getNameLoc().isValid())
range = SourceRange(importSourceLoc(begin), importSourceLoc(end));
else {
range = SourceRange(D->getNameLoc(), D->getNameLoc());
}
assert(range.isValid() == D->getNameLoc().isValid());
D->setBraces(range);
}

// SwiftAttrs on ParamDecls are interpreted by applyParamAttributes().
if (!isa<ParamDecl>(D))
importSwiftAttrAttributes(D);
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,10 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
// If the declaration has no source location and comes from a Clang module,
// pretty-print the declaration and use that location.
SourceLoc attachedToLoc = attachedTo->getLoc();
bool isPrettyPrintedDecl = false;
if (attachedToLoc.isInvalid() &&
isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
isPrettyPrintedDecl = true;
attachedToLoc = evaluateOrDefault(
ctx.evaluator, PrettyPrintDeclRequest{attachedTo}, SourceLoc());
}
Expand Down Expand Up @@ -1501,7 +1503,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
searchDecl = var->getParentPatternBinding();

auto startLoc = searchDecl->getStartLoc();
if (startLoc.isInvalid() && isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
if (isPrettyPrintedDecl) {
startLoc = attachedToLoc;
}

Expand Down