Skip to content

Commit c66569b

Browse files
committed
Generalize cpp changes and apply to csharp
1 parent 1dc6640 commit c66569b

File tree

11 files changed

+89
-91
lines changed

11 files changed

+89
-91
lines changed

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

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Basic query for C and C++ code
55

66
Learn to write and run a simple CodeQL query using Visual Studio Code with the CodeQL extension.
77

8-
.. include:: ../reusables/setup-to-run-queries.rst
8+
.. include:: ../reusables/vs-code-basic-instructions/setup-to-run-queries.rst
99

1010
About the query
1111
---------------
@@ -16,36 +16,12 @@ The query we're going to run performs a basic search of the code for ``if`` stat
1616
1717
if (error) { }
1818
19-
Finding a CodeQL database to experiment with
20-
--------------------------------------------
21-
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-
24-
#. In Visual Studio Code, click the **QL** icon |codeql-ext-icon| in the left sidebar to display the CodeQL extension.
25-
26-
#. Click **From GitHub** or the GitHub logo |github-db| at the top of the CodeQL extension to open an entry field.
27-
28-
#. Copy the URL for the repository into the field and press the keyboard **Enter** key. For example, https://github.com/protocolbuffers/protobuf.
29-
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-
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.
33-
34-
.. image:: ../images/codeql-for-visual-studio-code/database-selected.png
35-
:align: center
36-
:width: 500
19+
.. include:: ../reusables/vs-code-basic-instructions/find-database.rst
3720

3821
Running a quick query
3922
---------------------
4023

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.
42-
43-
#. From the command palette in Visual Studio Code, select **CodeQL: Quick Query**.
44-
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).
46-
47-
.. image:: ../images/codeql-for-visual-studio-code/quick-query-tab.png
48-
:align: center
24+
.. include:: ../reusables/vs-code-basic-instructions/run-quick-query-1.rst
4925

5026
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import cpp``.
5127

@@ -56,27 +32,17 @@ The CodeQL extension for Visual Studio Code adds several **CodeQL:** commands to
5632
block.getNumStmt() = 0
5733
select ifstmt, "This 'if' statement is redundant."
5834
59-
#. Save the query in its default location (a temporary "Quick Queries" directory under the workspace for ``GitHub.vscode-codeql/quick-queries``).
60-
61-
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
62-
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.
64-
65-
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.
35+
.. include:: ../reusables/vs-code-basic-instructions/run-quick-query-2.rst
6636

6737
.. image:: ../images/codeql-for-visual-studio-code/basic-cpp-query-results-1.png
68-
:align: center
38+
:align: center
6939

7040
If any matching code is found, click a link in the ``ifstmt`` column to open the file and highlight the matching ``if`` statement.
7141

7242
.. image:: ../images/codeql-for-visual-studio-code/basic-cpp-query-results-2.png
73-
:align: center
74-
75-
.. pull-quote::
76-
77-
Note
43+
:align: center
7844

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>`."
45+
.. include:: ../reusables/vs-code-basic-instructions/note-store-quick-query.rst
8046

8147
About the query structure
8248
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -151,10 +117,14 @@ Further reading
151117
.. include:: ../reusables/cpp-further-reading.rst
152118
.. include:: ../reusables/codeql-ref-tools-further-reading.rst
153119

154-
.. |codeql-ext-icon| image:: ../images/codeql-for-visual-studio-code/codeql-extension-icon.png
155-
:width: 20
156-
:alt: Icon for the CodeQL extension.
120+
.. Article-specific substitutions for the reusables used in docs/codeql/reusables/vs-code-basic-instructions
121+
122+
.. |language-text| replace:: C/C++
123+
124+
.. |language-code| replace:: ``cpp``
125+
126+
.. |example-url| replace:: https://github.com/protocolbuffers/protobuf
127+
128+
.. |image-quick-query| image:: ../images/codeql-for-visual-studio-code/quick-query-tab-cpp.png
157129

158-
.. |github-db| image:: ../images/codeql-for-visual-studio-code/add-codeql-db-github.png
159-
:width: 20
160-
:alt: Icon for the CodeQL extension option to download a CodeQL database from GitHub.
130+
.. |expression| replace:: ``ifstmt``

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

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
Basic query for 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/vs-code-basic-instructions/setup-to-run-queries.rst
79

810
About the query
911
---------------
@@ -14,62 +16,33 @@ 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>`__.
21-
22-
#. Click the project in the search results.
23-
24-
#. Click **Query this project**.
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>`__.)
27-
28-
.. pull-quote::
19+
.. include:: ../reusables/vs-code-basic-instructions/find-database.rst
2920

30-
Note
21+
Running a quick query
22+
---------------------
3123

32-
Alternatively, you can go straight to the query console by clicking **Query console** (at the top of any page), selecting **C#** from the **Language** drop-down list, then choosing one or more projects to query from those displayed in the **Project** drop-down list.
24+
.. include:: ../reusables/vs-code-basic-instructions/run-quick-query-1.rst
3325

34-
#. Copy the following query into the text box in the query console:
26+
#. In the quick query tab, delete ``select ""`` and paste the following query beneath the import statement ``import csharp``.
3527

3628
.. code-block:: ql
3729
38-
import csharp
39-
4030
from IfStmt ifstmt, BlockStmt block
4131
where ifstmt.getThen() = block and
4232
block.isEmpty()
4333
select ifstmt, "This 'if' statement is redundant."
4434
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.
46-
47-
#. Click **Run**.
48-
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:
50-
51-
.. image:: ../images/query-progress.png
52-
:align: center
53-
54-
.. pull-quote::
35+
.. include:: ../reusables/vs-code-basic-instructions/run-quick-query-2.rst
5536

56-
Note
37+
.. image:: ../images/codeql-for-visual-studio-code/basic-csharp-query-results-1.png
38+
:align: center
5739

58-
Your query is always run against the most recently analyzed commit to the selected project.
40+
If any matching code is found, click a link in the ``ifstmt`` column to open the file and highlight the matching ``if`` statement.
5941

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.
42+
.. image:: ../images/codeql-for-visual-studio-code/basic-csharp-query-results-2.png
43+
:align: center
6144

62-
➤ `Example query results <https://lgtm.com/query/1214010107827821393/>`__
63-
64-
.. pull-quote::
65-
66-
Note
67-
68-
An ellipsis (…) at the bottom of the table indicates that the entire list is not displayed—click it to show more results.
69-
70-
#. If any matching code is found, click a link in the ``ifstmt`` column to view the ``if`` statement in the code viewer.
71-
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.
45+
.. include:: ../reusables/vs-code-basic-instructions/note-store-quick-query.rst
7346

7447
About the query structure
7548
~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -143,10 +116,19 @@ To exclude ``if`` statements that have an ``else`` branch:
143116

144117
There are now fewer results because ``if`` statements with an ``else`` branch are no longer included.
145118

146-
➤ `See this in the query console <https://lgtm.com/query/6233102733683510530/>`__
147-
148119
Further reading
149120
---------------
150121

151122
.. include:: ../reusables/csharp-further-reading.rst
152123
.. include:: ../reusables/codeql-ref-tools-further-reading.rst
124+
125+
.. Article-specific substitutions for the reusables used in docs/codeql/reusables/vs-code-basic-instructions
126+
.. |language-text| replace:: C#
127+
128+
.. |language-code| replace:: ``csharp``
129+
130+
.. |example-url| replace:: https://github.com/PowerShell/PowerShell
131+
132+
.. |image-quick-query| image:: ../images/codeql-for-visual-studio-code/quick-query-tab-csharp.png
133+
134+
.. |expression| replace:: ``ifstmt``
189 KB
Loading
250 KB
Loading
48.4 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Finding a CodeQL database to experiment with
2+
--------------------------------------------
3+
4+
Before you start writing queries for |language-text| 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 |language-text| directly from GitHub.com.
5+
6+
#. In Visual Studio Code, click the **QL** icon |codeql-ext-icon| in the left sidebar to display the CodeQL extension.
7+
8+
#. Click **From GitHub** or the GitHub logo |github-db| at the top of the CodeQL extension to open an entry field.
9+
10+
#. Copy the URL for the repository into the field and press the keyboard **Enter** key. For example, |example-url|.
11+
12+
#. Optionally, if the repository has more than one CodeQL database available, select |language-code| to download the database created from the |language-text| code.
13+
14+
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.
15+
16+
.. image:: ../images/codeql-for-visual-studio-code/database-selected.png
17+
:align: center
18+
:width: 500
19+
20+
.. |codeql-ext-icon| image:: ../images/codeql-for-visual-studio-code/codeql-extension-icon.png
21+
:width: 20
22+
:alt: Icon for the CodeQL extension.
23+
24+
.. |github-db| image:: ../images/codeql-for-visual-studio-code/add-codeql-db-github.png
25+
:width: 20
26+
:alt: Icon for the CodeQL extension option to download a CodeQL database from GitHub.
27+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. pull-quote::
2+
3+
Note
4+
5+
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 |language-text| 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>`."
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
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.
2+
3+
#. From the command palette in Visual Studio Code, select **CodeQL: Quick Query**.
4+
5+
#. 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 |language-code| database).
6+
7+
|image-quick-query|
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
4. Save the query in its default location (a temporary "Quick Queries" directory under the workspace for ``GitHub.vscode-codeql/quick-queries``).
2+
3+
#. Right-click in the query window and select **CodeQL: Run Query**. (Alternatively, run the command from the Command Palette.)
4+
5+
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.
6+
7+
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 |expression| and is linked to the location in the source code of the project where |expression| occurs. The second column is the alert message.

0 commit comments

Comments
 (0)