Index: include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- include/clang/Basic/DiagnosticSemaKinds.td +++ include/clang/Basic/DiagnosticSemaKinds.td @@ -510,6 +510,7 @@ def err_main_arg_wrong : Error<"%select{first|second|third|fourth}0 " "parameter of 'main' (%select{argument count|argument array|environment|" "platform-specific data}0) must be of type %1">; +def err_main_global_variable : Error<"main can't be declared as global variable">; def ext_main_used : Extension< "ISO C++ does not allow 'main' to be used by a program">, InGroup
; Index: lib/Sema/SemaDecl.cpp =================================================================== --- lib/Sema/SemaDecl.cpp +++ lib/Sema/SemaDecl.cpp @@ -6105,6 +6105,13 @@ } } + // [basic.start.main]p3 + // A program that declares a variable main at global scope is ill-formed. + if (getLangOpts().CPlusPlus && Name.getAsString() == "main" && + NewVD->isFileVarDecl()) { + Diag(D.getLocStart(), diag::err_main_global_variable); + } + if (D.isRedeclaration() && !Previous.empty()) { checkDLLAttributeRedeclaration( *this, dyn_cast(Previous.getRepresentativeDecl()), NewVD, Index: test/CXX/basic/basic.start/basic.start.main/p3.cpp =================================================================== --- test/CXX/basic/basic.start/basic.start.main/p3.cpp +++ test/CXX/basic/basic.start/basic.start.main/p3.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +int main; // expected-error{{main can't be declared as global variable}} + +int f () { + int main; // OK + (void)main; +}