parsing.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <qstringlist.h>
  21. #include <qtextcodec.h>
  22. #include <qapplication.h>
  23. #include <qeventloop.h>
  24. #include <kprocio.h>
  25. #include <kmessagebox.h>
  26. #include <unistd.h>
  27. SourceFileFunctions getCFunctions(const QString &text)
  28. {
  29. // Parse C using Exuberant Ctags (http://ctags.sourceforge.net).
  30. SourceFileFunctions result;
  31. write_temp_file("parser_temp_source.c",text,0);
  32. {
  33. // The QTextCodec has to be passed explicitly, or it will default to
  34. // ISO-8859-1 regardless of the locale, which is just broken.
  35. KProcIO procio(QTextCodec::codecForLocale());
  36. // Use MergedStderr instead of Stderr so the messages get ordered
  37. // properly.
  38. procio.setComm(static_cast<KProcess::Communication>(
  39. KProcess::Stdout|KProcess::MergedStderr));
  40. procio.setWorkingDirectory(tempdir);
  41. procio<<"ctags"<<"-f"<<"-"<<"-n"<<"-u"<<"-h"<<".h"<<"--language-force=C"
  42. <<"--C-kinds=pf"<<"--fields=k"<<"-I"<<"CALLBACK,__ATTR_TIOS__,"
  43. "__ATTR_TIOS_NORETURN__,__ATTR_TIOS_CALLBACK__,__ATTR_GCC__,"
  44. "__ATTR_LIB_C__,__ATTR_LIB_ASM__,__ATTR_LIB_ASM_NORETURN__,"
  45. "__ATTR_LIB_CALLBACK_C__,__ATTR_LIB_CALLBACK_ASM__"
  46. <<"parser_temp_source.c";
  47. if (!procio.start()) {
  48. delete_temp_file("parser_temp_source.c");
  49. KMessageBox::error(0,"Could not run ctags.\nThis feature requires "
  50. "Exuberant Ctags, which can be obtained from: "
  51. "http://ctags.sourceforge.net");
  52. return result;
  53. }
  54. QString line;
  55. int ret;
  56. while ((ret=procio.readln(line))>=0 || procio.isRunning()) {
  57. if (ret>=0) {
  58. QStringList columns=QStringList::split('\t',line,TRUE);
  59. QString identifier=columns[0];
  60. QString linenoString=columns[2];
  61. int semicolonPos=linenoString.find(';');
  62. if (semicolonPos>=0) linenoString.truncate(semicolonPos);
  63. int lineno=linenoString.toInt()-1;
  64. QString kind=columns[3];
  65. SourceFileFunctions::Iterator it=result.find(identifier);
  66. if (kind=="p") {
  67. if (it==result.end())
  68. result.append(SourceFileFunction(identifier,lineno,-1));
  69. } else if (kind=="f") {
  70. if (it==result.end())
  71. result.append(SourceFileFunction(identifier,-1,lineno));
  72. else
  73. (*it).implementationLine=lineno;
  74. } else qWarning("Invalid result from ctags.");
  75. } else {
  76. usleep(10000);
  77. QApplication::eventLoop()->processEvents(QEventLoop::ExcludeUserInput,10);
  78. }
  79. }
  80. }
  81. delete_temp_file("parser_temp_source.c");
  82. return result;
  83. }
  84. SourceFileFunctions getASMFunctions(const QString &text)
  85. {
  86. // Parse ASM by hand.
  87. QStringList lines=QStringList::split('\n',text,TRUE);
  88. unsigned lineno=0;
  89. SourceFileFunctions result;
  90. for (QStringList::Iterator it=lines.begin(); it!=lines.end(); ++it,++lineno) {
  91. QString line=*it;
  92. if (line.isEmpty()) continue;
  93. QString identifier;
  94. unsigned col=0, l=line.length();
  95. while (col<l) {
  96. QChar c=line[col++];
  97. if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')||(c>='0'&&c<='9')||c=='_'||c=='$')
  98. identifier.append(c);
  99. else
  100. break;
  101. }
  102. if (line[col-1]==':') result.append(SourceFileFunction(identifier,-1,lineno));
  103. }
  104. return result;
  105. }