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
@@ -16,11 +16,33 @@ When you contribute a new [supported query](supported-queries.md) to this reposi
16
16
17
17
### Location and file name
18
18
19
-
Query help files must have the same base name as the query they describe and must be located in the same directory.
19
+
Query help files must have the same base name as the query they describe and must be located in the same directory.
20
20
21
21
### File structure and layout
22
22
23
-
Query help files are written using a custom XML format, and stored in a file with a `.qhelp` extension. The basic structure is as follows:
23
+
Query help files can be written in either a custom XML format (with a `.qhelp` extension) or in Markdown (with a `.md` extension). Both formats are supported by the CodeQL documentation tooling. There are a few minor differences, noted in the section `Differences between XML and markdown formats` below.
24
+
25
+
#### Markdown query help files
26
+
27
+
A Markdown query help file should use the following structure and section order (note that the `Implementation notes` section is optional):
28
+
29
+
```
30
+
## Overview
31
+
32
+
## Recommendation
33
+
34
+
## Example
35
+
36
+
## Implementation notes
37
+
38
+
## References
39
+
```
40
+
41
+
Each section should be clearly marked with the appropriate heading. See the other Markdown files in this repository for examples.
42
+
43
+
#### XML query help files
44
+
45
+
Query help files can also be written using a custom XML format, and stored in a file with a `.qhelp` extension. The basic structure is as follows:
24
46
25
47
```xml
26
48
<!DOCTYPEqhelp SYSTEM "qhelp.dtd">
@@ -33,7 +55,7 @@ The header and single top-level `<qhelp>` element are both mandatory.
33
55
34
56
### Section-level elements
35
57
36
-
Section-level elements are used to group the information within the query help file. All query help files should include at least the following section elements, in the order specified:
58
+
Section-level elements are used to group the information within the query help file. For both Markdown and XML formats, the following sections should be included, in the order specified:
37
59
38
60
1.`overview`—a short summary of the issue that the query identifies, including an explanation of how it could affect the behavior of the program.
39
61
2.`recommendation`—information on how to fix the issue highlighted by the query.
@@ -42,10 +64,9 @@ Section-level elements are used to group the information within the query help f
42
64
43
65
For further information about the other section-level, block, list and table elements supported by query help files, see [Query help files](https://codeql.github.com/docs/writing-codeql-queries/query-help-files/) on codeql.github.com.
44
66
45
-
46
67
## English style
47
68
48
-
You should write the overview and recommendation elements in simple English that is easy to follow. You should:
69
+
You should write the overview and recommendation sections in simple English that is easy to follow. You should:
49
70
50
71
* Use simple sentence structures and avoid complex or academic language.
51
72
* Avoid colloquialisms and contractions.
@@ -57,10 +78,11 @@ You should write the overview and recommendation elements in simple English that
57
78
Whenever possible, you should include a code example that helps to explain the issue you are highlighting. Any code examples that you include should adhere to the following guidelines:
58
79
59
80
* The example should be less than 20 lines, but it should still clearly illustrate the issue that the query identifies. If appropriate, then the example may also be runnable.
60
-
* Put the code example after the recommendation element where possible. Only include an example in the description element if absolutely necessary.
81
+
* Put the code example after the recommendation section where possible. Only include an example in the description section if absolutely necessary.
61
82
* If you are using an example to illustrate the solution to a problem, and the change required is minor, avoid repeating the whole example. It is preferable to either describe the change required or to include a smaller snippet of the corrected code.
62
83
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
63
-
* Define the code examples in `src` files. The language is inferred from the file extension:
84
+
* For Markdown files, use fenced code blocks with the appropriate language identifier (for example, <code> ```java </code>).
85
+
* For XML files, define the code examples in `src` files. The language is inferred from the file extension:
64
86
65
87
```xml
66
88
<example>
@@ -74,11 +96,11 @@ Whenever possible, you should include a code example that helps to explain the i
74
96
</example>
75
97
```
76
98
77
-
Note, if any code words are included in the `overview` and `recommendation` sections, they should be formatted with `<code> ... </code>` for emphasis.
99
+
Note, if any code words are included in the `overview` and `recommendation` sections, in Markdown they should be formatted with backticks (<code>`...`</code>) and in XML they should be formatted with`<code> ... </code>` for emphasis.
78
100
79
101
## Including references
80
102
81
-
You should include one or more references, list formatted with `<li> ... </li>` for each item, to provide further information about the problem that your query is designed to find. References can be of the following types:
103
+
You should include one or more references, formatted as an unordered list (`- ...` or `* ...`) in Markdown or with `<li> ... </li>` for each item in XML, to provide further information about the problem that your query is designed to find. References can be of the following types:
82
104
83
105
### Books
84
106
@@ -90,15 +112,14 @@ For example:
90
112
91
113
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
92
114
93
-
Note, & symbols need to be replaced by \&. The symbol will be displayed correctly in the HTML files generated from the query help files.
115
+
Note, & symbols need to be replaced by \& in XML. The symbol will be displayed correctly in the HTML files generated from the query help files.
94
116
95
117
### Academic papers
96
118
97
119
If you are citing an academic paper, we recommend adopting the reference style of the journal that you are citing. For example:
98
120
99
121
>S. R. Chidamber and C. F. Kemerer, _A metrics suite for object-oriented design_. IEEE Transactions on Software Engineering, 20(6):476-493, 1994.
100
122
101
-
102
123
### Websites
103
124
104
125
If you are citing a website, please use the following format, without breadcrumb trails:
@@ -111,28 +132,123 @@ For example:
111
132
112
133
### Referencing potential security weaknesses
113
134
114
-
If your query checks code for a CWE weakness, you should use the `@tags` element in the query file to reference the associated CWEs, as explained [here](query-metadata-style-guide.md). When you use these tags, a link to the appropriate entry from the [MITRE.org](https://cwe.mitre.org/scoring/index.html) site will automatically appear as a reference in the output HTML file.
135
+
If your query checks code for a CWE weakness, you should use the `@tags` element in the query file to reference the associated CWEs, as explained [here](query-metadata-style-guide.md). When you use these tags in a query help file in the custom XML format, a link to the appropriate entry from the [MITRE.org](https://cwe.mitre.org/scoring/index.html) site will automatically appear as a reference in the output HTML file.
115
136
116
-
## Validating qhelp files
137
+
## Validating query help files
117
138
118
-
Before making a pull request, please ensure the `.qhelp` files are well-formed and can be generated without errors. This can be done locally with the CodeQL CLI, as shown in the following example:
139
+
Before making a pull request, please ensure the `.qhelp`or `.md`files are well-formed and can be generated without errors. This can be done locally with the CodeQL CLI, as shown in the following example:
Please include the query help files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
126
149
127
-
Please include the `.qhelp` files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
150
+
More information on how to test your query help files can be found [within the documentation](https://docs.github.com/en/code-security/codeql-cli/using-the-codeql-cli/testing-query-help-files)
128
151
129
-
More information on how to test your `.qhelp` files can be found [within the documentation](https://docs.github.com/en/code-security/codeql-cli/using-the-codeql-cli/testing-query-help-files)
152
+
## Differences between XML and markdown formats
153
+
154
+
1. The XML format allows for the contents of other files to be included in the output generated by processing the file, as mentioned in the section `Code examples`. This is not possible with the Markdown format.
155
+
2. When using the XML format, references are added to the output HTML file based on CWE tags, as mentioned in the section `Referencing potential security weaknesses`.
156
+
3. For custom queries and custom query packs, only the Markdown format is supported.
130
157
131
158
## Query help example
132
159
133
-
The following example is a query help file for a query from the standard query suite for Java:
160
+
The following example is a query help file for a query from the standard query suite for Java, shown in both Markdown and XML formats.
134
161
135
-
```xml
162
+
### Markdown example
163
+
164
+
````markdown
165
+
# Overview
166
+
167
+
A control structure (an `if` statement or a loop) has a body that is either a block
168
+
of statements surrounded by curly braces or a single statement.
169
+
170
+
If you omit braces, it is particularly important to ensure that the indentation of the code
171
+
matches the control flow of the code.
172
+
173
+
## Recommendation
174
+
175
+
It is usually considered good practice to include braces for all control
176
+
structures in Java. This is because it makes it easier to maintain the code
177
+
later. For example, it's easy to see at a glance which part of the code is in the
178
+
scope of an `if` statement, and adding more statements to the body of the `if`
179
+
statement is less error-prone.
180
+
181
+
You should also ensure that the indentation of the code is consistent with the actual flow of
182
+
control, so that it does not confuse programmers.
183
+
184
+
## Example
185
+
186
+
In the example below, the original version of `Cart` is missing braces. This means
187
+
that the code triggers a `NullPointerException` at runtime if `i`
188
+
is `null`.
189
+
190
+
```java
191
+
class Cart {
192
+
Map<Integer, Integer> items = ...
193
+
public void addItem(Item i) {
194
+
// No braces and misleading indentation.
195
+
if (i != null)
196
+
log("Adding item: " + i);
197
+
// Indentation suggests that the following statements
198
+
// are in the body of the 'if'.
199
+
Integer curQuantity = items.get(i.getID());
200
+
if (curQuantity == null) curQuantity = 0;
201
+
items.put(i.getID(), curQuantity+1);
202
+
}
203
+
}
204
+
```
205
+
206
+
The corrected version of `Cart` does include braces, so
207
+
that the code executes as the indentation suggests.
208
+
209
+
```java
210
+
class Cart {
211
+
Map<Integer, Integer> items = ...
212
+
public void addItem(Item i) {
213
+
// Braces included.
214
+
if (i != null) {
215
+
log("Adding item: " + i);
216
+
Integer curQuantity = items.get(i.getID());
217
+
if (curQuantity == null) curQuantity = 0;
218
+
items.put(i.getID(), curQuantity+1);
219
+
}
220
+
}
221
+
}
222
+
```
223
+
224
+
In the following example the indentation may or may not be misleading depending on your tab width
225
+
settings. As such, mixing tabs and spaces in this way is not recommended, since what looks fine in
226
+
one context can be very misleading in another.
227
+
228
+
```java
229
+
// Tab width 8
230
+
if (b) // Indentation: 1 tab
231
+
f(); // Indentation: 2 tabs
232
+
g(); // Indentation: 8 spaces
233
+
234
+
// Tab width 4
235
+
if (b) // Indentation: 1 tab
236
+
f(); // Indentation: 2 tabs
237
+
g(); // Indentation: 8 spaces
238
+
```
239
+
240
+
If you mix tabs and spaces in this way, then you might get seemingly false positives, since your
241
+
tab width settings cannot be taken into account.
242
+
243
+
## References
244
+
245
+
* Java SE Documentation: [Compound Statements](https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#15395)
0 commit comments