Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21099x 21099x 21099x 21099x 21099x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 204504x 21099x 21099x 20205x 20205x 21099x 21099x 374x 374x 21099x 21099x 419x 419x 419x 21099x 21099x 629x 208x 208x 421x 421x 629x 10x 10x 411x 411x 629x 421x 2534x 2414x 2414x 2534x 2534x 2534x 2534x 2534x 2534x 2534x 2534x 83x 83x 37x 37x 2534x 37x 37x 2534x 19x 19x 18x 18x 18x 18x 18x 421x 411x 411x 411x 21099x 21099x 21099x 21099x 76x 76x 76x 76x 76x 8x 8x 8x 8x 3x 3x 5x 5x 5x 73x 76x 147x 147x 7x 7x 1x 1x 6x 147x 37x 1x 1x 36x 140x 39x 39x 13x 13x 26x 26x 103x 44x 64x 6x 20x 14x 14x 64x 147x 12x 12x 52x 52x 46x 46x 46x 21099x 21099x 70x 70x 70x 21099x 21099x 21099x 21099x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 21099x 21099x 6x 6x 6x 6x 6x 6x 2x 2x 6x 6x 21099x | import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {ISpaghettiScopeNode} from "../abap/5_syntax/_spaghetti_scope";
import {Constant as ConstantSyntax} from "../abap/5_syntax/expressions/constant";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {ExpressionNode, TokenNode} from "../abap/nodes";
import {MethodDefinition} from "../abap/types";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {Integer8Type, IntegerType, PackedType} from "../abap/types/basic";
import {Issue} from "../issue";
import {ABAPObject} from "../objects/_abap_object";
import {IObject} from "../objects/_iobject";
import {BasicRuleConfig} from "./_basic_rule_config";
import {IRule, IRuleMetadata} from "./_irule";
import {IRegistry} from "../_iregistry";
import {chainType, resolvedAt, typedIdentifierAt} from "./_type_lookup";
import {withoutParentheses} from "../abap/5_syntax/_parenthesized_operand";
export class InlinePackedComputationConf extends BasicRuleConfig {
}
export class InlinePackedComputation implements IRule {
private reg: IRegistry;
private conf = new InlinePackedComputationConf();
public getMetadata(): IRuleMetadata {
return {
key: "inline_packed_computation",
title: "Inline declaration from a packed computation",
shortDescription: `Finds inline declarations whose value is an arithmetic expression with calculation type p`,
extendedInformation: `The result of a computation with calculation type p has no length and number of decimals
that a declaration could take over, so DATA( ) or FINAL( ) gets the type p LENGTH 8 DECIMALS 0 and the
syntax check warns "For the result of a computation with type P, the type P(8,0) is used here implicitly".
Decimals of the operands are lost, and so are digits beyond the length.
Reported when at least one operand is packed and all other operands are of type i, int8 or p, also with
brackets around the whole expression. VALUE, CONV and EXACT operands count with their type, VALUE # only with
a single operand, "VALUE #( itab[ 1 ]-comp OPTIONAL )". Operands of other types, generic or unknown types,
other constructor expressions like COND and SWITCH, and the operator ** are not reported.
Declare the variable with the operands' type, or name the type with CONV.`,
tags: [],
badExample: `DATA a TYPE p LENGTH 8 DECIMALS 2.
DATA b TYPE p LENGTH 8 DECIMALS 2.
DATA(diff) = a - b.`,
goodExample: `DATA a TYPE p LENGTH 8 DECIMALS 2.
DATA b TYPE p LENGTH 8 DECIMALS 2.
DATA diff LIKE a.
diff = a - b.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: InlinePackedComputationConf) {
this.conf = conf;
}
public initialize(reg: IRegistry) {
this.reg = reg;
return this;
}
public run(obj: IObject): readonly Issue[] {
if (!(obj instanceof ABAPObject)) {
return [];
}
const syntax = new SyntaxLogic(this.reg, obj).run();
if (syntax.issues.length > 0) {
return [];
}
const issues: Issue[] = [];
for (const file of obj.getABAPFiles()) {
for (const statement of file.getStatements()) {
if (!(statement.get() instanceof Statements.Move)) {
continue;
}
const inline = statement.findDirectExpression(Expressions.Target)?.findDirectExpression(Expressions.InlineData);
const name = inline?.findDirectExpression(Expressions.TargetField)?.getFirstToken();
// "( a + b )" around the whole right-hand side, the brackets do not change the calculation
const right = statement.findDirectExpression(Expressions.Source);
const source = right && withoutParentheses(right);
if (name === undefined
|| source === undefined
|| source.findDirectExpression(Expressions.ArithOperator) === undefined) {
continue;
}
const scope = syntax.spaghetti.lookupPosition(name.getStart(), file.getFilename());
if (scope === undefined) {
continue;
}
const operands = this.operandTypes(source, scope);
if (operands === undefined || operands.some(t => t instanceof PackedType) === false) {
continue;
}
const message = `"${name.getStr()}" gets type p LENGTH 8 DECIMALS 0 from this packed computation, ` +
`declare it with the operands' type or use CONV`;
issues.push(Issue.atToken(file, name, message, this.getMetadata().key, this.conf.severity));
}
}
return issues;
}
/** The types of the operands of an arithmetic expression, undefined when an operand is not of type
* i, int8 or p, or is not known */
private operandTypes(source: ExpressionNode, scope: ISpaghettiScopeNode): AbstractType[] | undefined {
const ret: AbstractType[] = [];
let children = source.getChildren();
const first = children[0];
if (first instanceof TokenNode && ["VALUE", "CONV", "EXACT"].includes(first.getFirstToken().getStr().toUpperCase())) {
// a constructor operand, followed by the rest of the chain after its closing parenthesis
const close = children.findIndex((child, index) => index > 2 && child instanceof TokenNode);
const found = this.constructorType(source, scope);
if (close < 0 || found === undefined || this.isCalculationOperand(found) === false) {
return undefined;
}
ret.push(found);
children = children.slice(close + 1);
}
for (const child of children) {
let found: AbstractType | undefined = undefined;
if (child instanceof TokenNode) {
// parentheses and a sign, anything else is a constructor expression or a string operator
if (["(", ")", "+", "-"].includes(child.getFirstToken().getStr()) === false) {
return undefined;
}
continue;
} else if (child.get() instanceof Expressions.ArithOperator) {
if (["+", "-", "*", "/", "DIV", "MOD"].includes(child.concatTokens().toUpperCase()) === false) {
return undefined;
}
continue;
} else if (child.get() instanceof Expressions.Source) {
const nested = this.operandTypes(child, scope);
if (nested === undefined) {
return undefined;
}
ret.push(...nested);
continue;
} else if (child.get() instanceof Expressions.FieldChain) {
found = chainType(child.getChildren(), scope);
} else if (child.get() instanceof Expressions.MethodCallChain) {
found = this.methodCallType(child, scope);
} else if (child.get() instanceof Expressions.Constant) {
found = ConstantSyntax.runSyntax(child);
}
if (found === undefined || this.isCalculationOperand(found) === false) {
return undefined;
}
ret.push(found);
}
return ret;
}
private isCalculationOperand(type: AbstractType): boolean {
return type.isGeneric() === false
&& (type instanceof PackedType || type instanceof IntegerType || type instanceof Integer8Type);
}
/** The type of VALUE, CONV or EXACT as the syntax check inferred it, with # only VALUE with a single
* operand in the body, "VALUE #( itab[ 1 ]-comp OPTIONAL )" */
private constructorType(source: ExpressionNode, scope: ISpaghettiScopeNode): AbstractType | undefined {
const typeName = source.findDirectExpression(Expressions.TypeNameOrInfer);
if (typeName === undefined) {
return undefined;
}
if (typeName.concatTokens() === "#") {
// with DEFAULT the syntax check takes the type of the default value
const body = source.findDirectExpression(Expressions.ValueBody);
const children = body?.getChildren() || [];
if (children.length === 0
|| children.length > 2
|| !(children[0].get() instanceof Expressions.Source)
|| (children.length === 2 && children[1].concatTokens().toUpperCase() !== "OPTIONAL")) {
return undefined;
}
}
return typedIdentifierAt(scope, typeName.getFirstToken().getStart(), [ReferenceType.InferredType])?.getType();
}
private methodCallType(chain: ExpressionNode, scope: ISpaghettiScopeNode): AbstractType | undefined {
const last = chain.getLastChild();
if (!(last instanceof ExpressionNode) || !(last.get() instanceof Expressions.MethodCall)) {
return undefined;
}
const name = last.findDirectExpression(Expressions.MethodName)?.getFirstToken();
if (name === undefined) {
return undefined;
}
const method = resolvedAt(scope, name.getStart(), [ReferenceType.MethodReference]);
if (!(method instanceof MethodDefinition)) {
return undefined;
}
return method.getParameters().getReturning()?.getType();
}
}
|