Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java17
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java9
2 files changed, 23 insertions, 3 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java
index 86a9d9e25a..f7bc5fca93 100644
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ReturnChecker.java
@@ -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);
}
}
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java
index aba819266d..09ec9f408b 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ReturnCheckerTest.java
@@ -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();
+ }
} \ No newline at end of file

Back to the top