parsing.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ktigcc - TIGCC IDE for KDE
  3. Copyright (C) 2006-2007 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 <Q3ValueList>
  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. QCoreApplication::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. CompletionInfo result)
  113. {
  114. // Parse included files.
  115. // Empty lines can be ignored here.
  116. QStringList lines=QStringList::split('\n',fileText);
  117. bool inComment=false;
  118. bool isSystemHeader=pathInProject.isNull();
  119. for (QStringList::ConstIterator it=lines.begin(); it!=lines.end(); ++it) {
  120. const QString &line=(*it);
  121. if (!inComment) {
  122. QString strippedLine=line.trimmed();
  123. if (strippedLine.startsWith("#include")) {
  124. QString includedName=strippedLine.mid(8).trimmed();
  125. if (includedName[0]=='<') {
  126. int pos=includedName.find('>',1);
  127. if (pos>=0)
  128. result.includedSystem.append(includedName.mid(1,pos-1));
  129. } else if (includedName[0]=='\"') {
  130. int pos=includedName.find('\"',1);
  131. if (pos>=0) {
  132. if (isSystemHeader)
  133. // A system header can only include another system header.
  134. result.includedSystem.append(includedName.mid(1,pos-1));
  135. else
  136. result.included.append(QDir::cleanPath(pathInProject+"/"
  137. +includedName.mid(1,pos-1)));
  138. }
  139. } // else ignore
  140. }
  141. }
  142. int pos=0;
  143. if (inComment) {
  144. in_comment:
  145. pos=line.find("*/",pos);
  146. if (pos<0) continue; // Comment line only, next line.
  147. pos+=2;
  148. inComment=false;
  149. }
  150. pos=line.find("/*",pos);
  151. if (pos>=0) {
  152. pos+=2;
  153. inComment=true;
  154. goto in_comment;
  155. }
  156. }
  157. // Parse for prototypes etc. using ctags.
  158. QString fileTextCleaned=fileText;
  159. // ctags doesn't like asmspecs.
  160. fileTextCleaned.remove(QRegExp("\\b(asm|_asm|__asm)\\(\"%?[adAD][0-7]\"\\)"));
  161. write_temp_file("parser_temp_source.c",fileTextCleaned,0);
  162. {
  163. // The QTextCodec has to be passed explicitly, or it will default to
  164. // ISO-8859-1 regardless of the locale, which is just broken.
  165. KProcIO procio(QTextCodec::codecForLocale());
  166. // Use MergedStderr instead of Stderr so the messages get ordered
  167. // properly.
  168. procio.setComm(static_cast<KProcess::Communication>(
  169. KProcess::Stdout|KProcess::MergedStderr));
  170. procio.setWorkingDirectory(tempdir);
  171. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  172. <<"--C-kinds=defgpstuvx"<<"--fields=kS"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
  173. "__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
  174. "__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
  175. "__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
  176. <<"parser_temp_source.c";
  177. if (!procio.start()) {
  178. delete_temp_file("parser_temp_source.c");
  179. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  180. "Exuberant Ctags, which can be obtained from: "
  181. "http://ctags.sourceforge.net");
  182. result.dirty=true;
  183. return result;
  184. }
  185. QString line;
  186. int ret;
  187. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  188. if (ret>=0) {
  189. QStringList columns=QStringList::split('\t',line,TRUE);
  190. QString identifier=columns[0];
  191. QString linenoString=columns[2];
  192. int semicolonPos=linenoString.find(';');
  193. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  194. int lineno=linenoString.toInt()-1;
  195. QString kind=columns[3];
  196. QString type=(kind=="d")?"macro"
  197. :(kind=="e")?"enum"
  198. :(kind=="f" || kind=="p")?"func"
  199. :(kind=="v" || kind=="x")?"var"
  200. :"type";
  201. QString signature=columns[4];
  202. if (signature.startsWith("signature:")) signature.remove(0,10);
  203. signature.replace(QRegExp("\\s*,"),",").replace(QRegExp("\\s*\\)"),")");
  204. bool alreadyKnown=result.lineNumbers.contains(identifier);
  205. // This has to be done for system headers because there may already be
  206. // better information extracted from the .hsf files. However, .hsf files
  207. // obviously don't contain line number information.
  208. if (isSystemHeader) {
  209. for (Q3ValueList<CompletionEntry>::ConstIterator it
  210. =result.entries.begin(); it!=result.entries.end(); ++it) {
  211. if ((*it).text==identifier) {
  212. alreadyKnown=true;
  213. break;
  214. }
  215. }
  216. }
  217. if (lineno>=0)
  218. result.lineNumbers.insert(identifier,lineno,(kind!="p" && kind!="x"));
  219. if (!alreadyKnown) {
  220. CompletionEntry entry;
  221. entry.text=identifier;
  222. entry.prefix=type;
  223. entry.postfix=signature;
  224. result.entries.append(entry);
  225. }
  226. } else {
  227. usleep(10000);
  228. QCoreApplication::processEvents(QEventLoop::ExcludeUserInput,10);
  229. }
  230. }
  231. }
  232. delete_temp_file("parser_temp_source.c");
  233. return result;
  234. }