mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 332283 - Parser gives warning if main does not return anything
This commit is contained in:
parent
8ad690e18a
commit
2e1de7fb99
2 changed files with 23 additions and 3 deletions
|
@ -131,6 +131,18 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
|
|||
return false;
|
||||
}
|
||||
|
||||
public boolean isMain(IASTFunctionDefinition func) {
|
||||
try {
|
||||
String functionName = func.getDeclarator().getName().getRawSignature();
|
||||
if (functionName.equals("main")) { //$NON-NLS-1$
|
||||
return true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// well, not main
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
@ -144,7 +156,7 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
|
|||
ReturnStmpVisitor visitor = new ReturnStmpVisitor(func);
|
||||
func.accept(visitor);
|
||||
boolean nonVoid = !isVoid(func);
|
||||
if (nonVoid) {
|
||||
if (nonVoid && !isMain(func)) {
|
||||
// there a return but maybe it is only on one branch
|
||||
IASTStatement body = func.getBody();
|
||||
if (body instanceof IASTCompoundStatement) {
|
||||
|
@ -163,8 +175,7 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
|
|||
reportNoRet(func, visitor.hasret);
|
||||
}
|
||||
} else {
|
||||
|
||||
reportNoRet(func, false);
|
||||
reportNoRet(func, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -280,4 +280,13 @@ public class ReturnCheckerTest extends CheckerTestCase {
|
|||
checkNoErrors();
|
||||
}
|
||||
|
||||
// int main()
|
||||
// {
|
||||
// char c; // added so function body is non-empty
|
||||
// // no error since return value in main is optional
|
||||
// }
|
||||
public void testMainFunction() {
|
||||
loadCodeAndRunCpp(getAboveComment());
|
||||
checkNoErrors();
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue