summaryrefslogtreecommitdiffstats
path: root/llvm/docs/CodingStandards.html
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2010-11-12 00:19:41 +0000
committerChris Lattner <sabre@nondot.org>2010-11-12 00:19:41 +0000
commit8a7f4dafe596c3489e883e8ef61b6758be84b6e8 (patch)
treeaac9cd136b32137e8006d58b504f6802fdd59a8e /llvm/docs/CodingStandards.html
parent7fe1100c26f399b1fb20f415f19128dbe7c7bbc9 (diff)
downloadbcm5719-llvm-8a7f4dafe596c3489e883e8ef61b6758be84b6e8.tar.gz
bcm5719-llvm-8a7f4dafe596c3489e883e8ef61b6758be84b6e8.zip
describe the preferred approach to silencing 'unused variable warnings' due to asserts.
llvm-svn: 118863
Diffstat (limited to 'llvm/docs/CodingStandards.html')
-rw-r--r--llvm/docs/CodingStandards.html32
1 files changed, 32 insertions, 0 deletions
diff --git a/llvm/docs/CodingStandards.html b/llvm/docs/CodingStandards.html
index e3ec3ff136c..416c29b7bed 100644
--- a/llvm/docs/CodingStandards.html
+++ b/llvm/docs/CodingStandards.html
@@ -851,6 +851,38 @@ return 0;
</pre>
</div>
+<p>Another issue is that values used only by assertions will produce an "unused
+ value" warning when assertions are disabled. For example, this code will warn:
+</p>
+
+<div class="doc_code">
+<pre>
+ unsigned Size = V.size();
+ assert(Size &gt; 42 &amp;&amp; "Vector smaller than it should be");
+
+ bool NewToSet = Myset.insert(Value);
+ assert(NewToSet &amp;&amp; "The value shouldn't be in the set yet");
+</pre>
+</div>
+
+<p>These are two interesting different cases: in the first case, the call to
+V.size() is only useful for the assert, and we don't want it executed when
+assertions are disabled. Code like this should move the call into the assert
+itself. In the second case, the side effects of the call must happen whether
+the assert is enabled or not. In this case, the value should be cast to void
+to disable the warning. To be specific, it is preferred to write the code
+like this:</p>
+
+<div class="doc_code">
+<pre>
+ assert(V.size() &gt; 42 &amp;&amp; "Vector smaller than it should be");
+
+ bool NewToSet = Myset.insert(Value); (void)NewToSet;
+ assert(NewToSet &amp;&amp; "The value shouldn't be in the set yet");
+</pre>
+</div>
+
+
</div>
<!-- _______________________________________________________________________ -->
OpenPOWER on IntegriCloud