From: John McCall Date: Thu, 24 Dec 2009 09:58:38 +0000 (+0000) Subject: Tweak the text of several main() diagnostics and punch a hole specifically for X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6675586c70945fdd71911d96f83324788b93edd4;p=clang Tweak the text of several main() diagnostics and punch a hole specifically for Darwin's sekrit fourth argument. This should probably be factored to let targets make target-specific decisions about what main() should look like. Fixes rdar://problem/7414990 or if different platforms have radically different ideas of what they want in git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92128 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index bb507d17a7..b9754f4e24 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -186,11 +186,12 @@ def warn_unusual_main_decl : Warning<"'main' should not be declared " def err_unusual_main_decl : Error<"'main' is not allowed to be declared " "%select{static|inline|static or inline}0">; def err_main_returns_nonint : Error<"'main' must return 'int'">; -def err_main_surplus_args : Error<"%0 is too many arguments for 'main': " +def err_main_surplus_args : Error<"too many parameters (%0) for 'main': " "must be 0, 2, or 3">; -def warn_main_one_arg : Warning<"one-argument 'main' is usually a mistake">; -def err_main_arg_wrong : Error<"%select{first|second|third}0 argument of " - "'main' should be of type %1">; +def warn_main_one_arg : Warning<"only one parameter on 'main' declaration">; +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">; /// parser diagnostics def ext_typedef_without_a_name : ExtWarn<"typedef requires a name">; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index b7584c311e..9ed4fba66e 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -35,6 +35,7 @@ #include "clang/Lex/HeaderSearch.h" #include "llvm/ADT/BitVector.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/Triple.h" #include #include #include @@ -3398,7 +3399,16 @@ void Sema::CheckMain(FunctionDecl* FD) { unsigned nparams = FTP->getNumArgs(); assert(FD->getNumParams() == nparams); - if (nparams > 3) { + bool HasExtraParameters = (nparams > 3); + + // Darwin passes an undocumented fourth argument of type char**. If + // other platforms start sprouting these, the logic below will start + // getting shifty. + if (nparams == 4 && + Context.Target.getTriple().getOS() == llvm::Triple::Darwin) + HasExtraParameters = false; + + if (HasExtraParameters) { Diag(FD->getLocation(), diag::err_main_surplus_args) << nparams; FD->setInvalidDecl(true); nparams = 3; @@ -3409,7 +3419,7 @@ void Sema::CheckMain(FunctionDecl* FD) { QualType CharPP = Context.getPointerType(Context.getPointerType(Context.CharTy)); - QualType Expected[] = { Context.IntTy, CharPP, CharPP }; + QualType Expected[] = { Context.IntTy, CharPP, CharPP, CharPP }; for (unsigned i = 0; i < nparams; ++i) { QualType AT = FTP->getArgType(i); diff --git a/test/CXX/basic/basic.start/basic.start.main/p2f.cpp b/test/CXX/basic/basic.start/basic.start.main/p2f.cpp index e346d318b1..a3d6a79a4f 100644 --- a/test/CXX/basic/basic.start/basic.start.main/p2f.cpp +++ b/test/CXX/basic/basic.start/basic.start.main/p2f.cpp @@ -1,7 +1,7 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s void // expected-error {{error: 'main' must return 'int'}} -main( // expected-error {{error: first argument of 'main' should be of type 'int'}} +main( // expected-error {{error: first parameter of 'main' (argument count) must be of type 'int'}} float a ) { }