https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66606

            Bug ID: 66606
           Summary: missing diagnostic on using function main
           Product: gcc
           Version: 5.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While implementing a fix for pr66516 I noticed that GCC diagnoses some but not
all uses of main in a program (pursuant to 3.6.1, p3 of C++14.)  The following
test case shows the constructs that GCC does diagnose along with those it does
not.  The output below then shows the diagnostics produced by Clang for
comparison.  It's interesting that Clang includes the diagnostics in -Wmain. 
It might worth considering to do the same in GCC.

$ cat /build/tmp/u.cpp && ~/bin/gcc-5.1.0/bin/g++ -Wpedantic -c -o/dev/null
/build/tmp/u.cpp -std=c++11
#include <typeinfo>

int main () { }

typedef decltype (main) F;

F& foo () { return main; }

int bar () { return main (); }

const std::type_info &ti = typeid (main);

const bool e = noexcept (main);

const int i = (main, 0);

template <F> struct A { };
A<main> a;

template <F&> struct B { };
B<main> b;

/build/tmp/u.cpp: In function ‘int (& foo())()’:
/build/tmp/u.cpp:7:20: warning: ISO C++ forbids taking address of function
‘::main’ [-Wpedantic]
 F& foo () { return main; }
                    ^
/build/tmp/u.cpp: In function ‘int bar()’:
/build/tmp/u.cpp:9:27: warning: ISO C++ forbids taking address of function
‘::main’ [-Wpedantic]
 int bar () { return main (); }
                           ^
/build/tmp/u.cpp: At global scope:
/build/tmp/u.cpp:18:7: warning: ISO C++ forbids taking address of function
‘::main’ [-Wpedantic]
 A<main> a;
       ^


Clang output:

$ /build/llvm-3.6.0/bin/clang -Wall -Wpedantic -c -o/dev/null -std=c++11
/build/tmp/u.cpp
/build/tmp/u.cpp:5:19: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
typedef decltype (main) F;
                  ^
/build/tmp/u.cpp:7:20: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
F& foo () { return main; }
                   ^
/build/tmp/u.cpp:9:21: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
int bar () { return main (); }
                    ^
/build/tmp/u.cpp:11:36: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
const std::type_info &ti = typeid (main);
                                   ^
/build/tmp/u.cpp:13:26: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
const bool e = noexcept (main);
                         ^
/build/tmp/u.cpp:15:16: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
const int i = (main, 0);
               ^
/build/tmp/u.cpp:15:16: warning: expression result unused [-Wunused-value]
const int i = (main, 0);
               ^~~~
/build/tmp/u.cpp:18:3: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
A<main> a;
  ^
/build/tmp/u.cpp:21:3: warning: ISO C++ does not allow 'main' to be used by a
      program [-Wmain]
B<main> b;
  ^
/build/tmp/u.cpp:13:12: warning: unused variable 'e' [-Wunused-const-variable]
const bool e = noexcept (main);
           ^
/build/tmp/u.cpp:15:11: warning: unused variable 'i' [-Wunused-const-variable]
const int i = (main, 0);
          ^
11 warnings generated.

Reply via email to