diff --git a/README.org b/README.org index 4f5f40f..143331d 100644 --- a/README.org +++ b/README.org @@ -30,7 +30,7 @@ #+END_SRC - #+CAPTION: Graph from dot + #+CAPTION: AST graph from dot #+NAME: fig:graph-ast-1 [[./src/printast.dot/null.svg]] @@ -84,3 +84,51 @@ | semmle.graphKind | tree | dot source: none + +* Simple direct use of graph API + For illustration, the query [[./queries/graphout.ql]] uses the =@kind graph= output + for a trivial graph defined in the =edges()= predicate. + + The ouput: + #+CAPTION: Trivial graph rendered by dot + #+ATTR_HTML: :width 10% :height 180px + #+NAME: fig:graph-trivial-1 + [[./printast.dot/null.svg]] + + #+BEGIN_SRC sh + # + export PATH=$HOME/local/vmsync/codeql250:"$PATH" + + # Create the db + cd ~/w/codeql-javascript/src/ + rm -fR callbacks.db + codeql database create -j8 -v --language=javascript -s . callbacks.db + + # Run the query to create dot file (and bqrs as side effect) + cd ~/w/codeql-javascript/ + codeql database analyze \ + ~/w/codeql-javascript/src/callbacks.db/ \ + ~/w/codeql-javascript/queries/graphout.ql \ + -j8 -v --ram=16000 \ + --format=dot --rerun \ + --output=printast.dot + + # Create SVG version of graph + cd ~/w/codeql-javascript/ + dot -Tsvg < ./printast.dot/null.dot > ./printast.dot/null.svg + open -a safari printast.dot/null.svg + + # List query result meta info + BQRS=src/callbacks.db/results/exploratory-queries-javascript/graphout.bqrs + codeql bqrs info --format=text -- $BQRS + + # Format results using bqrs decode. + codeql bqrs decode --output=printast.csv --result-set=edges \ + --format=csv --entities=all -- $BQRS + codeql bqrs decode --output=printast.json --format=json --entities=all -- $BQRS + + # Result files + ls -1l ./src/callbacks.db/results/exploratory-queries-javascript/graphout.bqrs \ + ./printast.dot/null.dot \ + printast.csv printast.json + #+END_SRC diff --git a/printast.csv b/printast.csv new file mode 100644 index 0000000..b15a751 --- /dev/null +++ b/printast.csv @@ -0,0 +1,3 @@ +"a","b" +1,2 +2,3 diff --git a/printast.dot/null.dot b/printast.dot/null.dot new file mode 100644 index 0000000..eb86976 --- /dev/null +++ b/printast.dot/null.dot @@ -0,0 +1,8 @@ +digraph { + compound=true; + 0[label="1"; ]; + 1[label="2"; ]; + 2[label="3"; ]; + 0 -> 1[]; + 1 -> 2[]; +} diff --git a/printast.dot/null.svg b/printast.dot/null.svg new file mode 100644 index 0000000..190698e --- /dev/null +++ b/printast.dot/null.svg @@ -0,0 +1,42 @@ + + + + + + + + + +0 + +1 + + + +1 + +2 + + + +0->1 + + + + + +2 + +3 + + + +1->2 + + + + + diff --git a/printast.json b/printast.json new file mode 100644 index 0000000..fa7e242 --- /dev/null +++ b/printast.json @@ -0,0 +1,20 @@ +{"edges":{"columns":[ + {"name":"a","kind":"Integer"} + ,{"name":"b","kind":"Integer"}] + ,"tuples":[ + [1,2] + ,[2,3]] + },"nodes":{"columns":[ + {"name":"n","kind":"Integer"} + ,{"name":"key","kind":"String"} + ,{"name":"val","kind":"String"}] + ,"tuples":[ + [1,"semmle.label","1"] + ,[2,"semmle.label","2"] + ,[3,"semmle.label","3"]] + },"graphProperties":{"columns":[ + {"name":"key","kind":"String"} + ,{"name":"value","kind":"String"}] + ,"tuples":[ + ["semmle.graphKind","tree"]] + }} diff --git a/queries/graphout.ql b/queries/graphout.ql new file mode 100644 index 0000000..1b4b75d --- /dev/null +++ b/queries/graphout.ql @@ -0,0 +1,22 @@ +/** + * @name simple graph test + * @kind graph + */ + +import javascript + +query predicate edges(int a, int b) { + a = 1 and b = 2 + or + a = 2 and b = 3 +} + +query predicate nodes(int n, string key, string val) { + (edges(n, _) or edges(_, n)) and + key = "semmle.label" and + val = n.toString() +} + +query predicate graphProperties(string key, string value) { + key = "semmle.graphKind" and value = "tree" +} diff --git a/queries/qlpack.yml b/queries/qlpack.yml index 3d143f9..bc15677 100644 --- a/queries/qlpack.yml +++ b/queries/qlpack.yml @@ -1,3 +1,3 @@ -name: codeql-custom-queries-javascript +name: exploratory-queries-javascript version: 0.0.0 libraryPathDependencies: codeql-javascript