[clang] [Clang] strengthen checks for 'main' function to meet [basic.start.main] p3 requirements (PR #101853)
Mital Ashok via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 6 09:26:36 PDT 2024
================
@@ -8052,10 +8061,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(
}
// Special handling of variable named 'main'.
- if (Name.getAsIdentifierInfo() && Name.getAsIdentifierInfo()->isStr("main") &&
- NewVD->getDeclContext()->getRedeclContext()->isTranslationUnit() &&
- !getLangOpts().Freestanding && !NewVD->getDescribedVarTemplate()) {
-
+ if (!getLangOpts().Freestanding && isMainVar(Name, NewVD)) {
// C++ [basic.start.main]p3
// A program that declares a variable main at global scope is ill-formed.
----------------
MitalAshok wrote:
```suggestion
// C++ [basic.start.main]p3:
// A program that declares
// - a variable main at global scope, or
// - an entity named main with C language linkage (in any namespace)
// is ill-formed.
```
To facilitate the diagnostic showing whether its a global variable or a C linkage variable, you could make `isMainVar` return an enum like `IMV_NotMain, IMV_GlobalMain, IMV_CLinkageMain`. Or you could inline the `isMainVar` function into here again since there would be three options now. This quote from the standard should be good enough documentation
https://github.com/llvm/llvm-project/pull/101853
More information about the cfe-commits
mailing list