6

Often I could use some tools to statically analyze my code in order to help me making it cleaner. Something like compiler warnings, but those are not enough. Every now and then I dream about writing one (using clang libraries or gccxml), but I guess it would take too much work.

Some things that pop to my mind are:

  1. looking for magic numbers (ie: hardcoded constant numbers different from 0).

  2. checking that the rule of three is always respected (each class must have defined either all of destructor, copy-constructor and assignment operator, or none of them).


I also dream (but these things are pure utopia) about a preprocessor which parses some non-standard code and translates it to valid C++ code, a tool able to:

  1. expand template aliases, so that I can have them in C++ (well, with C++0x this is no longer needed)

  2. move inline functions at the end of files, so that I don't have to respect the declare-before-use rule, and am able to write classes inline as in Java.

  3. offer an extended syntax, like supporting custom operators (which will be expanded in function calls), or some ad-hoc syntax/keyword to implement easily some patterns.


Is there any tool out there, able to do a subset of these things?

Otherwise what libraries would you suggest to implement these tasks (clang libs, gccxml, ...), and how much work do you think it would take?

Kara
  • 5,650
  • 15
  • 48
  • 55
peoro
  • 24,051
  • 18
  • 90
  • 141
  • possible duplicate of [What open source C++ static analysis tools are available?](http://stackoverflow.com/questions/141498/what-open-source-c-static-analysis-tools-are-available) – Björn Pollex Nov 14 '10 at 10:52

4 Answers4

4

cppcheck is just wonderful.

Johan Kotlinski
  • 23,690
  • 9
  • 73
  • 100
1

Google has an interesting tool along with its style guide... called cpplint. It may be helpful for generating cleaner code. Have a look.

http://code.google.com/p/google-styleguide/source/browse/trunk/cpplint/cpplint.py?r=15

Natansh
  • 31
  • 2
1

clang can do static analysis.

dan_waterworth
  • 5,923
  • 1
  • 25
  • 39
0

Regarding finding constants, our SD Source Code Search Engine (SCSE) can do this off the shelf. SCSE provides GUI interface for search across large sets of mixed programming langauge files, using a query language that understands the lexical syntax of each language accurately.

A typical query:

'for' ... I=index* '=' N<10

will find for keywords near an Identifier whose name must start with the sequence index followed immediately by an = operator followed by a Number whose value is less than 10. Because SCSE understands the language structure, it isn't bothered by whitespace or formatting constraints. It will also match hexadecimal values for N; it understands the different format for literals and offers queries in terms of the actual value of the number, rather than as a string.

Given such a query, the GUI will find all matches across all your files, and show a set of hits; a click on a hit takes to you the source code with the hit line highlighted.

To find all constants that aren't zero, you write the following very simple query:

N>0

What you really want are all constants that aren't defined in some kind of constant definition. So you want to remove any constant-definitions from the hit list, which is accomplished by using the "query subtract" operator:

N>0 -  'const' 'int' I '=' N

This finds nonzero constants, and removes any that are also matched by the const declaration.


Your other dreams require a much more sophisticated engine. In essence, you want to analyze C++ code (your rule of three) or extend the C++ language with some new features, and then implement those features using standard C++ capabilities. You can accomplish this using a program transformation system that can manipulate C++.

Our DMS Software Reengineering Tookit can do this. It has a full C++ front end that parses C++ to internal compiler data structures and builds complete symbol tables (and can regenerate C++ source code with all details from its internal structures). Using that front end and DMS's built in pattern matching capabilities, you could implement your rule-of-three check.

For language extensions, you can modify the DMS C++ front end by modifying its grammar to include the extra constructs you think ideal. Then, you can write program transformations that look for such constructs, and transform them into the desired code. (This idea was called intentional programming by Charles Simyoni of MS fame, but is just a special case of program transformation.)

DMS has been used to carry out massive automated transformations on real C++ applications. It could do your task fairly easily from a technical point of view. However, understanding the definition of C++ and how everything fits together, and how DMS supports transformations, requires considerable compiler-style sophistication. What you suggest to do isn't an afternoon or a week's worth of work. It would take a significant investment in education and effort. But a lot less than trying to do with starting with just YACC.

Ira Baxter
  • 88,629
  • 18
  • 158
  • 311