/components/codetools/examples/removeemptymethods.lpr

http://github.com/graemeg/lazarus · Unknown · 105 lines · 94 code · 11 blank · 0 comment · 0 complexity · 5c672f3971e54b60fd450be607795abf MD5 · raw file

  1. {
  2. ***************************************************************************
  3. * *
  4. * This source 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 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This code is distributed in the hope that it will be useful, but *
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  12. * General Public License for more details. *
  13. * *
  14. * A copy of the GNU General Public License is available on the World *
  15. * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
  16. * obtain it by writing to the Free Software Foundation, *
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. * *
  19. ***************************************************************************
  20. Author: Mattias Gaertner
  21. Abstract:
  22. Simple demonstration, how to setup the codetools, FPC and Lazarus Source
  23. directory to remove empty methods.
  24. }
  25. program RemoveEmptyMethods;
  26. {$mode objfpc}{$H+}
  27. uses
  28. Classes, SysUtils, CodeCache, CodeToolManager, CodeToolsStructs,
  29. EmptyMethods1;
  30. const
  31. ConfigFilename = 'codetools.config';
  32. var
  33. Code: TCodeBuffer;
  34. X: Integer;
  35. Y: Integer;
  36. Filename: String;
  37. ListOfPCodeXYPosition: TFPList;
  38. i: Integer;
  39. P: PCodeXYPosition;
  40. All: boolean;
  41. Sections: TPascalClassSections;
  42. RemovedProcHeads: TStrings;
  43. begin
  44. if (ParamCount>=1) and (Paramcount<>3) then begin
  45. writeln('Usage:');
  46. writeln(' ',ParamStr(0));
  47. writeln(' ',ParamStr(0),' <filename> <X> <Y>');
  48. end;
  49. try
  50. CodeToolBoss.SimpleInit(ConfigFilename);
  51. X:=10;
  52. Y:=22;
  53. Filename:=ExpandFileName('scanexamples'+PathDelim+'emptymethods1.pas');
  54. if (ParamCount>=3) then begin
  55. Filename:=ExpandFileName(ParamStr(1));
  56. X:=StrToInt(ParamStr(2));
  57. Y:=StrToInt(ParamStr(3));
  58. end;
  59. // Step 1: load the file
  60. Code:=CodeToolBoss.LoadFile(Filename,false,false);
  61. if Code=nil then
  62. raise Exception.Create('loading failed '+Filename);
  63. // complete code
  64. ListOfPCodeXYPosition:=TFPList.Create;
  65. Sections:=[pcsPublished,pcsPrivate,pcsProtected,pcsPublic];
  66. if CodeToolBoss.FindEmptyMethods(Code,'',X,Y,Sections,ListOfPCodeXYPosition,All)
  67. then begin
  68. writeln('Found ',ListOfPCodeXYPosition.Count,' empty methods (All=',All,'):');
  69. for i:=0 to ListOfPCodeXYPosition.Count-1 do begin
  70. P:=PCodeXYPosition(ListOfPCodeXYPosition[i]);
  71. writeln(i,' ',Dbgs(P^));
  72. end;
  73. if CodeToolBoss.RemoveEmptyMethods(Code,'',X,Y,Sections,All,[],RemovedProcHeads)
  74. then begin
  75. writeln('Empty methods removed:');
  76. if RemovedProcHeads<>nil then
  77. writeln(RemovedProcHeads.Text);
  78. writeln('=========================');
  79. writeln(Code.Source);
  80. writeln('=========================');
  81. end else begin
  82. writeln('RemoveEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
  83. end;
  84. RemovedProcHeads.Free;
  85. end else begin
  86. writeln('FindEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
  87. end;
  88. CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
  89. except
  90. on E: Exception do begin
  91. writeln('EXCEPTION: '+E.Message);
  92. end;
  93. end;
  94. end.