C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Revising #pragma once

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 23 Sep 2024 20:00:50 +0200
pon., 23 wrz 2024 o 12:55 Gašper Ažman via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
> HASH DOES NOT WORK. There are reasons for this in this thread, but they boil down to this: you'd need to hash the entire reachable filesystem because #include's don't resolve the same way depending on where the file is accessed from on the filesystem.
>
> #pragma once
> #include "utils.h"
>
> You'll see that header verbatim several times for different libraries in a translation unit and you'll miscompile. HASH DOES NOT WORK.
>

You mention that you include the same header from different vendors,
and both should be the same. What if they are not?
Image "same" file but someone bump license dates like:

```
// Foo 1995-2023
#pragma once
void foo(int);
```

and another files is:

```
// Foo 1995-2024
#pragma once

/// This function is very useful
void foo(int a);
```

Should these files be considered the same or different?
Include guard could work in case like this (unit someone add some
breaking changes)
but any solution for `#pragma once` will not work as all things are diffrent:
* create date
* paths
* content

You would need to compile the whole header to be sure that these
are the same files and only one copy should be included.


> On Mon, Sep 23, 2024 at 10:28 AM Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> On Mon, Sep 23, 2024 at 9:04 AM Ville Voutilainen wrote:
>> >
>> > That's completely irrelevant, the build machine's newline handling can
>> > be whatever, as the content hash doesn't
>> > need to be the same on different machines.
>>
>>
>> I meant something like the following:
>>
>> Leonardo has his Apple laptop, and he checks out code to
>> \\192.168.1.62\code\project1.
>> Donatello has his MS-Windows laptop, and he checks out code to
>> \\192.168.1.62\code\project2.
>>
>> So Leonardo and Donatello have all of their code checked out to the
>> same network drive.
>>
>> In Donatello's project, he has a source file containing the line:
>>
>> #include "../project1/monkey.hpp"
>>
>> This header file has been checked out by Leonardo with Apple line
>> endings. But Donatello also has his own copy of "monkey.hpp" in his
>> own checkout, and one of Donatello's source files looks like this:
>>
>> #include "../project1/monkey.hpp"
>> #include "common//monkey.hpp"
>>
>> So the first directive gets the header file with "\r\n" line endings,
>> and the second directive gets it with "\n" line endings. And so if
>> we're going to be taking the hash of file contents, I think best to
>> normalise the line endings first (and also to remove trailing new line
>> characters).
>>
>> Taking the hash of file contents might have too great an impact on
>> compile times though. If compile time weren't an issue though, then I
>> think taking the hash of file contents would be the optimal way to do
>> "#pragma once".
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-09-23 18:01:09