Skip to content

Commit 67435d9

Browse files
committed
patch 8.0.1207: profiling skips the first and last script line
Problem: Profiling skips the first and last script line. Solution: Check for BOM after setting script ID. (Lemonboy, closes #2103, closes #2112) Add a test. List the trailing script lines.
1 parent fafcf0d commit 67435d9

3 files changed

Lines changed: 69 additions & 28 deletions

File tree

src/ex_cmds2.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ script_do_profile(scriptitem_T *si)
17141714
}
17151715

17161716
/*
1717-
* save time when starting to invoke another script or function.
1717+
* Save time when starting to invoke another script or function.
17181718
*/
17191719
void
17201720
script_prof_save(
@@ -1805,12 +1805,14 @@ script_dump_profile(FILE *fd)
18051805
fprintf(fd, "Cannot open file!\n");
18061806
else
18071807
{
1808-
for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1808+
/* Keep going till the end of file, so that trailing
1809+
* continuation lines are listed. */
1810+
for (i = 0; ; ++i)
18091811
{
18101812
if (vim_fgets(IObuff, IOSIZE, sfd))
18111813
break;
1812-
pp = &PRL_ITEM(si, i);
1813-
if (pp->snp_count > 0)
1814+
if (i < si->sn_prl_ga.ga_len
1815+
&& (pp = &PRL_ITEM(si, i))->snp_count > 0)
18141816
{
18151817
fprintf(fd, "%5d ", pp->snp_count);
18161818
if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
@@ -4234,27 +4236,6 @@ do_source(
42344236
save_sourcing_lnum = sourcing_lnum;
42354237
sourcing_lnum = 0;
42364238

4237-
#ifdef FEAT_MBYTE
4238-
cookie.conv.vc_type = CONV_NONE; /* no conversion */
4239-
4240-
/* Read the first line so we can check for a UTF-8 BOM. */
4241-
firstline = getsourceline(0, (void *)&cookie, 0);
4242-
if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
4243-
&& firstline[1] == 0xbb && firstline[2] == 0xbf)
4244-
{
4245-
/* Found BOM; setup conversion, skip over BOM and recode the line. */
4246-
convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
4247-
p = string_convert(&cookie.conv, firstline + 3, NULL);
4248-
if (p == NULL)
4249-
p = vim_strsave(firstline + 3);
4250-
if (p != NULL)
4251-
{
4252-
vim_free(firstline);
4253-
firstline = p;
4254-
}
4255-
}
4256-
#endif
4257-
42584239
#ifdef STARTUPTIME
42594240
if (time_fd != NULL)
42604241
time_push(&tv_rel, &tv_start);
@@ -4347,6 +4328,27 @@ do_source(
43474328
# endif
43484329
#endif
43494330

4331+
#ifdef FEAT_MBYTE
4332+
cookie.conv.vc_type = CONV_NONE; /* no conversion */
4333+
4334+
/* Read the first line so we can check for a UTF-8 BOM. */
4335+
firstline = getsourceline(0, (void *)&cookie, 0);
4336+
if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
4337+
&& firstline[1] == 0xbb && firstline[2] == 0xbf)
4338+
{
4339+
/* Found BOM; setup conversion, skip over BOM and recode the line. */
4340+
convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
4341+
p = string_convert(&cookie.conv, firstline + 3, NULL);
4342+
if (p == NULL)
4343+
p = vim_strsave(firstline + 3);
4344+
if (p != NULL)
4345+
{
4346+
vim_free(firstline);
4347+
firstline = p;
4348+
}
4349+
}
4350+
#endif
4351+
43504352
/*
43514353
* Call do_cmdline, which will call getsourceline() to get the lines.
43524354
*/
@@ -4829,7 +4831,8 @@ script_line_start(void)
48294831
{
48304832
/* Grow the array before starting the timer, so that the time spent
48314833
* here isn't counted. */
4832-
(void)ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
4834+
(void)ga_grow(&si->sn_prl_ga,
4835+
(int)(sourcing_lnum - si->sn_prl_ga.ga_len));
48334836
si->sn_prl_idx = sourcing_lnum - 1;
48344837
while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
48354838
&& si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
@@ -4864,7 +4867,7 @@ script_line_exec(void)
48644867
}
48654868

48664869
/*
4867-
* Called when done with a function line.
4870+
* Called when done with a script line.
48684871
*/
48694872
void
48704873
script_line_end(void)

src/testdir/test_profile.vim

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func Test_profile_file()
115115
call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
116116
call assert_equal('', lines[4])
117117
call assert_equal('count total (s) self (s)', lines[5])
118-
call assert_equal(' func! Foo()', lines[6])
118+
call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
119119
call assert_equal(' endfunc', lines[7])
120120
" Loop iterates 10 times. Since script runs twice, body executes 20 times.
121121
" First line of loop executes one more time than body to detect end of loop.
@@ -132,6 +132,42 @@ func Test_profile_file()
132132
call delete('Xprofile_file.log')
133133
endfunc
134134

135+
func Test_profile_file_with_cont()
136+
let lines = [
137+
\ 'echo "hello',
138+
\ ' \ world"',
139+
\ 'echo "foo ',
140+
\ ' \bar"',
141+
\ ]
142+
143+
call writefile(lines, 'Xprofile_file.vim')
144+
call system(v:progpath
145+
\ . ' -es --clean'
146+
\ . ' -c "profile start Xprofile_file.log"'
147+
\ . ' -c "profile file Xprofile_file.vim"'
148+
\ . ' -c "so Xprofile_file.vim"'
149+
\ . ' -c "qall!"')
150+
call assert_equal(0, v:shell_error)
151+
152+
let lines = readfile('Xprofile_file.log')
153+
call assert_equal(11, len(lines))
154+
155+
call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
156+
call assert_equal('Sourced 1 time', lines[1])
157+
call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
158+
call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
159+
call assert_equal('', lines[4])
160+
call assert_equal('count total (s) self (s)', lines[5])
161+
call assert_match(' 1 0.\d\+ echo "hello', lines[6])
162+
call assert_equal(' \ world"', lines[7])
163+
call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
164+
call assert_equal(' \bar"', lines[9])
165+
call assert_equal('', lines[10])
166+
167+
call delete('Xprofile_file.vim')
168+
call delete('Xprofile_file.log')
169+
endfunc
170+
135171
func Test_profile_completion()
136172
call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
137173
call assert_equal('"profile continue file func pause start', @:)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ static char *(features[]) =
761761

762762
static int included_patches[] =
763763
{ /* Add new patch number below this line */
764+
/**/
765+
1207,
764766
/**/
765767
1206,
766768
/**/

0 commit comments

Comments
 (0)