Errors are leaving files...

Bug reports (if you don't/won't have a Github account)
Post Reply
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Errors are leaving files...

Post by obiwanjacobi »

Perhaps a bit vague, but I had the following issue:

I had an .h file where I forgot a ';' on the last line.
Something like:

Code: Select all

#ifndef __MYFILE_H__
#define __MYFILE_H__

void just_some_function(int some_param)   // <=== forgot ;

#endif    //__MYFILE_H__
The result is that you get an error on a totally different location in a file that is not even related.
In my case I got a syntax error on '{'... go figure.

I think you can safely assume that if you did not find the ';' by the time the file ends, you have an error...
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

obiwanjacobi wrote:I think you can safely assume that if you did not find the ';' by the time the file ends, you have an error...
The compiler only gets to see the translation unit after the includes have been inlined by the pre-processor, so it has no idea the prototypes came from a separate header file. It just fails to see a semi-colon and continues looking to see if it will find an opening brace for a function body.

I'm not sure error reporting can be improved in this case unless something truly wacky is happening. Maybe the only remedy is to try running source with strange errors through lint or similar? Although I'm not sure what a tool like lint will make of the unusual attributes found in z88dk.
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

So in that case the text would be something like:

Code: Select all

#ifndef __MYFILE_H__
#define __MYFILE_H__

void just_some_function(int some_param)   // <=== forgot ;

#endif    //__MYFILE_H__

// #include "MyFile.h"

void some_other_function(int some_param)
{                                                               // <=== syntax error report on '{'
   // blabla
}
Looks odd that it would parse all the way over two function prototypes. Is that valid C syntax?

Anyway, this one took me several hours to track down because the error report had nothing to do with the location of the actual error.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

(All the preprocessor stuff will have been removed by the time the compiler sees it)

Code: Select all

void just_some_function(int some_param)   // <=== forgot ;

void some_other_function(int some_param)
{                                                               // <=== syntax error report on '{'
   // blabla
}
Looks odd that it would parse all the way over two function prototypes. Is that valid C syntax?
Anyway, this one took me several hours to track down because the error report had nothing to do with the location of the actual error.
I was giving it a little leeway because C is also supposed to accept the old K&R function prototypes as in:

Code: Select all

void func(a,b,c)
int a;
char b;
long c;
{
...
}
So a missing semi-colon as in your example may cause the scanner to look through a following K&R parameter declaration like "void some_other_function(int some_param)" before reporting an error. This list bit is not a valid parameter declaration but it won't known that until it sees the function syntax.

But then I tried this:

Code: Select all

void just_some_function(int some_param)   // <=== forgot ;

int a;

void some_other_function(int some_param)
{                                                               // <=== syntax error report on '{'
   // blabla
}
And the error is still not reported until "void some_other_function(int some_param)". The compiler went through the "int a" but it should be able to see there that the "a" does not match a parameter name in the function prototype's parameter list.

I think this is just reflection of the current state of sdcc's support for K&R syntax. It doesn't support K&R declarations as of now but I think it's scanner does recognize it so we get this halfway acceptance. The scanner knows that an "int a" could be a K&R parameter declaration but the parser/compiler doesn't know about K&R so can't generate an error at the right place.

I'll make a bug report for this at sdcc and see if they want to look at it. The K&R syntax has been on their to-do list for a long time so I don't know if they'd rather just wait for that to be done instead of fixing this.
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

Didn't know that was valid syntax. That explains some of it.

Thanx for the effort.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

obiwanjacobi wrote:Didn't know that was valid syntax. That explains some of it.
Yeah it's part of the obsolete grammar that goes back to the original C and is kept for sake of backward compatibility.
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

... and obfuscated C programming contests :P
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

If its obsolete you could just rip it out... :-P
stefano
Well known member
Posts: 1534
Joined: Mon Jul 16, 2007 7:39 pm

Post by stefano »

I'd like to keep it.. at least optionally
Post Reply