How to auto format C++ code in ROS2 using ament lint and clang-format
Transcript: Hello, I’m Tyler Weaver. And this is Ross developer notes. Lint is the fibers and bits of fabric. You clean out your dryer and your Naval. You also have it in your code. This video is about how to get linked outta your Ross. Two code linters are tools that check for bad patterns in your code and help you improve code quality. This will save you time by reducing accidental bugs and making your code easier to read who doesn’t want that. All right. The first and most basic lender is a format. The one we use at PickNik for CBO plus is called cl format in Ross. One, the tools were much more manual and harder to apply in Ross two, there are tests and Python, execute tools in the ENT package that this doing, doing this super easy here, I’ll show you. I’ve created a workspace with one of the examples from the official examples repository, and I will show you that it builds.
Um, so here’s it building. Um, and I’ll show you that it fails a test I’ve added the test already. Uh, no I haven’t. Um, we have to do that. All right. So how do you add the test? Uh, you just have to add the it to the package. XML. It’s super easy. You VI, um, your package XML. So, uh, there’s this cement lit auto thing. That’s pretty important. Um, and I’ll tell you why in a bit, but we’re just gonna add CMA, playing our format and test to depend. All right. Now what a mint auto does is a mint lit auto is a package that’s able to read the package XML and see what other liers you depend on, and then add them to your project. You need a little bit of CMA magic for it. I’m gonna show you that, um, it’s this thing right here. Um, you have to find the event lit auto package. If you’re doing build testing, and then you call this, this macro, there is auto fine test dependencies, and that’s the magic that makes this super easy. So I did that. We’re gonna now out. We have to rebuild because it works through CMA.
It’s gonna be real fast. Now we test and you see it fail. Uh, if we look at the result, um, you see, it wants to make a bunch of white space change, basically. Um, and I’ll show you how to see what those are. If we go into the package, there’s that we can run the executable. This is another great thing about ament link. So col on no, not coal on, um, meant playing format. All right, these are the changes we wants to make outputs like a dif basically. Um, but what’s really great about it is this it’ll do it for us. That’s the best part. You can stop worrying about how your code is formatted. Just build it into the tests. And then everybody just runs a min claim format, reformat over their code before they submit it. And now the whole repo is formatted exactly the same. And it’s easy to read. Um, I’ll show you that it passes the test, obviously, um, Uh, passes, um,
They’re nine tests, they’re nine, uh, C plus plus files that it tested over and all of ‘em are formatted correctly. I’ll show you what the dif is. If you, if you go in here and you diff look at the changes, it’s basically white space changes, um, to unify the formatting. You can see that that test append is the only thing that we add. Um, it’s making all these white space changes. Um, and another thing do is it’ll reorder your includes. So they’re by type and alphabetical. Um, anyway. Yeah, that’s it. Now go get that li outta your code. This has raised your Ross two IQ. Let me know. Thank you.