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
➤ `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.
- :doc:`Basic query for Java code <basic-query-for-java-code>`: Learn to write and run a simple CodeQL query using LGTM.
29
+
- :doc:`Basic query for Java code <basic-query-for-java-code>`: Learn to write and run a simple CodeQL query.
30
30
31
31
- :doc:`CodeQL library for Java <codeql-library-for-java>`: When analyzing Java code, you can use the large collection of classes in the CodeQL library for Java.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/codeql-library-for-java.rst
+10-22Lines changed: 10 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ For example, the following query finds all variables of type ``int`` in the prog
70
70
pt.hasName("int")
71
71
select v
72
72
73
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/860076406167044435/>`__. You're likely to get many results when you run this query because most projects contain many variables of type ``int``.
73
+
You're likely to get many results when you run this query because most projects contain many variables of type ``int``.
74
74
75
75
Reference types are also categorized according to their declaration scope:
76
76
@@ -87,7 +87,7 @@ For instance, this query finds all top-level types whose name is not the same as
87
87
where tl.getName() != tl.getCompilationUnit().getName()
88
88
select tl
89
89
90
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/4340983612585284460/>`__. This pattern is seen in many projects. When we ran it on the LGTM.com demo projects, most of the projects had at least one instance of this problem in the source code. There were many more instances in the files referenced by the source code.
90
+
You will typically see this pattern in the source code of a repository, with many more instances in the files referenced by the source code.
91
91
92
92
Several more specialized classes are available as well:
93
93
@@ -109,7 +109,7 @@ As an example, we can write a query that finds all nested classes that directly
109
109
where nc.getASupertype() instanceof TypeObject
110
110
select nc
111
111
112
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8482509736206423238/>`__. You're likely to get many results when you run this query because many projects include nested classes that extend ``Object`` directly.
112
+
You're likely to get many results when you run this query because many projects include nested classes that extend ``Object`` directly.
113
113
114
114
Generics
115
115
~~~~~~~~
@@ -143,8 +143,6 @@ For instance, we could use the following query to find all parameterized instanc
143
143
pt.getSourceDeclaration() = map
144
144
select pt
145
145
146
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/7863873821043873550/>`__. None of the LGTM.com demo projects contain parameterized instances of ``java.util.Map`` in their source code, but they all have results in reference files.
147
-
148
146
In general, generic types may restrict which types a type parameter can be bound to. For instance, a type of maps from strings to numbers could be declared as follows:
149
147
150
148
.. code-block:: java
@@ -166,8 +164,6 @@ As an example, the following query finds all type variables with type bound ``Nu
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6740696080876162817/>`__. When we ran it on the LGTM.com demo projects, the *neo4j/neo4j*, *hibernate/hibernate-orm* and *apache/hadoop* projects all contained examples of this pattern.
170
-
171
167
For dealing with legacy code that is unaware of generics, every generic type has a "raw" version without any type parameters. In the CodeQL libraries, raw types are represented using class ``RawType``, which has the expected subclasses ``RawClass`` and ``RawInterface``. Again, there is a predicate ``getSourceDeclaration`` for obtaining the corresponding generic type. As an example, we can find variables of (raw) type ``Map``:
172
168
173
169
.. code-block:: ql
@@ -179,8 +175,6 @@ For dealing with legacy code that is unaware of generics, every generic type has
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/4032913402499547882/>`__. Many projects have variables of raw type ``Map``.
183
-
184
178
For example, in the following code snippet this query would find ``m1``, but not ``m2``:
185
179
186
180
.. code-block:: java
@@ -230,7 +224,7 @@ For example, the following query finds all expressions whose parents are ``retur
230
224
where e.getParent() instanceof ReturnStmt
231
225
select e
232
226
233
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1947757851560375919/>`__. Many projects have examples of ``return`` statements with child expressions.
227
+
Many projects have examples of ``return`` statements with child expressions.
234
228
235
229
Therefore, if the program contains a return statement ``return x + y;``, this query will return ``x + y``.
236
230
@@ -244,7 +238,7 @@ As another example, the following query finds statements whose parent is an ``if
244
238
where s.getParent() instanceof IfStmt
245
239
select s
246
240
247
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1989464153689219612/>`__. Many projects have examples of ``if`` statements with child statements.
241
+
Many projects have examples of ``if`` statements with child statements.
248
242
249
243
This query will find both ``then`` branches and ``else`` branches of all ``if`` statements in the program.
250
244
@@ -258,8 +252,6 @@ Finally, here is a query that finds method bodies:
258
252
where s.getParent() instanceof Method
259
253
select s
260
254
261
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1016821702972128245/>`__. Most projects have many method bodies.
262
-
263
255
As these examples show, the parent node of an expression is not always an expression: it may also be a statement, for example, an ``IfStmt``. Similarly, the parent node of a statement is not always a statement: it may also be a method or a constructor. To capture this, the QL Java library provides two abstract class ``ExprParent`` and ``StmtParent``, the former representing any node that may be the parent node of an expression, and the latter any node that may be the parent node of a statement.
264
256
265
257
For more information on working with AST classes, see the :doc:`article on overflow-prone comparisons in Java <overflow-prone-comparisons-in-java>`.
@@ -278,7 +270,7 @@ For annotations, class ``Annotatable`` is a superclass of all program elements t
278
270
from Constructor c
279
271
select c.getAnAnnotation()
280
272
281
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/3206112561297137365/>`__. The LGTM.com demo projects all use annotations, you can see examples where they are used to suppress warnings and mark code as deprecated.
273
+
You may see examples where annotations are used to suppress warnings or to mark code as deprecated.
282
274
283
275
These annotations are represented by class ``Annotation``. An annotation is simply an expression whose type is an ``AnnotationType``. For example, you can amend this query so that it only reports deprecated constructors:
284
276
@@ -292,7 +284,7 @@ These annotations are represented by class ``Annotation``. An annotation is simp
292
284
anntp.hasQualifiedName("java.lang", "Deprecated")
293
285
select ann
294
286
295
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/5393027107459215059/>`__. Only constructors with the ``@Deprecated`` annotation are reported this time.
287
+
Only constructors with the ``@Deprecated`` annotation are reported this time.
296
288
297
289
For more information on working with annotations, see the :doc:`article on annotations <annotations-in-java>`.
298
290
@@ -307,7 +299,7 @@ For Javadoc, class ``Element`` has a member predicate ``getDoc`` that returns a
307
299
jdoc = f.getDoc().getJavadoc()
308
300
select jdoc
309
301
310
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6022769142134600659/>`__. You can see this pattern in many projects.
302
+
You can see this pattern in many projects.
311
303
312
304
Class ``Javadoc`` represents an entire Javadoc comment as a tree of ``JavadocElement`` nodes, which can be traversed using member predicates ``getAChild`` and ``getParent``. For instance, you could edit the query so that it finds all ``@author`` tags in Javadoc comments on private fields:
313
305
@@ -321,8 +313,6 @@ Class ``Javadoc`` represents an entire Javadoc comment as a tree of ``JavadocEle
321
313
at.getParent+() = jdoc
322
314
select at
323
315
324
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2510220694395289111/>`__. None of the LGTM.com demo projects uses the ``@author`` tag on private fields.
325
-
326
316
.. pull-quote::
327
317
328
318
Note
@@ -349,7 +339,7 @@ For example, the following query finds methods with a `cyclomatic complexity <ht
349
339
mc.getCyclomaticComplexity() > 40
350
340
select m
351
341
352
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/6566950741051181919/>`__. Most large projects include some methods with a very high cyclomatic complexity. These methods are likely to be difficult to understand and test.
342
+
Most large projects include some methods with a very high cyclomatic complexity. These methods are likely to be difficult to understand and test.
353
343
354
344
Call graph
355
345
----------
@@ -369,8 +359,6 @@ We can use predicate ``Call.getCallee`` to find out which method or constructor
369
359
m.hasName("println")
370
360
select c
371
361
372
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/5861255162551917595/>`__. The LGTM.com demo projects all include many calls to methods of this name.
373
-
374
362
Conversely, ``Callable.getAReference`` returns a ``Call`` that refers to it. So we can find methods and constructors that are never called using this query:
375
363
376
364
.. code-block:: ql
@@ -381,7 +369,7 @@ Conversely, ``Callable.getAReference`` returns a ``Call`` that refers to it. So
381
369
where not exists(c.getAReference())
382
370
select c
383
371
384
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/7261739919657747703/>`__. The LGTM.com demo projects all appear to have many methods that are not called directly, but this is unlikely to be the whole story. To explore this area further, see ":doc:`Navigating the call graph <navigating-the-call-graph>`."
372
+
Codebases often have many methods that are not called directly, but this is unlikely to be the whole story. To explore this area further, see ":doc:`Navigating the call graph <navigating-the-call-graph>`."
385
373
386
374
For more information about callables and calls, see the :doc:`article on the call graph <navigating-the-call-graph>`.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/javadoc.rst
+1-3Lines changed: 1 addition & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -149,8 +149,6 @@ Now we can write a query for finding all callables ``c`` and ``@throws`` tags ``
149
149
not mayThrow(c, exn)
150
150
select tt, "Spurious @throws tag."
151
151
152
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/1258570917227966396/>`__. This finds several results in the LGTM.com demo projects.
153
-
154
152
Improvements
155
153
~~~~~~~~~~~~
156
154
@@ -216,7 +214,7 @@ The first case can be covered by changing ``getDocumentedException`` to use the
216
214
(result.hasName(tt.getExceptionName()) and visibleIn(tt.getFile(), result))
217
215
}
218
216
219
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/8016848987103345329/>`__. This finds many fewer, more interesting results in the LGTM.com demo projects.
217
+
This query should find many fewer, more interesting results.
220
218
221
219
Currently, ``visibleIn`` only considers single-type imports, but you could extend it with support for other kinds of imports.
Copy file name to clipboardExpand all lines: docs/codeql/codeql-language-guides/overflow-prone-comparisons-in-java.rst
+40-12Lines changed: 40 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ We'll start by writing a query that finds less-than expressions (CodeQL class ``
44
44
expr.getRightOperand().getType().hasName("long")
45
45
select expr
46
46
47
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/490866529746563234/>`__. This query usually finds results on most projects.
47
+
This query usually finds results on most codebases.
48
48
49
49
Notice that we use the predicate ``getType`` (available on all subclasses of ``Expr``) to determine the type of the operands. Types, in turn, define the ``hasName`` predicate, which allows us to identify the primitive types ``int`` and ``long``. As it stands, this query finds *all* less-than expressions comparing ``int`` and ``long``, but in fact we are only interested in comparisons that are part of a loop condition. Also, we want to filter out comparisons where either operand is constant, since these are less likely to be real bugs. The revised query looks like this:
50
50
@@ -59,7 +59,7 @@ Notice that we use the predicate ``getType`` (available on all subclasses of ``E
59
59
not expr.getAnOperand().isCompileTimeConstant()
60
60
select expr
61
61
62
-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/4315986481180063825/>`__. Notice that fewer results are found.
62
+
Notice that fewer results are found.
63
63
64
64
The class ``LoopStmt`` is a common superclass of all loops, including, in particular, ``for`` loops as in our example above. While different kinds of loops have different syntax, they all have a loop condition, which can be accessed through predicate ``getCondition``. We use the reflexive transitive closure operator ``*`` applied to the ``getAChildExpr`` predicate to express the requirement that ``expr`` should be nested inside the loop condition. In particular, it can be the loop condition itself.
65
65
@@ -113,16 +113,44 @@ Now we rewrite our query to make use of these new classes:
113
113
114
114
.. code-block:: ql
115
115
116
-
import Java
117
-
118
-
// Insert the class definitions from above
119
-
120
-
from OverflowProneComparison expr
121
-
where exists(LoopStmt l | l.getCondition().getAChildExpr*() = expr) and
122
-
not expr.getAnOperand().isCompileTimeConstant()
123
-
select expr
124
-
125
-
➤ `See the full query in the query console on LGTM.com <https://lgtm.com/query/506868054626167462/>`__.
116
+
import java
117
+
118
+
// Return the width (in bits) of a given integral type
119
+
int width(PrimitiveType pt) {
120
+
(pt.hasName("byte") and result=8) or
121
+
(pt.hasName("short") and result=16) or
122
+
(pt.hasName("char") and result=16) or
123
+
(pt.hasName("int") and result=32) or
124
+
(pt.hasName("long") and result=64)
125
+
}
126
+
127
+
// Find any comparison where the width of the type on the smaller end of
128
+
// the comparison is less than the width of the type on the greater end
129
+
abstract class OverflowProneComparison extends ComparisonExpr {
130
+
Expr getLesserOperand() { none() }
131
+
Expr getGreaterOperand() { none() }
132
+
}
133
+
134
+
// Return `<=` and `<` comparisons
135
+
class LTOverflowProneComparison extends OverflowProneComparison {
136
+
LTOverflowProneComparison() {
137
+
(this instanceof LEExpr or this instanceof LTExpr) and
0 commit comments