diff options
author | Davide Italiano <davide@freebsd.org> | 2015-08-14 14:13:29 +0000 |
---|---|---|
committer | Davide Italiano <davide@freebsd.org> | 2015-08-14 14:13:29 +0000 |
commit | da8a3b903b4506e4b6a1498fc23f71b2a5d49f02 (patch) | |
tree | 6356bb0fe3855b3ffa8ca0b6d1eec7ec4c1f7fbc /clang/lib/Sema/SemaDecl.cpp | |
parent | beee25e484631608ae02433e76738eee6c7304e6 (diff) | |
download | bcm5719-llvm-da8a3b903b4506e4b6a1498fc23f71b2a5d49f02.tar.gz bcm5719-llvm-da8a3b903b4506e4b6a1498fc23f71b2a5d49f02.zip |
[Sema] main can't be declared as global variable, in C++.
So, we now reject that. We also warn for any external-linkage global
variable named main in C, because it results in undefined behavior.
PR: 24309
Differential Revision: http://reviews.llvm.org/D11658
Reviewed by: rsmith
llvm-svn: 245051
Diffstat (limited to 'clang/lib/Sema/SemaDecl.cpp')
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 7c54986f568..c3fdd900cd0 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6111,6 +6111,22 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, } } + // Special handling of variable named 'main'. + if (Name.isIdentifier() && Name.getAsIdentifierInfo()->isStr("main") && + NewVD->getDeclContext()->getRedeclContext()->isTranslationUnit() && + !getLangOpts().Freestanding && !NewVD->getDescribedVarTemplate()) { + + // C++ [basic.start.main]p3 + // A program that declares a variable main at global scope is ill-formed. + if (getLangOpts().CPlusPlus) + Diag(D.getLocStart(), diag::err_main_global_variable); + + // In C, and external-linkage variable named main results in undefined + // behavior. + else if (NewVD->hasExternalFormalLinkage()) + Diag(D.getLocStart(), diag::warn_main_redefined); + } + if (D.isRedeclaration() && !Previous.empty()) { checkDLLAttributeRedeclaration( *this, dyn_cast<NamedDecl>(Previous.getRepresentativeDecl()), NewVD, |