Skip to content

Commit 60a5512

Browse files
authored
Merge pull request #11813 from github/rc/3.8
Merge docs updates from rc/3.8 into main
2 parents b96160f + 9ef0056 commit 60a5512

File tree

3 files changed

+79
-32
lines changed

3 files changed

+79
-32
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.. pull-quote::
2+
3+
Note
4+
5+
You can use the CodeQL template (beta) in `GitHub Codespaces <https://github.com/codespaces/new?template_repository=github/codespaces-codeql>`__ to try out the QL concepts and programming-language-agnostic examples in these tutorials. The template includes a guided introduction to working with QL, and makes it easy to get started.
6+
7+
When you're ready to run CodeQL queries on actual codebases, you will need to install the CodeQL extension in Visual Studio Code. For instructions, see ":ref:`Setting up CodeQL in Visual Studio Code <setting-up-codeql-in-visual-studio-code>`."
8+

docs/codeql/writing-codeql-queries/find-the-thief.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ You start asking some creative questions and making notes of the answers so you
5050

5151
There is too much information to search through by hand, so you decide to use your newly acquired QL skills to help you with your investigation...
5252

53-
.. include:: ../reusables/setup-to-run-tutorials.rst
53+
.. include:: ../reusables/codespaces-template-note.rst
5454

5555
QL libraries
5656
------------

docs/codeql/writing-codeql-queries/introduction-to-ql.rst

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ QL is a logic programming language, so it is built up of logical formulas. QL us
1414

1515
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``.
1616

17+
.. include:: ../reusables/codespaces-template-note.rst
18+
1719
Running a query
1820
---------------
1921

20-
You can try out the following examples and exercises using :ref:`CodeQL for VS Code <codeql-for-visual-studio-code>`, 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).
22+
You can try out the following examples and exercises using :ref:`CodeQL for VS Code <codeql-for-visual-studio-code>` or the `CodeQL template <https://github.com/codespaces/new?template_repository=github/codespaces-codeql>`__ on GitHub Codespaces.
2123

22-
Once you have selected a language, the query console is populated with the query:
24+
Here is an example of a basic query:
2325

2426
.. code-block:: ql
2527
26-
import <language>
27-
2828
select "hello world"
2929
3030
This query returns the string ``"hello world"``.
@@ -52,39 +52,33 @@ Simple exercises
5252

5353
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.
5454

55-
Exercise 1
56-
~~~~~~~~~~
55+
Exercise 1 - Strings
56+
~~~~~~~~~~~~~~~~~~~~
5757

5858
Write a query which returns the length of the string ``"lgtm"``. (Hint: `here <https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#built-ins-for-string>`__ is the list of the functions that can be applied to strings.)
5959

60-
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2103060623/>`__
60+
➤ `Check your answer <#exercise-1>`__
6161

62-
There is often more than one way to define a query. For example, we can also write the above query in the shorter form:
63-
64-
.. code-block:: ql
65-
66-
select "lgtm".length()
67-
68-
Exercise 2
69-
~~~~~~~~~~
62+
Exercise 2 - Numbers
63+
~~~~~~~~~~~~~~~~~~~~
7064

7165
Write a query which returns the sine of the minimum of ``3^5`` (``3`` raised to the power ``5``) and ``245.6``.
7266

73-
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2093780343/>`__
67+
➤ `Check your answer <#exercise-2>`__
7468

75-
Exercise 3
76-
~~~~~~~~~~
69+
Exercise 3 - Booleans
70+
~~~~~~~~~~~~~~~~~~~~~
7771

7872
Write a query which returns the opposite of the boolean ``false``.
7973

80-
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2093780344/>`__
74+
➤ `Check your answer <#exercise-3>`__
8175

82-
Exercise 4
83-
~~~~~~~~~~
76+
Exercise 4 - Dates
77+
~~~~~~~~~~~~~~~~~~
8478

8579
Write a query which computes the number of days between June 10 and September 28, 2017.
8680

87-
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2100260596/>`__
81+
➤ `Check your answer <#exercise-4>`__
8882

8983
Example query with multiple results
9084
-----------------------------------
@@ -98,8 +92,6 @@ The exercises above all show queries with exactly one result, but in fact many q
9892
x*x + y*y = z*z
9993
select x, y, z
10094
101-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2100790036/>`__
102-
10395
To simplify the query, we can introduce a class ``SmallInt`` representing the integers between 1 and 10. We can also define a predicate ``square()`` on integers in that class. Defining classes and predicates in this way makes it easy to reuse code without having to repeat it every time.
10496

10597
.. code-block:: ql
@@ -113,17 +105,17 @@ To simplify the query, we can introduce a class ``SmallInt`` representing the in
113105
where x.square() + y.square() = z.square()
114106
select x, y, z
115107
116-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101340747/>`__
117-
118108
Example CodeQL queries
119109
----------------------
120110

121111
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.
122-
The following example queries *do* use these databases and give you an idea of how to use CodeQL to analyze projects.
112+
The following example queries *do* use these databases and give you an idea of how to use CodeQL to analyze projects.
123113

124114
Queries using the CodeQL libraries can find errors and uncover variants of important security vulnerabilities in codebases.
125115
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
126116

117+
Before you can run the following examples, you will need to install the CodeQL extension for Visual Studio Code. For more information, see :ref:`Setting up CodeQL in Visual Studio Code <setting-up-codeql-in-visual-studio-code>`. You will also need to import and select a database in the corresponding programming language. For more information about obtaining CodeQL databases, see `Analyzing your projects <https://codeql.github.com/docs/codeql-for-visual-studio-code/analyzing-your-projects/#choosing-a-database>`__ in the CodeQL for VS Code documentation.
118+
127119
To import the CodeQL library for a specific programming language, type ``import <language>`` at the start of the query.
128120

129121
.. code-block:: ql
@@ -134,7 +126,7 @@ To import the CodeQL library for a specific programming language, type ``import
134126
where count(f.getAnArg()) > 7
135127
select f
136128
137-
➤ `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.
129+
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.
138130

139131
.. code-block:: ql
140132
@@ -144,7 +136,7 @@ To import the CodeQL library for a specific programming language, type ``import
144136
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
145137
select c
146138
147-
➤ `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.
139+
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.
148140

149141
.. code-block:: ql
150142
@@ -154,9 +146,56 @@ To import the CodeQL library for a specific programming language, type ``import
154146
where not exists(p.getAnAccess())
155147
select p
156148
157-
➤ `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.
149+
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.
158150

159151
Further reading
160152
---------------
161153

162-
- For a more technical description of the underlying language, see the ":ref:`QL language reference <ql-language-reference>`."
154+
- For a more technical description of the underlying language, see the ":ref:`QL language reference <ql-language-reference>`."
155+
156+
--------------
157+
158+
Answers
159+
-------
160+
161+
Exercise 1
162+
~~~~~~~~~~
163+
164+
.. code-block:: ql
165+
166+
from string s
167+
where s = "lgtm"
168+
select s.length()
169+
170+
There is often more than one way to define a query. For example, we can also write the above query in the shorter form:
171+
172+
.. code-block:: ql
173+
174+
select "lgtm".length()
175+
176+
Exercise 2
177+
~~~~~~~~~~
178+
179+
.. code-block:: ql
180+
181+
from float x, float y
182+
where x = 3.pow(5) and y = 245.6
183+
select x.minimum(y).sin()
184+
185+
Exercise 3
186+
~~~~~~~~~~
187+
188+
.. code-block:: ql
189+
190+
from boolean b
191+
where b = false
192+
select b.booleanNot()
193+
194+
Exercise 4
195+
~~~~~~~~~~
196+
197+
.. code-block:: ql
198+
199+
from date start, date end
200+
where start = "10/06/2017".toDate() and end = "28/09/2017".toDate()
201+
select start.daysTo(end)

0 commit comments

Comments
 (0)