parsing.cpp 9.3 KB

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