diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-01-22 01:43:19 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-01-22 01:43:19 +0000 |
commit | b63b6ee9a00ef0710d899df6cfda78a1b8bd762a (patch) | |
tree | 16d99f0325f0f71e8c08cdf731bf3631774be256 /clang/lib/Sema | |
parent | 407e442245b5515c5f110f81b7c1e3f90c40c68c (diff) | |
download | bcm5719-llvm-b63b6ee9a00ef0710d899df6cfda78a1b8bd762a.tar.gz bcm5719-llvm-b63b6ee9a00ef0710d899df6cfda78a1b8bd762a.zip |
Enforce restrictions that 'main' is not allowed to be deleted, or to be used by
the program, in C++. (We allow the latter as an extension, since we've always
permitted it, and GCC does the same, and our supported C++ ABIs don't do
anything special in main.)
llvm-svn: 199782
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 7 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 7 |
3 files changed, 16 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index c08d87d59a8..9dac4d53024 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6809,6 +6809,8 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, } // If a function is defined as defaulted or deleted, mark it as such now. + // FIXME: Does this ever happen? ActOnStartOfFunctionDef forces the function + // definition kind to FDK_Definition. switch (D.getFunctionDefinitionKind()) { case FDK_Declaration: case FDK_Definition: @@ -7670,8 +7672,9 @@ static SourceRange getResultSourceRange(const FunctionDecl *FD) { } void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { - // C++11 [basic.start.main]p3: A program that declares main to be inline, - // static or constexpr is ill-formed. + // C++11 [basic.start.main]p3: + // A program that [...] declares main to be inline, static or + // constexpr is ill-formed. // C11 6.7.4p4: In a hosted environment, no function specifier(s) shall // appear in a declaration of main. // static main is not an error under C99, but we should warn about it. diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 39d71e20163..76b9d669d42 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -11984,6 +11984,11 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) { } } + // C++11 [basic.start.main]p3: + // A program that defines main as deleted [...] is ill-formed. + if (Fn->isMain()) + Diag(DelLoc, diag::err_deleted_main); + Fn->setDeletedAsWritten(); } diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 3640c5ad24b..ff54b6b24e4 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -264,13 +264,18 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) Diag(Suppressed[I].first, Suppressed[I].second); - + // Clear out the list of suppressed diagnostics, so that we don't emit // them again for this specialization. However, we don't obsolete this // entry from the table, because we want to avoid ever emitting these // diagnostics again. Suppressed.clear(); } + + // C++ [basic.start.main]p3: + // The function 'main' shall not be used within a program. + if (cast<FunctionDecl>(D)->isMain()) + Diag(Loc, diag::ext_main_used); } // See if this is an auto-typed variable whose initializer we are parsing. |