Skip to content

Commit 5d35944

Browse files
authored
WI #2012 Add diagnostic on MOVE from PIC X to Numeric with usage (#2017)
* WI #2012 Create a diagnostic on MOVE from PIC X to a numeric with an usage * WI #2012 Change diagnostic message and add test * WI #2012 Correct tests failed because of new diagnostic * WI #2012 Shorten diagnostic message and remove cast to use StorageAreaKind * WI#2012 Use else if because StorageAreaKind are exclusive
1 parent 58c1577 commit 5d35944

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

TypeCobol.Analysis.Test/BasicCfgInstrs/CGM110.diag

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ Line 28256[8,37] <37, Warning, General> - Warning: Paragraph 'CGBCALAK-SYSPUNCH-
1010
Line 28289[8,37] <37, Warning, General> - Warning: Paragraph 'CGBCALAK-SYSPUNCH-WSBA-SUITE' is empty
1111
Line 28327[8,32] <37, Warning, General> - Warning: Paragraph 'CGBCALAK-SYSPUNCH-SUITE' is empty
1212
Line 29421[13,18] <37, Warning, General> - Warning: a End statement is not aligned with the matching opening statement
13+
Line 29550[20,71] <37, Warning, General> - Warning: Moving alphanumeric 'WS-CMM010-CODE-CADRAGE-JJ' to numeric 'WS-CMM010-CADR-BIN' declared with an USAGE may lead to unexpected results.
1314
Line 29627[16,21] <37, Warning, General> - Warning: a End statement is not aligned with the matching opening statement
1415
Line 29628[16,21] <37, Warning, General> - Warning: a End statement is not aligned with the matching opening statement
16+
Line 29685[24,46] <37, Warning, General> - Warning: Moving alphanumeric 'WS-CMM010-CODE-CADRAGE-JJ' to numeric 'WS-CMM010-CADR-BIN' declared with an USAGE may lead to unexpected results.
1517
Line 29772[12,17] <37, Warning, General> - Warning: "end-if" is missing
1618
Line 29776[16,21] <37, Warning, General> - Warning: "end-if" is missing
1719
Line 31035[21,26] <37, Warning, General> - Warning: a End statement is not aligned with the matching opening statement
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
IDENTIFICATION DIVISION.
2+
PROGRAM-ID. Pgm.
3+
DATA DIVISION.
4+
WORKING-STORAGE SECTION.
5+
01 alpha pic X(03).
6+
01 alpha2 pic X(03).
7+
8+
01 num1 pic 9(03).
9+
01 num2 comp-1.
10+
01 num3 comp-2.
11+
01 num4 pic 9(03) comp-3.
12+
01 num5 pic 9(03) comp-4.
13+
01 num6 pic 9(03) comp-5.
14+
01 num7 pic 9(03) packed-decimal.
15+
01 num8 pic 9(03) binary.
16+
01 num9 pic 9(03) comp.
17+
18+
PROCEDURE DIVISION.
19+
20+
* Ok
21+
move alpha to alpha2
22+
* Ok because num1 is an extended numeric
23+
move alpha to num1
24+
* Ok as it is not a numeric
25+
move alpha to num2
26+
* Ok as it is not a numeric
27+
move alpha to num3
28+
29+
* Ko because it is a comp variable
30+
move alpha to num4
31+
* Ko because it is a comp variable
32+
move alpha to num5
33+
* Ko because it is a comp variable
34+
move alpha to num6
35+
* Ko because it is a comp variable
36+
move alpha to num7
37+
* Ko because it is a comp variable
38+
move alpha to num8
39+
* Ko because it is a comp variable
40+
move alpha to num9
41+
42+
* Ok because it is a function
43+
move FUNCTION WHEN-COMPILED to num4
44+
* Ok because it is a function
45+
move FUNCTION CURRENT-DATE to num4
46+
* Ok because it is a function
47+
move LENGTH alpha to num4
48+
* Ok because it is a function
49+
move LENGTH OF alpha to num4
50+
51+
GOBACK
52+
.
53+
54+
END PROGRAM Pgm.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
 IDENTIFICATION DIVISION.
2+
PROGRAM-ID. Pgm.
3+
DATA DIVISION.
4+
WORKING-STORAGE SECTION.
5+
01 alpha pic X(03).
6+
01 alpha2 pic X(03).
7+
8+
01 num1 pic 9(03).
9+
01 num2 comp-1.
10+
01 num3 comp-2.
11+
01 num4 pic 9(03) comp-3.
12+
01 num5 pic 9(03) comp-4.
13+
01 num6 pic 9(03) comp-5.
14+
01 num7 pic 9(03) packed-decimal.
15+
01 num8 pic 9(03) binary.
16+
01 num9 pic 9(03) comp.
17+
18+
PROCEDURE DIVISION.
19+
20+
* Ok
21+
move alpha to alpha2
22+
* Ok because num1 is an extended numeric
23+
move alpha to num1
24+
* Ok as it is not a numeric
25+
move alpha to num2
26+
* Ok as it is not a numeric
27+
move alpha to num3
28+
29+
* Ko because it is a comp variable
30+
Line 30[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num4' declared with an USAGE may lead to unexpected results.
31+
move alpha to num4
32+
* Ko because it is a comp variable
33+
Line 32[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num5' declared with an USAGE may lead to unexpected results.
34+
move alpha to num5
35+
* Ko because it is a comp variable
36+
Line 34[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num6' declared with an USAGE may lead to unexpected results.
37+
move alpha to num6
38+
* Ko because it is a comp variable
39+
Line 36[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num7' declared with an USAGE may lead to unexpected results.
40+
move alpha to num7
41+
* Ko because it is a comp variable
42+
Line 38[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num8' declared with an USAGE may lead to unexpected results.
43+
move alpha to num8
44+
* Ko because it is a comp variable
45+
Line 40[12,38] <37, Warning, General> - Warning: Moving alphanumeric 'alpha' to numeric 'num9' declared with an USAGE may lead to unexpected results.
46+
move alpha to num9
47+
48+
* Ok because it is a function
49+
move FUNCTION WHEN-COMPILED to num4
50+
* Ok because it is a function
51+
move FUNCTION CURRENT-DATE to num4
52+
* Ok because it is a function
53+
move LENGTH alpha to num4
54+
* Ok because it is a function
55+
move LENGTH OF alpha to num4
56+
57+
GOBACK
58+
.
59+
60+
END PROGRAM Pgm.

TypeCobol/Compiler/Diagnostics/CrossChecker.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,40 @@ public override bool Visit(Move move)
214214
{
215215
return true;
216216
}
217+
218+
var senderIsAlphanumeric = false;
219+
DataDefinition senderDataDefinition = null;
220+
if (moveSimple.SendingVariable?.StorageArea?.Kind == StorageAreaKind.DataOrCondition
221+
&& move.StorageAreaReadsDataDefinition?.TryGetValue(moveSimple.SendingVariable.StorageArea, out senderDataDefinition) == true)
222+
{
223+
senderIsAlphanumeric = senderDataDefinition.DataType == DataType.Alphanumeric;
224+
}
225+
217226
foreach (var area in moveSimple.StorageAreaWrites)
218227
{
219228
var receiver = area.StorageArea;
220-
if (receiver is FunctionCallResult)
229+
if (receiver == null) continue;
230+
231+
if (receiver.Kind == StorageAreaKind.FunctionCallResult)
232+
{
221233
DiagnosticUtils.AddError(move, "MOVE: illegal <function call> after TO");
234+
}
235+
else if (senderIsAlphanumeric
236+
&& receiver.Kind == StorageAreaKind.DataOrCondition
237+
&& move.StorageAreaWritesDataDefinition != null
238+
&& move.StorageAreaWritesDataDefinition.TryGetValue(receiver, out var receiverDataDefinition))
239+
{
240+
if (receiverDataDefinition.DataType == DataType.Numeric || receiverDataDefinition.DataType == DataType.NumericEdited)
241+
{
242+
if (receiverDataDefinition.Usage != null && receiverDataDefinition.Usage != DataUsage.None)
243+
{
244+
DiagnosticUtils.AddError(move, $"Moving alphanumeric '{senderDataDefinition.Name}' to numeric '{receiverDataDefinition.Name}' declared with an USAGE may lead to unexpected results.", code: MessageCode.Warning);
245+
}
246+
}
247+
}
222248
}
223249
}
224250

225-
226251
return true;
227252
}
228253

0 commit comments

Comments
 (0)