Skip to content

Commit 6786158

Browse files
committed
[ClangImporter] Always sort top-level value lookups from Clang.
The order of lookups affects the order of imports, which can sadly end up causing different behavior down the line. The sort is by Clang TU source order, which isn't wonderful but should be consistent. There are generally fewer decls without source locations that come from Clang. Swift SVN r28578
1 parent aadcf86 commit 6786158

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,22 @@ void ClangImporter::lookupValue(Identifier name, VisibleDeclConsumer &consumer){
19291929
bool FoundType = false;
19301930
bool FoundAny = false;
19311931
auto processResults = [&](clang::LookupResult &result) {
1932+
SmallVector<const clang::NamedDecl *, 16> sortedResults{result.begin(),
1933+
result.end()};
1934+
const clang::SourceManager &srcMgr = pp.getSourceManager();
1935+
std::sort(sortedResults.begin(), sortedResults.end(),
1936+
[&](const clang::NamedDecl *lhs,
1937+
const clang::NamedDecl *rhs) -> bool {
1938+
clang::SourceLocation lhsLoc = lhs->getLocStart();
1939+
clang::SourceLocation lhsExpLoc = srcMgr.getExpansionLoc(lhsLoc);
1940+
clang::SourceLocation rhsLoc = rhs->getLocStart();
1941+
clang::SourceLocation rhsExpLoc = srcMgr.getExpansionLoc(rhsLoc);
1942+
if (lhsExpLoc == rhsExpLoc)
1943+
return srcMgr.isBeforeInTranslationUnit(srcMgr.getSpellingLoc(lhsLoc),
1944+
srcMgr.getSpellingLoc(rhsLoc));
1945+
return srcMgr.isBeforeInTranslationUnit(lhsExpLoc, rhsExpLoc);
1946+
});
1947+
19321948
// FIXME: Filter based on access path? C++ access control?
19331949
for (auto decl : result) {
19341950
if (auto swiftDecl = Impl.importDeclReal(decl->getUnderlyingDecl())) {

0 commit comments

Comments
 (0)