Skip to content

Commit 219fcb7

Browse files
author
Robert Marsh
authored
Merge pull request #2160 from jf205/review-cpp-docs
docs: editorial suggestions to new C/C++ topics
2 parents 9f0499c + ec15add commit 219fcb7

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

docs/language/learn-ql/cpp/guards.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Using the guards library in C and C++
33

44
Overview
55
--------
6+
67
The guards library (defined in ``semmle.code.cpp.controlflow.Guards``) provides a class `GuardCondition <https://help.semmle.com/qldoc/cpp/semmle/code/cpp/controlflow/Guards.qll/type.Guards$GuardCondition.html>`__ representing Boolean values that are used to make control flow decisions.
78
A ``GuardCondition`` is considered to guard a basic block if the block can only be reached if the ``GuardCondition`` is evaluated a certain way. For instance, in the following code, ``x < 10`` is a ``GuardCondition``, and it guards all the code before the return statement.
89

@@ -20,6 +21,7 @@ A ``GuardCondition`` is considered to guard a basic block if the block can only
2021
2122
The ``controls`` predicate
2223
------------------------------------------------
24+
2325
The ``controls`` predicate helps determine which blocks are only run when the ``GuardCondition`` evaluates a certain way. ``guard.controls(block, testIsTrue)`` holds if ``block`` is only entered if the value of this condition is ``testIsTrue``.
2426

2527
In the following code sample, the call to ``isValid`` controls the calls to ``performAction`` and ``logFailure`` but not the return statement.
@@ -35,7 +37,8 @@ In the following code sample, the call to ``isValid`` controls the calls to ``pe
3537
}
3638
return succeeded;
3739
38-
In the following code sample, the call to `isValid` controls the body of the if and also the code after the if.
40+
In the following code sample, the call to ``isValid`` controls the body of the
41+
``if`` statement, and also the code after the ``if``.
3942

4043
.. code-block:: cpp
4144
@@ -48,14 +51,18 @@ In the following code sample, the call to `isValid` controls the body of the if
4851
4952
The ``ensuresEq`` and ``ensuresLt`` predicates
5053
----------------------------------------------
54+
5155
The ``ensuresEq`` and ``ensuresLt`` predicates are the main way of determining what, if any, guarantees the ``GuardCondition`` provides for a given basic block.
5256

5357
The ``ensuresEq`` predicate
5458
***************************
59+
60+
5561
When ``ensuresEq(left, right, k, block, true)`` holds, then ``block`` is only executed if ``left`` was equal to ``right + k`` at their last evaluation. When ``ensuresEq(left, right, k, block, false)`` holds, then ``block`` is only executed if ``left`` was not equal to ``right + k`` at their last evaluation.
5662

5763
The ``ensuresLt`` predicate
5864
***************************
65+
5966
When ``ensuresLt(left, right, k, block, true)`` holds, then ``block`` is only executed if ``left`` was strictly less than ``right + k`` at their last evaluation. When ``ensuresLt(left, right, k, block, false)`` holds, then ``block`` is only executed if ``left`` was greater than or equal to ``right + k`` at their last evaluation.
6067

6168
In the following code sample, the comparison on the first line ensures that ``index`` is less than ``size`` in the "then" block, and that ``index`` is greater than or equal to ``size`` in the "else" block.
@@ -71,13 +78,16 @@ In the following code sample, the comparison on the first line ensures that ``in
7178
7279
The ``comparesEq`` and ``comparesLt`` predicates
7380
------------------------------------------------
81+
7482
The ``comparesEq`` and ``comparesLt`` predicates help determine if the ``GuardCondition`` evaluates to true.
7583

7684
The ``comparesEq`` predicate
7785
****************************
86+
7887
``comparesEq(left, right, k, true, testIsTrue)`` holds if ``left`` equals ``right + k`` when the expression evaluates to ``testIsTrue``.
7988

8089
The ``comparesLt`` predicate
8190
****************************
91+
8292
``comparesLt(left, right, k, isLessThan, testIsTrue)`` holds if ``left < right + k`` evaluates to ``isLessThan`` when the expression evaluates to ``testIsTrue``.
8393

docs/language/learn-ql/cpp/range-analysis.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ Using range analysis for C and C++
33

44
Overview
55
--------
6+
67
Range analysis determines upper and lower bounds for an expression.
78

89
The range analysis library (defined in ``semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis``) provides a set of predicates for determining constant upper and lower bounds on expressions, as well as recognizing integer overflows. For performance, the library performs automatic widening and therefore may not provide the tightest possible bounds.
910

1011
Bounds predicates
1112
-----------------
13+
1214
The ``upperBound`` and ``lowerBound`` predicates provide constant bounds on expressions. No conversions of the argument are included in the bound. In the common case that your query needs to take conversions into account, call them on the converted form, such as ``upperBound(expr.getFullyConverted())``.
1315

1416
Overflow predicates
1517
-------------------
18+
1619
``exprMightOverflow`` and related predicates hold if the relevant expression might overflow, as determined by the range analysis library. The ``convertedExprMightOverflow`` family of predicates will take conversions into account.
1720

1821
Example
1922
-------
23+
2024
This query uses ``upperBound`` to determine whether the result of ``snprintf`` is checked when used in a loop.
2125

2226
.. code-block:: ql

docs/language/learn-ql/cpp/value-numbering-hash-cons.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Hash consing and value numbering
22
=================================================
3+
34
Overview
45
--------
6+
57
In C and C++ QL databases, each node in the abstract syntax tree is represented by a separate object. This allows both analysis and results display to refer to specific appearances of a piece of syntax. However, it is frequently useful to determine whether two expressions are equivalent, either syntactically or semantically.
68

79
The `hash consing <https://en.wikipedia.org/wiki/Hash_consing>`__ library (defined in ``semmle.code.cpp.valuenumbering.HashCons``) provides a mechanism for identifying expressions that have the same syntactic structure. The `global value numbering <https://en.wikipedia.org/wiki/Value_numbering>`__ library (defined in ``semmle.code.cpp.valuenumbering.GlobalValueNumbering``) provides a mechanism for identifying expressions that compute the same value at runtime.
@@ -41,10 +43,12 @@ However, in the next example, the uses of ``x + y`` will have different value nu
4143
4244
Value numbering
4345
---------------
46+
4447
The value numbering library (defined in ``semmle.code.cpp.valuenumbering.GlobalValueNumbering``) provides a mechanism for identifying expressions that compute the same value at runtime. Value numbering is useful when your primary concern is with the values being produced or the eventual machine code being run. For instance, value numbering might be used to determine whether a check is being done against the same value as the operation it is guarding.
4548

4649
The value numbering API
4750
~~~~~~~~~~~~~~~~~~~~~~~
51+
4852
The value numbering library exposes its interface primarily through the ``GVN`` class. Each instance of ``GVN`` represents a set of expressions that will always evaluate to the same value. To get an expression in the set represented by a particular ``GVN``, use the ``getAnExpr()`` member predicate.
4953

5054
To get the ``GVN`` of an ``Expr``, use the ``globalValueNumber`` predicate.
@@ -55,10 +59,11 @@ To get the ``GVN`` of an ``Expr``, use the ``globalValueNumber`` predicate.
5559

5660
Why not a predicate?
5761
~~~~~~~~~~~~~~~~~~~~
62+
5863
The obvious interface for this library would be a predicate ``equivalent(Expr e1, Expr e2)``. However, this predicate would be very large, with a quadratic number of rows for each set of equivalent expressions. By using a class as an intermediate step, the number of rows can be kept linear, and therefore can be cached.
5964

60-
Example Queries
61-
~~~~~~~~~~~~~~~
65+
Example query
66+
~~~~~~~~~~~~~
6267

6368
This query uses the ``GVN`` class to identify calls to ``strncpy`` where the size argument is derived from the source rather than the destination
6469

@@ -76,10 +81,12 @@ This query uses the ``GVN`` class to identify calls to ``strncpy`` where the siz
7681
7782
Hash consing
7883
------------
84+
7985
The hash consing library (defined in ``semmle.code.cpp.valuenumbering.HashCons``) provides a mechanism for identifying expressions that have the same syntactic structure. Hash consing is useful when your primary concern is with the text of the code. For instance, hash consing might be used to detect duplicate code within a function.
8086

8187
The hash consing API
8288
~~~~~~~~~~~~~~~~~~~~
89+
8390
The hash consing library exposes its interface primarily through the ``HashCons`` class. Each instance of ``HashCons`` represents a set of expressions within one function that have the same syntax (including referring to the same variables). To get an expression in the set represented by a particular ``HashCons``, use the ``getAnExpr()`` member predicate.
8491

8592
.. note::
@@ -88,8 +95,8 @@ The hash consing library exposes its interface primarily through the ``HashCons`
8895

8996
To get the ``HashCons`` of an ``Expr``, use the ``hashCons`` predicate.
9097

91-
Examples
92-
~~~~~~~~
98+
Example query
99+
~~~~~~~~~~~~~
93100

94101
.. TODO: prose explanations
95102
@@ -104,3 +111,4 @@ Examples
104111
hashCons(outer.getCondition()) = hashCons(inner.getCondition())
105112
select inner.getCondition(), "The condition of this if statement duplicates the condition of $@",
106113
outer.getCondition(), "an enclosing if statement"
114+

0 commit comments

Comments
 (0)