Logo Search packages:      
Sourcecode: uncrustify version File versions

static void newlines_brace_pair ( chunk_t br_open  )  [static]

Handles the brace_on_func_line setting and decides if the closing brace of a pair should be right after a newline. The only cases where the closing brace shouldn't be the first thing on a line is where the opening brace has junk after it AND where a one-liner in a class is supposed to be preserved.

General rule for break before close brace: If the brace is part of a function (call or definition) OR if the only thing after the opening brace is comments, the there must be a newline before the close brace.

Example of no newline before close struct mystring { int len; char str[]; }; while (*(++ptr) != 0) { }

Examples of newline before close void foo() { }

Insert a newline between the '=' and open brace, if needed

If the open brace hits a CT_NEWLINE, CT_NL_CONT, CT_COMMENT_MULTI, or CT_COMMENT_CPP without hitting anything other than CT_COMMENT, then there should be a newline before the close brace.

Definition at line 734 of file newlines.cpp.

References chunk_get_next_nc(), chunk_get_next_ncnl(), chunk_get_next_nnl(), chunk_get_next_type(), chunk_get_prev_nblank(), chunk_get_prev_nc(), chunk_get_prev_ncnl(), chunk_t::flags, chunk_t::level, newline_add_between, newline_del_between, newline_fnc_var_def(), chunk_t::nl_count, chunk_t::parent_type, and chunk_t::type.

Referenced by newlines_cleanup_braces().

{
   chunk_t  *prev;
   chunk_t  *next;
   chunk_t  *pc;
   argval_t val            = AV_IGNORE;
   bool     nl_close_brace = false;

   if (((br_open->flags & PCF_IN_PREPROC) != 0) &&
       !cpd.settings[UO_nl_define_macro].b)
   {
      return;
   }

   if (cpd.settings[UO_nl_collapse_empty_body].b)
   {
      next = chunk_get_next_nnl(br_open);
      if ((next != NULL) && (next->type == CT_BRACE_CLOSE))
      {
         pc = chunk_get_next(br_open);

         while ((pc != NULL) && (pc->type != CT_BRACE_CLOSE))
         {
            next = chunk_get_next(pc);
            if (pc->type == CT_NEWLINE)
            {
               chunk_del(pc);
            }
            pc = next;
         }
         return;
      }
   }

   if ((br_open->flags & PCF_ONE_CLASS) == PCF_ONE_CLASS)
   {
      return;
   }

   next = chunk_get_next_nc(br_open);

   /** Insert a newline between the '=' and open brace, if needed */
   if (br_open->parent_type == CT_ASSIGN)
   {
      /* Only mess with it if the open brace is followed by a newline */
      if (chunk_is_newline(next))
      {
         prev = chunk_get_prev_ncnl(br_open);
         if ((cpd.settings[UO_nl_assign_brace].a & AV_ADD) != 0)
         {
            newline_add_between(prev, br_open);
         }
         else if ((cpd.settings[UO_nl_assign_brace].a & AV_REMOVE) != 0)
         {
            newline_del_between(prev, br_open);
         }
      }
   }

   /* Eat any extra newlines after the brace open */
   if (cpd.settings[UO_eat_blanks_after_open_brace].b)
   {
      if (chunk_is_newline(next))
      {
         if (next->nl_count > 1)
         {
            next->nl_count = 1;
         }
      }
   }

   /* Handle the cases where the brace is part of a function call or definition */
   if ((br_open->parent_type == CT_FUNC_DEF) ||
       (br_open->parent_type == CT_FUNC_CALL) ||
       (br_open->parent_type == CT_FUNC_CLASS))
   {
      /* Need to force a newline before the close brace, if not in a class body */
      if ((br_open->flags & PCF_IN_CLASS) == 0)
      {
         nl_close_brace = true;
      }

      /* handle newlines after the open brace */
      pc = chunk_get_next_ncnl(br_open);
      newline_add_between(br_open, pc);

      val = ((br_open->parent_type == CT_FUNC_DEF) ||
             (br_open->parent_type == CT_FUNC_CLASS)) ?
            cpd.settings[UO_nl_fdef_brace].a :
            cpd.settings[UO_nl_fcall_brace].a;

      if (val != AV_IGNORE)
      {
         /* Grab the chunk before the open brace */
         prev = chunk_get_prev_ncnl(br_open);

         if (val & AV_ADD)
         {
            newline_add_between(prev, br_open);
         }
         else if (val & AV_REMOVE)
         {
            newline_del_between(prev, br_open);
         }
      }

      if (cpd.settings[UO_nl_func_var_def_blk].n > 0)
      {
         newline_fnc_var_def(br_open, cpd.settings[UO_nl_func_var_def_blk].n);
      }
   }

   /* Grab the matching brace close */
   chunk_t *br_close;
   br_close = chunk_get_next_type(br_open, CT_BRACE_CLOSE, br_open->level);
   if (br_close == NULL)
   {
      return;
   }

   if (!nl_close_brace)
   {
      /**
       * If the open brace hits a CT_NEWLINE, CT_NL_CONT, CT_COMMENT_MULTI, or
       * CT_COMMENT_CPP without hitting anything other than CT_COMMENT, then
       * there should be a newline before the close brace.
       */
      pc = chunk_get_next(br_open);
      while ((pc != NULL) && (pc->type == CT_COMMENT))
      {
         pc = chunk_get_next(pc);
      }
      if (chunk_is_newline(pc) || chunk_is_comment(pc))
      {
         nl_close_brace = true;
      }
   }

   prev = chunk_get_prev_nblank(br_close);
   if (nl_close_brace)
   {
      newline_add_between(prev, br_close);
   }
   else
   {
      newline_del_between(prev, br_close);
   }

   if (cpd.settings[UO_eat_blanks_before_close_brace].b)
   {
      pc = chunk_get_prev_nc(br_close);
      if (chunk_is_newline(pc))
      {
         if (pc->nl_count > 1)
         {
            pc->nl_count = 1;
         }
      }
   }
}


Generated by  Doxygen 1.6.0   Back to index