move "Treat Warnings as Errors" to commit time
MIT License
A configurable warning checking script for Xcode project.
LazyWarningChecker is:
Warnings may grow to a huge level if we aren't disciplined enough. Too many warnings can swallow the newly generated ones when coding, which may have potential problems.
For example, an unimplemented method may cause a crash, but it can be easily avoided at by fix the compile warnings.
"Treat Warnings as Errors" is good, but very annoying when coding/debugging. The building could be interpreted by warnings very often, such as unused variables and unimplemented methods, which are common halfway code when developing.
So we move the checking to commit time, and get both strict warning checking and relaxed coding experience.
Furthermore, the script provides a way to set checking rules, compared to prohibit all warning. You could only check several rules, or exclude some files.
Also, the script provides a functionality of generating a blame report for warning lines.
Copy the 2 script files to your project directory
Add below to the build Post-Action of your scheme (remember to replace the path). And set Provide build setting from
to your target.
# wait log file generated (3 seconds)
function action() { sleep 3; python $SRCROOT/path/to/check_warning.py $BUILD_ROOT -o $SRCROOT/.warning_checker/last_result; python $SRCROOT/path/to/add_pre_commit_hook.py add $SRCROOT; }
# run in background to avoid blocking app launching
action&
.warning_checker/last_result
to .gitignoreDone! Now you could get a commit time warning checking.
NOTE: if a pre-commit hook already existed, you should add the hook manually. The hook content could be got from python add_pre_commit_hook.py raw
NOTE: Depend on how it works, the script cannot 100% guarantee excludes the warning line from committing. The checking result is only refreshed when build finished. So any change after building cannot be checked by this script. But its enough for most using cases.
python check_warning.py --blame path/to/issues_log # the path is the content of $BUILD_ROOT, you can the log_path in .warning_checker/last_result
check_warning.py
the main script for checking warnings
add_pre_commit_hook.py
add pre-commit git hook to read the checking result.
more options in "help"
Configuration file could be pass to check_warnings.py with -c
option.
The configuration file is a JSON formatted text.
{
"show_non_pass_warning": "all",
"rules": [
{ "type" : "flag", "content": "-Wunused-variable" }
],
"exclusive_rules": [
{ "type" : "regex", "content": "ABC.m" }
]
}
show_non_pass_warning
: all
means record all matched warnings. first
means just return on the first match.rules
: the checking rules. No rule
key in config means check all warnings.
{ "type" : "flag", "content": "-Wunused-variable" }
flag
means the match the compiler's flag
regex
means match the warning log with regular expression. You could use it to match a specific file, or what you want. A common warning log looks like /path/to/the/sourefile/ABCView.h:112:39: warning: auto property synthesis will not synthesize property 'description' because it is 'readwrite' but it will be synthesized 'readonly' via another property [-Wobjc-property-synthesis]
exclusive_rules
the rule to exclude warning logs.LazyWarningChecker scans the build logs generated by Xcode. All issues are logged as xcactivitylog
file ( just a gzip file) in some path of the derivedData.
The script runs in the post-build action (so we could run background task without block debugging.) and saves the checking result to file, which will be read by pre-commit git hook.
LazyWarningChecker is available under the MIT license.