From 1ec2efd39ea54f08206467863084aa67efc0a71c Mon Sep 17 00:00:00 2001 From: Jonah Jeleniewski Date: Wed, 5 Mar 2025 12:52:46 +1100 Subject: [PATCH] Fix "tokens out of order" CPD errors around `>=` and `<=` tokens Token combination now creates a new token instead of mutating the last token in the group of combined tokens. This was causing problems because the combined token would be reflected back to the original list of "raw tokens" returned by `DelphiFile::getTokens`, resulting in overlapping tokens being reported to the sonar API. The mutability of the raw tokens is a point of brittleness that we could address, but it would have significant performance implications because it would involve constructing a copy of every token in every file. --- .../antlr3/au/com/integradev/delphi/antlr/Delphi.g | 11 ++++++----- its/src/projects/cpd-simple/src/CpdTest.pas | 12 ++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g b/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g index b86bfea13..8cff11310 100644 --- a/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g +++ b/delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g @@ -333,11 +333,12 @@ import org.apache.commons.lang3.StringUtils; private Token combineLastNTokens(int type, int count) { CommonToken firstToken = (CommonToken) input.LT(-count); CommonToken lastToken = (CommonToken) input.LT(-1); - lastToken.setType(type); - lastToken.setStartIndex(firstToken.getStartIndex()); - lastToken.setLine(firstToken.getLine()); - lastToken.setCharPositionInLine(firstToken.getCharPositionInLine()); - return lastToken; + CommonToken result = new CommonToken(lastToken); + result.setType(type); + result.setStartIndex(firstToken.getStartIndex()); + result.setLine(firstToken.getLine()); + result.setCharPositionInLine(firstToken.getCharPositionInLine()); + return result; } private BinaryExpressionNodeImpl createBinaryExpression(Object operator) { diff --git a/its/src/projects/cpd-simple/src/CpdTest.pas b/its/src/projects/cpd-simple/src/CpdTest.pas index aad2bf11a..7d3b13a98 100644 --- a/its/src/projects/cpd-simple/src/CpdTest.pas +++ b/its/src/projects/cpd-simple/src/CpdTest.pas @@ -15,7 +15,7 @@ TfDemo = class(TForm) public {testDefinitionsIncludes} end; - + type TfDemoSecond = class (TForm) bShowTracker: TButton; @@ -25,7 +25,7 @@ TfDemoSecond = class (TForm) public end; - + var fDemo: TfDemo; @@ -37,7 +37,7 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject); multiline comment *) - + for i:=0 to 100 do begin a := b; @@ -46,7 +46,7 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject); end; c := d; a := b; - if a < b then begin + if a >= b then begin c := b; end; end; @@ -59,11 +59,11 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject); end; c := d; a := b; - if a < b then begin + if a >= b then begin c := b; end; end; - + end; end. \ No newline at end of file