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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42202x 42202x 42202x 42202x 42202x 42202x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21103x 21103x 21103x 21103x 21103x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 63065x 21103x 21103x 20205x 20205x 21103x 21103x 375x 375x 21103x 21103x 417x 417x 417x 21103x 21103x 627x 1x 1x 626x 627x 247x 247x 379x 379x 627x 518x 518x 40x 40x 13x 13x 40x 518x 379x 379x 21103x 21103x 21103x 21103x 40x 40x 40x 40x 40x 40x 1x 1x 39x 40x 5x 5x 34x 34x 34x 34x 34x 40x 5x 5x 2x 5x 3x 3x 5x 31x 40x 2x 2x 29x 29x 29x 29x 21x 21x 21x 16x 16x 5x 5x 13x 13x 13x 13x 40x 40x 40x 40x 40x 21103x 21103x 21103x 54x 54x 54x 1x 1x 54x 5x 3x 3x 5x 50x 50x 50x 50x 54x 65x 65x 65x 9x 9x 56x 65x 42x 1x 1x 42x 40x 40x 41x 42x 2x 2x 65x 14x 1x 1x 14x 9x 9x 65x 48x 48x 21103x 21103x 21103x 105x 105x 136x 72x 72x 72x 64x 64x 136x 136x 5x 5x 136x 105x 105x 21103x 21103x 21103x 20x 20x 20x 20x 5x 5x 15x 15x 20x 20x 8x 8x 20x 20x 21103x 21103x 21103x 21103x 7x 3x 3x 7x 7x 7x 21103x 21103x 8x 17x 15x 15x 2x 17x 17x 2x 2x 17x 6x 6x 21103x 21103x 21103x 21103x 21103x 6x 8x 8x 8x 8x 3x 3x 5x 8x 5x 5x 8x 5x 5x 5x 3x 3x 5x 2x 5x 5x 21103x 21103x 21103x 5x 5x 2x 2x 3x 5x 5x 21103x 21103x | import * as Expressions from "../abap/2_statements/expressions";
import * as Statements from "../abap/2_statements/statements";
import * as Structures from "../abap/3_structures/structures";
import {ExpressionNode, StatementNode, StructureNode} from "../abap/nodes";
import {AbstractToken} from "../abap/1_lexer/tokens/abstract_token";
import {Identifier} from "../abap/4_file_information/_identifier";
import {ReferenceType} from "../abap/5_syntax/_reference";
import {IdentifierMeta, TypedIdentifier} from "../abap/types/_typed_identifier";
import {IStatement} from "../abap/2_statements/statements/_statement";
import {Issue} from "../issue";
import {IObject} from "../objects/_iobject";
import {IRegistry} from "../_iregistry";
import {BasicRuleConfig} from "./_basic_rule_config";
import {Severity} from "../severity";
import {IRule, IRuleMetadata} from "./_irule";
import {ObjectAnalysis, Procedure, Variable, isImmutable, plainMove, singleTarget} from "./_procedure_analysis";
import {isLoopStructure} from "../abap/5_syntax/_loop_structures";
export class SuspiciousLoopConf extends BasicRuleConfig {
public severity?: Severity = Severity.Warning;
/** Report WHILE loops whose condition the loop body never changes, or sets to the same value in every iteration.
* The loop runs zero times, once or forever */
public unchangedCondition: boolean = true;
}
/** statements that leave the loop, or may leave it, wherever they are in the loop body */
const LEAVING: (new () => IStatement)[] = [Statements.Return, Statements.Raise, Statements.Message, Statements.Leave,
Statements.Submit, Statements.Stop, Statements.Reject];
/** statements which run no other code than the method calls they contain */
const CALLING: (new () => IStatement)[] = [Statements.Call, Statements.Move, Statements.If, Statements.ElseIf,
Statements.Case, Statements.When, Statements.Append, Statements.InsertInternal, Statements.Concatenate];
/** data read by a condition or a source */
interface IReads {
/** local variables, written only where the analysis sees it */
locals: Variable[];
/** attributes, parameters and other data that is not local, nor a constant */
others: Identifier[];
/** local reference variables followed by ->, the object they point to is read */
receivers: Variable[];
}
export class SuspiciousLoop implements IRule {
private reg: IRegistry;
private conf = new SuspiciousLoopConf();
public getMetadata(): IRuleMetadata {
return {
key: "suspicious_loop",
title: "Suspicious loop",
shortDescription: `Finds WHILE loops whose condition the loop body never changes`,
extendedInformation: `Option unchangedCondition: reports a WHILE loop when the loop body cannot change its condition
after the first iteration. Then the loop runs zero times, once or forever.
The condition may read local variables of the METHOD, FORM or FUNCTION, constants, builtin functions
like lines( ), and attributes and parameters without ->. Each local variable in it is either
* not written in the loop body, or
* only written by var = source. directly in the loop body, not in a nested block, where the source is
the same in every iteration: no method calls, field symbols or ->*, and it only reads constants,
local variables that the loop body does not write, and other data that the loop body does not write.
Attributes and parameters in the condition must not be written in the loop body either.
When the condition or a source reads data that is not local, the loop body must not call methods,
functions or forms, except instance methods via local reference variables, and must not write via
field symbols or ->*. A source that reads local->attribute also needs that the loop body calls no
method via that local.
Not reported: conditions with system fields like sy-subrc, field symbols, -> or method calls, local
variables whose address is taken, loop bodies with EXIT, RETURN, RAISE, MESSAGE, LEAVE, SUBMIT, STOP or
REJECT, and, when the condition variables are written, loop bodies with CONTINUE or CHECK.
Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
INCLUDE, test seams, enhancements or native SQL.`,
tags: [],
badExample: `DATA(lo_x) = previous.
WHILE lo_x IS BOUND.
result = result && lo_x->get_text( ).
lo_x = previous->previous.
ENDWHILE.`,
goodExample: `DATA(lo_x) = previous.
WHILE lo_x IS BOUND.
result = result && lo_x->get_text( ).
lo_x = lo_x->previous.
ENDWHILE.`,
};
}
public getConfig() {
return this.conf;
}
public setConfig(conf: SuspiciousLoopConf) {
this.conf = conf;
}
public initialize(reg: IRegistry) {
this.reg = reg;
return this;
}
public run(obj: IObject): readonly Issue[] {
if (this.conf.unchangedCondition === false) {
return [];
}
const analysis = ObjectAnalysis.run(this.reg, obj);
if (analysis === undefined) {
return [];
}
const issues: Issue[] = [];
for (const procedure of analysis.procedures) {
if (procedure.supported === false) {
continue;
}
for (const loop of procedure.structure.findAllStructuresRecursive(Structures.While)) {
const issue = this.unchangedCondition(procedure, loop);
if (issue !== undefined) {
issues.push(issue);
}
}
}
return issues;
}
////////////////
private unchangedCondition(procedure: Procedure, loop: StructureNode): Issue | undefined {
const statement = loop.findDirectStatement(Statements.While);
const end = loop.findDirectStatement(Statements.EndWhile);
const cond = statement?.findDirectExpression(Expressions.Cond);
if (statement === undefined || end === undefined || cond === undefined
|| statement.findDirectTokenByText("VARY") !== undefined
|| cond.getAllTokens().some(t => t.getStr() === "->")) {
return undefined;
}
const reads = this.reads(procedure, cond);
if (reads === undefined || reads.locals.length + reads.others.length === 0) {
return undefined;
}
const from = procedure.index(statement);
const to = procedure.index(end);
const body = loop.findDirectStructure(Structures.Body);
let continues = false;
for (const found of body === undefined ? [] : this.loopControl(body, false)) {
const type = found.get();
if (type instanceof Statements.Continue || type instanceof Statements.Check) {
continues = true;
} else {
return undefined;
}
}
if (reads.others.some(o => this.writtenIn(procedure, o, from, to))
|| (reads.others.length > 0 && this.mayChangeOtherData(procedure, from, to, []))) {
return undefined;
}
let written: string | undefined = undefined;
for (const variable of reads.locals) {
for (const write of variable.writes()) {
if (write.index <= from || write.index >= to) {
continue;
} else if (continues || body === undefined
|| this.sameValueWrite(procedure, write.statement, write.token, body, from, to) === false) {
return undefined;
}
written = variable.identifier.getName();
}
}
const names = [...reads.locals.map(v => v.identifier.getName()), ...reads.others.map(o => o.getName())].join(", ");
const message = written === undefined
? `WHILE condition never changes, the loop body does not write ${names}, the loop runs zero times or forever`
: `WHILE condition does not change after the first iteration, the loop body sets ${written} `
+ `to the same value in every iteration, the loop runs once or forever`;
return Issue.atStatement(procedure.file, statement, message, this.getMetadata().key, this.conf.severity);
}
/** data read by the condition or source, undefined if it reads anything that can change unseen */
private reads(procedure: Procedure, node: ExpressionNode): IReads | undefined {
if (node.findFirstExpression(Expressions.SourceFieldSymbol) !== undefined
|| node.findFirstExpression(Expressions.Dereference) !== undefined
|| node.findFirstExpression(Expressions.NewObject) !== undefined) {
return undefined;
}
for (const call of node.findAllExpressionsRecursive(Expressions.MethodCall)) {
if (procedure.isBuiltinCall(call) === false) {
return undefined;
}
}
const ret: IReads = {locals: [], others: [], receivers: []};
const tokens = node.getAllTokens();
for (const field of [...node.findAllExpressionsRecursive(Expressions.SourceField),
...node.findAllExpressionsRecursive(Expressions.AttributeName)]) {
const token = field.getFirstToken();
const identifier = procedure.identifierAt(token);
if (isImmutable(identifier)) {
continue;
}
const local = procedure.local(identifier);
if (local !== undefined) {
if (local.escapes()) {
return undefined;
}
if (ret.locals.includes(local) === false) {
ret.locals.push(local);
}
const index = tokens.findIndex(t => t.getStart().equals(token.getStart()));
if (tokens[index + 1]?.getStr() === "->") {
ret.receivers.push(local);
}
} else if (!(identifier instanceof TypedIdentifier)
|| identifier.getMeta().includes(IdentifierMeta.BuiltIn)) {
// unknown, or system fields like sy-subrc, which change without a write reference
return undefined;
} else if (ret.others.includes(identifier) === false) {
ret.others.push(identifier);
}
}
return ret;
}
/** statements in the loop body that leave the loop or go to its next iteration */
private loopControl(node: StructureNode, nested: boolean): StatementNode[] {
const ret: StatementNode[] = [];
for (const child of node.getChildren()) {
if (child instanceof StructureNode) {
ret.push(...this.loopControl(child, nested || isLoopStructure(child.get())));
continue;
}
const type = child.get();
if (LEAVING.some(l => type instanceof l)
|| (nested === false && (type instanceof Statements.Exit
|| type instanceof Statements.Continue || type instanceof Statements.Check))) {
ret.push(child as StatementNode);
}
}
return ret;
}
/** var = source. directly in the loop body, the source has the same value in every iteration */
private sameValueWrite(procedure: Procedure, statement: StatementNode, token: AbstractToken,
body: StructureNode, from: number, to: number): boolean {
const move = plainMove(statement);
if (move === undefined || move.operator !== "=" || singleTarget(move.target) !== token
|| procedure.block(statement) !== body || procedure.isSimple(statement) === false) {
return false;
}
const reads = this.reads(procedure, move.source);
if (reads === undefined
|| reads.locals.some(l => this.changedIn(procedure, l, from, to))
|| reads.others.some(o => this.writtenIn(procedure, o, from, to))) {
return false;
}
return reads.others.length === 0 || this.mayChangeOtherData(procedure, from, to, reads.receivers) === false;
}
/** written in the loop body, or declared inline in it: the inline declaration is not one of the writes(),
* but sets the variable in every iteration, possibly from data the loop changes */
private changedIn(procedure: Procedure, local: Variable, from: number, to: number): boolean {
if (local.writes().some(w => w.index > from && w.index < to)) {
return true;
}
const index = local.inline && local.declaration !== undefined ? procedure.index(local.declaration) : -1;
return index > from && index < to;
}
private writtenIn(procedure: Procedure, identifier: Identifier, from: number, to: number): boolean {
for (const reference of procedure.analysis.referencesTo(identifier)) {
if (reference.referenceType !== ReferenceType.DataWriteReference || !procedure.contains(reference.position)) {
continue;
}
const statement = procedure.statementOf(reference.position.getToken());
const index = statement === undefined ? -1 : procedure.index(statement);
if (index > from && index < to) {
return true;
}
}
return false;
}
/** the loop body calls methods, functions or forms, or writes via field symbols or ->*, which can change
* attributes and other data that is not local; instance methods via local reference variables are accepted,
* unless the variable is one of the receivers */
private mayChangeOtherData(procedure: Procedure, from: number, to: number, receivers: Variable[]): boolean {
for (let index = from + 1; index < to; index++) {
const statement = procedure.statements[index];
if (statement.findFirstExpression(Expressions.TargetFieldSymbol) !== undefined
|| statement.findFirstExpression(Expressions.Dereference) !== undefined) {
return true;
} else if (procedure.isCallFree(statement)) {
continue;
}
const type = statement.get();
if (!CALLING.some(c => type instanceof c) || statement.findFirstExpression(Expressions.NewObject) !== undefined) {
return true;
}
const tokens = procedure.tokensOf(statement);
const methodSource = statement.findDirectExpression(Expressions.MethodSource);
if (methodSource !== undefined && this.localReceiver(procedure, tokens, methodSource.getLastToken(), receivers) === false) {
return true;
}
for (const call of statement.findAllExpressionsRecursive(Expressions.MethodCall)) {
if (procedure.isBuiltinCall(call) === false
&& this.localReceiver(procedure, tokens, call.getFirstToken(), receivers) === false) {
return true;
}
}
}
return false;
}
/** the method name token is preceded by local->, a local reference variable that is not one of the receivers */
private localReceiver(procedure: Procedure, tokens: readonly AbstractToken[], name: AbstractToken, receivers: Variable[]): boolean {
const index = tokens.findIndex(t => t.getStart().equals(name.getStart()));
if (index < 2 || tokens[index - 1].getStr() !== "->") {
return false;
}
const local = procedure.local(procedure.identifierAt(tokens[index - 2]));
return local !== undefined && receivers.includes(local) === false;
}
}
|