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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42199x 42199x 42199x 1x 21101x 21101x 21101x 21101x 21101x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 63111x 21101x 21101x 20205x 20205x 21101x 21101x 432x 432x 432x 21101x 21101x 374x 374x 21101x 21101x 642x 229x 229x 413x 413x 413x 413x 642x 423x 423x 423x 423x 3037x 3037x 35x 35x 3002x 3037x 3037x 3037x 3037x 3037x 3037x 2932x 2932x 70x 3037x 6x 6x 64x 3037x 53x 53x 64x 3037x 40x 3037x 15x 15x 64x 64x 423x 413x 413x 413x 21101x 21101x 21101x 21101x 423x 423x 3037x 6x 6x 3037x 423x 423x 21101x 21101x 21101x 70x 71x 6x 6x 71x 64x 64x 21101x 21101x 21101x 40x 40x 10x 10x 30x 30x 40x 19x 40x 11x 11x 40x 30x 40x 2x 2x 2x 28x 28x 40x 17x 17x 40x 21101x 21101x 15x 15x 15x 4x 4x 11x 15x 15x 11x 11x 15x 4x 4x 15x 21101x 21101x 21101x 1104x 1104x 1104x 1755x 1755x 715x 715x 1040x 1755x 1755x 1755x 6x 6x 6x 6x 4x 4x 4x 6x 1040x 1040x 1755x 21101x 21101x 25x 25x 25x 25x 21101x 21101x 21101x 33x 29x 29x 4x 33x 33x 21101x 21101x 21101x 21101x 12x 1x 1x 11x 11x 11x 21101x 21101x 21101x 45x 45x 45x 45x 11x 11x 45x 45x 21101x 21101x 39x 39x 39x 21101x 21101x | import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import {Comment} from "../abap/2_statements/statements/_statement";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Issue} from "../issue";
import {IRule, IRuleMetadata, RuleTag} from "./_irule";
import {SyntaxLogic} from "../abap/5_syntax/syntax";
import {hasKeyCompare} from "../abap/5_syntax/_key_compare";
import {IObject} from "../objects/_iobject";
import {ABAPObject} from "../objects/_abap_object";
import {IRegistry} from "../_iregistry";
import {TableAccessType, TableKeyType, TableType} from "../abap/types/basic";
import {AbstractType} from "../abap/types/basic/_abstract_type";
import {ExpressionNode, StatementNode, TokenNode} from "../abap/nodes";
import {ABAPFile} from "../abap/abap_file";
import {ISpaghettiScope} from "../abap/5_syntax/_spaghetti_scope";
import {chainType, nodeChainType, operandChain} from "./_type_lookup";
import {Severity} from "../severity";
export class SequentialReadSortedConf extends BasicRuleConfig {
public severity?: Severity = Severity.Warning;
}
export class SequentialReadSorted implements IRule {
private reg: IRegistry;
private conf = new SequentialReadSortedConf();
public getMetadata(): IRuleMetadata {
return {
key: "sequential_read_sorted",
title: "Sequential read on a sorted table",
shortDescription: `A WHERE condition or a free key on a sorted table that does not use the primary key`,
extendedInformation: `A sorted table is read with a binary search only when the condition fixes an initial part of
its key. When the first key component is not compared with = or EQ (or IS INITIAL) in a part of the condition joined
by AND, every line is read, like on a standard table. The SAP check "Low Performance Operations on Internal Tables"
reports this as "Possible sequential read on a sorted table".
Checked: LOOP AT ... WHERE, DELETE ... WHERE, MODIFY ... WHERE, READ TABLE ... WITH KEY and table expressions
itab[ comp = ... ], on a table whose type is statically known as SORTED with explicit key components, using the
primary key.
Not checked: standard, hashed, generic and unknown table types, tables with a default key, a dynamic WHERE or
dynamic components, WITH TABLE KEY, a named key (USING KEY, KEY ... COMPONENTS) other than primary_key,
READ TABLE ... BINARY SEARCH and access by index, including LOOP AT and DELETE with FROM or TO.
Fix: compare the first key component with =, change the key or the table type, or add a secondary key.
If the sequential read is intended, add the pseudo comment "#EC CI_SORTSEQ to the statement.`,
tags: [RuleTag.SingleFile, RuleTag.Performance],
pseudoComment: "EC CI_SORTSEQ",
badExample: `DATA lt_nodes TYPE SORTED TABLE OF ty_node WITH UNIQUE KEY path name.
LOOP AT lt_nodes INTO ls_node WHERE name = lv_name.
ENDLOOP.
DELETE lt_nodes WHERE path CP lv_pattern.`,
goodExample: `DATA lt_nodes TYPE SORTED TABLE OF ty_node WITH UNIQUE KEY path name.
LOOP AT lt_nodes INTO ls_node WHERE path = lv_path AND name = lv_name.
ENDLOOP.
DELETE lt_nodes WHERE path CP lv_pattern. "#EC CI_SORTSEQ`,
};
}
public getConfig() {
return this.conf;
}
public initialize(reg: IRegistry) {
this.reg = reg;
return this;
}
public setConfig(conf: SequentialReadSortedConf): void {
this.conf = conf;
}
public run(obj: IObject): Issue[] {
if (!(obj instanceof ABAPObject) || obj.getType() === "INTF") {
return [];
}
const issues: Issue[] = [];
let spaghetti: ISpaghettiScope | undefined = undefined;
for (const file of obj.getABAPFiles()) {
const statements = file.getStatements();
const pseudoRows = this.findPseudoCommentRows(statements);
for (const statement of statements) {
const get = statement.get();
if (get instanceof Comment) {
continue;
}
const isWhere = get instanceof Statements.Loop
|| get instanceof Statements.DeleteInternal
|| get instanceof Statements.ModifyInternal;
const isRead = get instanceof Statements.ReadTable;
if (isWhere === false
&& isRead === false
&& statement.findFirstExpression(Expressions.TableExpression) === undefined) {
continue;
}
if (this.hasPseudoComment(statement, pseudoRows)) {
continue;
}
if (spaghetti === undefined) {
spaghetti = new SyntaxLogic(this.reg, obj).run().spaghetti;
}
if (isWhere === true) {
this.checkWhere(statement, spaghetti, file, issues);
} else if (isRead === true) {
this.checkReadTable(statement, spaghetti, file, issues);
}
this.checkTableExpressions(statement, statement, spaghetti, file, issues);
}
}
return issues;
}
////////////////
private findPseudoCommentRows(statements: readonly StatementNode[]): Set<number> {
const rows = new Set<number>();
for (const s of statements) {
if (s.get() instanceof Comment && s.concatTokens().toUpperCase().includes(this.getMetadata().pseudoComment + "")) {
rows.add(s.getStart().getRow());
}
}
return rows;
}
/** the pseudo comment is accepted on any row of the statement */
private hasPseudoComment(statement: StatementNode, pseudoRows: Set<number>): boolean {
for (let row = statement.getStart().getRow(); row <= statement.getEnd().getRow(); row++) {
if (pseudoRows.has(row)) {
return true;
}
}
return false;
}
/** LOOP AT, DELETE and MODIFY with a static WHERE condition */
private checkWhere(statement: StatementNode, spaghetti: ISpaghettiScope, file: ABAPFile, issues: Issue[]) {
const cond = statement.findDirectExpression(Expressions.ComponentCond);
if (cond === undefined || this.usesPrimaryKey(statement) === false) {
return;
}
let table: ExpressionNode | undefined;
if (statement.get() instanceof Statements.Loop) {
table = statement.findDirectExpression(Expressions.LoopSource)?.getFirstChild() as ExpressionNode | undefined;
} else {
table = statement.findDirectExpression(Expressions.Target);
}
if (table === undefined) {
return;
}
if (!(statement.get() instanceof Statements.ModifyInternal)
&& (statement.findDirectTokenByText("FROM") !== undefined || statement.findDirectTokenByText("TO") !== undefined)) {
// an index range
return;
}
const first = this.firstKeyComponent(this.tableType(table, spaghetti, file));
if (first !== undefined && hasKeyCompare(cond, first) === false) {
this.report(statement, table.concatTokens(), first, file, issues);
}
}
private checkReadTable(statement: StatementNode, spaghetti: ISpaghettiScope, file: ABAPFile, issues: Issue[]) {
if (statement.findTokenSequencePosition("WITH", "KEY") === undefined
|| statement.findDirectTokenByText("COMPONENTS") !== undefined
|| statement.findDirectTokenByText("BINARY") !== undefined) {
return;
}
const compare = statement.findDirectExpression(Expressions.ComponentCompareSimple);
const table = statement.findDirectExpression(Expressions.SimpleSource2) ?? statement.findDirectExpression(Expressions.Source);
if (compare === undefined || table === undefined) {
return;
}
const first = this.firstKeyComponent(this.tableType(table, spaghetti, file));
if (first !== undefined && this.hasFreeKeyComponent(compare, first) === false) {
this.report(statement, table.concatTokens(), first, file, issues);
}
}
/** table expressions with a free key, itab[ comp = ... ], anywhere in the statement */
private checkTableExpressions(node: StatementNode | ExpressionNode, statement: StatementNode,
spaghetti: ISpaghettiScope, file: ABAPFile, issues: Issue[]) {
const children = node.getChildren();
for (let i = 0; i < children.length; i++) {
const child = children[i];
if (child instanceof TokenNode) {
continue;
}
if (child.get() instanceof Expressions.TableExpression
&& child instanceof ExpressionNode
&& (node.get() instanceof Expressions.FieldChain || node.get() instanceof Expressions.Target)
&& child.getChildren()[1]?.get() instanceof Expressions.ComponentChainSimple) {
// a free key, not an index and not a named key: KEY name [COMPONENTS]
const scope = spaghetti.lookupPosition(node.getFirstToken().getStart(), file.getFilename());
const first = this.firstKeyComponent(chainType(children.slice(0, i), scope, true));
if (first !== undefined && this.hasFreeKeyComponent(child, first) === false) {
const name = children.slice(0, i).map(c => c.concatTokens()).join("");
this.report(statement, name, first, file, issues);
}
}
this.checkTableExpressions(child, statement, spaghetti, file, issues);
}
}
private report(statement: StatementNode, name: string, first: string, file: ABAPFile, issues: Issue[]) {
const message = `Sequential read on sorted table ${name}, the first key component ${first} is not compared with =, ` +
`add it to the condition or "#EC CI_SORTSEQ`;
issues.push(Issue.atStatement(file, statement, message, this.getMetadata().key, this.conf.severity));
}
/** false if USING KEY names a key other than the primary key, or a dynamic one */
private usesPrimaryKey(statement: StatementNode): boolean {
if (statement.findDirectTokenByText("USING") === undefined) {
return true;
}
const keyName = statement.findExpressionAfterToken("KEY");
return keyName?.get() instanceof Expressions.SimpleName && keyName.concatTokens().toUpperCase() === "PRIMARY_KEY";
}
/** true if a free key, ComponentCompareSimple or the fields of a TableExpression, specifies the component;
* also true when a component is dynamic, as it cannot be decided */
private hasFreeKeyComponent(node: ExpressionNode, component: string): boolean {
if (node.findDirectExpression(Expressions.Dynamic) !== undefined) {
return true;
}
return node.findDirectExpressions(Expressions.ComponentChainSimple)
.some(c => c.concatTokens().toUpperCase().replace(/\s+/g, "") === component);
}
/** The first component of the primary key of a sorted table with explicit key components, else undefined */
private firstKeyComponent(type: AbstractType | undefined): string | undefined {
if (!(type instanceof TableType)
|| type.getAccessType() !== TableAccessType.sorted
|| type.getOptions().keyType !== TableKeyType.user
|| type.isGeneric()) {
return undefined;
}
return type.getOptions().primaryKey?.keyFields[0]?.toUpperCase();
}
private tableType(node: ExpressionNode, spaghetti: ISpaghettiScope, file: ABAPFile): AbstractType | undefined {
const chain = operandChain(node);
return chain === undefined ? undefined : nodeChainType(chain, spaghetti, file, true);
}
}
|