Skip to content

Commit b8f4d48

Browse files
committed
---
yaml --- r: 316927 b: refs/heads/master-rebranch c: 3a1059d h: refs/heads/master i: 316925: 57a5b7e 316923: 1699134 316919: e0b1ca6 316911: 32e6822 316895: 8a1b9d8 316863: 1947024 316799: 546104e 316671: 652af4d 316415: 262328f
1 parent dae18d6 commit b8f4d48

20 files changed

+190
-22
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1457,4 +1457,4 @@ refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-02-a: ddd2b2976aa9bfde5f20fe37f6bd2
14571457
refs/tags/swift-DEVELOPMENT-SNAPSHOT-2019-08-03-a: 171cc166f2abeb5ca2a4003700a8a78a108bd300
14581458
refs/heads/benlangmuir-patch-1: baaebaf39d52f3bf36710d4fe40cf212e996b212
14591459
refs/heads/i-do-redeclare: 8c4e6d5de5c1e3f0a2cedccf319df713ea22c48e
1460-
refs/heads/master-rebranch: b08454960122fbf4d3b942c61b8fdde7000764ce
1460+
refs/heads/master-rebranch: 3a1059ddd4c72aa530dbef97df342a6dd1e55d55

branches/master-rebranch/include/swift/AST/Module.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
264264
return { Files.begin(), Files.size() };
265265
}
266266

267+
bool isClangModule() const;
267268
void addFile(FileUnit &newFile);
268269
void removeFile(FileUnit &existingFile);
269270

@@ -1288,6 +1289,12 @@ class ModuleEntity {
12881289
bool isSystemModule() const;
12891290
bool isBuiltinModule() const;
12901291
const ModuleDecl *getAsSwiftModule() const;
1292+
const clang::Module *getAsClangModule() const;
1293+
1294+
void *getOpaqueValue() const {
1295+
assert(!Mod.isNull());
1296+
return Mod.getOpaqueValue();
1297+
}
12911298

12921299
explicit operator bool() const { return !Mod.isNull(); }
12931300
};

branches/master-rebranch/include/swift/AST/USRGeneration.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace swift {
1919
class AbstractStorageDecl;
2020
class ValueDecl;
2121
class ExtensionDecl;
22+
class ModuleEntity;
2223
enum class AccessorKind;
2324
class Type;
2425

@@ -36,6 +37,10 @@ bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
3637
/// \returns true if it failed, false on success.
3738
bool printDeclUSR(const ValueDecl *D, raw_ostream &OS);
3839

40+
/// Prints out the USR for the given ModuleEntity.
41+
/// \returns true if it failed, false on success.
42+
bool printModuleUSR(ModuleEntity Mod, raw_ostream &OS);
43+
3944
/// Prints out the accessor USR for the given storage Decl.
4045
/// \returns true if it failed, false on success.
4146
bool printAccessorUSR(const AbstractStorageDecl *D, AccessorKind AccKind,

branches/master-rebranch/include/swift/Index/IndexSymbol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace swift {
2121
class Decl;
2222
class ValueDecl;
23+
class ModuleEntity;
2324
enum class AccessorKind;
2425

2526
namespace index {
@@ -77,6 +78,7 @@ struct IndexSymbol : IndexRelation {
7778
}
7879
};
7980

81+
SymbolInfo getSymbolInfoForModule(ModuleEntity Mod);
8082
SymbolInfo getSymbolInfoForDecl(const Decl *D);
8183
SymbolSubKind getSubKindForAccessor(AccessorKind AK);
8284
bool isLocalSymbol(const Decl *D);

branches/master-rebranch/lib/AST/Module.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx)
365365
setAccess(AccessLevel::Public);
366366
}
367367

368+
bool ModuleDecl::isClangModule() const {
369+
return findUnderlyingClangModule() != nullptr;
370+
}
371+
368372
void ModuleDecl::addFile(FileUnit &newFile) {
369373
// Require Main and REPL files to be the first file added.
370374
assert(Files.empty() ||
@@ -1636,6 +1640,13 @@ const ModuleDecl* ModuleEntity::getAsSwiftModule() const {
16361640
return nullptr;
16371641
}
16381642

1643+
const clang::Module* ModuleEntity::getAsClangModule() const {
1644+
assert(!Mod.isNull());
1645+
if (Mod.is<const ModuleDecl*>())
1646+
return nullptr;
1647+
return getClangModule(Mod);
1648+
}
1649+
16391650
// See swift/Basic/Statistic.h for declaration: this enables tracing SourceFiles, is
16401651
// defined here to avoid too much layering violation / circular linkage
16411652
// dependency.

branches/master-rebranch/lib/AST/USRGeneration.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ swift::USRGenerationRequest::evaluate(Evaluator &evaluator, const ValueDecl* D)
249249
return NewMangler.mangleDeclAsUSR(D, getUSRSpacePrefix());
250250
}
251251

252+
bool ide::printModuleUSR(ModuleEntity Mod, raw_ostream &OS) {
253+
if (auto *D = Mod.getAsSwiftModule()) {
254+
StringRef moduleName = D->getName().str();
255+
return clang::index::generateFullUSRForTopLevelModuleName(moduleName, OS);
256+
} else if (auto ClangM = Mod.getAsClangModule()) {
257+
return clang::index::generateFullUSRForModule(ClangM, OS);
258+
} else {
259+
return true;
260+
}
261+
}
262+
252263
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
253264
auto result = evaluateOrDefault(D->getASTContext().evaluator,
254265
USRGenerationRequest { D },

branches/master-rebranch/lib/Index/Index.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
159159
StringRef name;
160160
};
161161
typedef llvm::PointerIntPair<Decl *, 3> DeclAccessorPair;
162-
llvm::DenseMap<Decl *, NameAndUSR> nameAndUSRCache;
162+
llvm::DenseMap<void *, NameAndUSR> nameAndUSRCache;
163163
llvm::DenseMap<DeclAccessorPair, NameAndUSR> accessorNameAndUSRCache;
164164
StringScratchSpace stringStorage;
165165

@@ -193,6 +193,28 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
193193
return false;
194194
}
195195

196+
bool getModuleNameAndUSR(ModuleEntity Mod, StringRef &name, StringRef &USR) {
197+
auto &result = nameAndUSRCache[Mod.getOpaqueValue()];
198+
if (result.USR.empty()) {
199+
SmallString<128> storage;
200+
{
201+
llvm::raw_svector_ostream OS(storage);
202+
if (ide::printModuleUSR(Mod, OS))
203+
return true;
204+
result.USR = stringStorage.copyString(OS.str());
205+
}
206+
storage.clear();
207+
{
208+
llvm::raw_svector_ostream OS(storage);
209+
OS << Mod.getFullName();
210+
result.name = stringStorage.copyString(OS.str());
211+
}
212+
}
213+
name = result.name;
214+
USR = result.USR;
215+
return false;
216+
}
217+
196218
bool getPseudoAccessorNameAndUSR(AbstractStorageDecl *D, AccessorKind AK, StringRef &Name, StringRef &USR) {
197219
assert(static_cast<int>(AK) < 0x111 && "AccessorKind too big for pair");
198220
DeclAccessorPair key(D, static_cast<int>(AK));
@@ -377,6 +399,30 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
377399
return true;
378400
}
379401

402+
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override {
403+
SourceLoc Loc = Range.getStart();
404+
405+
if (Loc.isInvalid())
406+
return true;
407+
408+
IndexSymbol Info;
409+
std::tie(Info.line, Info.column) = getLineCol(Loc);
410+
Info.roles |= (unsigned)SymbolRole::Reference;
411+
Info.symInfo = getSymbolInfoForModule(Mod);
412+
getModuleNameAndUSR(Mod, Info.name, Info.USR);
413+
414+
auto Parent = getParentDecl();
415+
if (Parent && isa<AbstractFunctionDecl>(Parent))
416+
addRelation(Info, (unsigned)SymbolRole::RelationContainedBy, Parent);
417+
418+
if (!IdxConsumer.startSourceEntity(Info)) {
419+
Cancelled = true;
420+
return true;
421+
}
422+
423+
return finishSourceEntity(Info.symInfo, Info.roles);
424+
}
425+
380426
Decl *getParentDecl() const {
381427
if (!EntitiesStack.empty())
382428
return EntitiesStack.back().D;
@@ -441,7 +487,11 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
441487
bool finishCurrentEntity() {
442488
Entity CurrEnt = EntitiesStack.pop_back_val();
443489
assert(CurrEnt.SymInfo.Kind != SymbolKind::Unknown);
444-
if (!IdxConsumer.finishSourceEntity(CurrEnt.SymInfo, CurrEnt.Roles)) {
490+
return finishSourceEntity(CurrEnt.SymInfo, CurrEnt.Roles);
491+
}
492+
493+
bool finishSourceEntity(SymbolInfo symInfo, SymbolRoleSet roles) {
494+
if (!IdxConsumer.finishSourceEntity(symInfo, roles)) {
445495
Cancelled = true;
446496
return false;
447497
}

branches/master-rebranch/lib/Index/IndexSymbol.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "swift/Index/IndexSymbol.h"
1414
#include "swift/AST/ASTContext.h"
1515
#include "swift/AST/Decl.h"
16+
#include "swift/AST/Module.h"
1617
#include "swift/AST/ParameterList.h"
1718
#include "swift/AST/Types.h"
1819

@@ -134,6 +135,25 @@ static SymbolKind getVarSymbolKind(const VarDecl *VD) {
134135
return SymbolKind::Variable;
135136
}
136137

138+
SymbolInfo index::getSymbolInfoForModule(ModuleEntity Mod) {
139+
SymbolInfo info;
140+
info.Kind = SymbolKind::Module;
141+
info.SubKind = SymbolSubKind::None;
142+
info.Properties = SymbolPropertySet();
143+
if (auto *D = Mod.getAsSwiftModule()) {
144+
if (!D->isClangModule()) {
145+
info.Lang = SymbolLanguage::Swift;
146+
} else {
147+
info.Lang = SymbolLanguage::C;
148+
}
149+
} else if (Mod.getAsClangModule()) {
150+
info.Lang = SymbolLanguage::C;
151+
} else {
152+
llvm_unreachable("unexpected module kind");
153+
}
154+
return info;
155+
}
156+
137157
SymbolInfo index::getSymbolInfoForDecl(const Decl *D) {
138158
SymbolInfo info{ SymbolKind::Unknown, SymbolSubKind::None,
139159
SymbolLanguage::Swift, SymbolPropertySet() };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
@import ClangModuleA;
1+
#import "ClangModuleA.h"
22
void funcB(void);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public struct MyType {}
2+
public struct MyGeneric<T, U> {
3+
public init() {}
4+
}

branches/master-rebranch/test/Index/Store/unit-pcm-dependency.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// REQUIRES: objc_interop
2-
31
// RUN: rm -rf %t
42
// RUN: %target-swift-frontend -index-store-path %t/idx -primary-file %s -o %t/s1.o -I %S/Inputs -typecheck -module-cache-path %t/mcp
53
// RUN: c-index-test core -print-unit %t/idx | %FileCheck %s -check-prefix=FILE1
@@ -74,6 +72,7 @@ func test() {
7472
// FILE2-NOT: Unit |{{.*}}ClangModuleB
7573
// FILE2-NOT: Record
7674
// FILE2: Unit | user | ClangModuleA | {{.*}}ClangModuleA-{{[A-Z0-9]*}}.pcm | ClangModuleA-{{[A-Z0-9]*}}.pcm-
75+
// FILE2: Record | user | {{.*}}s2.swift | s2.swift-
7776
// FILE2-NOT: Unit |{{.*}}ClangModuleB
7877
// FILE2-NOT: Record
7978
// FILE2: DEPEND END
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -enable-source-import -source-filename %s -I %S/Store/Inputs | %FileCheck %s
2+
3+
import NonExistingModuleName // Make sure invalid imports aren't affecting results
4+
// CHECK-NOT: {{.*}} | NonExistingModuleName
5+
6+
import ClangModuleB
7+
// CHECK: [[@LINE-1]]:8 | module/C | ClangModuleB | c:@M@ClangModuleB | Ref | rel: 0
8+
import ClangModuleC.Sub1
9+
// CHECK: [[@LINE-1]]:8 | module/C | ClangModuleC | [[ClangModuleC_USR:c:@M@ClangModuleC]] | Ref | rel: 0
10+
// CHECK: [[@LINE-2]]:21 | module/C | ClangModuleC.Sub1 | [[ClangModuleC_USR]]@M@Sub1 | Ref | rel: 0
11+
import ClangModuleC.Sub2
12+
// CHECK: [[@LINE-1]]:8 | module/C | ClangModuleC | [[ClangModuleC_USR]] | Ref | rel: 0
13+
// CHECK: [[@LINE-2]]:21 | module/C | ClangModuleC.Sub2 | [[ClangModuleC_USR]]@M@Sub2 | Ref | rel: 0
14+
import func ClangModuleA.funcA
15+
// CHECK: [[@LINE-1]]:13 | module/C | ClangModuleA | c:@M@ClangModuleA | Ref | rel: 0
16+
import SwiftModuleC
17+
// CHECK: [[@LINE-1]]:8 | module/Swift | SwiftModuleC | [[SwiftModuleC_USR:c:@M@SwiftModuleC]] | Ref | rel: 0
18+
19+
struct MyType {}
20+
21+
extension SwiftModuleC.MyType {
22+
// CHECK: [[@LINE-1]]:11 | module/Swift | SwiftModuleC | [[SwiftModuleC_USR]] | Ref | rel: 0
23+
func myMethod() {
24+
_ = SwiftModuleC.MyGeneric<SwiftModuleC.MyType, MyType>()
25+
// CHECK: [[@LINE-1]]:9 | module/Swift | SwiftModuleC | [[SwiftModuleC_USR]] | Ref,RelCont | rel: 1
26+
// CHECK-NEXT: RelCont | instance-method/Swift | myMethod() | [[myMethod_USR:.*]]
27+
// CHECK: [[@LINE-3]]:22 | struct/Swift | MyGeneric | {{.*}} | Ref
28+
// CHECK: [[@LINE-4]]:32 | module/Swift | SwiftModuleC | [[SwiftModuleC_USR]] | Ref,RelCont | rel: 1
29+
// CHECK-NEXT: RelCont | instance-method/Swift | myMethod() | [[myMethod_USR]]
30+
// CHECK: [[@LINE-6]]:45 | struct/Swift | MyType | {{.*}} | Ref
31+
}
32+
}

branches/master-rebranch/test/SourceKit/Indexing/index_bad_modulename.swift.response

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
}
4848
],
4949
key.entities: [
50+
{
51+
key.kind: source.lang.swift.ref.module,
52+
key.name: "ObjectiveC",
53+
key.usr: "c:@M@ObjectiveC",
54+
key.line: 8,
55+
key.column: 8
56+
},
5057
{
5158
key.kind: source.lang.swift.decl.var.global,
5259
key.name: "v",

branches/master-rebranch/test/SourceKit/Indexing/index_constructors.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: objc_interop
2+
13
// RUN: %sourcekitd-test -req=index %s -- %s %S/Inputs/index_constructors_other.swift -module-name index_constructors | %sed_clean > %t.response
24
// RUN: diff -u %s.response %t.response
35

branches/master-rebranch/test/SourceKit/Indexing/index_constructors.swift.response

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@
1010
}
1111
],
1212
key.entities: [
13+
{
14+
key.kind: source.lang.swift.ref.module,
15+
key.name: "Foundation",
16+
key.usr: "c:@M@Foundation",
17+
key.line: 6,
18+
key.column: 8
19+
},
1320
{
1421
key.kind: source.lang.swift.decl.class,
1522
key.name: "HorseObject",
1623
key.usr: "s:18index_constructors11HorseObjectC",
17-
key.line: 6,
24+
key.line: 8,
1825
key.column: 7,
1926
key.related: [
2027
{
2128
key.kind: source.lang.swift.ref.class,
2229
key.name: "DogObject",
2330
key.usr: "s:18index_constructors9DogObjectC",
24-
key.line: 6,
31+
key.line: 8,
2532
key.column: 21
2633
}
2734
],
@@ -30,27 +37,27 @@
3037
key.kind: source.lang.swift.ref.class,
3138
key.name: "DogObject",
3239
key.usr: "s:18index_constructors9DogObjectC",
33-
key.line: 6,
40+
key.line: 8,
3441
key.column: 21
3542
},
3643
{
3744
key.kind: source.lang.swift.decl.var.instance,
3845
key.name: "name",
3946
key.usr: "s:18index_constructors11HorseObjectC4nameXevp",
40-
key.line: 7,
47+
key.line: 9,
4148
key.column: 7,
4249
key.entities: [
4350
{
4451
key.kind: source.lang.swift.decl.function.accessor.getter,
4552
key.usr: "s:18index_constructors11HorseObjectC4nameXevg",
46-
key.line: 7,
53+
key.line: 9,
4754
key.column: 7,
4855
key.is_dynamic: 1
4956
},
5057
{
5158
key.kind: source.lang.swift.decl.function.accessor.setter,
5259
key.usr: "s:18index_constructors11HorseObjectC4nameXevs",
53-
key.line: 7,
60+
key.line: 9,
5461
key.column: 7,
5562
key.is_dynamic: 1
5663
}
@@ -60,7 +67,7 @@
6067
key.kind: source.lang.swift.decl.function.method.instance,
6168
key.name: "flip()",
6269
key.usr: "s:18index_constructors11HorseObjectC4flipyyF",
63-
key.line: 9,
70+
key.line: 11,
6471
key.column: 21,
6572
key.is_dynamic: 1,
6673
key.attributes: [

branches/master-rebranch/test/SourceKit/Indexing/index_func_import.swift.response

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
}
4141
],
4242
key.entities: [
43+
{
44+
key.kind: source.lang.swift.ref.module,
45+
key.name: "test_module",
46+
key.usr: "c:@M@test_module",
47+
key.line: 7,
48+
key.column: 13
49+
},
4350
{
4451
key.kind: source.lang.swift.ref.function.free,
4552
key.name: "globalFunc()",

branches/master-rebranch/test/SourceKit/Indexing/index_with_clang_module.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ func foo(a: FooClassDerived) {
1818
// CHECK-NEXT: key.filepath: "{{.*[/\\]}}Foo{{.*}}.pcm"
1919
// CHECK-NOT: key.hash:
2020

21+
// CHECK: key.kind: source.lang.swift.ref.module
22+
// CHECK-NEXT: key.name: "Foo"
23+
// CHECK-NEXT: key.usr: "c:@M@Foo"
24+
2125
// CHECK: key.kind: source.lang.swift.ref.class
2226
// CHECK-NEXT: key.name: "FooClassDerived"
2327
// CHECK-NEXT: key.usr: "c:objc(cs)FooClassDerived"

0 commit comments

Comments
 (0)