parsing.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006-2007 Kevin Kofler
  4. Copyright (C) 2007 Konrad Meyer
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. // This file handles parsing of source files for the function list and for
  18. // completion purposes.
  19. #include "parsing.h"
  20. #include "ktigcc.h"
  21. #include <QString>
  22. #include <QStringList>
  23. #include <QRegExp>
  24. #include <QTextCodec>
  25. #include <QApplication>
  26. #include <QEventLoop>
  27. #include <QDir>
  28. #include <QLinkedList>
  29. #include <kprocio.h>
  30. #include <kmessagebox.h>
  31. #include <unistd.h>
  32. SourceFileFunctions getCFunctions(const QString &text)
  33. {
  34. // Parse C using Exuberant Ctags (http://ctags.sourceforge.net).
  35. SourceFileFunctions result;
  36. write_temp_file("parser_temp_source.c",text,0);
  37. {
  38. // The QTextCodec has to be passed explicitly, or it will default to
  39. // ISO-8859-1 regardless of the locale, which is just broken.
  40. KProcIO procio(QTextCodec::codecForLocale());
  41. // Use MergedStderr instead of Stderr so the messages get ordered
  42. // properly.
  43. procio.setComm(static_cast<KProcess::Communication>(
  44. KProcess::Stdout|KProcess::MergedStderr));
  45. procio.setWorkingDirectory(tempdir);
  46. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  47. <<"--C-kinds=pf"<<"--fields=k"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
  48. "__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
  49. "__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
  50. "__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
  51. <<"parser_temp_source.c";
  52. if (!procio.start()) {
  53. delete_temp_file("parser_temp_source.c");
  54. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  55. "Exuberant Ctags, which can be obtained from: "
  56. "http://ctags.sourceforge.net");
  57. return result;
  58. }
  59. QString line;
  60. int ret;
  61. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  62. if (ret>=0) {
  63. QStringList columns=line.split('\t');
  64. int numColumns=columns.count();
  65. QString identifier;
  66. if (numColumns) identifier=columns[0];
  67. QString linenoString;
  68. if (numColumns>2) linenoString=columns[2];
  69. int semicolonPos=linenoString.find(';');
  70. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  71. int lineno=linenoString.toInt()-1;
  72. QString kind;
  73. if (numColumns>3) kind=columns[3];
  74. SourceFileFunctions::Iterator it=result.find(identifier);
  75. if (kind=="p") {
  76. if (it==result.end())
  77. result.append(SourceFileFunction(identifier,lineno,-1));
  78. } else if (kind=="f") {
  79. if (it==result.end())
  80. result.append(SourceFileFunction(identifier,-1,lineno));
  81. else
  82. (*it).implementationLine=lineno;
  83. } else qWarning("Invalid result from ctags.");
  84. } else {
  85. usleep(10000);
  86. QCoreApplication::processEvents(QEventLoop::ExcludeUserInput,10);
  87. }
  88. }
  89. }
  90. delete_temp_file("parser_temp_source.c");
  91. return result;
  92. }
  93. SourceFileFunctions getASMFunctions(const QString &text)
  94. {
  95. // Parse ASM by hand.
  96. QStringList lines=text.split('\n');
  97. unsigned lineno=0;
  98. SourceFileFunctions result;
  99. for (QStringList::Iterator it=lines.begin(); it!=lines.end(); ++it,++lineno) {
  100. QString line=*it;
  101. if (line.isEmpty()) continue;
  102. QString identifier;
  103. unsigned col=0, l=line.length();
  104. while (col<l) {
  105. QChar c=line[col++];
  106. if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_'||c=='$')
  107. identifier.append(c);
  108. else
  109. break;
  110. }
  111. if (line[col-1]==':') result.append(SourceFileFunction(identifier,-1,lineno));
  112. }
  113. return result;
  114. }
  115. CompletionInfo parseFileCompletion(const QString &fileText,
  116. const QString &pathInProject,
  117. CompletionInfo result)
  118. {
  119. // Parse included files.
  120. // Empty lines can be ignored here.
  121. QStringList lines=fileText.split('\n',QString::SkipEmptyParts);
  122. bool inComment=false;
  123. bool isSystemHeader=pathInProject.isNull();
  124. for (QStringList::ConstIterator it=lines.begin(); it!=lines.end(); ++it) {
  125. const QString &line=(*it);
  126. if (!inComment) {
  127. QString strippedLine=line.trimmed();
  128. if (strippedLine.startsWith("#include")) {
  129. QString includedName=strippedLine.mid(8).trimmed();
  130. if (!includedName.isEmpty()) {
  131. if (includedName[0]=='<') {
  132. int pos=includedName.find('>',1);
  133. if (pos>=0)
  134. result.includedSystem.append(includedName.mid(1,pos-1));
  135. } else if (includedName[0]=='\"') {
  136. int pos=includedName.find('\"',1);
  137. if (pos>=0) {
  138. if (isSystemHeader)
  139. // A system header can only include another system header.
  140. result.includedSystem.append(includedName.mid(1,pos-1));
  141. else
  142. result.included.append(QDir::cleanPath(pathInProject+"/"
  143. +includedName.mid(1,pos-1)));
  144. }
  145. } // else ignore
  146. }
  147. }
  148. }
  149. int pos=0;
  150. if (inComment) {
  151. in_comment:
  152. pos=line.find("*/",pos);
  153. if (pos<0) continue; // Comment line only, next line.
  154. pos+=2;
  155. inComment=false;
  156. }
  157. pos=line.find("/*",pos);
  158. if (pos>=0) {
  159. pos+=2;
  160. inComment=true;
  161. goto in_comment;
  162. }
  163. }
  164. // Parse for prototypes etc. using ctags.
  165. QString fileTextCleaned=fileText;
  166. // ctags doesn't like asmspecs.
  167. fileTextCleaned.remove(QRegExp("\\b(asm|_asm|__asm)\\(\"%?[adAD][0-7]\"\\)"));
  168. write_temp_file("parser_temp_source.c",fileTextCleaned,0);
  169. {
  170. // The QTextCodec has to be passed explicitly, or it will default to
  171. // ISO-8859-1 regardless of the locale, which is just broken.
  172. KProcIO procio(QTextCodec::codecForLocale());
  173. // Use MergedStderr instead of Stderr so the messages get ordered
  174. // properly.
  175. procio.setComm(static_cast<KProcess::Communication>(
  176. KProcess::Stdout|KProcess::MergedStderr));
  177. procio.setWorkingDirectory(tempdir);
  178. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  179. <<"--C-kinds=defgpstuvx"<<"--fields=kS"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
  180. "__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
  181. "__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
  182. "__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
  183. <<"parser_temp_source.c";
  184. if (!procio.start()) {
  185. delete_temp_file("parser_temp_source.c");
  186. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  187. "Exuberant Ctags, which can be obtained from: "
  188. "http://ctags.sourceforge.net");
  189. result.dirty=true;
  190. return result;
  191. }
  192. QString line;
  193. int ret;
  194. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  195. if (ret>=0) {
  196. QStringList columns=line.split('\t');
  197. int numColumns=columns.count();
  198. QString identifier;
  199. if (numColumns) identifier=columns[0];
  200. QString linenoString;
  201. if (numColumns>2) linenoString=columns[2];
  202. int semicolonPos=linenoString.find(';');
  203. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  204. int lineno=linenoString.toInt()-1;
  205. QString kind;
  206. if (numColumns>3) kind=columns[3];
  207. QString type=(kind=="d")?"macro"
  208. :(kind=="e")?"enum"
  209. :(kind=="f" || kind=="p")?"func"
  210. :(kind=="v" || kind=="x")?"var"
  211. :"type";
  212. QString signature;
  213. if (numColumns>4) signature=columns[4];
  214. if (signature.startsWith("signature:")) signature.remove(0,10);
  215. signature.replace(QRegExp("\\s*,"),",").replace(QRegExp("\\s*\\)"),")");
  216. bool alreadyKnown=result.lineNumbers.contains(identifier);
  217. // This has to be done for system headers because there may already be
  218. // better information extracted from the .hsf files. However, .hsf files
  219. // obviously don't contain line number information.
  220. if (isSystemHeader) {
  221. for (QLinkedList<CompletionEntry>::ConstIterator it
  222. =result.entries.begin(); it!=result.entries.end(); ++it) {
  223. if ((*it).text==identifier) {
  224. alreadyKnown=true;
  225. break;
  226. }
  227. }
  228. }
  229. if (lineno>=0)
  230. result.lineNumbers.insert(identifier,lineno,(kind!="p" && kind!="x"));
  231. if (!alreadyKnown) {
  232. CompletionEntry entry;
  233. entry.text=identifier;
  234. entry.prefix=type;
  235. entry.postfix=signature;
  236. result.entries.append(entry);
  237. }
  238. } else {
  239. usleep(10000);
  240. QCoreApplication::processEvents(QEventLoop::ExcludeUserInput,10);
  241. }
  242. }
  243. }
  244. delete_temp_file("parser_temp_source.c");
  245. return result;
  246. }