Beispiel #1
0
/**
 * We are in a virtual brace and hit a newline.
 * If this should end the vbrace, then insert a VSEMICOLON and return that.
 *
 * @param pc   The newline (CT_NEWLINE)
 * @return     Either the newline or the newly inserted virtual semicolon
 */
chunk_t *pawn_check_vsemicolon(chunk_t *pc)
{
   chunk_t *vb_open;
   chunk_t *prev;

   /* Grab the open VBrace */
   vb_open = chunk_get_prev_type(pc, CT_VBRACE_OPEN, -1);

   /**
    * Grab the item before the newline
    * Don't do anything if:
    *  - the only thing previous is the V-Brace open
    *  - in a preprocessor
    *  - level > (vb_open->level + 1) -- ie, in () or []
    *  - it is something that needs a continuation
    *    + arith, assign, bool, comma, compare
    */
   prev = chunk_get_prev_ncnl(pc);
   if ((prev == NULL) ||
       (prev == vb_open) ||
       ((prev->flags & PCF_IN_PREPROC) != 0) ||
       pawn_continued(prev, vb_open->level + 1))
   {
      if (prev != NULL)
      {
         LOG_FMT(LPVSEMI, "%s:  no  VSEMI on line %d, prev='%s' [%s]\n",
                 __func__, prev->orig_line, prev->str.c_str(), get_token_name(prev->type));
      }
      return(pc);
   }

   return(pawn_add_vsemi_after(prev));
}
Beispiel #2
0
static void examine_braces(void)
{
   LOG_FUNC_ENTRY();

   chunk_t *pc = chunk_get_tail();
   while (pc != NULL)
   {
      chunk_t *prev = chunk_get_prev_type(pc, CT_BRACE_OPEN, -1);
      if ((pc->type == CT_BRACE_OPEN) &&
          ((pc->flags & PCF_IN_PREPROC) == 0))
      {
         if ((((pc->parent_type == CT_IF) ||
               (pc->parent_type == CT_ELSE) ||
               (pc->parent_type == CT_ELSEIF)) &&
              ((cpd.settings[UO_mod_full_brace_if].a) == AV_REMOVE)) ||
             ((pc->parent_type == CT_DO) &&
              ((cpd.settings[UO_mod_full_brace_do].a) == AV_REMOVE)) ||
             ((pc->parent_type == CT_FOR) &&
              ((cpd.settings[UO_mod_full_brace_for].a) == AV_REMOVE)) ||
             ((pc->parent_type == CT_USING_STMT) &&
              ((cpd.settings[UO_mod_full_brace_using].a) == AV_REMOVE)) ||
             ((pc->parent_type == CT_WHILE) &&
              ((cpd.settings[UO_mod_full_brace_while].a) == AV_REMOVE)))
         {
            examine_brace(pc);
         }
      }
      pc = prev;
   }
}
Beispiel #3
0
/**
 * We are on a semicolon that is after an unidentified brace close.
 * Check for what is before the brace open.
 * Do not remove if it is a square close, word or type.
 */
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close)
{
   chunk_t *pc;

   pc = chunk_get_prev_type(brace_close, CT_BRACE_OPEN, brace_close->level);
   pc = chunk_get_prev_ncnl(pc);
   if ((pc != NULL) &&
       (pc->type != CT_WORD) &&
       (pc->type != CT_TYPE) &&
       (pc->type != CT_SQUARE_CLOSE) &&
       (pc->type != CT_TSQUARE))
   {
      remove_semicolon(semi);
   }
}
Beispiel #4
0
static void check_unknown_brace_close(chunk_t *semi, chunk_t *brace_close)
{
   LOG_FUNC_ENTRY();
   chunk_t *pc = chunk_get_prev_type(brace_close, CT_BRACE_OPEN, brace_close->level);
   pc = chunk_get_prev_ncnl(pc);
   if (  pc != nullptr
      && pc->type != CT_RETURN
      && pc->type != CT_WORD
      && pc->type != CT_TYPE
      && pc->type != CT_SQUARE_CLOSE
      && pc->type != CT_ANGLE_CLOSE
      && pc->type != CT_TSQUARE
      && !chunk_is_paren_close(pc))
   {
      remove_semicolon(semi);
   }
}
Beispiel #5
0
static void add_msg_header(c_token_t type, file_mem& fm)
{
   chunk_t *pc;
   chunk_t *ref;
   chunk_t *tmp;
   bool    do_insert;

   for (pc = chunk_get_head(); pc != NULL; pc = chunk_get_next_ncnlnp(pc))
   {
      if (pc->type != type)
      {
         continue;
      }

      do_insert = false;

      /* On a function proto or def. Back up to a close brace or semicolon on
       * the same level
       */
      ref = pc;
      while ((ref = chunk_get_prev(ref)) != NULL)
      {
         /* ignore the CT_TYPE token that is the result type */
         if ((ref->level != pc->level) &&
             ((ref->type == CT_TYPE) ||
              (ref->type == CT_PTR_TYPE)))
         {
            continue;
         }

         if ((ref->level != pc->level) && (ref->type == CT_OC_CATEGORY))
         {
            ref = chunk_get_next_ncnl(ref);
            if (ref)
            {
               do_insert = true;
            }
            break;
         }

         /* Bail if we change level or find an access specifier colon */
         if ((ref->level != pc->level) || (ref->type == CT_PRIVATE_COLON))
         {
            do_insert = true;
            break;
         }

         /* If we hit an angle close, back up to the angle open */
         if (ref->type == CT_ANGLE_CLOSE)
         {
            ref = chunk_get_prev_type(ref, CT_ANGLE_OPEN, ref->level, CNAV_PREPROC);
            continue;
         }

         /* Bail if we hit a preprocessor and cmt_insert_before_preproc is false */
         if (ref->flags & PCF_IN_PREPROC)
         {
            tmp = chunk_get_prev_type(ref, CT_PREPROC, ref->level);
            if ((tmp != NULL) && (tmp->parent_type == CT_PP_IF))
            {
               tmp = chunk_get_prev_nnl(tmp);
               if (chunk_is_comment(tmp) &&
                   !cpd.settings[UO_cmt_insert_before_preproc].b)
               {
                  break;
               }
            }
         }

         /* Ignore 'right' comments */
         if (chunk_is_comment(ref) && chunk_is_newline(chunk_get_prev(ref)))
         {
            break;
         }

         if ((ref->level == pc->level) &&
             ((ref->flags & PCF_IN_PREPROC) ||
              (ref->type == CT_SEMICOLON) ||
              (ref->type == CT_BRACE_CLOSE) ||
              (ref->type == CT_OC_CLASS)))
         {
            do_insert = true;
            break;
         }
      }

      if (do_insert)
      {
         /* Insert between after and ref */
         chunk_t *after = chunk_get_next_ncnl(ref);
         tokenize(fm.data, after);
         for (tmp = chunk_get_next(ref); tmp != after; tmp = chunk_get_next(tmp))
         {
            tmp->level = after->level;
         }
      }
   }
}
Beispiel #6
0
/**
 * Aligns all the stuff in m_aligned.
 * Re-adds 'newer' items in m_skipped.
 */
void AlignStack::Flush()
{
   int                     last_seqnum = 0;
   int                     idx;
   int                     tmp_col;
   const ChunkStack::Entry *ce = NULL;
   chunk_t                 *pc;

   LOG_FMT(LAS, "%s: m_aligned.Len()=%d\n", __func__, m_aligned.Len());
   LOG_FMT(LAS, "Flush (min=%d, max=%d)\n", m_min_col, m_max_col);
   if (m_aligned.Len() == 1)
   {
      // check if we have *one* typedef in the line
      pc = m_aligned.Get(0)->m_pc;
      chunk_t *temp = chunk_get_prev_type(pc, CT_TYPEDEF, pc->level);
      if (temp != NULL)
      {
         if (pc->orig_line == temp->orig_line)
         {
            // reset the gap only for *this* stack
            m_gap = 1;
         }
      }
   }

   m_last_added = 0;
   m_max_col    = 0;

   /* Recalculate the max_col - it may have shifted since the last Add() */
   for (idx = 0; idx < m_aligned.Len(); idx++)
   {
      pc = m_aligned.Get(idx)->m_pc;

      /* Set the column adjust and gap */
      int col_adj = 0;
      int gap     = 0;
      if (pc != pc->align.ref)
      {
         gap = pc->column - (pc->align.ref->column + pc->align.ref->len());
      }
      chunk_t *tmp = pc;
      if (tmp->type == CT_TPAREN_OPEN)
      {
         tmp = chunk_get_next(tmp);
      }
      if (chunk_is_ptr_operator(tmp) && (m_star_style == SS_DANGLE))
      {
         col_adj = pc->align.start->column - pc->column;
         gap     = pc->align.start->column - (pc->align.ref->column + pc->align.ref->len());
      }
      if (m_right_align)
      {
         /* Adjust the width for signed numbers */
         int start_len = pc->align.start->len();
         if (pc->align.start->type == CT_NEG)
         {
            tmp = chunk_get_next(pc->align.start);
            if ((tmp != NULL) && (tmp->type == CT_NUMBER))
            {
               start_len += tmp->len();
            }
         }
         col_adj += start_len;
      }

      pc->align.col_adj = col_adj;

      /* See if this pushes out the max_col */
      int endcol = pc->column + col_adj;
      if (gap < m_gap)
      {
         endcol += m_gap - gap;
      }
      if (endcol > m_max_col)
      {
         m_max_col = endcol;
      }
   }

   if (cpd.settings[UO_align_on_tabstop].b && (m_aligned.Len() > 1))
   {
      m_max_col = align_tab_column(m_max_col);
   }

   LOG_FMT(LAS, "%s: m_aligned.Len()=%d\n",
           __func__, m_aligned.Len());
   for (idx = 0; idx < m_aligned.Len(); idx++)
   {
      ce = m_aligned.Get(idx);
      pc = ce->m_pc;

      tmp_col = m_max_col - pc->align.col_adj;
      if (idx == 0)
      {
         if (m_skip_first && (pc->column != tmp_col))
         {
            LOG_FMT(LAS, "%s: %lu:%lu dropping first item due to skip_first\n",
                    __func__, pc->orig_line, pc->orig_col);
            m_skip_first = false;
            m_aligned.Pop_Front();
            Flush();
            m_skip_first = true;
            return;
         }
         chunk_flags_set(pc, PCF_ALIGN_START);

         pc->align.right_align = m_right_align;
         pc->align.amp_style   = (int)m_amp_style;
         pc->align.star_style  = (int)m_star_style;
      }
      pc->align.gap  = m_gap;
      pc->align.next = m_aligned.GetChunk(idx + 1);

      /* Indent the token, taking col_adj into account */
      LOG_FMT(LAS, "%s: line %lu: '%s' to col %d (adj=%d)\n",
              __func__, pc->orig_line, pc->text(), tmp_col, pc->align.col_adj);
      align_to_column(pc, tmp_col);
   }

   if (ce != NULL)
   {
      last_seqnum = ce->m_seqnum;
      m_aligned.Reset();
   }
   m_min_col = 9999;
   m_max_col = 0;

   if (m_skipped.Empty())
   {
      /* Nothing was skipped, sync the seqnums */
      m_nl_seqnum = m_seqnum;
   }
   else
   {
      /* Remove all items with seqnum < last_seqnum */
      for (idx = 0; idx < m_skipped.Len(); idx++)
      {
         if (m_skipped.Get(idx)->m_seqnum < last_seqnum)
         {
            m_skipped.Zap(idx);
         }
      }
      m_skipped.Collapse();

      /* Add all items from the skipped list */
      ReAddSkipped();
   }
} // AlignStack::Flush