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/codeql/codeql-language-guides/basic-query-for-cpp-code.rst
+38-18Lines changed: 38 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,49 +19,45 @@ The query we're going to run performs a basic search of the code for ``if`` stat
19
19
Finding a CodeQL database to experiment with
20
20
--------------------------------------------
21
21
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.
23
23
24
24
#. In Visual Studio Code, click the **QL** icon |codeql-ext-icon| in the left sidebar to display the CodeQL extension.
25
25
26
26
#. Click **From GitHub** or the GitHub logo |github-db| at the top of the CodeQL extension to open an entry field.
27
27
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.
29
29
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.
31
31
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.
#. 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.
44
42
45
-
.. code-block:: yaml
43
+
#. From the command palette in Visual Studio Code, select **CodeQL: Quick Query**.
46
44
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).
51
46
52
-
#. Create a second new file in your experiements folder with the ``.ql`` file extension. You will write your first query in this file.
#. 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``.
55
51
56
52
.. code-block:: ql
57
53
58
-
import cpp
59
-
60
54
from IfStmt ifstmt, BlockStmt block
61
55
where ifstmt.getThen() = block and
62
56
block.getNumStmt() = 0
63
57
select ifstmt, "This 'if' statement is redundant."
64
58
59
+
#. Save the query in its default location (a temporary "Quick Queries" directory under the workspace for ``GitHub.vscode-codeql/quick-queries``).
60
+
65
61
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
66
62
67
63
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
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
+
79
81
About the query structure
80
82
~~~~~~~~~~~~~~~~~~~~~~~~~
81
83
@@ -143,6 +145,24 @@ To exclude ``if`` statements that have an ``else`` branch:
143
145
144
146
There are now fewer results because ``if`` statements with an ``else`` branch are no longer reported.
145
147
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.
0 commit comments