PageRenderTime 18ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 21  Author: Mattias Gaertner
 22  
 23  Abstract:
 24    Simple demonstration, how to setup the codetools, FPC and Lazarus Source
 25    directory to remove empty methods.
 26}
 27program RemoveEmptyMethods;
 28
 29{$mode objfpc}{$H+}
 30
 31uses
 32  Classes, SysUtils, CodeCache, CodeToolManager, CodeToolsStructs,
 33  EmptyMethods1;
 34
 35const
 36  ConfigFilename = 'codetools.config';
 37var
 38  Code: TCodeBuffer;
 39  X: Integer;
 40  Y: Integer;
 41  Filename: String;
 42  ListOfPCodeXYPosition: TFPList;
 43  i: Integer;
 44  P: PCodeXYPosition;
 45  All: boolean;
 46  Sections: TPascalClassSections;
 47  RemovedProcHeads: TStrings;
 48begin
 49  if (ParamCount>=1) and (Paramcount<>3) then begin
 50    writeln('Usage:');
 51    writeln('  ',ParamStr(0));
 52    writeln('  ',ParamStr(0),' <filename> <X> <Y>');
 53  end;
 54
 55  try
 56    CodeToolBoss.SimpleInit(ConfigFilename);
 57    
 58    X:=10;
 59    Y:=22;
 60    Filename:=ExpandFileName('scanexamples'+PathDelim+'emptymethods1.pas');
 61    
 62    if (ParamCount>=3) then begin
 63      Filename:=ExpandFileName(ParamStr(1));
 64      X:=StrToInt(ParamStr(2));
 65      Y:=StrToInt(ParamStr(3));
 66    end;
 67
 68    // Step 1: load the file
 69    Code:=CodeToolBoss.LoadFile(Filename,false,false);
 70    if Code=nil then
 71      raise Exception.Create('loading failed '+Filename);
 72
 73    // complete code
 74    ListOfPCodeXYPosition:=TFPList.Create;
 75    Sections:=[pcsPublished,pcsPrivate,pcsProtected,pcsPublic];
 76    if CodeToolBoss.FindEmptyMethods(Code,'',X,Y,Sections,ListOfPCodeXYPosition,All)
 77    then begin
 78      writeln('Found ',ListOfPCodeXYPosition.Count,' empty methods (All=',All,'):');
 79      for i:=0 to ListOfPCodeXYPosition.Count-1 do begin
 80        P:=PCodeXYPosition(ListOfPCodeXYPosition[i]);
 81        writeln(i,' ',Dbgs(P^));
 82      end;
 83      if CodeToolBoss.RemoveEmptyMethods(Code,'',X,Y,Sections,All,[],RemovedProcHeads)
 84      then begin
 85        writeln('Empty methods removed:');
 86        if RemovedProcHeads<>nil then
 87          writeln(RemovedProcHeads.Text);
 88        writeln('=========================');
 89        writeln(Code.Source);
 90        writeln('=========================');
 91      end else begin
 92        writeln('RemoveEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
 93      end;
 94      RemovedProcHeads.Free;
 95    end else begin
 96      writeln('FindEmptyMethods failed: ',CodeToolBoss.ErrorMessage);
 97    end;
 98    CodeToolBoss.FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
 99  except
100    on E: Exception do begin
101      writeln('EXCEPTION: '+E.Message);
102    end;
103  end;
104end.
105