From 30335ab81ee52abe516d28bf20c9fc615eafd619 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Fri, 2 Aug 2024 22:00:31 +0200 Subject: [PATCH] C++: Add C11 `_Generic` IR tests --- .../library-tests/ir/ir/PrintAST.expected | 61 ++++++++++++++++++- .../library-tests/ir/ir/aliased_ir.expected | 58 +++++++++++++++++- cpp/ql/test/library-tests/ir/ir/generic.c | 29 ++++++++- .../test/library-tests/ir/ir/raw_ir.expected | 55 ++++++++++++++++- 4 files changed, 198 insertions(+), 5 deletions(-) diff --git a/cpp/ql/test/library-tests/ir/ir/PrintAST.expected b/cpp/ql/test/library-tests/ir/ir/PrintAST.expected index 4c912a36756c..66606ff7056e 100644 --- a/cpp/ql/test/library-tests/ir/ir/PrintAST.expected +++ b/cpp/ql/test/library-tests/ir/ir/PrintAST.expected @@ -4180,7 +4180,7 @@ destructors_for_temps.cpp: # 103| ValueCategory = prvalue # 104| getStmt(1): [ReturnStmt] return ... generic.c: -# 1| [TopLevelFunction] void c11_generic_test(unsigned int, int) +# 1| [TopLevelFunction] void c11_generic_test_with_load(unsigned int, int) # 1| : # 1| getParameter(0): [Parameter] x # 1| Type = [IntType] unsigned int @@ -4213,6 +4213,65 @@ generic.c: # 3| Value = [CStyleCast] 1 # 3| ValueCategory = prvalue # 4| getStmt(2): [ReturnStmt] return ... +# 12| [TopLevelFunction] char const* c11_generic_test_with_constant_and_macro() +# 12| : +# 13| getEntryPoint(): [BlockStmt] { ... } +# 14| getStmt(0): [DeclStmt] declaration +# 14| getDeclarationEntry(0): [VariableDeclarationEntry] definition of i +# 14| Type = [IntType] int +# 16| getStmt(1): [ReturnStmt] return ... +# 16| getExpr(): int +# 16| Type = [ArrayType] char[4] +# 16| Value = [StringLiteral] "int" +# 16| ValueCategory = lvalue +# 16| getExpr().getFullyConverted(): [CStyleCast] (const char *)... +# 16| Conversion = [PointerConversion] pointer conversion +# 16| Type = [PointerType] const char * +# 16| ValueCategory = prvalue +# 16| getExpr(): [ArrayToPointerConversion] array to pointer conversion +# 16| Type = [CharPointerType] char * +# 16| ValueCategory = prvalue +# 19| [TopLevelFunction] char const* c11_generic_test_with_constant_and_no_macro() +# 19| : +# 20| getEntryPoint(): [BlockStmt] { ... } +# 21| getStmt(0): [DeclStmt] declaration +# 21| getDeclarationEntry(0): [VariableDeclarationEntry] definition of i +# 21| Type = [IntType] int +# 23| getStmt(1): [ReturnStmt] return ... +# 23| getExpr(): int +# 23| Type = [ArrayType] char[4] +# 23| Value = [StringLiteral] "int" +# 23| ValueCategory = lvalue +# 23| getExpr().getFullyConverted(): [CStyleCast] (const char *)... +# 23| Conversion = [PointerConversion] pointer conversion +# 23| Type = [PointerType] const char * +# 23| ValueCategory = prvalue +# 23| getExpr(): [ArrayToPointerConversion] array to pointer conversion +# 23| Type = [CharPointerType] char * +# 23| ValueCategory = prvalue +# 26| [TopLevelFunction] void c11_generic_test_test_with_cast(int) +# 26| : +# 26| getParameter(0): [Parameter] y +# 26| Type = [IntType] int +# 26| getEntryPoint(): [BlockStmt] { ... } +# 27| getStmt(0): [DeclStmt] declaration +# 27| getDeclarationEntry(0): [VariableDeclarationEntry] definition of r +# 27| Type = [IntType] unsigned int +# 28| getStmt(1): [ExprStmt] ExprStmt +# 28| getExpr(): [AssignExpr] ... = ... +# 28| Type = [IntType] unsigned int +# 28| ValueCategory = prvalue +# 28| getLValue(): [VariableAccess] r +# 28| Type = [IntType] unsigned int +# 28| ValueCategory = lvalue +# 28| getRValue(): [VariableAccess] y +# 28| Type = [IntType] int +# 28| ValueCategory = prvalue(load) +# 28| getRValue().getFullyConverted(): [CStyleCast] (unsigned int)... +# 28| Conversion = [IntegralConversion] integral conversion +# 28| Type = [IntType] unsigned int +# 28| ValueCategory = prvalue +# 29| getStmt(2): [ReturnStmt] return ... ir.c: # 5| [TopLevelFunction] int getX(MyCoords*) # 5| : diff --git a/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected b/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected index 54ecfba3ffc4..ced441595d73 100644 --- a/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected +++ b/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected @@ -2959,7 +2959,7 @@ destructors_for_temps.cpp: # 102| v102_10(void) = ExitFunction : generic.c: -# 1| void c11_generic_test(unsigned int, int) +# 1| void c11_generic_test_with_load(unsigned int, int) # 1| Block 0 # 1| v1_1(void) = EnterFunction : # 1| m1_2(unknown) = AliasedDefinition : @@ -2982,6 +2982,62 @@ generic.c: # 1| v1_10(void) = AliasedUse : m1_3 # 1| v1_11(void) = ExitFunction : +# 12| char const* c11_generic_test_with_constant_and_macro() +# 12| Block 0 +# 12| v12_1(void) = EnterFunction : +# 12| m12_2(unknown) = AliasedDefinition : +# 12| m12_3(unknown) = InitializeNonLocal : +# 12| m12_4(unknown) = Chi : total:m12_2, partial:m12_3 +# 14| r14_1(glval) = VariableAddress[i] : +# 14| m14_2(int) = Uninitialized[i] : &:r14_1 +# 16| r16_1(glval) = VariableAddress[#return] : +# 16| r16_2(glval) = StringConstant[int] : +# 16| r16_3(char *) = Convert : r16_2 +# 16| r16_4(char *) = Convert : r16_3 +# 16| m16_5(char *) = Store[#return] : &:r16_1, r16_4 +# 12| r12_5(glval) = VariableAddress[#return] : +# 12| v12_6(void) = ReturnValue : &:r12_5, m16_5 +# 12| v12_7(void) = AliasedUse : m12_3 +# 12| v12_8(void) = ExitFunction : + +# 19| char const* c11_generic_test_with_constant_and_no_macro() +# 19| Block 0 +# 19| v19_1(void) = EnterFunction : +# 19| m19_2(unknown) = AliasedDefinition : +# 19| m19_3(unknown) = InitializeNonLocal : +# 19| m19_4(unknown) = Chi : total:m19_2, partial:m19_3 +# 21| r21_1(glval) = VariableAddress[i] : +# 21| m21_2(int) = Uninitialized[i] : &:r21_1 +# 23| r23_1(glval) = VariableAddress[#return] : +# 23| r23_2(glval) = StringConstant["int"] : +# 23| r23_3(char *) = Convert : r23_2 +# 23| r23_4(char *) = Convert : r23_3 +# 23| m23_5(char *) = Store[#return] : &:r23_1, r23_4 +# 19| r19_5(glval) = VariableAddress[#return] : +# 19| v19_6(void) = ReturnValue : &:r19_5, m23_5 +# 19| v19_7(void) = AliasedUse : m19_3 +# 19| v19_8(void) = ExitFunction : + +# 26| void c11_generic_test_test_with_cast(int) +# 26| Block 0 +# 26| v26_1(void) = EnterFunction : +# 26| m26_2(unknown) = AliasedDefinition : +# 26| m26_3(unknown) = InitializeNonLocal : +# 26| m26_4(unknown) = Chi : total:m26_2, partial:m26_3 +# 26| r26_5(glval) = VariableAddress[y] : +# 26| m26_6(int) = InitializeParameter[y] : &:r26_5 +# 27| r27_1(glval) = VariableAddress[r] : +# 27| m27_2(unsigned int) = Uninitialized[r] : &:r27_1 +# 28| r28_1(glval) = VariableAddress[y] : +# 28| r28_2(int) = Load[y] : &:r28_1, m26_6 +# 28| r28_3(unsigned int) = Convert : r28_2 +# 28| r28_4(glval) = VariableAddress[r] : +# 28| m28_5(unsigned int) = Store[r] : &:r28_4, r28_3 +# 29| v29_1(void) = NoOp : +# 26| v26_7(void) = ReturnVoid : +# 26| v26_8(void) = AliasedUse : m26_3 +# 26| v26_9(void) = ExitFunction : + ir.c: # 7| void MyCoordsTest(int) # 7| Block 0 diff --git a/cpp/ql/test/library-tests/ir/ir/generic.c b/cpp/ql/test/library-tests/ir/ir/generic.c index 3d669ba29fb9..22c9fb3cd162 100644 --- a/cpp/ql/test/library-tests/ir/ir/generic.c +++ b/cpp/ql/test/library-tests/ir/ir/generic.c @@ -1,6 +1,31 @@ -void c11_generic_test(unsigned int x, int y) { +void c11_generic_test_with_load(unsigned int x, int y) { unsigned int r; r = _Generic(r, unsigned int: x, int: y) + 1; } -// // semmle-extractor-options: -std=c11 +#define describe(val) \ + _Generic((val), \ + int: "int", \ + default: "unknown" \ + ) + +const char *c11_generic_test_with_constant_and_macro() +{ + int i; + + return describe(i); +} + +const char *c11_generic_test_with_constant_and_no_macro() +{ + int i; + + return _Generic(i, int: "int", default: "unknown"); +} + +void c11_generic_test_test_with_cast(int y) { + unsigned int r; + r = _Generic(r, unsigned int: (unsigned int)y, int: y); +} + +// semmle-extractor-options: -std=c11 diff --git a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected index 18f0d99ac676..01ebe0219f92 100644 --- a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected +++ b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected @@ -2733,7 +2733,7 @@ destructors_for_temps.cpp: # 102| v102_8(void) = ExitFunction : generic.c: -# 1| void c11_generic_test(unsigned int, int) +# 1| void c11_generic_test_with_load(unsigned int, int) # 1| Block 0 # 1| v1_1(void) = EnterFunction : # 1| mu1_2(unknown) = AliasedDefinition : @@ -2755,6 +2755,59 @@ generic.c: # 1| v1_9(void) = AliasedUse : ~m? # 1| v1_10(void) = ExitFunction : +# 12| char const* c11_generic_test_with_constant_and_macro() +# 12| Block 0 +# 12| v12_1(void) = EnterFunction : +# 12| mu12_2(unknown) = AliasedDefinition : +# 12| mu12_3(unknown) = InitializeNonLocal : +# 14| r14_1(glval) = VariableAddress[i] : +# 14| mu14_2(int) = Uninitialized[i] : &:r14_1 +# 16| r16_1(glval) = VariableAddress[#return] : +# 16| r16_2(glval) = StringConstant[int] : +# 16| r16_3(char *) = Convert : r16_2 +# 16| r16_4(char *) = Convert : r16_3 +# 16| mu16_5(char *) = Store[#return] : &:r16_1, r16_4 +# 12| r12_4(glval) = VariableAddress[#return] : +# 12| v12_5(void) = ReturnValue : &:r12_4, ~m? +# 12| v12_6(void) = AliasedUse : ~m? +# 12| v12_7(void) = ExitFunction : + +# 19| char const* c11_generic_test_with_constant_and_no_macro() +# 19| Block 0 +# 19| v19_1(void) = EnterFunction : +# 19| mu19_2(unknown) = AliasedDefinition : +# 19| mu19_3(unknown) = InitializeNonLocal : +# 21| r21_1(glval) = VariableAddress[i] : +# 21| mu21_2(int) = Uninitialized[i] : &:r21_1 +# 23| r23_1(glval) = VariableAddress[#return] : +# 23| r23_2(glval) = StringConstant["int"] : +# 23| r23_3(char *) = Convert : r23_2 +# 23| r23_4(char *) = Convert : r23_3 +# 23| mu23_5(char *) = Store[#return] : &:r23_1, r23_4 +# 19| r19_4(glval) = VariableAddress[#return] : +# 19| v19_5(void) = ReturnValue : &:r19_4, ~m? +# 19| v19_6(void) = AliasedUse : ~m? +# 19| v19_7(void) = ExitFunction : + +# 26| void c11_generic_test_test_with_cast(int) +# 26| Block 0 +# 26| v26_1(void) = EnterFunction : +# 26| mu26_2(unknown) = AliasedDefinition : +# 26| mu26_3(unknown) = InitializeNonLocal : +# 26| r26_4(glval) = VariableAddress[y] : +# 26| mu26_5(int) = InitializeParameter[y] : &:r26_4 +# 27| r27_1(glval) = VariableAddress[r] : +# 27| mu27_2(unsigned int) = Uninitialized[r] : &:r27_1 +# 28| r28_1(glval) = VariableAddress[y] : +# 28| r28_2(int) = Load[y] : &:r28_1, ~m? +# 28| r28_3(unsigned int) = Convert : r28_2 +# 28| r28_4(glval) = VariableAddress[r] : +# 28| mu28_5(unsigned int) = Store[r] : &:r28_4, r28_3 +# 29| v29_1(void) = NoOp : +# 26| v26_6(void) = ReturnVoid : +# 26| v26_7(void) = AliasedUse : ~m? +# 26| v26_8(void) = ExitFunction : + ir.c: # 7| void MyCoordsTest(int) # 7| Block 0