Skip to content

Commit

Permalink
Merge branch 'Unity-Technologies-features/msref-support'
Browse files Browse the repository at this point in the history
  • Loading branch information
bengardner committed Apr 3, 2015
2 parents 2f8c55d + 27bbbfc commit b6593c1
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 30 deletions.
13 changes: 6 additions & 7 deletions src/align_stack.cpp
Expand Up @@ -167,8 +167,7 @@ void AlignStack::Add(chunk_t *start, int seqnum)
/* Find ref. Back up to the real item that is aligned. */
prev = start;
while (((prev = chunk_get_prev(prev)) != NULL) &&
(chunk_is_star(prev) ||
chunk_is_addr(prev) ||
(chunk_is_ptr_operator(prev) ||
(prev->type == CT_TPAREN_OPEN)))
{
/* do nothing - we want prev when this exits */
Expand All @@ -183,9 +182,9 @@ void AlignStack::Add(chunk_t *start, int seqnum)
ali = start;
if (m_star_style != SS_IGNORE)
{
/* back up to the first '*' preceding the token */
/* back up to the first '*' or '^' preceding the token */
prev = chunk_get_prev(ali);
while (chunk_is_star(prev))
while (chunk_is_star(prev) || chunk_is_msref(prev))
{
ali = prev;
prev = chunk_get_prev(ali);
Expand Down Expand Up @@ -237,7 +236,8 @@ void AlignStack::Add(chunk_t *start, int seqnum)
tmp = chunk_get_next(tmp);
}
if ((chunk_is_star(tmp) && (m_star_style == SS_DANGLE)) ||
(chunk_is_addr(tmp) && (m_amp_style == SS_DANGLE)))
(chunk_is_addr(tmp) && (m_amp_style == SS_DANGLE)) ||
(chunk_is_msref(tmp) && (m_star_style == SS_DANGLE))) // TODO: add m_msref_style
{
col_adj = start->column - ali->column;
gap = start->column - (ref->column + ref->len());
Expand Down Expand Up @@ -364,8 +364,7 @@ void AlignStack::Flush()
{
tmp = chunk_get_next(tmp);
}
if ((chunk_is_star(tmp) && (m_star_style == SS_DANGLE)) ||
(chunk_is_addr(tmp) && (m_amp_style == SS_DANGLE)))
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());
Expand Down
15 changes: 15 additions & 0 deletions src/chunk_list.h
Expand Up @@ -220,6 +220,21 @@ bool chunk_is_addr(chunk_t *pc)
}


static_inline
bool chunk_is_msref(chunk_t *pc) // ms compilers for C++/CLI and WinRT use '^' instead of '*' for marking up reference types vs pointer types
{
return((cpd.lang_flags & LANG_CPP) &&
((pc != NULL) && (pc->len() == 1) && (pc->str[0] == '^') && (pc->type != CT_OPERATOR_VAL)));
}


static_inline
bool chunk_is_ptr_operator(chunk_t *pc)
{
return chunk_is_star(pc) || chunk_is_addr(pc) || chunk_is_msref(pc);
}


bool chunk_is_newline_between(chunk_t *start, chunk_t *end);

static_inline
Expand Down
48 changes: 25 additions & 23 deletions src/combine.cpp
Expand Up @@ -66,7 +66,7 @@ void make_type(chunk_t *pc)
{
set_chunk_type(pc, CT_TYPE);
}
else if (chunk_is_star(pc))
else if (chunk_is_star(pc) || chunk_is_msref(pc))
{
set_chunk_type(pc, CT_PTR_TYPE);
}
Expand Down Expand Up @@ -864,6 +864,10 @@ void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
{
set_chunk_type(pc, (prev->type == CT_ANGLE_CLOSE) ? CT_PTR_TYPE : CT_DEREF);
}
if ((cpd.lang_flags & LANG_CPP) && (pc->type == CT_CARET) && (prev->type == CT_ANGLE_CLOSE))
{
set_chunk_type(pc, CT_PTR_TYPE);
}
if (pc->type == CT_MINUS)
{
set_chunk_type(pc, CT_NEG);
Expand Down Expand Up @@ -914,7 +918,7 @@ void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
tmp = chunk_skip_to_match(tmp);
tmp = chunk_get_next_ncnl(tmp);
}
if ((tmp != NULL) && (chunk_is_star(tmp) || chunk_is_addr(tmp) || (tmp->type == CT_WORD)))
if ((tmp != NULL) && (chunk_is_ptr_operator(tmp) || (tmp->type == CT_WORD)))
{
mark_variable_definition(tmp);
}
Expand Down Expand Up @@ -955,7 +959,7 @@ void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
}

/* Change CT_STAR to CT_PTR_TYPE or CT_ARITH or CT_DEREF */
if (pc->type == CT_STAR)
if (pc->type == CT_STAR || ((cpd.lang_flags & LANG_CPP) && (pc->type == CT_CARET)))
{
if (chunk_is_paren_close(next) || (next->type == CT_COMMA))
{
Expand All @@ -971,7 +975,7 @@ void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
set_chunk_type(next, CT_PTR_TYPE);
set_chunk_parent(next, pc->parent_type);
}
else if ((prev->type == CT_SIZEOF) || (prev->type == CT_DELETE))
else if ((pc->type == CT_STAR) && ((prev->type == CT_SIZEOF) || (prev->type == CT_DELETE)))
{
set_chunk_type(pc, CT_DEREF);
}
Expand All @@ -984,7 +988,7 @@ void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
{
set_chunk_type(pc, CT_PTR_TYPE);
}
else
else if (pc->type == CT_STAR)
{
/* most PCF_PUNCTUATOR chunks except a paren close would make this
* a deref. A paren close may end a cast or may be part of a macro fcn.
Expand Down Expand Up @@ -1265,7 +1269,7 @@ static void mark_function_return_type(chunk_t *fname, chunk_t *start, c_token_t
{
break;
}
if (!chunk_is_addr(pc) && !chunk_is_star(pc))
if (!chunk_is_ptr_operator(pc))
{
first = pc;
}
Expand Down Expand Up @@ -1695,14 +1699,15 @@ static void fix_casts(chunk_t *start)
return;
}

/* Make sure there is only WORD, TYPE, and '*' before the close paren */
/* Make sure there is only WORD, TYPE, and '*' or '^' before the close paren */
pc = chunk_get_next_ncnl(start);
first = pc;
while ((pc != NULL) && (chunk_is_type(pc) ||
(pc->type == CT_WORD) ||
(pc->type == CT_QUALIFIER) ||
(pc->type == CT_DC_MEMBER) ||
(pc->type == CT_STAR) ||
(pc->type == CT_CARET) ||
(pc->type == CT_TSQUARE) ||
(pc->type == CT_AMP)))
{
Expand Down Expand Up @@ -1741,8 +1746,9 @@ static void fix_casts(chunk_t *start)
}
paren_close = pc;

/* If last is a type or star, we have a cast for sure */
/* If last is a type or star/caret, we have a cast for sure */
if ((last->type == CT_STAR) ||
(last->type == CT_CARET) ||
(last->type == CT_PTR_TYPE) ||
(last->type == CT_TYPE))
{
Expand Down Expand Up @@ -1809,7 +1815,7 @@ static void fix_casts(chunk_t *start)
}

nope = false;
if (chunk_is_star(pc) || chunk_is_addr(pc))
if (chunk_is_ptr_operator(pc))
{
/* star (*) and addr (&) are ambiguous */
if ((after->type == CT_NUMBER_FP) ||
Expand Down Expand Up @@ -2054,7 +2060,7 @@ static void fix_enum_struct_union(chunk_t *pc)
flags &= ~PCF_VAR_1ST; /* clear the first flag for the next items */
}

if (next->type == CT_STAR)
if (next->type == CT_STAR || ((cpd.lang_flags & LANG_CPP) && (next->type == CT_CARET)))
{
set_chunk_type(next, CT_PTR_TYPE);
}
Expand Down Expand Up @@ -2536,7 +2542,7 @@ static void fix_fcn_def_params(chunk_t *start)
{
continue;
}
if (chunk_is_star(pc))
if (chunk_is_star(pc) || chunk_is_msref(pc))
{
set_chunk_type(pc, CT_PTR_TYPE);
cs.Push_Back(pc);
Expand Down Expand Up @@ -2609,8 +2615,7 @@ static chunk_t *fix_var_def(chunk_t *start)
(pc->type == CT_QUALIFIER) ||
(pc->type == CT_DC_MEMBER) ||
(pc->type == CT_MEMBER) ||
chunk_is_addr(pc) ||
chunk_is_star(pc)))
chunk_is_ptr_operator(pc)))
{
LOG_FMT(LFVD, " %s[%s]", pc->str.c_str(), get_token_name(pc->type));
cs.Push_Back(pc);
Expand Down Expand Up @@ -2770,7 +2775,7 @@ static chunk_t *mark_variable_definition(chunk_t *start)
__func__, pc->orig_line, pc->str.c_str(),
get_token_name(pc->type), pc->orig_col, flg, pc->flags);
}
else if (chunk_is_star(pc))
else if (chunk_is_star(pc) || chunk_is_msref(pc))
{
set_chunk_type(pc, CT_PTR_TYPE);
}
Expand Down Expand Up @@ -2852,8 +2857,7 @@ static bool can_be_full_param(chunk_t *start, chunk_t *end)
word_cnt--;
}
}
else if ((pc != start) && (chunk_is_star(pc) ||
chunk_is_addr(pc)))
else if ((pc != start) && chunk_is_ptr_operator(pc))
{
/* chunk is OK */
}
Expand Down Expand Up @@ -2947,7 +2951,7 @@ static bool can_be_full_param(chunk_t *start, chunk_t *end)
}

last = chunk_get_prev_ncnl(pc);
if (chunk_is_star(last) || chunk_is_addr(last))
if (chunk_is_ptr_operator(last))
{
LOG_FMT(LFPARAM, " <== [%s] sure!\n", get_token_name(pc->type));
return(true);
Expand Down Expand Up @@ -3054,7 +3058,7 @@ static void mark_function(chunk_t *pc)
}
}

if (chunk_is_star(next) || chunk_is_addr(next))
if (chunk_is_ptr_operator(next))
{
next = chunk_get_next_ncnlnp(next);
}
Expand Down Expand Up @@ -3130,7 +3134,7 @@ static void mark_function(chunk_t *pc)
}

if (chunk_is_str(tmp3, ")", 1) &&
(chunk_is_star(tmp1) ||
(chunk_is_star(tmp1) || chunk_is_msref(tmp1) ||
((cpd.lang_flags & LANG_OC) && chunk_is_token(tmp1, CT_CARET)))
&&
((tmp2 == NULL) || (tmp2->type == CT_WORD)))
Expand Down Expand Up @@ -3323,8 +3327,7 @@ static void mark_function(chunk_t *pc)
isa_def = true;
}

if (chunk_is_addr(prev) ||
chunk_is_star(prev))
if (chunk_is_ptr_operator(prev))
{
hit_star = true;
}
Expand All @@ -3335,8 +3338,7 @@ static void mark_function(chunk_t *pc)
(prev->type != CT_QUALIFIER) &&
(prev->type != CT_TYPE) &&
(prev->type != CT_WORD) &&
!chunk_is_addr(prev) &&
!chunk_is_star(prev))
!chunk_is_ptr_operator(prev))
{
LOG_FMT(LFCN, " --> Stopping on %s [%s]\n",
prev->str.c_str(), get_token_name(prev->type));
Expand Down
1 change: 1 addition & 0 deletions src/tokenize_cleanup.cpp
Expand Up @@ -395,6 +395,7 @@ void tokenize_cleanup(void)
(tmp->type != CT_TYPE) &&
(tmp->type != CT_QUALIFIER) &&
(tmp->type != CT_STAR) &&
(tmp->type != CT_CARET) &&
(tmp->type != CT_AMP) &&
(tmp->type != CT_TSQUARE))
{
Expand Down
1 change: 1 addition & 0 deletions tests/config/ms-style-ref.cfg
@@ -0,0 +1 @@
sp_arith=add
1 change: 1 addition & 0 deletions tests/cpp.test
Expand Up @@ -229,3 +229,4 @@
31711 string_replace_tab_chars.cfg cpp/string_replace_tab_chars.cpp

31720 ben.cfg cpp/bit-colon.cpp
31730 ms-style-ref.cfg cpp/ms-style-ref.cpp
9 changes: 9 additions & 0 deletions tests/input/cpp/ms-style-ref.cpp
@@ -0,0 +1,9 @@
Foo^ foo = dynamic_cast<Bar^>(bar);
Foo* foo = dynamic_cast<Bar*>(bar);
x = a^b;

int main(Platform::Array<Platform::String^>^ /*args*/)
{
}

Platform::Array<unsigned char>^ a;
9 changes: 9 additions & 0 deletions tests/output/cpp/31730-ms-style-ref.cpp
@@ -0,0 +1,9 @@
Foo^ foo = dynamic_cast<Bar^>(bar);
Foo* foo = dynamic_cast<Bar*>(bar);
x = a ^ b;

int main(Platform::Array<Platform::String^>^ /*args*/)
{
}

Platform::Array<unsigned char>^ a;

0 comments on commit b6593c1

Please sign in to comment.