[llvm] [IR] Remove unused includes (NFC) (PR #114679)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 2 14:56:14 PDT 2024


kazutakahirata wrote:

> Can you share the commands you use the ensure this doesn't regress IWYU?

Sure.  I have a hacky script that post-processes the result from `clang-tidy`.

- I only handle straightforward removals.
- I keep includes of macro-oriented header files like `llvm-config.h`, `config.h`, and `Compiler.h` to avoid causing problems that my build environment might miss.
- I remove one include at a time and do a test build.

```
#!/bin/sh

set -e

# The build directory.
BUILD_DIR="$1"

# The path filter
PATH_FILTER="$2"

RESULT="$(mktemp)"

check_local_changes()
{
  echo "$(date): Checking for local changes..."
  if [ $(git diff | wc -l) != "0" ]; then
    echo "There are some uncommitted local changed."
    git diff
    exit 1
  fi
  echo "$(date): Done"
}

process_one_file()
{
  local file="$1"
  local line

  echo "$(date): Processing $file"
  "$BUILD_DIR/bin/clang-tidy" -p "$BUILD_DIR" \
    -checks=-*,misc-include-cleaner "$file" 2>&1 | \
    grep "not used directly" | \
    grep -vE " (llvm-config.h|config.h|Compiler.h) " | \
    tac | while IFS= read line ; do \
    file="$(echo $line | cut -d: -f1)"
    lineno="$(echo $line | cut -d: -f2)"
    sed -e "${lineno}d" -i "$file"
    echo "$(date): Trying:"
    git diff
    if ninja -j140 -C "$BUILD_DIR" ; then
      echo "$(date): Accepting:"
      git commit -a --amend --no-edit
    else
      echo "$(date): Rejecting:"
      git checkout .
    fi
  done
}

check_local_changes

git commit --allow-empty -m "[...] Remove unused includes (NFC)"

find | grep "$PATH_FILTER" | sort | while IFS= read line ; do \
  process_one_file "$line"
done
```


https://github.com/llvm/llvm-project/pull/114679


More information about the llvm-commits mailing list