Skip to content

Commit a74f2d9

Browse files
committed
more on static analyzer
1 parent 9435599 commit a74f2d9

File tree

2 files changed

+80
-22
lines changed

2 files changed

+80
-22
lines changed

src/main/java/core/JSAnalyzer.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -466,18 +466,6 @@ public void analyzeTestCodeProperties() throws Exception {
466466

467467
astVisitor.setScopeName(scopeName);
468468
/* recurse through AST */
469-
astVisitor.setVisitOnly("FunctionNode");
470-
ast.visit(astVisitor);
471-
472-
System.out.println("CoveredFunctions :" + astVisitor.getCoveredFunctions());
473-
//System.out.println("CoveredFunctions.size() :" + astVisitor.getCoveredFunctions().size());
474-
System.out.println("CoveredFunctionLines :" + astVisitor.getCoveredFunctionLines());
475-
System.out.println("MissedFunctions :" + astVisitor.getMissedFunctions());
476-
//System.out.println("MissedFunctions.size() :" + astVisitor.getMissedFunctions().size());
477-
System.out.println("MissedFunctionLines :" + astVisitor.getMissedFunctionLines());
478-
479-
480-
astVisitor.setVisitOnly("FunctionCall");
481469
ast.visit(astVisitor);
482470

483471
System.out.println("FunctionCalls :" + astVisitor.getFunctionCalls());
@@ -491,7 +479,6 @@ public void analyzeTestCodeProperties() throws Exception {
491479
}
492480
}
493481

494-
495482
coveredRegularFunc = astVisitor.getCoveredRegularFunc();
496483
missedRegularFunc = astVisitor.getMissedRegularFunc();
497484
coveredCallback = astVisitor.getCoveredCallback();
@@ -503,7 +490,6 @@ public void analyzeTestCodeProperties() throws Exception {
503490
coveredClosure = astVisitor.getCoveredClosure();
504491
missedClosure = astVisitor.getMissedClosure();
505492

506-
507493
System.out.println("++++ coveredRegularFunc: " + astVisitor.getCoveredRegularFunc());
508494
System.out.println("++++ missedRegularFunc: " + astVisitor.getMissedRegularFunc());
509495
System.out.println("++++ coveredCallback: " + astVisitor.getCoveredCallback());
@@ -535,26 +521,21 @@ public void analyzeTestCodeProperties() throws Exception {
535521
}
536522

537523

538-
/*
539524
System.out.println("assertionCounter: " + astVisitor.getAssertionCounter());
540525
System.out.println("newExpressionCounter: " + astVisitor.getNewExpressionCounter());
541526
System.out.println("testCounter: " + astVisitor.getTestCounter());
542527
System.out.println("asynchTestCounter: " + astVisitor.getAsynchTestCounter());
543528
System.out.println("trieggerCounter: " + astVisitor.getTriggerCounetr());
544-
*/
529+
545530

546531
/* clean up */
547532
Context.exit();
548533
} catch (RhinoException re) {
549534
System.err.println(re.getMessage());
550535
System.out.println("Unable to instrument. This might be a JSON response sent"
551536
+ " with the wrong Content-Type or a syntax error.");
552-
} catch (IllegalArgumentException iae) {
553-
554-
System.out.println("Invalid operator exception catched. Not instrumenting code.");
555537
}
556538

557-
//System.out.println("Here is the corresponding buffer: \n" + input + "\n");
558539
astVisitor.setVisitType("");
559540
}
560541

src/main/java/instrumentor/JSASTInstrumentor.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,12 @@ else if (node instanceof Assignment)
283283
analyzeProductionCodeAssignmentNode(node);
284284
}
285285
}else if (visitType.equals("AnalyzeTestCode")){
286-
286+
if (node instanceof NewExpression)
287+
newExpressionCounter++;
288+
else if (node instanceof FunctionNode)
289+
analyzeTestCodeFunctionNode(node);
290+
else if (node instanceof FunctionCall)
291+
analyzeTestCodeFunctionCallNode(node);
287292

288293
}else{
289294
System.out.println("visitType is not set!");
@@ -551,7 +556,63 @@ private void instrumentFunctionNode(AstNode node) {
551556

552557

553558
private void analyzeTestCodeFunctionNode(AstNode node) {
554-
System.out.println("=== instrumentFunctionCallNode ===");
559+
System.out.println("=== analyzeTestCodeFunctionNode ===");
560+
// getting the enclosing function name
561+
String enclosingFunction = "";
562+
if (node.getEnclosingFunction()!=null){
563+
enclosingFunction = getFunctionName(node.getEnclosingFunction());
564+
}
565+
System.out.println("enclosingFunction: " + enclosingFunction);
566+
567+
if (node.shortName().equals("NewExpression"))
568+
return;
569+
570+
FunctionCall fcall = (FunctionCall) node;
571+
AstNode targetNode = fcall.getTarget(); // node evaluating to the function to call. E.g document.getElemenyById(x)
572+
573+
// avoid instrumenting wrapper function calls!
574+
if (fcall.getParent().toSource().contains("funcionCallWrapper")){
575+
System.out.println("Not instrumenting " + fcall.getTarget().toSource() + ", because of: " + fcall.getParent().toSource());
576+
return;
577+
}
578+
579+
String functionName = targetNode.toSource().substring(targetNode.toSource().lastIndexOf(".")+1);
580+
581+
if (targetNode.toSource().equals("QUnit.module") || targetNode.toSource().equals("module"))
582+
currentTest = "TestModule";
583+
if (targetNode.toSource().equals("QUnit.test") || targetNode.toSource().equals("test")){
584+
currentTestNumber++;
585+
currentTest = "Test" + Integer.toString(currentTestNumber);
586+
setTestCounter(getTestCounter() + 1);
587+
}
588+
if (targetNode.toSource().equals("QUnit.asyncTest()") || targetNode.toSource().equals("asyncTest()")){
589+
currentTestNumber++;
590+
currentTest = "AsynchTest" + Integer.toString(currentTestNumber);
591+
setAsynchTestCounter(getAsynchTestCounter() + 1);
592+
}
593+
594+
if (targetNode.toSource().equals("trigger") || targetNode.toSource().equals("triggerHandler"))
595+
setTriggerCounetr(getTriggerCounetr() + 1);
596+
597+
String[] assertionSkipList = { "assert.expect", "expect", "assert.equal", "equal", "assert.notEqual", "notEqual", "assert.deepEqual", "deepEqual",
598+
"assert.notDeepEqual", "notDeepEqual", "assert.strictEqual", "strictEqual", "assert.notStrictEqual", "notStrictEqual", "QUnit.ok", "assert.ok", "ok", "assert.notOk", "notOk",
599+
"assert.propEqual", "propEqual", "assert.notPropEqual", "notPropEqual", "assert.push", "assert.throws", "throws", "assert.async"};
600+
601+
String[] otherSkipList = { "QUnit.module", "module", "QUnit.test", "test", "QUnit.asyncTest", "asyncTest", "jQuery", "$" , "start", "stop"}; // start/stop for asynchronous control
602+
603+
if (ArrayUtils.contains( assertionSkipList, targetNode.toSource() ))
604+
setAssertionCounter(getAssertionCounter() + 1);
605+
606+
if (ArrayUtils.contains( assertionSkipList, targetNode.toSource() ) || ArrayUtils.contains( otherSkipList, targetNode.toSource() )) {
607+
System.out.println("Not instrumenting " + targetNode.toSource());
608+
return;
609+
}else
610+
System.out.println("Instrumenting " + functionName);
611+
}
612+
613+
614+
private void analyzeTestCodeFunctionCallNode(AstNode node) {
615+
System.out.println("=== analyzeTestCodeFunctionCallNode ===");
555616
// getting the enclosing function name
556617
String enclosingFunction = "";
557618
if (node.getEnclosingFunction()!=null){
@@ -603,6 +664,22 @@ private void analyzeTestCodeFunctionNode(AstNode node) {
603664
return;
604665
}else
605666
System.out.println("Instrumenting " + functionName);
667+
668+
// functionName, enclosingFunction, function
669+
List<AstNode> args = new ArrayList<AstNode>(fcall.getArguments());
670+
String wrapperCode = scopeName.replace(".js","").replace("-", "_") + "_funcionCallWrapper(\""+ functionName +"\", \"" + scopeName.replace(".js","") + "_" + currentTest + "\", " + fcall.toSource() + ")";
671+
System.out.println("wrapperCode : " + wrapperCode );
672+
673+
AstNode wrapperNode = parse(wrapperCode);
674+
ExpressionStatement es = (ExpressionStatement)((AstNode) wrapperNode.getFirstChild());
675+
FunctionCall wrapperFunCall = (FunctionCall) es.getExpression();
676+
args.add(wrapperFunCall.getArguments().get(0));
677+
//wrapperFunCall.addArgument(fcall);
678+
System.out.println("Replacing functionCall: " + fcall.toSource() + " with wrapperFunCall: " + wrapperFunCall.toSource());
679+
fcall.setTarget(wrapperFunCall.getTarget());
680+
fcall.setArguments(wrapperFunCall.getArguments());
681+
System.out.println("New functionCall: " + fcall.toSource());
682+
606683
}
607684

608685

0 commit comments

Comments
 (0)