-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.cpp
244 lines (233 loc) · 9.12 KB
/
parsing.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
/*
ktigcc - TIGCC IDE for KDE
Copyright (C) 2006 Kevin Kofler
This program 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 2, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// This file handles parsing of source files for the function list and for
// completion purposes.
#include "parsing.h"
#include "ktigcc.h"
#include <qstring.h>
#include <qstringlist.h>
#include <qregexp.h>
#include <qtextcodec.h>
#include <qapplication.h>
#include <qeventloop.h>
#include <qdir.h>
#include <kprocio.h>
#include <kmessagebox.h>
#include <unistd.h>
SourceFileFunctions getCFunctions(const QString &text)
{
// Parse C using Exuberant Ctags (http://ctags.sourceforge.net).
SourceFileFunctions result;
write_temp_file("parser_temp_source.c",text,0);
{
// The QTextCodec has to be passed explicitly, or it will default to
// ISO-8859-1 regardless of the locale, which is just broken.
KProcIO procio(QTextCodec::codecForLocale());
// Use MergedStderr instead of Stderr so the messages get ordered
// properly.
procio.setComm(static_cast<KProcess::Communication>(
KProcess::Stdout|KProcess::MergedStderr));
procio.setWorkingDirectory(tempdir);
procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
<<"--C-kinds=pf"<<"--fields=k"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
"__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
"__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
"__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
<<"parser_temp_source.c";
if (!procio.start()) {
delete_temp_file("parser_temp_source.c");
KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
"Exuberant Ctags, which can be obtained from: "
"http://ctags.sourceforge.net");
return result;
}
QString line;
int ret;
while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
if (ret>=0) {
QStringList columns=QStringList::split('\t',line,TRUE);
QString identifier=columns[0];
QString linenoString=columns[2];
int semicolonPos=linenoString.find(';');
if (semicolonPos>=0) linenoString.truncate(semicolonPos);
int lineno=linenoString.toInt()-1;
QString kind=columns[3];
SourceFileFunctions::Iterator it=result.find(identifier);
if (kind=="p") {
if (it==result.end())
result.append(SourceFileFunction(identifier,lineno,-1));
} else if (kind=="f") {
if (it==result.end())
result.append(SourceFileFunction(identifier,-1,lineno));
else
(*it).implementationLine=lineno;
} else qWarning("Invalid result from ctags.");
} else {
usleep(10000);
QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,10);
}
}
}
delete_temp_file("parser_temp_source.c");
return result;
}
SourceFileFunctions getASMFunctions(const QString &text)
{
// Parse ASM by hand.
QStringList lines=QStringList::split('\n',text,TRUE);
unsigned lineno=0;
SourceFileFunctions result;
for (QStringList::Iterator it=lines.begin(); it!=lines.end(); ++it,++lineno) {
QString line=*it;
if (line.isEmpty()) continue;
QString identifier;
unsigned col=0, l=line.length();
while (col<l) {
QChar c=line[col++];
if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_'||c=='$')
identifier.append(c);
else
break;
}
if (line[col-1]==':') result.append(SourceFileFunction(identifier,-1,lineno));
}
return result;
}
CompletionInfo parseFileCompletion(const QString &fileText,
const QString &pathInProject,
CompletionInfo result)
{
// Parse included files.
// Empty lines can be ignored here.
QStringList lines=QStringList::split('\n',fileText);
bool inComment=false;
bool isSystemHeader=pathInProject.isNull();
for (QStringList::ConstIterator it=lines.begin(); it!=lines.end(); ++it) {
const QString &line=(*it);
if (!inComment) {
QString strippedLine=line.stripWhiteSpace();
if (strippedLine.startsWith("#include")) {
QString includedName=strippedLine.mid(8).stripWhiteSpace();
if (includedName[0]=='<') {
int pos=includedName.find('>',1);
if (pos>=0)
result.includedSystem.append(includedName.mid(1,pos-1));
} else if (includedName[0]=='\"') {
int pos=includedName.find('\"',1);
if (pos>=0) {
if (isSystemHeader)
// A system header can only include another system header.
result.includedSystem.append(includedName.mid(1,pos-1));
else
result.included.append(QDir::cleanDirPath(pathInProject+"/"
+includedName.mid(1,pos-1)));
}
} // else ignore
}
}
int pos=0;
if (inComment) {
in_comment:
pos=line.find("*/",pos);
if (pos<0) continue; // Comment line only, next line.
pos+=2;
inComment=false;
}
pos=line.find("/*",pos);
if (pos>=0) {
pos+=2;
inComment=true;
goto in_comment;
}
}
// Parse for prototypes etc. using ctags.
QString fileTextCleaned=fileText;
// ctags doesn't like asmspecs.
fileTextCleaned.remove(QRegExp("\\b(asm|_asm|__asm)\\(\"%?[adAD][0-7]\"\\)"));
write_temp_file("parser_temp_source.c",fileTextCleaned,0);
{
// The QTextCodec has to be passed explicitly, or it will default to
// ISO-8859-1 regardless of the locale, which is just broken.
KProcIO procio(QTextCodec::codecForLocale());
// Use MergedStderr instead of Stderr so the messages get ordered
// properly.
procio.setComm(static_cast<KProcess::Communication>(
KProcess::Stdout|KProcess::MergedStderr));
procio.setWorkingDirectory(tempdir);
procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
<<"--C-kinds=defgpstuvx"<<"--fields=kS"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
"__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
"__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
"__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
<<"parser_temp_source.c";
if (!procio.start()) {
delete_temp_file("parser_temp_source.c");
KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
"Exuberant Ctags, which can be obtained from: "
"http://ctags.sourceforge.net");
result.dirty=true;
return result;
}
QString line;
int ret;
while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
if (ret>=0) {
QStringList columns=QStringList::split('\t',line,TRUE);
QString identifier=columns[0];
QString linenoString=columns[2];
int semicolonPos=linenoString.find(';');
if (semicolonPos>=0) linenoString.truncate(semicolonPos);
int lineno=linenoString.toInt()-1;
QString kind=columns[3];
QString type=(kind=="d")?"macro"
:(kind=="e")?"enum"
:(kind=="f" || kind=="p")?"func"
:(kind=="v" || kind=="x")?"var"
:"type";
QString signature=columns[4];
if (signature.startsWith("signature:")) signature.remove(0,10);
signature.replace(QRegExp("\\s*,"),",").replace(QRegExp("\\s*\\)"),")");
bool alreadyKnown=result.lineNumbers.contains(identifier);
// This has to be done for system headers because there may already be
// better information extracted from the .hsf files. However, .hsf files
// obviously don't contain line number information.
if (isSystemHeader) {
for (QValueList<KTextEditor::CompletionEntry>::ConstIterator it
=result.entries.begin(); it!=result.entries.end(); ++it) {
if ((*it).text==identifier) {
alreadyKnown=true;
break;
}
}
}
if (lineno>=0)
result.lineNumbers.insert(identifier,lineno,(kind!="p" && kind!="x"));
if (!alreadyKnown) {
KTextEditor::CompletionEntry entry;
entry.text=identifier;
entry.prefix=type;
entry.postfix=signature;
result.entries.append(entry);
}
} else {
usleep(10000);
QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,10);
}
}
}
delete_temp_file("parser_temp_source.c");
return result;
}