Skip to content

Commit a23b8d0

Browse files
committed
Small overdue fixes to PHPSlice.
1 parent 142c36e commit a23b8d0

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

sdg-cli/src/main/java/es/upv/mist/slicing/cli/PHPSlice.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
2121
import java.io.PrintWriter;
22-
import java.util.Set;
2322

2423
public class PHPSlice {
2524
protected static final Options OPTIONS = new Options();
@@ -57,14 +56,14 @@ public class PHPSlice {
5756

5857
private final File outputDir;
5958
private File scFile;
60-
private int scId;
59+
private int scLine;
6160
private final CommandLine cliOpts;
6261

6362
public PHPSlice(String... cliArgs) throws ParseException {
6463
cliOpts = new DefaultParser().parse(OPTIONS, cliArgs);
6564
if (cliOpts.hasOption('h'))
6665
throw new ParseException(OPTIONS.toString());
67-
setScId(Integer.parseInt(cliOpts.getOptionValue("i")));
66+
setScLine(Integer.parseInt(cliOpts.getOptionValue("i")));
6867
setScFile(cliOpts.getOptionValue("f"));
6968
outputDir = new File(cliOpts.getOptionValue("o"));
7069
if (!outputDir.isDirectory())
@@ -78,10 +77,10 @@ private void setScFile(String fileName) throws ParseException {
7877
scFile = file;
7978
}
8079

81-
private void setScId(int line) throws ParseException {
80+
private void setScLine(int line) throws ParseException {
8281
if (line < 0)
8382
throw new ParseException("The line of the slicing criterion must be strictly greater than zero.");
84-
scId = line;
83+
scLine = line;
8584
}
8685

8786
public void slice() throws ParseException, IOException {
@@ -108,24 +107,23 @@ public void slice() throws ParseException, IOException {
108107
}
109108
sdg.build(units);
110109

111-
SlicingCriterion sc = graph -> Set.of(graph.findNodeBy(n -> n.getId() == (long) 0).orElseThrow());
112-
Slice slice = new Slice(Set.of());
113-
if (scId != 0) {
114-
// Slice the SDG
115-
sc = new FileLineSlicingCriterion(scFile, scId);
116-
slice = sdg.slice(sc);
117-
118-
// Convert the slice to code and output the result to `outputDir`
119-
for (CompilationUnit cu : slice.toAst()) {
120-
if (cu.getStorage().isEmpty())
121-
throw new IllegalStateException("A synthetic CompilationUnit was discovered, with no file associated to it.");
122-
File javaFile = new File(outputDir, cu.getStorage().get().getFileName());
123-
try (PrintWriter pw = new PrintWriter(javaFile)) {
124-
pw.print(new BlockComment(getDisclaimer(cu.getStorage().get())));
125-
pw.print(cu);
126-
} catch (FileNotFoundException e) {
127-
System.err.println("Could not write file " + javaFile);
128-
}
110+
if (scLine < 1)
111+
throw new IllegalArgumentException("Invalid node id");
112+
113+
// Slice the SDG
114+
SlicingCriterion sc = new FileLineSlicingCriterion(scFile, scLine);
115+
Slice slice = sdg.slice(sc);
116+
117+
// Convert the slice to code and output the result to `outputDir`
118+
for (CompilationUnit cu : slice.toAst()) {
119+
if (cu.getStorage().isEmpty())
120+
throw new IllegalStateException("A synthetic CompilationUnit was discovered, with no file associated to it.");
121+
File javaFile = new File(outputDir, cu.getStorage().get().getFileName());
122+
try (PrintWriter pw = new PrintWriter(javaFile)) {
123+
pw.print(new BlockComment(getDisclaimer(cu.getStorage().get())));
124+
pw.print(cu);
125+
} catch (FileNotFoundException e) {
126+
System.err.println("Could not write file " + javaFile);
129127
}
130128
}
131129

@@ -144,7 +142,7 @@ public void slice() throws ParseException, IOException {
144142

145143
protected String getDisclaimer(CompilationUnit.Storage s) {
146144
return String.format("\n\tThis file was automatically generated as part of a slice with criterion" +
147-
"\n\tnode id: %d\n\tOriginal file: %s\n", scId, s.getPath());
145+
"\n\tnode id: %d\n\tOriginal file: %s\n", scLine, s.getPath());
148146
}
149147

150148
public static void main(String... args) {

0 commit comments

Comments
 (0)