C ++ refactoring: conditional extension and block exclusion

I am reorganizing a very large amount of code, mainly C ++, to remove a number of temporary configuration checks that are constantly set to the given values. For example, I will have the following code:

#include <value1.h>
#include <value2.h>
#include <value3.h>

...

if ( value1() )
{
    // do something
}

bool b = value2();

if ( b && anotherCondition )
{
    // do more stuff
}

if ( value3() < 10 )
{
    // more stuff again
}

where value calls return either bool or int. Since I know the values ​​that these calls always return, I made some regular expression replacements to expand the calls to their normal values:

// where:
//   value1() == true
//   value2() == false
//   value3() == 4

// TODO: Remove expanded config (value1)
if ( true )
{
    // do something
}

// TODO: Remove expanded config (value2)
bool b = false;

if ( b && anotherCondition )
{
    // do more stuff
}

// TODO: Remove expanded config (value3)
if ( 4 < 10 )
{
    // more stuff again
}

Note that although the values ​​are fixed, they are not set at compile time, but are read from shared memory, so the compiler currently does not optimize anything behind the scenes.

, , , , , , , , true. (, ), , , , :

// do something

// DONT do more stuff (b being false always prevented this)

// more stuff again

, (, ) , , , , , , .

, - , , . , ++ , . , , , .

, , - , , - , ?

+5
2

, , "-"... , . : , .

, , ++ . . , .

, ; ++ , . , AST-AST, . , ++, , .

DMS Software Reengineering Toolkit ++ (11) front end ( ) ++ . AFAIK, , . , . DMS , , , :

  rule fix_value1():expression->expression
    "value1()" -> "true";
  rule fix_value2():expression->expression
    "value2()" -> "false";
  rule fix_value3():expression->expression
    "value3()" -> "4";

  rule simplify_boolean_and_true(r:relation):condition->condition
     "r && true" -> "r".
  rule simplify_boolean_or_ture(r:relation):condition->condition
     "r || true" -> "true".
  rule simplify_boolean_and_false(r:relation):condition->condition
     "r && false" -> "false".
  ...
  rule simplify_boolean_not_true(r:relation):condition->condition
     "!true" -> "false".
  ...

  rule simplify_if_then_false(s:statement): statement->statement
      " if (false) \s" -> ";";
  rule simplify_if_then_true(s:statement): statement->statement
      " if (true) \s" -> "\s";
  rule simplify_if_then_else_false(s1:statement, s2:statement): statement->statement
      " if (false) \s1 else \s2" -> "\s2";
  rule simplify_if_then_else_true(s1:statement, s2: statement): statement->statement
      " if (true) \s1 else \s2" -> "\s2";

( "fold" ) switch , . , DMS , . DMS.

, DMS " " ; , . AST, , . , ( ", " false && x "?); , && || DMS ++ , .

, , - ( , ) . , ( " " ). , , . , ; , DMS ++ , ; . ( DMS C ).

(EDIT February 2015: ++ 14, /).

1.5M SLOC C ++ IBM Tivoli ; : -}

+5

:

, , , , .

, . constexpr, :

#define value1() true
#define value2() false
#define value3() 4

. , <valueX.h> , , , valueX() , :

// call this at startup to make sure our agreed on values haven't changed
void check_values() {
    assert(value1() == get_value1_from_shared_memory());
    assert(value2() == get_value2_from_shared_memory());
    assert(value3() == get_value3_from_shared_memory());
}
+1

All Articles