Skip to content

Commit

Permalink
Bump iwyu to 0.19 and dirty fix angle-bracket-curse
Browse files Browse the repository at this point in the history
  • Loading branch information
storypku committed Dec 12, 2022
1 parent 94c6e95 commit a795772
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<version>",
sha256 = "<sha256sum>",
urls = [
"https://github.com/storypku/bazel_iwyu/archive/0.0.5.tar.gz",
"https://github.com/storypku/bazel_iwyu/archive/<version>.tar.gz",
],
)

Expand Down
11 changes: 3 additions & 8 deletions bazel/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
)

Expand Down
51 changes: 51 additions & 0 deletions internal/README.md
Original file line number Diff line number Diff line change
@@ -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.
100 changes: 100 additions & 0 deletions internal/angle_quote_curse_dirty_fix.patch
Original file line number Diff line number Diff line change
@@ -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<OutputLine> output_lines;

const set<string>& 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<LineSortKey, const OneIncludeOrForwardDeclareLine*> sorted_lines;
+
+ set<std::string> existing_includes;
+ set<std::string> 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");
28 changes: 28 additions & 0 deletions internal/build_iwyu.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a795772

Please sign in to comment.