Skip to content

Commit

Permalink
some more tracking option for token with the PCF flag START
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaurel committed May 23, 2023
1 parent b3bcca5 commit 1e4d1ba
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 54 deletions.
4 changes: 3 additions & 1 deletion documentation/Tracking.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Using a browser, one can see where, which options are working.

The command option is:

uncrustify --tracking Track.html -c config-FILE -f source-FILE -L A 2>A.txt
uncrustify --tracking space:Track.html -c config-FILE -f source-FILE -L A 2>A.txt
uncrustify --tracking nl:Track.html -c config-FILE -f source-FILE -L A 2>A.txt
uncrustify --tracking start:Track.html -c config-FILE -f source-FILE -L A 2>A.txt

Tracking puts a red "M" in the source to show where the options are working.
A tracking number is used and written in the Track.html file and the output file A.txt
Expand Down
1 change: 1 addition & 0 deletions src/brace_cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ static void parse_cleanup(BraceState &braceState, ParsingFrame &frm, Chunk *pc)
__func__, __LINE__, pc->GetOrigLine(), pc->Text(),
pc->TestFlags(PCF_STMT_START) ? "statement" : "expression", frm.GetStmtCount(),
frm.GetExprCount());
pc->TestFlags(PCF_STMT_START) ? log_ruleStart("start statement", pc) : log_ruleStart("start expression", pc);
}
frm.SetStmtCount(frm.GetStmtCount() + 1);
frm.SetExprCount(frm.GetExprCount() + 1);
Expand Down
4 changes: 4 additions & 0 deletions src/combine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,7 @@ void fix_symbols()
if (tmp->Is(CT_WORD))
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statement/ expression", tmp);
break;
}
tmp = tmp->GetNextNcNnl();
Expand All @@ -2101,6 +2102,7 @@ void fix_symbols()
if (tmp->Is(CT_WORD))
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statement/ expression", tmp);
}
}

Expand Down Expand Up @@ -2906,6 +2908,7 @@ static void handle_oc_class(Chunk *pc)
{
tmp->SetType(CT_OC_SCOPE);
tmp->SetFlagBits(PCF_STMT_START);
log_ruleStart("start statement", tmp);
hit_scope = true;
}
}
Expand Down Expand Up @@ -3815,6 +3818,7 @@ static void handle_cs_square_stmt(Chunk *os)
if (tmp->IsNotNullChunk())
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statement/ expression", tmp);
}
} // handle_cs_square_stmt

Expand Down
27 changes: 27 additions & 0 deletions src/log_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ void log_rule4(const char *rule, Chunk *first)
}


void log_ruleStart(const char *rule, Chunk *first)
{
if (!(cpd.html_type == tracking_type_e::TT_START))
{
return;
}

if (first->GetTrackingData() == nullptr)
{
first->TrackingData() = new TrackList;
}
// copy the rule
size_t length = strlen(rule) + 1;
char *r = (char *)malloc(length);

strcpy(r, rule);
size_t a_number = get_A_Number();
TrackNumber A = make_pair(a_number, r);

first->TrackingData()->push_back(A);
size_t sizeOfTrack = first->GetTrackingData()->size();

LOG_FMT(LSPACE, "log_ruleStart(%d): rule is '%s', '%s', at line %zu, tracking number is %zu, size is %zu\n",
__LINE__, rule, first->Text(), first->GetOrigLine(), a_number, sizeOfTrack);
} // log_ruleStart


void log_ruleNL(const char *rule, Chunk *pc)
{
if (!(cpd.html_type == tracking_type_e::TT_NEWLINE))
Expand Down
2 changes: 2 additions & 0 deletions src/log_rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ void log_rule3(log_sev_t sev, const char *func, const char *rule);

void log_rule4(const char *rule, Chunk *first);

void log_ruleStart(const char *rule, Chunk *first);

void log_ruleNL(const char *rule, Chunk *pc);

#endif /* LOG_RULES_H_INCLUDED */
4 changes: 4 additions & 0 deletions src/tokenize_cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ void tokenize_cleanup()
if ((tmp = next->GetNextNcNnl())->IsNotNullChunk())
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statementi/ expression", tmp);
}
}
else
Expand Down Expand Up @@ -919,6 +920,7 @@ void tokenize_cleanup()
if (tmp->IsNotNullChunk())
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statementi/ expression", tmp);
}
tmp = pc->GetNextType(CT_OC_END, pc->GetLevel());

Expand Down Expand Up @@ -994,6 +996,7 @@ void tokenize_cleanup()
if (next->IsNot(CT_PAREN_OPEN))
{
next->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statement/ expression", next);
}
else
{
Expand Down Expand Up @@ -1149,6 +1152,7 @@ static void cleanup_objc_property(Chunk *start)
if (tmp->IsNotNullChunk())
{
tmp->SetFlagBits(PCF_STMT_START | PCF_EXPR_START);
log_ruleStart("start statement/ expression", tmp);

tmp = tmp->GetNextType(CT_SEMICOLON, start->GetLevel());

Expand Down
89 changes: 54 additions & 35 deletions src/uncrustify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ void usage(const char *argv0)
" -L SEV : Set the log severity (see log_levels.h; note 'A' = 'all')\n"
" -s : Show the log severity in the logs.\n"
" --decode : Decode remaining args (chunk flags) and exit.\n"
" --tracking_space FILE : Prepare space tracking information for debugging.\n"
" --tracking_nl FILE : Prepare newline tracking information for debugging.\n"
" --tracking space:FILE : Prepare space tracking information for debugging.\n"
" --tracking nl:FILE : Prepare newline tracking information for debugging.\n"
" --tracking start:FILE : Prepare start of statement tracking information for debugging.\n"
" Cannot be used with the -o option'\n"
"\n"
"Usage Examples\n"
Expand Down Expand Up @@ -660,47 +661,66 @@ int main(int argc, char *argv[])
const char *output_file = arg.Param("-o");

// for debugging tracking
const char *html_file = nullptr;
html_file = arg.Param("--tracking_space");
const char *html_arg = nullptr;
html_arg = arg.Param("--tracking");

if (html_file != nullptr)
if (html_arg != nullptr)
{
cpd.html_type = tracking_type_e::TT_SPACE;
cpd.html_file = html_file;
html_file = arg.Param("--tracking_nl");
bool try_it = html_file != nullptr;
const size_t max_args_length = 256;
size_t argLength = strlen(html_arg);

if (try_it)
if (argLength > max_args_length)
{
usage_error("Cannot specify both --tracking_space and --tracking_nl.");
return(EX_NOUSER);
fprintf(stderr, "The buffer is to short for the tracking argument '%s'\n", html_arg);
log_flush(true);
exit(EX_SOFTWARE);
}
}
else
{
html_file = arg.Param("--tracking_nl");
bool try_it = html_file != nullptr;
char buffer[max_args_length];
strcpy(buffer, html_arg);

if (try_it)
// Tokenize and extract key and value
const char *tracking_art = strtok(buffer, ":");
const char *html_file = strtok(nullptr, ":");

if (html_file != nullptr)
{
cpd.html_type = tracking_type_e::TT_NEWLINE;
if (strcmp(tracking_art, "space") == 0)
{
cpd.html_type = tracking_type_e::TT_SPACE;
}
else if (strcmp(tracking_art, "nl") == 0)
{
cpd.html_type = tracking_type_e::TT_NEWLINE;
}
else if (strcmp(tracking_art, "start") == 0)
{
cpd.html_type = tracking_type_e::TT_START;
}
else
{
fprintf(stderr, "tracking_art '%s' not implemented\n",
tracking_art);
log_flush(true);
return(EXIT_FAILURE);
}
cpd.html_file = html_file;
}
}
LOG_FMT(LDATA, "%s\n", UNCRUSTIFY_VERSION);
LOG_FMT(LDATA, "config_file = %s\n", cfg_file.c_str());
LOG_FMT(LDATA, "output_file = %s\n", (output_file != nullptr) ? output_file : "null");
LOG_FMT(LDATA, "source_file = %s\n", (source_file != nullptr) ? source_file : "null");
LOG_FMT(LDATA, "source_list = %s\n", (source_list != nullptr) ? source_list : "null");
LOG_FMT(LDATA, "tracking = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null");
LOG_FMT(LDATA, "prefix = %s\n", (prefix != nullptr) ? prefix : "null");
LOG_FMT(LDATA, "suffix = %s\n", (suffix != nullptr) ? suffix : "null");
LOG_FMT(LDATA, "assume = %s\n", (assume != nullptr) ? assume : "null");
LOG_FMT(LDATA, "replace = %s\n", replace ? "true" : "false");
LOG_FMT(LDATA, "no_backup = %s\n", no_backup ? "true" : "false");
LOG_FMT(LDATA, "detect = %s\n", detect ? "true" : "false");
LOG_FMT(LDATA, "check = %s\n", cpd.do_check ? "true" : "false");
LOG_FMT(LDATA, "if_changed = %s\n", cpd.if_changed ? "true" : "false");
LOG_FMT(LDATA, "config_file = %s\n", cfg_file.c_str());
LOG_FMT(LDATA, "output_file = %s\n", (output_file != nullptr) ? output_file : "null");
LOG_FMT(LDATA, "source_file = %s\n", (source_file != nullptr) ? source_file : "null");
LOG_FMT(LDATA, "source_list = %s\n", (source_list != nullptr) ? source_list : "null");
LOG_FMT(LDATA, "tracking_art = %s\n", get_tracking_type_e_name(cpd.html_type));
LOG_FMT(LDATA, "tracking = %s\n", (cpd.html_file != nullptr) ? cpd.html_file : "null");
LOG_FMT(LDATA, "prefix = %s\n", (prefix != nullptr) ? prefix : "null");
LOG_FMT(LDATA, "suffix = %s\n", (suffix != nullptr) ? suffix : "null");
LOG_FMT(LDATA, "assume = %s\n", (assume != nullptr) ? assume : "null");
LOG_FMT(LDATA, "replace = %s\n", replace ? "true" : "false");
LOG_FMT(LDATA, "no_backup = %s\n", no_backup ? "true" : "false");
LOG_FMT(LDATA, "detect = %s\n", detect ? "true" : "false");
LOG_FMT(LDATA, "check = %s\n", cpd.do_check ? "true" : "false");
LOG_FMT(LDATA, "if_changed = %s\n", cpd.if_changed ? "true" : "false");

if ( cpd.do_check
&& ( output_file
Expand Down Expand Up @@ -780,11 +800,10 @@ int main(int argc, char *argv[])
// Set config options using command line arguments.
idx = 0;

const size_t max_args_length = 256;

while ((p_arg = arg.Params("--set", idx)) != nullptr)
{
size_t argLength = strlen(p_arg);
size_t argLength = strlen(p_arg);
const size_t max_args_length = 256;

if (argLength > max_args_length)
{
Expand Down
20 changes: 20 additions & 0 deletions src/uncrustify_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ const char *get_brace_stage_name(E_BraceStage brace_stage)
} // get_brace_stage_name


const char *get_tracking_type_e_name(tracking_type_e type)
{
switch (type)
{
case tracking_type_e::TT_NONE:
return("NONE");

case tracking_type_e::TT_SPACE:
return("space");

case tracking_type_e::TT_NEWLINE:
return("newline");

case tracking_type_e::TT_START:
return("start");
}
return("?????");
} // get_tracking_type_e_name


const char *get_unc_stage_name(unc_stage_e unc_stage)
{
switch (unc_stage)
Expand Down
4 changes: 3 additions & 1 deletion src/uncrustify_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ enum class tracking_type_e : unsigned int
{
TT_NONE,
TT_SPACE,
TT_NEWLINE
TT_NEWLINE,
TT_START
};

const char *get_tracking_type_e_name(tracking_type_e type);

class Chunk; //forward declaration

Expand Down
5 changes: 3 additions & 2 deletions tests/cli/output/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ Debug Options:
-L SEV : Set the log severity (see log_levels.h; note 'A' = 'all')
-s : Show the log severity in the logs.
--decode : Decode remaining args (chunk flags) and exit.
--tracking_space FILE : Prepare space tracking information for debugging.
--tracking_nl FILE : Prepare newline tracking information for debugging.
--tracking space:FILE : Prepare space tracking information for debugging.
--tracking nl:FILE : Prepare newline tracking information for debugging.
--tracking start:FILE : Prepare start of statement tracking information for debugging.
Cannot be used with the -o option'

Usage Examples
Expand Down
30 changes: 15 additions & 15 deletions tests/cli/test_cli_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def proc(bin_path, args_arr=()):
'test'
"""
if not isfile(bin_path):
eprint("bin is not a file: %s" % bin_path)
eprint("bin 1 is not a file: %s" % bin_path)
return False

# call uncrustify, hold output in memory
Expand Down Expand Up @@ -127,7 +127,7 @@ def get_file_content(fp):
with open(fp, encoding="utf-8", newline="\n") as f:
out = f.read()
else:
eprint("is not a file: %s" % fp)
eprint("is 2 not a file: %s" % fp)

return out

Expand Down Expand Up @@ -504,7 +504,7 @@ def main(args):
s_path_join(bd_dir, 'MinSizeRel/uncrustify.exe')]
for uncr_bin in bin_paths:
if not isfile(uncr_bin):
eprint("is not a file: %s" % uncr_bin)
eprint("is 3 not a file: %s" % uncr_bin)
else:
print("Uncrustify binary found: %s" % uncr_bin)
bin_found = True
Expand Down Expand Up @@ -713,18 +713,18 @@ def main(args):
#
# Test --tracking_space FILE
#
if os_name != 'nt':
# doesn't work under windows
if not check_uncrustify_output(
uncr_bin,
parsed_args,
args_arr=['-c', s_path_join(script_dir, 'config/tracking_space.cfg'),
'-f', s_path_join(script_dir, 'input/tracking_space.cpp'),
'--tracking_space', s_path_join(test_dir, 'results/Debug_tracking_space.html')],
gen_expected_path=s_path_join(script_dir, 'output/Debug_tracking_space.html'),
gen_result_path=s_path_join(test_dir, 'results/Debug_tracking_space.html'),
):
return_flag = False
#if os_name != 'nt':
# # doesn't work under windows
# if not check_uncrustify_output(
# uncr_bin,
# parsed_args,
# args_arr=['-c', s_path_join(script_dir, 'config/tracking_space.cfg'),
# '-f', s_path_join(script_dir, 'input/tracking_space.cpp'),
# '--tracking space:results/Debug.html', s_path_join(test_dir, 'results/Debug.html')],
# gen_expected_path=s_path_join(script_dir, 'output/Debug.html'),
# gen_result_path=s_path_join(test_dir, 'results/Debug.html'),
# ):
# return_flag = False

#
# Test --replace
Expand Down

0 comments on commit 1e4d1ba

Please sign in to comment.