Skip to content

Commit 47d2c24

Browse files
committed
[IDE] Factor code that looks up USRs into getDeclFromUSR
1 parent 24aea20 commit 47d2c24

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

include/swift/IDE/Utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ void getLocationInfoForClangNode(ClangNode ClangNode,
125125

126126
Optional<std::pair<unsigned, unsigned>> parseLineCol(StringRef LineCol);
127127

128+
Decl *getDeclFromUSR(ASTContext &context, StringRef USR, std::string &error);
128129
Decl *getDeclFromMangledSymbolName(ASTContext &context, StringRef mangledName,
129130
std::string &error);
130131

lib/IDE/ReconstructType.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,6 +2375,22 @@ VisitNode(ASTContext *ast, std::vector<Demangle::NodePointer> &nodes,
23752375
nodes.pop_back();
23762376
}
23772377

2378+
Decl *ide::getDeclFromUSR(ASTContext &context, StringRef USR, std::string &error) {
2379+
if (!USR.startswith("s:")) {
2380+
error = "not a Swift USR";
2381+
return nullptr;
2382+
}
2383+
2384+
std::string mangledName(USR);
2385+
// Convert to a symbol name by replacing the USR prefix.
2386+
// This relies on USR generation being very close to symbol mangling; if we
2387+
// need to support entities with customized USRs (e.g. extensions), we will
2388+
// need to do something smarter here.
2389+
mangledName.replace(0, 2, "_T");
2390+
2391+
return getDeclFromMangledSymbolName(context, mangledName, error);
2392+
}
2393+
23782394
Decl *ide::getDeclFromMangledSymbolName(ASTContext &context,
23792395
StringRef mangledName,
23802396
std::string &error) {

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,22 +1051,15 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
10511051
{std::make_pair("USR", USR)});
10521052
}
10531053

1054-
std::string mangledName(USR);
1055-
if (USR.startswith("s:")) {
1056-
mangledName.replace(0, 2, "_T");
1057-
} else if (USR.startswith("c:")) {
1054+
if (USR.startswith("c:")) {
10581055
LOG_WARN_FUNC("lookup for C/C++/ObjC USRs not implemented");
10591056
Receiver({});
10601057
return;
1061-
} else if (!USR.startswith("_T")) {
1062-
LOG_WARN_FUNC("unknown USR prefix");
1063-
Receiver({});
1064-
return;
10651058
}
10661059

10671060
auto &context = CompIns.getASTContext();
10681061
std::string error;
1069-
Decl *D = ide::getDeclFromMangledSymbolName(context, mangledName, error);
1062+
Decl *D = ide::getDeclFromUSR(context, USR, error);
10701063

10711064
if (!D) {
10721065
Receiver({});

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,19 +2423,14 @@ class TypeReconstructWalker : public SourceEntityWalker {
24232423

24242424
private:
24252425
void tryDemangleDecl(ValueDecl *VD, CharSourceRange range) {
2426-
std::string mangledName;
2426+
std::string USR;
24272427
{
2428-
llvm::raw_string_ostream OS(mangledName);
2428+
llvm::raw_string_ostream OS(USR);
24292429
printDeclUSR(VD, OS);
24302430
}
24312431

2432-
// Put the expected symbol _T prefix on the name by replacing the s:.
2433-
assert(StringRef(mangledName).startswith("s:"));
2434-
mangledName[0] = '_';
2435-
mangledName[1] = 'T';
2436-
24372432
std::string error;
2438-
if (Decl *reDecl = getDeclFromMangledSymbolName(Ctx, mangledName, error)) {
2433+
if (Decl *reDecl = getDeclFromUSR(Ctx, USR, error)) {
24392434
Stream << "reconstructed decl from usr for '" << range.str() << "' is '";
24402435
reDecl->print(Stream, PrintOptions());
24412436
Stream << "'\n";

0 commit comments

Comments
 (0)