All files / src/utils include_graph.ts

96.44% Statements 244/253
93.54% Branches 58/62
100% Functions 13/13
96.44% Lines 244/253

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 2531x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1508x 1508x 1508x 2334x 1522x 1522x 2334x 1508x 1508x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 754x 754x 754x 754x 1x 1x 737x 584x 584x 737x 737x 1x 1x 88x 88x 1x 1x 447x 447x 1x 1x 76x 67x 67x 76x 76x 1x 1x 1x 76x 76x 76x 97x 97x 21x 21x 76x 76x 76x 76x 97x 55x 55x 1x 1x 108x 108x 108x 62x 62x 13x 13x 49x 49x 108x 108x 63x 63x 63x 63x 63x 108x 108x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 754x 754x 754x 754x 754x 754x 754x 1x 1x 59x 59x     59x 63x 54x 54x 63x 59x 59x 1x 1x 384x 384x 45x 20x 20x 45x 384x 384x 384x 384x 384x 384x 4x 4x     4x 4x 4x 384x 384x 384x 1x 1x 1x 1x 754x 754x 754x 754x 761x 783x       783x 783x 4915x 92x 92x     92x 92x 4x 4x 88x 92x 3x 3x 2x 2x 2x 92x 9x 9x 85x 76x 76x 76x 92x 4915x 783x 761x 754x 754x 754x 76x 21x 21x 21x 76x 754x 1x 1x 754x 761x 580x 580x 580x 580x 580x 580x 580x 761x 7x 7x 7x 7x 7x 7x 7x 181x 126x 140x 140x 140x 140x 140x 174x 6x 4x 4x 4x 4x 4x 6x 6x 6x 6x 6x 6x 6x 6x 761x 754x 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 {StatementNode} from "../abap/nodes/statement_node";
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, 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);
  }
 
  // true if "to" is reachable from "from" via the edges, "from" itself counts
  public isReachable(from: string, to: string): boolean {
    const seen = new Set<string>();
    const stack = [from];
    while (stack.length > 0) {
      const current = stack.pop()!;
      if (current === to) {
        return true;
      }
      if (seen.has(current) === false) {
        seen.add(current);
        stack.push(...this.edges[current] || []);
      }
    }
    return false;
  }
 
  public findTop(filename: string, path: Set<string> = new Set()): IVertex[] {
    const ret: IVertex[] = [];
    path.add(filename);
    for (const to of this.edges[filename] || []) {
      // an INCLUDE cycle, the file is already on the current path
      if (path.has(to)) {
        continue;
      }
      ret.push(...this.findTop(to, path));
    }
    path.delete(filename);
    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();
    const included: {file: IFile, statement: StatementNode, vertex: IVertex, name: string}[] = [];
 
    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());
              included.push({file: f, statement: s, vertex: found, name});
            }
          }
        }
      }
    }
 
    // cycles are not followed, but ABAP rejects them, so report them
    for (const i of included) {
      if (this.graph.isReachable(i.file.getFilename(), i.vertex.filename)) {
        const issue = Issue.atStatement(i.file, i.statement, "Cyclic INCLUDE, " + i.name, new CheckInclude().getMetadata().key, this.severity);
        this.issues.push(issue);
      }
    }
  }
 
  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});
        }
      }
    }
  }
 
}