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