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/analyzing-control-flow-in-python.rst
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,7 +47,7 @@ Example finding unreachable AST nodes
47
47
where not exists(node.getAFlowNode())
48
48
select node
49
49
50
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/669220024/>`__. The demo projects on LGTM.com all have some code that has no control flow node, and is therefore unreachable. However, since the ``Module`` class is also a subclass of the ``AstNode`` class, the query also finds any modules implemented in C or with no source code. Therefore, it is better to find all unreachable statements.
50
+
Many codebases have some code that has no control flow node, and is therefore unreachable. However, since the ``Module`` class is also a subclass of the ``AstNode`` class, the query also finds any modules implemented in C or with no source code. Therefore, it is better to find all unreachable statements.
51
51
52
52
Example finding unreachable statements
53
53
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -60,7 +60,7 @@ Example finding unreachable statements
60
60
where not exists(s.getAFlowNode())
61
61
select s
62
62
63
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/670720181/>`__. This query gives fewer results, but most of the projects have some unreachable nodes. These are also highlighted by the standard "Unreachable code" query. For more information, see `Unreachable code <https://lgtm.com/rules/3980095>`__ on LGTM.com.
63
+
This query should give fewer results. You can also find unreachable code using the standard "Unreachable code" query. For more information, see `Unreachable code <https://codeql.github.com/codeql-query-help/python/py-unreachable-statement/>`__.
64
64
65
65
The ``BasicBlock`` class
66
66
------------------------
@@ -114,7 +114,7 @@ Example finding mutually exclusive blocks within the same function
114
114
)
115
115
select b1, b2
116
116
117
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/671000028/>`__. This typically gives a very large number of results, because it is a common occurrence in normal control flow. It is, however, an example of the sort of control-flow analysis that is possible. Control-flow analyses such as this are an important aid to data flow analysis. For more information, see ":doc:`Analyzing data flow in Python <analyzing-data-flow-in-python>`."
117
+
This typically gives a very large number of results, because it is a common occurrence in normal control flow. It is, however, an example of the sort of control-flow analysis that is possible. Control-flow analyses such as this are an important aid to data flow analysis. For more information, see ":doc:`Analyzing data flow in Python <analyzing-data-flow-in-python>`."
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8635258505893505141/>`__. Two of the demo projects make use of this low-level API.
101
-
102
100
Notice the use of the ``API`` module for referring to library functions. For more information, see ":doc:`Using API graphs in Python <using-api-graphs-in-python>`."
103
101
104
-
Unfortunately this will only give the expression in the argument, not the values which could be passed to it. So we use local data flow to find all expressions that flow into the argument:
102
+
Unfortunately this query will only give the expression in the argument, not the values which could be passed to it. So we use local data flow to find all expressions that flow into the argument:
105
103
106
104
.. code-block:: ql
107
105
@@ -115,9 +113,7 @@ Unfortunately this will only give the expression in the argument, not the values
115
113
DataFlow::localFlow(expr, call.getArg(0))
116
114
select call, expr
117
115
118
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8213643003890447109/>`__. Many expressions flow to the same call.
119
-
120
-
We see that we get several data-flow nodes for an expression as it flows towards a call (notice repeated locations in the ``call`` column). We are mostly interested in the "first" of these, what might be called the local source for the file name. To restrict attention to such local sources, and to simultaneously make the analysis more performant, we have the QL class ``LocalSourceNode``. We could demand that ``expr`` is such a node:
116
+
Typically, you will see several data-flow nodes for an expression as it flows towards a call (notice repeated locations in the ``call`` column). We are mostly interested in the "first" of these, what might be called the local source for the file name. To restrict attention to such local sources, and to simultaneously make the analysis more performant, we have the QL class ``LocalSourceNode``. We could demand that ``expr`` is such a node:
121
117
122
118
.. code-block:: ql
123
119
@@ -160,9 +156,9 @@ As an alternative, we can ask more directly that ``expr`` is a local source of t
160
156
expr = call.getArg(0).getALocalSource()
161
157
select call, expr
162
158
163
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6602079735954016687/>`__. All these three queries give identical results. We now mostly have one expression per call.
159
+
These three queries all give identical results. We now mostly have one expression per call.
164
160
165
-
We still have some cases of more than one expression flowing to a call, but then they flow through different code paths (possibly due to control-flow splitting, as in the second case).
161
+
We still have some cases of more than one expression flowing to a call, but then they flow through different code paths (possibly due to control-flow splitting).
166
162
167
163
We might want to make the source more specific, for example a parameter to a function or method. This query finds instances where a parameter is used as the name when opening a file:
168
164
@@ -178,7 +174,7 @@ We might want to make the source more specific, for example a parameter to a fun
178
174
DataFlow::localFlow(p, call.getArg(0))
179
175
select call, p
180
176
181
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/3998032643497238063/>`__. Very few results now; these could feasibly be inspected manually.
177
+
For most codebases, this will return only a few results and these could be inspected manually.
182
178
183
179
Using the exact name supplied via the parameter may be too strict. If we want to know if the parameter influences the file name, we can use taint tracking instead of data flow. This query finds calls to ``os.open`` where the filename is derived from a parameter:
184
180
@@ -194,7 +190,7 @@ Using the exact name supplied via the parameter may be too strict. If we want to
194
190
TaintTracking::localTaint(p, call.getArg(0))
195
191
select call, p
196
192
197
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2129957933670836953/>`__. Now we get more results and in more projects.
193
+
Typically, this finds more results.
198
194
199
195
Global data flow
200
196
----------------
@@ -369,8 +365,6 @@ This data flow configuration tracks data flow from environment variables to open
369
365
select fileOpen, "This call to 'os.open' uses data from $@.",
370
366
environment, "call to 'os.getenv'"
371
367
372
-
➤ `Running this in the query console on LGTM.com <https://lgtm.com/query/6582374907796191895/>`__ unsurprisingly yields no results in the demo projects.
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/1775658606775222283/>`__. Several of the LGTM.com demo projects use the ``@SuppressWarnings`` annotation. Looking at the ``value``\ s of the annotation element returned by the query, we can see that the *apache/activemq* project uses the ``"rawtypes"`` value described above.
54
+
If the codebase you are analyzing uses the ``@SuppressWarnings`` annotation, you can check the ``value``\ s of the annotation element returned by the query. They should use the ``"rawtypes"`` value described above.
55
55
56
56
As another example, this query finds all annotation types that only have a single annotation element, which has name ``value``:
57
57
@@ -66,8 +66,6 @@ As another example, this query finds all annotation types that only have a singl
66
66
)
67
67
select anntp
68
68
69
-
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/2145264152490258283/>`__.
@@ -124,7 +122,7 @@ This makes it very easy to write our query for finding methods that override ano
124
122
not overriding.getAnAnnotation() instanceof OverrideAnnotation
125
123
select overriding, "Method overrides another method, but does not have an @Override annotation."
126
124
127
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/7419756266089837339/>`__. In practice, this query may yield many results from compiled library code, which aren't very interesting. It's therefore a good idea to add another conjunct ``overriding.fromSource()`` to restrict the result to only report methods for which source code is available.
125
+
In practice, this query may yield many results from compiled library code, which aren't very interesting. It's therefore a good idea to add another conjunct ``overriding.fromSource()`` to restrict the result to only report methods for which source code is available.
128
126
129
127
Example: Finding calls to deprecated methods
130
128
--------------------------------------------
@@ -237,7 +235,7 @@ Now we can extend our query to filter out calls in methods carrying a ``Suppress
237
235
and not call.getCaller().getAnAnnotation() instanceof SuppressDeprecationWarningAnnotation
238
236
select call, "This call invokes a deprecated method."
239
237
240
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8706367340403790260/>`__. It's fairly common for projects to contain calls to methods that appear to be deprecated.
238
+
It's fairly common for projects to contain calls to methods that appear to be deprecated.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/codeql-for-cpp.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat
21
21
hash-consing-and-value-numbering
22
22
23
23
24
-
- :doc:`Basic query for C and C++ code <basic-query-for-cpp-code>`: Learn to write and run a simple CodeQL query using LGTM.
24
+
- :doc:`Basic query for C and C++ code <basic-query-for-cpp-code>`: Learn to write and run a simple CodeQL query.
25
25
26
26
- :doc:`CodeQL library for C and C++ <codeql-library-for-cpp>`: When analyzing C or C++ code, you can use the large collection of classes in the CodeQL library for C and C++.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/codeql-for-csharp.rst
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ Experiment and learn how to write effective and efficient queries for CodeQL dat
12
12
codeql-library-for-csharp
13
13
analyzing-data-flow-in-csharp
14
14
15
-
- :doc:`Basic query for C# code <basic-query-for-csharp-code>`: Learn to write and run a simple CodeQL query using LGTM.
15
+
- :doc:`Basic query for C# code <basic-query-for-csharp-code>`: Learn to write and run a simple CodeQL query.
16
16
17
17
- :doc:`CodeQL library for C# <codeql-library-for-csharp>`: When you're analyzing a C# program, you can make use of the large collection of classes in the CodeQL library for C#.
- :doc:`Basic query for Go code <basic-query-for-go-code>`: Learn to write and run a simple CodeQL query using LGTM.
16
+
- :doc:`Basic query for Go code <basic-query-for-go-code>`: Learn to write and run a simple CodeQL query.
17
17
18
18
- :doc:`CodeQL library for Go <codeql-library-for-go>`: When you're analyzing a Go program, you can make use of the large collection of classes in the CodeQL library for Go.
0 commit comments