Skip to content

Commit 702c9d2

Browse files
cirrasfourls
authored andcommitted
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.
1 parent bb0a855 commit 702c9d2

File tree

2 files changed

+12
-11
lines changed
  • delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr
  • its/src/projects/cpd-simple/src

2 files changed

+12
-11
lines changed

delphi-frontend/src/main/antlr3/au/com/integradev/delphi/antlr/Delphi.g

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,12 @@ import org.apache.commons.lang3.StringUtils;
333333
private Token combineLastNTokens(int type, int count) {
334334
CommonToken firstToken = (CommonToken) input.LT(-count);
335335
CommonToken lastToken = (CommonToken) input.LT(-1);
336-
lastToken.setType(type);
337-
lastToken.setStartIndex(firstToken.getStartIndex());
338-
lastToken.setLine(firstToken.getLine());
339-
lastToken.setCharPositionInLine(firstToken.getCharPositionInLine());
340-
return lastToken;
336+
CommonToken result = new CommonToken(lastToken);
337+
result.setType(type);
338+
result.setStartIndex(firstToken.getStartIndex());
339+
result.setLine(firstToken.getLine());
340+
result.setCharPositionInLine(firstToken.getCharPositionInLine());
341+
return result;
341342
}
342343

343344
private BinaryExpressionNodeImpl createBinaryExpression(Object operator) {

its/src/projects/cpd-simple/src/CpdTest.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TfDemo = class(TForm)
1515
public
1616
{testDefinitionsIncludes}
1717
end;
18-
18+
1919
type
2020
TfDemoSecond = class (TForm)
2121
bShowTracker: TButton;
@@ -25,7 +25,7 @@ TfDemoSecond = class (TForm)
2525
public
2626

2727
end;
28-
28+
2929
var
3030
fDemo: TfDemo;
3131

@@ -37,7 +37,7 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject);
3737
multiline
3838
comment
3939
*)
40-
40+
4141
for i:=0 to 100 do
4242
begin
4343
a := b;
@@ -46,7 +46,7 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject);
4646
end;
4747
c := d;
4848
a := b;
49-
if a < b then begin
49+
if a >= b then begin
5050
c := b;
5151
end;
5252
end;
@@ -59,11 +59,11 @@ procedure TfDemo.bShowTrackerClick(Sender: TObject);
5959
end;
6060
c := d;
6161
a := b;
62-
if a < b then begin
62+
if a >= b then begin
6363
c := b;
6464
end;
6565
end;
66-
66+
6767
end;
6868

6969
end.

0 commit comments

Comments
 (0)