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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1014x 1014x 1014x 1336x 1052x 1052x 1336x 1014x 1014x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 507x 507x 507x 507x 1x 1x 500x 377x 377x 500x 500x 1x 1x 22x 22x 1x 1x 296x 296x 1x 1x 16x 16x 16x 16x 16x 1x 1x 41x 41x 13x 13x 41x 28x 28x 28x 28x 28x 41x 41x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 507x 507x 507x 507x 507x 507x 507x 1x 1x 28x 28x 28x 28x 26x 26x 28x 28x 28x 1x 1x 268x 268x 7x 4x 4x 7x 268x 268x 268x 268x 268x 268x 1x 1x 1x 1x 1x 268x 268x 268x 1x 1x 1x 1x 507x 507x 507x 526x 542x 542x 542x 2959x 26x 26x 26x 26x 4x 4x 22x 26x 3x 3x 2x 2x 2x 26x 3x 3x 19x 16x 16x 26x 2959x 542x 526x 507x 1x 1x 507x 526x 373x 373x 373x 373x 373x 373x 373x 526x 7x 7x 7x 7x 7x 7x 7x 153x 102x 112x 112x 112x 112x 112x 146x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 526x 507x 1x 1x | import {Include} from "../abap/2_statements/statements";
import {IncludeName} from "../abap/2_statements/expressions";
import {Class, FunctionGroup, Program, TypePool} from "../objects";
import {CheckInclude} from "../rules/check_include";
import {Position} from "../position";
import {Issue} from "../issue";
import {IFile} from "../files/_ifile";
import {IIncludeGraph} from "./_include_graph";
import {IRegistry} from "../_iregistry";
import {ABAPObject} from "../objects/_abap_object";
import {Severity} from "../severity";
// todo, check for cycles/circular dependencies, method findTop
// todo, add configurable error for multiple use includes
const FMXXINCLUDE = /^(\/\w+\/)?L.+XX$/;
function getABAPObjects(reg: IRegistry): ABAPObject[] {
const ret: ABAPObject[] = [];
for (const o of reg.getObjects()) {
if (o instanceof ABAPObject) {
ret.push(o);
}
}
return ret;
}
interface IVertex {
filename: string;
includeName: string | undefined; // undefined for classes
include: boolean;
}
class Graph {
public readonly verticesIncludenameIndex: {[includeName: string]: IVertex};
public readonly verticesFilenameIndex: {[filenameName: string]: IVertex};
public readonly edges: {[from: string]: string[]};
public constructor() {
this.verticesIncludenameIndex = {};
this.verticesFilenameIndex = {};
this.edges = {};
}
public addVertex(vertex: IVertex) {
if (vertex.includeName !== undefined) {
this.verticesIncludenameIndex[vertex.includeName.toUpperCase()] = vertex;
}
this.verticesFilenameIndex[vertex.filename.toUpperCase()] = vertex;
}
public findVertexViaIncludename(includeName: string): IVertex | undefined {
return this.verticesIncludenameIndex[includeName.toUpperCase()];
}
public findVertexByFilename(filename: string): IVertex | undefined {
return this.verticesFilenameIndex[filename.toUpperCase()];
}
public addEdge(from: IVertex, toFilename: string) {
if (this.edges[from.filename] === undefined) {
this.edges[from.filename] = [];
}
this.edges[from.filename].push(toFilename);
}
public findTop(filename: string): IVertex[] {
const ret: IVertex[] = [];
for (const to of this.edges[filename] || []) {
ret.push(...this.findTop(to));
}
if (ret.length === 0) {
const found = this.findVertexByFilename(filename);
if (found !== undefined) {
ret.push(found);
}
}
return ret;
}
}
export class IncludeGraph implements IIncludeGraph {
private readonly reg: IRegistry;
private readonly issues: Issue[];
private readonly graph: Graph;
private readonly severity: Severity;
private readonly allowUnused: boolean;
public constructor(reg: IRegistry, severity: Severity = Severity.Error, allowUnused: boolean = false) {
this.reg = reg;
this.issues = [];
this.graph = new Graph();
this.severity = severity;
this.allowUnused = allowUnused;
this.build();
}
public listMainForInclude(filename: string | undefined): string[] {
const ret: string[] = [];
if (filename === undefined) {
return [];
}
for (const f of this.graph.findTop(filename)) {
if (f.include === false) {
ret.push(f.filename);
}
}
return ret;
}
public getIssuesFile(file: IFile): Issue[] {
const ret: Issue[] = [];
for (const i of this.issues) {
if (i.getFilename() === file.getFilename()) {
ret.push(i);
}
}
const v = this.graph.findVertexByFilename(file.getFilename());
if (v !== undefined
&& this.allowUnused === false
&& v.include === true
&& this.listMainForInclude(v.filename).length === 0) {
const f = this.reg.getFileByName(v.filename);
if (f === undefined) {
throw new Error("findUnusedIncludes internal error");
}
const issue = Issue.atPosition(f, new Position(1, 1), "INCLUDE not used anywhere", new CheckInclude().getMetadata().key, this.severity);
ret.push(issue);
}
return ret;
}
///////////////////////////////
private build() {
this.addVertices();
for (const o of getABAPObjects(this.reg)) {
for (const f of o.getABAPFiles()) {
if (f.getFilename().includes(".prog.screen_") || f.getFilename().includes(".fugr.screen_")) {
// skip dynpro files
continue;
}
for (const s of f.getStatements()) {
if (s.get() instanceof Include) {
const iexp = s.findFirstExpression(IncludeName);
if (iexp === undefined) {
throw new Error("unexpected Include node");
}
const name = iexp.getFirstToken().getStr().toUpperCase();
if (name.match(FMXXINCLUDE)) { // function module XX includes, possibily namespaced
continue;
}
const found = this.graph.findVertexViaIncludename(name);
if (found === undefined) {
const ifFound = s.concatTokens().toUpperCase().includes("IF FOUND");
if (ifFound === false) {
const issue = Issue.atStatement(f, s, "Include " + name + " not found", new CheckInclude().getMetadata().key, this.severity);
this.issues.push(issue);
}
} else if (found.include === false) {
const issue = Issue.atStatement(f, s, "Not possible to INCLUDE a main program, " + name, new CheckInclude().getMetadata().key, this.severity);
this.issues.push(issue);
} else {
this.graph.addEdge(found, f.getFilename());
}
}
}
}
}
}
private addVertices() {
for (const o of getABAPObjects(this.reg)) {
if (o instanceof Program) {
const file = o.getMainABAPFile();
if (file) {
this.graph.addVertex({
filename: file.getFilename(),
includeName: o.getName(),
include: o.isInclude()});
}
} else if (o instanceof TypePool) {
const file = o.getMainABAPFile();
if (file) {
this.graph.addVertex({
filename: file.getFilename(),
includeName: undefined,
include: false});
}
} else if (o instanceof Class) {
for (const f of o.getSequencedFiles()) {
this.graph.addVertex({
filename: f.getFilename(),
includeName: undefined,
include: false});
}
} else if (o instanceof FunctionGroup) {
for (const i of o.getIncludeFiles()) {
this.graph.addVertex({
filename: i.file.getFilename(),
includeName: i.name,
include: true});
}
const file = o.getMainABAPFile();
if (file) {
this.graph.addVertex({
filename: file.getFilename(),
includeName: undefined, // this is the SAPL program
include: false});
}
}
}
}
} |