Skip to content

Commit 0cc0977

Browse files
author
Robert Marsh
committed
C++/Docs: more examples and rewording for guards
1 parent 9aea2ed commit 0cc0977

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Using the guards library in C and C++
33

44
Overview
55
--------
6-
The guards library (defined in ``semmle.code.cpp.controlflow.Guards``) provides a class ``GuardCondition`` representing Boolean values that are used to make control flow decisions.
6+
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.
77
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.
88

9-
.. code:: cpp
9+
.. code-block:: cpp
1010
1111
if(x < 10) {
1212
f(x);
@@ -24,7 +24,7 @@ The ``controls`` predicate helps determine which blocks are only run when the ``
2424

2525
In the following code sample, the call to ``isValid`` controls the calls to ``performAction`` and ``logFailure`` but not the return statement.
2626

27-
.. code:: cpp
27+
.. code-block:: cpp
2828
2929
if(isValid(accessToken)) {
3030
performAction();
@@ -35,22 +35,49 @@ In the following code sample, the call to ``isValid`` controls the calls to ``pe
3535
}
3636
return succeeded;
3737
38+
In the following code sample, the call to `isValid` controls the body of the if and also the code after the if.
39+
40+
.. code-block:: cpp
41+
42+
if(!isValid(accessToken)) {
43+
logFailure();
44+
return 0;
45+
}
46+
performAction();
47+
return succeeded;
48+
3849
The ``ensuresEq`` and ``ensuresLt`` predicates
3950
----------------------------------------------
4051
The ``ensuresEq`` and ``ensuresLt`` predicates are the main way of determining what, if any, guarantees the ``GuardCondition`` provides for a given basic block.
4152

53+
The ``ensuresEq`` predicate
54+
***************************
4255
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.
4356

57+
The ``ensuresLt`` predicate
58+
***************************
4459
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.
4560

46-
.. TODO: examples for these predicates (none for others?)
61+
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.
62+
63+
.. code-block:: cpp
4764
65+
if(index < size) {
66+
ret = array[index];
67+
} else {
68+
ret = nullptr
69+
}
70+
return ret;
4871
4972
The ``comparesEq`` and ``comparesLt`` predicates
5073
------------------------------------------------
5174
The ``comparesEq`` and ``comparesLt`` predicates help determine if the ``GuardCondition`` evaluates to true.
5275

76+
The ``comparesEq`` predicate
77+
****************************
5378
``comparesEq(left, right, k, true, testIsTrue)`` holds if ``left`` equals ``right + k`` when the expression evaluates to ``testIsTrue``.
5479

80+
The ``comparesLt`` predicate
81+
****************************
5582
``comparesLt(left, right, k, isLessThan, testIsTrue)`` holds if ``left < right + k`` evaluates to ``isLessThan`` when the expression evaluates to ``testIsTrue``.
5683

0 commit comments

Comments
 (0)