parsing.cpp 9.3 KB

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