Klocwork documentation

Learning

Klocwork University
View detailed technical videos on Klocwork tools on the Klocwork Universtity page.

Refactoring updates in Insight 9.5 / 9.6

Jan. 17, 2012

You may have noticed that all C/C++ users of Klocwork's plug-in for Visual Studio or Eclipse now have access to Klocwork Refactoring without the requirement of a separate license. This great feature set promotes clean, maintainable code that lowers long-term cost of ownership. Whether you're following a full-on Red/Green/Refactor test-driven development model, or just looking to make sense of overly-complicated inherited code, Klocwork Refactoring is a great tool to add to your arsenal.

Focusing on high-value, complex refactorings, we support a number of the most frequently used code modification types, such as:

  • Extract / inline function or method
  • Extract / inline variable
  • Rename local entity

Built on a smart framework that understands copy/paste clones, automatic extraction of one, ten or even hundreds of "nearly identical" code blocks into a common data-driven function or method is incredibly simple and fast to achieve.

New with Klocwork Insight 9.5, our core refactoring engine has been updated to support many new capabilities, such as:

Extraction of declarations and initializations

class A {
public:
  A(int i) { x = i; }
  int x;
};
int foo() {
  A a(12);
  A b(3);
  a.x = b.x;
  return a.x + 3; }

Refactoring the shaded code to extract function:

A extracted_function() {
  A a(12);
  A b(3);
  a.x = b.x;
  return a;
}
int foo() 
  A a = extracted_function();
  return a.x + 3;
}

Fuzzy clone matching

void foo() {
  int a, b = 1, c = 3, m, n = 2;
  a = b + c + n
  m = n + 4 + a;
}

Refactoring the shaded code to extract function:

int extracted_function(int b, int c, int n) {
  return b + c + n;
}
void foo() {
  int a, b = 1, c = 3, m, n = 2;
  a = extracted_function(b, c, n);
  m = extracted_function(n, 4, a);
}

Better path extraction

int main(int argc, char* argv[]) {
  int i = 0;
  char c = *argv[1];
 
  if( c >= 'a' && c <= 'z' )
    return 1;
  else if( c >= 'A' && c <= 'Z' )
    return 2;
  else
    i = 3;
  return i; }

Refactoring the shaded code to extract function:

int extracted_function(char c, int* i, int* o) {
  if( c >= 'a' && c <= 'z' ) {
    *o = 1;
    return 1;
  }
  else if( c >= 'A' && c <= 'Z' ) {
    *o = 2;
    return 1;
  }
  else
    *i = 3;
  return 0;
}
int main(int argc, char* argv[]) {
  int o;
  int i = 0;
  char c = *argv[1];
 
  if( extracted_function(c, &i, &o) )
    return o;
  return i;
}

... and more

Current users of Klocwork Refactoring will notice that the header analysis refactoring types have been recast as checkers in Insight 9.5, resulting in suboptimal header inclusion strategies being highlighted as defects (replaces Analyze Headers), accompanied by a "quick fix" action from the right mouse context menu (replaced Optimize Headers).