C++: Support C11 _Generic expressions

This commit is contained in:
Jeroen Ketema
2024-08-02 22:41:44 +02:00
parent 30335ab81e
commit 4945943732
21 changed files with 11125 additions and 1133 deletions

View File

@@ -0,0 +1,32 @@
/*
* Approach: replace conversion expressions of kind 389 (= @c11_generic) by
* conversion expressions of kind 12 (= @parexpr), i.e., a `ParenthesisExpr`,
* and drop the relation which its child expressions, which are just syntactic
* sugar. Parenthesis expressions are equally benign as C11 _Generic expressions,
* and behave similarly in the context of the IR.
*/
class Expr extends @expr {
string toString() { none() }
}
class Location extends @location {
string toString() { none() }
}
class ExprParent extends @exprparent {
string toString() { none() }
}
query predicate new_exprs(Expr expr, int new_kind, Location loc) {
exists(int kind | exprs(expr, kind, loc) | if kind = 389 then new_kind = 12 else new_kind = kind)
}
query predicate new_exprparents(Expr expr, int index, ExprParent expr_parent) {
exprparents(expr, index, expr_parent) and
(
not expr_parent instanceof @expr
or
exists(int kind | exprs(expr_parent.(Expr), kind, _) | kind != 389)
)
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,4 @@
description: Expose C11 _Generics
compatibility: partial
exprs.rel: run downgrades.ql new_exprs
exprparents.rel: run downgrades.ql new_exprparents

View File

@@ -0,0 +1,4 @@
---
category: feature
---
* Added a class `C11GenericExpr` to represent C11 generic selection expressions. The generic selection is represented as a `Conversion` on the expression that will be selected.

View File

@@ -385,6 +385,21 @@ class CastNode extends ConversionNode {
}
}
/**
* A node representing a `C11GenericExpr`.
*/
class C11GenericNode extends ConversionNode {
C11GenericExpr generic;
C11GenericNode() { generic = conv }
override AstNode getChildInternal(int childIndex) {
result = super.getChildInternal(childIndex - count(generic.getAChild()))
or
result.getAst() = generic.getChild(childIndex)
}
}
/**
* A node representing a `StmtExpr`.
*/
@@ -860,6 +875,15 @@ private predicate namedExprChildPredicates(Expr expr, Element ele, string pred)
or
expr.(BuiltInVarArgsStart).getLastNamedParameter() = ele and pred = "getLastNamedParameter()"
or
expr.(C11GenericExpr).getControllingExpr() = ele and pred = "getControllingExpr()"
or
exists(int n |
expr.(C11GenericExpr).getAssociationType(n) = ele.(TypeName).getType() and
pred = "getAssociationType(" + n + ")"
or
expr.(C11GenericExpr).getAssociationExpr(n) = ele and pred = "getAssociationExpr(" + n + ")"
)
or
expr.(Call).getQualifier() = ele and pred = "getQualifier()"
or
exists(int n | expr.(Call).getArgument(n) = ele and pred = "getArgument(" + n.toString() + ")")

View File

@@ -632,6 +632,104 @@ class ParenthesisExpr extends Conversion, @parexpr {
override string getAPrimaryQlClass() { result = "ParenthesisExpr" }
}
/**
* A node representing a C11 `_Generic` selection expression.
*
* For example:
* ```
* _Generic(e, int: "int", default: "unknown")
* ```
*/
class C11GenericExpr extends Conversion, @c11_generic {
int associationCount;
C11GenericExpr() { associationCount = (count(this.getAChild()) - 1) / 2 }
override string toString() { result = "_Generic" }
override string getAPrimaryQlClass() { result = "C11GenericExpr" }
/**
* Gets the controlling expression of the generic selection.
*
* For example, for
* ```
* _Generic(e, int: "a", default: "b")
* ```
* the result is `e`.
*/
Expr getControllingExpr() { result = this.getChild(0) }
/**
* Gets the type of the `n`th element in the association list of the generic selection.
*
* For example, for
* ```
* _Generic(e, int: "a", default: "b")
* ```
* the type of the 0th element is `int`. In the case of the default element the
* type will an instance of `VoidType`.
*/
Type getAssociationType(int n) {
n in [0 .. associationCount - 1] and
result = this.getChild(n * 2 + 1).(TypeName).getType()
}
/**
* Gets the type of an element in the association list of the generic selection.
*/
Type getAnAssociationType() { result = this.getAssociationType(_) }
/**
* Gets the expression of the `n`th element in the association list of
* the generic selection.
*
* For example, for
* ```
* _Generic(e, int: "a", default: "b")
* ```
* the expression for 0th element is `"a"`, and the expression for the
* 1st element is `"b"`. For the selected expression, this predicate
* will yield a `ReuseExpr`, such that
* ```
* this.getAssociationExpr(n).(ReuseExpr).getReusedExpr() = this.getExpr()
* ```
*/
Expr getAssociationExpr(int n) {
n in [0 .. associationCount - 1] and
result = this.getChild(n * 2 + 2)
}
/**
* Gets the expression of an element in the association list of the generic selection.
*/
Expr getAnAssociationExpr() { result = this.getAssociationExpr(_) }
/**
* Holds if the `n`th element of the association list of the generic selection is the
* default element.
*
* For example, for
* ```
* _Generic(e, int: "a", default: "b")
* ```
* this holds for 1.
*/
predicate isDefaultAssociation(int n) { this.getAssociationType(n) instanceof VoidType }
/**
* Holds if the `n`th element of the association list of the generic selection is the
* one whose expression was selected.
*
* For example, with `e` of type `int` and
* ```
* _Generic(e, int: "a", default: "b")
* ```
* this holds for 0.
*/
predicate isSelectedAssociation(int n) { this.getAssociationExpr(n) instanceof ReuseExpr }
}
/**
* A C/C++ expression that could not be resolved, or that can no longer be
* represented due to a database upgrade or downgrade.
@@ -668,6 +766,8 @@ class AssumeExpr extends Expr, @assume {
/**
* A C/C++ comma expression.
*
* For example:
* ```
* int c = compute1(), compute2(), resulting_value;
* ```

View File

@@ -128,6 +128,9 @@ private predicate ignoreExprAndDescendants(Expr expr) {
vaStartExpr.getLastNamedParameter().getFullyConverted() = expr
)
or
// The children of C11 _Generic expressions are just surface syntax.
exists(C11GenericExpr generic | generic.getAChild() = expr)
or
// Do not translate implicit destructor calls for unnamed temporary variables that are
// conditionally constructed (until we have a mechanism for calling these only when the
// temporary's constructor was run)
@@ -432,6 +435,9 @@ predicate ignoreLoad(Expr expr) {
// The load is duplicated from the right operand.
isExtractorFrontendVersion65OrHigher() and expr instanceof CommaExpr
or
// The load is duplicated from the chosen expression.
expr instanceof C11GenericExpr
or
expr.(PointerDereferenceExpr).getOperand().getFullyConverted().getType().getUnspecifiedType()
instanceof FunctionPointerType
or

View File

@@ -893,7 +893,8 @@ class TranslatedTransparentConversion extends TranslatedTransparentExpr {
(
expr instanceof ParenthesisExpr or
expr instanceof ReferenceDereferenceExpr or
expr instanceof ReferenceToExpr
expr instanceof ReferenceToExpr or
expr instanceof C11GenericExpr
)
}

View File

@@ -1210,6 +1210,7 @@ conversionkinds(
| @reference_to
| @ref_indirect
| @temp_init
| @c11_generic
;
/*
@@ -1792,6 +1793,7 @@ case @expr.kind of
| 386 = @isscopedenum
| 387 = @istriviallyrelocatable
| 388 = @datasizeof
| 389 = @c11_generic
;
@var_args_expr = @vastartexpr

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
description: Expose C11 _Generics
compatibility: backwards

View File

@@ -0,0 +1,458 @@
#-----| [CopyAssignmentOperator] __va_list_tag& __va_list_tag::operator=(__va_list_tag const&)
#-----| <params>:
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
#-----| Type = [LValueReferenceType] const __va_list_tag &
#-----| [MoveAssignmentOperator] __va_list_tag& __va_list_tag::operator=(__va_list_tag&&)
#-----| <params>:
#-----| getParameter(0): [Parameter] (unnamed parameter 0)
#-----| Type = [RValueReferenceType] __va_list_tag &&
generic.c:
# 3| [FormattingFunction,TopLevelFunction] int printf(char const*)
# 3| <params>:
# 3| getParameter(0): [Parameter] format
# 3| Type = [PointerType] const char *
# 14| [TopLevelFunction] int main()
# 14| <params>:
# 15| getEntryPoint(): [BlockStmt] { ... }
# 16| getStmt(0): [DeclStmt] declaration
# 16| getDeclarationEntry(0): [VariableDeclarationEntry] definition of i
# 16| Type = [IntType] int
# 17| getStmt(1): [DeclStmt] declaration
# 17| getDeclarationEntry(0): [VariableDeclarationEntry] definition of m
# 17| Type = [CTypedefType] MYINT
# 18| getStmt(2): [DeclStmt] declaration
# 18| getDeclarationEntry(0): [VariableDeclarationEntry] definition of s
# 18| Type = [PointerType] const char *
# 19| getStmt(3): [DeclStmt] declaration
# 19| getDeclarationEntry(0): [VariableDeclarationEntry] definition of f
# 19| Type = [PointerType] float ***
# 21| getStmt(4): [ExprStmt] ExprStmt
# 21| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 21| Type = [IntType] int
# 21| ValueCategory = prvalue
# 21| getArgument(0): i is %s\n
# 21| Type = [ArrayType] char[9]
# 21| Value = [StringLiteral] "i is %s\n"
# 21| ValueCategory = lvalue
# 21| getArgument(1): int
# 21| Type = [ArrayType] char[4]
# 21| Value = [StringLiteral] "int"
# 21| ValueCategory = lvalue
# 21| getArgument(0).getFullyConverted(): [CStyleCast] (const char *)...
# 21| Conversion = [PointerConversion] pointer conversion
# 21| Type = [PointerType] const char *
# 21| ValueCategory = prvalue
# 21| getExpr(): [ArrayToPointerConversion] array to pointer conversion
# 21| Type = [CharPointerType] char *
# 21| ValueCategory = prvalue
# 21| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 21| Type = [CharPointerType] char *
# 21| ValueCategory = prvalue
# 21| getExpr(): [C11GenericExpr] _Generic
# 21| Type = [ArrayType] char[4]
# 21| Value = [C11GenericExpr] int
# 21| ValueCategory = lvalue
# 21| getControllingExpr(): [VariableAccess] i
# 21| Type = [IntType] int
# 21| ValueCategory = prvalue(load)
# 21| getAssociationType(0): [TypeName] int
# 21| Type = [IntType] int
# 21| ValueCategory = prvalue
# 21| getAssociationExpr(0): [ReuseExpr] reuse of int
# 21| Type = [ArrayType] char[4]
# 21| ValueCategory = lvalue
# 21| getAssociationType(1): [TypeName] const char *
# 21| Type = [PointerType] const char *
# 21| ValueCategory = prvalue
# 21| getAssociationExpr(1): string
# 21| Type = [ArrayType] char[7]
# 21| Value = [StringLiteral] "string"
# 21| ValueCategory = lvalue
# 21| getAssociationType(2): [TypeName] void
# 21| Type = [VoidType] void
# 21| ValueCategory = prvalue
# 21| getAssociationExpr(2): unknown
# 21| Type = [ArrayType] char[8]
# 21| Value = [StringLiteral] "unknown"
# 21| ValueCategory = lvalue
# 21| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 21| Type = [IntType] int
# 21| ValueCategory = prvalue(load)
# 22| getStmt(5): [ExprStmt] ExprStmt
# 22| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 22| Type = [IntType] int
# 22| ValueCategory = prvalue
# 22| getArgument(0): c is %s\n
# 22| Type = [ArrayType] char[9]
# 22| Value = [StringLiteral] "c is %s\n"
# 22| ValueCategory = lvalue
# 22| getArgument(1): int
# 22| Type = [ArrayType] char[4]
# 22| Value = [StringLiteral] "int"
# 22| ValueCategory = lvalue
# 22| getArgument(0).getFullyConverted(): [CStyleCast] (const char *)...
# 22| Conversion = [PointerConversion] pointer conversion
# 22| Type = [PointerType] const char *
# 22| ValueCategory = prvalue
# 22| getExpr(): [ArrayToPointerConversion] array to pointer conversion
# 22| Type = [CharPointerType] char *
# 22| ValueCategory = prvalue
# 22| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 22| Type = [CharPointerType] char *
# 22| ValueCategory = prvalue
# 22| getExpr(): [C11GenericExpr] _Generic
# 22| Type = [ArrayType] char[4]
# 22| Value = [C11GenericExpr] int
# 22| ValueCategory = lvalue
# 22| getControllingExpr(): [VariableAccess] m
# 22| Type = [CTypedefType] MYINT
# 22| ValueCategory = prvalue(load)
# 22| getAssociationType(0): [TypeName] int
# 22| Type = [IntType] int
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(0): [ReuseExpr] reuse of int
# 22| Type = [ArrayType] char[4]
# 22| ValueCategory = lvalue
# 22| getAssociationType(1): [TypeName] const char *
# 22| Type = [PointerType] const char *
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(1): string
# 22| Type = [ArrayType] char[7]
# 22| Value = [StringLiteral] "string"
# 22| ValueCategory = lvalue
# 22| getAssociationType(2): [TypeName] void
# 22| Type = [VoidType] void
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(2): unknown
# 22| Type = [ArrayType] char[8]
# 22| Value = [StringLiteral] "unknown"
# 22| ValueCategory = lvalue
# 22| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 22| Type = [CTypedefType] MYINT
# 22| ValueCategory = prvalue(load)
# 23| getStmt(6): [ExprStmt] ExprStmt
# 23| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue
# 23| getArgument(0): s is %s\n
# 23| Type = [ArrayType] char[9]
# 23| Value = [StringLiteral] "s is %s\n"
# 23| ValueCategory = lvalue
# 23| getArgument(1): string
# 23| Type = [ArrayType] char[7]
# 23| Value = [StringLiteral] "string"
# 23| ValueCategory = lvalue
# 23| getArgument(0).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
# 23| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 23| Type = [CharPointerType] char *
# 23| ValueCategory = prvalue
# 23| getExpr(): [C11GenericExpr] _Generic
# 23| Type = [ArrayType] char[7]
# 23| Value = [C11GenericExpr] string
# 23| ValueCategory = lvalue
# 23| getControllingExpr(): [VariableAccess] s
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue(load)
# 23| getAssociationType(0): [TypeName] int
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(0): int
# 23| Type = [ArrayType] char[4]
# 23| Value = [StringLiteral] "int"
# 23| ValueCategory = lvalue
# 23| getAssociationType(1): [TypeName] const char *
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(1): [ReuseExpr] reuse of string
# 23| Type = [ArrayType] char[7]
# 23| ValueCategory = lvalue
# 23| getAssociationType(2): [TypeName] void
# 23| Type = [VoidType] void
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(2): unknown
# 23| Type = [ArrayType] char[8]
# 23| Value = [StringLiteral] "unknown"
# 23| ValueCategory = lvalue
# 23| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue(load)
# 24| getStmt(7): [ExprStmt] ExprStmt
# 24| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 24| Type = [IntType] int
# 24| ValueCategory = prvalue
# 24| getArgument(0): f is %s\n
# 24| Type = [ArrayType] char[9]
# 24| Value = [StringLiteral] "f is %s\n"
# 24| ValueCategory = lvalue
# 24| getArgument(1): unknown
# 24| Type = [ArrayType] char[8]
# 24| Value = [StringLiteral] "unknown"
# 24| ValueCategory = lvalue
# 24| getArgument(0).getFullyConverted(): [CStyleCast] (const char *)...
# 24| Conversion = [PointerConversion] pointer conversion
# 24| Type = [PointerType] const char *
# 24| ValueCategory = prvalue
# 24| getExpr(): [ArrayToPointerConversion] array to pointer conversion
# 24| Type = [CharPointerType] char *
# 24| ValueCategory = prvalue
# 24| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 24| Type = [CharPointerType] char *
# 24| ValueCategory = prvalue
# 24| getExpr(): [C11GenericExpr] _Generic
# 24| Type = [ArrayType] char[8]
# 24| Value = [C11GenericExpr] unknown
# 24| ValueCategory = lvalue
# 24| getControllingExpr(): [VariableAccess] f
# 24| Type = [PointerType] float ***
# 24| ValueCategory = prvalue(load)
# 24| getAssociationType(0): [TypeName] int
# 24| Type = [IntType] int
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(0): int
# 24| Type = [ArrayType] char[4]
# 24| Value = [StringLiteral] "int"
# 24| ValueCategory = lvalue
# 24| getAssociationType(1): [TypeName] const char *
# 24| Type = [PointerType] const char *
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(1): string
# 24| Type = [ArrayType] char[7]
# 24| Value = [StringLiteral] "string"
# 24| ValueCategory = lvalue
# 24| getAssociationType(2): [TypeName] void
# 24| Type = [VoidType] void
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(2): [ReuseExpr] reuse of unknown
# 24| Type = [ArrayType] char[8]
# 24| ValueCategory = lvalue
# 24| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 24| Type = [PointerType] float ***
# 24| ValueCategory = prvalue(load)
# 25| getStmt(8): [ReturnStmt] return ...
#-----| getExpr(): [Literal] 0
#-----| Type = [IntType] int
#-----| Value = [Literal] 0
#-----| ValueCategory = prvalue
generic.cpp:
# 4| [FormattingFunction,TopLevelFunction] int printf(char const*)
# 4| <params>:
# 4| getParameter(0): [Parameter] format
# 4| Type = [PointerType] const char *
# 15| [TopLevelFunction] int main()
# 15| <params>:
# 16| getEntryPoint(): [BlockStmt] { ... }
# 17| getStmt(0): [DeclStmt] declaration
# 17| getDeclarationEntry(0): [VariableDeclarationEntry] definition of i
# 17| Type = [IntType] int
# 18| getStmt(1): [DeclStmt] declaration
# 18| getDeclarationEntry(0): [VariableDeclarationEntry] definition of m
# 18| Type = [CTypedefType] MYINT
# 19| getStmt(2): [DeclStmt] declaration
# 19| getDeclarationEntry(0): [VariableDeclarationEntry] definition of s
# 19| Type = [PointerType] const char *
# 20| getStmt(3): [DeclStmt] declaration
# 20| getDeclarationEntry(0): [VariableDeclarationEntry] definition of f
# 20| Type = [PointerType] float ***
# 22| getStmt(4): [ExprStmt] ExprStmt
# 22| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 22| Type = [IntType] int
# 22| ValueCategory = prvalue
# 22| getArgument(0): i is %s\n
# 22| Type = [ArrayType] const char[9]
# 22| Value = [StringLiteral] "i is %s\n"
# 22| ValueCategory = lvalue
# 22| getArgument(1): int
# 22| Type = [ArrayType] const char[4]
# 22| Value = [StringLiteral] "int"
# 22| ValueCategory = lvalue
# 22| getArgument(0).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 22| Type = [PointerType] const char *
# 22| ValueCategory = prvalue
# 22| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 22| Type = [PointerType] const char *
# 22| ValueCategory = prvalue
# 22| getExpr(): [C11GenericExpr] _Generic
# 22| Type = [ArrayType] const char[4]
# 22| Value = [C11GenericExpr] int
# 22| ValueCategory = lvalue
# 22| getControllingExpr(): [VariableAccess] i
# 22| Type = [IntType] int
# 22| ValueCategory = lvalue
# 22| getAssociationType(0): [TypeName] int
# 22| Type = [IntType] int
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(0): [ReuseExpr] reuse of int
# 22| Type = [ArrayType] const char[4]
# 22| ValueCategory = lvalue
# 22| getAssociationType(1): [TypeName] const char *
# 22| Type = [PointerType] const char *
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(1): string
# 22| Type = [ArrayType] const char[7]
# 22| Value = [StringLiteral] "string"
# 22| ValueCategory = lvalue
# 22| getAssociationType(2): [TypeName] void
# 22| Type = [VoidType] void
# 22| ValueCategory = prvalue
# 22| getAssociationExpr(2): unknown
# 22| Type = [ArrayType] const char[8]
# 22| Value = [StringLiteral] "unknown"
# 22| ValueCategory = lvalue
# 22| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 22| Type = [IntType] int
# 22| ValueCategory = lvalue
# 23| getStmt(5): [ExprStmt] ExprStmt
# 23| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue
# 23| getArgument(0): c is %s\n
# 23| Type = [ArrayType] const char[9]
# 23| Value = [StringLiteral] "c is %s\n"
# 23| ValueCategory = lvalue
# 23| getArgument(1): int
# 23| Type = [ArrayType] const char[4]
# 23| Value = [StringLiteral] "int"
# 23| ValueCategory = lvalue
# 23| getArgument(0).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue
# 23| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue
# 23| getExpr(): [C11GenericExpr] _Generic
# 23| Type = [ArrayType] const char[4]
# 23| Value = [C11GenericExpr] int
# 23| ValueCategory = lvalue
# 23| getControllingExpr(): [VariableAccess] m
# 23| Type = [CTypedefType] MYINT
# 23| ValueCategory = lvalue
# 23| getAssociationType(0): [TypeName] int
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(0): [ReuseExpr] reuse of int
# 23| Type = [ArrayType] const char[4]
# 23| ValueCategory = lvalue
# 23| getAssociationType(1): [TypeName] const char *
# 23| Type = [PointerType] const char *
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(1): string
# 23| Type = [ArrayType] const char[7]
# 23| Value = [StringLiteral] "string"
# 23| ValueCategory = lvalue
# 23| getAssociationType(2): [TypeName] void
# 23| Type = [VoidType] void
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(2): unknown
# 23| Type = [ArrayType] const char[8]
# 23| Value = [StringLiteral] "unknown"
# 23| ValueCategory = lvalue
# 23| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 23| Type = [CTypedefType] MYINT
# 23| ValueCategory = lvalue
# 24| getStmt(6): [ExprStmt] ExprStmt
# 24| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 24| Type = [IntType] int
# 24| ValueCategory = prvalue
# 24| getArgument(0): s is %s\n
# 24| Type = [ArrayType] const char[9]
# 24| Value = [StringLiteral] "s is %s\n"
# 24| ValueCategory = lvalue
# 24| getArgument(1): string
# 24| Type = [ArrayType] const char[7]
# 24| Value = [StringLiteral] "string"
# 24| ValueCategory = lvalue
# 24| getArgument(0).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 24| Type = [PointerType] const char *
# 24| ValueCategory = prvalue
# 24| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 24| Type = [PointerType] const char *
# 24| ValueCategory = prvalue
# 24| getExpr(): [C11GenericExpr] _Generic
# 24| Type = [ArrayType] const char[7]
# 24| Value = [C11GenericExpr] string
# 24| ValueCategory = lvalue
# 24| getControllingExpr(): [VariableAccess] s
# 24| Type = [PointerType] const char *
# 24| ValueCategory = lvalue
# 24| getAssociationType(0): [TypeName] int
# 24| Type = [IntType] int
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(0): int
# 24| Type = [ArrayType] const char[4]
# 24| Value = [StringLiteral] "int"
# 24| ValueCategory = lvalue
# 24| getAssociationType(1): [TypeName] const char *
# 24| Type = [PointerType] const char *
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(1): [ReuseExpr] reuse of string
# 24| Type = [ArrayType] const char[7]
# 24| ValueCategory = lvalue
# 24| getAssociationType(2): [TypeName] void
# 24| Type = [VoidType] void
# 24| ValueCategory = prvalue
# 24| getAssociationExpr(2): unknown
# 24| Type = [ArrayType] const char[8]
# 24| Value = [StringLiteral] "unknown"
# 24| ValueCategory = lvalue
# 24| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 24| Type = [PointerType] const char *
# 24| ValueCategory = lvalue
# 25| getStmt(7): [ExprStmt] ExprStmt
# 25| getExpr(): [FormattingFunctionCall,FunctionCall] call to printf
# 25| Type = [IntType] int
# 25| ValueCategory = prvalue
# 25| getArgument(0): f is %s\n
# 25| Type = [ArrayType] const char[9]
# 25| Value = [StringLiteral] "f is %s\n"
# 25| ValueCategory = lvalue
# 25| getArgument(1): unknown
# 25| Type = [ArrayType] const char[8]
# 25| Value = [StringLiteral] "unknown"
# 25| ValueCategory = lvalue
# 25| getArgument(0).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 25| Type = [PointerType] const char *
# 25| ValueCategory = prvalue
# 25| getArgument(1).getFullyConverted(): [ArrayToPointerConversion] array to pointer conversion
# 25| Type = [PointerType] const char *
# 25| ValueCategory = prvalue
# 25| getExpr(): [C11GenericExpr] _Generic
# 25| Type = [ArrayType] const char[8]
# 25| Value = [C11GenericExpr] unknown
# 25| ValueCategory = lvalue
# 25| getControllingExpr(): [VariableAccess] f
# 25| Type = [PointerType] float ***
# 25| ValueCategory = lvalue
# 25| getAssociationType(0): [TypeName] int
# 25| Type = [IntType] int
# 25| ValueCategory = prvalue
# 25| getAssociationExpr(0): int
# 25| Type = [ArrayType] const char[4]
# 25| Value = [StringLiteral] "int"
# 25| ValueCategory = lvalue
# 25| getAssociationType(1): [TypeName] const char *
# 25| Type = [PointerType] const char *
# 25| ValueCategory = prvalue
# 25| getAssociationExpr(1): string
# 25| Type = [ArrayType] const char[7]
# 25| Value = [StringLiteral] "string"
# 25| ValueCategory = lvalue
# 25| getAssociationType(2): [TypeName] void
# 25| Type = [VoidType] void
# 25| ValueCategory = prvalue
# 25| getAssociationExpr(2): [ReuseExpr] reuse of unknown
# 25| Type = [ArrayType] const char[8]
# 25| ValueCategory = lvalue
# 25| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 25| Type = [PointerType] float ***
# 25| ValueCategory = lvalue
# 26| getStmt(8): [ReturnStmt] return ...
#-----| getExpr(): [Literal] 0
#-----| Type = [IntType] int
#-----| Value = [Literal] 0
#-----| ValueCategory = prvalue

View File

@@ -0,0 +1 @@
semmle/code/cpp/PrintAST.ql

View File

@@ -0,0 +1,8 @@
| generic.c:21:22:21:32 | _Generic | generic.c:21:22:21:32 | describe(val) |
| generic.c:22:22:22:32 | _Generic | generic.c:22:22:22:32 | describe(val) |
| generic.c:23:22:23:32 | _Generic | generic.c:23:22:23:32 | describe(val) |
| generic.c:24:22:24:32 | _Generic | generic.c:24:22:24:32 | describe(val) |
| generic.cpp:22:22:22:32 | _Generic | generic.cpp:22:22:22:32 | describe(val) |
| generic.cpp:23:22:23:32 | _Generic | generic.cpp:23:22:23:32 | describe(val) |
| generic.cpp:24:22:24:32 | _Generic | generic.cpp:24:22:24:32 | describe(val) |
| generic.cpp:25:22:25:32 | _Generic | generic.cpp:25:22:25:32 | describe(val) |

View File

@@ -0,0 +1,5 @@
import cpp
from C11GenericExpr g, MacroInvocation m
where m.getAnExpandedElement() = g
select g, m

View File

@@ -4207,6 +4207,24 @@ generic.c:
# 3| Type = [IntType] int
# 3| Value = [Literal] 1
# 3| ValueCategory = prvalue
# 3| getLeftOperand().getFullyConverted(): [C11GenericExpr] _Generic
# 3| Type = [IntType] unsigned int
# 3| ValueCategory = prvalue(load)
# 3| getControllingExpr(): [VariableAccess] r
# 3| Type = [IntType] unsigned int
# 3| ValueCategory = prvalue(load)
# 3| getAssociationType(0): [TypeName] unsigned int
# 3| Type = [IntType] unsigned int
# 3| ValueCategory = prvalue
# 3| getAssociationExpr(0): [ReuseExpr] reuse of x
# 3| Type = [IntType] unsigned int
# 3| ValueCategory = lvalue
# 3| getAssociationType(1): [TypeName] int
# 3| Type = [IntType] int
# 3| ValueCategory = prvalue
# 3| getAssociationExpr(1): [VariableAccess] y
# 3| Type = [IntType] int
# 3| ValueCategory = lvalue
# 3| getRightOperand().getFullyConverted(): [CStyleCast] (unsigned int)...
# 3| Conversion = [IntegralConversion] integral conversion
# 3| Type = [IntType] unsigned int
@@ -4231,6 +4249,29 @@ generic.c:
# 16| getExpr(): [ArrayToPointerConversion] array to pointer conversion
# 16| Type = [CharPointerType] char *
# 16| ValueCategory = prvalue
# 16| getExpr(): [C11GenericExpr] _Generic
# 16| Type = [ArrayType] char[4]
# 16| Value = [C11GenericExpr] int
# 16| ValueCategory = lvalue
# 16| getControllingExpr(): [VariableAccess] i
# 16| Type = [IntType] int
# 16| ValueCategory = prvalue(load)
# 16| getAssociationType(0): [TypeName] int
# 16| Type = [IntType] int
# 16| ValueCategory = prvalue
# 16| getAssociationExpr(0): [ReuseExpr] reuse of int
# 16| Type = [ArrayType] char[4]
# 16| ValueCategory = lvalue
# 16| getAssociationType(1): [TypeName] void
# 16| Type = [VoidType] void
# 16| ValueCategory = prvalue
# 16| getAssociationExpr(1): unknown
# 16| Type = [ArrayType] char[8]
# 16| Value = [StringLiteral] "unknown"
# 16| ValueCategory = lvalue
# 16| getControllingExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 16| Type = [IntType] int
# 16| ValueCategory = prvalue(load)
# 19| [TopLevelFunction] char const* c11_generic_test_with_constant_and_no_macro()
# 19| <params>:
# 20| getEntryPoint(): [BlockStmt] { ... }
@@ -4249,6 +4290,26 @@ generic.c:
# 23| getExpr(): [ArrayToPointerConversion] array to pointer conversion
# 23| Type = [CharPointerType] char *
# 23| ValueCategory = prvalue
# 23| getExpr(): [C11GenericExpr] _Generic
# 23| Type = [ArrayType] char[4]
# 23| Value = [C11GenericExpr] int
# 23| ValueCategory = lvalue
# 23| getControllingExpr(): [VariableAccess] i
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue(load)
# 23| getAssociationType(0): [TypeName] int
# 23| Type = [IntType] int
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(0): [ReuseExpr] reuse of int
# 23| Type = [ArrayType] char[4]
# 23| ValueCategory = lvalue
# 23| getAssociationType(1): [TypeName] void
# 23| Type = [VoidType] void
# 23| ValueCategory = prvalue
# 23| getAssociationExpr(1): unknown
# 23| Type = [ArrayType] char[8]
# 23| Value = [StringLiteral] "unknown"
# 23| ValueCategory = lvalue
# 26| [TopLevelFunction] void c11_generic_test_test_with_cast(int)
# 26| <params>:
# 26| getParameter(0): [Parameter] y
@@ -4267,10 +4328,28 @@ generic.c:
# 28| getRValue(): [VariableAccess] y
# 28| Type = [IntType] int
# 28| ValueCategory = prvalue(load)
# 28| getRValue().getFullyConverted(): [CStyleCast] (unsigned int)...
# 28| Conversion = [IntegralConversion] integral conversion
# 28| getRValue().getFullyConverted(): [C11GenericExpr] _Generic
# 28| Type = [IntType] unsigned int
# 28| ValueCategory = prvalue
# 28| getControllingExpr(): [VariableAccess] r
# 28| Type = [IntType] unsigned int
# 28| ValueCategory = prvalue(load)
# 28| getAssociationType(0): [TypeName] unsigned int
# 28| Type = [IntType] unsigned int
# 28| ValueCategory = prvalue
# 28| getAssociationExpr(0): [ReuseExpr] reuse of y
# 28| Type = [IntType] int
# 28| ValueCategory = prvalue
# 28| getAssociationType(1): [TypeName] int
# 28| Type = [IntType] int
# 28| ValueCategory = prvalue
# 28| getAssociationExpr(1): [VariableAccess] y
# 28| Type = [IntType] int
# 28| ValueCategory = lvalue
# 28| getExpr(): [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*)

View File

@@ -2991,7 +2991,7 @@ generic.c:
# 14| r14_1(glval<int>) = VariableAddress[i] :
# 14| m14_2(int) = Uninitialized[i] : &:r14_1
# 16| r16_1(glval<char *>) = VariableAddress[#return] :
# 16| r16_2(glval<char[4]>) = StringConstant[int] :
# 16| r16_2(glval<char[4]>) = Constant[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
@@ -3009,7 +3009,7 @@ generic.c:
# 21| r21_1(glval<int>) = VariableAddress[i] :
# 21| m21_2(int) = Uninitialized[i] : &:r21_1
# 23| r23_1(glval<char *>) = VariableAddress[#return] :
# 23| r23_2(glval<char[4]>) = StringConstant["int"] :
# 23| r23_2(glval<char[4]>) = Constant[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

View File

@@ -2763,7 +2763,7 @@ generic.c:
# 14| r14_1(glval<int>) = VariableAddress[i] :
# 14| mu14_2(int) = Uninitialized[i] : &:r14_1
# 16| r16_1(glval<char *>) = VariableAddress[#return] :
# 16| r16_2(glval<char[4]>) = StringConstant[int] :
# 16| r16_2(glval<char[4]>) = Constant[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
@@ -2780,7 +2780,7 @@ generic.c:
# 21| r21_1(glval<int>) = VariableAddress[i] :
# 21| mu21_2(int) = Uninitialized[i] : &:r21_1
# 23| r23_1(glval<char *>) = VariableAddress[#return] :
# 23| r23_2(glval<char[4]>) = StringConstant["int"] :
# 23| r23_2(glval<char[4]>) = Constant[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