[clang] [Clang] strengthen checks for 'main' function to meet [basic.start.main] p3 requirements (PR #101853)

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 5 12:39:00 PDT 2024


================
@@ -8052,15 +8061,13 @@ 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 (isMainVar(Name, NewVD) && !getLangOpts().Freestanding) {
     // C++ [basic.start.main]p3
     // A program that declares a variable main at global scope is ill-formed.
-    if (getLangOpts().CPlusPlus)
-      Diag(D.getBeginLoc(), diag::err_main_global_variable);
-
+    if (getLangOpts().CPlusPlus) {
+      if (!CheckLinkageSpecification(DC, NewVD))
----------------
a-tarasyuk wrote:

There is a test to cover this case in [p3](https://github.com/llvm/llvm-project/blob/cc25748d07b0adb1a0223e836e9cae22af150496/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp#L13)

https://github.com/llvm/llvm-project/pull/101853


More information about the cfe-commits mailing list