diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index b33024d299adb..0fdb66d8184e6 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -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" @@ -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 @@ -5975,9 +5974,10 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) { if (auto typeAlias = dyn_cast(decl)) { auto rawMemory = allocateMemoryForDecl( 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; diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 5e1d349b17a96..7f975faae1800 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -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" @@ -6491,7 +6492,8 @@ Decl *SwiftDeclConverter::importGlobalAsInitializer( } auto result = Impl.createDeclWithClangNode( - 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(), @@ -7006,7 +7008,8 @@ ConstructorDecl *SwiftDeclConverter::importConstructor( assert(!importedName.getAsyncInfo()); auto result = Impl.createDeclWithClangNode( 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, diff --git a/lib/ClangImporter/ImportType.cpp b/lib/ClangImporter/ImportType.cpp index 82311a91db10d..5c8951572d59a 100644 --- a/lib/ClangImporter/ImportType.cpp +++ b/lib/ClangImporter/ImportType.cpp @@ -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) { diff --git a/lib/ClangImporter/ImporterImpl.h b/lib/ClangImporter/ImporterImpl.h index 18939159e273a..83913f85df085 100644 --- a/lib/ClangImporter/ImporterImpl.h +++ b/lib/ClangImporter/ImporterImpl.h @@ -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" @@ -1719,6 +1721,20 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation if (!isa(D)) importSwiftAttrAttributes(D); + if constexpr (std::is_base_of_v) { + // 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() + D->setBraces(range); + } + return D; }