how_main_is_not_mangled.rst 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. How is main() function not mangled in C++
  2. =================================================
  3. When building a parser tree, GCC generates a "main" identifier node::
  4. // in gcc/tree.h
  5. #define main_identifier_node global_trees[TI_MAIN_IDENTIFIER]
  6. #define MAIN_NAME_P(NODE) \
  7. (IDENTIFIER_NODE_CHECK (NODE) == main_identifier_node)
  8. // in gcc/c-family/c-common.c
  9. main_identifier_node = get_identifier ("main");
  10. GCC checks main and set it as a C declaration in gcc/cp/decl.c::
  11. /* `main' and builtins have implicit 'C' linkage. */
  12. if (ctype == NULL_TREE
  13. && DECL_FILE_SCOPE_P (decl)
  14. && current_lang_name == lang_name_cplusplus
  15. && (MAIN_NAME_P (declarator)
  16. || (IDENTIFIER_LENGTH (declarator) > 10
  17. && IDENTIFIER_POINTER (declarator)[0] == '_'
  18. && IDENTIFIER_POINTER (declarator)[1] == '_'
  19. && strncmp (IDENTIFIER_POINTER (declarator)+2,
  20. "builtin_", 8) == 0)
  21. || (targetcm.cxx_implicit_extern_c
  22. && (targetcm.cxx_implicit_extern_c
  23. (IDENTIFIER_POINTER (declarator))))))
  24. SET_DECL_LANGUAGE (decl, lang_c);
  25. Also, the compiler don't warn when main() doesn't have a return statement. This is also checked using MAIN_NAME_P macro.