From b39d51ce1c4f7852e966436e733b3c72cbc0a4d0 Mon Sep 17 00:00:00 2001 From: "Hongwei (Henry) Zhou" Date: Sat, 22 Jul 2017 18:58:32 -0400 Subject: [PATCH 1/5] Initial redundant parentheses stats --- src/atom_finder/util/10_clj_util.clj | 7 +++++++ src/atom_finder/util/cdt_util.clj | 14 -------------- src/atom_finder/util/writer-util.clj | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/atom_finder/util/10_clj_util.clj b/src/atom_finder/util/10_clj_util.clj index b93d08e..caad384 100644 --- a/src/atom_finder/util/10_clj_util.clj +++ b/src/atom_finder/util/10_clj_util.clj @@ -96,6 +96,13 @@ ~else) then))) +(defmacro when-let* + [bindings & then] + (if (seq bindings) + `(when-let [~(first bindings) ~(second bindings)] + (when-let* ~(drop 2 bindings) ~@then)) + `(do ~@then))) + (defn bin "Convert a value to 1 or 0 based on its truthiness" [bool] (if bool 1 0)) diff --git a/src/atom_finder/util/cdt_util.clj b/src/atom_finder/util/cdt_util.clj index f5c2b85..8871282 100644 --- a/src/atom_finder/util/cdt_util.clj +++ b/src/atom_finder/util/cdt_util.clj @@ -248,20 +248,6 @@ (defn all-comments [node] (->> node root-ancestor .getComments (into []))) -(defn print-node-context - "Print the line that contains the node and the lines around it" - ([node] (print-node-context 2 node)) - ([n-lines node] - (with-open [rdr (clojure.java.io/reader (.getContainingFilename node))] - (let [line-num (start-line node) - file-seq (line-seq rdr) - first-line (max 0 (- line-num n-lines 1)) - lines-to-print (->> file-seq (drop first-line) (take (+ n-lines 1 n-lines)))] - (println "===================================================") - (doseq-indexed [line lines-to-print idx] - (println (str (+ idx first-line) (if (= (+ idx first-line 1) line-num) " >> " " ") line))) - (println "==================================================="))))) - (defn count-nodes "Count the size of the ast" [node] diff --git a/src/atom_finder/util/writer-util.clj b/src/atom_finder/util/writer-util.clj index b03fcbc..e40749f 100644 --- a/src/atom_finder/util/writer-util.clj +++ b/src/atom_finder/util/writer-util.clj @@ -23,6 +23,21 @@ node 1 [])) +(defn print-node-context + "Print the line that contains the node and the lines around it" + ([node] (print-node-context 2 node)) + ([n-lines node] + (with-open [rdr (clojure.java.io/reader (.getContainingFilename node))] + (let [line-num (start-line node) + file-seq (line-seq rdr) + first-line (max 0 (- line-num n-lines 1)) + lines-to-print (->> file-seq (drop first-line) (take (+ n-lines 1 n-lines)))] + (println "===================================================") + (doseq-indexed [line lines-to-print idx] + (println (str (+ idx first-line) (if (= (+ idx first-line 1) line-num) " >> " " ") line))) + (println "==================================================="))))) + + (def ast-writer (ASTWriter.)) (defn write-ast [node] (.write ast-writer node)) From ce2e658c8d0f529291a90ac51c3c0fe79f730594 Mon Sep 17 00:00:00 2001 From: "Hongwei (Henry) Zhou" Date: Sat, 22 Jul 2017 18:58:39 -0400 Subject: [PATCH 2/5] Initial redundant parentheses stats --- .../questions/redundent_parentheses.clj | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/atom_finder/questions/redundent_parentheses.clj diff --git a/src/atom_finder/questions/redundent_parentheses.clj b/src/atom_finder/questions/redundent_parentheses.clj new file mode 100644 index 0000000..62bba70 --- /dev/null +++ b/src/atom_finder/questions/redundent_parentheses.clj @@ -0,0 +1,67 @@ +(ns atom-finder.questions.redundant-parentheses + (:require + [clj-jgit.internal :as giti] + [clj-jgit.porcelain :as gitp] + [clj-jgit.querying :as gitq] + [atom-finder.patch :refer :all] + [atom-finder.atom-patch :refer :all] + [atom-finder.constants :refer :all] + [atom-finder.util :refer :all] + [atom-finder.classifier :refer [operator-group confusing-operator-combination?]] + [clojure.pprint :refer [pprint]] + [clojure.string :as string] + ) + (:import + [org.eclipse.cdt.core.dom.ast IASTBinaryExpression] + [org.eclipse.cdt.internal.core.dom.parser.cpp CPPASTConditionalExpression] + )) + +(defn paren-in-children + [node] + (find-first paren-node? (children node))) + +(defn redundant-paren? + [node] + (when-let* [paren-test (paren-node? node) + parent-level (precedence-level (safe-parent node)) + children-level (precedence-level(first (children node)))] + (or (< children-level parent-level) + (and (= children-level parent-level) + (= node (first (children (safe-parent node)))))))) + +(defn atom-without-paren? + "Given a parenthesis node, would this be an operator-precedence-atom without the parentheses?" + [node] + (when-let* [parent-node (safe-parent node) + parent-group (operator-group parent-node) + child-group (operator-group (first (children node))) + opt-group-combination [parent-group child-group]] + + (if + (and (or (instance? IASTBinaryExpression parent-node) + (instance? CPPASTConditionalExpression parent-node)) + (= node (first (children parent-node)))) + (confusing-operator-combination? (reverse opt-group-combination)) + (confusing-operator-combination? opt-group-combination)))) + +(->> "1 - (2 + 3)" parse-expr (get-in-tree [1]) (#(and (atom-without-paren? %) + (redundant-paren? %)))) + +(->> gcc-path + (pmap-dir-trees (fn [root] (filter-tree redundant-paren? root))) + flatten + (remove nil?) + ;(map safe-parent) + ;(map write-ast) + ;(map prn) + ;(take 100000) + ((fn [a] (prn (count a)) a)) + (map atom-without-paren?) + (remove (fn [input] (or (nil? input) (false? input)))) + count + prn + dorun + time-mins) + +;total: 392166 +;would-be-atom: 39470 From 2994429b67b4a12e8ef0d6259a0f1beda4e3c8ab Mon Sep 17 00:00:00 2001 From: "Hongwei (Henry) Zhou" Date: Mon, 24 Jul 2017 14:14:36 -0400 Subject: [PATCH 3/5] adjusted operator-precedence --- .../classifier/operator-precedence.clj | 27 ++++++-- ...entheses.clj => redundant_parentheses.clj} | 66 ++++++++++++------- src/test/resources/operator-precedence.c | 3 + 3 files changed, 68 insertions(+), 28 deletions(-) rename src/atom_finder/questions/{redundent_parentheses.clj => redundant_parentheses.clj} (53%) diff --git a/src/atom_finder/classifier/operator-precedence.clj b/src/atom_finder/classifier/operator-precedence.clj index 1c3e7e3..62482b1 100644 --- a/src/atom_finder/classifier/operator-precedence.clj +++ b/src/atom_finder/classifier/operator-precedence.clj @@ -47,10 +47,9 @@ IASTBinaryExpression/op_logicalOr :or}] (->> node .getOperator operator-group-table)))) -(def order-insensitive-opt-combination +(def order-insensitive-opt-combination-underclassify (into #{} (map sort [[:multiply :add] [:and :add] [:and :multiply] [:or :add] [:or :multiply] [:or :and] [:not :arith_unary] - ;;[:compare :and] [:compare :or] [:compare :compare]// Underclassify [:cond :cond] [:non-asso :multiply] [:non-asso :and] [:non-asso :or] [:non-asso :non-asso] [:field_ref :pointer] @@ -59,6 +58,10 @@ [:bitwise_bin :add] [:bitwise_bin :multiply] [:bitwise_bin :and] [:biwise_bin :or] [:bitwise_bin :compare] [:bitwise_bin :pointer] [:bitwise_bin :non-asso]]))) +(def order-insensitive-opt-combination-overclassify + "Order insensitive combinations that are considered overclassifying" + (into #{} (map sort [[:compare :and] [:compare :or] [:compare :compare]]))) + (def order-sensitive-opt-combination #{[:pointer :de_incr] [:arith_unary :add] [:arith_unary :multiply] [:arith_unary :and] [:arith_unary :or] [:not :add] [:not :multiply] [:not :and] [:not :or] @@ -71,15 +74,20 @@ [:bitwise_not :de_incr] [:arith_unary :bitwise_bin] [:not :bitwise_bin] [:bitwise_bin :cond] [:bitwise_not :bitwise_bin]}) -(defn confusing-operator-combination? +(defn underclassify-confusing-operator-combination? "Is this combination of the operator groups confusing?" [combination] (or (order-sensitive-opt-combination combination) - (order-insensitive-opt-combination (sort combination)) + (order-insensitive-opt-combination-underclassify (sort combination)) - ;;SPECIAL CASE 1: Comma operator in any combination; + ;;SPECIAL CASE: Comma operator in any combination; (some (partial = :comma) combination))) +(defn overclassify-confusing-operator-combination? + [combination] + (or (underclassify-confusing-operator-combination? combination) + (order-insensitive-opt-combination-overclassify (sort combination)))) + (defn operator-group-pair? [node] (and (operator-group node) @@ -141,4 +149,11 @@ (not (= :assign (operator-group node))) - (some confusing-operator-combination? (group-pair node)))) + + (or + ;;SPECIAL CASE 2: [:bitwise_bin :bitwise_bin] is confusing if the two are different + (and (= :bitwise_bin (operator-group node)) + (some #(and (= :bitwise_bin (operator-group %1)) + (not (= (.getOperator node) (.getOperator %1)))) (children node))) + + (some underclassify-confusing-operator-combination? (group-pair node))))) diff --git a/src/atom_finder/questions/redundent_parentheses.clj b/src/atom_finder/questions/redundant_parentheses.clj similarity index 53% rename from src/atom_finder/questions/redundent_parentheses.clj rename to src/atom_finder/questions/redundant_parentheses.clj index 62bba70..36c5243 100644 --- a/src/atom_finder/questions/redundent_parentheses.clj +++ b/src/atom_finder/questions/redundant_parentheses.clj @@ -7,7 +7,7 @@ [atom-finder.atom-patch :refer :all] [atom-finder.constants :refer :all] [atom-finder.util :refer :all] - [atom-finder.classifier :refer [operator-group confusing-operator-combination?]] + [atom-finder.classifier :refer [operator-group underclassify-confusing-operator-combination? overclassify-confusing-operator-combination?]] [clojure.pprint :refer [pprint]] [clojure.string :as string] ) @@ -37,31 +37,53 @@ child-group (operator-group (first (children node))) opt-group-combination [parent-group child-group]] - (if + (cond + + (and (= :bitwise_bin parent-group) + (= parent-group child-group)) + (not (= (.getOperator parent-node) (.getOperator (first (children node))))) + + (and (or (instance? IASTBinaryExpression parent-node) (instance? CPPASTConditionalExpression parent-node)) (= node (first (children parent-node)))) - (confusing-operator-combination? (reverse opt-group-combination)) - (confusing-operator-combination? opt-group-combination)))) + (underclassify-confusing-operator-combination? (reverse opt-group-combination)) + + :else(underclassify-confusing-operator-combination? opt-group-combination)))) -(->> "1 - (2 + 3)" parse-expr (get-in-tree [1]) (#(and (atom-without-paren? %) - (redundant-paren? %)))) +(->> "(fclose(f1a) == EOF) || (fclose(f1b) | EOF)" parse-expr + ;print-tree + (get-in-tree [0]) + ;print-tree + ;redundant-paren? + atom-without-paren? +) (->> gcc-path - (pmap-dir-trees (fn [root] (filter-tree redundant-paren? root))) - flatten - (remove nil?) - ;(map safe-parent) - ;(map write-ast) - ;(map prn) - ;(take 100000) - ((fn [a] (prn (count a)) a)) - (map atom-without-paren?) - (remove (fn [input] (or (nil? input) (false? input)))) - count - prn - dorun - time-mins) + (pmap-dir-trees (fn [root] (filter-tree ;redundant-paren? + #(and (redundant-paren? %)(atom-without-paren? %)) + root))) + flatten + (remove nil?) + (map safe-parent) + (map write-ast) + (map #(spit "underclas-atom-without.txt" (str % "\r\n") :append true)) + (take 100) + + ;((fn [a] (prn (count a)) a)) + ;(map atom-without-paren?) + ;(remove (fn [input] (or (nil? input) (false? input)))) + + count + prn + dorun + time-mins) + +;redundant-total: 392166 + +;Underclassify: +;would-be-atom: 66037 + +;Overclassify: +;would-be-atom: 75855 -;total: 392166 -;would-be-atom: 39470 diff --git a/src/test/resources/operator-precedence.c b/src/test/resources/operator-precedence.c index af73e65..e570e82 100644 --- a/src/test/resources/operator-precedence.c +++ b/src/test/resources/operator-precedence.c @@ -166,4 +166,7 @@ int main(){ ~str++;// --~str; + + a = a << 1 << 2; //, underclassify + a = a | 2 & 3; // } From 5a260981cdcb9348c48f08cb75b55066951c4fdd Mon Sep 17 00:00:00 2001 From: "Hongwei (Henry) Zhou" Date: Tue, 19 Sep 2017 19:36:39 -0400 Subject: [PATCH 4/5] Test --- .../classifier/operator-precedence.clj | 30 ++++++------ .../questions/redundant_parentheses.clj | 46 +++++++++++++------ src/atom_finder/util/classifier_util.clj | 12 +++-- src/conf/henry.edn | 6 +-- 4 files changed, 59 insertions(+), 35 deletions(-) diff --git a/src/atom_finder/classifier/operator-precedence.clj b/src/atom_finder/classifier/operator-precedence.clj index 62482b1..a077095 100644 --- a/src/atom_finder/classifier/operator-precedence.clj +++ b/src/atom_finder/classifier/operator-precedence.clj @@ -5,7 +5,7 @@ (defmulti operator-group"Returns the name of the operator group that the node belongs to, nil not defined in this operator-group" class) (defmethod operator-group :default [node] (let [operator-group-table - {CPPASTConditionalExpression :cond + {CPPASTConditionalExpression :ternary CPPASTExpressionList :comma CPPASTFieldReference :field_ref}] (->> node type operator-group-table))) @@ -50,12 +50,12 @@ (def order-insensitive-opt-combination-underclassify (into #{} (map sort [[:multiply :add] [:and :add] [:and :multiply] [:or :add] [:or :multiply] [:or :and] [:not :arith_unary] - [:cond :cond] [:non-asso :multiply] + [:ternary :ternary] [:non-asso :multiply] [:non-asso :and] [:non-asso :or] [:non-asso :non-asso] [:field_ref :pointer] [:bitwise_not :arith_unary] [:bitwise_not :not] [:bitwise_not :pointer] - [:bitwise_bin :add] [:bitwise_bin :multiply] [:bitwise_bin :and] [:biwise_bin :or] + [:bitwise_bin :add] [:bitwise_bin :multiply] [:bitwise_bin :and] [:bitwise_bin :or] [:bitwise_bin :compare] [:bitwise_bin :pointer] [:bitwise_bin :non-asso]]))) (def order-insensitive-opt-combination-overclassify @@ -65,13 +65,13 @@ (def order-sensitive-opt-combination #{[:pointer :de_incr] [:arith_unary :add] [:arith_unary :multiply] [:arith_unary :and] [:arith_unary :or] [:not :add] [:not :multiply] [:not :and] [:not :or] - [:not :compare] [:pointer :add] [:arith_unary :cond] [:and :cond] - [:or :cond] [:not :cond] [:compare :cond] [:non-asso :add] + [:not :compare] [:pointer :add] [:arith_unary :ternary] [:and :ternary] + [:or :ternary] [:not :ternary] [:compare :ternary] [:non-asso :add] [:arith_unary :non-asso] [:not :non-asso] - [:bitwise_not :add] [:bitwise_not :multiply] [:bitwise_not :or] [:bitwise_not :cond] + [:bitwise_not :add] [:bitwise_not :multiply] [:bitwise_not :or] [:bitwise_not :ternary] [:bitwise_not :compare] [:bitwise_not :non-asso] [:bitwise-not :field_ref] - [:bitwise_not :de_incr] [:arith_unary :bitwise_bin] [:not :bitwise_bin] [:bitwise_bin :cond] + [:bitwise_not :de_incr] [:arith_unary :bitwise_bin] [:not :bitwise_bin] [:bitwise_bin :ternary] [:bitwise_not :bitwise_bin]}) (defn underclassify-confusing-operator-combination? @@ -81,7 +81,7 @@ (order-insensitive-opt-combination-underclassify (sort combination)) ;;SPECIAL CASE: Comma operator in any combination; - (some (partial = :comma) combination))) + (if (some (partial = :comma) combination) [:comma :*] false))) (defn overclassify-confusing-operator-combination? [combination] @@ -117,15 +117,15 @@ (let [node-group (operator-group node)] (cond (instance? IASTUnaryExpression node) - [[node-group (nth child-groups 0)]] + [[node-group (first child-groups)]] (instance? IASTBinaryExpression node) (-> node unary-before-binary-pairs - (conj [(nth child-groups 0) node-group] [node-group (nth child-groups 1)])) + (conj [(first child-groups) node-group] [node-group (second child-groups)])) (instance? CPPASTConditionalExpression node) - [[(nth child-groups 0) node-group] [node-group (nth child-groups 1)] [node-group (nth child-groups 2)]] + [[(first child-groups) node-group] [node-group (second child-groups)] [node-group (nth child-groups 2 nil)]] :else (map (fn [child-group] [node-group child-group]) child-groups)))) @@ -152,8 +152,10 @@ (or ;;SPECIAL CASE 2: [:bitwise_bin :bitwise_bin] is confusing if the two are different - (and (= :bitwise_bin (operator-group node)) - (some #(and (= :bitwise_bin (operator-group %1)) - (not (= (.getOperator node) (.getOperator %1)))) (children node))) + (if + (and (= :bitwise_bin (operator-group node)) + (some #(and (= :bitwise_bin (operator-group %1)) + (not (= (.getOperator node) (.getOperator %1)))) (children node))) + [:bitwise_bin :bitwise_bin] false) (some underclassify-confusing-operator-combination? (group-pair node))))) diff --git a/src/atom_finder/questions/redundant_parentheses.clj b/src/atom_finder/questions/redundant_parentheses.clj index 36c5243..3a4fbd4 100644 --- a/src/atom_finder/questions/redundant_parentheses.clj +++ b/src/atom_finder/questions/redundant_parentheses.clj @@ -7,7 +7,8 @@ [atom-finder.atom-patch :refer :all] [atom-finder.constants :refer :all] [atom-finder.util :refer :all] - [atom-finder.classifier :refer [operator-group underclassify-confusing-operator-combination? overclassify-confusing-operator-combination?]] + [atom-finder.classifier :refer [operator-group underclassify-confusing-operator-combination? + overclassify-confusing-operator-combination? operator-precedence-atom?]] [clojure.pprint :refer [pprint]] [clojure.string :as string] ) @@ -39,9 +40,10 @@ (cond - (and (= :bitwise_bin parent-group) - (= parent-group child-group)) - (not (= (.getOperator parent-node) (.getOperator (first (children node))))) + (and (and (= :bitwise_bin parent-group) + (= parent-group child-group)) + (not (= (.getOperator parent-node) (.getOperator (first (children node)))))) + [:bitwise_bin :bitwise_bin] (and (or (instance? IASTBinaryExpression parent-node) @@ -51,7 +53,10 @@ :else(underclassify-confusing-operator-combination? opt-group-combination)))) -(->> "(fclose(f1a) == EOF) || (fclose(f1b) | EOF)" parse-expr +; +;========GENERAL TESTING============== +; +(->> "(fclose(f1a) | EOF) & (fclose(f1b) | EOF)" parse-expr ;print-tree (get-in-tree [0]) ;print-tree @@ -59,26 +64,39 @@ atom-without-paren? ) -(->> gcc-path - (pmap-dir-trees (fn [root] (filter-tree ;redundant-paren? - #(and (redundant-paren? %)(atom-without-paren? %)) +(def codebase_name "wcdb") + +; +;=======CODEBASE DATA GATHERING========== +; +(->> (str "d:/Codebases/" codebase_name) + (pmap-dir-trees (fn [root] (filter-tree + redundant-paren? + ;#(and (redundant-paren? %)(atom-without-paren? %)) + ;operator-precedence-atom? root))) flatten (remove nil?) - (map safe-parent) - (map write-ast) - (map #(spit "underclas-atom-without.txt" (str % "\r\n") :append true)) - (take 100) + ;(map safe-parent) + ;(map write-ast) + ;(map #(spit "underclas-atom-without.txt" (str % "\r\n") :append true)) + ;(take 100) ;((fn [a] (prn (count a)) a)) - ;(map atom-without-paren?) - ;(remove (fn [input] (or (nil? input) (false? input)))) + (map atom-without-paren?) + ;(map operator-precedence-atom?) + (remove (fn [input] (or (nil? input) (false? input)))) + frequencies + (sort-by last) + (map #(spit (str "c:/Users/Henry/Desktop/stuff/would-be-atoms/" (str codebase_name "-rpa.txt")) (str % "\r\n") :append true)) count prn dorun time-mins) +;IASTNode total: 31886519 + ;redundant-total: 392166 ;Underclassify: diff --git a/src/atom_finder/util/classifier_util.clj b/src/atom_finder/util/classifier_util.clj index 9fea442..ebf1e5f 100644 --- a/src/atom_finder/util/classifier_util.clj +++ b/src/atom_finder/util/classifier_util.clj @@ -4,7 +4,7 @@ IASTBinaryExpression IASTLiteralExpression IASTForStatement IASTFunctionDefinition IASTArraySubscriptExpression IASTCastExpression IASTFunctionCallExpression IASTFieldReference IASTFunctionDefinition) - '(org.eclipse.cdt.internal.core.dom.parser.cpp CPPASTExpressionList CPPASTConditionalExpression)) + '(org.eclipse.cdt.internal.core.dom.parser.cpp CPPASTQualifiedName CPPASTExpressionList CPPASTConditionalExpression CPPASTNewExpression CPPASTDeleteExpression)) (defn function-node? [node] (instance? IASTFunctionDefinition node)) @@ -214,14 +214,16 @@ (defn in-function? [node] (ancestral-instance? IASTFunctionDefinition node)) -;;Note: Operators missing from the list: (scope/ resolution ::) (memory allocation/ new new[] delete delete[]) (pointer-to-member/ ->* .*) (defmulti precedence-level "returns the precedence level of the node" class) (defmethod precedence-level :default [node] (let [precedence-list - {IASTArraySubscriptExpression 2 + {CPPASTQualifiedName 1 + IASTArraySubscriptExpression 2 IASTFieldReference 2 IASTCastExpression 2 IASTFunctionCallExpression 2 + CPPASTNewExpression 3 + CPPASTDeleteExpression 3 CPPASTConditionalExpression 15 CPPASTExpressionList 16}] (precedence-list (type node)))) @@ -242,7 +244,9 @@ (precedence-list (.getOperator node)))) (defmethod precedence-level IASTBinaryExpression [node] (let [precedence-list - {IASTBinaryExpression/op_modulo 5 + {IASTBinaryExpression/op_pmdot 4 + IASTBinaryExpression/op_pmarrow 4 + IASTBinaryExpression/op_modulo 5 IASTBinaryExpression/op_multiply 5 IASTBinaryExpression/op_divide 5 IASTBinaryExpression/op_plus 6 diff --git a/src/conf/henry.edn b/src/conf/henry.edn index 68e76b2..47278eb 100644 --- a/src/conf/henry.edn +++ b/src/conf/henry.edn @@ -1,5 +1,5 @@ { - :gcc-path "~/opt/src/gcc", - :ag-path "~/opt/src/the_silver_searcher", - :github-top-c "~/opt/src/github-top-c" + :gcc-path "d:/Codebases/gcc/opt/src/gcc", + :ag-path "d:/Codebases/gcc/opt/src/the_silver_searcher", + :github-top-c "d:/Codebases/gcc/opt/src/github-top-c" } From 888c6b781854e726e3da5fa01e444d0d1b8bd397 Mon Sep 17 00:00:00 2001 From: "Hongwei (Henry) Zhou" Date: Sat, 23 Sep 2017 15:38:27 -0400 Subject: [PATCH 5/5] fixed operator precedence --- .../classifier/operator-precedence.clj | 22 ++++++++++++------- .../questions/redundant_parentheses.clj | 13 +++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/atom_finder/classifier/operator-precedence.clj b/src/atom_finder/classifier/operator-precedence.clj index d12715c..bbe3694 100644 --- a/src/atom_finder/classifier/operator-precedence.clj +++ b/src/atom_finder/classifier/operator-precedence.clj @@ -78,7 +78,9 @@ (order-insensitive-opt-combination (sort combination)) ;;SPECIAL CASE 1: Comma operator in any combination; - (some (partial = :comma) combination))) + (some (partial = :comma) combination) + ;SPECIAL CASE 2: note that the pair name is not [:bitwise_bin :bitwise-bin] + (= [:bitwise--bin :bitwise--bin] combination))) (defn operator-group-pair? [node] @@ -105,12 +107,18 @@ (defn group-pairs-in-node "Returns all operator group pairs in a node" - [node child-groups] - (let [node-group (operator-group node)] + [node child-nodes] + (let [node-group (operator-group node) child-groups (map operator-group child-nodes)] (cond (instance? IASTUnaryExpression node) [[node-group (safe-nth child-groups 0)]] + ;;SPECIAL CASE 2: Two bitwise_bins that are not the same, note that the group pair name is not [:bitwise_bin and bitwise_bin] + (and (and (= :bitwise_bin node-group) + (= :bitwise_bin (safe-nth child-groups 1))) + (not (= (.getOperator node) (.getOperator (safe-nth child-nodes 1))))) + [[:bitwise--bin :bitwise--bin]] + (instance? IASTBinaryExpression node) (-> node unary-before-binary-pairs @@ -129,16 +137,14 @@ ([node collection] (->> collection - (map operator-group) (group-pairs-in-node node) (remove (partial some nil?))))) (defn operator-precedence-atom? "Is this node an operator-precedence-atom?" [node] - (and - (operator-group-pair? node) + (and (operator-group-pair? node) - (not (= :assign (operator-group node))) + (not (= :assign (operator-group node))) - (some confusing-operator-combination? (group-pair node)))) + (some confusing-operator-combination? (group-pair node)))) diff --git a/src/atom_finder/questions/redundant_parentheses.clj b/src/atom_finder/questions/redundant_parentheses.clj index 3a4fbd4..cf3d8e2 100644 --- a/src/atom_finder/questions/redundant_parentheses.clj +++ b/src/atom_finder/questions/redundant_parentheses.clj @@ -7,8 +7,8 @@ [atom-finder.atom-patch :refer :all] [atom-finder.constants :refer :all] [atom-finder.util :refer :all] - [atom-finder.classifier :refer [operator-group underclassify-confusing-operator-combination? - overclassify-confusing-operator-combination? operator-precedence-atom?]] + [atom-finder.classifier :refer [operator-group confusing-operator-combination? + operator-precedence-atom?]] [clojure.pprint :refer [pprint]] [clojure.string :as string] ) @@ -49,9 +49,9 @@ (and (or (instance? IASTBinaryExpression parent-node) (instance? CPPASTConditionalExpression parent-node)) (= node (first (children parent-node)))) - (underclassify-confusing-operator-combination? (reverse opt-group-combination)) + (confusing-operator-combination? (reverse opt-group-combination)) - :else(underclassify-confusing-operator-combination? opt-group-combination)))) + :else(confusing-operator-combination? opt-group-combination)))) ; ;========GENERAL TESTING============== @@ -64,6 +64,11 @@ atom-without-paren? ) +(->> "a | b & c" parse-expr + ;(get-in-tree [0]) + operator-precedence-atom? + ) + (def codebase_name "wcdb") ;