public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "piotrwn1 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/110092] Missing warning that internal header is used
Date: Mon, 05 Jun 2023 09:13:17 +0000	[thread overview]
Message-ID: <bug-110092-4-kUHSt5USVw@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110092-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110092

--- Comment #5 from Piotr Nycz <piotrwn1 at gmail dot com> ---
(In reply to Jonathan Wakely from comment #2)
> (In reply to Piotr Nycz from comment #0)
> > So, probably it is doable to add warning like: "bist/shared_ptr.h is an
> > internal header file, included by other library headers. Do not attempt to
> > use it directly. Use <memory> instead"
> 
> We could add this to <bits/shared_ptr.h>:
> 
> #ifndef _GLIBCXX_MEMORY
> # error Do not include <bits/shared_ptr.h> directly, include <memory>
> instead.
> #endif
> 
> But then you'd get an error when you include <regex>, or <filesystem>, or
> <chrono>, or any of the other headers that use std::shared_ptr internally.
> 
> So we'd have to do:
> 
> #if !defined _GLIBCXX_MEMORY && !defined _GLIBCXX_CHRONO && !defined
> _GLIBCXX_REGEX \
>     && ! defined ...
> 
> And that would be a pain to maintain.
> 
> And then it would still not give an error for this, even though it's still
> wrong:
> 
> #include <chrono>
> #include <bits/shared_ptr.h>
> std::shared_ptr<int> p;
> 
> So I don't think this can really be solved in the compiler without a lot of
> work to hardcode special handling for each of those headers.

After few days this problem was sitting in my head - I think I found easy
solution to std library and all other libs with similar problems of how to
prevent users from including internal headers.

1. Define at the beginning of each interface header some macro/symbol, undef it
at the end of each file.
2. In every internal header - make sure this symbol is defined, otherwise
#error (I would prefer #warning in this case - just in case somebody do include
this internal header already for some reasons)

An example:

```
//@file memory

#ifndef GCC_STD_LIBRARY_HEADER
#define GCC_STD_LIBRARY_HEADER 1
#else
#define GCC_STD_LIBRARY_MEMORY_HEADER_IS_NOT_FRONT_HEADER 1
#endif

...
#include <bits/shared_ptr.h>

...

#ifdef GCC_STD_LIBRARY_MEMORY_HEADER_IS_NOT_FRONT_HEADER
#undef GCC_STD_LIBRARY_MEMORY_HEADER_IS_NOT_FRONT_HEADER
#else
#undef GCC_STD_LIBRARY_HEADER
#endif


```

```
//@file bits/shared_ptr.h

#if !defined(GCC_STD_LIBRARY_HEADER) &&
!defined(I_KNOW_INCLUDING_BITS_SHARED_PTR_H_IS_NOT_SUPPORTED_BY_STDLIB_BUT_I_NEED_TO_DO_IT)
#warning bits/shared_ptr.h is ... internal ..., include <memory> instead"
#endif

...

```

      parent reply	other threads:[~2023-06-05  9:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-02 12:41 [Bug c++/110092] New: " piotrwn1 at gmail dot com
2023-06-02 12:57 ` [Bug c++/110092] " redi at gcc dot gnu.org
2023-06-02 14:14 ` redi at gcc dot gnu.org
2023-06-02 14:45 ` piotrwn1 at gmail dot com
2023-06-02 22:34 ` pinskia at gcc dot gnu.org
2023-06-05  9:13 ` piotrwn1 at gmail dot com [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110092-4-kUHSt5USVw@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).