forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReferencesJob.cpp
185 lines (173 loc) · 6.8 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
/* 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 "Server.h"
#include "RTags.h"
#include "Project.h"
ReferencesJob::ReferencesJob(const Location &loc, const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &proj)
: QueryJob(query, proj)
{
locations.insert(loc);
}
ReferencesJob::ReferencesJob(const String &sym, const std::shared_ptr<QueryMessage> &query, const std::shared_ptr<Project> &proj)
: QueryJob(query, proj), 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 int paren = string.indexOf('(');
if (paren == -1 || 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())
sym = proj->findTarget(sym);
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)) {
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);
}
}
}
if (rename) {
if (!references.isEmpty()) {
if (queryFlags() & QueryMessage::ReverseSort) {
Map<Location, std::pair<bool, CXCursorKind> >::const_iterator it = references.end();
do {
--it;
write(it->first);
} while (it != references.begin());
} else {
for (const auto &it : references) {
write(it.first);
}
}
return 0;
}
} 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;
write(loc);
}
if (count)
return 0;
}
return 1;
}