Skip to content

Commit 97db2c9

Browse files
committed
First draft update basic query to use VS Code
(cherry picked from commit 3e0702f)
1 parent 9780990 commit 97db2c9

File tree

7 files changed

+51
-39
lines changed

7 files changed

+51
-39
lines changed

docs/codeql/codeql-language-guides/basic-query-for-cpp-code.rst

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Basic query for C and C++ code
44
==============================
55

6-
Learn to write and run a simple CodeQL query using LGTM.
6+
Learn to write and run a simple CodeQL query using Visual Studio Code with the CodeQL extension.
7+
8+
.. include:: ../reusables/setup-to-run-queries.rst
79

810
About the query
911
---------------
@@ -14,62 +16,65 @@ The query we're going to run performs a basic search of the code for ``if`` stat
1416
1517
if (error) { }
1618
17-
Running the query
18-
-----------------
19-
20-
#. In the main search box on LGTM.com, search for the project you want to query. For tips, see `Searching <https://lgtm.com/help/lgtm/searching>`__.
19+
Finding a CodeQL database to experiment with
20+
--------------------------------------------
2121

22-
#. Click the project in the search results.
22+
Before you start writing queries, you need a CodeQL database to run them against. The simplest way to do this is to download a database directly from GitHub.com.
2323

24-
#. Click **Query this project**.
24+
#. In Visual Studio Code, click the **QL** icon |codeql-ext-icon| in the left sidebar to display the CodeQL extension.
2525

26-
This opens the query console. (For information about using this, see `Using the query console <https://lgtm.com/help/lgtm/using-query-console>`__.)
26+
#. Click **From GitHub** or the GitHub logo |github-db| at the top of the CodeQL extension to open an entry field.
2727

28-
.. pull-quote::
28+
#. Copy the URL for the repository into the field and press the keyboard **Enter** key.
2929

30-
Note
30+
#. Optionally, if the repository has more than one CodeQL database available, choose which language to download.
3131

32-
Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **C/C++** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list.
33-
34-
#. Copy the following query into the text box in the query console:
35-
36-
.. code-block:: ql
32+
Information about the progress of the database download is shown in the bottom right corner of Visual Studio Code. When the download is complete, the database is shown with a check mark in the **Databases** section of the CodeQL extension.
3733

38-
import cpp
34+
.. image:: ../images/codeql-for-visual-studio-code/database-selected.png
35+
:align: center
36+
:width: 500
3937

40-
from IfStmt ifstmt, BlockStmt block
41-
where ifstmt.getThen() = block and
42-
block.getNumStmt() = 0
43-
select ifstmt, "This 'if' statement is redundant."
38+
Running the query
39+
-----------------
4440

45-
LGTM checks whether your query compiles and, if all is well, the **Run** button changes to green to indicate that you can go ahead and run the query.
41+
#. In Visual Studio Code, create a new folder to store your experimental queries for C and C++ CodeQL databases. For example, ``cpp-experiments``.
4642

47-
#. Click **Run**.
43+
#. Create a ``qlpack.yml`` file in your experiments folder with the contents shown below. This tells the CodeQL extension that any queries you create in the folder are intended to run on C or C++ CodeQL databases.
4844

49-
The name of the project you are querying, and the ID of the most recently analyzed commit to the project, are listed below the query box. To the right of this is an icon that indicates the progress of the query operation:
45+
.. code-block:: yaml
5046
51-
.. image:: ../images/query-progress.png
52-
:align: center
47+
name: github-owner/cpp-experiments
48+
version: 0.0.1
49+
dependencies:
50+
codeql/cpp-all: ^0.1.2
5351
54-
.. pull-quote::
52+
#. Create a second new file in your experiements folder with the ``.ql`` file extension. You will write your first query in this file.
5553

56-
Note
54+
#. Copy the following query into the new file and save the file:
5755

58-
Your query is always run against the most recently analyzed commit to the selected project.
56+
.. code-block:: ql
5957
60-
The query will take a few moments to return results. When the query completes, the results are displayed below the project name. The query results are listed in two columns, corresponding to the two expressions in the ``select`` clause of the query. The first column corresponds to the expression ``ifstmt`` and is linked to the location in the source code of the project where ``ifstmt`` occurs. The second column is the alert message.
58+
import cpp
6159
62-
➤ `Example query results <https://lgtm.com/query/4242591143131494898/>`__
60+
from IfStmt ifstmt, BlockStmt block
61+
where ifstmt.getThen() = block and
62+
block.getNumStmt() = 0
63+
select ifstmt, "This 'if' statement is redundant."
6364
64-
.. pull-quote::
65+
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
6566

66-
Note
67+
The query will take a few moments to return results. When the query completes, the results are displayed in a CodeQL Query Results window, alongside the query window.
68+
69+
The query results are listed in two columns, corresponding to the two expressions in the ``select`` clause of the query. The first column corresponds to the expression ``ifstmt`` and is linked to the location in the source code of the project where ``ifstmt`` occurs. The second column is the alert message.
6770

68-
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
71+
.. image:: ../images/codeql-for-visual-studio-code/basic-cpp-query-results-1.png
72+
:align: center
6973

70-
#. If any matching code is found, click a link in the ``ifstmt`` column to view the ``if`` statement in the code viewer.
74+
If any matching code is found, click a link in the ``ifstmt`` column to open the file and highlight the matching ``if`` statement.
7175

72-
The matching ``if`` statement is highlighted with a yellow background in the code viewer. If any code in the file also matches a query from the standard query library for that language, you will see a red alert message at the appropriate point within the code.
76+
.. image:: ../images/codeql-for-visual-studio-code/basic-cpp-query-results-2.png
77+
:align: center
7378

7479
About the query structure
7580
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -120,7 +125,7 @@ In this case, identifying the ``if`` statement with the empty ``then`` branch as
120125

121126
To exclude ``if`` statements that have an ``else`` branch:
122127

123-
#. Extend the ``where`` clause to include the following extra condition:
128+
#. Edit your query and extend the ``where`` clause to include the following extra condition:
124129

125130
.. code-block:: ql
126131
@@ -134,14 +139,20 @@ To exclude ``if`` statements that have an ``else`` branch:
134139
block.getNumStmt() = 0 and
135140
not ifstmt.hasElse()
136141
137-
#. Click **Run**.
142+
#. Re-run the query.
138143

139144
There are now fewer results because ``if`` statements with an ``else`` branch are no longer reported.
140145

141-
➤ `See this in the query console <https://lgtm.com/query/1899933116489579248/>`__
142-
143146
Further reading
144147
---------------
145148

146149
.. include:: ../reusables/cpp-further-reading.rst
147150
.. include:: ../reusables/codeql-ref-tools-further-reading.rst
151+
152+
.. |codeql-ext-icon| image:: ../images/codeql-for-visual-studio-code/codeql-extension-icon.png
153+
:width: 20
154+
:alt: Icon for the CodeQL extension.
155+
156+
.. |github-db| image:: ../images/codeql-for-visual-studio-code/add-codeql-db-github.png
157+
:width: 20
158+
:alt: Icon for the CodeQL extension option to download a CodeQL database from GitHub.
3.92 KB
Loading
304 KB
Loading
538 KB
Loading
3.97 KB
Loading
38.3 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
For information about installing the CodeQL extension for Visual Studio code, see ":ref:`Setting up CodeQL in Visual Studio Code <setting-up-codeql-in-visual-studio-code>`."

0 commit comments

Comments
 (0)