All files / src/utils include_graph.ts

95.9% Statements 211/220
91.83% Branches 45/49
100% Functions 12/12
95.9% Lines 211/220

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 2201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1016x 1016x 1016x 1340x 1056x 1056x 1340x 1016x 1016x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 508x 508x 508x 508x 1x 1x 502x 379x 379x 502x 502x 1x 1x 23x 23x 1x 1x 297x 297x 1x 1x 17x 17x 17x 17x 17x 1x 1x 43x 43x 14x 14x 43x 29x 29x 29x 29x 29x 43x 43x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 508x 508x 508x 508x 508x 508x 508x 1x 1x 29x 29x     29x 29x 27x 27x 29x 29x 29x 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 508x 508x 508x 528x 544x       544x 544x 2961x 27x 27x     27x 27x 4x 4x 23x 27x 3x 3x 2x 2x 2x 27x 3x 3x 20x 17x 17x 27x 2961x 544x 528x 508x 1x 1x 508x 528x 375x 375x 375x 375x 375x 375x 375x 528x 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 528x 508x 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});
        }
      }
    }
  }
 
}