Skip to content

Commit 77e037d

Browse files
author
Gabor Horvath
committed
[cxx-interop] Fix invalid source range for some template instantiations
Apparently, clang will pick the source range of the canonical declaration for template specializations. This might be in a different buffer than the definition triggering various issues. Fortunately, Clang also has the source locations we want, we just need to use a different API to get them. Unfortunately, I haven't figured it out yet how to trigger a failing test. rdar://148250404
1 parent 8c8ed34 commit 77e037d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/ClangImporter/ImporterImpl.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,8 +1722,15 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
17221722

17231723
if constexpr (std::is_base_of_v<NominalTypeDecl, DeclTy>) {
17241724
// Estimate brace locations.
1725-
auto begin = ClangN.getAsDecl()->getBeginLoc();
1726-
auto end = ClangN.getAsDecl()->getEndLoc();
1725+
clang::SourceLocation begin;
1726+
clang::SourceLocation end;
1727+
if (auto *td = dyn_cast_or_null<clang::TagDecl>(ClangN.getAsDecl())) {
1728+
begin = td->getBraceRange().getBegin();
1729+
end = td->getBraceRange().getEnd();
1730+
} else {
1731+
begin = ClangN.getAsDecl()->getBeginLoc();
1732+
end = ClangN.getAsDecl()->getEndLoc();
1733+
}
17271734
SourceRange range;
17281735
if (begin.isValid() && end.isValid() && D->getNameLoc().isValid())
17291736
range = SourceRange(importSourceLoc(begin), importSourceLoc(end));
@@ -1732,6 +1739,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
17321739
}
17331740
assert(range.isValid() == D->getNameLoc().isValid());
17341741
D->setBraces(range);
1742+
#ifndef NDEBUG
1743+
auto declRange = D->getSourceRange();
1744+
CharSourceRange checkValidRange(SwiftContext.SourceMgr, declRange.Start,
1745+
declRange.End);
1746+
#endif
17351747
}
17361748

17371749
// SwiftAttrs on ParamDecls are interpreted by applyParamAttributes().

0 commit comments

Comments
 (0)