Skip to content
Snippets Groups Projects
Commit d219262f authored by Johannes Mey's avatar Johannes Mey
Browse files

initial (empty) scope analysis

parent 71964384
No related branches found
No related tags found
No related merge requests found
aspect Navigation {
syn boolean Element.isScope() = false;
eq Scope.isScope() = true;
syn Scope Element.asScope() = null;
eq Scope.asScope() = this;
syn boolean Element.isDeclaration() = false;
eq Declaration.isDeclaration() = true;
syn Declaration Element.asDeclaration() = null;
eq Declaration.asDeclaration() = this;
}
ScopeTree : Scope;
abstract Element;
Declaration:Element ::= <Name:String>;
Scope:Element ::= Element*;
aspect Shadowing {
public class VariableShadowFinding {
// add fields and getters/setters for shadowed and shadowing variable
}
}
aspect Shadowing {
coll Set<VariableShadowFinding> ScopeTree.variableShadowings() [new HashSet<>()] with add root ScopeTree;
syn int Scope.numScopes() {
int result = 1;
for (Element element : getElementList()) {
if (element.isScope()) {
result += element.asScope().numScopes();
}
}
return result;
}
syn int Scope.numDeclarations() {
int result = 0;
for (Element element : getElementList()) {
if (element.isScope()) {
result += element.asScope().numDeclarations();
} else {
result++;
}
}
return result;
}
}
# Compiled class file
*.class
# Log file
*.log
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
out/
*.iml
/.idea/*
!.idea/codeStyles
!libs/relast.jar
graph/
src/gen/
build/
buildscript {
repositories.mavenLocal()
repositories.mavenCentral()
dependencies {
classpath 'org.jastadd:jastaddgradle:1.13.3'
}
}
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'jastadd'
apply plugin: 'idea'
repositories {
mavenLocal()
}
idea {
module {
generatedSourceDirs += file('./src/gen/java')
sourceDirs += file('../extendj/src/frontend')
sourceDirs += file('../extendj/src/frontend-main')
}
}
sourceSets.main {
java {
srcDirs "src/gen/java"
srcDirs '../extendj/src/frontend'
srcDirs '../extendj/src/frontend-main'
}
resources {
srcDir '../extendj/src/res'
srcDir jastadd.buildInfoDir
}
}
dependencies {
}
jastadd {
configureModuleBuild()
modules {
include("../extendj/jastadd_modules")
module "scope-analysis", {
imports "java8 frontend"
jastadd {
basedir ".."
include "scope4j/src/gen/jastadd/Java.ast"
include "scope4j/src/gen/jastadd/Java.jadd"
include "scope4j/src/main/jastadd/*.jrag"
include "scope4j/src/main/jastadd/*.jadd"
include "scope/src/main/jastadd/*.jrag"
include "scope/src/main/jastadd/*.jadd"
excludeFrom "java8 frontend", "grammar/ConstructorReference.ast"
excludeFrom "java8 frontend", "grammar/IntersectionCasts.ast"
excludeFrom "java8 frontend", "grammar/Lambda.ast"
excludeFrom "java8 frontend", "grammar/LambdaAnonymousDecl.ast"
excludeFrom "java8 frontend", "grammar/MethodReference.ast"
// excludeFrom "java7 frontend", "grammar/BasicTWR.ast"
excludeFrom "java7 frontend", "grammar/Diamond.ast"
excludeFrom "java7 frontend", "grammar/Literals.ast"
excludeFrom "java7 frontend", "grammar/MultiCatch.ast"
excludeFrom "java7 frontend", "grammar/TryWithResources.ast"
excludeFrom "java5 frontend", "grammar/Annotations.ast"
excludeFrom "java5 frontend", "grammar/EnhancedFor.ast"
excludeFrom "java5 frontend", "grammar/Enums.ast"
excludeFrom "java5 frontend", "grammar/GenericMethods.ast"
excludeFrom "java5 frontend", "grammar/Generics.ast"
excludeFrom "java5 frontend", "grammar/StaticImports.ast"
excludeFrom "java5 frontend", "grammar/VariableArityParameters.ast"
excludeFrom "java4 frontend", "grammar/BoundNames.ast"
excludeFrom "java4 frontend", "grammar/Java.ast"
excludeFrom "java4 frontend", "grammar/Literals.ast"
excludeFrom "java4 frontend", "grammar/NTAFinally.ast"
excludeFrom "java7 frontend", "frontend/JavaVersion.jrag"
excludeFrom "java5 frontend", "frontend/BytecodeReader.jrag"
excludeFrom "java7 frontend", "frontend/Variable.jadd"
}
java {
basedir "src/main/java/"
include "**/*.java"
}
}
}
// Target module to build:
module = 'scope-analysis'
astPackage = 'org.extendj.ast'
parser.name = 'JavaParser'
scanner.name = 'OriginalScanner'
genDir = 'src/gen/java'
parser.genDir = 'src/gen/java/org/extendj/parser'
scanner.genDir = 'src/gen/java/org/extendj/scanner'
parser.genDir = 'src/gen/java/org/extendj/parser'
scanner.genDir = 'src/gen/java/org/extendj/scanner'
//
// if (project.hasProperty('extraJastAddOptions')) {
// extraJastAddOptions += project.extraJastAddOptions.split(',') as List
// print("options: ${extraJastAddOptions}")
// }
// jastaddOptions = [ "--rewrite=cnta", "--safeLazy", "--tracing=all" ]
jastaddOptions = ["--rewrite=cnta", "--safeLazy", "--tracing=api", "--visitCheck=false"]
// jastaddOptions = [ "--concurrent", "--rewrite=cnta", "--safeLazy" ]
// jastaddOptions = [ "--concurrent", "--rewrite=cnta", "--safeLazy", "--cache=all" ]
}
run {
mainClassName = 'org.extendj.SccChecker'
if (project.hasProperty("appArgs")) {
args Eval.me(appArgs)
}
}
task preprocess(type: JavaExec) {
group = 'Build'
main = "-jar"
doFirst {
delete "src/gen/jastadd"
mkdir "src/gen/jastadd"
}
args = [
"../tools/relast.jar",
"src/main/jastadd/ProgramToScopeTree.relast",
"../scope/src/main/jastadd/ScopeTree.relast",
"../extendj/java8/grammar/ConstructorReference.ast",
"../extendj/java8/grammar/IntersectionCasts.ast",
"../extendj/java8/grammar/Lambda.ast",
"../extendj/java8/grammar/LambdaAnonymousDecl.ast",
"../extendj/java8/grammar/MethodReference.ast",
// "../extendj/java7/grammar/BasicTWR.ast",
"../extendj/java7/grammar/Diamond.ast",
"../extendj/java7/grammar/Literals.ast",
"../extendj/java7/grammar/MultiCatch.ast",
"../extendj/java7/grammar/TryWithResources.ast",
"../extendj/java5/grammar/Annotations.ast",
"../extendj/java5/grammar/EnhancedFor.ast",
"../extendj/java5/grammar/Enums.ast",
"../extendj/java5/grammar/GenericMethods.ast",
"../extendj/java5/grammar/Generics.ast",
"../extendj/java5/grammar/StaticImports.ast",
"../extendj/java5/grammar/VariableArityParameters.ast",
"../extendj/java4/grammar/BoundNames.ast",
"../extendj/java4/grammar/Java.ast",
"../extendj/java4/grammar/Literals.ast",
"../extendj/java4/grammar/NTAFinally.ast",
"--listClass=ArrayList",
// "--jastAddList=JastAddList",
// "--serializer=jackson",
"--useJastAddNames",
"--file",
// "--resolverHelper",
"--grammarName=src/gen/jastadd/Java"
]
inputs.files file("../extendj/java8/grammar/ConstructorReference.ast"),
file("src/main/jastadd/ProgramToScopeTree.relast"),
file("../scope/src/main/jastadd/ScopeTree.relast"),
file("../extendj/java8/grammar/IntersectionCasts.ast"),
file("../extendj/java8/grammar/Lambda.ast"),
file("../extendj/java8/grammar/LambdaAnonymousDecl.ast"),
file("../extendj/java8/grammar/MethodReference.ast"),
file("../extendj/java7/grammar/BasicTWR.ast"),
file("../extendj/java7/grammar/Diamond.ast"),
file("../extendj/java7/grammar/Literals.ast"),
file("../extendj/java7/grammar/MultiCatch.ast"),
file("../extendj/java7/grammar/TryWithResources.ast"),
file("../extendj/java5/grammar/Annotations.ast"),
file("../extendj/java5/grammar/EnhancedFor.ast"),
file("../extendj/java5/grammar/Enums.ast"),
file("../extendj/java5/grammar/GenericMethods.ast"),
file("../extendj/java5/grammar/Generics.ast"),
file("../extendj/java5/grammar/StaticImports.ast"),
file("../extendj/java5/grammar/VariableArityParameters.ast"),
file("../extendj/java4/grammar/BoundNames.ast"),
file("../extendj/java4/grammar/Java.ast"),
file("../extendj/java4/grammar/Literals.ast"),
file("../extendj/java4/grammar/NTAFinally.ast"),
file("../tools/relast.jar")
outputs.files file("src/gen/jastadd/Java.ast"),
file("src/gen/jastadd/Java.jadd")
}
generateAst.dependsOn preprocess
mainClassName = 'org.extendj.ScopeAnalysis'
jar.manifest.attributes 'Main-Class': mainClassName
jar.destinationDir = projectDir
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
aspect ProgramToScopeTree {
syn lazy ScopeTree Program.scopeTree() {
ScopeTree tree = new ScopeTree();
tree.setProgram(this);
return tree;
}
}
// glue relation for the Java-based variable shadowing analysis
rel ScopeTree.Program -> Program;
aspect Shadow {
}
package org.extendj;
import org.extendj.ast.VariableShadowFinding;
public class JavaVariableShadowFinding extends VariableShadowFinding {
// add getters for the actual Java structures
}
package org.extendj;
import org.extendj.ast.*;
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.stream.Collectors;
public class ScopeAnalysis extends Frontend {
public ScopeAnalysis() {
super("Java Scope Anaysis", ExtendJVersion.getVersion());
}
/**
* Entry point for the Java checker.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("usage: SccChecker <directory with java files>");
System.exit(-1);
}
try {
List<String> files = Files.walk(Paths.get(args[2]))
.filter(Files::isRegularFile)
.filter(x -> x.getFileName().toString().endsWith(".java")).map(Path::toString).collect(Collectors.toList());
// measure the time (with parsing) from here
long startMeasurementTime = System.nanoTime();
Program program = new ScopeAnalysis().readProgram(files);
// measure the time (without parsing) from here
long startGenerationTime = System.nanoTime();
ScopeTree scopeTree = program.scopeTree();
long startAnalysisTime = System.nanoTime();
Set<VariableShadowFinding> findings = scopeTree.variableShadowings();
// measure the time until here
long endTime = System.nanoTime();
System.out.print("java,var,false,"
+ files.size() + ","
+ scopeTree.numScopes() + ","
+ scopeTree.numDeclarations() + ","
+ (scopeTree.numScopes() + scopeTree.numDeclarations()) + ","
+ findings.size() + ",");
long parseTime = startGenerationTime - startMeasurementTime;
long generationTime = startAnalysisTime - startGenerationTime;
long analysisTime = endTime - startAnalysisTime;
long fullTime = endTime - startMeasurementTime;
System.out.print((fullTime / 1000000) + ",");
System.out.print((parseTime / 1000000) + ",");
System.out.print((generationTime / 1000000) + ",");
System.out.print((analysisTime / 1000000) + ",");
System.out.print(((generationTime + analysisTime) / 1000000) + ",");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Program readProgram(Collection<String> files) throws IOException {
Program program = new Program();
program.resetStatistics();
program.initBytecodeReader(Program.defaultBytecodeReader());
program.initJavaParser(Program.defaultJavaParser());
initOptions();
for (String file : files) {
program.addSourceFile(file);
}
TypeDecl object = program.lookupType("java.lang", "Object");
if (object.isUnknown()) {
// If we try to continue without java.lang.Object, we'll just get a stack overflow
// in member lookups because the unknown (Object) type would be treated as circular.
System.err.println("Error: java.lang.Object is missing."
+ " The Java standard library was not found.");
throw new RuntimeException("exiting with unhandled error!");
}
return program;
}
}
...@@ -10,10 +10,12 @@ ...@@ -10,10 +10,12 @@
rootProject.name = 'relast-reuse' rootProject.name = 'relast-reuse'
include 'statemachine' include 'statemachine'
include 'dg'
include 'simplecfg'
include 'extendj' include 'extendj'
include 'dg'
include 'deps4j' include 'deps4j'
include 'scope'
include 'scope4j'
include 'simplecfg'
include 'extendj:java4' include 'extendj:java4'
include 'extendj:java5' include 'extendj:java5'
include 'extendj:java6' include 'extendj:java6'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment