parsing.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. //Added by qt3to4:
  28. #include <Q3ValueList>
  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=QStringList::split('\t',line,TRUE);
  64. QString identifier=columns[0];
  65. QString linenoString=columns[2];
  66. int semicolonPos=linenoString.find(';');
  67. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  68. int lineno=linenoString.toInt()-1;
  69. QString kind=columns[3];
  70. SourceFileFunctions::Iterator it=result.find(identifier);
  71. if (kind=="p") {
  72. if (it==result.end())
  73. result.append(SourceFileFunction(identifier,lineno,-1));
  74. } else if (kind=="f") {
  75. if (it==result.end())
  76. result.append(SourceFileFunction(identifier,-1,lineno));
  77. else
  78. (*it).implementationLine=lineno;
  79. } else qWarning("Invalid result from ctags.");
  80. } else {
  81. usleep(10000);
  82. QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,10);
  83. }
  84. }
  85. }
  86. delete_temp_file("parser_temp_source.c");
  87. return result;
  88. }
  89. SourceFileFunctions getASMFunctions(const QString &text)
  90. {
  91. // Parse ASM by hand.
  92. QStringList lines=QStringList::split('\n',text,TRUE);
  93. unsigned lineno=0;
  94. SourceFileFunctions result;
  95. for (QStringList::Iterator it=lines.begin(); it!=lines.end(); ++it,++lineno) {
  96. QString line=*it;
  97. if (line.isEmpty()) continue;
  98. QString identifier;
  99. unsigned col=0, l=line.length();
  100. while (col<l) {
  101. QChar c=line[col++];
  102. if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_'||c=='$')
  103. identifier.append(c);
  104. else
  105. break;
  106. }
  107. if (line[col-1]==':') result.append(SourceFileFunction(identifier,-1,lineno));
  108. }
  109. return result;
  110. }
  111. CompletionInfo parseFileCompletion(const QString &fileText,
  112. const QString &pathInProject,
  113. CompletionInfo result)
  114. {
  115. // Parse included files.
  116. // Empty lines can be ignored here.
  117. QStringList lines=QStringList::split('\n',fileText);
  118. bool inComment=false;
  119. bool isSystemHeader=pathInProject.isNull();
  120. for (QStringList::ConstIterator it=lines.begin(); it!=lines.end(); ++it) {
  121. const QString &line=(*it);
  122. if (!inComment) {
  123. QString strippedLine=line.stripWhiteSpace();
  124. if (strippedLine.startsWith("#include")) {
  125. QString includedName=strippedLine.mid(8).stripWhiteSpace();
  126. if (includedName[0]=='<') {
  127. int pos=includedName.find('>',1);
  128. if (pos>=0)
  129. result.includedSystem.append(includedName.mid(1,pos-1));
  130. } else if (includedName[0]=='\"') {
  131. int pos=includedName.find('\"',1);
  132. if (pos>=0) {
  133. if (isSystemHeader)
  134. // A system header can only include another system header.
  135. result.includedSystem.append(includedName.mid(1,pos-1));
  136. else
  137. result.included.append(QDir::cleanDirPath(pathInProject+"/"
  138. +includedName.mid(1,pos-1)));
  139. }
  140. } // else ignore
  141. }
  142. }
  143. int pos=0;
  144. if (inComment) {
  145. in_comment:
  146. pos=line.find("*/",pos);
  147. if (pos<0) continue; // Comment line only, next line.
  148. pos+=2;
  149. inComment=false;
  150. }
  151. pos=line.find("/*",pos);
  152. if (pos>=0) {
  153. pos+=2;
  154. inComment=true;
  155. goto in_comment;
  156. }
  157. }
  158. // Parse for prototypes etc. using ctags.
  159. QString fileTextCleaned=fileText;
  160. // ctags doesn't like asmspecs.
  161. fileTextCleaned.remove(QRegExp("\\b(asm|_asm|__asm)\\(\"%?[adAD][0-7]\"\\)"));
  162. write_temp_file("parser_temp_source.c",fileTextCleaned,0);
  163. {
  164. // The QTextCodec has to be passed explicitly, or it will default to
  165. // ISO-8859-1 regardless of the locale, which is just broken.
  166. KProcIO procio(QTextCodec::codecForLocale());
  167. // Use MergedStderr instead of Stderr so the messages get ordered
  168. // properly.
  169. procio.setComm(static_cast<KProcess::Communication>(
  170. KProcess::Stdout|KProcess::MergedStderr));
  171. procio.setWorkingDirectory(tempdir);
  172. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  173. <<"--C-kinds=defgpstuvx"<<"--fields=kS"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
  174. "__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
  175. "__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
  176. "__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
  177. <<"parser_temp_source.c";
  178. if (!procio.start()) {
  179. delete_temp_file("parser_temp_source.c");
  180. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  181. "Exuberant Ctags, which can be obtained from: "
  182. "http://ctags.sourceforge.net");
  183. result.dirty=true;
  184. return result;
  185. }
  186. QString line;
  187. int ret;
  188. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  189. if (ret>=0) {
  190. QStringList columns=QStringList::split('\t',line,TRUE);
  191. QString identifier=columns[0];
  192. QString linenoString=columns[2];
  193. int semicolonPos=linenoString.find(';');
  194. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  195. int lineno=linenoString.toInt()-1;
  196. QString kind=columns[3];
  197. QString type=(kind=="d")?"macro"
  198. :(kind=="e")?"enum"
  199. :(kind=="f" || kind=="p")?"func"
  200. :(kind=="v" || kind=="x")?"var"
  201. :"type";
  202. QString signature=columns[4];
  203. if (signature.startsWith("signature:")) signature.remove(0,10);
  204. signature.replace(QRegExp("\\s*,"),",").replace(QRegExp("\\s*\\)"),")");
  205. bool alreadyKnown=result.lineNumbers.contains(identifier);
  206. // This has to be done for system headers because there may already be
  207. // better information extracted from the .hsf files. However, .hsf files
  208. // obviously don't contain line number information.
  209. if (isSystemHeader) {
  210. for (Q3ValueList<KTextEditor::CompletionEntry>::ConstIterator it
  211. =result.entries.begin(); it!=result.entries.end(); ++it) {
  212. if ((*it).text==identifier) {
  213. alreadyKnown=true;
  214. break;
  215. }
  216. }
  217. }
  218. if (lineno>=0)
  219. result.lineNumbers.insert(identifier,lineno,(kind!="p" && kind!="x"));
  220. if (!alreadyKnown) {
  221. KTextEditor::CompletionEntry entry;
  222. entry.text=identifier;
  223. entry.prefix=type;
  224. entry.postfix=signature;
  225. result.entries.append(entry);
  226. }
  227. } else {
  228. usleep(10000);
  229. QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,10);
  230. }
  231. }
  232. }
  233. delete_temp_file("parser_temp_source.c");
  234. return result;
  235. }