diff --git a/cpp/ql/lib/semmle/code/cpp/exprs/Literal.qll b/cpp/ql/lib/semmle/code/cpp/exprs/Literal.qll index 750afcbd7f5..8e6ed1a709b 100644 --- a/cpp/ql/lib/semmle/code/cpp/exprs/Literal.qll +++ b/cpp/ql/lib/semmle/code/cpp/exprs/Literal.qll @@ -208,7 +208,8 @@ class ClassAggregateLiteral extends AggregateLiteral { /** * Gets the expression within the aggregate literal that is used to initialize - * field `field` the `repitition`'th time in the initializer list, if present. + * field `field`, if present. The expression is the `position`'th entry in the + * aggregate literal. * * For example, if `aggr` represents the initialization literal `{.x = 1234, .x = 5678}` in * ```cpp @@ -217,10 +218,10 @@ class ClassAggregateLiteral extends AggregateLiteral { * ``` * then `aggr.getFieldExpr(x, 0)` gives `1234`, and `aggr.getFieldExpr(x, 1)` gives `5678`. */ - Expr getFieldExpr(Field field, int repitition) { + Expr getFieldExpr(Field field, int position) { field = classType.getAField() and aggregate_field_init(underlyingElement(this), unresolveElement(result), unresolveElement(field), - repitition) + position) } /** @@ -309,7 +310,8 @@ class ArrayOrVectorAggregateLiteral extends AggregateLiteral { /** * Gets the expression within the aggregate literal that is used to initialize - * element `elementIndex` the `repitition`'th time in the initializer list, if present. + * element `elementIndex`, if present. The expression is the `position`'th entry + * in the aggregate literal. * * For example, if `a` represents the initialization literal `{[0] = 1234, [0] = 5678}` in * ```cpp @@ -317,8 +319,8 @@ class ArrayOrVectorAggregateLiteral extends AggregateLiteral { * ``` * then `a.getElementExpr(0, 0)` gives `1234`, and `a.getElementExpr(0, 1)` gives `5678`. */ - Expr getElementExpr(int elementIndex, int repitition) { - aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, repitition) + Expr getElementExpr(int elementIndex, int position) { + aggregate_array_init(underlyingElement(this), unresolveElement(result), elementIndex, position) } /** diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll index 70061b453a9..c320c7aa49e 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll @@ -619,11 +619,11 @@ newtype TTranslatedElement = ) } or // The initialization of a field via a member of an initializer list. - TTranslatedExplicitFieldInitialization(Expr ast, Field field, Expr expr, int repitition) { + TTranslatedExplicitFieldInitialization(Expr ast, Field field, Expr expr, int position) { exists(ClassAggregateLiteral initList | not ignoreExpr(initList) and ast = initList and - expr = initList.getFieldExpr(field, repitition).getFullyConverted() + expr = initList.getFieldExpr(field, position).getFullyConverted() ) or exists(ConstructorFieldInit init | @@ -631,7 +631,7 @@ newtype TTranslatedElement = ast = init and field = init.getTarget() and expr = init.getExpr().getFullyConverted() and - repitition = 0 + position = -1 ) } or // The value initialization of a field due to an omitted member of an @@ -645,10 +645,10 @@ newtype TTranslatedElement = } or // The initialization of an array element via a member of an initializer list. TTranslatedExplicitElementInitialization( - ArrayOrVectorAggregateLiteral initList, int elementIndex, int repitition + ArrayOrVectorAggregateLiteral initList, int elementIndex, int position ) { not ignoreExpr(initList) and - exists(initList.getElementExpr(elementIndex, repitition)) + exists(initList.getElementExpr(elementIndex, position)) } or // The value initialization of a range of array elements that were omitted // from an initializer list. diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedInitialization.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedInitialization.qll index 315d66b1a4c..5e50c834d67 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedInitialization.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedInitialization.qll @@ -206,7 +206,7 @@ class TranslatedClassListInitialization extends TranslatedListInitialization { fieldInit = getTranslatedFieldInitialization(expr, _) and fieldInit.getOrder() = ord | - fieldInit order by ord, fieldInit.getRepetitionIndex() + fieldInit order by ord, fieldInit.getPosition() ) } } @@ -224,7 +224,7 @@ class TranslatedArrayListInitialization extends TranslatedListInitialization { rank[id + 1](TranslatedElementInitialization init | init.getInitList() = expr | - init order by init.getElementIndex(), init.getRepetitionIndex() + init order by init.getElementIndex(), init.getPosition() ) } } @@ -525,11 +525,8 @@ abstract class TranslatedFieldInitialization extends TranslatedElement { final Field getField() { result = field } - /** - * Gets the index of this initialization, if the field is mentioned - * multiple times in the initializer. - */ - int getRepetitionIndex() { result = 0 } + /** Gets the position in the initializer list, or `-1` if the initialization is implicit. */ + int getPosition() { result = -1 } } /** @@ -540,10 +537,10 @@ class TranslatedExplicitFieldInitialization extends TranslatedFieldInitializatio InitializationContext, TTranslatedExplicitFieldInitialization { Expr expr; - int repitition; + int position; TranslatedExplicitFieldInitialization() { - this = TTranslatedExplicitFieldInitialization(ast, field, expr, repitition) + this = TTranslatedExplicitFieldInitialization(ast, field, expr, position) } override Instruction getTargetAddress() { result = getInstruction(getFieldAddressTag()) } @@ -566,7 +563,7 @@ class TranslatedExplicitFieldInitialization extends TranslatedFieldInitializatio result = getTranslatedInitialization(expr) } - override int getRepetitionIndex() { result = repitition } + override int getPosition() { result = position } } private string getZeroValue(Type type) { @@ -700,7 +697,7 @@ abstract class TranslatedElementInitialization extends TranslatedElement { abstract int getElementIndex(); - int getRepetitionIndex() { result = 0 } + int getPosition() { result = -1 } final InstructionTag getElementAddressTag() { result = InitializerElementAddressTag() } @@ -719,10 +716,10 @@ class TranslatedExplicitElementInitialization extends TranslatedElementInitializ TTranslatedExplicitElementInitialization, InitializationContext { int elementIndex; - int repetition; + int position; TranslatedExplicitElementInitialization() { - this = TTranslatedExplicitElementInitialization(initList, elementIndex, repetition) + this = TTranslatedExplicitElementInitialization(initList, elementIndex, position) } override Instruction getTargetAddress() { result = getInstruction(getElementAddressTag()) } @@ -745,12 +742,12 @@ class TranslatedExplicitElementInitialization extends TranslatedElementInitializ override int getElementIndex() { result = elementIndex } - override int getRepetitionIndex() { result = repetition } + override int getPosition() { result = position } TranslatedInitialization getInitialization() { result = getTranslatedInitialization(initList - .getElementExpr(elementIndex, repetition) + .getElementExpr(elementIndex, position) .getFullyConverted()) } }