Skip to content

Commit ff8e9e6

Browse files
committed
Fix code block in other CodeQL docs
1 parent f1d8d94 commit ff8e9e6

File tree

5 files changed

+58
-18
lines changed

5 files changed

+58
-18
lines changed

docs/codeql/codeql-language-guides/codeql-library-for-csharp.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ Count the number of lines of code, excluding the directory ``external``:
108108
.. code-block:: ql
109109
110110
select sum(SourceFile f |
111-
not exists(Folder external | external.getShortName() = "external" |
112-
external.getAFolder*().getAFile() = f) |
111+
not exists(Folder ext | ext.getShortName() = "external" |
112+
ext.getAFolder*().getAFile() = f) |
113113
f.getNumberOfLines())
114114
115115
Exercises
@@ -961,7 +961,7 @@ Find all obsolete elements:
961961
962962
Model NUnit test fixtures:
963963

964-
.. code-block:: csharp
964+
.. code-block:: ql
965965
966966
class TestFixture extends Class
967967
{

docs/codeql/writing-codeql-queries/about-codeql-queries.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ For more information on how to format your code when contributing queries to the
2121
Basic query structure
2222
*********************
2323

24-
:ref:`Queries <queries>` written with CodeQL have the file extension ``.ql``, and contain a ``select`` clause. Many of the existing queries include additional optional information, and have the following structure::
24+
:ref:`Queries <queries>` written with CodeQL have the file extension ``.ql``, and contain a ``select`` clause. Many of the existing queries include additional optional information, and have the following structure:
25+
26+
.. code-block:: ql
2527
2628
/**
2729
*

docs/codeql/writing-codeql-queries/creating-path-queries.rst

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ Constructing a path query
4444
Path queries require certain metadata, query predicates, and ``select`` statement structures.
4545
Many of the built-in path queries included in CodeQL follow a simple structure, which depends on how the language you are analyzing is modeled with CodeQL.
4646

47-
For C/C++, C#, Java, and JavaScript you should use the following template::
47+
For C/C++, C#, Java, and JavaScript you should use the following template:
48+
49+
.. code-block:: ql
4850
4951
/**
5052
* ...
@@ -66,7 +68,9 @@ Where:
6668
- ``source`` and ``sink`` are nodes on the `path graph <https://en.wikipedia.org/wiki/Path_graph>`__, and ``DataFlow::PathNode`` is their type.
6769
- ``Configuration`` is a class containing the predicates which define how data may flow between the ``source`` and the ``sink``.
6870

69-
For Python you should use a slightly different template::
71+
For Python you should use a slightly different template:
72+
73+
.. code-block:: ql
7074
7175
/**
7276
* ...
@@ -104,13 +108,17 @@ To do this you need to define a :ref:`query predicate <query-predicates>` called
104108
This predicate defines the edge relations of the graph you are computing, and it is used to compute the paths related to each result that your query generates.
105109
You can import a predefined ``edges`` predicate from a path graph module in one of the standard data flow libraries. In addition to the path graph module, the data flow libraries contain the other ``classes``, ``predicates``, and ``modules`` that are commonly used in data flow analysis. The import statement to use depends on the language that you are analyzing.
106110

107-
For C/C++, C#, Java, and JavaScript you would use::
111+
For C/C++, C#, Java, and JavaScript you would use:
112+
113+
.. code-block:: ql
108114
109115
import DataFlow::PathGraph
110116
111117
This statement imports the ``PathGraph`` module from the data flow library (``DataFlow.qll``), in which ``edges`` is defined.
112118

113-
For Python, the ``Paths`` module contains the ``edges`` predicate::
119+
For Python, the ``Paths`` module contains the ``edges`` predicate:
120+
121+
.. code-block:: ql
114122
115123
import semmle.python.security.Paths
116124
@@ -121,7 +129,9 @@ For all languages, you can also optionally define a ``nodes`` query predicate, w
121129
Defining your own ``edges`` predicate
122130
-------------------------------------
123131

124-
You can also define your own ``edges`` predicate in the body of your query. It should take the following form::
132+
You can also define your own ``edges`` predicate in the body of your query. It should take the following form:
133+
134+
.. code-block:: ql
125135
126136
query predicate edges(PathNode a, PathNode b) {
127137
/** Logical conditions which hold if `(a,b)` is an edge in the data flow graph */
@@ -136,7 +146,9 @@ You must provide information about the ``source`` and ``sink`` in your path quer
136146
The name and the type of the ``source`` and the ``sink`` must be declared in the ``from`` statement of the query, and the types must be compatible with the nodes of the graph computed by the ``edges`` predicate.
137147

138148
If you are querying C/C++, C#, Java, or JavaScript code (and you have used ``import DataFlow::PathGraph`` in your query), the definitions of the ``source`` and ``sink`` are accessed via the ``Configuration`` class in the data flow library. You should declare all three of these objects in the ``from`` statement.
139-
For example::
149+
For example:
150+
151+
.. code-block:: ql
140152
141153
from Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
142154
@@ -149,7 +161,9 @@ For more information on using the configuration class in your analysis see the s
149161

150162
You can also create a configuration for different frameworks and environments by extending the ``Configuration`` class. For more information, see ":ref:`Types <defining-a-class>`" in the QL language reference.
151163

152-
If you are querying Python code (and you have used ``import semmle.python.security.Paths`` in your query) you should declare ``TaintedPathSource source, TaintedPathSink sink`` in your ``from`` statement. You do not need to declare a ``Configuration`` class as the definitions of the ``TaintedPathSource`` and ``TaintedPathSink`` contain all of the type information that is required::
164+
If you are querying Python code (and you have used ``import semmle.python.security.Paths`` in your query) you should declare ``TaintedPathSource source, TaintedPathSink sink`` in your ``from`` statement. You do not need to declare a ``Configuration`` class as the definitions of the ``TaintedPathSource`` and ``TaintedPathSink`` contain all of the type information that is required:
165+
166+
.. code-block:: ql
153167
154168
from TaintedPathSource source, TaintedPathSink sink
155169
@@ -163,11 +177,15 @@ This clause can use :ref:`aggregations <aggregations>`, :ref:`predicates <predic
163177

164178
When writing a path queries, you would typically include a predicate that holds only if data flows from the ``source`` to the ``sink``.
165179

166-
For C/C++, C#, Java or JavaScript, you would use the ``hasFlowPath`` predicate to define flow from the ``source`` to the ``sink`` for a given ``Configuration``::
180+
For C/C++, C#, Java or JavaScript, you would use the ``hasFlowPath`` predicate to define flow from the ``source`` to the ``sink`` for a given ``Configuration``:
181+
182+
.. code-block:: ql
167183
168184
where config.hasFlowPath(source, sink)
169185
170-
For Python, you would simply use the ``flowsTo`` predicate to define flow from the ``source`` to the ``sink``::
186+
For Python, you would simply use the ``flowsTo`` predicate to define flow from the ``source`` to the ``sink``:
187+
188+
.. code-block:: ql
171189
172190
where source.flowsTo(sink)
173191

docs/codeql/writing-codeql-queries/cross-the-river.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ a piece of cargo.
4747
*Show/hide code*
4848

4949
.. literalinclude:: river-crossing.ql
50+
:language: ql
5051
:lines: 15-23
5152

5253
Second, any item can be on one of two shores. Let's call these the "left shore" and the "right shore".
@@ -63,6 +64,7 @@ You can do this by defining a member predicate
6364
*Show/hide code*
6465

6566
.. literalinclude:: river-crossing.ql
67+
:language: ql
6668
:lines: 25-38
6769

6870
We also want a way to keep track of where the man, the goat, the cabbage, and the wolf are at any point. We can call this combined
@@ -80,6 +82,7 @@ temporary variables in the body of a class are called :ref:`fields <fields>`.
8082
*Show/hide code*
8183

8284
.. literalinclude:: river-crossing-1.ql
85+
:language: ql
8386
:lines: 33-40,87
8487

8588
We are interested in two particular states, namely the initial state and the goal state,
@@ -94,6 +97,7 @@ Assuming that all items start on the left shore and end up on the right shore, d
9497
*Show/hide code*
9598

9699
.. literalinclude:: river-crossing-1.ql
100+
:language: ql
97101
:lines: 89-97
98102

99103
.. pull-quote::
@@ -112,6 +116,7 @@ Using the above note, the QL code so far looks like this:
112116
*Show/hide code*
113117

114118
.. literalinclude:: river-crossing.ql
119+
:language: ql
115120
:lines: 15-52,103-113
116121

117122
Model the action of "ferrying"
@@ -130,6 +135,7 @@ after ferrying a particular cargo. (Hint: Use the predicate ``other``.)
130135
*Show/hide code*
131136

132137
.. literalinclude:: river-crossing.ql
138+
:language: ql
133139
:lines: 54-67
134140

135141
Of course, not all ferrying actions are possible. Add some extra conditions to describe when a ferrying
@@ -147,6 +153,7 @@ For example, follow these steps:
147153
*Show/hide code*
148154

149155
.. literalinclude:: river-crossing.ql
156+
:language: ql
150157
:lines: 69-81
151158

152159
Find paths from one state to another
@@ -185,6 +192,7 @@ for example ``steps <= 7``.
185192
*Show/hide code*
186193

187194
.. literalinclude:: river-crossing-1.ql
195+
:language: ql
188196
:lines: 70-86
189197

190198
However, although this ensures that the solution is finite, it can still contain loops if the upper bound
@@ -215,6 +223,7 @@ the given path without revisiting any previously visited states.
215223
*Show/hide code*
216224

217225
.. literalinclude:: river-crossing.ql
226+
:language: ql
218227
:lines: 83-102
219228

220229
Display the results
@@ -230,6 +239,7 @@ that returns the resulting path.
230239
*Show/hide code*
231240

232241
.. literalinclude:: river-crossing.ql
242+
:language: ql
233243
:lines: 115-117
234244

235245
The :ref:`don't-care expression <don-t-care-expressions>` (``_``),

docs/codeql/writing-codeql-queries/troubleshooting-query-performance.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ The performance of a predicate can often be judged by considering roughly how ma
2727
One way of creating badly performing predicates is by using two variables without relating them in any way, or only relating them using a negation.
2828
This leads to computing the `Cartesian product <https://en.wikipedia.org/wiki/Cartesian_product>`__ between the sets of possible values for each variable, potentially generating a huge table of results.
2929
This can occur if you don't specify restrictions on your variables.
30-
For instance, consider the following predicate that checks whether a Java method ``m`` may access a field ``f``::
30+
For instance, consider the following predicate that checks whether a Java method ``m`` may access a field ``f``:
31+
32+
.. code-block:: ql
3133
3234
predicate mayAccess(Method m, Field f) {
3335
f.getAnAccess().getEnclosingCallable() = m
@@ -39,7 +41,9 @@ The predicate holds if ``m`` contains an access to ``f``, but also conservativel
3941

4042
However, if ``m`` is a native method, the table computed by ``mayAccess`` will contain a row ``m, f`` for *all* fields ``f`` in the codebase, making it potentially very large.
4143

42-
This example shows a similar mistake in a member predicate::
44+
This example shows a similar mistake in a member predicate:
45+
46+
.. code-block:: ql
4347
4448
class Foo extends Class {
4549
...
@@ -57,11 +61,15 @@ Use specific types
5761
~~~~~~~~~~~~~~~~~~
5862

5963
":ref:`Types <types>`" provide an upper bound on the size of a relation.
60-
This helps the query optimizer be more effective, so it's generally good to use the most specific types possible. For example::
64+
This helps the query optimizer be more effective, so it's generally good to use the most specific types possible. For example:
65+
66+
.. code-block:: ql
6167
6268
predicate foo(LoggingCall e)
6369
64-
is preferred over::
70+
is preferred over:
71+
72+
.. code-block:: ql
6573
6674
predicate foo(Expr e)
6775
@@ -95,7 +103,9 @@ Avoid complex recursion
95103
":ref:`Recursion <recursion>`" is about self-referencing definitions.
96104
It can be extremely powerful as long as it is used appropriately.
97105
On the whole, you should try to make recursive predicates as simple as possible.
98-
That is, you should define a *base case* that allows the predicate to *bottom out*, along with a single *recursive call*::
106+
That is, you should define a *base case* that allows the predicate to *bottom out*, along with a single *recursive call*:
107+
108+
.. code-block:: ql
99109
100110
int depth(Stmt s) {
101111
exists(Callable c | c.getBody() = s | result = 0) // base case

0 commit comments

Comments
 (0)