Skip to content
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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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 @@ -2854,14 +2855,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 @@ -5975,9 +5974,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
7 changes: 5 additions & 2 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 @@ -6491,7 +6492,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 @@ -7006,7 +7008,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 @@ -2788,7 +2788,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 @@ -1719,6 +1721,20 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
if (!isa<ParamDecl>(D))
importSwiftAttrAttributes(D);

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());
}
// Invariant: range.isValid() == D->getNameLoc().isValid()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be an assertion?

D->setBraces(range);
}

return D;
}

Expand Down