Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e752f4

Browse files
C++: Add more Lambda IR test cases
New tests for mixed =/& captures and for captures with initializers
1 parent 8770258 commit 2e752f4

3 files changed

Lines changed: 485 additions & 90 deletions

File tree

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

Lines changed: 260 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6876,7 +6876,121 @@ ir.cpp:
68766876
# 1027| Type = int
68776877
# 1027| Value = 4
68786878
# 1027| ValueCategory = prvalue
6879-
# 1028| 10: return ...
6879+
# 1028| 10: declaration
6880+
# 1028| 0: definition of lambda_mixed_explicit
6881+
# 1028| Type = decltype([...](...){...})
6882+
# 1028| init: initializer for lambda_mixed_explicit
6883+
# 1028| expr: [...](...){...}
6884+
# 1028| Type = decltype([...](...){...})
6885+
# 1028| ValueCategory = prvalue
6886+
# 1028| 0: {...}
6887+
# 1028| Type = decltype([...](...){...})
6888+
# 1028| ValueCategory = prvalue
6889+
# 1028| .s: (reference to)
6890+
# 1028| Type = const String &
6891+
# 1028| ValueCategory = prvalue
6892+
# 1028| expr: (reference dereference)
6893+
# 1028| Type = const String
6894+
# 1028| ValueCategory = lvalue
6895+
# 1028| expr: s
6896+
# 1028| Type = const String &
6897+
# 1028| ValueCategory = prvalue(load)
6898+
# 1028| .x: x
6899+
# 1028| Type = int
6900+
# 1028| ValueCategory = prvalue(load)
6901+
# 1029| 11: ExprStmt
6902+
# 1029| 0: call to operator()
6903+
# 1029| Type = char
6904+
# 1029| ValueCategory = prvalue
6905+
# 1029| -1: (const lambda [] type at line 1028, col. 32)...
6906+
# 1029| Conversion = glvalue conversion
6907+
# 1029| Type = const lambda [] type at line 1028, col. 32
6908+
# 1029| ValueCategory = lvalue
6909+
# 1029| expr: lambda_mixed_explicit
6910+
# 1029| Type = decltype([...](...){...})
6911+
# 1029| ValueCategory = lvalue
6912+
# 1029| 0: (float)...
6913+
# 1029| Conversion = integral to floating point conversion
6914+
# 1029| Type = float
6915+
# 1029| Value = 5.0
6916+
# 1029| ValueCategory = prvalue
6917+
# 1029| expr: 5
6918+
# 1029| Type = int
6919+
# 1029| Value = 5
6920+
# 1029| ValueCategory = prvalue
6921+
# 1030| 12: declaration
6922+
# 1030| 0: definition of r
6923+
# 1030| Type = int
6924+
# 1030| init: initializer for r
6925+
# 1030| expr: ... - ...
6926+
# 1030| Type = int
6927+
# 1030| ValueCategory = prvalue
6928+
# 1030| 0: x
6929+
# 1030| Type = int
6930+
# 1030| ValueCategory = prvalue(load)
6931+
# 1030| 1: 1
6932+
# 1030| Type = int
6933+
# 1030| Value = 1
6934+
# 1030| ValueCategory = prvalue
6935+
# 1031| 13: declaration
6936+
# 1031| 0: definition of lambda_inits
6937+
# 1031| Type = decltype([...](...){...})
6938+
# 1031| init: initializer for lambda_inits
6939+
# 1031| expr: [...](...){...}
6940+
# 1031| Type = decltype([...](...){...})
6941+
# 1031| ValueCategory = prvalue
6942+
# 1031| 0: {...}
6943+
# 1031| Type = decltype([...](...){...})
6944+
# 1031| ValueCategory = prvalue
6945+
# 1031| .s: (reference to)
6946+
# 1031| Type = const String &
6947+
# 1031| ValueCategory = prvalue
6948+
# 1031| expr: (reference dereference)
6949+
# 1031| Type = const String
6950+
# 1031| ValueCategory = lvalue
6951+
# 1031| expr: s
6952+
# 1031| Type = const String &
6953+
# 1031| ValueCategory = prvalue(load)
6954+
# 1031| .x: x
6955+
# 1031| Type = int
6956+
# 1031| ValueCategory = prvalue(load)
6957+
# 1031| .i: ... + ...
6958+
# 1031| Type = int
6959+
# 1031| ValueCategory = prvalue
6960+
# 1031| 0: x
6961+
# 1031| Type = int
6962+
# 1031| ValueCategory = prvalue(load)
6963+
# 1031| 1: 1
6964+
# 1031| Type = int
6965+
# 1031| Value = 1
6966+
# 1031| ValueCategory = prvalue
6967+
# 1031| .j: (reference to)
6968+
# 1031| Type = int &
6969+
# 1031| ValueCategory = prvalue
6970+
# 1031| expr: r
6971+
# 1031| Type = int
6972+
# 1031| ValueCategory = lvalue
6973+
# 1032| 14: ExprStmt
6974+
# 1032| 0: call to operator()
6975+
# 1032| Type = char
6976+
# 1032| ValueCategory = prvalue
6977+
# 1032| -1: (const lambda [] type at line 1031, col. 23)...
6978+
# 1032| Conversion = glvalue conversion
6979+
# 1032| Type = const lambda [] type at line 1031, col. 23
6980+
# 1032| ValueCategory = lvalue
6981+
# 1032| expr: lambda_inits
6982+
# 1032| Type = decltype([...](...){...})
6983+
# 1032| ValueCategory = lvalue
6984+
# 1032| 0: (float)...
6985+
# 1032| Conversion = integral to floating point conversion
6986+
# 1032| Type = float
6987+
# 1032| Value = 6.0
6988+
# 1032| ValueCategory = prvalue
6989+
# 1032| expr: 6
6990+
# 1032| Type = int
6991+
# 1032| Value = 6
6992+
# 1032| ValueCategory = prvalue
6993+
# 1033| 15: return ...
68806994
# 1018| (void Lambda(int, String const&))::(lambda [] type at line 1018, col. 23)& (void Lambda(int, String const&))::(lambda [] type at line 1018, col. 23)::operator=((void Lambda(int, String const&))::(lambda [] type at line 1018, col. 23) const&)
68816995
# 1018| params:
68826996
#-----| 0: p#0
@@ -7148,3 +7262,148 @@ ir.cpp:
71487262
# 1026| Type = int
71497263
# 1026| Value = 0
71507264
# 1026| ValueCategory = prvalue
7265+
# 1028| (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)& (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)::operator=((void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32) const&)
7266+
# 1028| params:
7267+
#-----| 0: p#0
7268+
#-----| Type = const lambda [] type at line 1028, col. 32 &
7269+
# 1028| void (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)::(constructor)((void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32) const&)
7270+
# 1028| params:
7271+
#-----| 0: p#0
7272+
#-----| Type = const lambda [] type at line 1028, col. 32 &
7273+
# 1028| void (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)::(constructor)((void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)&&)
7274+
# 1028| params:
7275+
#-----| 0: p#0
7276+
#-----| Type = lambda [] type at line 1028, col. 32 &&
7277+
# 1028| initializations:
7278+
# 1028| 0: constructor init of field s
7279+
# 1028| Type = const String &
7280+
# 1028| ValueCategory = prvalue
7281+
# 1028| 0: Unknown literal
7282+
# 1028| Type = const String &
7283+
# 1028| ValueCategory = prvalue
7284+
# 1028| 1: constructor init of field x
7285+
# 1028| Type = int
7286+
# 1028| ValueCategory = prvalue
7287+
# 1028| 0: Unknown literal
7288+
# 1028| Type = int
7289+
# 1028| ValueCategory = prvalue
7290+
# 1028| body: { ... }
7291+
# 1028| 0: return ...
7292+
# 1028| void (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)::(constructor)()
7293+
# 1028| params:
7294+
# 1028| char (void Lambda(int, String const&))::(lambda [] type at line 1028, col. 32)::operator()(float) const
7295+
# 1028| params:
7296+
# 1028| 0: f
7297+
# 1028| Type = float
7298+
# 1028| body: { ... }
7299+
# 1028| 0: return ...
7300+
# 1028| 0: access to array
7301+
# 1028| Type = char
7302+
# 1028| ValueCategory = prvalue(load)
7303+
# 1028| 0: call to c_str
7304+
# 1028| Type = const char *
7305+
# 1028| ValueCategory = prvalue
7306+
# 1028| -1: (reference dereference)
7307+
# 1028| Type = const String
7308+
# 1028| ValueCategory = lvalue
7309+
#-----| expr: s
7310+
#-----| Type = const String &
7311+
#-----| ValueCategory = prvalue(load)
7312+
#-----| -1: this
7313+
#-----| Type = const lambda [] type at line 1028, col. 32 *
7314+
#-----| ValueCategory = prvalue(load)
7315+
#-----| 1: x
7316+
#-----| Type = int
7317+
#-----| ValueCategory = prvalue(load)
7318+
#-----| -1: this
7319+
#-----| Type = const lambda [] type at line 1028, col. 32 *
7320+
#-----| ValueCategory = prvalue(load)
7321+
# 1031| (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)& (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)::operator=((void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23) const&)
7322+
# 1031| params:
7323+
#-----| 0: p#0
7324+
#-----| Type = const lambda [] type at line 1031, col. 23 &
7325+
# 1031| void (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)::(constructor)((void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23) const&)
7326+
# 1031| params:
7327+
#-----| 0: p#0
7328+
#-----| Type = const lambda [] type at line 1031, col. 23 &
7329+
# 1031| void (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)::(constructor)((void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)&&)
7330+
# 1031| params:
7331+
#-----| 0: p#0
7332+
#-----| Type = lambda [] type at line 1031, col. 23 &&
7333+
# 1031| initializations:
7334+
# 1031| 0: constructor init of field s
7335+
# 1031| Type = const String &
7336+
# 1031| ValueCategory = prvalue
7337+
# 1031| 0: Unknown literal
7338+
# 1031| Type = const String &
7339+
# 1031| ValueCategory = prvalue
7340+
# 1031| 1: constructor init of field x
7341+
# 1031| Type = int
7342+
# 1031| ValueCategory = prvalue
7343+
# 1031| 0: Unknown literal
7344+
# 1031| Type = int
7345+
# 1031| ValueCategory = prvalue
7346+
# 1031| 2: constructor init of field i
7347+
# 1031| Type = int
7348+
# 1031| ValueCategory = prvalue
7349+
# 1031| 0: Unknown literal
7350+
# 1031| Type = int
7351+
# 1031| ValueCategory = prvalue
7352+
# 1031| 3: constructor init of field j
7353+
# 1031| Type = int &
7354+
# 1031| ValueCategory = prvalue
7355+
# 1031| 0: Unknown literal
7356+
# 1031| Type = int &
7357+
# 1031| ValueCategory = prvalue
7358+
# 1031| body: { ... }
7359+
# 1031| 0: return ...
7360+
# 1031| void (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)::(constructor)()
7361+
# 1031| params:
7362+
# 1031| char (void Lambda(int, String const&))::(lambda [] type at line 1031, col. 23)::operator()(float) const
7363+
# 1031| params:
7364+
# 1031| 0: f
7365+
# 1031| Type = float
7366+
# 1031| body: { ... }
7367+
# 1031| 0: return ...
7368+
# 1031| 0: access to array
7369+
# 1031| Type = char
7370+
# 1031| ValueCategory = prvalue(load)
7371+
# 1031| 0: call to c_str
7372+
# 1031| Type = const char *
7373+
# 1031| ValueCategory = prvalue
7374+
# 1031| -1: (reference dereference)
7375+
# 1031| Type = const String
7376+
# 1031| ValueCategory = lvalue
7377+
#-----| expr: s
7378+
#-----| Type = const String &
7379+
#-----| ValueCategory = prvalue(load)
7380+
#-----| -1: this
7381+
#-----| Type = const lambda [] type at line 1031, col. 23 *
7382+
#-----| ValueCategory = prvalue(load)
7383+
# 1031| 1: ... - ...
7384+
# 1031| Type = int
7385+
# 1031| ValueCategory = prvalue
7386+
# 1031| 0: ... + ...
7387+
# 1031| Type = int
7388+
# 1031| ValueCategory = prvalue
7389+
#-----| 0: x
7390+
#-----| Type = int
7391+
#-----| ValueCategory = prvalue(load)
7392+
#-----| -1: this
7393+
#-----| Type = const lambda [] type at line 1031, col. 23 *
7394+
#-----| ValueCategory = prvalue(load)
7395+
# 1031| 1: i
7396+
# 1031| Type = int
7397+
# 1031| ValueCategory = prvalue(load)
7398+
#-----| -1: this
7399+
#-----| Type = const lambda [] type at line 1031, col. 23 *
7400+
#-----| ValueCategory = prvalue(load)
7401+
# 1031| 1: (reference dereference)
7402+
# 1031| Type = int
7403+
# 1031| ValueCategory = prvalue(load)
7404+
# 1031| expr: j
7405+
# 1031| Type = int &
7406+
# 1031| ValueCategory = prvalue(load)
7407+
#-----| -1: this
7408+
#-----| Type = const lambda [] type at line 1031, col. 23 *
7409+
#-----| ValueCategory = prvalue(load)

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,28 @@ void Lambda(int x, const String& s) {
10251025
lambda_ref_explicit(3);
10261026
auto lambda_val_explicit = [s](float f) { return s.c_str()[0]; };
10271027
lambda_val_explicit(4);
1028-
}
1028+
auto lambda_mixed_explicit = [&s, x](float f) { return s.c_str()[x]; };
1029+
lambda_mixed_explicit(5);
1030+
int r = x - 1;
1031+
auto lambda_inits = [&s, x, i = x + 1, &j = r](float f) { return s.c_str()[x + i - j]; };
1032+
lambda_inits(6);
1033+
}
1034+
1035+
/*
1036+
Explicit capture of `this` requires possible extractor fixes.
1037+
1038+
struct LambdaContainer {
1039+
int x;
1040+
1041+
void LambdaMember(const String& s) {
1042+
auto lambda_implicit_this = [=](float f) { return s.c_str()[x]; };
1043+
lambda_implicit_this(1);
1044+
auto lambda_explicit_this_byref = [this, &s](float f) { return s.c_str()[x]; };
1045+
lambda_explicit_this_byref(2);
1046+
auto lambda_explicit_this_bycopy = [*this, &s](float f) { return s.c_str()[x]; };
1047+
lambda_explicit_this_bycopy(3);
1048+
}
1049+
};
1050+
*/
10291051

10301052
// semmle-extractor-options: -std=c++17

0 commit comments

Comments
 (0)