-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.cxx
136 lines (110 loc) · 3.75 KB
/
index.cxx
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
#include "libclang++/libclang++.hxx"
#include "getopt++/getopt.hxx"
#include "util/util.hxx"
#include "application.hxx"
#include <cstdlib>
#include <string>
#include <iostream>
#include <fstream>
class Indexer : public LibClang::Visitor<Indexer> {
public:
Indexer (const std::string & fileName,
const std::vector<std::string> & exclude,
Storage & storage,
std::ostream & cout)
: sourceFile_ (fileName),
exclude_ (exclude),
storage_ (storage),
cout_ (cout)
{
needsUpdate_[fileName] = storage.beginFile (fileName);
storage_.addInclude (fileName, fileName);
}
CXChildVisitResult visit (LibClang::Cursor cursor,
LibClang::Cursor parent)
{
const LibClang::Cursor cursorDef (cursor.referenced());
// Skip non-reference cursors
if (cursorDef.isNull()) {
return CXChildVisit_Recurse;
}
const std::string usr = cursorDef.USR();
if (usr == "") {
return CXChildVisit_Recurse;
}
const LibClang::SourceLocation::Position begin = cursor.location().expansionLocation();
const String fileName = begin.file;
if (fileName == "") {
return CXChildVisit_Continue;
}
{ // Skip excluded paths
auto it = exclude_.begin();
auto end = exclude_.end();
for ( ; it != end ; ++it) {
if (fileName.startsWith (*it)) {
return CXChildVisit_Continue;
}
}
}
if (needsUpdate_.count(fileName) == 0) {
cout_ << " " << fileName << std::endl;
needsUpdate_[fileName] = storage_.beginFile (fileName);
storage_.addInclude (fileName, sourceFile_);
}
if (needsUpdate_[fileName]) {
const LibClang::SourceLocation::Position end = cursor.end().expansionLocation();
storage_.addTag (usr, cursor.kindStr(), cursor.spelling(), fileName,
begin.line, begin.column, begin.offset,
end.line, end.column, end.offset,
cursor.isDeclaration());
}
return CXChildVisit_Recurse;
}
private:
const std::string & sourceFile_;
const std::vector<std::string> & exclude_;
Storage & storage_;
std::map<std::string, bool> needsUpdate_;
std::ostream & cout_;
};
void Application::index (IndexArgs & args, std::ostream & cout) {
cout << std::endl
<< "-- Indexing project" << std::endl;
storage_.setOption ("exclude", args.exclude);
storage_.cleanIndex();
updateIndex_ (args, cout);
}
void Application::update (IndexArgs & args, std::ostream & cout) {
cout << std::endl
<< "-- Updating index" << std::endl;
args.exclude = storage_.getOption ("exclude", Storage::Vector());
updateIndex_ (args, cout);
}
void Application::updateIndex_ (IndexArgs & args, std::ostream & cout) {
Timer totalTimer;
{
auto transaction(storage_.beginTransaction());
std::string fileName;
while ((fileName = storage_.nextFile()) != "") {
cout << fileName << ":" << std::endl
<< " parsing..." << std::flush;
Timer timer;
LibClang::TranslationUnit tu = translationUnit_(fileName);
cout << "\t" << timer.get() << "s." << std::endl;
timer.reset();
// Print clang diagnostics if requested
if (args.diagnostics) {
for (unsigned int N = tu.numDiagnostics(),
i = 0 ; i < N ; ++i) {
cout << tu.diagnostic (i) << std::endl << std::endl;
}
}
cout << " indexing..." << std::endl;
LibClang::Cursor top (tu);
Indexer indexer (fileName, args.exclude, storage_, cout);
indexer.visitChildren (top);
cout << " indexing...\t" << timer.get() << "s." << std::endl;
}
}
cout << totalTimer.get() << "s." << std::endl;
}