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
@@ -20,7 +20,23 @@ Query help files must have the same base name as the query they describe and mus
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.
24
+
25
+
#### Markdown query help files
26
+
27
+
A Markdown query help file should use the following structure and section order:
Each section should be clearly marked with the appropriate heading. See the other Markdown files in this repository for examples.
36
+
37
+
#### XML query help files
38
+
39
+
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
40
25
41
```xml
26
42
<!DOCTYPEqhelp SYSTEM "qhelp.dtd">
@@ -33,7 +49,7 @@ The header and single top-level `<qhelp>` element are both mandatory.
33
49
34
50
### Section-level elements
35
51
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:
52
+
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
53
38
54
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
55
2.`recommendation`—information on how to fix the issue highlighted by the query.
@@ -44,7 +60,7 @@ For further information about the other section-level, block, list and table ele
44
60
45
61
## English style
46
62
47
-
You should write the overview and recommendation elements in simple English that is easy to follow. You should:
63
+
You should write the overview and recommendation sections in simple English that is easy to follow. You should:
48
64
49
65
* Use simple sentence structures and avoid complex or academic language.
50
66
* Avoid colloquialisms and contractions.
@@ -56,10 +72,11 @@ You should write the overview and recommendation elements in simple English that
56
72
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:
57
73
58
74
* 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.
59
-
* Put the code example after the recommendation element where possible. Only include an example in the description element if absolutely necessary.
75
+
* Put the code example after the recommendation section where possible. Only include an example in the description section if absolutely necessary.
60
76
* 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.
61
77
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
62
-
* Define the code examples in `src` files. The language is inferred from the file extension:
78
+
* For Markdown files, use fenced code blocks with the appropriate language identifier (for example, <code> ```java </code>).
79
+
* For XML files, define the code examples in `src` files. The language is inferred from the file extension:
63
80
64
81
```xml
65
82
<example>
@@ -73,11 +90,11 @@ Whenever possible, you should include a code example that helps to explain the i
73
90
</example>
74
91
```
75
92
76
-
Note, if any code words are included in the `overview` and `recommendation` sections, they should be formatted with `<code> ... </code>` for emphasis.
93
+
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.
77
94
78
95
## Including references
79
96
80
-
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:
97
+
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:
81
98
82
99
### Books
83
100
@@ -89,7 +106,7 @@ For example:
89
106
90
107
>W. C. Wake, _Refactoring Workbook_, pp. 93 – 94, Addison-Wesley Professional, 2004.
91
108
92
-
Note, & symbols need to be replaced by \&. The symbol will be displayed correctly in the HTML files generated from the query help files.
109
+
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.
93
110
94
111
### Academic papers
95
112
@@ -111,26 +128,115 @@ For example:
111
128
112
129
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.
113
130
114
-
## Validating qhelp files
131
+
## Validating query help files
115
132
116
-
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:
133
+
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.
124
143
125
-
Please include the `.qhelp` files (and any associated code snippets) in your pull request, but do not commit the generated Markdown.
126
-
127
-
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)
144
+
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
145
129
146
## Query help example
130
147
131
-
The following example is a query help file for a query from the standard query suite for Java:
148
+
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.
132
149
133
-
```xml
150
+
### Markdown example
151
+
152
+
````markdown
153
+
# Overview
154
+
155
+
A control structure (an `if` statement or a loop) has a body that is either a block
156
+
of statements surrounded by curly braces or a single statement.
157
+
158
+
If you omit braces, it is particularly important to ensure that the indentation of the code
159
+
matches the control flow of the code.
160
+
161
+
## Recommendation
162
+
163
+
It is usually considered good practice to include braces for all control
164
+
structures in Java. This is because it makes it easier to maintain the code
165
+
later. For example, it's easy to see at a glance which part of the code is in the
166
+
scope of an `if` statement, and adding more statements to the body of the `if`
167
+
statement is less error-prone.
168
+
169
+
You should also ensure that the indentation of the code is consistent with the actual flow of
170
+
control, so that it does not confuse programmers.
171
+
172
+
## Example
173
+
174
+
In the example below, the original version of `Cart` is missing braces. This means
175
+
that the code triggers a `NullPointerException` at runtime if `i`
176
+
is `null`.
177
+
178
+
```java
179
+
class Cart {
180
+
Map<Integer, Integer> items = ...
181
+
public void addItem(Item i) {
182
+
// No braces and misleading indentation.
183
+
if (i != null)
184
+
log("Adding item: " + i);
185
+
// Indentation suggests that the following statements
186
+
// are in the body of the 'if'.
187
+
Integer curQuantity = items.get(i.getID());
188
+
if (curQuantity == null) curQuantity = 0;
189
+
items.put(i.getID(), curQuantity+1);
190
+
}
191
+
}
192
+
```
193
+
194
+
The corrected version of `Cart` does include braces, so
195
+
that the code executes as the indentation suggests.
196
+
197
+
```java
198
+
class Cart {
199
+
Map<Integer, Integer> items = ...
200
+
public void addItem(Item i) {
201
+
// Braces included.
202
+
if (i != null) {
203
+
log("Adding item: " + i);
204
+
Integer curQuantity = items.get(i.getID());
205
+
if (curQuantity == null) curQuantity = 0;
206
+
items.put(i.getID(), curQuantity+1);
207
+
}
208
+
}
209
+
}
210
+
```
211
+
212
+
In the following example the indentation may or may not be misleading depending on your tab width
213
+
settings. As such, mixing tabs and spaces in this way is not recommended, since what looks fine in
214
+
one context can be very misleading in another.
215
+
216
+
```java
217
+
// Tab width 8
218
+
if (b) // Indentation: 1 tab
219
+
f(); // Indentation: 2 tabs
220
+
g(); // Indentation: 8 spaces
221
+
222
+
// Tab width 4
223
+
if (b) // Indentation: 1 tab
224
+
f(); // Indentation: 2 tabs
225
+
g(); // Indentation: 8 spaces
226
+
```
227
+
228
+
If you mix tabs and spaces in this way, then you might get seemingly false positives, since your
229
+
tab width settings cannot be taken into account.
230
+
231
+
## References
232
+
233
+
* Java SE Documentation: [Compound Statements](https://www.oracle.com/java/technologies/javase/codeconventions-statements.html#15395)
0 commit comments