Skip to content

Commit bcdf32a

Browse files
authored
Merge pull request #3521 from masatake/sh--array-alike-function
Sh: distinguish a function definition from an array initialization
2 parents 4344494 + 356311d commit bcdf32a

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
func input.bash /^func() {$/;" f
2+
func1= input.bash /^function func1=$/;" f
3+
func2= input.bash /^function func2= {$/;" f
4+
func3= input.bash /^func3=() {$/;" f
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
func() {
3+
:
4+
}
5+
funcs=()
6+
7+
function func1=
8+
{
9+
:
10+
}
11+
12+
function func2= {
13+
:
14+
}
15+
16+
func3=() {
17+
:
18+
}
19+
20+
notfunc=()
21+
{
22+
:
23+
}

parsers/sh.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,22 @@ static void findShTagsCommon (size_t (* keyword_handler) (int,
661661
++cp;
662662
if (*cp == ')')
663663
{
664-
found_kind = K_FUNCTION;
664+
/* A function definiton can look like an array initialization:
665+
*
666+
* ... f=()
667+
*
668+
* We use followng heuristics to distinguish a function definition
669+
* from an array initialization:
670+
*
671+
* preceding "function" keyword: function f=()
672+
* followed by '{': f=() {
673+
*
674+
*/
675+
if (! ((vStringLast(name) == '=')
676+
/* Have we found "function" yet? */
677+
&& (found_kind != K_FUNCTION)
678+
&& (strchr ((char *)(cp + 1), '{') == NULL)))
679+
found_kind = K_FUNCTION;
665680
++cp;
666681
}
667682
}

0 commit comments

Comments
 (0)