git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Karsten Blees <karsten.blees@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Stepan Kasal <kasal@ucw.cz>,
	GIT Mailing-list <git@vger.kernel.org>,
	Erik Faye-Lund <kusmabite@gmail.com>,
	msysgit@googlegroups.com
Subject: Re: [PATCH 2/2] mingw: avoid const warning
Date: Fri, 06 Jun 2014 23:15:04 +0200	[thread overview]
Message-ID: <53922F58.80509@gmail.com> (raw)
In-Reply-To: <xmqq1tv1rguh.fsf@gitster.dls.corp.google.com>

Am 06.06.2014 21:13, schrieb Junio C Hamano:
> Karsten Blees <karsten.blees@gmail.com> writes:
> 
>> Am 29.05.2014 12:47, schrieb Stepan Kasal:
>>> Fix const warnings in http-fetch.c and remote-curl.c main() where is
>>> argv declared as const.
>>>
>>> The fix should work for all future declarations of main, no matter
>>> whether the second parameter's type is "char**", "const char**", or
>>> "char *[]".
>>
>> I'm 100% in favor of a solution that doesn't restrict main to non-const char**! Thanks.
>>
>>> Signed-off-by: Stepan Kasal <kasal@ucw.cz>
>>> ---
>>>  compat/mingw.h | 5 +++--
>>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/compat/mingw.h b/compat/mingw.h
>>> index 15f0c9d..8745d19 100644
>>> --- a/compat/mingw.h
>>> +++ b/compat/mingw.h
>>> @@ -369,10 +369,11 @@ extern CRITICAL_SECTION pinfo_cs;
>>>  void mingw_startup();
>>>  #define main(c,v) dummy_decl_mingw_main(); \
>>>  static int mingw_main(c,v); \
>>> -int main(int argc, char **argv) \
>>> +int main(c, char **main_argv_not_used) \
>>>  { \
>>> +	typedef v, **argv_type; \
>>>  	mingw_startup(); \
>>> -	return mingw_main(__argc, __argv); \
>>> +	return mingw_main(__argc, (argv_type)__argv); \
>>>  } \
>>>  static int mingw_main(c,v)
>>>  
>>
>> I have to admit I had trouble understanding what 'typedef v,
>> **arv_type;' does (looks invalid at first glance), and why you
>> would need main_argv_not_used instead of just main(c,v).
>>
>> So, I'd like to award +10 points for cleverness, but -10 for
>> obscurity ;-) Probably deserves a comment or an explanation in the
>> commit message.
> 
> Agreed.  The "typedef" one is a cute hack.
> 
> I am wondering why the solution is not a more obvious "drop const
> that is not ANSI C", though.  I only have a ready-access to N1570
> draft but in it I find:
> 

Actually, that was the original solution ($gmane/247535). I just complained because it was slightly different from what we had in msysgit for quite some time [1], causing merge conflicts.

I guess compilers probably won't complain if you declare argv const, even if the standard is more strict. After all, you aren't supposed to modify argv.

> 
>> A simpler solution that works with all definitions of main() is to
>> cast to void* (tell the compiler all responsibility is on
>> us).
> 
> Can you cast away the constness that way, though?
> 

Not 'away'. This passes a non-const value to a const parameter, which is typically not a problem. Its just 'char**' to 'const char**' that produces the warning, because 'const char' hides behind a non-const pointer, see [2]. 'void*' to 'const char**' works, though.

[1] https://github.com/msysgit/git/commit/6949537a
[2] http://c-faq.com/ansi/constmismatch.html

  reply	other threads:[~2014-06-06 21:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-29 10:43 [PATCH 0/2] mingw: macro main(), const warnings Stepan Kasal
2014-05-29 10:45 ` [PATCH 1/2] Win32: move main macro to a function Stepan Kasal
2014-05-29 10:47 ` [PATCH 2/2] mingw: avoid const warning Stepan Kasal
2014-06-06 15:08   ` Karsten Blees
2014-06-06 19:13     ` Junio C Hamano
2014-06-06 21:15       ` Karsten Blees [this message]
2014-06-07  6:46         ` [PATCH v2 0/2] mingw: macro main(), const warnings Stepan Kasal
2014-06-07  6:46           ` [PATCH v2 1/2] Win32: move main macro to a function Stepan Kasal
2014-06-07  6:46           ` [PATCH v2 2/2] mingw: avoid const warning Stepan Kasal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=53922F58.80509@gmail.com \
    --to=karsten.blees@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=kasal@ucw.cz \
    --cc=kusmabite@gmail.com \
    --cc=msysgit@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).