Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IWYU to CI #17078

Merged
merged 42 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
48d41c2
Revert "Move workflow to nightly and fix static configure parameters"
vyasr Oct 14, 2024
e057f08
Add IWYU build deps
vyasr Oct 14, 2024
0a03a4a
Add IWYU to the CMake
vyasr Oct 14, 2024
63ec03f
Build IWYU in CI so that it's available for the test run.
vyasr Oct 14, 2024
97566e2
Allow for PYTHONPATH not existing
vyasr Oct 14, 2024
7534c45
Typo
vyasr Oct 14, 2024
215f92d
Parse IWYU output and upload it as an artifact.
vyasr Oct 19, 2024
fb84527
Disable warnings as errors for clang-tidy
vyasr Oct 19, 2024
a911555
Update ci/clang_tidy.sh
vyasr Oct 19, 2024
623dbf0
Switch to custom-job's upload logic
vyasr Oct 20, 2024
4b834f5
Fix some issues with the script
vyasr Oct 21, 2024
4fa7833
Fix output so that it runs easily locally
vyasr Oct 22, 2024
3dd6262
Don't write anything for files that had no removals, otherwise applyi…
vyasr Oct 22, 2024
67bf686
Fix ext
vyasr Oct 22, 2024
5602522
Use a better name for the file
vyasr Oct 28, 2024
3ff957a
Stop running clang-tidy on tests
vyasr Oct 29, 2024
7a828ac
Generalize the CMake logic to handle different choices of linters
vyasr Oct 29, 2024
eeb1830
Add comment
vyasr Oct 29, 2024
0e83d99
Fix filename
vyasr Oct 29, 2024
927692d
Update option in run script
vyasr Oct 29, 2024
828ebd5
Add debug print
vyasr Oct 29, 2024
ea9a66c
Louder debug message and fail hard
vyasr Oct 29, 2024
c4dd9f6
One more typo
vyasr Oct 29, 2024
138e81b
Clean up and hide unnecessary warnings
vyasr Oct 29, 2024
6338020
Point back to main branch of workflows
vyasr Oct 30, 2024
f153fb0
Revert changes to test workflow
vyasr Oct 30, 2024
9c82acc
Update file key
vyasr Oct 30, 2024
07169c7
Rename script
vyasr Oct 30, 2024
a699658
Remove now invalid comment
vyasr Oct 30, 2024
8cc9714
Update comment
vyasr Oct 30, 2024
55abc36
Update comment
vyasr Oct 30, 2024
5141f86
Update script to parse and print live rather than as batch
vyasr Nov 6, 2024
81b6ed9
Update comment
vyasr Nov 6, 2024
9a8e724
Merge remote-tracking branch 'upstream/branch-24.12' into feat/iwyu
vyasr Nov 6, 2024
45c10ce
Filter out some additional output
vyasr Nov 6, 2024
20319f2
Only write output when there are removals
vyasr Nov 6, 2024
dd77e7e
Ignore a few more warnings
vyasr Nov 6, 2024
471144e
Quote flag
vyasr Nov 6, 2024
1e72f30
comment
vyasr Nov 6, 2024
02645b0
Merge branch 'branch-24.12' into feat/iwyu
vyasr Nov 7, 2024
8e186ea
Remove job from pr
vyasr Nov 7, 2024
4f07b7d
Update ci/cpp_linters.sh
vyasr Nov 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
needs:
- changed-files
- checks
# TODO: Remove this check before merging this PR and make sure the copy
# in test.yaml is up-to-date.
- clang-tidy
- conda-cpp-build
- conda-cpp-checks
- conda-cpp-tests
Expand Down Expand Up @@ -94,6 +97,13 @@ jobs:
uses: rapidsai/shared-workflows/.github/workflows/[email protected]
with:
enable_check_generated_files: false
clang-tidy:
secrets: inherit
uses: rapidsai/shared-workflows/.github/workflows/[email protected]
with:
build_type: pull-request
run_script: "ci/cpp_linters.sh"
file_to_upload: iwyu_results.txt
conda-cpp-build:
needs: checks
secrets: inherit
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ jobs:
branch: ${{ inputs.branch }}
date: ${{ inputs.date }}
sha: ${{ inputs.sha }}
run_script: "ci/clang_tidy.sh"
run_script: "ci/cpp_linters.sh"
file_to_upload: iwyu_results.txt
conda-python-cudf-tests:
secrets: inherit
uses: rapidsai/shared-workflows/.github/workflows/[email protected]
Expand Down
29 changes: 0 additions & 29 deletions ci/clang_tidy.sh

This file was deleted.

47 changes: 47 additions & 0 deletions ci/cpp_linters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
# Copyright (c) 2024, NVIDIA CORPORATION.

set -euo pipefail

rapids-logger "Create checks conda environment"
. /opt/conda/etc/profile.d/conda.sh

ENV_YAML_DIR="$(mktemp -d)"

rapids-dependency-file-generator \
--output conda \
--file-key clang_tidy \
--matrix "cuda=${RAPIDS_CUDA_VERSION%.*};arch=$(arch);py=${RAPIDS_PY_VERSION}" | tee "${ENV_YAML_DIR}/env.yaml"

rapids-mamba-retry env create --yes -f "${ENV_YAML_DIR}/env.yaml" -n clang_tidy

# Temporarily allow unbound variables for conda activation.
set +u
conda activate clang_tidy
set -u

RAPIDS_VERSION_MAJOR_MINOR="$(rapids-version-major-minor)"

source rapids-configure-sccache

# TODO: For testing purposes, clone and build IWYU. We can switch to a release
# once a clang 19-compatible version is available, which should be soon
# (https://github.com/include-what-you-use/include-what-you-use/issues/1641).
git clone https://github.com/include-what-you-use/include-what-you-use.git
vyasr marked this conversation as resolved.
Show resolved Hide resolved
pushd include-what-you-use
# IWYU's CMake build uses some Python scripts that assume that the cwd is
# importable, so support that legacy behavior.
export PYTHONPATH=${PWD}:${PYTHONPATH:-}
cmake -S . -B build -GNinja --install-prefix=${CONDA_PREFIX}
cmake --build build
cmake --install build
popd
vyasr marked this conversation as resolved.
Show resolved Hide resolved

# Run the build via CMake, which will run clang-tidy when CUDF_STATIC_LINTERS is enabled.
cmake -S cpp -B cpp/build -DCMAKE_BUILD_TYPE=Release -DCUDF_STATIC_LINTERS=ON -GNinja
cmake --build cpp/build 2>&1 | python cpp/scripts/parse_iwyu_output.py

# Remove invalid components of the path for local usage. The path below is
# valid in the CI due to where the project is cloned, but presumably the fixes
# will be applied locally from inside a clone of cudf.
sed -i 's/\/__w\/cudf\/cudf\///' iwyu_results.txt
vyasr marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions cpp/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Checks:
-clang-analyzer-optin.core.EnumCastOutOfRange,
-clang-analyzer-optin.cplusplus.UninitializedObject'

WarningsAsErrors: '*'
HeaderFilterRegex: '.*cudf/cpp/(src|include|tests).*'
WarningsAsErrors: ''
vyasr marked this conversation as resolved.
Show resolved Hide resolved
HeaderFilterRegex: '.*cudf/cpp/(src|include).*'
vyasr marked this conversation as resolved.
Show resolved Hide resolved
ExcludeHeaderFilterRegex: '.*(Message_generated.h|Schema_generated.h|brotli_dict.hpp|unbz2.hpp|cxxopts.hpp).*'
FormatStyle: none
CheckOptions:
Expand Down
58 changes: 44 additions & 14 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ option(
${DEFAULT_CUDF_BUILD_STREAMS_TEST_UTIL}
)
mark_as_advanced(CUDF_BUILD_STREAMS_TEST_UTIL)
option(CUDF_CLANG_TIDY "Enable clang-tidy checking" OFF)
option(CUDF_STATIC_LINTERS "Enable static linters during compilation" OFF)

message(VERBOSE "CUDF: Build with NVTX support: ${USE_NVTX}")
message(VERBOSE "CUDF: Configure CMake to build tests: ${BUILD_TESTS}")
Expand Down Expand Up @@ -146,8 +146,10 @@ if(NOT CUDF_GENERATED_INCLUDE_DIR)
endif()

# ##################################################################################################
# * clang-tidy configuration ----------------------------------------------------------------------
if(CUDF_CLANG_TIDY)
# * linter configuration ---------------------------------------------------------------------------
if(CUDF_STATIC_LINTERS)
# For simplicity, for now we assume that all linters can be installed into an environment where
# any linter is being run. We could relax this requirement if desired.
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
Expand All @@ -174,24 +176,48 @@ if(CUDF_CLANG_TIDY)
"clang-tidy version ${expected_clang_tidy_version} is required, but found ${LLVM_VERSION}"
)
endif()

find_program(IWYU_EXE NAMES include-what-you-use iwyu REQUIRED)
endif()

# Turn on the clang-tidy property for a target excluding the files specified in SKIPPED_FILES.
function(enable_clang_tidy target)
set(_tidy_options)
function(enable_static_checkers target)
set(_tidy_options IWYU CLANG_TIDY)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these options, or should we just always enable all linters?

set(_tidy_one_value)
set(_tidy_multi_value SKIPPED_FILES)
cmake_parse_arguments(
_TIDY "${_tidy_options}" "${_tidy_one_value}" "${_tidy_multi_value}" ${ARGN}
_LINT "${_tidy_options}" "${_tidy_one_value}" "${_tidy_multi_value}" ${ARGN}
)

if(CUDF_CLANG_TIDY)
# clang will complain about unused link libraries on the compile line unless we specify
# -Qunused-arguments.
set_target_properties(
${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};--extra-arg=-Qunused-arguments"
)
foreach(file IN LISTS _TIDY_SKIPPED_FILES)
if(CUDF_STATIC_LINTERS)
if(_LINT_CLANG_TIDY)
# clang will complain about unused link libraries on the compile line unless we specify
# -Qunused-arguments.
set_target_properties(
${target} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE};--extra-arg=-Qunused-arguments"
)
endif()
if(_LINT_IWYU)
# A few extra warnings pop up when building with IWYU. I'm not sure why, but they are not
# relevant since they don't show up in any other build so it's better to suppress them until
# we can figure out the cause. Setting this as part of CXX_INCLUDE_WHAT_YOU_USE does not
# appear to be sufficient, we must also ensure that it is set to the underlying target's CXX
# compile flags. To do this completely cleanly we should modify the flags on the target rather
# than the global CUDF_CXX_FLAGS, but this solution is good enough for now since we never run
# the linters on real builds.
foreach(_flag -Wno-missing-braces -Wno-absolute-value -Wunneeded-internal-declaration)
list(FIND CUDF_CXX_FLAGS "${flag}" _flag_index)
if(_flag_index EQUAL -1)
list(APPEND CUDF_CXX_FLAGS ${flag})
endif()
endforeach()
set(CUDF_CXX_FLAGS
"${CUDF_CXX_FLAGS}"
PARENT_SCOPE
)
set_target_properties(${target} PROPERTIES CXX_INCLUDE_WHAT_YOU_USE "${IWYU_EXE}")
endif()
foreach(file IN LISTS _LINT_SKIPPED_FILES)
set_source_files_properties(${file} PROPERTIES SKIP_LINTING ON)
endforeach()
endif()
Expand Down Expand Up @@ -771,11 +797,15 @@ set_target_properties(
INTERFACE_POSITION_INDEPENDENT_CODE ON
)

# Note: This must come before the target_compile_options below so that the function can modify the
# flags if necessary.
enable_static_checkers(
cudf SKIPPED_FILES src/io/comp/cpu_unbz2.cpp src/io/comp/brotli_dict.cpp CLANG_TIDY IWYU
)
target_compile_options(
cudf PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:${CUDF_CXX_FLAGS}>"
"$<$<COMPILE_LANGUAGE:CUDA>:${CUDF_CUDA_FLAGS}>"
)
enable_clang_tidy(cudf SKIPPED_FILES src/io/comp/cpu_unbz2.cpp src/io/comp/brotli_dict.cpp)

if(CUDF_BUILD_STACKTRACE_DEBUG)
# Remove any optimization level to avoid nvcc warning "incompatible redefinition for option
Expand Down
168 changes: 168 additions & 0 deletions cpp/scripts/parse_iwyu_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Helper script to modify IWYU output to only include removals.

Lines that are not from include-what-you-use are removed from the output.
"""

import argparse
import re
from enum import Enum


class Mode(Enum):
NORMAL = 0
ADD = 1
REMOVE = 2
FULL_INCLUDE_LIST = 3


def extract_include_file(include_line):
"""Extract the core file path from an #include directive."""
match = re.search(r'#include\s+[<"]([^">]+)[">]', include_line)
if match:
return match.group(1)
return None


def parse_output(input_stream):
include_modifications = {}
current_file = None
mode = Mode.NORMAL

for line in input_stream:
if match := re.match(r"(\/\S+) should add these lines:", line):
current_file = match.group(1)
include_modifications.setdefault(
current_file,
{
"add_includes": [],
"remove_includes": [],
"full_include_list": [],
},
)
mode = Mode.ADD
elif match := re.match(r"(\/\S+) should remove these lines:", line):
mode = Mode.REMOVE
elif match := re.match(r"The full include-list for (\/\S+):", line):
mode = Mode.FULL_INCLUDE_LIST
elif line.strip() == "---":
current_file = None
mode = Mode.NORMAL
else:
if current_file:
if mode == Mode.ADD:
include_modifications[current_file]["add_includes"].append(
line.strip()
)
elif mode == Mode.REMOVE:
include_modifications[current_file][
"remove_includes"
].append(line.strip())
elif mode == Mode.FULL_INCLUDE_LIST:
include_modifications[current_file][
"full_include_list"
].append(line.strip())
else:
if (
line.strip()
and "include-what-you-use reported diagnostics" not in line
and "In file included from" not in line
and "has correct #includes/fwd-decls" not in line
):
print(line, end="")

return include_modifications


def post_process_includes(include_modifications):
"""Deduplicate and remove redundant entries from add and remove includes."""
for mods in include_modifications.values():
# Deduplicate add_includes and remove_includes
mods["add_includes"] = list(set(mods["add_includes"]))
mods["remove_includes"] = list(set(mods["remove_includes"]))

# Extract file paths from add_includes and remove_includes
add_files = {
extract_include_file(line) for line in mods["add_includes"]
}
remove_files = {
extract_include_file(line) for line in mods["remove_includes"]
}

# Remove entries that exist in both add_includes and remove_includes
common_files = add_files & remove_files
mods["add_includes"] = [
line
for line in mods["add_includes"]
if extract_include_file(line) not in common_files
]
mods["remove_includes"] = [
line
for line in mods["remove_includes"]
if extract_include_file(line) not in common_files
]

# Remove entries that exist in add_includes from full_include_list
mods["full_include_list"] = [
include
for include in mods["full_include_list"]
if extract_include_file(include) not in add_files
]


def write_output(include_modifications, output_stream):
for filename, mods in include_modifications.items():
if mods["remove_includes"]:
output_stream.write(f"{filename} should add these lines:\n\n")
vyasr marked this conversation as resolved.
Show resolved Hide resolved

output_stream.write(f"{filename} should remove these lines:\n")
for line in mods["remove_includes"]:
output_stream.write(line + "\n")
output_stream.write("\n")

output_stream.write(f"The full include-list for {filename}:\n")
for line in mods["full_include_list"]:
output_stream.write(line + "\n")
output_stream.write("---\n")


def main():
parser = argparse.ArgumentParser(
description="Process include modifications from a build output log."
)
parser.add_argument(
"input",
nargs="?",
type=argparse.FileType("r"),
default="-",
help="Input file to read (default: stdin)",
)
parser.add_argument(
"--output",
type=argparse.FileType("w"),
default="iwyu_results.txt",
help="Output file to write (default: iwyu_output.txt)",
)
args = parser.parse_args()

include_modifications = parse_output(args.input)
post_process_includes(include_modifications)
write_output(include_modifications, args.output)


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion cpp/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ function(ConfigureTest CMAKE_TEST_NAME)
"GTEST_CUDF_STREAM_MODE=new_${_CUDF_TEST_STREAM_MODE}_default;LD_PRELOAD=$<TARGET_FILE:cudf_identify_stream_usage_mode_${_CUDF_TEST_STREAM_MODE}>"
)
endif()
enable_clang_tidy(${CMAKE_TEST_NAME})
vyasr marked this conversation as resolved.
Show resolved Hide resolved
endfunction()

# ##################################################################################################
Expand Down
Loading
Loading