Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Include what you use #167

Merged
merged 1 commit into from
Aug 21, 2024
Merged

Include what you use #167

merged 1 commit into from
Aug 21, 2024

Conversation

RichLogan
Copy link
Contributor

@RichLogan RichLogan commented Aug 15, 2024

There are a couple of things to call out here:

  1. I have done this naively. We may want consumers of this API (and ourselves) to only have to include <transport/transport.h> to get all/some subset of the <transport/*.h> headers. We can add a pragma to mark those as exported that will be understood if they/us run clang-tidy or IWYU. Perhaps the existing ones transport includes already would make sense(?): cc @TimEvens
#include <transport/uintvar.h>
#include <transport/safe_queue.h>
#include <transport/transport_metrics.h>
#include <transport/stream_buffer.h>
  1. On MacOS at least, IncludeCleaner suggests some private MacOS headers that contain the actual implementations rather than the more canonical UNIX headers. E.g timeval should come from sys/time.h not sys/types/_timeval.h. IWYU allows a mapping file to correct these, but clang-tidy's version doesn't seem to. I have marked them NOLINT for now.

@RichLogan RichLogan requested a review from TimEvens August 15, 2024 13:16
@RichLogan RichLogan mentioned this pull request Aug 20, 2024
There are a couple of things to call out here:

1. I have done this naively. We may want consumers of this API to only have to include <transport/transport.h> to get all/some subset of the <transport/*.h> headers. We can add a pragma to mark those as exported, that consumers of the API and ourselves will understand if they also run clang-tidy or IWYU.
2. On MacOS at least, IncludeCleaner suggests some private MacOS headers that contain the actual implementations rather than the more canonical UNIX headers. E.g timeval should come from sys/time.h not <sys/types/_timeval.h>. IWYU allows a mapping file to correct these, but clang-tidy's version doesn't seem to. I have marked them NOLINT for now.
Copy link
Contributor

@GhostofCookie GhostofCookie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are optional, I see no reason this shouldn't move forward.

@@ -3,6 +3,10 @@
#if not defined(PLATFORM_ESP)
#include <transport_picoquic.h>
#endif
#include <memory>
#include <stdexcept>
#include <spdlog/logger.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It preferred this over

#include <spdlog/spdlog.h>

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it doesn't know about umbrella headers that don't actually define the thing directly, although we can declare them in a mapping file so it does know. Here it didn't seem too horrendous so I didn't bother. But that's the same thing that I'm raising in my comment about <transport/*.h>. We can add a pragma in that case if we want.

Comment on lines +3 to +41
// PicoQuic.
#include <picoquic.h>
#include <picoquic_internal.h>
#include <picoquic_packet_loop.h>
#include <autoqlog.h>
#include <picoquic_utils.h>
#include <spdlog/spdlog.h>

#include <picoquic_config.h>
#include <picosocks.h>

// Transport.
#include "transport/transport.h"
#include "transport/transport_metrics.h"
#include "transport/safe_queue.h"
#include "transport/stream_buffer.h"
#include "transport/priority_queue.h"
#include "transport/span.h"
#include "transport/time_queue.h"
#include <spdlog/logger.h>

// System.
#include <arpa/inet.h>
#include <sys/socket.h>
#include <cassert>
#include <cstring>
#include <ctime>
#include <iostream>
#include <thread>
#include <unistd.h>
#include <sstream>
#include <cstdint>
#include <optional>
#include <utility>
#include <memory>
#include <mutex>
#include <vector>
#include <string>
#include <chrono>
#include <sstream>
#include <stdexcept>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we run the formatter over this to sort them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, personally (though I'm sure I got this habit from somewhere vaguely official) I like to order the headers as

#include "local/header.h"

#include <dependency/header.h>

#include <system/header>

That way your dependency headers are always explicitly after your locally declared headers so that you don't run into potential include errors where you didn't include a dependency header in a local header, but that gets hidden when the dependency header comes before the local header in this implementation file. Of course IWYU fixes that issue.

Copy link
Contributor Author

@RichLogan RichLogan Aug 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah these will all get reordered when we run a format pass (#159) , and I think that's the order it'll do them it.

Google suggests order of:

Related header, C system headers, C++ standard library headers, other libraries' headers, your project's headers.

@@ -821,7 +842,7 @@ PicoQuicTransport::ConnectionContext& PicoQuicTransport::createConnContext(picoq
/*(const void*)(&((struct sockaddr_in*)addr)->sin_addr),*/
conn_ctx.peer_addr_text,
sizeof(conn_ctx.peer_addr_text));
conn_ctx.peer_port = ntohs(((struct sockaddr_in*)addr)->sin_port);
conn_ctx.peer_port = ntohs(((struct sockaddr_in*)addr)->sin_port); // NOLINT (include)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the linter mad about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nthos is declared in a weird implementation header on Mac at least, so it's asking me to include that rather than the canonical header we want. I can also fix this in a mapping file.

src/transport_udp.cpp Show resolved Hide resolved
@@ -485,7 +504,7 @@ bool UDPTransport::send_data(ConnectionContext& conn, DataContext& data_ctx, con
* - loop reads data from fd_write_queue and writes it to the socket
*/
void UDPTransport::fd_writer() {
timeval to;
timeval to; // NOLINT (include).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy, that's a lot of these comments. How do we make it happy?

@RichLogan RichLogan merged commit 90f5199 into main Aug 21, 2024
2 checks passed
@RichLogan RichLogan deleted the missing-includes branch August 21, 2024 14:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants