Skip to content

Commit

Permalink
Fixed EOG handling in function declaration statements (#456)
Browse files Browse the repository at this point in the history
* Saving the EOG stack when handling a function declaration statement
* Only save/restore EOG stack for function declaration statements
  • Loading branch information
oxisto authored Jul 13, 2021
1 parent b8c2821 commit 0c1280a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ private void handleStatementHolder(StatementHolder statementHolder) {

private void handleFunctionDeclaration(@NonNull Node node) {
FunctionDeclaration funcDecl = (FunctionDeclaration) node;

// reset EOG
this.currentEOG.clear();

Expand Down Expand Up @@ -474,10 +475,20 @@ private void handleDeclarationStatement(@NonNull Node node) {
DeclarationStatement declarationStatement = (DeclarationStatement) node;
// loop through declarations
for (Declaration declaration : declarationStatement.getDeclarations()) {
if (declaration instanceof VariableDeclaration
|| declaration instanceof FunctionDeclaration) {
if (declaration instanceof VariableDeclaration) {
// analyze the initializers if there is one
createEOG(declaration);
} else if (declaration instanceof FunctionDeclaration) {
// save the current EOG stack, because we can have a function declaration within an
// existing function and the EOG handler for handling function declarations will reset the
// stack
var oldEOG = new ArrayList<>(this.currentEOG);

// analyze the defaults
createEOG(declaration);

// reset the oldEOG stack
this.currentEOG = oldEOG;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@
import de.fraunhofer.aisec.cpg.helpers.NodeComparator;
import de.fraunhofer.aisec.cpg.helpers.SubgraphWalker;
import de.fraunhofer.aisec.cpg.helpers.Util;
import de.fraunhofer.aisec.cpg.processing.IVisitor;
import de.fraunhofer.aisec.cpg.processing.strategy.Strategy;
import de.fraunhofer.aisec.cpg.sarif.PhysicalLocation;
import de.fraunhofer.aisec.cpg.sarif.Region;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -1260,4 +1263,34 @@ void testUnityBuild() throws Exception {
// should contain 3 declarations (2 include and 1 function decl from the include)
assertEquals(3, declarations.get(0).getDeclarations().size());
}

@Test
void testEOGCompleteness() throws Exception {
var file = new File("src/test/resources/fix-455/main.cpp");
var tu = TestUtils.analyzeAndGetFirstTU(List.of(file), file.getParentFile().toPath(), true);

var main = tu.getDeclarationsByName("main", FunctionDeclaration.class).iterator().next();
assertNotNull(main);

var body = (CompoundStatement) main.getBody();
assertNotNull(body);

var returnStatement = body.getStatements().get(body.getStatements().size() - 1);
assertNotNull(returnStatement);

// we need to assert, that we have a consistent chain of EOG edges from the first statement to
// the return statement. otherwise, the EOG chain is somehow broken
var eogEdges = new ArrayList<Node>();

main.accept(
Strategy::EOG_FORWARD,
new IVisitor<>() {
public void visit(Node n) {
System.out.println(n);
eogEdges.add(n);
}
});

assertTrue(eogEdges.contains(returnStatement));
}
}
17 changes: 17 additions & 0 deletions cpg-library/src/test/resources/fix-455/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Test {
public:
int call(int a) {
return a + 1;
}
};

int main() {
int foo = 42;
foo = 3;
Test t;
t.call(foo);

// note, this is not a construct expression, but a function declaration.
// however this is still just a regular declaration statement and the EOG should continue beyond it
Test t2();
}

0 comments on commit 0c1280a

Please sign in to comment.