Skip to content
Snippets Groups Projects
Victor Poughon's avatar
Victor Poughon authored
Before this commit, many files are using std::string without including
<string>. It can work accidentally but causes issues when refactoring,
especially if using operator <<() which is included implicitly by some
compilers.

To find guilty header files, I used:

    grep -l "^ *std::string" $(grep -L "#include <string>" $(find . -type f -name "*.h"))

which finds all files containing "std::string" at the beginning of a line
(usually a member or variable declaration), but not "#include <string>".
And then this script to add the includes (plus some manual ediing):

    #!/usr/bin/env python3

    import re
    import argparse

    def fix_file(filename, header):

        with open(filename, "r") as f:
            content = f.read()

        matches = list(re.finditer(r"(#include .*\n)\n", content))
        if len(matches) == 0:
            print("no include!")
            sys.exit(-1)

        pos = matches[-1].end(1)
        open(filename, "w").write(content[:pos] + "#include <{}>\n".format(header) + content[pos:])

    if __name__ == "__main__":
        parser = argparse.ArgumentParser()
        parser.add_argument('--header', type=str, required=True)
        parser.add_argument('files', type=str, nargs='+')
        args = parser.parse_args()

        for filename in args.files:
            fix_file(filename, args.header)
fc923078
History
Name Last commit Last update
..