]> git.sur5r.net Git - cc65/blobdiff - src/cc65/stmt.c
Add a warning if a function returning something does not contain a return
[cc65] / src / cc65 / stmt.c
index ef150fad598b49286196a4e6eb0318d09ba53ab6..b9b258aa09e365741b5263cf3d1e24d9e1715dfe 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2008 Ullrich von Bassewitz                                       */
-/*               Roemerstrasse 52                                            */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1998-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -292,17 +292,16 @@ static void ReturnStatement (void)
     NextToken ();
     if (CurTok.Tok != TOK_SEMI) {
 
-        /* Check if the function has a return value declared */
-               if (F_HasVoidReturn (CurrentFunc)) {
-                   Error ("Returning a value in function with return type void");
-               }
-
        /* Evaluate the return expression */
        hie0 (&Expr);
 
-       /* Ignore the return expression if the function returns void */
-       if (!F_HasVoidReturn (CurrentFunc)) {
-
+        /* If we return something in a void function, print an error and
+         * ignore the value. Otherwise convert the value to the type of the
+         * return.
+         */
+               if (F_HasVoidReturn (CurrentFunc)) {
+                   Error ("Returning a value in function with return type void");
+               } else {
            /* Convert the return value to the type of the function result */
            TypeConversion (&Expr, F_GetReturnType (CurrentFunc));
 
@@ -314,6 +313,9 @@ static void ReturnStatement (void)
        Error ("Function `%s' must return a value", F_GetFuncName (CurrentFunc));
     }
 
+    /* Mark the function as having a return statement */
+    F_HasReturn (CurrentFunc);
+
     /* Cleanup the stack in case we're inside a block with locals */
     g_space (StackPtr - F_GetTopLevelSP (CurrentFunc));