PHILO-HE commented on code in PR #11287: URL: https://github.com/apache/incubator-gluten/pull/11287#discussion_r2706930873
########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... + $ python iwyu_tool.py -p . + +See iwyu_tool.py -h for more details on command-line arguments. +""" + +from __future__ import print_function +import os +import re +import sys +import json +import time +import shlex +import shutil +import argparse +import tempfile +import subprocess + +CORRECT_RE = re.compile(r"^\((.*?) has correct #includes/fwd-decls\)$") +SHOULD_ADD_RE = re.compile(r"^(.*?) should add these lines:$") +ADD_RE = re.compile("^(.*?) +// (.*)$") +SHOULD_REMOVE_RE = re.compile(r"^(.*?) should remove these lines:$") +FULL_LIST_RE = re.compile(r"The full include-list for (.*?):$") +END_RE = re.compile(r"^---$") +LINES_RE = re.compile(r"^- (.*?) // lines ([0-9]+)-[0-9]+$") + + +GENERAL, ADD, REMOVE, LIST = range(4) + + +def clang_formatter(output, style): + """Process iwyu's output into something clang-like.""" + formatted = [] + + state = (GENERAL, None) + for line in output.splitlines(): + match = CORRECT_RE.match(line) + if match: + # See PR#1806 for more info + continue + match = SHOULD_ADD_RE.match(line) + if match: + state = (ADD, match.group(1)) + continue + match = SHOULD_REMOVE_RE.match(line) + if match: + state = (REMOVE, match.group(1)) + continue + match = FULL_LIST_RE.match(line) + if match: + state = (LIST, match.group(1)) + elif END_RE.match(line): + state = (GENERAL, None) + elif not line.strip(): + continue + elif state[0] == GENERAL: + formatted.append(line) + elif state[0] == ADD: + match = ADD_RE.match(line) + if match: + formatted.append( + "%s:1:1: %s: add '%s' (%s)" + % (state[1], style, match.group(1), match.group(2)) + ) + else: + formatted.append("%s:1:1: %s: add '%s'" % (state[1], style, line)) + elif state[0] == REMOVE: + match = LINES_RE.match(line) + line_no = match.group(2) if match else "1" + formatted.append( + "%s:%s:1: %s: superfluous '%s'" + % (state[1], line_no, style, match.group(1)) + ) + + return os.linesep.join(formatted) + + +DEFAULT_FORMAT = "iwyu" +FORMATTERS = { + "iwyu": lambda output: output, + "clang": lambda output: clang_formatter(output, style="error"), + "clang-warning": lambda output: clang_formatter(output, style="warning"), +} + + +if sys.platform.startswith("win"): Review Comment: Why need to consider windows? If not needed, please remove all such checks. ########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... + $ python iwyu_tool.py -p . + +See iwyu_tool.py -h for more details on command-line arguments. +""" + +from __future__ import print_function +import os +import re +import sys +import json +import time +import shlex +import shutil +import argparse +import tempfile +import subprocess + +CORRECT_RE = re.compile(r"^\((.*?) has correct #includes/fwd-decls\)$") +SHOULD_ADD_RE = re.compile(r"^(.*?) should add these lines:$") +ADD_RE = re.compile("^(.*?) +// (.*)$") +SHOULD_REMOVE_RE = re.compile(r"^(.*?) should remove these lines:$") +FULL_LIST_RE = re.compile(r"The full include-list for (.*?):$") +END_RE = re.compile(r"^---$") +LINES_RE = re.compile(r"^- (.*?) // lines ([0-9]+)-[0-9]+$") + + +GENERAL, ADD, REMOVE, LIST = range(4) + + +def clang_formatter(output, style): + """Process iwyu's output into something clang-like.""" + formatted = [] + + state = (GENERAL, None) + for line in output.splitlines(): + match = CORRECT_RE.match(line) + if match: + # See PR#1806 for more info + continue + match = SHOULD_ADD_RE.match(line) + if match: + state = (ADD, match.group(1)) + continue + match = SHOULD_REMOVE_RE.match(line) + if match: + state = (REMOVE, match.group(1)) + continue + match = FULL_LIST_RE.match(line) + if match: + state = (LIST, match.group(1)) + elif END_RE.match(line): + state = (GENERAL, None) + elif not line.strip(): + continue + elif state[0] == GENERAL: + formatted.append(line) + elif state[0] == ADD: + match = ADD_RE.match(line) + if match: + formatted.append( + "%s:1:1: %s: add '%s' (%s)" + % (state[1], style, match.group(1), match.group(2)) + ) + else: + formatted.append("%s:1:1: %s: add '%s'" % (state[1], style, line)) + elif state[0] == REMOVE: + match = LINES_RE.match(line) + line_no = match.group(2) if match else "1" + formatted.append( + "%s:%s:1: %s: superfluous '%s'" + % (state[1], line_no, style, match.group(1)) + ) + + return os.linesep.join(formatted) + + +DEFAULT_FORMAT = "iwyu" +FORMATTERS = { + "iwyu": lambda output: output, + "clang": lambda output: clang_formatter(output, style="error"), + "clang-warning": lambda output: clang_formatter(output, style="warning"), +} + + +if sys.platform.startswith("win"): + # Case-insensitive match on Windows + def normcase(s): + return s.lower() + +else: + + def normcase(s): + return s + + +def is_subpath_of(path, parent): + """Return True if path is equal to or fully contained within parent. + + Assumes both paths are canonicalized with os.path.realpath. + """ + parent = normcase(parent) + path = normcase(path) + + if path == parent: + return True + + if not path.startswith(parent): + return False + + # Now we know parent is a prefix of path, but they only share lineage if the + # difference between them starts with a path separator, e.g. /a/b/c/file + # is not a parent of /a/b/c/file.cpp, but /a/b/c and /a/b/c/ are. + parent = parent.rstrip(os.path.sep) + suffix = path[len(parent) :] + return suffix.startswith(os.path.sep) + + +def is_msvc_driver(compile_command): + """Return True if compile_command matches an MSVC CL-style driver.""" + compile_command = normcase(compile_command) + + if compile_command.endswith("cl.exe"): Review Comment: Ditto ########## .github/workflows/iwyu.yml: ########## @@ -0,0 +1,119 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +name: Include What You Use Check + +on: + pull_request: + paths: + - '.github/workflows/iwyu.yml' + - 'cpp/**/*.cc' + - 'cpp/**/*.h' + +env: + ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION: true + CCACHE_DIR: "${{ github.workspace }}/.ccache" + +concurrency: + group: ${{ github.repository }}-${{ github.head_ref || github.sha }}-${{ github.workflow }} + cancel-in-progress: true + +jobs: + build-native-lib-centos-7: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Get Ccache + uses: actions/cache/restore@v4 + with: + path: '${{ env.CCACHE_DIR }}' + key: ccache-centos7-release-default-${{github.sha}} + restore-keys: | + ccache-centos7-release-default + - name: Build Gluten native libraries + run: | + docker pull apache/gluten:vcpkg-centos-8 + docker run -v $GITHUB_WORKSPACE:/work -w /work apache/gluten:vcpkg-centos-8 bash -c " + set -e + yum install tzdata -y + source /opt/rh/gcc-toolset-11/enable + df -a + cd /work + export CCACHE_DIR=/work/.ccache + mkdir -p /work/.ccache + wget -q https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04.tar.xz + tar -xf clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04.tar.xz + rm -rf clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04.tar.xz + cd /work/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04/bin/ && ls -1 | grep -v \"^clang\" | xargs rm -rf + ln -s /lib64/libtinfo.so.6.1 /lib64/libtinfo.so.5 + export CC=/work/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04/bin/clang + export CXX=/work/clang+llvm-18.1.8-x86_64-linux-gnu-ubuntu-18.04/bin/clang++ + export CXXFLAGS=\"-stdlib=libstdc++ -I/opt/rh/gcc-toolset-11/root/usr/include/c++/11 -I/opt/rh/gcc-toolset-11/root/usr/include/c++/11/x86_64-redhat-linux\" + export LDFLAGS=\"-L/opt/rh/gcc-toolset-11/root/usr/lib64 -Wl,-rpath,/opt/rh/gcc-toolset-11/root/usr/lib64 -stdlib=libstdc++\" + cd /work + bash dev/ci-velox-buildstatic-centos-8.sh + mkdir -p /work/.m2/repository/org/apache/arrow/ + cp -r /root/.m2/repository/org/apache/arrow/* /work/.m2/repository/org/apache/arrow/ + " + - name: "Save ccache" + uses: actions/cache/save@v4 + id: ccache + with: + path: '${{ env.CCACHE_DIR }}' + key: ccache-centos7-release-default-${{github.sha}} + - uses: actions/upload-artifact@v4 + with: + name: velox-native-lib-centos-7-${{github.sha}} + path: ./cpp/build/ + if-no-files-found: error + - uses: actions/upload-artifact@v4 + with: + name: arrow-jars-centos-7-${{github.sha}} + path: .m2/repository/org/apache/arrow/ + if-no-files-found: error + + iwyu-check: + needs: build-native-lib-centos-7 + runs-on: ubuntu-22.04 + container: apache/gluten:centos-8-jdk8 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/download-artifact@v4 + with: + name: velox-native-lib-centos-7-${{github.sha}} + path: ./cpp/build/ + - name: build include-what-you-use + run: | + yum install -y llvm llvm-devel clang clang-devel llvm-toolset + cd $GITHUB_WORKSPACE/ + git clone https://github.com/include-what-you-use/include-what-you-use.git + cd include-what-you-use + git checkout clang_12 + mkdir build && cd build + cmake -G "Unix Makefiles" -DCMAKE_PREFIX_PATH=/usr/include/llvm ../ + make -j$(nproc) + ls ./bin + ln -s $GITHUB_WORKSPACE/include-what-you-use/build/bin/include-what-you-use /usr/bin/include-what-you-use + - name: Check Include What You Use + run: | + pip3 install regex + cd $GITHUB_WORKSPACE/ + sed -i "s|/work|$(pwd)|g" cpp/build/compile_commands.json + ls /usr/bin/include-what-you-use + export CPLUS_INCLUDE_PATH=/opt/rh/gcc-toolset-11/root/usr/lib/gcc/x86_64-redhat-linux/11/include + export CXXFLAGS="--gcc-toolchain=/opt/rh/gcc-toolset-11/root/usr" + python3 dev/check.py iwyu commit Review Comment: Will it only check the modified files of PR? ########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... + $ python iwyu_tool.py -p . + +See iwyu_tool.py -h for more details on command-line arguments. +""" + +from __future__ import print_function +import os +import re +import sys +import json +import time +import shlex +import shutil +import argparse +import tempfile +import subprocess + +CORRECT_RE = re.compile(r"^\((.*?) has correct #includes/fwd-decls\)$") +SHOULD_ADD_RE = re.compile(r"^(.*?) should add these lines:$") +ADD_RE = re.compile("^(.*?) +// (.*)$") +SHOULD_REMOVE_RE = re.compile(r"^(.*?) should remove these lines:$") +FULL_LIST_RE = re.compile(r"The full include-list for (.*?):$") +END_RE = re.compile(r"^---$") +LINES_RE = re.compile(r"^- (.*?) // lines ([0-9]+)-[0-9]+$") + + +GENERAL, ADD, REMOVE, LIST = range(4) + + +def clang_formatter(output, style): + """Process iwyu's output into something clang-like.""" + formatted = [] + + state = (GENERAL, None) + for line in output.splitlines(): + match = CORRECT_RE.match(line) + if match: + # See PR#1806 for more info + continue + match = SHOULD_ADD_RE.match(line) + if match: + state = (ADD, match.group(1)) + continue + match = SHOULD_REMOVE_RE.match(line) + if match: + state = (REMOVE, match.group(1)) + continue + match = FULL_LIST_RE.match(line) + if match: + state = (LIST, match.group(1)) + elif END_RE.match(line): + state = (GENERAL, None) + elif not line.strip(): + continue + elif state[0] == GENERAL: + formatted.append(line) + elif state[0] == ADD: + match = ADD_RE.match(line) + if match: + formatted.append( + "%s:1:1: %s: add '%s' (%s)" + % (state[1], style, match.group(1), match.group(2)) + ) + else: + formatted.append("%s:1:1: %s: add '%s'" % (state[1], style, line)) + elif state[0] == REMOVE: + match = LINES_RE.match(line) + line_no = match.group(2) if match else "1" + formatted.append( + "%s:%s:1: %s: superfluous '%s'" + % (state[1], line_no, style, match.group(1)) + ) + + return os.linesep.join(formatted) + + +DEFAULT_FORMAT = "iwyu" +FORMATTERS = { + "iwyu": lambda output: output, + "clang": lambda output: clang_formatter(output, style="error"), + "clang-warning": lambda output: clang_formatter(output, style="warning"), +} + + +if sys.platform.startswith("win"): + # Case-insensitive match on Windows + def normcase(s): + return s.lower() + +else: + + def normcase(s): + return s + + +def is_subpath_of(path, parent): + """Return True if path is equal to or fully contained within parent. + + Assumes both paths are canonicalized with os.path.realpath. + """ + parent = normcase(parent) + path = normcase(path) + + if path == parent: + return True + + if not path.startswith(parent): + return False + + # Now we know parent is a prefix of path, but they only share lineage if the + # difference between them starts with a path separator, e.g. /a/b/c/file + # is not a parent of /a/b/c/file.cpp, but /a/b/c and /a/b/c/ are. + parent = parent.rstrip(os.path.sep) + suffix = path[len(parent) :] + return suffix.startswith(os.path.sep) + + +def is_msvc_driver(compile_command): + """Return True if compile_command matches an MSVC CL-style driver.""" + compile_command = normcase(compile_command) + + if compile_command.endswith("cl.exe"): + # Native MSVC compiler or clang-cl.exe + return True + + if compile_command.endswith("clang-cl"): + # Cross clang-cl on non-Windows + return True + + return False + + +def win_split(cmdline): Review Comment: Suggest removing this. ########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... + $ python iwyu_tool.py -p . + +See iwyu_tool.py -h for more details on command-line arguments. +""" + +from __future__ import print_function +import os +import re +import sys +import json +import time +import shlex +import shutil +import argparse +import tempfile +import subprocess + +CORRECT_RE = re.compile(r"^\((.*?) has correct #includes/fwd-decls\)$") +SHOULD_ADD_RE = re.compile(r"^(.*?) should add these lines:$") +ADD_RE = re.compile("^(.*?) +// (.*)$") +SHOULD_REMOVE_RE = re.compile(r"^(.*?) should remove these lines:$") +FULL_LIST_RE = re.compile(r"The full include-list for (.*?):$") +END_RE = re.compile(r"^---$") +LINES_RE = re.compile(r"^- (.*?) // lines ([0-9]+)-[0-9]+$") + + +GENERAL, ADD, REMOVE, LIST = range(4) + + +def clang_formatter(output, style): + """Process iwyu's output into something clang-like.""" + formatted = [] + + state = (GENERAL, None) + for line in output.splitlines(): + match = CORRECT_RE.match(line) + if match: + # See PR#1806 for more info + continue + match = SHOULD_ADD_RE.match(line) + if match: + state = (ADD, match.group(1)) + continue + match = SHOULD_REMOVE_RE.match(line) + if match: + state = (REMOVE, match.group(1)) + continue + match = FULL_LIST_RE.match(line) + if match: + state = (LIST, match.group(1)) + elif END_RE.match(line): + state = (GENERAL, None) + elif not line.strip(): + continue + elif state[0] == GENERAL: + formatted.append(line) + elif state[0] == ADD: + match = ADD_RE.match(line) + if match: + formatted.append( + "%s:1:1: %s: add '%s' (%s)" + % (state[1], style, match.group(1), match.group(2)) + ) + else: + formatted.append("%s:1:1: %s: add '%s'" % (state[1], style, line)) + elif state[0] == REMOVE: + match = LINES_RE.match(line) + line_no = match.group(2) if match else "1" + formatted.append( + "%s:%s:1: %s: superfluous '%s'" + % (state[1], line_no, style, match.group(1)) + ) + + return os.linesep.join(formatted) + + +DEFAULT_FORMAT = "iwyu" +FORMATTERS = { + "iwyu": lambda output: output, + "clang": lambda output: clang_formatter(output, style="error"), + "clang-warning": lambda output: clang_formatter(output, style="warning"), +} + + +if sys.platform.startswith("win"): + # Case-insensitive match on Windows + def normcase(s): + return s.lower() + +else: + + def normcase(s): + return s + + +def is_subpath_of(path, parent): + """Return True if path is equal to or fully contained within parent. + + Assumes both paths are canonicalized with os.path.realpath. + """ + parent = normcase(parent) + path = normcase(path) + + if path == parent: + return True + + if not path.startswith(parent): + return False + + # Now we know parent is a prefix of path, but they only share lineage if the + # difference between them starts with a path separator, e.g. /a/b/c/file + # is not a parent of /a/b/c/file.cpp, but /a/b/c and /a/b/c/ are. + parent = parent.rstrip(os.path.sep) + suffix = path[len(parent) :] + return suffix.startswith(os.path.sep) + + +def is_msvc_driver(compile_command): + """Return True if compile_command matches an MSVC CL-style driver.""" + compile_command = normcase(compile_command) + + if compile_command.endswith("cl.exe"): + # Native MSVC compiler or clang-cl.exe + return True + + if compile_command.endswith("clang-cl"): + # Cross clang-cl on non-Windows + return True + + return False + + +def win_split(cmdline): + """Minimal implementation of shlex.split for Windows following + https://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft.aspx. + """ + + def split_iter(cmdline): + in_quotes = False + backslashes = 0 + arg = "" + for c in cmdline: + if c == "\\": + # MSDN: Backslashes are interpreted literally, unless they + # immediately precede a double quotation mark. + # Buffer them until we know what comes next. + backslashes += 1 + elif c == '"': + # Quotes can either be an escaped quote or the start of a quoted + # string. Paraphrasing MSDN: + # Before quotes, place one backslash in the arg for every pair + # of leading backslashes. If the number of backslashes is odd, + # retain the double quotation mark, otherwise interpret it as a + # string delimiter and switch state. + arg += "\\" * (backslashes // 2) + if backslashes % 2 == 1: + arg += c + else: + in_quotes = not in_quotes + backslashes = 0 + elif c in (" ", "\t") and not in_quotes: + # MSDN: Arguments are delimited by white space, which is either + # a space or a tab [but only outside of a string]. + # Flush any buffered backslashes and yield arg, unless empty. + arg += "\\" * backslashes + if arg: + yield arg + arg = "" + backslashes = 0 + else: + # Flush buffered backslashes and append. + arg += "\\" * backslashes + arg += c + backslashes = 0 + + if arg: + arg += "\\" * backslashes + yield arg + + return list(split_iter(cmdline)) + + +def split_command(cmdstr): + """Split a command string into a list, respecting shell quoting.""" + if sys.platform.startswith("win"): + # shlex.split does not work for Windows command-lines, so special-case + # to our own implementation. + cmd = win_split(cmdstr) + else: + cmd = shlex.split(cmdstr) + + return cmd + + +def find_include_what_you_use(): Review Comment: It seems no need to be complex. Can we just pass the installed IWYU path to it? ########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... Review Comment: Suggest removing these comments related to windows usage. ########## dev/iwyu_tool.py: ########## @@ -0,0 +1,617 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +""" Driver to consume a Clang compilation database and invoke IWYU. + +Example usage with CMake: + + # Unix systems + $ mkdir build && cd build + $ CC="clang" CXX="clang++" cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ... + $ iwyu_tool.py -p . + + # Windows systems + $ mkdir build && cd build + $ cmake -DCMAKE_CXX_COMPILER="%VCINSTALLDIR%/bin/cl.exe" \ + -DCMAKE_C_COMPILER="%VCINSTALLDIR%/VC/bin/cl.exe" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -G Ninja ... + $ python iwyu_tool.py -p . + +See iwyu_tool.py -h for more details on command-line arguments. +""" + +from __future__ import print_function +import os +import re +import sys +import json +import time +import shlex +import shutil +import argparse +import tempfile +import subprocess + +CORRECT_RE = re.compile(r"^\((.*?) has correct #includes/fwd-decls\)$") +SHOULD_ADD_RE = re.compile(r"^(.*?) should add these lines:$") +ADD_RE = re.compile("^(.*?) +// (.*)$") +SHOULD_REMOVE_RE = re.compile(r"^(.*?) should remove these lines:$") +FULL_LIST_RE = re.compile(r"The full include-list for (.*?):$") +END_RE = re.compile(r"^---$") +LINES_RE = re.compile(r"^- (.*?) // lines ([0-9]+)-[0-9]+$") + + +GENERAL, ADD, REMOVE, LIST = range(4) + + +def clang_formatter(output, style): + """Process iwyu's output into something clang-like.""" + formatted = [] + + state = (GENERAL, None) + for line in output.splitlines(): + match = CORRECT_RE.match(line) + if match: + # See PR#1806 for more info + continue + match = SHOULD_ADD_RE.match(line) + if match: + state = (ADD, match.group(1)) + continue + match = SHOULD_REMOVE_RE.match(line) + if match: + state = (REMOVE, match.group(1)) + continue + match = FULL_LIST_RE.match(line) + if match: + state = (LIST, match.group(1)) + elif END_RE.match(line): + state = (GENERAL, None) + elif not line.strip(): + continue + elif state[0] == GENERAL: + formatted.append(line) + elif state[0] == ADD: + match = ADD_RE.match(line) + if match: + formatted.append( + "%s:1:1: %s: add '%s' (%s)" + % (state[1], style, match.group(1), match.group(2)) + ) + else: + formatted.append("%s:1:1: %s: add '%s'" % (state[1], style, line)) + elif state[0] == REMOVE: + match = LINES_RE.match(line) + line_no = match.group(2) if match else "1" + formatted.append( + "%s:%s:1: %s: superfluous '%s'" + % (state[1], line_no, style, match.group(1)) + ) + + return os.linesep.join(formatted) + + +DEFAULT_FORMAT = "iwyu" +FORMATTERS = { + "iwyu": lambda output: output, + "clang": lambda output: clang_formatter(output, style="error"), + "clang-warning": lambda output: clang_formatter(output, style="warning"), +} + + +if sys.platform.startswith("win"): + # Case-insensitive match on Windows + def normcase(s): + return s.lower() + +else: + + def normcase(s): + return s + + +def is_subpath_of(path, parent): + """Return True if path is equal to or fully contained within parent. + + Assumes both paths are canonicalized with os.path.realpath. + """ + parent = normcase(parent) + path = normcase(path) + + if path == parent: + return True + + if not path.startswith(parent): + return False + + # Now we know parent is a prefix of path, but they only share lineage if the + # difference between them starts with a path separator, e.g. /a/b/c/file + # is not a parent of /a/b/c/file.cpp, but /a/b/c and /a/b/c/ are. + parent = parent.rstrip(os.path.sep) + suffix = path[len(parent) :] + return suffix.startswith(os.path.sep) + + +def is_msvc_driver(compile_command): + """Return True if compile_command matches an MSVC CL-style driver.""" + compile_command = normcase(compile_command) + + if compile_command.endswith("cl.exe"): + # Native MSVC compiler or clang-cl.exe + return True + + if compile_command.endswith("clang-cl"): + # Cross clang-cl on non-Windows + return True + + return False + + +def win_split(cmdline): + """Minimal implementation of shlex.split for Windows following + https://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft.aspx. + """ + + def split_iter(cmdline): + in_quotes = False + backslashes = 0 + arg = "" + for c in cmdline: + if c == "\\": + # MSDN: Backslashes are interpreted literally, unless they + # immediately precede a double quotation mark. + # Buffer them until we know what comes next. + backslashes += 1 + elif c == '"': + # Quotes can either be an escaped quote or the start of a quoted + # string. Paraphrasing MSDN: + # Before quotes, place one backslash in the arg for every pair + # of leading backslashes. If the number of backslashes is odd, + # retain the double quotation mark, otherwise interpret it as a + # string delimiter and switch state. + arg += "\\" * (backslashes // 2) + if backslashes % 2 == 1: + arg += c + else: + in_quotes = not in_quotes + backslashes = 0 + elif c in (" ", "\t") and not in_quotes: + # MSDN: Arguments are delimited by white space, which is either + # a space or a tab [but only outside of a string]. + # Flush any buffered backslashes and yield arg, unless empty. + arg += "\\" * backslashes + if arg: + yield arg + arg = "" + backslashes = 0 + else: + # Flush buffered backslashes and append. + arg += "\\" * backslashes + arg += c + backslashes = 0 + + if arg: + arg += "\\" * backslashes + yield arg + + return list(split_iter(cmdline)) + + +def split_command(cmdstr): + """Split a command string into a list, respecting shell quoting.""" + if sys.platform.startswith("win"): Review Comment: Ditto. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
