Skip to content

Commit 0a20f9f

Browse files
C++: Print field names and element indices for aggregate literals in PrintAST
1 parent 90b79eb commit 0a20f9f

File tree

2 files changed

+68
-32
lines changed

2 files changed

+68
-32
lines changed

cpp/ql/src/semmle/code/cpp/PrintAST.qll

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,42 @@ class FunctionNode extends ASTNode {
542542
}
543543
}
544544

545+
/**
546+
* A node representing an `ClassAggregateLiteral`.
547+
*/
548+
class ClassAggregateLiteralNode extends ExprNode {
549+
ClassAggregateLiteral list;
550+
551+
ClassAggregateLiteralNode() {
552+
list = ast
553+
}
554+
555+
override string getChildEdgeLabel(int childIndex) {
556+
exists(Field field |
557+
list.getFieldExpr(field) = list.getChild(childIndex) and
558+
result = "." + field.getName()
559+
)
560+
}
561+
}
562+
563+
/**
564+
* A node representing an `ArrayAggregateLiteral`.
565+
*/
566+
class ArrayAggregateLiteralNode extends ExprNode {
567+
ArrayAggregateLiteral list;
568+
569+
ArrayAggregateLiteralNode() {
570+
list = ast
571+
}
572+
573+
override string getChildEdgeLabel(int childIndex) {
574+
exists(int elementIndex |
575+
list.getElementExpr(elementIndex) = list.getChild(childIndex) and
576+
result = "[" + elementIndex.toString() + "]"
577+
)
578+
}
579+
}
580+
545581
query predicate nodes(PrintASTNode node, string key, string value) {
546582
node.shouldPrint() and
547583
value = node.getProperty(key)

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3325,10 +3325,10 @@ ir.cpp:
33253325
# 504| expr: {...}
33263326
# 504| Type = Point
33273327
# 504| ValueCategory = prvalue
3328-
# 504| 0: x
3328+
# 504| .x: x
33293329
# 504| Type = int
33303330
# 504| ValueCategory = prvalue(load)
3331-
# 504| 1: (int)...
3331+
# 504| .y: (int)...
33323332
# 504| Conversion = floating point to integral conversion
33333333
# 504| Type = int
33343334
# 504| ValueCategory = prvalue
@@ -3342,7 +3342,7 @@ ir.cpp:
33423342
# 505| expr: {...}
33433343
# 505| Type = Point
33443344
# 505| ValueCategory = prvalue
3345-
# 505| 0: x
3345+
# 505| .x: x
33463346
# 505| Type = int
33473347
# 505| ValueCategory = prvalue(load)
33483348
# 506| 2: declaration
@@ -3390,13 +3390,13 @@ ir.cpp:
33903390
# 514| expr: {...}
33913391
# 514| Type = Rect
33923392
# 514| ValueCategory = prvalue
3393-
# 514| 0: {...}
3393+
# 514| .topLeft: {...}
33943394
# 514| Type = Point
33953395
# 514| ValueCategory = prvalue
3396-
# 514| 0: x
3396+
# 514| .x: x
33973397
# 514| Type = int
33983398
# 514| ValueCategory = prvalue(load)
3399-
# 514| 1: (int)...
3399+
# 514| .y: (int)...
34003400
# 514| Conversion = floating point to integral conversion
34013401
# 514| Type = int
34023402
# 514| ValueCategory = prvalue
@@ -3410,26 +3410,26 @@ ir.cpp:
34103410
# 515| expr: {...}
34113411
# 515| Type = Rect
34123412
# 515| ValueCategory = prvalue
3413-
# 515| 0: {...}
3413+
# 515| .topLeft: {...}
34143414
# 515| Type = Point
34153415
# 515| ValueCategory = prvalue
3416-
# 515| 0: x
3416+
# 515| .x: x
34173417
# 515| Type = int
34183418
# 515| ValueCategory = prvalue(load)
3419-
# 515| 1: (int)...
3419+
# 515| .y: (int)...
34203420
# 515| Conversion = floating point to integral conversion
34213421
# 515| Type = int
34223422
# 515| ValueCategory = prvalue
34233423
# 515| expr: f
34243424
# 515| Type = float
34253425
# 515| ValueCategory = prvalue(load)
3426-
# 515| 1: {...}
3426+
# 515| .bottomRight: {...}
34273427
# 515| Type = Point
34283428
# 515| ValueCategory = prvalue
3429-
# 515| 0: x
3429+
# 515| .x: x
34303430
# 515| Type = int
34313431
# 515| ValueCategory = prvalue(load)
3432-
# 515| 1: (int)...
3432+
# 515| .y: (int)...
34333433
# 515| Conversion = floating point to integral conversion
34343434
# 515| Type = int
34353435
# 515| ValueCategory = prvalue
@@ -3443,16 +3443,16 @@ ir.cpp:
34433443
# 516| expr: {...}
34443444
# 516| Type = Rect
34453445
# 516| ValueCategory = prvalue
3446-
# 516| 0: {...}
3446+
# 516| .topLeft: {...}
34473447
# 516| Type = Point
34483448
# 516| ValueCategory = prvalue
3449-
# 516| 0: x
3449+
# 516| .x: x
34503450
# 516| Type = int
34513451
# 516| ValueCategory = prvalue(load)
3452-
# 516| 1: {...}
3452+
# 516| .bottomRight: {...}
34533453
# 516| Type = Point
34543454
# 516| ValueCategory = prvalue
3455-
# 516| 0: x
3455+
# 516| .x: x
34563456
# 516| Type = int
34573457
# 516| ValueCategory = prvalue(load)
34583458
# 517| 4: return ...
@@ -3477,17 +3477,17 @@ ir.cpp:
34773477
# 521| expr: {...}
34783478
# 521| Type = int[3]
34793479
# 521| ValueCategory = prvalue
3480-
# 521| 0: x
3480+
# 521| [0]: x
34813481
# 521| Type = int
34823482
# 521| ValueCategory = prvalue(load)
3483-
# 521| 1: (int)...
3483+
# 521| [1]: (int)...
34843484
# 521| Conversion = floating point to integral conversion
34853485
# 521| Type = int
34863486
# 521| ValueCategory = prvalue
34873487
# 521| expr: f
34883488
# 521| Type = float
34893489
# 521| ValueCategory = prvalue(load)
3490-
# 521| 2: 0
3490+
# 521| [2]: 0
34913491
# 521| Type = int
34923492
# 521| Value = 0
34933493
# 521| ValueCategory = prvalue
@@ -3498,7 +3498,7 @@ ir.cpp:
34983498
# 522| expr: {...}
34993499
# 522| Type = int[3]
35003500
# 522| ValueCategory = prvalue
3501-
# 522| 0: x
3501+
# 522| [0]: x
35023502
# 522| Type = int
35033503
# 522| ValueCategory = prvalue(load)
35043504
# 523| 3: return ...
@@ -3524,7 +3524,7 @@ ir.cpp:
35243524
# 531| expr: {...}
35253525
# 531| Type = U
35263526
# 531| ValueCategory = prvalue
3527-
# 531| 0: (double)...
3527+
# 531| .d: (double)...
35283528
# 531| Conversion = floating point conversion
35293529
# 531| Type = double
35303530
# 531| ValueCategory = prvalue
@@ -3708,7 +3708,7 @@ ir.cpp:
37083708
# 577| expr: {...}
37093709
# 577| Type = char[2]
37103710
# 577| ValueCategory = prvalue
3711-
# 577| 0: (char)...
3711+
# 577| [0]: (char)...
37123712
# 577| Conversion = integral conversion
37133713
# 577| Type = char
37143714
# 577| Value = 0
@@ -3724,7 +3724,7 @@ ir.cpp:
37243724
# 578| expr: {...}
37253725
# 578| Type = char[2]
37263726
# 578| ValueCategory = prvalue
3727-
# 578| 0: (char)...
3727+
# 578| [0]: (char)...
37283728
# 578| Conversion = integral conversion
37293729
# 578| Type = char
37303730
# 578| Value = 0
@@ -3733,7 +3733,7 @@ ir.cpp:
37333733
# 578| Type = int
37343734
# 578| Value = 0
37353735
# 578| ValueCategory = prvalue
3736-
# 578| 1: (char)...
3736+
# 578| [1]: (char)...
37373737
# 578| Conversion = integral conversion
37383738
# 578| Type = char
37393739
# 578| Value = 1
@@ -3749,7 +3749,7 @@ ir.cpp:
37493749
# 579| expr: {...}
37503750
# 579| Type = char[3]
37513751
# 579| ValueCategory = prvalue
3752-
# 579| 0: (char)...
3752+
# 579| [0]: (char)...
37533753
# 579| Conversion = integral conversion
37543754
# 579| Type = char
37553755
# 579| Value = 0
@@ -6372,7 +6372,7 @@ ir.cpp:
63726372
# 954| 1: {...}
63736373
# 954| Type = String[]
63746374
# 954| ValueCategory = prvalue
6375-
# 954| 0: call to String
6375+
# 954| [0]: call to String
63766376
# 954| Type = void
63776377
# 954| ValueCategory = prvalue
63786378
# 954| 2: n
@@ -6414,7 +6414,7 @@ ir.cpp:
64146414
# 957| 1: {...}
64156415
# 957| Type = DefaultCtorWithDefaultParam[]
64166416
# 957| ValueCategory = prvalue
6417-
# 957| 0: call to DefaultCtorWithDefaultParam
6417+
# 957| [0]: call to DefaultCtorWithDefaultParam
64186418
# 957| Type = void
64196419
# 957| ValueCategory = prvalue
64206420
# 957| 2: n
@@ -6427,15 +6427,15 @@ ir.cpp:
64276427
# 958| 1: {...}
64286428
# 958| Type = int[3]
64296429
# 958| ValueCategory = prvalue
6430-
# 958| 0: 0
6430+
# 958| [0]: 0
64316431
# 958| Type = int
64326432
# 958| Value = 0
64336433
# 958| ValueCategory = prvalue
6434-
# 958| 1: 1
6434+
# 958| [1]: 1
64356435
# 958| Type = int
64366436
# 958| Value = 1
64376437
# 958| ValueCategory = prvalue
6438-
# 958| 2: 2
6438+
# 958| [2]: 2
64396439
# 958| Type = int
64406440
# 958| Value = 2
64416441
# 958| ValueCategory = prvalue
@@ -6453,11 +6453,11 @@ ir.cpp:
64536453
# 962| expr: {...}
64546454
# 962| Type = int[1000]
64556455
# 962| ValueCategory = prvalue
6456-
# 962| 0: 10002
6456+
# 962| [2]: 10002
64576457
# 962| Type = int
64586458
# 962| Value = 10002
64596459
# 962| ValueCategory = prvalue
6460-
# 962| 1: 10900
6460+
# 962| [900]: 10900
64616461
# 962| Type = int
64626462
# 962| Value = 10900
64636463
# 962| ValueCategory = prvalue

0 commit comments

Comments
 (0)