<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tewha.net</title>
    <link>https://tewha.net/tags/building/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 21 Feb 2013 16:00:00 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/building/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Against GCC_WARN_SHADOW</title>
      <link>https://tewha.net/2013/02/against-gcc_warn_shadow/</link>
      <pubDate>Thu, 21 Feb 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/02/against-gcc_warn_shadow/</guid>
      <description>&lt;p&gt;Over the years I have tweaked which compiler warnings I use. There’s one in particular that I used to turn on but will turn off from now on: &lt;code&gt;GCC_WARN_SHADOW&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;GCC_WARN_SHADOW&lt;/code&gt; is essentially drawing your attention to you &lt;em&gt;possibly&lt;/em&gt; doing something other than you intended. This is like most warnings, but the difference is that the behaviour &lt;code&gt;GCC_WARN_SHADOW&lt;/code&gt; is blocking is very useful.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Over the years I have tweaked which compiler warnings I use. There’s one in particular that I used to turn on but will turn off from now on: <code>GCC_WARN_SHADOW</code>.</p>
<p><code>GCC_WARN_SHADOW</code> is essentially drawing your attention to you <em>possibly</em> doing something other than you intended. This is like most warnings, but the difference is that the behaviour <code>GCC_WARN_SHADOW</code> is blocking is very useful.</p>
<p>This is the sort of things the more paranoid compiler warnings are good at. The downside is that it’s difficult (not impossible) to drop a particular warning to indicate that you know what you’re doing.</p>
<p>With <code>GCC_WARN_SHADOW</code> on, this code will generate a warning:</p>
<pre><code>int a = 5;
int b = 3;
int c = 4;
int value = MAX(a,MAX(b,c));
</code></pre>
<p>Despite that, it will function perfectly. The reason for the warning is that it compiles down to this:</p>
<pre><code>({
   int __a = (a);
   int __b = (({
       int __a = (b);
       int __b = (c);
       __a &lt; __b ? __b : __a;
   }));
   __a &lt; __b ? __b : __a;
})
</code></pre>
<p>This is horribly ugly code. You wouldn’t want to write this code. You wouldn’t want to read it. But here’s the catch: it’s perfectly valid to the compiler, and hiding the ugliness <em>is why <code>MAX()</code> exists</em>.</p>
<p>There’s a few ways to avoid this. The first is to not use the <code>MAX()</code> macro at all:</p>
<pre><code>int a = 5;
int b = 3;
int c = 4;
int value = a &lt; b ? (b &lt; c ? c : b) : a;
</code></pre>
<p>Or, you could do this:</p>
<pre><code>int a = 5;
int b = 3;
int c = 4;
int temp = MAX(b,c);
int value = MAX(a,temp);
</code></pre>
<p>If you’re interested in <code>temp</code> — if it’s a value that has meaning — this is a fine approach. Give it a better name than <code>temp</code> and run with it. But if it’s a value that has no meaning other than an uninteresting temporary variable on your way to <code>value</code>, you’ve made your code worse.</p>
<p>Next up, you could turn off the warning:</p>
<pre><code>int a = 5;
int b = 3;
int c = 4;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored &quot;-Wshadow&quot;
int value = MAX(a,MAX(b,c));
#pragma GCC diagnostic pop
</code></pre>
<p>This last method can be simplified (a little):</p>
<pre><code>#define PUSH_NOSHADOW _Pragma(&quot;GCC diagnostic push&quot;) _Pragma(&quot;GCC diagnostic ignored &quot;-Wshadow&quot;&quot;)
#define POP_NOSHADOW _Pragma(&quot;GCC diagnostic pop&quot;)
int a = 5;
int b = 3;
int c = 4;
PUSH_NOSHADOW
int value = MAX(a,MAX(b,c));
POP_NOSHADOW
</code></pre>
<p>This last technique suggests permanent fix: Apple could change the definition of MAX to avoid this (10296999).</p>
<p>But really? Just stop using <code>GCC_WARN_SHADOW</code>. It’s an unnecessary pain in the ass.</p>
<p>These are artificial examples, granted. But let’s pull this back to Objective-C, and something a typical Cocoa or Cocoa Touch programmer might have to do:</p>
<p>Also, if you use this you’ll need to name error variables in blocks differently:</p>
<pre><code>NSError *e;
[self somethingWithError: &amp;e];
[self runWithCompletion:^{
    NSError *e;
    [self recoverFromError:&amp;e]; // warning, or rename inner error to e2
}];
</code></pre>
<p>Overall, I didn’t think it was worth the extra effort anymore. Your mileage will (of course) vary.</p>]]></content:encoded>
    </item>
    <item>
      <title>Better Xcode warnings through .xcconfig files</title>
      <link>https://tewha.net/2010/11/better-xcode-warnings-through-xcconfig-files/</link>
      <pubDate>Sat, 27 Nov 2010 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2010/11/better-xcode-warnings-through-xcconfig-files/</guid>
      <description>&lt;p&gt;Peter Hosey &lt;a href=&#34;http://boredzo.org/blog/archives/2009-11-07/warnings&#34;&gt;posted a list of warnings he turns on&lt;/a&gt;. Here’s the warnings I turn on. It’s mostly the same list.&lt;/p&gt;
&lt;p&gt;Rather than set these per project, I have a .xcconfig file I add to my project. I then base each build configuration off this file. Changing the .xcconfig file changes all projects based on it (though with the current Xcode, it’s sometimes necessary to reload the project to get the settings to take).&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>Peter Hosey <a href="http://boredzo.org/blog/archives/2009-11-07/warnings">posted a list of warnings he turns on</a>. Here’s the warnings I turn on. It’s mostly the same list.</p>
<p>Rather than set these per project, I have a .xcconfig file I add to my project. I then base each build configuration off this file. Changing the .xcconfig file changes all projects based on it (though with the current Xcode, it’s sometimes necessary to reload the project to get the settings to take).</p>
<p>//
//  MoreWarnings.xcconfig
//
//  Created by Steven Fisher:
//      <a href="http://tewha.net/2010/11/xcode-warnings/">http://tewha.net/2010/11/xcode-warnings/</a>
//  See also:
//      <a href="http://boredzo.org/blog/archives/2009-11-07/warnings">http://boredzo.org/blog/archives/2009-11-07/warnings</a>
//</p>
<pre><code>GCC_WARN_CHECK_SWITCH_STATEMENTS = YES
GCC_WARN_SHADOW = YES
GCC_WARN_64_TO_32_BIT_CONVERSION = YES
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES
GCC_WARN_ABOUT_RETURN_TYPE = YES
GCC_WARN_MISSING_PARENTHESES = YES
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES
GCC_WARN_ABOUT_MISSING_NEWLINE = YES
GCC_WARN_SIGN_COMPARE = YES
GCC_WARN_UNDECLARED_SELECTOR = YES
GCC_WARN_UNUSED_FUNCTION = YES
GCC_WARN_UNUSED_LABEL = YES
GCC_WARN_UNUSED_VALUE = YES
GCC_WARN_UNUSED_VARIABLE = YES
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES
GCC_WARN_TYPECHECK_CALLS_TO_PRINTF = YES
GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES
GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES
GCC_WARN_ABOUT_INVALID_OFFSETOF_MACRO = YES
GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES
GCC_TREAT_WARNINGS_AS_ERRORS = YES
RUN_CLANG_STATIC_ANALYZER = YES
</code></pre>
<p>Peter wrote a great explanation of why you’d want most of these warnings, which I’m not going to attempt. I’ve added some C++ warnings, too. They don’t do anything with clang 1.6, but might be useful in the future.</p>
<p>A few notes:</p>
<ul>
<li>Like Peter, I don’t turn on <code>GCC_WARN_UNUSED_PARAMETER</code>. Although you can add <code>__unused</code> to each parameter to disable the warning, doing so breaks Xcode’s code formatting. I use code formatting constantly, so anything that breaks it isn’t acceptable.</li>
<li>The warnings that were the most painful to turn on were <code>GCC_WARN_64_TO_32_BIT_CONVERSION</code> and <code>GCC_WARN_SIGN_COMPARE</code>. If you use <code>int</code> and <code>NSInteger</code> interchangeably, you’re going to have a tough time with these. (I don’t, but some of the internal libraries I use do.) These are also some of the more dangerous warnings to fix; if you’ve got automated tests, you should run them after every few corrections.</li>
<li><code>RUN_CLANG_STATIC_ANALYZER</code> will roughly double (or more) the time your project takes to compile. I consider this a good trade: I rarely trigger a static analyzer warning now, but when I do I want to fix it.</li>
<li>Also note the <code>GCC_TREAT_WARNINGS_AS_ERRORS</code>. If you are applying these to an existing project, you may start with hundreds of warnings. You’ll probably want to start with this set to <code>NO</code> and change it to <code>YES</code> after you’ve fixed the warnings.</li>
<li>Remember, too, that this changes the <strong>defaults</strong> for settings. You can still specify different settings in the build configuration! If the static analyzer is taking too long to run in one project, just turn it off in that one project but leave the .xcconfig file alone!</li>
</ul>]]></content:encoded>
    </item>
  </channel>
</rss>
