Skip to content
Snippets Groups Projects
check_style_c.py 36.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •             index_line_start = i + 1
            else:
                col += len(tok.text.expandtabs(TAB_SIZE))
    
            #elif tok.type == Token.Name:
            #    print(tok.text)
    
            #print(ttype, type(ttype))
            #print((ttype, value))
    
        #for ttype, value in la:
        #    #print(value, end="")
    
    
    def scan_source_filepath(filepath, args):
        # for quick tests
        #~ if not filepath.endswith("creator.c"):
        #~     return
    
        code = open(filepath, 'r', encoding="utf-8").read()
    
        # fast checks which don't require full parsing
        quick_check_source(filepath, code, args)
    
        # use lexer
        scan_source(filepath, code, args)
    
    
    def scan_source_recursive(dirpath, args):
        import os
        from os.path import join, splitext
    
        def source_list(path, filename_check=None):
            for dirpath, dirnames, filenames in os.walk(path):
    
                # skip '.svn'
                if dirpath.startswith("."):
                    continue
    
                for filename in filenames:
                    filepath = join(dirpath, filename)
                    if filename_check is None or filename_check(filepath):
                        yield filepath
    
        def is_source(filename):
            ext = splitext(filename)[1]
            return (ext in {".c", ".inl", ".cpp", ".cxx", ".hpp", ".hxx", ".h", ".osl"})
    
        for filepath in sorted(source_list(dirpath, is_source)):
            if is_ignore(filepath):
                continue
    
            scan_source_filepath(filepath, args)
    
    
    if __name__ == "__main__":
        import sys
        import os
    
        desc = 'Check C/C++ code for conformance with blenders style guide:\nhttp://wiki.blender.org/index.php/Dev:Doc/CodeStyle)'
        parser = argparse.ArgumentParser(description=desc)
        parser.add_argument("paths", nargs='+', help="list of files or directories to check")
        parser.add_argument("-l", "--no-length-check", action="store_true",
                            help="skip warnings for long lines")
        args = parser.parse_args()
    
        if 0:
    
            SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))))
    
            #scan_source_recursive(os.path.join(SOURCE_DIR, "source", "blender", "bmesh"))
            scan_source_recursive(os.path.join(SOURCE_DIR, "source/blender/makesrna/intern"), args)
            sys.exit(0)
    
        for filepath in args.paths:
            if os.path.isdir(filepath):
                # recursive search
                scan_source_recursive(filepath, args)
            else:
                # single file
                scan_source_filepath(filepath, args)