From b71f9a58a9af53b097aeca2d84413544695643ea Mon Sep 17 00:00:00 2001 From: Julian Blake Kongslie Date: Sat, 25 Jun 2022 20:56:30 -0700 Subject: Include what you use. --- Makefile | 27 ++++++++++++++++++++++----- aisa/aisa.h | 5 ----- aisa/coroutine.h | 39 ++++++++++++++++++++------------------- aisa/eval.h | 4 ++++ main.cpp | 5 +++++ tools/iwyu-header | 5 +++++ 6 files changed, 56 insertions(+), 29 deletions(-) create mode 100755 tools/iwyu-header diff --git a/Makefile b/Makefile index debe4b5..9e84cc8 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,12 @@ DEBUG ?= -g AR ?= ar CXX ?= g++ +IWYU ?= iwyu +CHRONIC ?= chronic CXXFLAGS := -Wall -Werror -std=c++20 -fPIC -iquote . ${OPTIMIZE} ${DEBUG} -.DEFAULT: issim +.DEFAULT_GOAL := all VERSION := $(shell git describe --always --dirty --long --tags 2> /dev/null) ifndef VERSION @@ -21,14 +23,19 @@ std::string GIT_TAG = "$(subst ",\",${VERSION})"; endef export GITTAGCPP +ifneq ($(shell which ${IWYU}),) +iwyu = ${CHRONIC} ${IWYU} -Xiwyu --error ${CXXFLAGS} $(1) +else +$(warning Not using IWYU) +iwyu = +endif + libname = $(shell realpath --canonicalize-missing --relative-to . build/$(dir $(1))/lib$(notdir $(1))) define mklib = ifneq ($(wildcard $(1)/*.cpp),) -$$(info Component $(call libname,$(1))) - $(call libname,$(1).a): $(patsubst %.cpp,build/%.o,$(wildcard $(1)/*.cpp)) @mkdir -p $$(dir $$@) $${AR} cr $$@ $$+ @@ -42,15 +49,22 @@ PARTSOS += $(call libname,$(1).so) else -$$(info Header-only component $(1)) +$(call libname,$(1).cpp): + @mkdir -p $$(dir $$@) + @tools/iwyu-header $$(wildcard $(1)/*.h) > $$@ + @$$(call iwyu,$$@) +.PHONY: $(call libname,$(1).cpp) + +EXTRA_TARGETS += $(call libname,$(1).cpp) endif endef -PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -print -\))) +PARTS := $(patsubst ./%,%,$(shell find -mindepth 1 -type d -\( -name .\* -prune -o -name build -prune -o -name tools -prune -o -print -\))) PARTARS := PARTSOS := +EXTRA_TARGETS := $(foreach part,${PARTS},$(eval $(call mklib,${part}))) @@ -59,6 +73,8 @@ MAINOBJS += build/git-tag.o $(info ) +all: ${EXTRA_TARGETS} issim issim-static + issim: build/issim-dynamic @ln -sf $< $@ .PHONY: issim @@ -88,6 +104,7 @@ include $(shell find -type f -name \*.d) build/%.o: %.cpp @mkdir -p $(dir $@) + @$(call iwyu,$<) ${CXX} ${CXXFLAGS} -MMD -c -o $@ $< build/%.o: build/%.cpp diff --git a/aisa/aisa.h b/aisa/aisa.h index c9a215c..3c260c1 100644 --- a/aisa/aisa.h +++ b/aisa/aisa.h @@ -1,15 +1,10 @@ #pragma once -#include #include -#include -#include #include #include #include -#include "aisa/coroutine.h" - namespace aisa { using regnum_t = std::uint_fast64_t; diff --git a/aisa/coroutine.h b/aisa/coroutine.h index a456c89..333cb26 100644 --- a/aisa/coroutine.h +++ b/aisa/coroutine.h @@ -1,46 +1,47 @@ #pragma once #include +#include namespace aisa { - template struct promise; + template struct task_promise; - template struct task : public std::coroutine_handle> { - using handle = std::coroutine_handle>; - using promise_type = struct promise; + template struct task : public std::coroutine_handle> { + using handle = std::coroutine_handle>; + using promise_type = task_promise; bool await_ready() const noexcept { return handle::done(); } result_t await_resume() const noexcept; - template void await_suspend(std::coroutine_handle> h) const noexcept; + template void await_suspend(std::coroutine_handle> h) const noexcept; std::optional operator()() noexcept; }; - template<> struct task : public std::coroutine_handle> { - using handle = std::coroutine_handle>; - using promise_type = struct promise; + template<> struct task : public std::coroutine_handle> { + using handle = std::coroutine_handle>; + using promise_type = task_promise; bool await_ready() const noexcept { return handle::done(); } void await_resume() const noexcept; - template void await_suspend(std::coroutine_handle> h) const noexcept; + template void await_suspend(std::coroutine_handle> h) const noexcept; bool operator()() noexcept; }; - template struct promise { + template struct task_promise { std::coroutine_handle<> precursor; std::optional result; - promise() = default; - promise(const promise &) = delete; - task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } + task_promise() = default; + task_promise(const task_promise &) = delete; + task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } std::suspend_never initial_suspend() const noexcept { return {}; } std::suspend_always final_suspend() const noexcept { return {}; } void unhandled_exception() { } void return_value(result_t x) noexcept { result = std::move(x); } }; - template<> struct promise { + template<> struct task_promise { std::coroutine_handle<> precursor; - promise() = default; - promise(const promise &) = delete; - task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } + task_promise() = default; + task_promise(const task_promise &) = delete; + task get_return_object() noexcept { return task{std::coroutine_handle>::from_promise(*this)}; } std::suspend_never initial_suspend() const noexcept { return {}; } std::suspend_always final_suspend() const noexcept { return {}; } void unhandled_exception() { } @@ -54,7 +55,7 @@ namespace aisa { return std::move(x); } - template template void task::await_suspend(std::coroutine_handle> h) const noexcept + template template void task::await_suspend(std::coroutine_handle> h) const noexcept { h.promise().precursor = *this; } @@ -87,7 +88,7 @@ namespace aisa { handle::destroy(); } - template void task::await_suspend(std::coroutine_handle> h) const noexcept + template void task::await_suspend(std::coroutine_handle> h) const noexcept { h.promise().precursor = *this; } diff --git a/aisa/eval.h b/aisa/eval.h index 6d65520..30dc617 100644 --- a/aisa/eval.h +++ b/aisa/eval.h @@ -1,8 +1,12 @@ #pragma once #include +#include +#include +#include #include "aisa/aisa.h" +#include "aisa/coroutine.h" // IWYU pragma: export namespace aisa { diff --git a/main.cpp b/main.cpp index 6c0705c..df2aca1 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,11 @@ +#include #include #include #include +#include +#include +#include +#include #include "aisa/aisa.h" #include "aisa/eval.h" diff --git a/tools/iwyu-header b/tools/iwyu-header new file mode 100755 index 0000000..cd71456 --- /dev/null +++ b/tools/iwyu-header @@ -0,0 +1,5 @@ +#!/bin/bash + +for ARG in "$@"; do + echo "#include \"$ARG\" // IWYU pragma: associated" +done -- cgit v1.2.3