View previous topic :: View next topic |
Author |
Message |
John Morley
Joined: 09 Aug 2004 Posts: 97
|
Compiler warning..... [Solved] |
Posted: Mon Jun 24, 2013 7:15 am |
|
|
Hi All,
PCH Compiler: v4.121
I have created a function that seems to work just fine, but is generating a compiler warning at compile time. The warning is telling me that my
function is not Void (true), yet it does not return a value (not true). I have several similarly declared functions, and they result in the same
warning.
Here is the warning: Function not void and does not return a value CommTestOK
Here is how the function is defined, and how I'm returning a value.
Code: |
int1 CommTestOK(void)
{
// Here we return the COMM status!
if (strcmp(Msg1, CMDOK) == 0)
Return(True);
else
Return(False);
}
|
Am I doing something wrong, or is this a false warning with a function declared as a boolean?
Thanks, _________________ John Morley
Last edited by John Morley on Tue Jun 25, 2013 9:03 pm; edited 1 time in total |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jun 24, 2013 7:59 am |
|
|
Replace:
Code: | int1 CommTestOK(void) |
with:
... warning goes away.
G. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 24, 2013 8:07 am |
|
|
If you do a basic 'test program' as:
Code: |
#include <12F615.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPUT //No Power Up Timer
#FUSES BROWNOUT //Reset when brownout detected
//Obviously setup to suit your processor to here
#use delay(clock=8000000)
char Msg1[10];
char CMDOK[]="OK";
#include "string.h"
int1 CommTestOK(void)
{
// Here we return the COMM status!
if (strcmp(Msg1, CMDOK) == 0)
Return(True);
else
Return(False);
}
void main(void)
{
int val;
while(1)
{
val=CommTestOK();
val=!val;
}
}
|
It compiles without warning.
I'd suspect you have something like a function prototype, without the void declaration?...
Best Wishes |
|
|
John Morley
Joined: 09 Aug 2004 Posts: 97
|
|
Posted: Mon Jun 24, 2013 8:07 am |
|
|
Hi Gabriel,
I should have mentioned that I already tried that, and the warning still persisted!
I also tried a slightly different form for returning the desired value, and the warning still persisted:
What compiler version do you have? _________________ John Morley |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 24, 2013 8:10 am |
|
|
I didn't get a warning. I made a test program (which you should have
posted) and it gives no warning like that. It gives the usual warning
on the while(1) statement, but that's all:
Quote: |
CCS PCH C Compiler, Version 4.121, ----- 24-Jun-13 07:06
Executing: "C:\Program Files\PICC\Ccsc.exe" +FH "PCH_Test.c" +DF +LN -T -A +M -Z +Y=9 +EA #__18F4620=TRUE
>>> Warning 203 "PCH_Test.c" Line 28(1,1): Condition always TRUE
Memory usage: ROM=0% RAM=1% - 1%
0 Errors, 1 Warnings.
Loaded C:\Program Files\PICC\Projects\PCH_Test\PCH_Test.cof.
BUILD SUCCEEDED: Mon Jun 24 07:06:45 2013
|
Test program:
Code: |
#include <18F4620.h>
#fuses INTRC_IO,NOWDT
#use delay(clock=4M)
#include <string.h>
int8 CMDOK[4] = "OK\r";
int8 Msg1[20];
int1 CommTestOK(void)
{
// Here we return the COMM status!
if (strcmp(Msg1, CMDOK) == 0)
Return(True);
else
Return(False);
}
//======================================
void main(void)
{
int1 result;
result = CommTestOK();
while(1);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 24, 2013 8:19 am |
|
|
I tried with 4.118, 4.121, and 4.141. None gave the warning.
Best Wishes |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Jun 24, 2013 8:22 am |
|
|
I had that warning problem last friday while compiling the SHT75 driver found on the library...
i removed the "void" from the functions and the warnings went away...
... compiler issue?
I use 4.135
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
John Morley
Joined: 09 Aug 2004 Posts: 97
|
|
Posted: Mon Jun 24, 2013 8:28 am |
|
|
Hi TTelmah,
I tried to compile your small test program, and it compiles fine with no warnings. For the most part I don't use function prototypes, I just arrange
my code so that they aren't needed. In this case there is definitely no function prototype for the CommTestOK() function.
Again, this function is working as expected, and overall, the code is working well. I'm just trying to clean up a few things prior to release.....
Thanks! _________________ John Morley |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Jun 24, 2013 9:02 am |
|
|
I'd be suspicious of something 'else'. CCS has a habit of reporting errors because of another 'almost error' somewhere else. Perhaps another function 'in front', that is just happening to report here....
Being 'pedantic', the function is not declared as a Boolean.... C, does not 'have' a Boolean construct/type. It is declared as an int1, that you just happen to be using to return the two defines 'TRUE', and 'FALSE', which correspond to the 'Boolean' usage.
I actually wonder if anything is saved by doing this. A whole byte probably has to be returned 'anyway', so it is probably easier/safer just to use the standard 'int'....
One thing I 'dislike' slightly is your use of 'True', rather than 'TRUE'. Normally the C 'standard', is to fully capitalise all 'defines'. Mixing case like this could easily cause things to go wrong, if case significance was enabled.
I'd be 90% suspicious it is actually being caused by some tiny syntax error further up the code, that is not being reported, but is 'revealing itself' this way. One old trick used to be to put CCS code through Lint, and use this to help tidy it up.
Best Wishes |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Jun 24, 2013 4:20 pm |
|
|
Also, "Return" is a statement, not a function. Therefore the correct syntax is:
Code: |
return TRUE;
// and not
Return(TRUE);
// or even
Return TRUE; // This works with CCS C which is case insensitive by default. Other Cs are not and will not think its the same as "return TRUE".
return(TRUE); // Looks like a function, but is just a return of an expression which has brackets around it which evaluates to 1.
|
Also, while syntactically AND semantically correct in C (it wouldn't be in some other C derived languages such as C#), int1 is the conceptually the wrong return type for a boolean function. It would be more logical to use boolean, though in C boolean is just int. Not even int1 in fact, just int. Int1 being a special, single bit and always unsigned, version of int.
All that said, it may well be just as Ttelmah writes: the real problem is elsewhere and this is just where CCS C finally reports a problem.
Last edited by RF_Developer on Mon Jun 24, 2013 4:28 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 24, 2013 4:27 pm |
|
|
K&R says this, in chapter 4:
Quote: |
The return statement is the mechanism for returning a value from
the called function to its caller. Any expression can follow return:
return expression;
The expression will be converted to the return type of the function if
necessary. Parentheses are often used around the expression,
but they are optional.
|
So it's certainly legal. It's a matter of style. |
|
|
John Morley
Joined: 09 Aug 2004 Posts: 97
|
Compiler warning..... [Solved] |
Posted: Tue Jun 25, 2013 2:43 pm |
|
|
Hi All,
I solved the issue I was having. In the subroutine in question, I had an alternate 'Return' statement in the case of a buffer overflow. This Return
did not return an argument as it should have. Some of this code was a cut-n-paste from another effort, and I didn't check it carefully enough....
Problem code:
Code: |
if (ArrayIndex1 > 70)
Return; //Msg1 buffer has overflowed, so exit the subroutine!
|
Corrected code:
Code: |
if (ArrayIndex1 > 70)
Return(False); //Msg1 buffer has overflowed, so exit the subroutine!
|
Thanks for the input on this guys! I learned something and I made it to PCM programmers 'boneheaded-post-of-the-day' list
Thanks, _________________ John Morley |
|
|
|