Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix objc colon getting extra spaces after ternary #4311

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/tokenizer/combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1136,8 +1136,7 @@ void do_symbol_check(Chunk *prev, Chunk *pc, Chunk *next)
&& !is_oc_block(pc)
&& pc->GetParentType() != CT_OC_MSG_DECL
&& pc->GetParentType() != CT_OC_MSG_SPEC
&& ( pc->IsString(")")
|| pc->Is(CT_FUNC_TYPE))
&& pc->IsString(")")
&& next->IsString("("))
{
if (language_is_set(lang_flag_e::LANG_D))
Expand Down
25 changes: 17 additions & 8 deletions src/tokenizer/mark_question_colon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
* return the chunk colon if found or Chunk::NullChunkPtr
* if a ; (CT_SEMI_COLON) chunk is found
*/
Chunk *search_for_colon(Chunk *pc_question)
Chunk *search_for_colon(Chunk *pc_question, int depth)
{
Chunk *pc2 = pc_question->GetNextNcNnl();
bool colon_found = false;
Chunk *pc2 = pc_question->GetNextNcNnl();
bool colon_found = false;
int square_bracket_depth = 0;

LOG_FMT(LCOMBINE, "%s(%d): pc_question.orig line is %zu, orig col is %zu, level is %zu, Text() is '%s'\n",
__func__, __LINE__, pc_question->GetOrigLine(), pc_question->GetOrigCol(), pc_question->GetLevel(),
Expand Down Expand Up @@ -67,7 +68,7 @@ Chunk *search_for_colon(Chunk *pc_question)
{
LOG_FMT(LCOMBINE, "%s(%d): orig line is %zu, orig col is %zu, level is %zu, Text() is '%s'\n",
__func__, __LINE__, pc2->GetOrigLine(), pc2->GetOrigCol(), pc2->GetLevel(), pc2->Text());
pc2 = search_for_colon(pc2);
pc2 = search_for_colon(pc2, depth + 1);
LOG_FMT(LCOMBINE, "%s(%d): orig line is %zu, orig col is %zu, level is %zu, Text() is '%s'\n",
__func__, __LINE__, pc2->GetOrigLine(), pc2->GetOrigCol(), pc2->GetLevel(), pc2->Text());
continue;
Expand Down Expand Up @@ -102,17 +103,19 @@ Chunk *search_for_colon(Chunk *pc_question)
}
}
}
else if (pc2->Is(CT_COLON))
else if (pc2->Is(CT_COLON)
&& square_bracket_depth == 0)
{
LOG_FMT(LCOMBINE, "%s(%d): orig line is %zu, orig col is %zu, level is %zu, Text() is '%s'\n",
__func__, __LINE__, pc2->GetOrigLine(), pc2->GetOrigCol(), pc2->GetLevel(), pc2->Text());

if (colon_found)
if (colon_found && depth > 0)
{
// There can only be another CT_COND_COLON if there is more than 1 CT_QUESTION (ie. depth > 0)
pc2->SetType(CT_COND_COLON);
return(pc2);
}
else
else if (!colon_found)
{
// E2 found orig line is 23, orig col is 3
pc2->SetType(CT_COND_COLON);
Expand All @@ -125,6 +128,12 @@ Chunk *search_for_colon(Chunk *pc_question)
colon_found = true;
}
}
else if (pc2->Is(CT_SQUARE_OPEN)) {
square_bracket_depth++;
}
else if (pc2->Is(CT_SQUARE_CLOSE)) {
square_bracket_depth--;
}
pc2 = pc2->GetNextNcNnl();
}

Expand Down Expand Up @@ -157,7 +166,7 @@ void mark_question_colon()
{
pc_question = pc;
// look for E2, COLON, E3...
pc = search_for_colon(pc);
pc = search_for_colon(pc, 0);

LOG_FMT(LCOMBINE, "%s(%d): orig line is %zu, orig col is %zu, level is %zu, Text() is '%s'\n",
__func__, __LINE__, pc->GetOrigLine(), pc->GetOrigCol(), pc->GetLevel(), pc->Text());
Expand Down
14 changes: 7 additions & 7 deletions tests/cli/output/in_fcn_def.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
# 4> TYPEDEF| NONE| PARENT_NOT_SET[ 1/ 1/ 8/ 0][0/0/0][ e 0000][0-0] typedef
# 4> TYPE| NONE| PARENT_NOT_SET[ 9/ 9/ 13/ 1][0/0/0][ 2 0080][0-1] void
# 4> FUNC_TYPE| TYPEDEF| PARENT_NOT_SET[ 14/ 14/ 22/ 1][0/0/0][ 80][0-0] function
# 4> FPAREN_OPEN| FUNC_CALL| PARENT_NOT_SET[ 22/ 22/ 23/ 0][0/0/0][ 2 0000 0080][0-0] (
# 4> TYPE| NONE| PARENT_NOT_SET[ 23/ 23/ 26/ 0][0/1/0][ 8a 0088][0-0] int
# 4> WORD| NONE| PARENT_NOT_SET[ 27/ 27/ 28/ 1][0/1/0][ 100 0088][0-0] i
# 4> COMMA| NONE| PARENT_NOT_SET[ 28/ 28/ 29/ 0][0/1/0][ 2 0000 0088][0-0] ,
# 4> TYPE| NONE| PARENT_NOT_SET[ 30/ 30/ 36/ 1][0/1/0][ 8a 0088][0-0] double
# 4> WORD| NONE| PARENT_NOT_SET[ 37/ 37/ 38/ 1][0/1/0][ 100 0088][0-0] d
# 4> FPAREN_CLOSE| FUNC_CALL| PARENT_NOT_SET[ 38/ 38/ 39/ 0][0/0/0][ 2 0000 0088][0-0] )
# 4> FPAREN_OPEN| TYPEDEF| PARENT_NOT_SET[ 22/ 22/ 23/ 0][0/0/0][ 2 0000 0080][0-0] (
# 4> TYPE| NONE| PARENT_NOT_SET[ 23/ 23/ 26/ 0][0/1/0][ 8a 0080][0-0] int
# 4> WORD| NONE| PARENT_NOT_SET[ 27/ 27/ 28/ 1][0/1/0][ 100 0080][0-0] i
# 4> COMMA| NONE| PARENT_NOT_SET[ 28/ 28/ 29/ 0][0/1/0][ 2 0000 0080][0-0] ,
# 4> TYPE| NONE| PARENT_NOT_SET[ 30/ 30/ 36/ 1][0/1/0][ 8a 0080][0-0] double
# 4> WORD| NONE| PARENT_NOT_SET[ 37/ 37/ 38/ 1][0/1/0][ 100 0080][0-0] d
# 4> FPAREN_CLOSE| TYPEDEF| PARENT_NOT_SET[ 38/ 38/ 39/ 0][0/0/0][ 2 0000 0080][0-0] )
# 4> SEMICOLON| TYPEDEF| PARENT_NOT_SET[ 39/ 39/ 40/ 0][0/0/0][ 2 0000 0080][0-0] ;
# 4> NEWLINE| NONE| PARENT_NOT_SET[ 40/ 40/ 1/ 0][0/0/0][ 0][2-0]
# -=====-
5 changes: 5 additions & 0 deletions tests/config/oc/60012.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sp_cond_colon = force
align_oc_msg_colon_span = 1
indent_columns = 4
indent_with_tabs = 0
indent_ternary_operator = 2
2 changes: 2 additions & 0 deletions tests/config/oc/60013.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
indent_columns = 4
indent_with_tabs = 0
5 changes: 5 additions & 0 deletions tests/config/oc/60014.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sp_cond_colon = force
align_oc_msg_colon_span = 1
indent_columns = 4
indent_with_tabs = 0
indent_ternary_operator = 2
5 changes: 5 additions & 0 deletions tests/expected/oc/60012-Issue_4304.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static void func() {
[NSObject doStuff:true ? 1 : 0
param2:@"test"
param3:@"test2"];
}
11 changes: 11 additions & 0 deletions tests/expected/oc/60013-Issue_4306.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import <Foundation/Foundation.h>

typedef NS_CLOSED_ENUM(NSUInteger, FOO) {
FOOA,
FOOB
};

typedef NS_CLOSED_ENUM(NSUInteger, BAR) {
BARA,
BARB
};
3 changes: 3 additions & 0 deletions tests/expected/oc/60014-4309.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
static void func() {
true ? [Foo test:@""] : @"bar";
}
3 changes: 3 additions & 0 deletions tests/input/oc/4309.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
static void func() {
true ? [Foo test:@""] : @"bar";
}
5 changes: 5 additions & 0 deletions tests/input/oc/Issue_4304.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
static void func() {
[NSObject doStuff:true ? 1 : 0
param2:@"test"
param3:@"test2"];
}
11 changes: 11 additions & 0 deletions tests/input/oc/Issue_4306.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#import <Foundation/Foundation.h>

typedef NS_CLOSED_ENUM(NSUInteger, FOO) {
FOOA,
FOOB
};

typedef NS_CLOSED_ENUM(NSUInteger, BAR) {
BARA,
BARB
};
3 changes: 3 additions & 0 deletions tests/objective-c.test
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,6 @@
# we need any expert to solve
#11032 oc/U10-Cpp.cfg oc/newlines.mm
60011 oc/UNI-11095.cfg oc/UNI-11095.mm
60012 oc/60012.cfg oc/Issue_4304.mm
60013 oc/60013.cfg oc/Issue_4306.mm
60014 oc/60014.cfg oc/4309.mm
Loading