public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
@ 2015-08-19 20:27 Ville Voutilainen
  2015-08-19 20:28 ` Paolo Carlini
  0 siblings, 1 reply; 16+ messages in thread
From: Ville Voutilainen @ 2015-08-19 20:27 UTC (permalink / raw)
  To: gcc-patches, Paolo Carlini, Jason Merrill

>submitter noticed that, in violation of [basic.start.main], we don't reject
>as ill-formed the declaration of a 'main' variable in the global namespace.
>Not a big deal IMHO, but the below simple check works well for me on x86_64-linux.

Just fyi, gcc accepts

decltype(main) x;

decltype(sizeof(decltype(main)*)) x;

which are "uses" of main and also violate [basic.start.main]/3.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 20:27 [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function") Ville Voutilainen
@ 2015-08-19 20:28 ` Paolo Carlini
  2015-08-19 20:37   ` Ville Voutilainen
  0 siblings, 1 reply; 16+ messages in thread
From: Paolo Carlini @ 2015-08-19 20:28 UTC (permalink / raw)
  To: Ville Voutilainen, gcc-patches, Jason Merrill

Hi Ville,

On 08/19/2015 10:12 PM, Ville Voutilainen wrote:
>> submitter noticed that, in violation of [basic.start.main], we don't reject
>> as ill-formed the declaration of a 'main' variable in the global namespace.
>> Not a big deal IMHO, but the below simple check works well for me on x86_64-linux.
> Just fyi, gcc accepts
>
> decltype(main) x;
>
> decltype(sizeof(decltype(main)*)) x;
>
> which are "uses" of main and also violate [basic.start.main]/3.
"good" to know. In my experience sometimes the front end appears to 
so-to-speak pre-declare entities. For instance I filed a while ago 
c++/48396. Not sure if in practice the exact same code is involved...

Paolo.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 20:28 ` Paolo Carlini
@ 2015-08-19 20:37   ` Ville Voutilainen
  2015-08-19 20:42     ` Paolo Carlini
  0 siblings, 1 reply; 16+ messages in thread
From: Ville Voutilainen @ 2015-08-19 20:37 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: gcc-patches, Jason Merrill

On 19 August 2015 at 23:26, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Hi Ville,
>
>
> On 08/19/2015 10:12 PM, Ville Voutilainen wrote:
>>>
>>> submitter noticed that, in violation of [basic.start.main], we don't
>>> reject
>>> as ill-formed the declaration of a 'main' variable in the global
>>> namespace.
>>> Not a big deal IMHO, but the below simple check works well for me on
>>> x86_64-linux.
>>
>> Just fyi, gcc accepts
>>
>> decltype(main) x;
>>
>> decltype(sizeof(decltype(main)*)) x;
>>
>> which are "uses" of main and also violate [basic.start.main]/3.
>
> "good" to know. In my experience sometimes the front end appears to
> so-to-speak pre-declare entities. For instance I filed a while ago
> c++/48396. Not sure if in practice the exact same code is involved...

Let me clarify: this is not about that. It's code like

int main() {}

decltype(main) x;

whereas just having

decltype(main) x;

as the whole program will diagnose the use of an undeclared
identifier. Nevertheless,
no use, not just odr-use, but use of main as in the entry point
pseudo-function is allowed
by the standard, but gcc allows some of them. gcc rejects attempts to
call main even in
such decltype contexts, but it can be fooled to allow other uses of main.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 20:37   ` Ville Voutilainen
@ 2015-08-19 20:42     ` Paolo Carlini
  2015-08-19 21:46       ` Ville Voutilainen
  0 siblings, 1 reply; 16+ messages in thread
From: Paolo Carlini @ 2015-08-19 20:42 UTC (permalink / raw)
  To: Ville Voutilainen; +Cc: gcc-patches, Jason Merrill

Hi,

On 08/19/2015 10:33 PM, Ville Voutilainen wrote:
> On 19 August 2015 at 23:26, Paolo Carlini <paolo.carlini@oracle.com> wrote:
>> Hi Ville,
>>
>>
>> On 08/19/2015 10:12 PM, Ville Voutilainen wrote:
>>>> submitter noticed that, in violation of [basic.start.main], we don't
>>>> reject
>>>> as ill-formed the declaration of a 'main' variable in the global
>>>> namespace.
>>>> Not a big deal IMHO, but the below simple check works well for me on
>>>> x86_64-linux.
>>> Just fyi, gcc accepts
>>>
>>> decltype(main) x;
>>>
>>> decltype(sizeof(decltype(main)*)) x;
>>>
>>> which are "uses" of main and also violate [basic.start.main]/3.
>> "good" to know. In my experience sometimes the front end appears to
>> so-to-speak pre-declare entities. For instance I filed a while ago
>> c++/48396. Not sure if in practice the exact same code is involved...
> Let me clarify: this is not about that. It's code like
>
> int main() {}
>
> decltype(main) x;
>
> whereas just having
>
> decltype(main) x;
>
> as the whole program will diagnose the use of an undeclared
> identifier. Nevertheless,
> no use, not just odr-use, but use of main as in the entry point
> pseudo-function is allowed
> by the standard, but gcc allows some of them. gcc rejects attempts to
> call main even in
> such decltype contexts, but it can be fooled to allow other uses of main.
Ah, Ok, I didn't actually try to compile your snippet. Then I suspect 
you mean c++/66606?!? Please double check if something is missing in 
Martin's bug!

Paolo.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 20:42     ` Paolo Carlini
@ 2015-08-19 21:46       ` Ville Voutilainen
  0 siblings, 0 replies; 16+ messages in thread
From: Ville Voutilainen @ 2015-08-19 21:46 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: gcc-patches, Jason Merrill

On 19 August 2015 at 23:37, Paolo Carlini <paolo.carlini@oracle.com> wrote:
> Ah, Ok, I didn't actually try to compile your snippet. Then I suspect you
> mean c++/66606?!? Please double check if something is missing in Martin's
> bug!


That looks fairly comprehensive to me, I don't think I have cases to
add to it. I did confirm
that bug, though, I've been playing with such abuses of main but never
thought it important
enough to bother reporting a bug. :)

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21 17:22           ` Paolo Carlini
@ 2015-08-21 17:37             ` Jason Merrill
  0 siblings, 0 replies; 16+ messages in thread
From: Jason Merrill @ 2015-08-21 17:37 UTC (permalink / raw)
  To: Paolo Carlini, Markus Trippelsdorf; +Cc: gcc-patches

On 08/21/2015 01:11 PM, Paolo Carlini wrote:
> Ah nice. Simply checking:
>
>      scope == global_namespace
>
> appears to work great. Shall I go with that if testing is fine?

Please.

Jason


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21 15:44         ` Jason Merrill
@ 2015-08-21 17:22           ` Paolo Carlini
  2015-08-21 17:37             ` Jason Merrill
  0 siblings, 1 reply; 16+ messages in thread
From: Paolo Carlini @ 2015-08-21 17:22 UTC (permalink / raw)
  To: Jason Merrill, Markus Trippelsdorf; +Cc: gcc-patches

Hi,

On 08/21/2015 05:37 PM, Jason Merrill wrote:
> On 08/21/2015 05:06 AM, Paolo Carlini wrote:
>>     if (DECL_NAME (decl)
>>         && MAIN_NAME_P (DECL_NAME (decl))
>> -      && CP_DECL_CONTEXT (decl) == global_namespace)
>> +      && CP_DECL_CONTEXT (decl) == global_namespace
>> +      && !at_function_scope_p ())
>
> How about looking at the "scope" local variable instead of 
> CP_DECL_CONTEXT?
Ah nice. Simply checking:

     scope == global_namespace

appears to work great. Shall I go with that if testing is fine?

Thanks!
Paolo.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  9:22       ` Paolo Carlini
  2015-08-21  9:29         ` Markus Trippelsdorf
@ 2015-08-21 15:44         ` Jason Merrill
  2015-08-21 17:22           ` Paolo Carlini
  1 sibling, 1 reply; 16+ messages in thread
From: Jason Merrill @ 2015-08-21 15:44 UTC (permalink / raw)
  To: Paolo Carlini, Markus Trippelsdorf; +Cc: gcc-patches

On 08/21/2015 05:06 AM, Paolo Carlini wrote:
>     if (DECL_NAME (decl)
>         && MAIN_NAME_P (DECL_NAME (decl))
> -      && CP_DECL_CONTEXT (decl) == global_namespace)
> +      && CP_DECL_CONTEXT (decl) == global_namespace
> +      && !at_function_scope_p ())

How about looking at the "scope" local variable instead of CP_DECL_CONTEXT?

Jason

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  9:29         ` Markus Trippelsdorf
@ 2015-08-21 12:57           ` Paolo Carlini
  0 siblings, 0 replies; 16+ messages in thread
From: Paolo Carlini @ 2015-08-21 12:57 UTC (permalink / raw)
  To: Markus Trippelsdorf; +Cc: gcc-patches, Jason Merrill

Hi,

On 08/21/2015 11:23 AM, Markus Trippelsdorf wrote:
> On 2015.08.21 at 11:06 +0200, Paolo Carlini wrote:
>> ... I'm testing the below. So far appears to work well for me.
> Yes for me, too. Firefox now builds without any issues.
> So emitting an error (like clang) is fine after all.
Excellent. Testing completed successfully. Thus, unless Jason disagrees, 
I mean to commit it as an obvious follow up later today.

Paolo.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  9:22       ` Paolo Carlini
@ 2015-08-21  9:29         ` Markus Trippelsdorf
  2015-08-21 12:57           ` Paolo Carlini
  2015-08-21 15:44         ` Jason Merrill
  1 sibling, 1 reply; 16+ messages in thread
From: Markus Trippelsdorf @ 2015-08-21  9:29 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: gcc-patches, Jason Merrill

On 2015.08.21 at 11:06 +0200, Paolo Carlini wrote:
> ... I'm testing the below. So far appears to work well for me.

Yes for me, too. Firefox now builds without any issues.
So emitting an error (like clang) is fine after all.

Thanks.
-- 
Markus

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  9:07     ` Paolo Carlini
@ 2015-08-21  9:22       ` Paolo Carlini
  2015-08-21  9:29         ` Markus Trippelsdorf
  2015-08-21 15:44         ` Jason Merrill
  0 siblings, 2 replies; 16+ messages in thread
From: Paolo Carlini @ 2015-08-21  9:22 UTC (permalink / raw)
  To: Markus Trippelsdorf; +Cc: gcc-patches, Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 92 bytes --]

... I'm testing the below. So far appears to work well for me.

Paolo.

///////////////////

[-- Attachment #2: patchlet --]
[-- Type: text/plain, Size: 859 bytes --]

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 227054)
+++ cp/decl.c	(working copy)
@@ -8357,7 +8357,8 @@ grokvardecl (tree type,
 
   if (DECL_NAME (decl)
       && MAIN_NAME_P (DECL_NAME (decl))
-      && CP_DECL_CONTEXT (decl) == global_namespace)
+      && CP_DECL_CONTEXT (decl) == global_namespace
+      && !at_function_scope_p ())
     error ("cannot declare %<::main%> to be a global variable");
 
   /* Check that the variable can be safely declared as a concept.
Index: testsuite/g++.dg/other/pr67065.C
===================================================================
--- testsuite/g++.dg/other/pr67065.C	(revision 227054)
+++ testsuite/g++.dg/other/pr67065.C	(working copy)
@@ -1,3 +1,5 @@
 // PR c++/67065
 
 int main;  // { dg-error "cannot declare" }
+
+void foo() { int main; }

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  7:47   ` Markus Trippelsdorf
@ 2015-08-21  9:07     ` Paolo Carlini
  2015-08-21  9:22       ` Paolo Carlini
  0 siblings, 1 reply; 16+ messages in thread
From: Paolo Carlini @ 2015-08-21  9:07 UTC (permalink / raw)
  To: Markus Trippelsdorf; +Cc: gcc-patches, Jason Merrill

Hi,

On 08/21/2015 08:30 AM, Markus Trippelsdorf wrote:
> On 2015.08.21 at 08:18 +0200, Markus Trippelsdorf wrote:
>> On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
>>> Hi,
>>>
>>> submitter noticed that, in violation of [basic.start.main], we don't
>>> reject as ill-formed the declaration of a 'main' variable in the global
>>> namespace. Not a big deal IMHO, but the below simple check works well
>>> for me on x86_64-linux.
>> Well, it breaks building Firefox for example (several violations).
>> I'm not sure this is really worth the hassle. Maybe a warning instead of
>> an error would be acceptable?
> For example:
>
>   % echo "void foo() { int main; }" | g++ -c -x c++ -
> <stdin>: In function ‘void foo()’:
> <stdin>:1:18: error: cannot declare ‘::main’ to be a global variable
This is simply a bug in my patch, isn't intended. I'll see if I can fix 
it today or I will simply revert the change for now. Sorry about that. 
Another issue, is whether we want to implement that sentence, and we 
want an error. For example, current clang does, and emits an error. I 
don't have a strong opinion about that.

Paolo.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-21  6:30 ` Markus Trippelsdorf
@ 2015-08-21  7:47   ` Markus Trippelsdorf
  2015-08-21  9:07     ` Paolo Carlini
  0 siblings, 1 reply; 16+ messages in thread
From: Markus Trippelsdorf @ 2015-08-21  7:47 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: gcc-patches, Jason Merrill

On 2015.08.21 at 08:18 +0200, Markus Trippelsdorf wrote:
> On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
> > Hi,
> > 
> > submitter noticed that, in violation of [basic.start.main], we don't 
> > reject as ill-formed the declaration of a 'main' variable in the global 
> > namespace. Not a big deal IMHO, but the below simple check works well 
> > for me on x86_64-linux.
> 
> Well, it breaks building Firefox for example (several violations).
> I'm not sure this is really worth the hassle. Maybe a warning instead of
> an error would be acceptable?

For example:

 % echo "void foo() { int main; }" | g++ -c -x c++ -
<stdin>: In function ‘void foo()’:
<stdin>:1:18: error: cannot declare ‘::main’ to be a global variable

-- 
Markus

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 19:52 Paolo Carlini
  2015-08-20  2:35 ` Jason Merrill
@ 2015-08-21  6:30 ` Markus Trippelsdorf
  2015-08-21  7:47   ` Markus Trippelsdorf
  1 sibling, 1 reply; 16+ messages in thread
From: Markus Trippelsdorf @ 2015-08-21  6:30 UTC (permalink / raw)
  To: Paolo Carlini; +Cc: gcc-patches, Jason Merrill

On 2015.08.19 at 21:46 +0200, Paolo Carlini wrote:
> Hi,
> 
> submitter noticed that, in violation of [basic.start.main], we don't 
> reject as ill-formed the declaration of a 'main' variable in the global 
> namespace. Not a big deal IMHO, but the below simple check works well 
> for me on x86_64-linux.

Well, it breaks building Firefox for example (several violations).
I'm not sure this is really worth the hassle. Maybe a warning instead of
an error would be acceptable?

-- 
Markus

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
  2015-08-19 19:52 Paolo Carlini
@ 2015-08-20  2:35 ` Jason Merrill
  2015-08-21  6:30 ` Markus Trippelsdorf
  1 sibling, 0 replies; 16+ messages in thread
From: Jason Merrill @ 2015-08-20  2:35 UTC (permalink / raw)
  To: Paolo Carlini, gcc-patches

OK.

Jason

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function")
@ 2015-08-19 19:52 Paolo Carlini
  2015-08-20  2:35 ` Jason Merrill
  2015-08-21  6:30 ` Markus Trippelsdorf
  0 siblings, 2 replies; 16+ messages in thread
From: Paolo Carlini @ 2015-08-19 19:52 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill

[-- Attachment #1: Type: text/plain, Size: 291 bytes --]

Hi,

submitter noticed that, in violation of [basic.start.main], we don't 
reject as ill-formed the declaration of a 'main' variable in the global 
namespace. Not a big deal IMHO, but the below simple check works well 
for me on x86_64-linux.

Thanks,
Paolo.

//////////////////////////////

[-- Attachment #2: CL_67065 --]
[-- Type: text/plain, Size: 245 bytes --]

/cp
2015-08-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/67065
	* decl.c (grokvardecl): Reject 'main' as global variable.

/testsuite
2015-08-19  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/67065
	* g++.dg/other/pr67065.C: New.

[-- Attachment #3: patch_67065 --]
[-- Type: text/plain, Size: 866 bytes --]

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 227003)
+++ cp/decl.c	(working copy)
@@ -8355,6 +8355,11 @@ grokvardecl (tree type,
   else
     DECL_INTERFACE_KNOWN (decl) = 1;
 
+  if (DECL_NAME (decl)
+      && MAIN_NAME_P (DECL_NAME (decl))
+      && CP_DECL_CONTEXT (decl) == global_namespace)
+    error ("cannot declare %<::main%> to be a global variable");
+
   /* Check that the variable can be safely declared as a concept.
      Note that this also forbids explicit specializations.  */
   if (conceptp)
Index: testsuite/g++.dg/other/pr67065.C
===================================================================
--- testsuite/g++.dg/other/pr67065.C	(revision 0)
+++ testsuite/g++.dg/other/pr67065.C	(working copy)
@@ -0,0 +1,3 @@
+// PR c++/67065
+
+int main;  // { dg-error "cannot declare" }

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2015-08-21 17:31 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-19 20:27 [C++ Patch] PR 67065 ("Missing diagnostics for ill-formed program with main variable instead of function") Ville Voutilainen
2015-08-19 20:28 ` Paolo Carlini
2015-08-19 20:37   ` Ville Voutilainen
2015-08-19 20:42     ` Paolo Carlini
2015-08-19 21:46       ` Ville Voutilainen
  -- strict thread matches above, loose matches on Subject: below --
2015-08-19 19:52 Paolo Carlini
2015-08-20  2:35 ` Jason Merrill
2015-08-21  6:30 ` Markus Trippelsdorf
2015-08-21  7:47   ` Markus Trippelsdorf
2015-08-21  9:07     ` Paolo Carlini
2015-08-21  9:22       ` Paolo Carlini
2015-08-21  9:29         ` Markus Trippelsdorf
2015-08-21 12:57           ` Paolo Carlini
2015-08-21 15:44         ` Jason Merrill
2015-08-21 17:22           ` Paolo Carlini
2015-08-21 17:37             ` Jason Merrill

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 read-only IMAP folder(s) and NNTP newsgroup(s).