1

I am looking for something that can understand c++ enough to be useful during compilation to find misspelled words in literal strings. I am not really interested in variable names or comments, just the things that will end up in from of an end user.

I know this require some fine tuning before it is really useful, but I can't find ANY compile time spell checkers.

There are numerous IDE extensions and the like, but I want something I can run from Make that will produce a list of misspelled words.

I'm using Visual Studio on a Windows platform, with Make (and Makefiles) managing the actual build.

Does anyone know of anything?

  • I don't think if any compiler would offer that feature request. – Bagas Sanjaya Dec 4 '17 at 23:43
  • I'm happy if it's a third party tool I can run over the source code. It doesn't need to be part of the compilation step. – Simon Parker Dec 5 '17 at 0:35
  • What does spell checking have to do with C++? – einpoklum - reinstate Monica Dec 5 '17 at 9:43
  • @einpoklum - think of the strings that would be printed or otherwise displayed.... – ivanivan Dec 5 '17 at 20:58
  • Are you looking only for misspellings in strings and comments? If so, please state so. If not, expect most variables names to be identified as not being in the dictionary. In any case, variable names in strings or comments will give problems. – Mawg Dec 7 '17 at 8:39
1

I know that this may seem like a lot of work but you will be surprised.

The python (standard libraries) re library can be used to extract the content of strings, and optionally comments.

There is also an installable python library for python called pyenchant which is a spell checker, (for various human languages), which can be quickly run on the extracted text to spot, (and optionally make correction suggestions for), miss-spelt words. To install pyenchant simply invoke pip install pyenchant while connected to the internet.

Between the two it is simple, (once you get your head round regular expressions), to write a python script that will, on being invoked with a C/CPP/etc file, or list of files, as a parameter will extract and spell check all of the strings in those files.

This could be invoked from the makefile, (optionally returning non-zero to abort the make if any are found), or given that VS has some python integration added lately possibly even from within VS.

  • Price: Free
  • Licence: Open Source
  • Make Integration: Easy I, and a lot of others, python use python as a part of our make process all of the time as there are a lot of other things that can be done such as getting information from a VCS such as git/mercurial/svn to automatically insert version information or to stop release builds on un-checked in code.

Maybe something like, (the following is a rough & ready example for strings in quotes - note that it could be improved in numerous ways):

import enchant
import re

CHECKER = enchant.Dict()  # Create the dictionary in the default language

def get_bad_words(textlist):
    """ Get the words in the list of strings """
    words = set()
    for line in textlist:
        #print(line)
        for word in line.split():
            word = word.strip(r'\x0a\x0d\\:;.,!?*() ')
            if len(word) and not word.startswith('%') and not CHECKER.check(word):
                words.add(word)
    return words

def check_file(filename):
    """ Check a single file """
    content = open(filename).read()
    strings = re.findall(r'"(.*)"', content) # Use re to find strings
    suspects = get_bad_words(strings)
    if len(suspects):
         print(filename, "contains the following suspect words")
         print(repr(', '.join(list(suspects))))

if __name__ == "__main__":  # Guard against execution when imported
    import sys
    if len(sys.argv) < 2:
        print("Usage:\n\tpython CSpellCheck.py FILENAME [....]
    else:
        for FNAME in sys.argv[1:]:
            check_file(FNAME)
  • 1
    Thank you. I'll look into this. Excuse me for not ticking this as the answer, just yet. I'm being a bit lazy and hoping someone will go 'oh, yes., just use CppSpell(tm)' or the like. Though my confidence is rapidly dropping... – Simon Parker Dec 5 '17 at 23:01
  • @SimonParker Added an example of what I was thinking of. – Steve Barnes Dec 6 '17 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.