1*b89a7cc2SEnji CooperGoal: 2*b89a7cc2SEnji Cooper----- 3*b89a7cc2SEnji Cooper CppClean attempts to find problems in C++ source that slow development 4*b89a7cc2SEnji Cooper in large code bases, for example various forms of unused code. 5*b89a7cc2SEnji Cooper Unused code can be unused functions, methods, data members, types, etc 6*b89a7cc2SEnji Cooper to unnecessary #include directives. Unnecessary #includes can cause 7*b89a7cc2SEnji Cooper considerable extra compiles increasing the edit-compile-run cycle. 8*b89a7cc2SEnji Cooper 9*b89a7cc2SEnji Cooper The project home page is: http://code.google.com/p/cppclean/ 10*b89a7cc2SEnji Cooper 11*b89a7cc2SEnji Cooper 12*b89a7cc2SEnji CooperFeatures: 13*b89a7cc2SEnji Cooper--------- 14*b89a7cc2SEnji Cooper * Find and print C++ language constructs: classes, methods, functions, etc. 15*b89a7cc2SEnji Cooper * Find classes with virtual methods, no virtual destructor, and no bases 16*b89a7cc2SEnji Cooper * Find global/static data that are potential problems when using threads 17*b89a7cc2SEnji Cooper * Unnecessary forward class declarations 18*b89a7cc2SEnji Cooper * Unnecessary function declarations 19*b89a7cc2SEnji Cooper * Undeclared function definitions 20*b89a7cc2SEnji Cooper * (planned) Find unnecessary header files #included 21*b89a7cc2SEnji Cooper - No direct reference to anything in the header 22*b89a7cc2SEnji Cooper - Header is unnecessary if classes were forward declared instead 23*b89a7cc2SEnji Cooper * (planned) Source files that reference headers not directly #included, 24*b89a7cc2SEnji Cooper ie, files that rely on a transitive #include from another header 25*b89a7cc2SEnji Cooper * (planned) Unused members (private, protected, & public) methods and data 26*b89a7cc2SEnji Cooper * (planned) Store AST in a SQL database so relationships can be queried 27*b89a7cc2SEnji Cooper 28*b89a7cc2SEnji CooperAST is Abstract Syntax Tree, a representation of parsed source code. 29*b89a7cc2SEnji Cooperhttp://en.wikipedia.org/wiki/Abstract_syntax_tree 30*b89a7cc2SEnji Cooper 31*b89a7cc2SEnji Cooper 32*b89a7cc2SEnji CooperSystem Requirements: 33*b89a7cc2SEnji Cooper-------------------- 34*b89a7cc2SEnji Cooper * Python 2.4 or later (2.3 probably works too) 35*b89a7cc2SEnji Cooper * Works on Windows (untested), Mac OS X, and Unix 36*b89a7cc2SEnji Cooper 37*b89a7cc2SEnji Cooper 38*b89a7cc2SEnji CooperHow to Run: 39*b89a7cc2SEnji Cooper----------- 40*b89a7cc2SEnji Cooper For all examples, it is assumed that cppclean resides in a directory called 41*b89a7cc2SEnji Cooper /cppclean. 42*b89a7cc2SEnji Cooper 43*b89a7cc2SEnji Cooper To print warnings for classes with virtual methods, no virtual destructor and 44*b89a7cc2SEnji Cooper no base classes: 45*b89a7cc2SEnji Cooper 46*b89a7cc2SEnji Cooper /cppclean/run.sh nonvirtual_dtors.py file1.h file2.h file3.cc ... 47*b89a7cc2SEnji Cooper 48*b89a7cc2SEnji Cooper To print all the functions defined in header file(s): 49*b89a7cc2SEnji Cooper 50*b89a7cc2SEnji Cooper /cppclean/run.sh functions.py file1.h file2.h ... 51*b89a7cc2SEnji Cooper 52*b89a7cc2SEnji Cooper All the commands take multiple files on the command line. Other programs 53*b89a7cc2SEnji Cooper include: find_warnings, headers, methods, and types. Some other programs 54*b89a7cc2SEnji Cooper are available, but used primarily for debugging. 55*b89a7cc2SEnji Cooper 56*b89a7cc2SEnji Cooper run.sh is a simple wrapper that sets PYTHONPATH to /cppclean and then 57*b89a7cc2SEnji Cooper runs the program in /cppclean/cpp/PROGRAM.py. There is currently 58*b89a7cc2SEnji Cooper no equivalent for Windows. Contributions for a run.bat file 59*b89a7cc2SEnji Cooper would be greatly appreciated. 60*b89a7cc2SEnji Cooper 61*b89a7cc2SEnji Cooper 62*b89a7cc2SEnji CooperHow to Configure: 63*b89a7cc2SEnji Cooper----------------- 64*b89a7cc2SEnji Cooper You can add a siteheaders.py file in /cppclean/cpp to configure where 65*b89a7cc2SEnji Cooper to look for other headers (typically -I options passed to a compiler). 66*b89a7cc2SEnji Cooper Currently two values are supported: _TRANSITIVE and GetIncludeDirs. 67*b89a7cc2SEnji Cooper _TRANSITIVE should be set to a boolean value (True or False) indicating 68*b89a7cc2SEnji Cooper whether to transitively process all header files. The default is False. 69*b89a7cc2SEnji Cooper 70*b89a7cc2SEnji Cooper GetIncludeDirs is a function that takes a single argument and returns 71*b89a7cc2SEnji Cooper a sequence of directories to include. This can be a generator or 72*b89a7cc2SEnji Cooper return a static list. 73*b89a7cc2SEnji Cooper 74*b89a7cc2SEnji Cooper def GetIncludeDirs(filename): 75*b89a7cc2SEnji Cooper return ['/some/path/with/other/headers'] 76*b89a7cc2SEnji Cooper 77*b89a7cc2SEnji Cooper # Here is a more complicated example. 78*b89a7cc2SEnji Cooper def GetIncludeDirs(filename): 79*b89a7cc2SEnji Cooper yield '/path1' 80*b89a7cc2SEnji Cooper yield os.path.join('/path2', os.path.dirname(filename)) 81*b89a7cc2SEnji Cooper yield '/path3' 82*b89a7cc2SEnji Cooper 83*b89a7cc2SEnji Cooper 84*b89a7cc2SEnji CooperHow to Test: 85*b89a7cc2SEnji Cooper------------ 86*b89a7cc2SEnji Cooper For all examples, it is assumed that cppclean resides in a directory called 87*b89a7cc2SEnji Cooper /cppclean. The tests require 88*b89a7cc2SEnji Cooper 89*b89a7cc2SEnji Cooper cd /cppclean 90*b89a7cc2SEnji Cooper make test 91*b89a7cc2SEnji Cooper # To generate expected results after a change: 92*b89a7cc2SEnji Cooper make expected 93*b89a7cc2SEnji Cooper 94*b89a7cc2SEnji Cooper 95*b89a7cc2SEnji CooperCurrent Status: 96*b89a7cc2SEnji Cooper--------------- 97*b89a7cc2SEnji Cooper The parser works pretty well for header files, parsing about 99% of Google's 98*b89a7cc2SEnji Cooper header files. Anything which inspects structure of C++ source files should 99*b89a7cc2SEnji Cooper work reasonably well. Function bodies are not transformed to an AST, 100*b89a7cc2SEnji Cooper but left as tokens. Much work is still needed on finding unused header files 101*b89a7cc2SEnji Cooper and storing an AST in a database. 102*b89a7cc2SEnji Cooper 103*b89a7cc2SEnji Cooper 104*b89a7cc2SEnji CooperNon-goals: 105*b89a7cc2SEnji Cooper---------- 106*b89a7cc2SEnji Cooper * Parsing all valid C++ source 107*b89a7cc2SEnji Cooper * Handling invalid C++ source gracefully 108*b89a7cc2SEnji Cooper * Compiling to machine code (or anything beyond an AST) 109*b89a7cc2SEnji Cooper 110*b89a7cc2SEnji Cooper 111*b89a7cc2SEnji CooperContact: 112*b89a7cc2SEnji Cooper-------- 113*b89a7cc2SEnji Cooper If you used cppclean, I would love to hear about your experiences 114*b89a7cc2SEnji Cooper [email protected]. Even if you don't use cppclean, I'd like to 115*b89a7cc2SEnji Cooper hear from you. :-) (You can contact me directly at: [email protected]) 116