You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/language/learn-ql/beginner/find-the-thief.rst
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -289,8 +289,8 @@ Have you found the thief?
289
289
290
290
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/1505744186085/>`__
291
291
292
-
What next?
293
-
----------
292
+
Further reading
293
+
---------------
294
294
295
295
- Help the villagers track down another criminal in the :doc:`next tutorial <catch-the-fire-starter>`.
296
296
- Find out more about the concepts you discovered in this tutorial in the `QL language reference <https://help.semmle.com/QL/ql-handbook/index.html>`__.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/beginner/ql-tutorials.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,17 +3,17 @@ QL tutorials
3
3
4
4
Solve puzzles to learn the basics of QL before you analyze code with CodeQL. The tutorials teach you how to write queries and introduce you to key logic concepts along the way.
5
5
6
-
Before starting these tutorials, you can read the :doc:`Introduction to QL <../introduction-to-ql>` for a description of the language and some simple examples.
7
-
8
6
.. toctree::
9
7
:hidden:
10
8
9
+
../introduction-to-ql
11
10
find-the-thief
12
11
catch-the-fire-starter
13
12
crown-the-rightful-heir
14
13
cross-the-river
15
14
16
-
- :doc:`Find the thief <find-the-thief>`:Take on the role of a detective to find the thief in this fictional village. You will learn how to use logical connectives, quantifiers, and aggregates in QL along the way.
15
+
- :doc:`Introduction to QL <../introduction-to-ql>`: Work through some simple exercises and examples to learn about the basics of QL and CodeQL.
16
+
- :doc:`Find the thief <find-the-thief>`: Take on the role of a detective to find the thief in this fictional village. You will learn how to use logical connectives, quantifiers, and aggregates in QL along the way.
17
17
- :doc:`Catch the fire starter <catch-the-fire-starter>`: Learn about QL predicates and classes to solve your second mystery as a QL detective.
18
18
- :doc:`Crown the rightful heir <crown-the-rightful-heir>`: This is a QL detective puzzle that shows you how to use recursion in QL to write more complex queries.
19
19
- :doc:`Cross the river <cross-the-river>`: Use common QL features to write a query that finds a solution to the "River crossing" logic puzzle.
Copy file name to clipboardExpand all lines: docs/language/learn-ql/introduction-to-ql.rst
+28-34Lines changed: 28 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,21 @@
1
1
Introduction to QL
2
2
==================
3
3
4
-
QL is the powerful query language that underlies CodeQL, which is used to analyze code.
5
-
Queries written with CodeQL can find errors and uncover variants of important security vulnerabilities.
6
-
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
4
+
Work through some simple exercises and examples to learn about the basics of QL and CodeQL.
5
+
6
+
Basic syntax
7
+
------------
7
8
8
-
Before diving into code analysis with CodeQL, it can be helpful to learn about the underlying language more generally.
9
+
The basic syntax of QL will look familiar to anyone who has used SQL, but it is used somewhat differently.
9
10
10
11
QL is a logic programming language, so it is built up of logical formulas. QL uses common logical connectives (such as ``and``, ``or``, and ``not``), quantifiers (such as ``forall`` and ``exists``), and other important logical concepts such as predicates.
11
12
12
13
QL also supports recursion and aggregates. This allows you to write complex recursive queries using simple QL syntax and directly use aggregates such as ``count``, ``sum``, and ``average``.
13
14
14
-
Basic syntax
15
-
------------
16
-
17
-
The basic syntax of QL will look familiar to anyone who has used SQL, but it is used somewhat differently.
15
+
Running a query
16
+
---------------
18
17
19
-
A query is defined by a **select** clause, which specifies what the result of the query should be. You can try out the examples and exercises in this topic directly in LGTM. Open the `query console on LGTM.com <https://lgtm.com/query>`__. Before you can run a query, you need to select a language and project to query (for these logic examples, any language and project will do).
18
+
You can try out the following examples and exercises using `CodeQL for VS Code <https://help.semmle.com/codeql/codeql-for-vscode.html>`__, or you can run them in the `query console on LGTM.com <https://lgtm.com/query>`__. Before you can run a query on LGTM.com, you need to select a language and project to query (for these logic examples, any language and project will do).
20
19
21
20
Once you have selected a language, the query console is populated with the query:
22
21
@@ -26,7 +25,7 @@ Once you have selected a language, the query console is populated with the query
26
25
27
26
select "hello world"
28
27
29
-
This query simply returns the string ``"hello world"``.
28
+
This query returns the string ``"hello world"``.
30
29
31
30
More complicated queries typically look like this:
32
31
@@ -49,14 +48,14 @@ Note that ``int`` specifies that the **type** of ``x`` and ``y`` is 'integer'. T
49
48
Simple exercises
50
49
----------------
51
50
52
-
You can try to write simple queries using the some of the basic functions that are available for the ``integer``, ``date``, ``float``, ``boolean`` and ``string`` types. To apply a function, simply append it to the argument. For example, ``1.toString()`` converts the value ``1`` to a string. Notice that as you start typing a function, a pop-up is displayed making it easy to select the function that you want. Also note that you can apply multiple functions in succession. For example, ``100.log().sqrt()`` first takes the natural logarithm of 100 and then computes the square root of the result.
51
+
You can write simple queries using the some of the basic functions that are available for the ``int``, ``date``, ``float``, ``boolean`` and ``string`` types. To apply a function, append it to the argument. For example, ``1.toString()`` converts the value ``1`` to a string. Notice that as you start typing a function, a pop-up is displayed making it easy to select the function that you want. Also note that you can apply multiple functions in succession. For example, ``100.log().sqrt()`` first takes the natural logarithm of 100 and then computes the square root of the result.
53
52
54
53
Exercise 1
55
54
~~~~~~~~~~
56
55
57
56
Write a query which returns the length of the string ``"lgtm"``. (Hint: `here <https://help.semmle.com/QL/ql-spec/language.html#built-ins-for-string>`__ is the list of the functions that can be applied to strings.)
58
57
59
-
➤ `Answer<https://lgtm.com/query/2103060623/>`__
58
+
➤ `See answer in the query console on LGTM.com<https://lgtm.com/query/2103060623/>`__
60
59
61
60
There is often more than one way to define a query. For example, we can also write the above query in the shorter form:
62
61
@@ -69,24 +68,24 @@ Exercise 2
69
68
70
69
Write a query which returns the sine of the minimum of ``3^5`` (``3`` raised to the power ``5``) and ``245.6``.
71
70
72
-
➤ `Answer<https://lgtm.com/query/2093780343/>`__
71
+
➤ `See answer in the query console on LGTM.com<https://lgtm.com/query/2093780343/>`__
73
72
74
73
Exercise 3
75
74
~~~~~~~~~~
76
75
77
76
Write a query which returns the opposite of the boolean ``false``.
78
77
79
-
➤ `Answer<https://lgtm.com/query/2093780344/>`__
78
+
➤ `See answer in the query console on LGTM.com<https://lgtm.com/query/2093780344/>`__
80
79
81
80
Exercise 4
82
81
~~~~~~~~~~
83
82
84
83
Write a query which computes the number of days between June 10 and September 28, 2017.
85
84
86
-
➤ `Answer<https://lgtm.com/query/2100260596/>`__
85
+
➤ `See answer in the query console on LGTM.com<https://lgtm.com/query/2100260596/>`__
87
86
88
-
Example queries
89
-
---------------
87
+
Example query with multiple results
88
+
-----------------------------------
90
89
91
90
The exercises above all show queries with exactly one result, but in fact many queries have multiple results. For example, the following query computes all `Pythagorean triples <https://en.wikipedia.org/wiki/Pythagorean_triple>`__ between 1 and 10:
92
91
@@ -114,15 +113,16 @@ To simplify the query, we can introduce a class ``SmallInt`` representing the in
114
113
115
114
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101340747/>`__
116
115
117
-
Now that you've seen some general examples, let's use the CodeQL libraries to analyze projects.
118
-
In particular, LGTM generates a database representing the code and then CodeQL is used to query this database. For more information, see `Database generation <https://lgtm.com/help/lgtm/generate-database>`__ on LGTM.com.
116
+
Example CodeQL queries
117
+
----------------------
119
118
120
-
.. XX: Perhaps a link to the "CodeQL libraries for X"?
119
+
The previous examples used the primitive types built in to QL. Although we chose a project to query, we didn't use the information in that project's database.
120
+
The following example queries *do* use these databases and give you an idea of how to use CodeQL to analyze projects.
121
121
122
-
The previous exercises just used the primitive types built in to QL. Although we chose a project to query, they did not use the project-specific database. The following example queries *do* use these databases and give you an idea of what CodeQL can be used for. There are more details about how to use CodeQL `below <#learning-ql>`__, so don't worry if you don't fully understand these examples yet!
122
+
Queries using the CodeQL libraries can find errors and uncover variants of important security vulnerabilities in codebases.
123
+
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
123
124
124
-
Python
125
-
~~~~~~
125
+
To import the CodeQL library for a specific programming language, type ``import <language>`` at the start of the query.
126
126
127
127
.. code-block:: ql
128
128
@@ -132,10 +132,7 @@ Python
132
132
where count(f.getAnArg()) > 7
133
133
select f
134
134
135
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2096810474/>`__. The ``from`` clause defines a variable ``f`` representing a function. The ``where`` part limits the functions ``f`` to those with more than 7 arguments. Finally, the ``select`` clause lists these functions.
136
-
137
-
JavaScript
138
-
~~~~~~~~~~
135
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2096810474/>`__. The ``from`` clause defines a variable ``f`` representing a Python function. The ``where`` part limits the functions ``f`` to those with more than 7 arguments. Finally, the ``select`` clause lists these functions.
139
136
140
137
.. code-block:: ql
141
138
@@ -145,10 +142,7 @@ JavaScript
145
142
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
146
143
select c
147
144
148
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101530483/>`__. The ``from`` clause defines a variable ``c`` representing a comment. The ``where`` part limits the comments ``c`` to those containing the word ``"TODO"``. The ``select`` clause lists these comments.
149
-
150
-
Java
151
-
~~~~
145
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101530483/>`__. The ``from`` clause defines a variable ``c`` representing a JavaScript comment. The ``where`` part limits the comments ``c`` to those containing the word ``"TODO"``. The ``select`` clause lists these comments.
152
146
153
147
.. code-block:: ql
154
148
@@ -158,11 +152,11 @@ Java
158
152
where not exists(p.getAnAccess())
159
153
select p
160
154
161
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2098670762/>`__. The ``from`` clause defines a variable ``p`` representing a parameter. The ``where`` clause finds unused parameters by limiting the parameters ``p`` to those which are not accessed. Finally, the ``select`` clause lists these parameters.
155
+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2098670762/>`__. The ``from`` clause defines a variable ``p`` representing a Java parameter. The ``where`` clause finds unused parameters by limiting the parameters ``p`` to those which are not accessed. Finally, the ``select`` clause lists these parameters.
162
156
163
-
Learning CodeQL
157
+
Further reading
164
158
---------------
165
159
166
160
- To find out more about how to write your own queries, try working through the :doc:`QL tutorials <beginner/ql-tutorials>`.
167
161
- For an overview of the other available resources, see :doc:`Learning CodeQL <../index>`.
168
-
- For a more technical description of the underlying language, see the `QL language reference <https://help.semmle.com/QL/ql-handbook>`__
162
+
- For a more technical description of the underlying language, see the `QL language reference <https://help.semmle.com/QL/ql-handbook>`__.
0 commit comments