SimonKagstrom / kcov

Code coverage tool for compiled programs, Python and Bash which uses debugging information to collect and report data without special compilation options
http://simonkagstrom.github.io/kcov/
GNU General Public License v2.0
720 stars 110 forks source link

bash: Write proper parser for shell scripts #145

Open SimonKagstrom opened 8 years ago

SimonKagstrom commented 8 years ago

The current parser is quite uncapable, so some constructions will lead it astray. A proper shell script parser is needed for this to work better.

mvdan commented 6 years ago

This might be of use to you: https://github.com/mvdan/sh

It's not written in C/C++, but I believe it is possible to build a Go package as a C shared library and call it from most languages. For example: https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf

The binary also has a json output flag, which might help bring the barrier down:

$ cat foo.sh
echo foo bar # comment
$ shfmt -exp.tojson <foo.sh
<AST as a JSON object>
SimonKagstrom commented 6 years ago

Thanks! Will look into it.

I think the approach go for if using your project is to dlopen it and use it if it exists, otherwise fall back to the current version. Is it buildable as a shared library?

(I should note that I know nothing about go)

mvdan commented 6 years ago

Is it buildable as a shared library?

Yes, that is what you can do with Go. Google "golang shared library" and you'll find plenty of guides and blog posts.

You could either optionally depend on the shared library, or optionally depend on the shfmt binary for the JSON exporting I showed above. Perhaps the latter would make it easier for both you and the user.

ivopieniak commented 3 years ago

Hi @SimonKagstrom , I really appreciate your work with kcov! I have started using it recently and has proven to be a great tool for Bash testing coverage combined with Shellspec. Would it be possible for you to provide some details about the plans for fixing the parsing issues?

SimonKagstrom commented 3 years ago

I don't have any concrete plans for fixing this. The library mvdan mentioned above could be a way to do this, but otherwise it would be a major undertaking

What I did was to provide a more basic parsing mode instead, which can be enabled using

kcov --configure=bash-use-basic-parser=1 [...]

which gives more false hits, but is also more resilient against going astray as the standard parser can sometimes do.