-
Notifications
You must be signed in to change notification settings - Fork 1
/
clang-tags.cpp
375 lines (310 loc) · 11.2 KB
/
clang-tags.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <iostream>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Host.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/FileSystemOptions.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/DirectoryLookup.h"
#include "clang/Basic/FileManager.h"
#include "clang/Frontend/HeaderSearchOptions.h"
#include "clang/Frontend/Utils.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/PreprocessorOptions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Builtins.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/Type.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Ownership.h"
#include "clang/AST/DeclGroup.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
//std-c lib
#include <list>
class ETagsWriter {
public:
ETagsWriter() : m_FD(-1) { }
virtual ~ETagsWriter() {
if (m_FD != -1) {
close(m_FD);
m_FD = -1;
}
}
virtual int openFile(const char* filename) {
m_FD = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 644);
return m_FD;
}
virtual void closeFile() {
close(m_FD);
m_FD = -1;
}
virtual void startSection(const char* sname) {
strcpy(sourceName, sname);
}
virtual void closeSection() {
if (m_tagDefinitions.size() == 0) {
return;
}
// Write the section start
char c[2] = { 0x0c, 0x0a };
int result = doWrite(m_FD, c, 2);
result = doWrite(m_FD, sourceName, strlen(sourceName));
result = doWrite(m_FD, ",", 1);
int totalSize = 0;
char buf[32];
for (std::list<char*>::iterator it = m_tagDefinitions.begin(); it != m_tagDefinitions.end(); it++) {
// std::cout << "Symbol: '" << (*it) << "' is " << std::strlen(*it) << " bytes long" << std::endl;
totalSize += std::strlen(*it);
}
// Now that I have the total size, write it out to the head
sprintf(buf, "%d\n", totalSize);
// std::cout << "Total size is " << totalSize << std::endl;
result = doWrite(m_FD, buf, std::strlen(buf));
for (std::list<char*>::iterator it = m_tagDefinitions.begin(); it != m_tagDefinitions.end(); it++) {
result = doWrite(m_FD, (*it), std::strlen(*it));
// FIXME: Crasher
// free(*it);
}
m_tagDefinitions.clear();
}
virtual void addTag(const char* tagDefinition, const char* tagName, unsigned int lineNumber, unsigned int byteOffset) {
char* buf = (char* )malloc(2048);
sprintf(buf, "%s%c%s%c%d,%d\n", tagDefinition, 0x7f, tagName, 0x01, lineNumber, byteOffset);
// std::cout << "Creating tag: " << buf << std::endl;
m_tagDefinitions.push_back(buf);
}
protected:
int doWrite(int FD, const void* buf, int totalBytes) {
int result = write(FD, buf, totalBytes);
if (result <= 0) {
std::cout << "Error " << result << " writing to file; ETAGS file probably won't be readable" << std::endl;
}
return result;
}
private:
char sourceName[1024];
mutable int m_FD;
std::list<char *> m_tagDefinitions;
};
class TagsASTConsumer : public clang::ASTConsumer {
public:
TagsASTConsumer(clang::SourceManager *sourceManager, ETagsWriter *writer)
: clang::ASTConsumer(),
_sourceManager(sourceManager),
_writer(writer) {
_lastBufferName[0] = '\0';
}
virtual ~TagsASTConsumer() { }
virtual void HandleTopLevelDecl( clang::DeclGroupRef d) {
clang::DeclGroupRef::iterator it;
for( it = d.begin(); it != d.end(); it++) {
char *bufferName;
char name[1024];
unsigned lineNumber = 0;
unsigned fileOffset = 0;
int found = 0;
if (dyn_cast<clang::ObjCInterfaceDecl>(*it) || dyn_cast<clang::ObjCProtocolDecl>(*it)) {
found = 1;
}
if (found) {
clang::NamedDecl *decl = dyn_cast<clang::NamedDecl>(*it);
bufferName = (char *)_sourceManager->getBufferName(decl->getLocation());
strcpy(name, decl->getNameAsString().c_str());
lineNumber = _sourceManager->getInstantiationLineNumber(decl->getLocation());
fileOffset = _sourceManager->getFileOffset(decl->getLocation());
}
if (std::strcmp(_lastBufferName, bufferName) != 0 && found) {
// was there a previous buffer?
if (std::strlen(_lastBufferName) > 0) {
// std::cout << "Close section " << std::endl;
_writer->closeSection();
}
_writer->startSection(bufferName);
// std::cout << "New section --------------------------------------------------" <<std::endl;
std::strcpy(_lastBufferName, bufferName);
}
if (found) {
_writer->addTag("", name, lineNumber, fileOffset);
// std::cout << "Name: "
// << name
// << " LineNum: "
// << lineNumber
// << " Filename: "
// << bufferName
// << " File offset "
// << fileOffset
// << " Tag Definition "
// << std::endl;
}
}
}
private:
clang::SourceManager *_sourceManager;
ETagsWriter *_writer;
char _lastBufferName[1024];
};
void ParseFile(char *path, ETagsWriter *writer) {
// I don't know which of these objects can be reused
// when parsing a new file, so for now just tear
// it all down and setup again. Maybe someone who knows
// more about Clang can clean this up...
clang::DiagnosticOptions diagnosticOptions;
clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
new clang::TextDiagnosticPrinter(
llvm::outs(),
diagnosticOptions);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
clang::Diagnostic diagnostic(pDiagIDs, pTextDiagnosticPrinter);
clang::LangOptions languageOptions;
languageOptions.ObjC1 = 1;
languageOptions.ObjC2 = 1;
languageOptions.C99 = 1;
languageOptions.CPlusPlus = 1;
clang::FileSystemOptions fileSystemOptions;
clang::FileManager fileManager(fileSystemOptions);
clang::SourceManager sourceManager(
diagnostic,
fileManager);
clang::HeaderSearch headerSearch(fileManager);
// We don't want to include any system files
clang::HeaderSearchOptions headerSearchOptions;
headerSearchOptions.UseBuiltinIncludes = 0;
headerSearchOptions.UseStandardIncludes = 0;
headerSearchOptions.UseStandardCXXIncludes = 0;
clang::TargetOptions targetOptions;
targetOptions.Triple = llvm::sys::getHostTriple();
clang::TargetInfo *pTargetInfo =
clang::TargetInfo::CreateTargetInfo(
diagnostic,
targetOptions);
clang::ApplyHeaderSearchOptions(
headerSearch,
headerSearchOptions,
languageOptions,
pTargetInfo->getTriple());
clang::Preprocessor preprocessor(
diagnostic,
languageOptions,
*pTargetInfo,
sourceManager,
headerSearch);
clang::PreprocessorOptions preprocessorOptions;
clang::FrontendOptions frontendOptions;
clang::InitializePreprocessor(
preprocessor,
preprocessorOptions,
headerSearchOptions,
frontendOptions);
// We don't want Clang to load files via #import or #include
// Otherwise we will visit the same files multiple times as they are imported
// and we may visit files outside the directories we are crawling
headerSearch.SetSearchPaths(std::vector<clang::DirectoryLookup>(), 0, 0, true);
const clang::TargetInfo &targetInfo = *pTargetInfo;
clang::IdentifierTable identifierTable(languageOptions);
clang::SelectorTable selectorTable;
clang::Builtin::Context builtinContext(targetInfo);
clang::ASTContext astContext(
languageOptions,
sourceManager,
targetInfo,
identifierTable,
selectorTable,
builtinContext,
0 /* size_reserve*/);
TagsASTConsumer astConsumer(&sourceManager, writer);
clang::Sema sema(
preprocessor,
astContext,
astConsumer);
sema.Initialize();
const clang::FileEntry *pFile = fileManager.getFile(path);
sourceManager.createMainFileID(pFile);
pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
clang::ParseAST(preprocessor, &astConsumer, astContext);
pTextDiagnosticPrinter->EndSourceFile();
writer->closeSection();
}
bool CompareExtension(char *filename, char *extension) {
if (filename == NULL || extension == NULL) {
return false;
}
if (strlen(filename) == 0 || strlen(extension) == 0) {
return false;
}
if (strchr(filename, '.') == NULL || strchr(extension, '.') == NULL) {
return false;
}
/* Iterate backwards through respective strings and compare each char one at a time */
for (int i = 0; i < strlen(filename); i++) {
if (tolower(filename[strlen(filename) - i - 1]) == tolower(extension[strlen(extension) - i - 1])) {
if (i == strlen(extension) - 1) {
return true;
}
} else {
break;
}
}
return false;
}
void CrawlDirectory(char *dirpath, ETagsWriter *writer) {
struct dirent **namelist;
int n = scandir(dirpath, &namelist, 0, alphasort);
if (n < 0) {
printf("scandir failed on %s\n", dirpath);
return;
}
while(n--) {
char *name = namelist[n]->d_name;
// Skip over entries . and ..
if (std::strcmp(".", name) != 0 && std::strcmp("..", name) != 0) {
char path[1024];
sprintf(path, "%s/%s", dirpath, name);
struct stat statbuf;
if (stat(path, &statbuf)) {
printf("stat failed on %s\n", path);
free(namelist[n]);
continue;
}
// Is it a directory?
if (S_ISDIR(statbuf.st_mode)) {
// If so, recurse!
CrawlDirectory(path, writer);
} else {
// It is a file!
if (CompareExtension(name, ".h") ||
CompareExtension(name, ".m") ||
CompareExtension(name, ".mm") ||
CompareExtension(name, ".cpp") ||
CompareExtension(name, ".c")) {
ParseFile(path, writer);
}
}
}
free(namelist[n]);
}
free(namelist);
}
int main() {
ETagsWriter writer;
writer.openFile("TAGS");
//ParseFile("test.m", &writer);
CrawlDirectory(".", &writer);
writer.closeFile();
return 0;
}