Skip to content

Commit

Permalink
Add include-what-you-use (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Apr 14, 2024
1 parent 8a4623e commit 04cd254
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 7 deletions.
85 changes: 85 additions & 0 deletions .github/scripts/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# This file is from
# https://github.com/acts-project/acts/blob/main/CI/iwyu/filter.py
import sys
import yaml
import re
import argparse
from collections import namedtuple


Config = namedtuple(
"Config", ["remove_lines", "replace_lines", "ignore_files"], defaults=[[], [], []]
)


class State:
skip_file: bool = False


def parse_config(config: Config):
remove_lines = []
for s in config["remove_lines"]:
remove_lines.append(re.compile(s))

replace_lines = []
for s in config["replace_lines"]:
s, r = list(s.items())[0]
replace_lines.append((re.compile(s), r))

ignore_files = []
for s in config["ignore_files"]:
ignore_files.append(re.compile(s))

return Config(
remove_lines=remove_lines,
replace_lines=replace_lines,
ignore_files=ignore_files,
)


def filter(line: str, config: Config, state: State):
if state.skip_file:
if line.endswith("---\n"):
state.skip_file = False
return None

if line.endswith(" should add these lines:\n"):
for s in config.ignore_files:
if s.search(line):
state.skip_file = True
return None

for s in config.remove_lines:
if s.search(line):
return None

for s, r in config.replace_lines:
if s.search(line):
return s.sub(r, line)

return line


parser = argparse.ArgumentParser()
parser.add_argument("config")
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()

with open(args.config, "r") as config_file:
try:
config = yaml.safe_load(config_file)
config = parse_config(config)
except yaml.YAMLError as exc:
print(exc)
sys.exit(1)

with open(args.input, "r") as input_file, open(args.output, "w") as output_file:
state = State()

for line in input_file:
filtered_line = filter(line, config, state)
if filtered_line is not None:
output_file.write(filtered_line)

sys.exit(0)
23 changes: 23 additions & 0 deletions .github/scripts/filter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file is from
# https://github.com/acts-project/acts/blob/main/CI/iwyu/filter.yaml
remove_lines:
# ignore pybind11
- "^(- )?#include <pybind11/"
- "^(- )?#include [<\"]pybind11/"
# ignore python
- "^#include <abstract.h>"
- "^#include <floatobject.h>"
- "^#include <longobject.h>"
- "^#include <pyerrors.h>"

replace_lines:
- "^#include <assert\\.h>": "#include <cassert>"
- "^#include <stddef\\.h>": "#include <cstddef>"
- "^#include <math\\.h>": "#include <cmath>"
- "^#include <limits\\.h>": "#include <climits>"
- "^#include <unistd\\.h>": "#include <cunistd>"
- "^#include <stdint\\.h>": "#include <cstdint>"
- "^#include <stdlib.h>": "#include <cstdlib>"

ignore_files:
- ".*_deps/"
171 changes: 171 additions & 0 deletions .github/workflows/iwyu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
name: iwyu

on:
push:
branches:
- master
- iwyu-2
pull_request:
branches:
- master

workflow_dispatch:

concurrency:
group: iwyu-${{ github.ref }}
cancel-in-progress: true

# References
# https://github.com/official-stockfish/Stockfish/blob/master/.github/workflows/iwyu.yml
# https://github.com/acts-project/acts/actions/runs/8588671882/workflow
# https://github.com/acts-project/acts/tree/main/CI/iwyu

jobs:
iwyu:
if: github.repository_owner == 'csukuangfj' || github.repository_owner == 'k2-fsa'
permissions:
contents: write # for stefanzweifel/git-auto-commit-action to push code in repo
name: Include what you use
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Download required linux packages
shell: bash
run: |
echo "GITHUB_WORKSPACE: $GITHUB_WORKSPACE"
sudo add-apt-repository 'deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-17 main'
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt update
sudo apt install -y libclang-17-dev clang-17 libc++-17-dev
- name: Display llvm
shell: bash
run: |
ls -lh /usr/lib/*llvm*
ls -lh /usr/bin/*clang*
- name: cache-iwyu
id: cache-iwyu
uses: actions/cache@v4
with:
path: /tmp/iwyu-install
key: iwyu-install-2024-04-13

- name: Get IWYU source
if: steps.cache-iwyu.outputs.cache-hit != 'true'
uses: actions/checkout@v4
with:
repository: include-what-you-use/include-what-you-use
ref: clang_17
path: iwyu

- name: Build IWYU
if: steps.cache-iwyu.outputs.cache-hit != 'true'
shell: bash
run: |
mkdir iwyu-build iwyu-install
cmake -B iwyu-build -S iwyu -DCMAKE_PREFIX_PATH=/usr/lib/llvm-17 -DCMAKE_INSTALL_PREFIX=/tmp/iwyu-install
cmake --build iwyu-build --target install
ls -lh /tmp/iwyu-install
tree /tmp/iwyu-install
/tmp/iwyu-install/bin/include-what-you-use --version
- name: setup path
shell: bash
run: |
echo "/tmp/iwyu-install/bin" >> "$GITHUB_PATH"
- name: display include what you use version
shell: bash
run: |
include-what-you-use --version
which include-what-you-use
include-what-you-use --help
- uses: actions/upload-artifact@v4
with:
name: iwyu-install
path: /tmpiwyu-install/*

- name: Check
shell: bash
run: |
mkdir build
cd build
cmake \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
..
- name: Run iwyu_tool.py
shell: bash
run: |
python3 /tmp/iwyu-install/bin/iwyu_tool.py -p build/ -j2 | tee iwyu-output.txt
- name: Filter IWYU output
shell: bash
run: |
python3 .github/scripts/filter.py .github/scripts/filter.yaml iwyu-output.txt iwyu-filtered.txt
- name: Show IWYU output
shell: bash
run: |
cat iwyu-output.txt
- name: Show filtered IWYU output
shell: bash
run: |
cat iwyu-filtered.txt
- name: Upload IWYU output
uses: actions/upload-artifact@v4
with:
name: iwyu
path: |
iwyu-output.txt
iwyu-filtered.txt
- name: Apply iwyu
shell: bash
run: |
git status
python3 /tmp/iwyu-install/bin/fix_includes.py < iwyu-filtered.txt
rm -rf iwyu*
- name: Show diff
shell: bash
run: |
git status
git diff
git diff > a.diff
# Download a.diff
# Run
# git apply a.diff
- name: Upload IWYU output
uses: actions/upload-artifact@v4
with:
name: diff
path: |
a.diff
# https://github.com/stefanzweifel/git-auto-commit-action
- uses: stefanzweifel/git-auto-commit-action@v5
if: false
with:
file_pattern: '*.h *.cc'
commit_message: "apply iwyu changes"
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(kaldi-native-fbank CXX C)

set(KALDI_NATIVE_FBANK_VERSION "1.19.1")
set(KALDI_NATIVE_FBANK_VERSION "1.19.2")

# Disable warning about
#
Expand Down
2 changes: 2 additions & 0 deletions kaldi-native-fbank/csrc/feature-fbank.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
#include <cmath>
#include <limits>
#include <vector>
#include <utility>

#include "kaldi-native-fbank/csrc/feature-functions.h"
#include "kaldi-native-fbank/csrc/log.h"

namespace knf {

Expand Down
2 changes: 2 additions & 0 deletions kaldi-native-fbank/csrc/feature-fbank.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <map>
#include <string>
#include <vector>
#include <cstdint>
#include <sstream>

#include "kaldi-native-fbank/csrc/feature-window.h"
#include "kaldi-native-fbank/csrc/mel-computations.h"
Expand Down
1 change: 1 addition & 0 deletions kaldi-native-fbank/csrc/feature-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cmath>
#include <limits>
#include <vector>
#include <cstddef>

#include "kaldi-native-fbank/csrc/kaldi-math.h"

Expand Down
1 change: 1 addition & 0 deletions kaldi-native-fbank/csrc/feature-window.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sstream>
#include <string>
#include <vector>
#include <cstdint>

#include "kaldi-native-fbank/csrc/log.h"

Expand Down
2 changes: 2 additions & 0 deletions kaldi-native-fbank/csrc/kaldi-math.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include "kaldi-native-fbank/csrc/kaldi-math.h"

#if defined(_POSIX_THREAD_SAFE_FUNCTIONS)
#include <mutex> // NOLINT
#endif

namespace knf {

Expand Down
2 changes: 2 additions & 0 deletions kaldi-native-fbank/csrc/mel-computations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

#include "kaldi-native-fbank/csrc/mel-computations.h"

#include <stdio.h>
#include <algorithm>
#include <sstream>
#include <vector>

#include "kaldi-native-fbank/csrc/feature-window.h"
#include "kaldi-native-fbank/csrc/log.h"

namespace knf {

Expand Down
3 changes: 3 additions & 0 deletions kaldi-native-fbank/csrc/mel-computations.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
#include <string>
#include <utility>
#include <vector>
#include <cstdint>
#include <sstream>

#include "kaldi-native-fbank/csrc/feature-window.h"

namespace knf {
struct FrameExtractionOptions;

struct MelBanksOptions {
int32_t num_bins = 25; // e.g. 25; number of triangular bins
Expand Down
1 change: 1 addition & 0 deletions kaldi-native-fbank/csrc/online-feature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <algorithm>
#include <utility>
#include <vector>
#include <cstddef>

#include "kaldi-native-fbank/csrc/feature-window.h"
#include "kaldi-native-fbank/csrc/log.h"
Expand Down
1 change: 1 addition & 0 deletions kaldi-native-fbank/csrc/online-feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "kaldi-native-fbank/csrc/feature-fbank.h"
#include "kaldi-native-fbank/csrc/whisper-feature.h"
#include "kaldi-native-fbank/csrc/feature-window.h"

namespace knf {

Expand Down
2 changes: 2 additions & 0 deletions kaldi-native-fbank/csrc/rfft.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define KALDI_NATIVE_FBANK_CSRC_RFFT_H_

#include <memory>
#include <cstdint>

namespace knf {

Expand Down Expand Up @@ -48,6 +49,7 @@ class Rfft {

private:
class RfftImpl;

std::unique_ptr<RfftImpl> impl_;
};

Expand Down
Loading

0 comments on commit 04cd254

Please sign in to comment.