Skip to content

Commit 229c3e6

Browse files
committed
Update for James' review comments
(cherry picked from commit 174fbf6)
1 parent 97db2c9 commit 229c3e6

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,45 @@ The query we're going to run performs a basic search of the code for ``if`` stat
1919
Finding a CodeQL database to experiment with
2020
--------------------------------------------
2121

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.
22+
Before you start writing queries for C or C++ code, you need a CodeQL database to run them against. The simplest way to do this is to download a database for a repository that uses C or C++ directly from GitHub.com.
2323

2424
#. In Visual Studio Code, click the **QL** icon |codeql-ext-icon| in the left sidebar to display the CodeQL extension.
2525

2626
#. Click **From GitHub** or the GitHub logo |github-db| at the top of the CodeQL extension to open an entry field.
2727

28-
#. Copy the URL for the repository into the field and press the keyboard **Enter** key.
28+
#. Copy the URL for the repository into the field and press the keyboard **Enter** key. For example, https://github.com/protocolbuffers/protobuf.
2929

30-
#. Optionally, if the repository has more than one CodeQL database available, choose which language to download.
30+
#. Optionally, if the repository has more than one CodeQL database available, select ``cpp`` to download the database created from the C and/or C++ code.
3131

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.
32+
Information about the download progress for the database 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.
3333

3434
.. image:: ../images/codeql-for-visual-studio-code/database-selected.png
3535
:align: center
3636
:width: 500
3737

38-
Running the query
39-
-----------------
38+
Running a quick query
39+
---------------------
4040

41-
#. In Visual Studio Code, create a new folder to store your experimental queries for C and C++ CodeQL databases. For example, ``cpp-experiments``.
42-
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.
41+
The CodeQL extension for Visual Studio Code adds several **CodeQL:** commands to the command palette including **Quick Query**, which you can use to run a query without any set up.
4442

45-
.. code-block:: yaml
43+
#. From the command palette in Visual Studio Code, select **CodeQL: Quick Query**.
4644

47-
name: github-owner/cpp-experiments
48-
version: 0.0.1
49-
dependencies:
50-
codeql/cpp-all: ^0.1.2
45+
#. After a momment, a new tab *quick-query.ql* is opened, ready for you to write a query for your currently selected CodeQL database (here a ``cpp`` database).
5146

52-
#. Create a second new file in your experiements folder with the ``.ql`` file extension. You will write your first query in this file.
47+
.. image:: ../images/codeql-for-visual-studio-code/quick-query-tab.png
48+
:align: center
5349

54-
#. Copy the following query into the new file and save the file:
50+
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import cpp``.
5551

5652
.. code-block:: ql
5753
58-
import cpp
59-
6054
from IfStmt ifstmt, BlockStmt block
6155
where ifstmt.getThen() = block and
6256
block.getNumStmt() = 0
6357
select ifstmt, "This 'if' statement is redundant."
6458
59+
#. Save the query in its default location (a temporary "Quick Queries" directory under the workspace for ``GitHub.vscode-codeql/quick-queries``).
60+
6561
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
6662

6763
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.
@@ -76,6 +72,12 @@ If any matching code is found, click a link in the ``ifstmt`` column to open the
7672
.. image:: ../images/codeql-for-visual-studio-code/basic-cpp-query-results-2.png
7773
:align: center
7874

75+
.. pull-quote::
76+
77+
Note
78+
79+
If you want to move your experimental query somewhere more permanent, you need to move the whole ``Quick Queries`` directory. The directory is a CodeQL pack with a ``qlpack.yml`` file that defines the content as queries for C/C++ CodeQL databases. For more information about CodeQL packs, see ":ref:`Working with CodeQL packs in Visual Studio Code <working-with-codeql-packs-in-visual-studio-code>`."
80+
7981
About the query structure
8082
~~~~~~~~~~~~~~~~~~~~~~~~~
8183

@@ -143,6 +145,24 @@ To exclude ``if`` statements that have an ``else`` branch:
143145

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

148+
Saving your quick query for future development
149+
----------------------------------------------
150+
151+
152+
#. In Visual Studio Code, create a new folder to store your experimental queries for C and C++ CodeQL databases. For example, ``cpp-experiments``.
153+
154+
#. 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.
155+
156+
.. code-block:: yaml
157+
158+
name: github-owner/cpp-experiments
159+
version: 0.0.1
160+
dependencies:
161+
codeql/cpp-all: ^0.1.2
162+
163+
#. Create a second new file in your experiements folder with the ``.ql`` file extension. You will write your first query in this file.
164+
165+
146166
Further reading
147167
---------------
148168

10.5 KB
Loading
-151 KB
Loading
70.2 KB
Loading

0 commit comments

Comments
 (0)