From 4cbaa89e52a420805705eee2f8b9e8b082181c68 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Thu, 13 Apr 2017 13:15:12 -0400 Subject: [PATCH 1/7] Add files via upload --- .../classifier/implicit-predicate.clj | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/atom_finder/classifier/implicit-predicate.clj diff --git a/src/atom_finder/classifier/implicit-predicate.clj b/src/atom_finder/classifier/implicit-predicate.clj new file mode 100644 index 0000000..d3804fe --- /dev/null +++ b/src/atom_finder/classifier/implicit-predicate.clj @@ -0,0 +1,38 @@ +(in-ns 'atom-finder.classifier) +(import '(org.eclipse.cdt.core.dom.ast IASTIfStatement IASTForStatement IASTWhileStatement IASTDoStatement IASTBinaryExpression)) + +(def logical-operators #{IASTBinaryExpression/op_equals + IASTBinaryExpression/op_greaterEqual + IASTBinaryExpression/op_greaterThan + IASTBinaryExpression/op_lessThan + IASTBinaryExpression/op_lessEqual + IASTBinaryExpression/op_logicalAnd + IASTBinaryExpression/op_logicalOr + IASTBinaryExpression/op_notequals}) + +(defn implicit-preciate-for-if-statement? + "Is there an implicit predicate in this if or for statement" + [node] + (cond + (instance? IASTBinaryExpression (.getConditionExpression node)) + (not (contains? logical-operators (.getOperator (.getConditionExpression node)))) + :else true)) + +(defn implicit-predicate-while-do-loop? + "Is there an implicit predicate in this do-while loop" + [node] + (cond + (instance? IASTBinaryExpression (.getCondition node)) + (not (contains? logical-operators (.getOperator (.getCondition node)))) + :else true)) + +(defn implicit-predicate-atom? + "Does this AST node have an implicit predicate atom" + [node] + (cond + (leaf? node) nil + (instance? IASTForStatement node) (implicit-preciate-for-if-statement? node) + (instance? IASTIfStatement node) (implicit-preciate-for-if-statement? node) + (instance? IASTWhileStatement node) (implicit-predicate-while-do-loop? node) + (instance? IASTDoStatement node) (implicit-predicate-while-do-loop? node) + :else false)) \ No newline at end of file From 94cfb684a647e337bcb8fed0714664837cc9ca6a Mon Sep 17 00:00:00 2001 From: jakeinc Date: Thu, 13 Apr 2017 13:17:00 -0400 Subject: [PATCH 2/7] Update classifier_test.clj --- test/atom_finder/classifier_test.clj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/atom_finder/classifier_test.clj b/test/atom_finder/classifier_test.clj index 07185cf..d8dcc29 100644 --- a/test/atom_finder/classifier_test.clj +++ b/test/atom_finder/classifier_test.clj @@ -151,3 +151,7 @@ (deftest test-assignment-as-value-atom? (testing "assignment-as-value-atom? finds all atoms in snippet study code" (test-atom-lines "assignment-as-value.c" "" (default-finder assignment-as-value-atom?)))) + +(deftest test-implicit-predicate-atom? + (testing "implicit-predicate-atom? finds all atoms in sample code") + (test-atom-lines "implicit-predicate.c" "" (default-finder implicit-predicate-atom?))) From d02a65da015d20b5c10143a786291d0538041785 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Thu, 13 Apr 2017 13:18:02 -0400 Subject: [PATCH 3/7] Add files via upload --- src/test/resources/implicit-predicate.c | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/test/resources/implicit-predicate.c diff --git a/src/test/resources/implicit-predicate.c b/src/test/resources/implicit-predicate.c new file mode 100644 index 0000000..51ca7d7 --- /dev/null +++ b/src/test/resources/implicit-predicate.c @@ -0,0 +1,35 @@ +int main() +{ + int y = 5; + + for (int x = 4; 4 != 4; x++); + + for (int x = 5; 4 - 4; x++); // + + for (int x = 4; 4 == 4; x++); + + for (int x = 4; y = 4; x++); // + + for (int x = 4; 8 * 9 + 4; x++); // + + if (3 < 6) + if (3 | 6); // + + if (6 & 8) // + if (2 ^ 9); // + + if (3 > 6); + + if (sizeof(34)); // + + if (y >= 6); + + while (y || 3) + while (y); // + + while (y <= 34); + + while (y && y); + + return; +} \ No newline at end of file From 38ee52e07d71bb14289935e10e70f42b9399da83 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Thu, 27 Apr 2017 00:26:26 -0400 Subject: [PATCH 4/7] Update classifier.clj --- src/atom_finder/classifier.clj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/atom_finder/classifier.clj b/src/atom_finder/classifier.clj index c63b76b..9460e33 100644 --- a/src/atom_finder/classifier.clj +++ b/src/atom_finder/classifier.clj @@ -43,7 +43,7 @@ (def atoms [ - (ValidatedAtom :preprocessor-in-statement define-parent? non-toplevel-defines) + (ValidatedAtom :preprocessor-in-statement preprocessor-parent? all-non-toplevel-preprocessors) (ValidatedAtom :logic-as-control-flow logic-as-control-flow-atom? logic-as-control-flow-atoms) (ValidatedAtom :conditional conditional-atom? (default-finder conditional-atom?)) (ValidatedAtom :reversed-subscript reversed-subscript-atom? (default-finder reversed-subscript-atom?)) @@ -53,6 +53,7 @@ (ValidatedAtom :omitted-curly-braces omitted-curly-braces-atom? (default-finder omitted-curly-braces-atom?)) (ValidatedAtom :assignment-as-value assignment-as-value-atom? (default-finder assignment-as-value-atom?)) (ValidatedAtom :macro-operator-precedence macro-def-precedence-atom? macro-operator-precedence-atoms) + (ValidatedAtom :implicit-predicate implicit-predicate-atom? (default-finder implicit-predicate-atom?)) ] ) From 543e85b1f473390e0f6aaad617a6a29ac2448666 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Thu, 27 Apr 2017 00:27:30 -0400 Subject: [PATCH 5/7] Update classifier_util.clj --- src/atom_finder/classifier_util.clj | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/atom_finder/classifier_util.clj b/src/atom_finder/classifier_util.clj index c20cea8..5fd26bd 100644 --- a/src/atom_finder/classifier_util.clj +++ b/src/atom_finder/classifier_util.clj @@ -129,4 +129,13 @@ (if (or (nil? node) (instance? IASTFunctionDefinition node)) node - (enclosing-function (parent node)))) \ No newline at end of file + (enclosing-function (parent node)))) + +(def logical-operators #{IASTBinaryExpression/op_equals + IASTBinaryExpression/op_greaterEqual + IASTBinaryExpression/op_greaterThan + IASTBinaryExpression/op_lessThan + IASTBinaryExpression/op_lessEqual + IASTBinaryExpression/op_logicalAnd + IASTBinaryExpression/op_logicalOr + IASTBinaryExpression/op_notequals}) From c6b520aba6aa7504acf97c8baf950ddea6e6f0e2 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Mon, 1 May 2017 14:49:57 -0400 Subject: [PATCH 6/7] Update implicit-predicate.c --- src/test/resources/implicit-predicate.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/test/resources/implicit-predicate.c b/src/test/resources/implicit-predicate.c index 51ca7d7..c127ab2 100644 --- a/src/test/resources/implicit-predicate.c +++ b/src/test/resources/implicit-predicate.c @@ -2,13 +2,15 @@ int main() { int y = 5; + (y == 5) ? y = 5 : y == 7; + for (int x = 4; 4 != 4; x++); for (int x = 5; 4 - 4; x++); // for (int x = 4; 4 == 4; x++); - for (int x = 4; y = 4; x++); // + (y = 4) ? y : y++; // for (int x = 4; 8 * 9 + 4; x++); // @@ -20,16 +22,22 @@ int main() if (3 > 6); + ((y != 5)) ? y : y; + if (sizeof(34)); // - if (y >= 6); + if ((y >= 6)); while (y || 3) while (y); // while (y <= 34); - while (y && y); + (((y = 2))) ? 4 : 5; // + + while (!(y && y)); + + if (!(5 == y)); return; -} \ No newline at end of file +} From e1f5e456064c1b38de0a1420b75033c7e4779496 Mon Sep 17 00:00:00 2001 From: jakeinc Date: Mon, 1 May 2017 14:50:13 -0400 Subject: [PATCH 7/7] Update implicit-predicate.clj --- .../classifier/implicit-predicate.clj | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/atom_finder/classifier/implicit-predicate.clj b/src/atom_finder/classifier/implicit-predicate.clj index d3804fe..564d9cf 100644 --- a/src/atom_finder/classifier/implicit-predicate.clj +++ b/src/atom_finder/classifier/implicit-predicate.clj @@ -1,38 +1,38 @@ (in-ns 'atom-finder.classifier) -(import '(org.eclipse.cdt.core.dom.ast IASTIfStatement IASTForStatement IASTWhileStatement IASTDoStatement IASTBinaryExpression)) +(import '(org.eclipse.cdt.core.dom.ast IASTIfStatement IASTForStatement IASTWhileStatement IASTDoStatement IASTBinaryExpression IASTUnaryExpression)) -(def logical-operators #{IASTBinaryExpression/op_equals - IASTBinaryExpression/op_greaterEqual - IASTBinaryExpression/op_greaterThan - IASTBinaryExpression/op_lessThan - IASTBinaryExpression/op_lessEqual - IASTBinaryExpression/op_logicalAnd - IASTBinaryExpression/op_logicalOr - IASTBinaryExpression/op_notequals}) +(def implicit-types #{IASTIfStatement IASTForStatement IASTWhileStatement IASTDoStatement IASTConditionalExpression}) +; If statement as an implicit type: C99 6.8.4.1/1 +; For/While/Do statements as implicit types: C99 6.8.5/2 +; Ternary operator as an implicit type: C99 6.5.15/1 -(defn implicit-preciate-for-if-statement? - "Is there an implicit predicate in this if or for statement" - [node] - (cond - (instance? IASTBinaryExpression (.getConditionExpression node)) - (not (contains? logical-operators (.getOperator (.getConditionExpression node)))) - :else true)) +(declare implicit-expression?) -(defn implicit-predicate-while-do-loop? - "Is there an implicit predicate in this do-while loop" - [node] - (cond - (instance? IASTBinaryExpression (.getCondition node)) - (not (contains? logical-operators (.getOperator (.getCondition node)))) - :else true)) +(defn remove-parentheses +[expr] +(cond + (== IASTUnaryExpression/op_not (.getOperator expr)) true + (== IASTUnaryExpression/op_bracketedPrimary (.getOperator expr)) + (not (implicit-expression? (.getOperand expr))) + :else false)) + +(defn implicit-expression? + "Does this expression have a logical operator in it?" + [expr] + (not (cond + (instance? IASTBinaryExpression expr) + (contains? logical-operators (.getOperator expr)) + (instance? IASTUnaryExpression expr) + (remove-parentheses expr) + :else false))) (defn implicit-predicate-atom? "Does this AST node have an implicit predicate atom" [node] (cond - (leaf? node) nil - (instance? IASTForStatement node) (implicit-preciate-for-if-statement? node) - (instance? IASTIfStatement node) (implicit-preciate-for-if-statement? node) - (instance? IASTWhileStatement node) (implicit-predicate-while-do-loop? node) - (instance? IASTDoStatement node) (implicit-predicate-while-do-loop? node) - :else false)) \ No newline at end of file + (instance? IASTForStatement node) (implicit-expression? (.getConditionExpression node)) + (instance? IASTIfStatement node) (implicit-expression? (.getConditionExpression node)) + (instance? IASTWhileStatement node) (implicit-expression? (.getCondition node)) + (instance? IASTDoStatement node) (implicit-expression? (.getCondition node)) + (instance? IASTConditionalExpression node) (implicit-expression? (.getLogicalConditionExpression node)) + :else false))