Skip to content

Commit 62c73a5

Browse files
author
Robert Marsh
committed
C++/Docs: more work on guards.rst
Added some examples and reworded portions of guards.rst. There's still more to do - examples for ensures and compares predicates, and possibly rewording the description of the compares predicates
1 parent 500a81a commit 62c73a5

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ 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`` representing Boolean values that are used to make control flow decisions. A ``GuardCondition`` is considered to guard a basic block if the block is only reached if the ``GuardCondition`` 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.
7+
8+
.. code:: cpp
9+
10+
if(x < 10) {
11+
f(x);
12+
} else if (x < 20) {
13+
g(x);
14+
} else {
15+
h(x);
16+
}
17+
return 0;
718
819
The ``ensuresEq`` and ``ensuresLt`` predicates
920
----------------------------------------------
1021
The ``ensuresEq`` and ``ensuresLt`` predicates are the main way of determining what, if any, guarantees the ``GuardCondition`` provides for a given basic block.
1122

12-
``ensuresEq(left, right, k, block, areEqual)`` holds if ``left == right + k`` must be ``areEqual`` for ``block`` to be executed. If ``areEqual = false`` then this implies ``left != right + k`` must be true for ``block`` to be executed.
23+
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.
1324

14-
``ensuresLt(left, right, k, block, isLessThan)`` holds if ``left < right + k`` must be ``isLessThan`` for ``block`` to be executed. If ``isLessThan = false`` then this implies ``left >= right + k`` must be true for ``block`` to be executed.
25+
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.
1526

1627
.. TODO: examples for these predicates (none for others?)
1728
@@ -20,10 +31,23 @@ The ``comparesEq`` and ``comparesLt`` predicates
2031
------------------------------------------------
2132
The ``comparesEq`` and ``comparesLt`` predicates help determine if the ``GuardCondition`` evaluates to true.
2233

23-
``comparesEq(left, right, k, areEqual, testIsTrue)`` holds if ``left == right + k`` evaluates to ``areEqual`` if the expression evaluates to ``testIsTrue``.
34+
``comparesEq(left, right, k, true, testIsTrue)`` holds if ``left`` equals ``right + k`` when the expression evaluates to ``testIsTrue``.
2435

25-
``comparesLt(left, right, k, isLessThan, testIsTrue)`` holds if ``left < right + k`` evaluates to ``isLessThan`` if the expression evaluates to ``testIsTrue``.
36+
``comparesLt(left, right, k, isLessThan, testIsTrue)`` holds if ``left < right + k`` evaluates to ``isLessThan`` when the expression evaluates to ``testIsTrue``.
2637

2738
The ``controls`` predicate
2839
------------------------------------------------
29-
The ``controls`` predicate helps determine which blocks are only run when the ``IRGuardCondition`` evaluates a certain way. ``controls(block, testIsTrue)`` holds if ``block`` is only entered if the value of this condition is ``testIsTrue``.
40+
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``.
41+
42+
In the following code sample, the call to ``isValid`` controls the calls to ``performAction`` and ``logFailure`` but not the return statement.
43+
44+
.. code:: cpp
45+
46+
if(isValid(accessToken)) {
47+
performAction();
48+
succeeded = 1;
49+
} else {
50+
logFailure();
51+
succeeded = 0;
52+
}
53+
return succeeded;

0 commit comments

Comments
 (0)