mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Docs: Use GitHub links for guides, improve formatting
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
## Introduction
|
||||
|
||||
This document describes how to format the code you contribute to this repository. It covers aspects such as layout, white-space, naming, and documentation. Adhering to consistent standards makes code easier to read and maintain. Of course, these are only guidelines, and can be overridden as the need arises on a case-by-case basis. Where existing code deviates from these guidelines, prefer consistency with the surrounding code.
|
||||
Note, if you use [CodeQL for Visual Studio Code](https://help.semmle.com/codeql/codeql-for-vscode/procedures/about-codeql-for-vscode.html), you can autoformat your query in the editor.
|
||||
Note, if you use [CodeQL for Visual Studio Code](https://codeql.github.com/docs/codeql-for-visual-studio-code/about-codeql-for-visual-studio-code/), you can autoformat your query in the editor.
|
||||
|
||||
Words in *italic* are defined in the [Glossary](#glossary).
|
||||
|
||||
@@ -166,7 +166,7 @@ private predicate foo(Expr e, Expr p) {
|
||||
```
|
||||
|
||||
## Naming
|
||||
1. Use [PascalCase](http://wiki.c2.com/?PascalCase) for:
|
||||
1. Use [PascalCase](https://wiki.c2.com/?PascalCase) for:
|
||||
- `class` names
|
||||
- `module` names
|
||||
- `newtype` names
|
||||
@@ -249,7 +249,7 @@ For more information about documenting the code that you contribute to this repo
|
||||
1. The `and` and `else` keywords *may* be placed on the same line as the closing parenthesis.
|
||||
1. The `and` and `else` keywords *may* be "cuddled": `) else (`
|
||||
1. *Always* qualify *calls* to predicates of the same class with `this`.
|
||||
2. *Prefer* postfix casts `a.(Expr)` to prefix casts `(Expr)a`.
|
||||
1. *Prefer* postfix casts `a.(Expr)` to prefix casts `(Expr)a`.
|
||||
|
||||
### Examples
|
||||
|
||||
@@ -350,16 +350,16 @@ For more information about documenting the code that you contribute to this repo
|
||||
|
||||
| Phrase | Meaning |
|
||||
|-------------|----------|
|
||||
| *[annotation](https://help.semmle.com/QL/ql-handbook/language.html#annotations)* | An additional specifier used to modify a declaration, such as `private`, `override`, `deprecated`, `pragma`, `bindingset`, or `cached`. |
|
||||
| *[annotation](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#annotations)* | An additional specifier used to modify a declaration, such as `private`, `override`, `deprecated`, `pragma`, `bindingset`, or `cached`. |
|
||||
| *body* | The text inside `{ }`, `( )`, or each section of an `if`-`then`-`else` or `from`-`where`-`select`. |
|
||||
| *binary operator* | An operator with two operands, such as comparison operators, `and`, `or`, `implies`, or arithmetic operators. |
|
||||
| *call* | A *formula* that invokes a predicate, e.g. `this.isStatic()` or `calls(a,b)`. |
|
||||
| *[conjunct](https://help.semmle.com/QL/ql-handbook/language.html#conjunctions)* | A formula that is an operand to an `and`. |
|
||||
| *[conjunct](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#conjunctions)* | A formula that is an operand to an `and`. |
|
||||
| *declaration* | A class, module, predicate, field or newtype. |
|
||||
| *[disjunct](https://help.semmle.com/QL/ql-handbook/language.html#disjunctions)* | A formula that is an operand to an `or`. |
|
||||
| *[formula](https://help.semmle.com/QL/ql-handbook/language.html#formulas)* | A logical expression, such as `A = B`, a *call*, a *quantifier*, `and`, `or`, `not`, `in` or `instanceof`. |
|
||||
| *[disjunct](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#disjunctions)* | A formula that is an operand to an `or`. |
|
||||
| *[formula](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#formulas)* | A logical expression, such as `A = B`, a *call*, a *quantifier*, `and`, `or`, `not`, `in` or `instanceof`. |
|
||||
| *should/should not/avoid/prefer* | Adhere to this rule wherever possible, where it makes sense. |
|
||||
| *may/can* | This is a reasonable alternative, to be used with discretion. |
|
||||
| *must/always/do not* | Always adhere to this rule. |
|
||||
| *[quantifier/aggregation](https://help.semmle.com/QL/ql-handbook/language.html#aggregations)* | `exists`, `count`, `strictcount`, `any`, `forall`, `forex` and so on. |
|
||||
| *[quantifier/aggregation](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#aggregations)* | `exists`, `count`, `strictcount`, `any`, `forall`, `forex` and so on. |
|
||||
| *variable* | A parameter to a predicate, a field, a from variable, or a variable introduced by a *quantifier* or *aggregation*. |
|
||||
|
||||
@@ -124,11 +124,12 @@ Certain special predicates should be documented consistently.
|
||||
* The location spans column `startcolumn` of line `startline` to
|
||||
* column `endcolumn` of line `endline` in file `filepath`.
|
||||
* For more information, see
|
||||
* [Locations](https://help.semmle.com/QL/learn-ql/locations.html).
|
||||
* [Locations](https://codeql.github.com/docs/writing-codeql-queries/providing-locations-in-codeql-queries/).
|
||||
*/
|
||||
|
||||
predicate hasLocationInfo(string filepath, int startline, int startcolumn, int endline, int endcolumn) { ... }
|
||||
```
|
||||
|
||||
## QLDoc for classes
|
||||
|
||||
1. Document classes using a noun phrase of the form `A <domain element> that <has property>.`
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
|
||||
When you contribute a new [supported query](supported-queries.md) to this repository, or add a custom query for analysis in LGTM, you should also write a query help file. This file provides detailed information about the purpose and use of the query, which is available to users in LGTM (for example [here](https://lgtm.com/rules/1506093386171/)) and on the query homepages:
|
||||
|
||||
* [C/C++ queries](https://help.semmle.com/wiki/display/CCPPOBJ/)
|
||||
* [C# queries](https://help.semmle.com/wiki/display/CSHARP/)
|
||||
* [Java queries](https://help.semmle.com/wiki/display/JAVA/)
|
||||
* [JavaScript queries](https://help.semmle.com/wiki/display/JS/)
|
||||
* [Python queries](https://help.semmle.com/wiki/display/PYTHON/)
|
||||
* [C/C++ queries](https://codeql.github.com/codeql-query-help/cpp/)
|
||||
* [C# queries](https://codeql.github.com/codeql-query-help/csharp/)
|
||||
* [Go queries](https://codeql.github.com/codeql-query-help/go/)
|
||||
* [Java queries](https://codeql.github.com/codeql-query-help/java/)
|
||||
* [JavaScript queries](https://codeql.github.com/codeql-query-help/javascript/)
|
||||
* [Python queries](https://codeql.github.com/codeql-query-help/python/)
|
||||
|
||||
### Location and file name
|
||||
|
||||
@@ -18,7 +19,7 @@ Query help files must have the same base name as the query they describe and mus
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
```xml
|
||||
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
|
||||
<qhelp>
|
||||
CONTAINS one or more section-level elements
|
||||
@@ -36,7 +37,7 @@ Section-level elements are used to group the information within the query help f
|
||||
3. `example`—an example of code showing the problem. Where possible, this section should also include a solution to the issue.
|
||||
4. `references`—relevant references, such as authoritative sources on language semantics and best practice.
|
||||
|
||||
For further information about the other section-level, block, list and table elements supported by query help files, see [Query help files](https://help.semmle.com/QL/learn-ql/ql/writing-queries/query-help.html) on help.semmle.com.
|
||||
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.
|
||||
|
||||
|
||||
## English style
|
||||
@@ -58,7 +59,7 @@ Whenever possible, you should include a code example that helps to explain the i
|
||||
* Clearly indicate which of the samples is an example of bad coding practice and which is recommended practice.
|
||||
* Define the code examples in `src` files. The language is inferred from the file extension:
|
||||
|
||||
```
|
||||
```xml
|
||||
<example>
|
||||
<p>This example highlights poor coding practice</p>
|
||||
|
||||
@@ -113,7 +114,7 @@ If your query checks code for a CWE weakness, you should use the `@tags` element
|
||||
|
||||
The following example is a query help file for a query from the standard query suite for Java:
|
||||
|
||||
```
|
||||
```xml
|
||||
<!DOCTYPE qhelp PUBLIC
|
||||
"-//Semmle//qhelp//EN"
|
||||
"qhelp.dtd">
|
||||
|
||||
@@ -10,25 +10,25 @@ Query files have the extension `.ql`. Each file has two distinct areas:
|
||||
|
||||
* Metadata area–displayed at the top of the file, contains the metadata that defines how results for the query are interpreted and gives a brief description of the purpose of the query.
|
||||
* Query definition–defined using QL. The query includes a select statement, which defines the content and format of the results. For further information about writing QL, see the following topics:
|
||||
* [Learning CodeQL](https://help.semmle.com/QL/learn-ql/index.html)
|
||||
* [QL language reference](https://help.semmle.com/QL/ql-handbook/index.html)
|
||||
* [CodeQL style guide](https://github.com/github/codeql/blob/main/docs/ql-style-guide.md)
|
||||
* [CodeQL documentation](https://codeql.github.com/docs/)
|
||||
* [QL language reference](https://codeql.github.com/docs/ql-language-reference/)
|
||||
* [CodeQL style guide](ql-style-guide.md)
|
||||
|
||||
|
||||
For examples of query files for the languages supported by CodeQL, visit the following links:
|
||||
|
||||
* [C/C++ queries](https://help.semmle.com/wiki/display/CCPPOBJ/)
|
||||
* [C# queries](https://help.semmle.com/wiki/display/CSHARP/)
|
||||
* [Go queries](https://help.semmle.com/wiki/display/GO/)
|
||||
* [Java queries](https://help.semmle.com/wiki/display/JAVA/)
|
||||
* [JavaScript queries](https://help.semmle.com/wiki/display/JS/)
|
||||
* [Python queries](https://help.semmle.com/wiki/display/PYTHON/)
|
||||
* [C/C++ queries](https://codeql.github.com/codeql-query-help/cpp/)
|
||||
* [C# queries](https://codeql.github.com/codeql-query-help/csharp/)
|
||||
* [Go queries](https://codeql.github.com/codeql-query-help/go/)
|
||||
* [Java queries](https://codeql.github.com/codeql-query-help/java/)
|
||||
* [JavaScript queries](https://codeql.github.com/codeql-query-help/javascript/)
|
||||
* [Python queries](https://codeql.github.com/codeql-query-help/python/)
|
||||
|
||||
## Metadata area
|
||||
|
||||
Query file metadata contains important information that defines the identifier and purpose of the query. The metadata is included as the content of a valid [QLDoc](https://codeql.github.com/docs/ql-language-reference/ql-language-specification/#qldoc) comment, on lines with leading whitespace followed by `*`, between an initial `/**` and a trailing `*/`. For example:
|
||||
|
||||
```
|
||||
```ql
|
||||
/**
|
||||
* @name Useless assignment to local variable
|
||||
* @description An assignment to a local variable that is not used later on, or whose value is always
|
||||
@@ -42,7 +42,7 @@ Query file metadata contains important information that defines the identifier a
|
||||
*/
|
||||
```
|
||||
|
||||
To help others use your query, and to ensure that the query works correctly on LGTM, you should include all of the required information outlined below in the metadata, and as much of the optional information as possible. For further information on query metadata see [Metadata for CodeQL queries](https://help.semmle.com/QL/learn-ql/ql/writing-queries/query-metadata.html) on help.semmle.com.
|
||||
To help others use your query, and to ensure that the query works correctly on LGTM, you should include all of the required information outlined below in the metadata, and as much of the optional information as possible. For further information on query metadata see [Metadata for CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/metadata-for-codeql-queries/) on codeql.github.com.
|
||||
|
||||
|
||||
|
||||
@@ -132,12 +132,12 @@ The `@tags` property is used to define categories that the query relates to. Eac
|
||||
|
||||
There are also more specific `@tags` that can be added. See, the following pages for examples of the low-level tags:
|
||||
|
||||
* [C/C++ queries](https://help.semmle.com/wiki/display/CCPPOBJ/)
|
||||
* [C# queries](https://help.semmle.com/wiki/display/CSHARP/)
|
||||
* [Go queries](https://help.semmle.com/wiki/display/GO/)
|
||||
* [Java queries](https://help.semmle.com/wiki/display/JAVA/)
|
||||
* [JavaScript queries](https://help.semmle.com/wiki/display/JS/)
|
||||
* [Python queries](https://help.semmle.com/wiki/display/PYTHON/)
|
||||
* [C/C++ queries](https://codeql.github.com/codeql-query-help/cpp/)
|
||||
* [C# queries](https://codeql.github.com/codeql-query-help/csharp/)
|
||||
* [Go queries](https://codeql.github.com/codeql-query-help/go/)
|
||||
* [Java queries](https://codeql.github.com/codeql-query-help/java/)
|
||||
* [JavaScript queries](https://codeql.github.com/codeql-query-help/javascript/)
|
||||
* [Python queries](https://codeql.github.com/codeql-query-help/python/)
|
||||
|
||||
If necessary, you can also define your own low-level tags to categorize the queries specific to your project or organization. When creating your own tags, you should:
|
||||
|
||||
@@ -155,7 +155,7 @@ If your query is a security query, use one or more `@tags` to associate it with
|
||||
||`external/cwe/cwe-036` |
|
||||
||`external/cwe/cwe-073` |
|
||||
|
||||
When you tag a query like this, the associated CWE pages from [MITRE.org](http://cwe.mitre.org/index.html) will automatically appear in the reference section of its associated qhelp file.
|
||||
When you tag a query like this, the associated CWE pages from [MITRE.org](https://cwe.mitre.org/index.html) will automatically appear in the reference section of its associated qhelp file.
|
||||
|
||||
## QL area
|
||||
|
||||
@@ -165,18 +165,18 @@ The select clause of each alert query defines the alert message that is displaye
|
||||
|
||||
* Each message should be a complete, standalone sentence. That is, it should be capitalized and have proper punctuation, including a full stop.
|
||||
* The message should factually describe the problem that is being highlighted–it should not contain recommendations about how to fix the problem or value judgements.
|
||||
* Program element references should be in 'single quotes' to distinguish them from ordinary words. Quotes are not needed around substitutions ($@).
|
||||
* Program element references should be in 'single quotes' to distinguish them from ordinary words. Quotes are not needed around substitutions (`$@`).
|
||||
* Avoid constant alert message strings and include some context, if possible. For example, `The class 'Foo' is duplicated as 'Bar'.` is preferable to `This class is duplicated here.`
|
||||
* Where you reference another program element, link to it if possible using a substitution (`$@`). Links should be used inline in the sentence, rather than as parenthesised lists or appositions.
|
||||
* When a message contains multiple links, construct a sentence that has the most variable link (that is, the link with most targets) last. For further information, see [Defining the results of a query](https://help.semmle.com/QL/learn-ql/ql/writing-queries/select-statement.html).
|
||||
* When a message contains multiple links, construct a sentence that has the most variable link (that is, the link with most targets) last. For further information, see [Defining the results of a query](https://codeql.github.com/docs/writing-codeql-queries/defining-the-results-of-a-query/).
|
||||
|
||||
For examples of select clauses and alert messages, see the query source files at the following pages:
|
||||
|
||||
* [C/C++ queries](https://help.semmle.com/wiki/display/CCPPOBJ/)
|
||||
* [C# queries](https://help.semmle.com/wiki/display/CSHARP/)
|
||||
* [Go queries](https://help.semmle.com/wiki/display/GO/)
|
||||
* [Java queries](https://help.semmle.com/wiki/display/JAVA/)
|
||||
* [JavaScript queries](https://help.semmle.com/wiki/display/JS/)
|
||||
* [Python queries](https://help.semmle.com/wiki/display/PYTHON/)
|
||||
* [C/C++ queries](https://codeql.github.com/codeql-query-help/cpp/)
|
||||
* [C# queries](https://codeql.github.com/codeql-query-help/csharp/)
|
||||
* [Go queries](https://codeql.github.com/codeql-query-help/go/)
|
||||
* [Java queries](https://codeql.github.com/codeql-query-help/java/)
|
||||
* [JavaScript queries](https://codeql.github.com/codeql-query-help/javascript/)
|
||||
* [Python queries](https://codeql.github.com/codeql-query-help/python/)
|
||||
|
||||
For further information on query writing, see [CodeQL queries](https://help.semmle.com/QL/learn-ql/ql/writing-queries/writing-queries.html). For more information on learning CodeQL, see [Learning CodeQL](https://help.semmle.com/QL/learn-ql/index.html).
|
||||
For further information on query writing, see [CodeQL queries](https://codeql.github.com/docs/writing-codeql-queries/codeql-queries/). For more information on learning CodeQL, see [CodeQL documentation](https://codeql.github.com/docs/).
|
||||
|
||||
@@ -27,7 +27,7 @@ The process must begin with the first step and must conclude with the final step
|
||||
|
||||
Add one or more unit tests for the query (and for any library changes you make) to the `ql/<language>/ql/test/experimental` directory. Tests for library changes go into the `library-tests` subdirectory, and tests for queries go into `query-tests` with their relative path mirroring the query's location under `ql/<language>/ql/src/experimental`.
|
||||
|
||||
- See the section on [Testing custom queries](https://help.semmle.com/codeql/codeql-cli/procedures/test-queries.html) in the [CodeQL documentation](https://help.semmle.com/codeql/) for more information.
|
||||
- See the section on [Testing custom queries](https://help.semmle.com/codeql/codeql-cli/procedures/test-queries.html) in the [CodeQL documentation](https://codeql.github.com/docs/) for more information.
|
||||
- See [C/C++ CodeQL tests](/cpp/ql/test/README.md) for more information about contributing tests for C/C++ queries in particular.
|
||||
|
||||
4. **Test for correctness on real-world code**
|
||||
@@ -46,10 +46,10 @@ The process must begin with the first step and must conclude with the final step
|
||||
|
||||
QL performance profiling and tuning is an advanced topic, and some tasks will require assistance from GitHub employees. With that said, there are several things you can do.
|
||||
|
||||
- Understand [the evaluation model of QL](https://help.semmle.com/QL/ql-handbook/evaluation.html). It's more similar to SQL than to any mainstream programming language.
|
||||
- Understand [the evaluation model of QL](https://codeql.github.com/docs/ql-language-reference/evaluation-of-ql-programs/). It's more similar to SQL than to any mainstream programming language.
|
||||
- Most performance tuning in QL boils down to computing as few tuples (rows of data) as possible. As a mental model, think of predicate evaluation as enumerating all combinations of parameters that satisfy the predicate body. This includes the implicit parameters `this` and `result`.
|
||||
- The major libraries in CodeQL are _cached_ and will only be computed once for the entire suite of queries. The first query that needs a cached _stage_ will trigger its evaluation. This means that query authors should usually only look at the run time of the last stage of evaluation.
|
||||
- In [the settings for the VSCode extension](https://help.semmle.com/codeql/codeql-for-vscode/reference/settings.html), check the box "Running Queries: Debug" (`codeQL.runningQueries.debug`). Then find "CodeQL Query Server" in the VSCode Output panel (View -> Output) and capture the output when running the query. That output contains timing and tuple counts for all computed predicates.
|
||||
- In [the settings for the VSCode extension](https://codeql.github.com/docs/codeql-for-visual-studio-code/customizing-settings/), check the box "Running Queries: Debug" (`codeQL.runningQueries.debug`). Then find "CodeQL Query Server" in the VSCode Output panel (View -> Output) and capture the output when running the query. That output contains timing and tuple counts for all computed predicates.
|
||||
- To clear the entire cache, invoke "CodeQL: Clear Cache" from the VSCode command palette.
|
||||
|
||||
6. **Make sure your query has the correct metadata**
|
||||
|
||||
Reference in New Issue
Block a user