From 1710cc994ecd3b422a54a35117b089adff4d42c1 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Thu, 23 Apr 2015 16:12:42 +0000 Subject: [PATCH] Diagnose variadic main() as an extension; addresses PR17905. llvm-svn: 235605 --- clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaDecl.cpp | 6 ++++++ clang/test/Sema/warn-main.c | 2 ++ 3 files changed, 10 insertions(+) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index d7b5718..b44aa37 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -485,6 +485,8 @@ def warn_static_main : Warning<"'main' should not be declared static">, InGroup
; def err_static_main : Error<"'main' is not allowed to be declared static">; def err_inline_main : Error<"'main' is not allowed to be declared inline">; +def ext_variadic_main : ExtWarn< + "'main' is not allowed to be declared variadic">, InGroup
; def ext_noreturn_main : ExtWarn< "'main' is not allowed to be declared _Noreturn">, InGroup
; def note_main_remove_noreturn : Note<"remove '_Noreturn'">; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 5bb39cb..6916710 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -8252,6 +8252,12 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { bool HasExtraParameters = (nparams > 3); + if (FTP->isVariadic()) { + Diag(FD->getLocation(), diag::ext_variadic_main); + // FIXME: if we had information about the location of the ellipsis, we + // could add a FixIt hint to remove it as a parameter. + } + // Darwin passes an undocumented fourth argument of type char**. If // other platforms start sprouting these, the logic below will start // getting shifty. diff --git a/clang/test/Sema/warn-main.c b/clang/test/Sema/warn-main.c index 58a6dfd..4620663 100644 --- a/clang/test/Sema/warn-main.c +++ b/clang/test/Sema/warn-main.c @@ -29,3 +29,5 @@ _Noreturn int main() { return 0; } +// expected-warning@+1 {{'main' is not allowed to be declared variadic}} +int main(int argc, char**argv, ...) { return 0; } -- 2.7.4