Merge pull request #1162 from gedankenexperimenter/iwyu-simulator
Expand and improve IWYU coveragepull/1181/head
commit
8c15bb79ec
@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# ------------------------------------------------------------------------------
|
||||
# Copyright (c) 2022 Michael Richters <gedankenexperimenter@gmail.com>
|
||||
|
||||
# This is free and unencumbered software released into the public domain.
|
||||
|
||||
# Anyone is free to copy, modify, publish, use, compile, sell, or
|
||||
# distribute this software, either in source code form or as a compiled
|
||||
# binary, for any purpose, commercial or non-commercial, and by any
|
||||
# means.
|
||||
|
||||
# In jurisdictions that recognize copyright laws, the author or authors
|
||||
# of this software dedicate any and all copyright interest in the
|
||||
# software to the public domain. We make this dedication for the benefit
|
||||
# of the public at large and to the detriment of our heirs and
|
||||
# successors. We intend this dedication to be an overt act of
|
||||
# relinquishment in perpetuity of all present and future rights to this
|
||||
# software under copyright law.
|
||||
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
# OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# For more information, please refer to <http://unlicense.org/>
|
||||
# ------------------------------------------------------------------------------
|
||||
"""Utilities shared by Kaleidoscope code maintenance tools."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
@contextmanager
|
||||
def cwd(temp_wd):
|
||||
"""Execute code in a different working directory
|
||||
|
||||
Parameters:
|
||||
`temp_wd` (`str`): The name of a directory to (temporarily) change to
|
||||
|
||||
Change the current working directory, then automatically restore the previous working
|
||||
directory when done. Invoke `cwd()` like this:
|
||||
```py
|
||||
with cwd(temp_wd):
|
||||
...
|
||||
```
|
||||
"""
|
||||
old_wd = os.getcwd()
|
||||
os.chdir(temp_wd)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
os.chdir(old_wd)
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
def split_on_newlines(string):
|
||||
"""Split the string using newlines as the separator."""
|
||||
return string.splitlines()
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------
|
||||
def split_on_nulls(string):
|
||||
"""Split the input string using NULL characters as the separator."""
|
||||
targets = [_ for _ in string.split('\0') if _ != '']
|
||||
return targets or []
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
def setup_logging(loglevel):
|
||||
"""Set up basic logging."""
|
||||
logformat = "%(message)s"
|
||||
logging.basicConfig(
|
||||
level=loglevel,
|
||||
stream=sys.stdout,
|
||||
format=logformat,
|
||||
datefmt="",
|
||||
)
|
||||
return logging.getLogger()
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
def check_git_diff():
|
||||
"""Check for unstaged changes with `git diff`
|
||||
|
||||
Returns: a list of the names of files with unstaged changes
|
||||
|
||||
This check isn't perfect, because it can give false positives (if there are unrelated
|
||||
unstaged changes).
|
||||
"""
|
||||
git_diff_cmd = ['git', 'diff', '-z', '--exit-code', '--name-only']
|
||||
|
||||
changed_files = []
|
||||
proc = subprocess.run(git_diff_cmd, capture_output=True)
|
||||
if proc.returncode != 0:
|
||||
changed_files = split_on_nulls(proc.stdout.decode('utf-8'))
|
||||
return changed_files
|
||||
|
||||
|
||||
# ==============================================================================
|
||||
if __name__ == "__main__":
|
||||
sys.exit(1)
|
@ -1,6 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# In case it hasn't been set, assume we're being run from the root of the local
|
||||
# Kaleidoscope repository.
|
||||
: "${KALEIDOSCOPE_DIR:=$(pwd)}"
|
||||
cd "${KALEIDOSCOPE_DIR}" || exit 1
|
||||
|
||||
git ls-files -m | grep -E '\.(h|cpp)$' | xargs "${KALEIDOSCOPE_DIR}"/bin/iwyu.py
|
||||
# This variable is a git ref that should point to the current master branch of
|
||||
# the primary Kaleidoscope repository. In most cases, this would be
|
||||
# `origin/master`.
|
||||
: "${KALEIDOSCOPE_MERGE_BASE:=origin/master}"
|
||||
|
||||
# Don't do anything if the working tree has unstaged changes, to avoid
|
||||
# unintentional combining of contentful changes with formatting changes.
|
||||
if ! git diff -z --exit-code --quiet; then
|
||||
echo "Working tree has unstaged changes; aborting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Run git-diff so we only run IWYU on files that differ from `master`. This
|
||||
# isn't necessarily what we want, but if the current branch has been rebased, it
|
||||
# shouldn't touch any extra files.
|
||||
git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- src plugins \
|
||||
| "${KALEIDOSCOPE_DIR}/bin/iwyu.py" -z -v
|
||||
|
||||
# After running it on Kaleidoscope source files, run it on the test simulator,
|
||||
# which requires some additional include dirs.
|
||||
git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- testing \
|
||||
| "${KALEIDOSCOPE_DIR}/bin/iwyu.py" \
|
||||
-z -v \
|
||||
-I="${KALEIDOSCOPE_DIR}" \
|
||||
-I="${KALEIDOSCOPE_DIR}/testing/googletest/googlemock/include" \
|
||||
-I="${KALEIDOSCOPE_DIR}/testing/googletest/googletest/include"
|
||||
|
||||
# Always run clang-format after IWYU, because they have different indentation
|
||||
# rules for comments added by IWYU.
|
||||
git diff -z --name-only "${KALEIDOSCOPE_MERGE_BASE}" -- src plugins testing \
|
||||
| "${KALEIDOSCOPE_DIR}/bin/format-code.py" \
|
||||
-z -v \
|
||||
--exclude-dir='testing/googletest' \
|
||||
--exclude-file='generated-testcase.cpp' \
|
||||
--force \
|
||||
--check \
|
||||
--verbose
|
||||
|
@ -0,0 +1,36 @@
|
||||
/* -*- mode: c++ -*-
|
||||
* Copyright (C) 2022 Keyboardio, Inc.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License as published by the Free Software
|
||||
* Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#undef TEST
|
||||
|
||||
// In order to include gtest files, we need to undefine some macros that
|
||||
// Arduino.h unwisely exports. Rahter than including "gtest/gtest.h" (et al)
|
||||
// directly, any simulator code should instead include "testing/gtest.h".
|
||||
#undef min
|
||||
#undef max
|
||||
|
||||
// The headers listed here other than "gtest/gtest.h" and "gmock/gmock.h" are
|
||||
// only included to prevent IWYU from inserting them directly into simulator
|
||||
// source files. This seems mildly preferable to modifying the gtest files
|
||||
// themselves.
|
||||
#include "gmock/gmock-matchers.h" // IWYU pragma: export
|
||||
#include "gmock/gmock.h" // IWYU pragma: export
|
||||
#include "gtest/gtest-message.h" // IWYU pragma: export
|
||||
#include "gtest/gtest-test-part.h" // IWYU pragma: export
|
||||
#include "gtest/gtest.h" // IWYU pragma: export
|
||||
#include "gtest/gtest_pred_impl.h" // IWYU pragma: export
|
Loading…
Reference in new issue