parsing.cpp 8.3 KB

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