forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferencesJob.cpp
243 lines (225 loc) · 9.1 KB
/
ReferencesJob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* This file is part of RTags (http://rtags.net).
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#include "ReferencesJob.h"
#include "Project.h"
#include "RTags.h"
#include "Server.h"
static inline Flags<QueryJob::JobFlag> jobFlags(Flags<QueryMessage::Flag> queryFlags)
{
return (queryFlags & QueryMessage::Elisp ? Flags<QueryJob::JobFlag>(QueryJob::QuoteOutput) : Flags<QueryJob::JobFlag>());
}
ReferencesJob::ReferencesJob(const Location &loc, const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &proj)
: QueryJob(query, proj, ::jobFlags(query->flags()))
{
locations.insert(loc);
}
ReferencesJob::ReferencesJob(const String &sym, const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &proj)
: QueryJob(query, proj, ::jobFlags(query->flags())), symbolName(sym)
{
}
int ReferencesJob::execute()
{
const bool rename = queryFlags() & QueryMessage::Rename;
std::shared_ptr<Project> proj = project();
if (!proj)
return 1;
Set<Symbol> refs;
Map<Location, std::pair<bool, CXCursorKind> > references;
if (!symbolName.isEmpty()) {
const bool hasFilter = QueryJob::hasFilter();
auto inserter = [this, hasFilter](Project::SymbolMatchType type, const String &string, const Set<Location> &locs) {
if (type == Project::StartsWith) {
const size_t paren = string.indexOf('(');
if (paren == String::npos || paren != symbolName.size() || RTags::isFunctionVariable(string))
return;
}
for (const auto &l : locs) {
if (!hasFilter || filter(l.path())) {
locations.insert(l);
}
}
};
proj->findSymbols(symbolName, inserter, queryFlags());
}
const bool declarationOnly = queryFlags() & QueryMessage::DeclarationOnly;
const bool definitionOnly = queryFlags() & QueryMessage::DefinitionOnly;
Location startLocation;
bool first = true;
for (auto it = locations.begin(); it != locations.end(); ++it) {
const Location pos = *it;
Symbol sym = proj->findSymbol(pos);
if (sym.isNull())
continue;
if (first && !(queryFlags() & QueryMessage::NoSortReferencesByInput)) {
first = false;
startLocation = sym.location;
}
if (sym.isReference()) {
const Symbol target = proj->findTarget(sym);
if (!target.isNull())
sym = target;
}
if (sym.isNull())
continue;
if (rename && sym.isConstructorOrDestructor()) {
const Location loc = sym.location;
sym.clear();
const Set<String> usrs = proj->findTargetUsrs(loc);
for (const String &usr : usrs) {
for (const Symbol &s : proj->findByUsr(usr, loc.fileId(), Project::ArgDependsOn, loc)) {
if (s.isClass()) {
sym = s;
if (s.isDefinition())
break;
}
}
}
if (sym.isNull())
continue;
}
if (queryFlags() & QueryMessage::AllReferences) {
const Set<Symbol> all = proj->findAllReferences(sym);
for (const auto &symbol : all) {
if (rename) {
if (symbol.kind == CXCursor_MacroExpansion && sym.kind != CXCursor_MacroDefinition)
continue;
if (symbol.flags & Symbol::AutoRef)
continue;
} else if (sym.isClass() && symbol.isConstructorOrDestructor()) {
continue;
}
const bool def = symbol.isDefinition();
if (def) {
if (declarationOnly)
continue;
} else if (definitionOnly) {
continue;
}
references[symbol.location] = std::make_pair(def, symbol.kind);
}
} else if (queryFlags() & QueryMessage::FindVirtuals) {
const Set<Symbol> virtuals = proj->findVirtuals(sym);
for (const auto &symbol : virtuals) {
const bool def = symbol.isDefinition();
if (def) {
if (declarationOnly)
continue;
} else if (definitionOnly) {
continue;
}
references[symbol.location] = std::make_pair(def, symbol.kind);
}
} else {
const Set<Symbol> symbols = proj->findCallers(sym);
for (const auto &symbol : symbols) {
const bool def = symbol.isDefinition();
if (def) {
if (declarationOnly)
continue;
} else if (definitionOnly) {
continue;
}
references[symbol.location] = std::make_pair(false, CXCursor_FirstInvalid);
}
}
}
Flags<QueryJob::WriteFlag> writeFlags;
Flags<Location::ToStringFlag> kf = locationToStringFlags();
if (queryFlags() & QueryMessage::Elisp) {
write("(list ", DontQuote);
writeFlags |= QueryJob::NoContext;
} else if (queryFlags() & QueryMessage::NoContext) {
writeFlags |= QueryJob::NoContext;
}
auto writeCons = [this](const String &car, const String &cdr) {
write("(cons ", DontQuote);
write(car, DontQuote);
write(cdr);
write(")", DontQuote);
};
auto writeLoc = [this, writeCons, writeFlags, kf](const Location &loc) {
if (queryFlags() & QueryMessage::Elisp) {
if (!filterLocation(loc))
return;
write("(list ", DontQuote);
locationToString(loc, [writeCons, this](LocationPiece piece, const String &string) {
switch (piece) {
case Piece_ContainingFunctionLocation:
if (queryFlags() & QueryMessage::ContainingFunctionLocation)
writeCons("'cfl", string);
break;
case Piece_ContainingFunctionName:
if (queryFlags() & QueryMessage::ContainingFunction)
writeCons("'cf", string);
break;
case Piece_Location:
writeCons("'loc", string);
break;
case Piece_Context:
if (!(queryFlags() & QueryMessage::NoContext))
writeCons("'ctx", string);
break;
case Piece_SymbolName:
case Piece_Kind:
break;
}
});
write(")", DontQuote);
} else {
write(loc, writeFlags);
}
};
if (rename) {
if (!references.isEmpty()) {
if (queryFlags() & QueryMessage::ReverseSort) {
Map<Location, std::pair<bool, CXCursorKind> >::const_iterator it = references.end();
do {
--it;
writeLoc(it->first);
} while (it != references.begin());
} else {
for (const auto &it : references) {
writeLoc(it.first);
}
}
}
} else {
List<RTags::SortedSymbol> sorted;
sorted.reserve(references.size());
for (Map<Location, std::pair<bool, CXCursorKind> >::const_iterator it = references.begin();
it != references.end(); ++it) {
sorted.append(RTags::SortedSymbol(it->first, it->second.first, it->second.second));
}
if (queryFlags() & QueryMessage::ReverseSort) {
std::sort(sorted.begin(), sorted.end(), std::greater<RTags::SortedSymbol>());
} else {
std::sort(sorted.begin(), sorted.end());
}
int startIndex = 0;
const int count = sorted.size();
if (!startLocation.isNull()) {
for (int i=0; i<count; ++i) {
if (sorted.at(i).location == startLocation) {
startIndex = i + 1;
break;
}
}
}
for (int i=0; i<count; ++i) {
const Location &loc = sorted.at((startIndex + i) % count).location;
writeLoc(loc);
}
}
if (queryFlags() & QueryMessage::Elisp)
write(")", DontQuote);
return references.isEmpty() ? 1 : 0;
}