diff options
author | Vladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com> | 2019-02-20 02:22:25 +0700 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-02-19 20:22:25 +0100 |
commit | 0eaaf18192cd1d5fc6371d429cb7107b1c461dd1 (patch) | |
tree | 1b33360d5410dabab25e276ef564778ca6b474e9 | |
parent | 957d1671990ea08c661dc35e8ad7cf42893c5544 (diff) | |
download | flatbuffers-0eaaf18192cd1d5fc6371d429cb7107b1c461dd1.tar.gz |
Utility for checking the encoding and line ending of source files (#5188)
* Add utility for checking the encoding of source files
- accept source files with ASCII or UTF-8 without BOM
- accept only CRLF line ending
* Fix non-ascii symbol in idl_parcer.cpp
* Remove BOM from test.cpp
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | .travis/check-sources.sh | 33 | ||||
-rw-r--r-- | .travis/check-sources.sh.py | 36 | ||||
-rw-r--r-- | src/idl_parser.cpp | 2 | ||||
-rw-r--r-- | tests/test.cpp | 2 |
5 files changed, 72 insertions, 2 deletions
diff --git a/.travis.yml b/.travis.yml index b6ab9952..23c4c23b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -89,6 +89,7 @@ matrix: - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi script: + - bash .travis/check-sources.sh - bash grpc/build_grpc.sh - cmake . -DCMAKE_BUILD_TYPE=$BUILD_TYPE diff --git a/.travis/check-sources.sh b/.travis/check-sources.sh new file mode 100644 index 00000000..3e6dbf16 --- /dev/null +++ b/.travis/check-sources.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# +# Copyright 2018 Google Inc. All rights reserved. +# +# 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. +set -e + +if [ -n "$1" ]; then + scan_dir="$1" +else + scan_dir="$( pwd )" +fi + +py_checker="$0.py" + +echo "scan root directory = '$scan_dir'" +python3 --version +# Scan recursively and search all *.cpp and *.h files using regex patterns. +# Assume that script running from a root of Flatbuffers working dir. +python3 $py_checker "ascii" "$scan_dir/include" "\.h$" +python3 $py_checker "ascii" "$scan_dir/src" "\.cpp$" +python3 $py_checker "ascii" "$scan_dir/tests" "\.h$" +python3 $py_checker "utf-8" "$scan_dir/tests" "\.cpp$" diff --git a/.travis/check-sources.sh.py b/.travis/check-sources.sh.py new file mode 100644 index 00000000..5ad060cd --- /dev/null +++ b/.travis/check-sources.sh.py @@ -0,0 +1,36 @@ +import os +import re +import sys + +def check_encoding(encoding, scan_dir, regex_pattern): + fname = None + try: + assert encoding in ['ascii', 'utf-8'], "unexpected encoding" + cmp = re.compile(regex_pattern) + for root, dirs, files in os.walk(scan_dir): + fname = root + cmp_list = [f for f in files if cmp.search(f) is not None] + for f in cmp_list: + fname = os.path.join(root, f) + with open(fname, mode='rb') as test_file: + btext = test_file.read() + # check encoding + btext.decode(encoding=encoding, errors="strict") + if encoding == "utf-8" and btext.startswith(b'\xEF\xBB\xBF'): + raise ValueError("unexpected BOM in file") + # check strict CRLF line-ending + LF = btext.count(b'\r') + CRLF = btext.count(b'\r\n') + assert LF >= CRLF, "CRLF logic error" + if CRLF != LF: + raise ValueError("CRLF violation: found {} LF characters".format(LF - CRLF)) + except Exception as err: + print("ERROR with [{}]: {}".format(fname, err)) + return -1 + else: + return 0 + +if __name__ == "__main__": + # python check-sources.sh.py 'ascii' '.' '.*\.(cpp|h)$' + res = check_encoding(sys.argv[1], sys.argv[2], sys.argv[3]) + sys.exit(0 if res == 0 else -1) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 32a9a720..a9e207e2 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -428,7 +428,7 @@ CheckedError Parser::Next() { auto dot_lvl = (c == '.') ? 0 : 1; // dot_lvl==0 <=> exactly one '.' seen if (!dot_lvl && !is_digit(*cursor_)) return NoError(); // enum? - // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4). + // Parser accepts hexadecimal-floating-literal (see C++ 5.13.4). if (is_digit(c) || has_sign || !dot_lvl) { const auto start = cursor_ - 1; auto start_digits = !is_digit(c) ? cursor_ : cursor_ - 1; diff --git a/tests/test.cpp b/tests/test.cpp index a1a10ba3..a18983f4 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1,4 +1,4 @@ -/* +/* * Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); |