PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freetype2/src/tools/chktrcmp.py

https://bitbucket.org/bgirard/tiling
Python | 114 lines | 96 code | 9 blank | 9 comment | 8 complexity | 26eec47f6e3314b96c8b93e6810b2f21 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, LGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception, GPL-2.0, JSON, Apache-2.0, 0BSD, MIT
  1. #!/usr/bin/env python
  2. #
  3. # Check trace components in FreeType 2 source.
  4. # Author: suzuki toshiya, 2009
  5. #
  6. # This code is explicitly into the public domain.
  7. import sys
  8. import os
  9. import re
  10. SRC_FILE_LIST = []
  11. USED_COMPONENT = {}
  12. KNOWN_COMPONENT = {}
  13. SRC_FILE_DIRS = [ "src" ]
  14. TRACE_DEF_FILES = [ "include/freetype/internal/fttrace.h" ]
  15. # --------------------------------------------------------------
  16. # Parse command line options
  17. #
  18. for i in range( 1, len( sys.argv ) ):
  19. if sys.argv[i].startswith( "--help" ):
  20. print "Usage: %s [option]" % sys.argv[0]
  21. print "Search used-but-defined and defined-but-not-used trace_XXX macros"
  22. print ""
  23. print " --help:"
  24. print " Show this help"
  25. print ""
  26. print " --src-dirs=dir1:dir2:..."
  27. print " Specify the directories of C source files to be checked"
  28. print " Default is %s" % ":".join( SRC_FILE_DIRS )
  29. print ""
  30. print " --def-files=file1:file2:..."
  31. print " Specify the header files including FT_TRACE_DEF()"
  32. print " Default is %s" % ":".join( TRACE_DEF_FILES )
  33. print ""
  34. exit(0)
  35. if sys.argv[i].startswith( "--src-dirs=" ):
  36. SRC_FILE_DIRS = sys.argv[i].replace( "--src-dirs=", "", 1 ).split( ":" )
  37. elif sys.argv[i].startswith( "--def-files=" ):
  38. TRACE_DEF_FILES = sys.argv[i].replace( "--def-files=", "", 1 ).split( ":" )
  39. # --------------------------------------------------------------
  40. # Scan C source and header files using trace macros.
  41. #
  42. c_pathname_pat = re.compile( '^.*\.[ch]$', re.IGNORECASE )
  43. trace_use_pat = re.compile( '^[ \t]*#define[ \t]+FT_COMPONENT[ \t]+trace_' )
  44. for d in SRC_FILE_DIRS:
  45. for ( p, dlst, flst ) in os.walk( d ):
  46. for f in flst:
  47. if c_pathname_pat.match( f ) != None:
  48. src_pathname = os.path.join( p, f )
  49. line_num = 0
  50. for src_line in open( src_pathname, 'r' ):
  51. line_num = line_num + 1
  52. src_line = src_line.strip()
  53. if trace_use_pat.match( src_line ) != None:
  54. component_name = trace_use_pat.sub( '', src_line )
  55. if component_name in USED_COMPONENT:
  56. USED_COMPONENT[component_name].append( "%s:%d" % ( src_pathname, line_num ) )
  57. else:
  58. USED_COMPONENT[component_name] = [ "%s:%d" % ( src_pathname, line_num ) ]
  59. # --------------------------------------------------------------
  60. # Scan header file(s) defining trace macros.
  61. #
  62. trace_def_pat_opn = re.compile( '^.*FT_TRACE_DEF[ \t]*\([ \t]*' )
  63. trace_def_pat_cls = re.compile( '[ \t\)].*$' )
  64. for f in TRACE_DEF_FILES:
  65. line_num = 0
  66. for hdr_line in open( f, 'r' ):
  67. line_num = line_num + 1
  68. hdr_line = hdr_line.strip()
  69. if trace_def_pat_opn.match( hdr_line ) != None:
  70. component_name = trace_def_pat_opn.sub( '', hdr_line )
  71. component_name = trace_def_pat_cls.sub( '', component_name )
  72. if component_name in KNOWN_COMPONENT:
  73. print "trace component %s is defined twice, see %s and fttrace.h:%d" % \
  74. ( component_name, KNOWN_COMPONENT[component_name], line_num )
  75. else:
  76. KNOWN_COMPONENT[component_name] = "%s:%d" % \
  77. ( os.path.basename( f ), line_num )
  78. # --------------------------------------------------------------
  79. # Compare the used and defined trace macros.
  80. #
  81. print "# Trace component used in the implementations but not defined in fttrace.h."
  82. cmpnt = USED_COMPONENT.keys()
  83. cmpnt.sort()
  84. for c in cmpnt:
  85. if c not in KNOWN_COMPONENT:
  86. print "Trace component %s (used in %s) is not defined." % ( c, ", ".join( USED_COMPONENT[c] ) )
  87. print "# Trace component is defined but not used in the implementations."
  88. cmpnt = KNOWN_COMPONENT.keys()
  89. cmpnt.sort()
  90. for c in cmpnt:
  91. if c not in USED_COMPONENT:
  92. if c != "any":
  93. print "Trace component %s (defined in %s) is not used." % ( c, KNOWN_COMPONENT[c] )