Query Your C Code [Hackaday]

View Article on Hackaday

If you’ve ever worked on a large project — your own or a group effort — you know it can be difficult to find exactly where you want to be in the source code. Sure, you can use ctags and most other editors have some way of searching for things. But ClangQL from [AmrDeveloper] lets you treat your code base like a database.

Honestly, we’ve often thought about writing something that parses C code and stuffs it into a SQL database. This tool leverages the CLang parser and lets you write queries like:

SELECT * FROM functions

That may not seem like the best example, but how about:

SELECT COUNT(name) FROM functions WHERE return_type="int"

That’s a bit more interesting. The functions table provides each function’s name, signature, a count of arguments, a return type, and a flag to indicate methods. We hope the system will grow to let you query on other things, too, like variables, templates, preprocessor defines, and data types. The tool can handle C or C++ and could probably work with other CLang front ends with a little work.

This would be great for estimating the difficulty of tasks. Imagine asking for how many functions return a float when trying to decide how long it would take to switch to fixed point. We plan to try it on a source tree for the Linux kernel and give it a spin.

Truthfully, we’ve long been surprised databases haven’t taken over as file systems and source code anyway. A lot of what we do in git could be done in a database. And vice-versa.