Clang warning: ISO C++ does not allow 'main' to be used by a program [-Wmain] (ext_main_used)
Jump to navigation
Jump to search
Text | error: ISO C++ does not allow 'main' to be used by a program |
---|---|
Type | Warning |
Category | Semantic Issue |
Internal Id | ext_main_used |
Active by Default | No |
Flags | -Wmain (7 elements) -Wpedantic (151 elements) |
Internal Message | ISO C++ does not allow 'main' to be used by a program
|
Regular Expression | (?:warning|error|fatal error)\: ISO C\+\+ does not allow 'main' to be used by a program \[(?:\-Werror,)?\-Wmain[^\]]*\]
|
First Commit | 2014-01-22 b63b6ee9a00e Enforce restrictions that 'main' is not allowed to be deleted, or to be used by |
Description
Example
Flags | -xc++ -Wmain
|
|
---|---|---|
Source |
int main() {
// Recursion
main();
return 0;
}
| |
Compiler Output |
<source>:3:3: warning: ISO C++ does not allow 'main' to be used by a program [-Wmain] |
Clang Internals (17.0.6)
Git Commit Message
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
Used in Clang Sources
This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.
clang/lib/Sema/SemaExpr.cpp (line 248)
/// Determine whether the use of this declaration is valid, and
/// emit any corresponding diagnostics.
///
/// This routine diagnoses various problems with referencing
/// declarations that can occur when using a declaration. For example,
/// it might warn if a deprecated or unavailable declaration is being
/// used, or produce an error (and return true) if a C++0x deleted
/// function is being used.
///
/// \returns true if there was an error (this declaration cannot be
/// referenced), false otherwise.
///
bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs, const ObjCInterfaceDecl *UnknownObjCClass, bool ObjCPropertyAccess, bool AvoidPartialAvailabilityChecks, ObjCInterfaceDecl *ClassReceiver, bool SkipTrailingRequiresClause) {
// ...
if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) {
// ...
// 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);
Triggered in Clang Tests
This section lists all internal Clang test cases that trigger the diagnostic.
clang/test/CXX/basic/basic.start/basic.start.init/p3.cpp
- clang/test/CXX/basic/basic.start/basic.start.init/p3.cpp:19:32: error: ISO C++ does not allow 'main' to be used by a program [-Werror,-Wmain]
- clang/test/CXX/basic/basic.start/basic.start.init/p3.cpp:22:5: error: ISO C++ does not allow 'main' to be used by a program [-Werror,-Wmain]