parsing.cpp 8.2 KB

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