DH - find duplicates in Delphi code

What is DH?

DH (Delphi Helper) is a command line tool that searches for duplicated sections in Delphi source files (.pas, .dpr, .inc) and presents them. The program understands the lexical structure of the source files so that the changes in letter case, whitespace, and variable naming do not affect the results. The duplicated sections can be in the same file or in different files. It is pretty fast too: the search in the source code delivered with Delphi XE5 Enterprise takes about 4 minutes.

Download demo version

You can download the demo version here. It is limited to 10 source files and 5000 tokens in a file.

How to run

Just start dh.exe in a directory with the source code or use the --dir parameter to specify a directory. The program scans all Delphi source files recursively.

        >dh.exe --dir C:\Users\IEUser\Downloads\mORMot2-master
        ...
        =========================
        Found duplicated code of 232 tokens:
        C:\Users\IEUser\Downloads\mORMot2-master\src\core\mormot.core.data.pas:7424:11
        inc(IP, ElemSize);
                end;
                while Compare(JP^, Pivot^) > 0 do
                begin
                  dec(J);
                  dec(JP, ElemSize);
                end;
                if I <= J then
                begin
                  if I <> J then
                    Exchg(IP, JP, ElemSize);
                  if p = I then
                    p := J
                  else if p = J then
                    p := I;
                  inc(I);
                  dec(J);
                end;
              until I > J;
              if J - L < R - I then
              begin
                // use recursion only for smaller range
                if L < J then
                  QuickSort(L, J);
                L := I;
              end
              else
              begin
                if I < R then
                  QuickSort(I, R);
                R := J;
              end;
            until L >= R;
        end;
        
        procedure TDynArrayQuickSort.QuickSortEvent(L, R: PtrInt);
        var
          I, J: PtrInt;
        begin
          if L < R then
            repeat
              I := L;
              J := R;
              p := (L + R) shr 1;
              repeat
                Pivot := Value + PtrUInt(p) * ElemSize;
                IP := Value + PtrUInt(I) * ElemSize;
                JP := Value + PtrUInt(J) * ElemSize;
                while CompareEvent(IP^, Pivot^
        
        C:\Users\IEUser\Downloads\mORMot2-master\src\core\mormot.core.data.pas:7475:11
        inc(IP, ElemSize);
                end;
                while CompareEvent(JP^, Pivot^) > 0 do
                begin
                  dec(J);
                  dec(JP, ElemSize);
                end;
                if I <= J then
                begin
                  if I <> J then
                    Exchg(IP, JP, ElemSize);
                  if p = I then
                    p := J
                  else if p = J then
                    p := I;
                  inc(I);
                  dec(J);
                end;
              until I > J;
              if J - L < R - I then
              begin
                // use recursion only for smaller range
                if L < J then
                  QuickSortEvent(L, J);
                L := I;
              end
              else
              begin
                if I < R then
                  QuickSortEvent(I, R);
                R := J;
              end;
            until L >= R;
        end;
        
        procedure TDynArrayQuickSort.QuickSortEventReverse(L, R: PtrInt);
        var
          I, J: PtrInt;
        begin
          if L < R then
            repeat
              I := L;
              J := R;
              p := (L + R) shr 1;
              repeat
                Pivot := Value + PtrUInt(p) * ElemSize;
                IP := Value + PtrUInt(I) * ElemSize;
                JP := Value + PtrUInt(J) * ElemSize;
                while CompareEvent(IP^, Pivot^
        
        =========================
        Found duplicated code of 680 tokens:
        C:\Users\IEUser\Downloads\mORMot2-master\src\core\mormot.core.collections.pas:1790:7
        ...        
    

Buy

Send me 10 Euros via PayPal or contact me at tim.lebedkov@gmail.com.

March 12, 2022