Skip to content

Commit

Permalink
Merge pull request #141 from vlad902/simplify-c-udg-expressions
Browse files Browse the repository at this point in the history
Simplify dynamically generated UDG expressions
  • Loading branch information
Fabian Yamaguchi committed Dec 1, 2016
2 parents de0cd6a + 4fa3f70 commit 6f9f91a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package udg.useDefAnalysis;

public class CUseDefExpression {
/**
* Eliminate redundant "& * " and "* & " patterns that can occur in
* dynamically generated UseDef references.
*/
public static String simplify(String expr)
{
return expr.replace("& * ", "").replace("* & ", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;

import udg.ASTProvider;
import udg.useDefAnalysis.CUseDefExpression;
import udg.useDefAnalysis.environments.EmitDefAndUseEnvironment;

public class ArgumentEnvironment extends EmitDefAndUseEnvironment
Expand All @@ -15,20 +16,15 @@ public void addChildSymbols(LinkedList<String> childSymbols,
{
if (isDef(child))
{
// For tainted arguments, add "* symbol" instead of symbol
// to defined symbols. Make an exception if symbol starts with '& '

LinkedList<String> derefChildSymbols = new LinkedList<String>();
for (String symbol : childSymbols)
{

derefChildSymbols.add(CUseDefExpression.simplify("* " + symbol));
if (!symbol.startsWith("& "))
{
derefChildSymbols.add("* " + symbol);
// !patch to see if we can detect macro-sources!
derefChildSymbols.add(symbol);
} else
derefChildSymbols.add(symbol.substring(2));
}
}

defSymbols.addAll(derefChildSymbols);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;

import udg.ASTProvider;
import udg.useDefAnalysis.CUseDefExpression;
import udg.useDefAnalysis.environments.EmitUseEnvironment;

public class ArrayIndexingEnvironment extends EmitUseEnvironment
Expand All @@ -15,7 +16,7 @@ public void addChildSymbols(LinkedList<String> childSymbols,
LinkedList<String> derefedChildren = new LinkedList<String>();
for (String c : childSymbols)
{
derefedChildren.add("* " + c);
derefedChildren.add(CUseDefExpression.simplify("* " + c));
}

symbols.addAll(derefedChildren);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.LinkedList;

import udg.ASTProvider;
import udg.useDefAnalysis.CUseDefExpression;
import udg.useDefAnalysis.environments.UseDefEnvironment;
import udg.useDefGraph.UseOrDef;

Expand All @@ -20,7 +21,7 @@ public LinkedList<String> upstreamSymbols()
LinkedList<String> derefedChildren = new LinkedList<String>();
for (String c : symbols)
{
derefedChildren.add("* " + c);
derefedChildren.add(CUseDefExpression.simplify("* " + c));
}

retval.addAll(derefedChildren);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;

import udg.ASTProvider;
import udg.useDefAnalysis.CUseDefExpression;
import udg.useDefAnalysis.environments.EmitUseEnvironment;

public class UnaryOpEnvironment extends EmitUseEnvironment
Expand All @@ -18,8 +19,9 @@ public void addChildSymbols(LinkedList<String> childSymbols,
{
for (String symbol : childSymbols)
{
symbols.add("& " + symbol);
symbols.add(CUseDefExpression.simplify("& " + symbol));
}

return;
}

Expand All @@ -36,7 +38,7 @@ public void addChildSymbols(LinkedList<String> childSymbols,
LinkedList<String> derefedChildren = new LinkedList<String>();
for (String c : childSymbols)
{
derefedChildren.add("* " + c);
derefedChildren.add(CUseDefExpression.simplify("* " + c));
}

retval.addAll(derefedChildren);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tests.udg;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class testUseDefGraphCreator extends TestDBTestsBatchInserter
aMap.put("plusEqualsUse", "int f(){ x += y; }");
aMap.put("ddg_test_struct",
"int ddg_test_struct(){ struct my_struct foo; foo.bar = 10; copy_somehwere(foo); }");
aMap.put("udg_simplify_expressions", "int test() { func(&a[0]); }");

functionMap = aMap;
}
Expand Down Expand Up @@ -87,6 +89,16 @@ public void test_condition()
assertOnlyUseForXFound(useDefGraph, "z");
}

// Test that expressions are simplified, e.g. lack of an "& * a" element
@Test
public void test_simplified_expression()
{
UseDefGraph useDefGraph = createUDGForFunction("udg_simplify_expressions");
assertEquals(useDefGraph.keySet().size(), 1);
assertTrue(useDefGraph.keySet().contains("a"));
}


private UseDefGraph createUDGForFunction(String functionName)
{
String code = functionMap.get(functionName);
Expand All @@ -107,7 +119,7 @@ private void assertOnlyDefForXFound(UseDefGraph useDefGraph, String symbol)
assertTrue(usesAndDefs != null);
assertTrue(usesAndDefs.size() > 0);

// make sure only 'uses' of x exist
// make sure only 'definitions' of x exist
for (UseOrDefRecord r : usesAndDefs)
{
assertTrue(r.isDef());
Expand Down Expand Up @@ -139,7 +151,7 @@ private void assertDefAndUseForXFound(UseDefGraph useDefGraph,

boolean isDefined = false, isUsed = false;

// make sure only 'definitions' of x exist
// make sure 'definitions' and 'uses' of x exist
for (UseOrDefRecord r : usesAndDefs)
{
if (r.isDef())
Expand Down

0 comments on commit 6f9f91a

Please sign in to comment.