[doc] Add doc for linters and simplify c++ lint script. (#9750)

This commit is contained in:
Jiaming Yuan 2023-11-07 05:03:30 +08:00 committed by GitHub
parent 98238d63fa
commit 82828621d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -144,11 +144,5 @@ jobs:
python -m pip install wheel setuptools cmakelint cpplint pylint python -m pip install wheel setuptools cmakelint cpplint pylint
- name: Run lint - name: Run lint
run: | run: |
python3 tests/ci_build/lint_cpp.py xgboost cpp R-package/src python3 tests/ci_build/lint_cpp.py
python3 tests/ci_build/lint_cpp.py xgboost cpp include src python-package \
--exclude_path python-package/xgboost/dmlc-core python-package/xgboost/include \
python-package/xgboost/lib python-package/xgboost/rabit \
python-package/xgboost/src
sh ./tests/ci_build/lint_cmake.sh sh ./tests/ci_build/lint_cmake.sh

View File

@ -118,16 +118,40 @@ two automatic checks to enforce coding style conventions. To expedite the code r
Linter Linter
====== ======
We use `pylint <https://github.com/PyCQA/pylint>`_ and `cpplint <https://github.com/cpplint/cpplint>`_ to enforce style convention and find potential errors. Linting is especially useful for Python, as we can catch many errors that would have otherwise occurred at run-time. We use a combination of linters to enforce style convention and find potential errors. Linting is especially useful for scripting languages like Python, as we can catch many errors that would have otherwise occurred at run-time.
To run this check locally, run the following command from the top level source tree: For Python scripts, `pylint <https://github.com/PyCQA/pylint>`_, `black <https://github.com/psf/black>`__ and `isort <https://github.com/PyCQA/isort>`__ are used for providing guidance on coding style, and `mypy <https://github.com/python/mypy>`__ is required for type checking. For C++, `cpplint <https://github.com/cpplint/cpplint>`_ is used along with ``clang-tidy``. For R, ``lintr`` is used.
To run checks for Python locally, install the checkers mentioned previously and run:
.. code-block:: bash .. code-block:: bash
cd /path/to/xgboost/ cd /path/to/xgboost/
make lint python ./tests/ci_build/lint_python.py --fix
To run checks for R:
.. code-block:: bash
cd /path/to/xgboost/
Rscript tests/ci_build/lint_r.R $(pwd)
To run checks for cpplint locally:
.. code-block:: bash
cd /path/to/xgboost/
python ./tests/ci_build/lint_cpp.py
See next section for clang-tidy. For CMake scripts:
.. code-block:: bash
bash ./tests/ci_build/lint_cmake.sh
Lastly, the linter for jvm-packages is integrated into the maven build process.
This command requires the Python packages pylint and cpplint.
Clang-tidy Clang-tidy
========== ==========

View File

@ -134,7 +134,12 @@ def process(fname, allow_type):
def main(): def main():
parser = argparse.ArgumentParser(description="run cpp lint") parser = argparse.ArgumentParser(description="run cpp lint")
parser.add_argument("path", nargs="+", help="path to traverse") parser.add_argument(
"path",
nargs="*",
help="Path to traverse",
default=["src", "include", os.path.join("R-package", "src"), "python-package"],
)
parser.add_argument( parser.add_argument(
"--exclude_path", "--exclude_path",
nargs="+", nargs="+",
@ -148,6 +153,8 @@ def main():
allow_type += CXX_SUFFIX allow_type += CXX_SUFFIX
for path in args.path: for path in args.path:
if not os.path.exists(path):
raise ValueError(f"Unknown path: {path}")
if os.path.isfile(path): if os.path.isfile(path):
normpath = os.path.normpath(path) normpath = os.path.normpath(path)
if normpath not in excluded_paths: if normpath not in excluded_paths: