1
0
Fork 0
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:
Alena Laskavaia 2011-06-27 22:01:24 -04:00
parent 8ad690e18a
commit 2e1de7fb99
2 changed files with 23 additions and 3 deletions

View file

@ -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,7 +175,6 @@ public class ReturnChecker extends AbstractAstFunctionChecker {
reportNoRet(func, visitor.hasret);
}
} else {
reportNoRet(func, false);
}
}

View file

@ -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();
}
}