commit a8920575d372ef9a38779604d60d211921086893
Author: David Malcolm <dmalcolm(a)redhat.com>
Date: Thu Sep 29 15:51:45 2011 -0400
add spell-checking example
run-test-suite.py | 5 +++
tests/examples/spelling-checker/input.c | 37 ++++++++++++++++++++
tests/examples/spelling-checker/script.py | 51 ++++++++++++++++++++++++++++
tests/examples/spelling-checker/stderr.txt | 13 +++++++
4 files changed, 106 insertions(+), 0 deletions(-)
---
diff --git a/run-test-suite.py b/run-test-suite.py
index b50bbce..ae7857e 100644
--- a/run-test-suite.py
+++ b/run-test-suite.py
@@ -189,6 +189,11 @@ def run_test(testdir):
# don't treat the stderr output as leading to an expected failure:
expsuccess = True
+ if testdir == 'tests/examples/spelling-checker':
+ # This test case emits warnings on stderr;
+ # don't treat the stderr output as leading to an expected failure:
+ expsuccess = True
+
# Check exit code:
if expsuccess:
# Expect a successful exit:
diff --git a/tests/examples/spelling-checker/input.c
b/tests/examples/spelling-checker/input.c
new file mode 100644
index 0000000..7123c32
--- /dev/null
+++ b/tests/examples/spelling-checker/input.c
@@ -0,0 +1,37 @@
+/*
+ Copyright 2011 David Malcolm <dmalcolm(a)redhat.com>
+ Copyright 2011 Red Hat, Inc.
+
+ This 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, either version 3 of the License, or
+ (at your option) any later version.
+
+ 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/>.
+*/
+
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ const char *p = argc ? "correctly spelled" : "not so korectly
speled";
+
+ printf("The quick brown fox jumps over the lazy dog\n");
+
+ printf("Ths s n xmple f spllng mstke\n");
+}
+
+/*
+ PEP-7
+Local variables:
+c-basic-offset: 4
+indent-tabs-mode: nil
+End:
+*/
diff --git a/tests/examples/spelling-checker/script.py
b/tests/examples/spelling-checker/script.py
new file mode 100644
index 0000000..fbc6347
--- /dev/null
+++ b/tests/examples/spelling-checker/script.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+# Copyright 2011 David Malcolm <dmalcolm(a)redhat.com>
+# Copyright 2011 Red Hat, Inc.
+#
+# This 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, either version 3 of the License, or
+# (at your option) any later version.
+#
+# 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/>.
+
+import gcc
+
+# Use the Python bindings to the "enchant" spellchecker:
+import enchant
+spellingdict = enchant.Dict("en_US")
+
+class SpellcheckingPass(gcc.GimplePass):
+ def execute(self, fun):
+ # This is called per-function during compilation:
+ for bb in fun.cfg.basic_blocks:
+ if bb.gimple:
+ for stmt in bb.gimple:
+ stmt.walk_tree(self.spellcheck_node, stmt.loc)
+
+ def spellcheck_node(self, node, loc):
+ # Spellcheck any textual constants found within the node:
+ if isinstance(node, gcc.StringCst):
+ words = node.constant.split()
+ for word in words:
+ if not spellingdict.check(word):
+ # Warn about the spelling error (controlling the warning
+ # with the -Wall command-line option):
+ if gcc.warning(loc,
+ gcc.Option('-Wall'),
+ 'Possibly misspelt word in string constant:
%r' % word):
+ # and, if the warning was not suppressed at the command line,
emit
+ # suggested respellings:
+ suggestions = spellingdict.suggest(word)
+ if suggestions:
+ gcc.inform(loc, 'Suggested respellings: %r' % ',
'.join(suggestions))
+
+ps = SpellcheckingPass(name='spellchecker')
+ps.register_after('cfg')
diff --git a/tests/examples/spelling-checker/stderr.txt
b/tests/examples/spelling-checker/stderr.txt
new file mode 100644
index 0000000..be9185a
--- /dev/null
+++ b/tests/examples/spelling-checker/stderr.txt
@@ -0,0 +1,13 @@
+tests/examples/spelling-checker/input.c: In function 'main':
+tests/examples/spelling-checker/input.c:24:48: warning: Possibly misspelt word in string
constant: 'korectly' [-Wall]
+tests/examples/spelling-checker/input.c:24:48: note: Suggested respellings:
'correctly'
+tests/examples/spelling-checker/input.c:24:48: warning: Possibly misspelt word in string
constant: 'speled' [-Wall]
+tests/examples/spelling-checker/input.c:24:48: note: Suggested respellings: 'speed,
spieled, spelled, spewed, speckled, peeled, sped'
+tests/examples/spelling-checker/input.c:28:11: warning: Possibly misspelt word in string
constant: 'Ths' [-Wall]
+tests/examples/spelling-checker/input.c:28:11: note: Suggested respellings: "Th,
Th's, Ohs, Thu, TVs, T's, Th s, Ts, This, Thus, The, Tho, Tbs, Thy, Goths"
+tests/examples/spelling-checker/input.c:28:11: warning: Possibly misspelt word in string
constant: 'xmple' [-Wall]
+tests/examples/spelling-checker/input.c:28:11: note: Suggested respellings:
'ample'
+tests/examples/spelling-checker/input.c:28:11: warning: Possibly misspelt word in string
constant: 'spllng' [-Wall]
+tests/examples/spelling-checker/input.c:28:11: note: Suggested respellings:
'spelling'
+tests/examples/spelling-checker/input.c:28:11: warning: Possibly misspelt word in string
constant: 'mstke' [-Wall]
+tests/examples/spelling-checker/input.c:28:11: note: Suggested respellings:
'mistake'
diff --git a/tests/examples/spelling-checker/stdout.txt
b/tests/examples/spelling-checker/stdout.txt
new file mode 100644
index 0000000..e69de29