git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 2/2] mingw: avoid const warning

From
Karsten Blees <karsten.blees@gmail.com>
Date
Jun 6, 2014, 21:15 UTC
Message-ID
<53922F58.80509@gmail.com>
In-Reply-To
<xmqq1tv1rguh.fsf@gitster.dls.corp.google.com>
Am 06.06.2014 21:13, schrieb Junio C Hamano:
Show 50 quoted lines
> 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.
Show 7 quoted lines
> 
>> 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

Previous: Junio C HamanoNext: Stepan Kasal
Message 6 of 9 in “mingw: macro main(), const warnings”
  1. 0/2 mingw: macro main(), const warningsStepan Kasal, May 29, 2014
  2. 1/2 Win32: move main macro to a functionStepan Kasal, May 29, 2014
  3. 2/2 mingw: avoid const warningStepan Kasal, May 29, 2014
  4. Karsten BleesJun 6, 2014
  5. Junio C HamanoJun 6, 2014
  6. Karsten BleesJun 6, 2014
  7. 0/2 mingw: macro main(), const warningsStepan Kasal, Jun 7, 2014
  8. 1/2 Win32: move main macro to a functionStepan Kasal, Jun 7, 2014
  9. 2/2 mingw: avoid const warningStepan Kasal, Jun 7, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.