Skip to content
This repository has been archived by the owner on Mar 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #29 from bzz/fix-npe-27
Browse files Browse the repository at this point in the history
Fix native driver NPE
  • Loading branch information
bzz authored Mar 20, 2019
2 parents 53e003a + bf7743c commit dfc7c13
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ target
native/.idea/*
native/src/main/java/tech/sourced/babelfish/j2cpp/*
vendor/
fixtures/*_got
.idea/*
*.iml
7 changes: 7 additions & 0 deletions fixtures/issue_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#if E

#ifndef F
#define F
#endif

#endif
32 changes: 32 additions & 0 deletions fixtures/issue_27.cpp.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
IASTClass: "CPPASTTranslationUnit",
LocOffsetEnd: 41,
LocOffsetStart: 0,
'Prop_BuiltinMacroDefinitions': [],
'Prop_PreprocStatements': [
{
Condition: "E",
IASTClass: "ASTIf",
IsTaken: false,
LocOffsetEnd: 5,
LocOffsetStart: 0,
},
{
Condition: "F",
IASTClass: "ASTIfndef",
IsTaken: false,
LocOffsetEnd: 16,
LocOffsetStart: 7,
},
{
IASTClass: "ASTEndif",
LocOffsetEnd: 33,
LocOffsetStart: 27,
},
{
IASTClass: "ASTEndif",
LocOffsetEnd: 41,
LocOffsetStart: 35,
},
],
}
82 changes: 82 additions & 0 deletions fixtures/issue_27.cpp.sem.uast
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{ '@type': "cpp:CPPASTTranslationUnit",
'@role': [File, Module],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
end: { '@type': "uast:Position",
offset: 41,
line: 7,
col: 7,
},
},
'Prop_BuiltinMacroDefinitions': [],
'Prop_PreprocStatements': [
{ '@type': "cpp:ASTIf",
'@role': [Unannotated],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
end: { '@type': "uast:Position",
offset: 5,
line: 1,
col: 6,
},
},
Condition: "E",
IsTaken: false,
},
{ '@type': "cpp:ASTIfndef",
'@role': [Unannotated],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 7,
line: 3,
col: 1,
},
end: { '@type': "uast:Position",
offset: 16,
line: 3,
col: 10,
},
},
Condition: "F",
IsTaken: false,
},
{ '@type': "cpp:ASTEndif",
'@role': [Incomplete, Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 27,
line: 5,
col: 1,
},
end: { '@type': "uast:Position",
offset: 33,
line: 5,
col: 7,
},
},
},
{ '@type': "cpp:ASTEndif",
'@role': [Incomplete, Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 35,
line: 7,
col: 1,
},
end: { '@type': "uast:Position",
offset: 41,
line: 7,
col: 7,
},
},
},
],
}
82 changes: 82 additions & 0 deletions fixtures/issue_27.cpp.uast
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{ '@type': "CPPASTTranslationUnit",
'@role': [File, Module],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
end: { '@type': "uast:Position",
offset: 41,
line: 7,
col: 7,
},
},
'Prop_BuiltinMacroDefinitions': [],
'Prop_PreprocStatements': [
{ '@type': "ASTIf",
'@role': [Unannotated],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 0,
line: 1,
col: 1,
},
end: { '@type': "uast:Position",
offset: 5,
line: 1,
col: 6,
},
},
Condition: "E",
IsTaken: false,
},
{ '@type': "ASTIfndef",
'@role': [Unannotated],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 7,
line: 3,
col: 1,
},
end: { '@type': "uast:Position",
offset: 16,
line: 3,
col: 10,
},
},
Condition: "F",
IsTaken: false,
},
{ '@type': "ASTEndif",
'@role': [Incomplete, Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 27,
line: 5,
col: 1,
},
end: { '@type': "uast:Position",
offset: 33,
line: 5,
col: 7,
},
},
},
{ '@type': "ASTEndif",
'@role': [Incomplete, Noop],
'@pos': { '@type': "uast:Positions",
start: { '@type': "uast:Position",
offset: 35,
line: 7,
col: 1,
},
end: { '@type': "uast:Position",
offset: 41,
line: 7,
col: 7,
},
},
},
],
}
4 changes: 1 addition & 3 deletions native/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
<version>3.0.0-M3</version>
</plugin>
</plugins>
</build>
Expand Down
23 changes: 13 additions & 10 deletions native/src/main/java/tech/sourced/babelfish/JsonASTVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Stack;
import java.lang.Comparable;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.Vector;

/// Visitor pattern implementation for the CPP AST. This will write every
/// node in the Json output. Since CDT unfortunately doesnt have something like JDT
Expand All @@ -25,10 +24,10 @@
/// children that derive for IASTNode (including all the ones returned by any method
/// of that node returning an IASTNode-derived object), a lot of nodes also have
/// other methods that return something not derived from IASTNode and thus not returned
/// by getChildren. So for all these IASTNode subinterfaces we must call and store the
/// by getChildren. So for all these IASTNode sub-interfaces we must call and store the
/// value of those non-IASTNode-returning methods. The most infamous is probably the
/// getOperator of some nodes that return an int than then you have to match in a switch
/// because the possible values are not even declarated in an enum but as final int
/// because the possible values are not even declared in an enum but as final int
/// class members.

public class JsonASTVisitor extends ASTVisitor {
Expand Down Expand Up @@ -1293,12 +1292,16 @@ private void serializePreproStatements(IASTTranslationUnit unit) throws IOExcept
IASTPreprocessorIfndefStatement s = (IASTPreprocessorIfndefStatement)stmt;
json.writeStringField("Condition", new String(s.getCondition()));
json.writeBooleanField("IsTaken", s.taken());
json.writeStringField("MacroReference", s.getMacroReference().toString());
if (s.getMacroReference() != null ) {
json.writeStringField("MacroReference", s.getMacroReference().toString());
}
} else if (stmt instanceof IASTPreprocessorIfdefStatement) {
IASTPreprocessorIfdefStatement s = (IASTPreprocessorIfdefStatement)stmt;
json.writeStringField("Condition", new String(s.getCondition()));
json.writeBooleanField("IsTaken", s.taken());
json.writeStringField("MacroReference", s.getMacroReference().toString());
if (s.getMacroReference() != null ) {
json.writeStringField("MacroReference", s.getMacroReference().toString());
}
} else if (stmt instanceof IASTPreprocessorElifStatement) {
IASTPreprocessorElifStatement s = (IASTPreprocessorElifStatement)stmt;
json.writeStringField("Condition", new String(s.getCondition()));
Expand Down

0 comments on commit dfc7c13

Please sign in to comment.