From f56fec28f1bc1d6a76f558afa00fd96c3533a8b0 Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Tue, 24 Dec 2019 13:03:39 +0100
Subject: [PATCH] add debug mode (triggered by command line flag '--debug' or
 not giving any parameters), improve output

---
 .../main/java/org/extendj/ScopeAnalysis.java  | 35 ++++++++++++-------
 1 file changed, 22 insertions(+), 13 deletions(-)

diff --git a/scope4j/src/main/java/org/extendj/ScopeAnalysis.java b/scope4j/src/main/java/org/extendj/ScopeAnalysis.java
index 53b570a..9ac9889 100644
--- a/scope4j/src/main/java/org/extendj/ScopeAnalysis.java
+++ b/scope4j/src/main/java/org/extendj/ScopeAnalysis.java
@@ -7,9 +7,8 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.Collection;
 import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 
 public class ScopeAnalysis extends Frontend {
@@ -26,11 +25,15 @@ public class ScopeAnalysis extends Frontend {
    */
   public static void main(String[] args) {
 
-    if (args.length > 1 || args.length < 0) {
-      System.out.println("usage: ScopeAnalysis <directory with java files>");
+    List<String> arguments = new ArrayList<>(Arrays.asList(args));
+
+    boolean debug = arguments.isEmpty() || arguments.remove("--debug");
+
+    if (arguments.size() > 1) {
+      System.out.println("usage: ScopeAnalysis [--debug] <directory with java files>");
       System.exit(-1);
     }
-    String path = args.length == 1 ? args[0] : "../testprograms/simpleScope";
+    String path = arguments.isEmpty() ? "../testprograms/simpleScope" : arguments.get(arguments.size() - 1);
 
     try {
       List<String> files = Files.walk(Paths.get(path))
@@ -47,22 +50,28 @@ public class ScopeAnalysis extends Frontend {
 
       ScopeTree scopeTree = program.scopeTree();
 
-      scopeTree.printAST();
+      if (debug) {
+        scopeTree.printAST();
 
-      System.out.println("\nExtendJ found the following problems:");
-      for (CompilationUnit unit : program.getCompilationUnitList()) {
-        for (Problem problem: unit.problems()) {
-          System.out.println(problem);
+        System.out.println("\nExtendJ found the following problems:");
+        for (CompilationUnit unit : program.getCompilationUnitList()) {
+          for (Problem problem : unit.problems()) {
+            System.out.println(problem);
+          }
         }
+        System.out.println();
       }
-      System.out.println("\n");
 
       long startAnalysisTime = System.nanoTime();
 
       Set<VariableShadowFinding> findings = scopeTree.variableShadowings();
 
-      for (VariableShadowFinding finding : findings) {
-        System.out.println(finding);
+      if (debug) {
+        System.out.println("\nScope4J found the following problems:");
+        for (VariableShadowFinding finding : findings) {
+          System.out.println(finding);
+        }
+        System.out.println();
       }
 
       // measure the time until here
-- 
GitLab