Skip to content

Commit

Permalink
feat(iwyu): Organize includes with include-what-you-use
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Mar 3, 2021
1 parent b54cd07 commit dc8239f
Show file tree
Hide file tree
Showing 63 changed files with 266 additions and 151 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/linting.yml
@@ -0,0 +1,40 @@
name: Linting
on: [push, pull_request]

jobs:
build:
name: Include What You Use
runs-on: ubuntu-20.04
steps:
- name: Checkout source
uses: actions/checkout@v2

- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
- name: Install dependencies
run: |
sudo apt-get remove -y --purge man-db # avoid time-consuming trigger
sudo add-apt-repository ppa:gjasny/iwyu
sudo apt-get update
sudo apt-get install -y clang-11 iwyu libbenchmark-dev libcurl4-openssl-dev ninja-build zlib1g-dev
- name: "CMake Configure"
run: cmake -GNinja -DRUN_IWYU=ON -S ${{ github.workspace }} -B ${{ github.workspace }}/_build

- name: Build
run: cmake --build ${{ github.workspace }}/_build 2>&1 | tee ${{ github.workspace }}/output.txt

- name: Check build output
run: if egrep -q 'should (add|remove) these lines' ${{ github.workspace }}/output.txt; then exit 1; fi

#- name: "CMake Configure"
# run: cmake -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -S ${{ github.workspace }} -B ${{ github.workspace }}/_build

#- name: "Run IWYU"
# run: iwyu_tool -p ${{ github.workspace }}/_build core push pull -- -Xiwyu --mapping_file=${{ github.workspace }}/cmake/googletest.imp -Xiwyu --no_fwd_decls 2>&1 | tee ${{ github.workspace }}/output.txt

20 changes: 20 additions & 0 deletions CMakeLists.txt
Expand Up @@ -20,6 +20,7 @@ option(ENABLE_TESTING "Build tests" ON)
option(USE_THIRDPARTY_LIBRARIES "Use 3rdParty submodules" ON)
option(THIRDPARTY_CIVETWEB_WITH_SSL "Enable SSL support for embedded civetweb source code")
option(OVERRIDE_CXX_STANDARD_FLAGS "Force building with -std=c++11 even if the CXXLFAGS are configured differently" ON)
option(RUN_IWYU "Run include-what-you-use" OFF)

if(OVERRIDE_CXX_STANDARD_FLAGS)
set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -53,6 +54,24 @@ endif()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads)

# include-what-you-use

if(RUN_IWYU)
find_program(IWYU_EXECUTABLE NAMES include-what-you-use iwyu)
if(NOT IWYU_EXECUTABLE)
message(FATAL_ERROR "Include-what-you-use not found")
endif()

set(IWYU_ARGS
"${IWYU_EXECUTABLE}"
"-Xiwyu" "--no_fwd_decls"
"-Xiwyu" "--mapping_file=${CMAKE_CURRENT_SOURCE_DIR}/cmake/googletest.imp"
)

set(CMAKE_C_INCLUDE_WHAT_YOU_USE ${IWYU_ARGS})
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU_ARGS})
endif()

# check for required libatomic

include(CheckAtomic)
Expand Down Expand Up @@ -148,4 +167,5 @@ include(FeatureSummary)
add_feature_info("Pull" "${ENABLE_PULL}" "support for pulling metrics")
add_feature_info("Push" "${ENABLE_PUSH}" "support for pushing metrics to a push-gateway")
add_feature_info("Compression" "${ENABLE_COMPRESSION}" "support for zlib compression of metrics")
add_feature_info("IYWU" "${RUN_IWYU}" "include-what-you-use")
feature_summary(WHAT ALL)
5 changes: 5 additions & 0 deletions cmake/civetweb-3rdparty-config.cmake
Expand Up @@ -60,3 +60,8 @@ if(BUILD_SHARED_LIBS)
VISIBILITY_INLINES_HIDDEN ON
)
endif()

set_target_properties(civetweb PROPERTIES
C_INCLUDE_WHAT_YOU_USE ""
CXX_INCLUDE_WHAT_YOU_USE ""
)
7 changes: 6 additions & 1 deletion cmake/googlemock-3rdparty-config.cmake
Expand Up @@ -8,7 +8,7 @@ add_library(gmock_main STATIC EXCLUDE_FROM_ALL
${_IMPORT_PREFIX}/googlemock/src/gmock_main.cc
)

target_include_directories(gmock_main
target_include_directories(gmock_main SYSTEM
PUBLIC
${_IMPORT_PREFIX}/googletest/include
${_IMPORT_PREFIX}/googlemock/include
Expand All @@ -22,3 +22,8 @@ target_link_libraries(gmock_main
Threads::Threads
)
add_library(GTest::gmock_main ALIAS gmock_main)

set_target_properties(gmock_main PROPERTIES
C_INCLUDE_WHAT_YOU_USE ""
CXX_INCLUDE_WHAT_YOU_USE ""
)
4 changes: 4 additions & 0 deletions cmake/googletest.imp
@@ -0,0 +1,4 @@
[
{ include: [ "@<gmock/.*>", private, "<gmock/gmock.h>", public ] },
{ include: [ "@<gtest/.*>", private, "<gtest/gtest.h>", public ] }
]
65 changes: 0 additions & 65 deletions cmake/project-import/sample_client.cc

This file was deleted.

1 change: 1 addition & 0 deletions cmake/project-import/sample_client.cc
4 changes: 2 additions & 2 deletions core/benchmarks/benchmark_helpers.cc
Expand Up @@ -3,10 +3,10 @@
#include <algorithm>
#include <cstdlib>

std::string GenerateRandomString(size_t length) {
std::string GenerateRandomString(std::size_t length) {
auto randchar = []() -> char {
const char charset[] = "abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
const std::size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
Expand Down
3 changes: 2 additions & 1 deletion core/benchmarks/benchmark_helpers.h
@@ -1,8 +1,9 @@
#pragma once

#include <cstddef>
#include <map>
#include <string>

std::string GenerateRandomString(size_t length);
std::string GenerateRandomString(std::size_t length);
std::map<std::string, std::string> GenerateRandomLabels(
std::size_t number_of_labels);
8 changes: 6 additions & 2 deletions core/benchmarks/counter_bench.cc
@@ -1,6 +1,10 @@
#include <benchmark/benchmark.h>
#include <prometheus/counter.h>
#include <prometheus/registry.h>

#include <string>

#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"

static void BM_Counter_Increment(benchmark::State& state) {
using prometheus::BuildCounter;
Expand Down
8 changes: 6 additions & 2 deletions core/benchmarks/gauge_bench.cc
@@ -1,6 +1,10 @@
#include <benchmark/benchmark.h>
#include <prometheus/gauge.h>
#include <prometheus/registry.h>

#include <string>

#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/registry.h"

static void BM_Gauge_Increment(benchmark::State& state) {
using prometheus::BuildGauge;
Expand Down
8 changes: 6 additions & 2 deletions core/benchmarks/histogram_bench.cc
@@ -1,9 +1,13 @@
#include <benchmark/benchmark.h>
#include <prometheus/histogram.h>
#include <prometheus/registry.h>

#include <chrono>
#include <random>
#include <string>
#include <vector>

#include "prometheus/family.h"
#include "prometheus/histogram.h"
#include "prometheus/registry.h"

using prometheus::Histogram;

Expand Down
5 changes: 3 additions & 2 deletions core/benchmarks/registry_bench.cc
@@ -1,10 +1,11 @@
#include <benchmark/benchmark.h>
#include <prometheus/counter.h>
#include <prometheus/registry.h>

#include <chrono>

#include "benchmark_helpers.h"
#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"

static void BM_Registry_CreateFamily(benchmark::State& state) {
using prometheus::BuildCounter;
Expand Down
10 changes: 8 additions & 2 deletions core/benchmarks/summary_bench.cc
@@ -1,9 +1,15 @@
#include <benchmark/benchmark.h>
#include <prometheus/registry.h>
#include <prometheus/summary.h>

#include <algorithm>
#include <chrono>
#include <cmath>
#include <random>
#include <string>
#include <vector>

#include "prometheus/family.h"
#include "prometheus/registry.h"
#include "prometheus/summary.h"

using prometheus::Summary;

Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/counter.h
@@ -1,7 +1,7 @@
#pragma once

#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h"
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/metric_type.h"
Expand Down
7 changes: 5 additions & 2 deletions core/include/prometheus/detail/builder.h
Expand Up @@ -3,11 +3,14 @@
#include <map>
#include <string>

// IWYU pragma: private
// IWYU pragma: no_include "prometheus/family.h"

namespace prometheus {

template <typename T>
class Family;
class Registry;
class Family; // IWYU pragma: keep
class Registry; // IWYU pragma: keep

namespace detail {

Expand Down
2 changes: 2 additions & 0 deletions core/include/prometheus/detail/ckms_quantiles.h
Expand Up @@ -7,6 +7,8 @@

#include "prometheus/detail/core_export.h"

// IWYU pragma: private, include "prometheus/summary.h"

namespace prometheus {
namespace detail {

Expand Down
4 changes: 3 additions & 1 deletion core/include/prometheus/detail/time_window_quantiles.h
Expand Up @@ -4,9 +4,11 @@
#include <cstddef>
#include <vector>

#include "prometheus/detail/ckms_quantiles.h"
#include "prometheus/detail/ckms_quantiles.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"

// IWYU pragma: private, include "prometheus/summary.h"

namespace prometheus {
namespace detail {

Expand Down
11 changes: 5 additions & 6 deletions core/include/prometheus/family.h
@@ -1,25 +1,24 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "prometheus/check_names.h"
#include "prometheus/client_metric.h"
#include "prometheus/collectable.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/detail/utils.h"
#include "prometheus/metric_family.h"

// IWYU pragma: no_include "prometheus/counter.h"
// IWYU pragma: no_include "prometheus/gauge.h"
// IWYU pragma: no_include "prometheus/histogram.h"
// IWYU pragma: no_include "prometheus/summary.h"

namespace prometheus {

/// \brief A metric of type T with a set of labeled dimensions.
Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/gauge.h
Expand Up @@ -3,7 +3,7 @@
#include <atomic>

#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h"
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/metric_type.h"

Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/histogram.h
Expand Up @@ -4,7 +4,7 @@

#include "prometheus/client_metric.h"
#include "prometheus/counter.h"
#include "prometheus/detail/builder.h"
#include "prometheus/detail/builder.h" // IWYU pragma: export
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/metric_type.h"
Expand Down
3 changes: 1 addition & 2 deletions core/include/prometheus/registry.h
Expand Up @@ -8,7 +8,6 @@

#include "prometheus/collectable.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/family.h"
#include "prometheus/metric_family.h"

Expand All @@ -22,7 +21,7 @@ class Summary;
namespace detail {

template <typename T>
class Builder;
class Builder; // IWYU pragma: keep

}
/// \brief Manages the collection of a number of metrics.
Expand Down

0 comments on commit dc8239f

Please sign in to comment.