From:  David Brown <david.brown@hesbynett.no>
Date:  23 Sep 2024 17:14:07 Hong Kong Time
Newsgroup:  news.alt119.net/comp.lang.c
Subject:  

Re: how to make a macro work as a single line if stmt without braces

NNTP-Posting-Host:  null

On 23/09/2024 09:06, Richard Harnden wrote:
> On 23/09/2024 07:16, David Brown wrote:
>> On 22/09/2024 22:39, Keith Thompson wrote:
>>> Bart  writes:
>>>> On 22/09/2024 16:11, Kaz Kylheku wrote:
>>> [...]
>>>>> Also GCC has been able to diagnose misleading indentation for some
>>>>> years now.
>>>>
>>>> How many years was that out of the last 52? How exactly do you turn it
>>>> on? Since -Wall -Wpedantic -Wextra doesn't report it.
>>>
>>> The -Wmisleading-indentation option was added to gcc on 2015-05-12,
>>> and incorporated into -Wall 2015-12-10.  gcc 6.1.0 has the option
>>> and includes it in -Wall; gcc 5.3.0 does not.  (Are you using a gcc
>>> release that old?)  It uses the -ftabstop= option (defaulting to 8)
>>> to determine whether indentation lines up or not.
>>>
>>> Inconsistent tabstops and mixing of spaces and tabs can certainly
>>> cause problems.
>>>
>>
>> That would be detected quite easily if the default for -ftabstop were, 
>> say, 27.  Then the chance of accidentally matching indents with tabs 
>> and spaces would be negligible.
>>
> 
> Isn't that the opposite of what you want, though?
> 

What I would be looking for in this case is a warning if I had used tabs 
and spaces (for indents) at different places in the code.  If the tab 
setting is a sensible choice - typically 4 or 8 spaces worth - then you 
can easily have code that looks right (in the sense of having visual 
indents that match the real block structure) with one setting and looks 
wrong with a different setting.

For example, you might write this code :

if (test)
foo();
<  ><  >bar();

You've made an error here - you had intended to have both calls within 
the conditional.  With 4 spaces per tab when you made the error, this 
could be spotted by tools using tabstop settings of 4, or by a code 
reviewer with those settings.

However, to tools or code reviewers with tabstop settings of 8, the code 
would appear as:

if (test)
foo();
<  ><  >bar();

Now the indentation matches the syntactical structure, and the mistake 
is missed.

With a tabstop of 27, the "if" and "bar()" lines do not match up, and 
the mistake can be flagged.

Of course a tabstop of 27 is not necessary - anything other than a sane 
value of 4 or 8 would catch almost anything.  But having a bit of extra 
distance also catches cases of tab then space typos.

And there could also be a warning that checked directly for mixes of 
tabs and spaces in indents.  But such mixes can happen when moving or 
copying code between files, and there are some people who like to mix 
8-space tabs with 4 explicit spaces in their code.