From a795772d20a96591bef315912e9a08c7ae771641 Mon Sep 17 00:00:00 2001 From: Jiaming Liu Date: Mon, 12 Dec 2022 21:19:29 +0800 Subject: [PATCH] Bump iwyu to 0.19 and dirty fix angle-bracket-curse --- README.md | 6 +- bazel/dependencies.bzl | 11 +-- internal/README.md | 51 +++++++++++ internal/angle_quote_curse_dirty_fix.patch | 100 +++++++++++++++++++++ internal/build_iwyu.sh | 28 ++++++ 5 files changed, 185 insertions(+), 11 deletions(-) create mode 100644 internal/README.md create mode 100644 internal/angle_quote_curse_dirty_fix.patch create mode 100644 internal/build_iwyu.sh diff --git a/README.md b/README.md index 137dce9..096a3be 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,10 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "com_github_storypku_bazel_iwyu", - strip_prefix = "bazel_iwyu-0.0.5", - sha256 = "b4596e4f11e61c1ead77065dca8755fccff51e98a903b79ff61562c93ae7138d", + strip_prefix = "bazel_iwyu-", + sha256 = "", urls = [ - "https://github.com/storypku/bazel_iwyu/archive/0.0.5.tar.gz", + "https://github.com/storypku/bazel_iwyu/archive/.tar.gz", ], ) diff --git a/bazel/dependencies.bzl b/bazel/dependencies.bzl index 8dbb22a..3be5635 100644 --- a/bazel/dependencies.bzl +++ b/bazel/dependencies.bzl @@ -8,20 +8,15 @@ def bazel_iwyu_dependencies(): name = "iwyu_prebuilt_pkg", build_file = Label("//bazel/iwyu:BUILD.prebuilt_pkg"), urls = { - "linux-aarch64": [ - "https://github.com/storypku/bazel_iwyu/releases/download/asset/iwyu-0.18.0-aarch64-linux-gnu.tar.xz", - ], "linux-x86_64": [ - "https://github.com/storypku/bazel_iwyu/releases/download/asset/iwyu-0.18.0-x86_64-linux-gnu.tar.xz", + "https://github.com/storypku/bazel_iwyu/releases/download/0.19/iwyu-0.19-x86_64-linux-gnu.tar.xz", ], }, strip_prefix = { - "linux-aarch64": "iwyu-0.18.0-aarch64-linux-gnu", - "linux-x86_64": "iwyu-0.18.0-x86_64-linux-gnu", + "linux-x86_64": "iwyu-0.19-x86_64-linux-gnu", }, sha256 = { - "linux-aarch64": "2417fe28620b2fd55b7c8085706c85c607bc58c9cd2b74dc7859d06f633e22c8", - "linux-x86_64": "9288990cba15b9b17c649449b7ceee328e79802aa7db2a0a6e6ba815e2ea8753", + "linux-x86_64": "b6cdae11c647fd28d1608889de68fd27bb7926ed9311d6187294b9780c85c752", }, ) diff --git a/internal/README.md b/internal/README.md new file mode 100644 index 0000000..5eaf9ab --- /dev/null +++ b/internal/README.md @@ -0,0 +1,51 @@ +# Steps to Build IWYU 0.19 + + +## Install LLVM/Clang 15.x + +1. Download LLVM/Clang 15.x distribution from https://github.com/llvm/llvm-project/releases + +2. Extract and rename it to /opt/llvm after downloading. + +E.g., + +``` +sudo tar xJvf clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04.tar.gz -C /opt +sudo mv /opt/clang+llvm-15.0.6-x86_64-linux-gnu-ubuntu-18.04 /opt/llvm +``` + +## Download IWYU 0.19 + +IWYU 0.19 can be downloaded from https://github.com/include-what-you-use/include-what-you-use/releases/tag/0.19 + +## Build IWYU + +When IWYU tarball is extracted, cd into the directory, and run: + +``` +# patch python shebang for our environment (we need python3, not python) +sed -i 's,^#!/usr/bin/env python,#!/usr/bin/env python3,g' iwyu_tool.py +sed -i 's,^#!/usr/bin/env python,#!/usr/bin/env python3,g' fix_includes.py + +patch -p1 < /path/to/angle_bracket_curse_dirty_fix.patch +``` + +then build it with `build_iwyu.sh`. + +To work around [IWYU Issue #100](https://github.com/include-what-you-use/include-what-you-use/issues/100#issuecomment-111944224) + +We may need to run the following commands after the `make install` step in `build_iwyu.sh`. + +``` +pushd /tmp/iwyu-0.19-linux-x86_64 +mkdir -p lib/clang/15.0.6/include +cp -r /opt/llvm/lib/clang/15.0.6/include/* \ + lib/clang/15.0.6/include/ +popd +pushd /tmp +tar czvf iwyu-0.19-linux-x86_64.tar.gz iwyu-0.19-linux-x86_64 +``` + +## About angle-bracket-curse +See [IWYU Issues on angle-bracket-curse](https://github.com/include-what-you-use/include-what-you-use/issues?q=angle+label%3Aangle-quote-curse) for details. +We should remove `angle_bracket_curse_dirty_fix.patch` once upstream has this issue fixed. diff --git a/internal/angle_quote_curse_dirty_fix.patch b/internal/angle_quote_curse_dirty_fix.patch new file mode 100644 index 0000000..8557cb3 --- /dev/null +++ b/internal/angle_quote_curse_dirty_fix.patch @@ -0,0 +1,100 @@ +diff --git a/iwyu_output.cc b/iwyu_output.cc +index 3f364ac..40f4ed5 100644 +--- a/iwyu_output.cc ++++ b/iwyu_output.cc +@@ -1986,16 +1986,24 @@ size_t PrintableDiffs(const string& filename, + vector output_lines; + + const set& aqi = associated_quoted_includes; // short alias +- + // Sort all the output-lines: system headers before user headers + // before forward-declares, etc. The easiest way to do this is to + // just put them all in multimap whose key is a sort-order (multimap + // because some headers might be listed twice in the source file.) + multimap sorted_lines; ++ ++ set existing_includes; ++ set to_add_includes; + for (const OneIncludeOrForwardDeclareLine& line : lines) { + const IwyuFileInfo* file_info = nullptr; +- if (line.IsIncludeLine()) ++ if (line.IsIncludeLine()) { + file_info = preprocessor_info->FileInfoFor(line.included_file()); ++ const std::string& unquoted_include = line.unquoted_include(); ++ existing_includes.insert(unquoted_include); ++ if (line.is_desired() && !line.is_present()) { ++ to_add_includes.insert(unquoted_include); ++ } ++ } + + sorted_lines.insert(make_pair(GetSortKey(line, aqi, file_info), &line)); + } +@@ -2005,10 +2013,27 @@ size_t PrintableDiffs(const string& filename, + bool no_adds_or_deletes = true; + for (const auto& key_line : sorted_lines) { + const OneIncludeOrForwardDeclareLine* line = key_line.second; +- if ((line->is_desired() && !line->is_present()) || // add +- (line->is_present() && !line->is_desired())) { // delete +- no_adds_or_deletes = false; +- break; ++ if (line->is_desired() && !line->is_present()) { // add ++ if (line->IsIncludeLine()) { ++ if (!ContainsKey(existing_includes, line->unquoted_include())) { ++ no_adds_or_deletes = false; ++ break; ++ } ++ } else { ++ no_adds_or_deletes = false; ++ break; ++ } ++ } ++ if (line->is_present() && !line->is_desired()) { // delete ++ if (line->IsIncludeLine()) { ++ if (!ContainsKey(to_add_includes, line->unquoted_include())) { ++ no_adds_or_deletes = false; ++ break; ++ } ++ } else { ++ no_adds_or_deletes = false; ++ break; ++ } + } + } + if (no_adds_or_deletes && !GlobalFlags().update_comments) { +@@ -2025,6 +2050,9 @@ size_t PrintableDiffs(const string& filename, + for (const auto& key_line : sorted_lines) { + const OneIncludeOrForwardDeclareLine* line = key_line.second; + if (line->is_desired() && !line->is_present()) { ++ if (line->IsIncludeLine() && ContainsKey(existing_includes, line->unquoted_include())) { ++ continue; ++ } + output_lines.push_back( + PrintableIncludeOrForwardDeclareLine(*line, aqi)); + ++num_edits; +@@ -2039,6 +2067,9 @@ size_t PrintableDiffs(const string& filename, + for (const auto& key_line : sorted_lines) { + const OneIncludeOrForwardDeclareLine* line = key_line.second; + if (line->is_present() && !line->is_desired()) { ++ if (line->IsIncludeLine() && ContainsKey(to_add_includes, line->unquoted_include())) { ++ continue; ++ } + output_lines.push_back( + PrintableIncludeOrForwardDeclareLine(*line, aqi)); + output_lines.back().add_prefix("- "); +diff --git a/iwyu_output.h b/iwyu_output.h +index 43d2cc8..2b13b75 100644 +--- a/iwyu_output.h ++++ b/iwyu_output.h +@@ -134,6 +134,12 @@ class OneIncludeOrForwardDeclareLine { + return quoted_include_; + } + ++ string unquoted_include() const { ++ CHECK_(IsIncludeLine() && "Must call unquoted_include() on include lines"); ++ CHECK_(!fwd_decl_ && "unquoted_include and fwd_decl are mutually exclusive"); ++ return quoted_include_.substr(1, quoted_include_.size() - 2); ++ } ++ + const clang::FileEntry* included_file() const { + CHECK_(IsIncludeLine() && "Must call included_file() on include lines"); + CHECK_(!fwd_decl_ && "included_file and fwd_decl are mutually exclusive"); diff --git a/internal/build_iwyu.sh b/internal/build_iwyu.sh new file mode 100644 index 0000000..b4ab58b --- /dev/null +++ b/internal/build_iwyu.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +TOP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" + +# Set BUILD_TYPE via Cmdline: +# /build_me.sh RelWithDebInfo +BUILD_TYPE="${1:-Release}" + +[[ -d "${TOP_DIR}/build" ]] || mkdir "${TOP_DIR}/build" +rm -rf "${TOP_DIR}/build"/* + +pushd "${TOP_DIR}/build" >/dev/null + +ARCH="$(uname -m)" +DEST_DIR="/tmp/iwyu-${VERSION}-linux-${ARCH}" +export CC=/opt/llvm/bin/clang +export CXX=/opt/llvm/bin/clang++ + +cmake -G "Unix Makefiles" .. \ + -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \ + -DCMAKE_PREFIX_PATH=/opt/llvm \ + -DCMAKE_VERBOSE_MAKEFILE=ON \ + -DCMAKE_INSTALL_PREFIX="${DEST_DIR}" + +make -j$(nproc) +make install + +popd >/dev/null