Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is an empty translation unit valid?

Reply
Thread Tools

Is an empty translation unit valid?

 
 
Chris Croughton
Guest
Posts: n/a
 
      06-28-2005
Does a translation unit have to have at least one externally visible
declaration or function definition to be valid? As I read the standard,
it doesn't: it must have at least one declaration or function definition,
but they can be static, extern or even a typedef.

6.9 External definitions
1 translation-unit:
external-declaration
translation-unit external-declaration

external-declaration:
function-definition
declaration

6.9.1 Function definitions
1 function-definition:
declaration-specifiers declarator declaration-list(opt)
compound-statement

6.7 Declarations
1 declaration:
declaration-specifiers init-declarator-listopt ;
declaration-specifiers:
storage-class-specifier declaration-specifiers(opt)
type-specifier declaration-specifiers(opt)
type-qualifier declaration-specifiers(opt)
function-specifier declaration-specifiers(opt)

6.7.1 Storage-class specifiers
1 storage-class-specifier:
typedef
extern
static
auto
register

So as I read the standard a translation unit consisting of only

typedef int fred;

would be valid, and a conforming compiler shouldn't complain about it.

Is this correct, or have I missed something?

Chris C
 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-29-2005
On Tue, 28 Jun 2005 14:09:31 +0100, Chris Croughton wrote:

....

> So as I read the standard a translation unit consisting of only
>
> typedef int fred;
>
> would be valid, and a conforming compiler shouldn't complain about it.


Yes, it is valid. However a conforming compiler can complain about
anything it likes. A translation unit with no definition having external
linkage seems to be useless, although correct. It is something that a
compiler could reasonably complain about, as long as it doesn't reject the
program.

Lawrence
 
Reply With Quote
 
 
 
 
Chris Croughton
Guest
Posts: n/a
 
      06-30-2005
On Wed, 29 Jun 2005 13:48:10 +0100, Lawrence Kirby
<(E-Mail Removed)> wrote:

> On Tue, 28 Jun 2005 14:09:31 +0100, Chris Croughton wrote:
>
> ...
>
>> So as I read the standard a translation unit consisting of only
>>
>> typedef int fred;
>>
>> would be valid, and a conforming compiler shouldn't complain about it.

>
> Yes, it is valid. However a conforming compiler can complain about
> anything it likes.


True, it can complain. However, a compiler which complains about legal
programs too much has quality of implementation problems...

> A translation unit with no definition having external
> linkage seems to be useless, although correct. It is something that a
> compiler could reasonably complain about, as long as it doesn't reject the
> program.


Well, it's useless to the compiler, not necessarily useless in the
context of a multi-target build system (where parts of the program may
be not needed for a particular build, so they are conditionally compiled
out, as in the cases I'm dealing with).

I've found that having just a 'useless' typedef does indeed shut up the
compilers I'm using (whereas having an unreferenced static identifier
causes them to complain differently). Probably because it can't
complain reasonably about unused typedefs, there are millions of them in
header files...

Chris C
 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-30-2005
On Thu, 30 Jun 2005 11:47:48 +0100, Chris Croughton wrote:

> On Wed, 29 Jun 2005 13:48:10 +0100, Lawrence Kirby
> <(E-Mail Removed)> wrote:
>
>> On Tue, 28 Jun 2005 14:09:31 +0100, Chris Croughton wrote:
>>
>> ...
>>
>>> So as I read the standard a translation unit consisting of only
>>>
>>> typedef int fred;
>>>
>>> would be valid, and a conforming compiler shouldn't complain about it.

>>
>> Yes, it is valid. However a conforming compiler can complain about
>> anything it likes.

>
> True, it can complain. However, a compiler which complains about legal
> programs too much has quality of implementation problems...


Compilers can generate a lot of warnins about technically correct code,
e.g. unused variable warnings. An unusable translation unit isn't an
unreasonable case for a warning.

>> A translation unit with no definition having external
>> linkage seems to be useless, although correct. It is something that a
>> compiler could reasonably complain about, as long as it doesn't reject the
>> program.

>
> Well, it's useless to the compiler, not necessarily useless in the
> context of a multi-target build system (where parts of the program may
> be not needed for a particular build, so they are conditionally compiled
> out, as in the cases I'm dealing with).


Unused variables can occur in similar circumstances. It may be a useful
diagnostic in other circumstances, this is just an argument for being able
to configure what warnings a compiler will generate.

> I've found that having just a 'useless' typedef does indeed shut up the
> compilers I'm using (whereas having an unreferenced static identifier
> causes them to complain differently). Probably because it can't
> complain reasonably about unused typedefs, there are millions of them in
> header files...


Yes, it would be unreasonable to warn about an unused typedef.

Lawrence
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      07-03-2005
In article <(E-Mail Removed)>,
Chris Croughton <(E-Mail Removed)> wrote:
....
>> Yes, it is valid. However a conforming compiler can complain about
>> anything it likes.

>
>True, it can complain. However, a compiler which complains about legal
>programs too much has quality of implementation problems...


Yes, but QOI is OT in clc.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro with the same name in translation unit Mark C Programming 5 09-28-2011 09:18 PM
Read-only outside translation unit Tomás Ó hÉilidhe C Programming 4 12-08-2007 02:04 PM
is there a way to tell the compiler that an object exists in adifferent translation unit? aaragon C++ 4 11-26-2007 03:35 AM
connect a source unit with an S-Video connection to a receiving unit that has an RCA composite video connection ? worth it ? OCZ Guy DVD Video 6 08-01-2004 05:44 PM
Connect a source unit DVD with an S-Video connection to a receiving unit TV that has an RCA composite video connection Worth it. OCZ Guy Computer Information 0 07-31-2004 12:29 PM



Advertisments