[llvm-commits] [llvm] r118863 - /llvm/trunk/docs/CodingStandards.html
Chris Lattner
sabre at nondot.org
Thu Nov 11 16:19:41 PST 2010
Author: lattner
Date: Thu Nov 11 18:19:41 2010
New Revision: 118863
URL: http://llvm.org/viewvc/llvm-project?rev=118863&view=rev
Log:
describe the preferred approach to silencing 'unused variable warnings' due to asserts.
Modified:
llvm/trunk/docs/CodingStandards.html
Modified: llvm/trunk/docs/CodingStandards.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CodingStandards.html?rev=118863&r1=118862&r2=118863&view=diff
==============================================================================
--- llvm/trunk/docs/CodingStandards.html (original)
+++ llvm/trunk/docs/CodingStandards.html Thu Nov 11 18:19:41 2010
@@ -851,6 +851,38 @@
</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 > 42 && "Vector smaller than it should be");
+
+ bool NewToSet = Myset.insert(Value);
+ assert(NewToSet && "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() > 42 && "Vector smaller than it should be");
+
+ bool NewToSet = Myset.insert(Value); (void)NewToSet;
+ assert(NewToSet && "The value shouldn't be in the set yet");
+</pre>
+</div>
+
+
</div>
<!-- _______________________________________________________________________ -->
More information about the llvm-commits
mailing list