Skip to contentSkip to conversation

#pragma once (was Re: incoming)

DISCUSSION Alexey Dobriyan 4 participants 5 messages Feb 26, 2021 – Mar 2, 2021

lore
Share & tools
lore
#0 lore raw
Quoted 5 lines · Linus

I'm hoping to just do -rc1 this weekend after all - despite my late start due to loss of power for several days.

I'll allow late stragglers with good reason through, but the fewer of those there are, the better, of course.

I want to sent treewide "#pragma once" conversion:

	18092 files changed, 18883 insertions(+), 99841 deletions(-)

ideally it is done as 1 commit avoiding my death by thousand cuts of going through all of maintainers.

There may be a compile failure or two but the list of exceptions is stable.

Opinions? :-)

#1 lore raw
Quoted 2 lines · Alexey Dobriyan

I want to sent treewide "#pragma once" conversion:

Are there *any* advantages to it?

It's non-standard, and the historical argument for it ("it can reduce compile times because the preprocessor doesn't open the file twice" is pure and utter hogwash. Any preprocessor worth its salt does the same thing for the standard and traditional #ifndef/#define guard sequence.

Honestly, "#pragma once" was always a hack for bad preprocessors that weren't smart enough to just figure it out from the regular guarding macros.

I can't imagine that any preprocessor that incompetent exists any more, and if i does, we sure shouldn't be using it.

So #pragma once seems to have no actual advantages.

Linus

#2 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 2 lines · Alexey Dobriyan

I want to sent treewide "#pragma once" conversion:

Are there *any* advantages to it?

It's non-standard,

It is effectively standard:
https://en.wikipedia.org/wiki/Pragma_once#Portability

and I'll spare UAPI headers from conversion.

Quoted 5 lines

and the historical argument for it ("it can reduce
compile times because the preprocessor doesn't open the file twice" is pure and utter hogwash. Any preprocessor worth its salt does the same thing for the standard and traditional #ifndef/#define guard sequence.

Quoted 8 lines

Honestly, "#pragma once" was always a hack for bad preprocessors that weren't smart enough to just figure it out from the regular guarding macros.

I can't imagine that any preprocessor that incompetent exists any more, and if i does, we sure shouldn't be using it.

So #pragma once seems to have no actual advantages.

The advantage is removing busywork that is include guards.

There are rules and schemes about how to create guard macro.

Should it be prefixed by underscore?
Should it be prefixed by two underscores?
Should it be full path uppercased or just last path component? Should the guard macro be lowercased?
Should it be changed when header is moved?
Should trailing #endif contain comment?
Should #define be just #define or "#define FOO 1"?

I've even seen advice (or an IDE doing that) that is should contain timestamp of a header creation time to minimise collisions (implying collisions could happen as could typos as could broken guards)

All this zoo of styles and made up mental work is completely avoided by using #pragma once:

1) put #pragma once on the first line

or

2) put #pragma once on the second line after SPDX banner

and that's it.

No fuss, no filled up preprocessor hashtables, no implicit arguing about styles. And way less LOC:

	18092 files changed, 18883 insertions(+), 99841 deletions(-)

Now if old school header guard is necessary it can be used like in good old times.

Nobody would miss include guards.

#3 lore raw

From: Alexey Dobriyan <[email protected]>

Quoted 5 lines

Sent: 26 February 2021 23:02

Quoted 2 lines · Linus Torvalds
Quoted 1 line · Alexey Dobriyan
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I want to sent treewide "#pragma once" conversion:

Are there *any* advantages to it?

It's non-standard,

It is effectively standard:
https://en.wikipedia.org/wiki/Pragma_once#Portability

At the top of the page....

In the C and C++ programming languages, pragma once is a non-standard but widely supported

So non-standard :-)

And #pragma is ugly...

There are also times when an include guard has to be set in order to stop a problematic inner file being included.
Perhaps only during hacking, but it is useful.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#4 lore raw
In reply to Alexey Dobriyan
Quoted 9 lines · Alexey Dobriyan

There are rules and schemes about how to create guard macro.

Should it be prefixed by underscore?
Should it be prefixed by two underscores?
Should it be full path uppercased or just last path component? Should the guard macro be lowercased?
Should it be changed when header is moved?
Should trailing #endif contain comment?
Should #define be just #define or "#define FOO 1"?

Who cares? To all of the above, really.

Quoted 3 lines

I've even seen advice (or an IDE doing that) that is should contain timestamp of a header creation time to minimise collisions (implying collisions could happen as could typos as could broken guards)

Ever seen that in the tree? Where, if so?

Quoted 5 lines

All this zoo of styles and made up mental work is completely avoided by using #pragma once:

1) put #pragma once on the first line

Quoted 8 lines

or

2) put #pragma once on the second line after SPDX banner

and that's it.

No fuss, no filled up preprocessor hashtables, no implicit arguing about styles.

Care to provide some stats on the amount of those macros encountered on build, along with the total amount of defines parsed and hashed? It's noise.

And what is "implicit arguing", BTW? I'm yet to see any fights along those lines - you are the first one to bring that up, AFAICS. Maybe I'd simply been lucky until now, of course, but...