src/ifdefs.cc
/* Let's enforce the following style rules:
 *   (Rule 1) If there are any blank lines in the vicinity of
 *       an #ifdef, place exactly one blank line before the
 *       #ifdef and none after.
 *   (Rule 2) If there are any blank lines in the vicinity of
 *       an #endif, place exactly one blank line after the
 *       #endif and none before.
 * In practice, this is equivalent to:
 *   (Imp 1) Collapse each contiguous series of blank lines into a
 *   single blank line, *unless* the first non-blank line on
 *   both sides have matching masks, in which case simply remove
 *   any blank lines that don't match that mask.
 *   (Imp 2) If a single blank line is preceded by a line with
 *   mask A, and followed by a line with mask B, we want the
 *   blank line's mask to be A|B.
 */
Let's enforce the following style rules:
(Rule 1) If there are any blank lines in the vicinity of
an #ifdef, place exactly one blank line before the
#ifdef and none after.
(Rule 2) If there are any blank lines in the vicinity of
an #endif, place exactly one blank line after the
#endif and none before.
In practice, this is equivalent to:
(Imp 1) Collapse each contiguous series of blank lines into a
single blank line, *unless* the first non-blank line on
both sides have matching masks, in which case simply remove
any blank lines that don't match that mask.
(Imp 2) If a single blank line is preceded by a line with
mask A, and followed by a line with mask B, we want the
blank line's mask to be A|B.
Let ' s enforce the following style rules : ( Rule 1 ) If there are any blank lines in the vicinity of an #ifdef place exactly one blank line before the #ifdef and none after . ( Rule 2 ) If there are any blank lines in the vicinity of an #endif place exactly one blank line after the #endif and none before . In practice this is equivalent to : ( Imp 1 ) Collapse each contiguous series of blank lines into a single blank line * unless * the first non - blank line on both sides have matching masks in which case simply remove any blank lines that don ' t match that mask . ( Imp 2 ) If a single blank line is preceded by a line with mask A and followed by a line with mask B we want the blank line ' s mask to be A|B .
static void collapse_blank_lines(Difdef::Diff &diff)
{
    for (size_t i=0; i < diff.lines.size(); ++i) {
        Difdef::Diff::Line &line = diff.lines[i];
        const bool is_blank_line = (*line.text == "");
        if (!is_blank_line) continue;
        size_t end = i;
        while (end < diff.lines.size() && *diff.lines[end].text == "") ++end;
        mask_t startmask = (i > 0) ? diff.lines[i-1].mask : diff.all_files_mask();
        mask_t endmask = (end < diff.lines.size()) ? diff.lines[end].mask : diff.all_files_mask();
        int blank_lines_we_still_want = 0;
        if (startmask == endmask) {
            for (size_t j = i; j < end; ++j) {
                if (contains(diff.lines[j].mask, startmask)) {
                    ++blank_lines_we_still_want;
                }
            }
        } else {
            blank_lines_we_still_want = 1;
        }
        for (size_t j = i; j < i + blank_lines_we_still_want; ++j) {
            diff.lines[j].mask = (startmask | endmask);
        }
        diff.lines.erase(diff.lines.begin() + i + blank_lines_we_still_want,
                         diff.lines.begin() + end);
        i += blank_lines_we_still_want-1;
    }
}
static void collapse_blank_lines ( Difdef :: Diff & diff ) { for ( size_t i = 0 ; i < diff . lines . size ( ) ; ++ i ) { Difdef :: Diff :: Line & line = diff . lines [ i ] ; const bool is_blank_line = ( * line . text == " " ) ; if ( ! is_blank_line ) continue ; size_t end = i ; while ( end < diff . lines . size ( ) && * diff . lines [ end ] . text == " " ) ++ end ; mask_t startmask = ( i > 0 ) ? diff . lines [ i - 1 ] . mask : diff . all_files_mask ( ) ; mask_t endmask = ( end < diff . lines . size ( ) ) ? diff . lines [ end ] . mask : diff . all_files_mask ( ) ; int blank_lines_we_still_want = 0 ; if ( startmask == endmask ) { for ( size_t j = i ; j < end ; ++ j ) { if ( contains ( diff . lines [ j ] . mask , startmask ) ) { ++ blank_lines_we_still_want ; } } } else { blank_lines_we_still_want = 1 ; } for ( size_t j = i ; j < i + blank_lines_we_still_want ; ++ j ) { diff . lines [ j ] . mask = ( startmask | endmask ) ; } diff . lines . erase ( diff . lines . begin ( ) + i + blank_lines_we_still_want , diff . lines . begin ( ) + end ) ; i += blank_lines_we_still_want - 1 ; } }
Let's enforce the following style rules:
   (Rule 1) If there are any blank lines in the vicinity of
       an #ifdef, place exactly one blank line before the
       #ifdef and none after.
Let ' s enforce the following style rules : ( Rule 1 ) If there are any blank lines in the vicinity of an #ifdef place exactly one blank line before the #ifdef and none after .
/* Find this series of blank lines. */ /* Look at the lines on either side. */ /* Preserve these blank lines; they don't border an #ifdef. */ /* This blank line appears in a superset of startmask. */ /* There will be an #if here. Reduce N blank lines to 1. */