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
@@ -14,62 +16,65 @@ The query we're going to run performs a basic search of the code for ``if`` stat
14
16
15
17
if (error) { }
16
18
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
+
--------------------------------------------
21
21
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.
23
23
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.
25
25
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.
27
27
28
-
.. pull-quote::
28
+
#. Copy the URL for the repository into the field and press the keyboard **Enter** key.
29
29
30
-
Note
30
+
#. Optionally, if the repository has more than one CodeQL database available, choose which language to download.
31
31
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.
select ifstmt, "This 'if' statement is redundant."
38
+
Running the query
39
+
-----------------
44
40
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``.
46
42
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.
48
44
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
50
46
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
53
51
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.
55
53
56
-
Note
54
+
#. Copy the following query into the new file and save the file:
57
55
58
-
Your query is always run against the most recently analyzed commit to the selected project.
56
+
.. code-block:: ql
59
57
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.
select ifstmt, "This 'if' statement is redundant."
63
64
64
-
.. pull-quote::
65
+
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
65
66
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.
67
70
68
-
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
#. 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.
71
75
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.
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