From a05efc77be5f6df0418ccba5f7bf524773885448 Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Wed, 30 Mar 2016 15:31:23 +0100 Subject: [PATCH] style: Add a control character checker Add a style checker that verifies that source code doesn't contain non-printable (control) characters. The only allowed control characters are: * 0x0a / \n: New line * 0x09 / \t: Tab (the whitespace checker enforces no-tabs for C/C++ files) Signed-off-by: Andreas Sandberg Reviewed-by: Brandon Potter --HG-- extra : rebase_source : 9ba3e2971774a7b3d73cda34bbee1f19c4add746 --- util/style/verifiers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/util/style/verifiers.py b/util/style/verifiers.py index a9c78b413..8626cc7ce 100644 --- a/util/style/verifiers.py +++ b/util/style/verifiers.py @@ -348,6 +348,20 @@ class LineLength(LineVerifier): def fix_line(self, line): pass +class ControlCharacters(LineVerifier): + languages = set(('C', 'C++', 'swig', 'python', 'asm', 'isa', 'scons')) + test_name = 'control character' + opt_name = 'ascii' + + valid = ('\n', '\t') + invalid = "".join([chr(i) for i in range(0, 0x20) if chr(i) not in valid]) + + def check_line(self, line): + return self.fix_line(line) == line + + def fix_line(self, line): + return line.translate(None, ControlCharacters.invalid) + class BoolCompare(LineVerifier): languages = set(('C', 'C++', 'python')) test_name = 'boolean comparison' -- 2.30.2