Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Utility to ensure appropriate headers were included (http://www.velocityreviews.com/forums/t593809-utility-to-ensure-appropriate-headers-were-included.html)

Tomás Ó hÉilidhe 02-24-2008 12:02 PM

Utility to ensure appropriate headers were included
 

Recently, there was a Linux program distributed as source code, and it
compiled fine on the majority of systems. However on some systems, it
failed to compile. On some of these systems, people were getting
errors for undeclared tokens, while others were getting linking
errors.

Anyway, the problem was that one of the source files was missing:

#include <stdio.h>

This wasn't a problem on most systems because some of the other header
files that were included actually included stdio.h.

Is there any utility out there that will process a source file and
notify you if you're depending on a declaration that isn't present in
a header file that's included directly in the source file?

jacob navia 02-24-2008 12:08 PM

Re: Utility to ensure appropriate headers were included
 
Tomás Ó hÉilidhe wrote:
> Recently, there was a Linux program distributed as source code, and it
> compiled fine on the majority of systems. However on some systems, it
> failed to compile. On some of these systems, people were getting
> errors for undeclared tokens, while others were getting linking
> errors.
>
> Anyway, the problem was that one of the source files was missing:
>
> #include <stdio.h>
>
> This wasn't a problem on most systems because some of the other header
> files that were included actually included stdio.h.
>
> Is there any utility out there that will process a source file and
> notify you if you're depending on a declaration that isn't present in
> a header file that's included directly in the source file?


Yes, that utility is called a C compiler.
Normally, all compilers will warn you about missing declarations.
The problem is that people think that compiling means just
"press 'compile' in the IDE" and ignore completely the errors
or warnings that the compiler prints.



--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Richard Heathfield 02-24-2008 01:00 PM

Re: Utility to ensure appropriate headers were included
 
jacob navia said:

> Tomás Ó hÉilidhe wrote:

<snip>
>>
>> Is there any utility out there that will process a source file and
>> notify you if you're depending on a declaration that isn't present in
>> a header file that's included directly in the source file?

>
> Yes, that utility is called a C compiler.


You appear to have mis-read the question.

> Normally, all compilers will warn you about missing declarations.


He's not talking about missing declarations. He's talking about
declarations that depend on a header that is included indirectly by
another header.

> The problem is that people think that compiling means just
> "press 'compile' in the IDE" and ignore completely the errors
> or warnings that the compiler prints.


No, that isn't the problem. It's *a* problem, but it's not *this* problem.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Harald van Dijk 02-24-2008 02:11 PM

Re: Utility to ensure appropriate headers were included
 
On Sun, 24 Feb 2008 04:02:31 -0800, Tomás Ó hÉilidhe wrote:
> Recently, there was a Linux program distributed as source code, and it
> compiled fine on the majority of systems. However on some systems, it
> failed to compile. On some of these systems, people were getting errors
> for undeclared tokens, while others were getting linking errors.
>
> Anyway, the problem was that one of the source files was missing:
>
> #include <stdio.h>
>
> This wasn't a problem on most systems because some of the other header
> files that were included actually included stdio.h.
>
> Is there any utility out there that will process a source file and
> notify you if you're depending on a declaration that isn't present in a
> header file that's included directly in the source file?


What exactly do you mean?

a.c:
#include <stdio.h>
int myputchar(char c) { return putchar(c); }

b.c:
#include "b.h"
int myputchar(char c) { return putchar(c); }

b.h:
#include <stdio.h>

c.c:
#include <curses.h>
int myputchar(char c) { return putchar(c); }

For which files do you want a notification? I'm guessing you want none for
a, and one for c. I don't know about b. However, what if <stdio.h> doesn't
define putchar, but instead includes a non-standard header which does? On
such a system (mine happens to declare putchar in <stdio.h> and define it
in <bits/stdio.h>, but the problem does exist for several other standard
library headers) there's really no difference between a and c that any
tool could tell without special built-in knowledge. So what do you want
the tool to do?

CBFalconer 02-24-2008 02:36 PM

Re: Utility to ensure appropriate headers were included
 
Tomás Ó hÉilidhe wrote:
>

.... snip ...
>
> Is there any utility out there that will process a source file and
> notify you if you're depending on a declaration that isn't present
> in a header file that's included directly in the source file?


I don't see the problem. When the compiler goes through its
linking phase it will (normally) announce the identity of any
unfound routines or objects. These indicate the absence of some
library or object module. If the missing item is in the standard
library it should have been found already, unless it is in the math
library. Otherwise the thing missing is your own code. Write it.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com


Richard Tobin 02-24-2008 05:01 PM

Re: Utility to ensure appropriate headers were included
 
In article <5649c69f-79d8-4e6f-a9a8-181b86a0f35c@q33g2000hsh.googlegroups.com>,
Tomás Ó hÉilidhe <toe@lavabit.com> wrote:

>Recently, there was a Linux program distributed as source code, and it
>compiled fine on the majority of systems. However on some systems, it
>failed to compile. On some of these systems, people were getting
>errors for undeclared tokens, while others were getting linking
>errors.
>
>Anyway, the problem was that one of the source files was missing:
>
>#include <stdio.h>
>
>This wasn't a problem on most systems because some of the other header
>files that were included actually included stdio.h.


Presumably you mean that one of the system headers included stdio.h,
because if it was one of the program's headers this wouldn't be a
problem on "some systems".

I'm not sure to what extent this is permitted for standard C headers.
Presumably it's allowed, because all the identifiers they declare are
reserved. Perhaps someone can quote chapter and verse on this.

>Is there any utility out there that will process a source file and
>notify you if you're depending on a declaration that isn't present in
>a header file that's included directly in the source file?


I don't think that's possible, since it's perfectly legal for, say,
stdio.h to work by including some other system headers that declares
printf().

-- Richard



--
:wq

Harald van Dijk 02-24-2008 05:13 PM

Re: Utility to ensure appropriate headers were included
 
On Sun, 24 Feb 2008 17:01:13 +0000, Richard Tobin wrote:
> In article
> <5649c69f-79d8-4e6f-a9a8-181b86a0f35c@q33g2000hsh.googlegroups.com>,
> Tomás Ó hÉilidhe <toe@lavabit.com> wrote:
>>Recently, there was a Linux program distributed as source code, and it
>>compiled fine on the majority of systems. However on some systems, it
>>failed to compile. On some of these systems, people were getting errors
>>for undeclared tokens, while others were getting linking errors.
>>
>>Anyway, the problem was that one of the source files was missing:
>>
>>#include <stdio.h>
>>
>>This wasn't a problem on most systems because some of the other header
>>files that were included actually included stdio.h.

>
> Presumably you mean that one of the system headers included stdio.h,
> because if it was one of the program's headers this wouldn't be a
> problem on "some systems".
>
> I'm not sure to what extent this is permitted for standard C headers.


Not at all, except in specific cases under the as-if rule.

> Presumably it's allowed, because all the identifiers they declare are
> reserved. Perhaps someone can quote chapter and verse on this.


Most aren't reserved (except as external identifiers) unless the header is
included; part of 7.1.3p1 is:
"Each identifier with file scope listed in any of the following subclauses
(including the future library directions) is reserved for use as a macro
name and as an identifier with file scope in the same name space if any
of its associated headers is included."

This is a strictly conforming program:

#include <stdlib.h>
static int puts = 3;
int main(void) {
return puts - 3;
}

and it wouldn't work if <stdlib.h> includes <stdio.h>.

>>Is there any utility out there that will process a source file and
>>notify you if you're depending on a declaration that isn't present in a
>>header file that's included directly in the source file?

>
> I don't think that's possible, since it's perfectly legal for, say,
> stdio.h to work by including some other system headers that declares
> printf().


Right.

Tomás Ó hÉilidhe 02-24-2008 06:28 PM

Re: Utility to ensure appropriate headers were included
 

> Presumably you mean that one of the system headers included stdio.h,
> because if it was one of the program's headers this wouldn't be a
> problem on "some systems".



Not necessarily. The code could have used some other shared headers,
something like:

#include <openssl/decrypt.h>

The newer version of "decrypt.h" might not include <stdio.h>.

Keith Thompson 02-24-2008 06:54 PM

Re: Utility to ensure appropriate headers were included
 
"Tomás Ó hÉilidhe" <toe@lavabit.com> writes:
> Recently, there was a Linux program distributed as source code, and it
> compiled fine on the majority of systems. However on some systems, it
> failed to compile. On some of these systems, people were getting
> errors for undeclared tokens, while others were getting linking
> errors.
>
> Anyway, the problem was that one of the source files was missing:
>
> #include <stdio.h>
>
> This wasn't a problem on most systems because some of the other header
> files that were included actually included stdio.h.
>
> Is there any utility out there that will process a source file and
> notify you if you're depending on a declaration that isn't present in
> a header file that's included directly in the source file?


As others have said, that's a tough problem.

Assume the following:

<sys/foo.h> has "#include <stdio.h>"
<stdio.h> has "#include <sys/puts.h>"
<sys/puts.h> has the declaration of puts; <stdio.h> does not

Your program that calls puts should definitely have
"#include <stdio.h>", not "#include <sys/puts.h>"; your proposed tool
would flag that as an error.

Your program that happens to use <sys/foo.h> and that calls puts can
get away with omitting the "#include <stdio.h>" on this particular
system, but it's not portable.

What you want to find is not which header directly declares a given
function, but which header *should* be #included by a program that
calls that function (and likewise for things other than function
declarations). This information is not available in any obvious way
by examining the system header files themselves.

But if some kind of database were built that said:

For puts, use <stdio.h>
For foo, use <sys/foo.h>
...

then a tool could use that information to diagnose the kind of problem
you're talking about. Such a tool could also diagnose unnecessary
#include directives (for example, a program that uses malloc doesn't
need "#include <malloc.h>" even if the implementation happens to
provide it as a non-standard header).

I've never heard of such a tool, but it could be useful.

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer 02-25-2008 02:11 AM

Re: Utility to ensure appropriate headers were included
 
Harald van D?k wrote:
> Richard Tobin wrote:
>

.... snip ...
>
>> I'm not sure to what extent this is permitted for standard C
>> headers.

>
> Not at all, except in specific cases under the as-if rule.
>
>> Presumably it's allowed, because all the identifiers they
>> declare are reserved. Perhaps someone can quote chapter and
>> verse on this.

>
> Most aren't reserved (except as external identifiers) unless
> the header is included; part of 7.1.3p1 is:
>
> "Each identifier with file scope listed in any of the following
> subclauses (including the future library directions) is
> reserved for use as a macro name and as an identifier with file
> scope in the same name space if any of its associated headers
> is included."
>
> This is a strictly conforming program:
>
> #include <stdlib.h>
> static int puts = 3;
> int main(void) {
> return puts - 3;
> }
>
> and it wouldn't work if <stdlib.h> includes <stdio.h>.


The actual verbiage (from N1256, since it has an extra paragraph
over N859) is:

7.1.3 Reservedidentifiers
1 Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines
identifiers listed in its associated future library directions
subclause and identifiers which are always reserved either for
anyuse or for use as file scope identifiers.

— All identifiers that begin with an underscore and either an
uppercase letter or another underscore are always reserved
for anyuse.
— All identifiers that begin with an underscore are always
reserved for use as identifiers with file scope in both the
ordinary and tag name spaces.
— Each macro name in any of the following subclauses
(including the future library directions) is reserved for
use as specified if any of its associated headers is
included; unless explicitly stated otherwise (see 7.1.4).
— All identifiers with external linkage in any of the
following subclauses (including the future library
directions) are always reserved for use as identifiers with
external linkage.
— Each identifier with file scope listed in any of the
following subclauses (including the future library
directions) is reserved for use as a macro name and as an
identifier with file scope in the same name space if any of
its associated headers is included.

And, as I read this, any identifiers declared in any standard
library section are reserved everywhere. This does not mean that
your program will not work, just that it MAY not work, and is NOT
strictly conforming.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com



All times are GMT. The time now is 02:18 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.