Help writing fixits in clang


I’d like to add a fixit (or two) to suggest parentheses in the following expressions:

struct S { float value; };
S operator"" _f(long double t);

void test() {
int i = 0x2e+‘a’; // fixit: (0x2e)+‘a’

float j = 100.0_e-10.0; // fixit: (100.0_e)-10.0

float k = 100.0_f.value; // fixit: (100.0_f).value

int l = 0x2e+i; // fixit: (0x2e)+i


However, I am not sure of the correct way to do this.

Would someone, please, give me a quick tutorial or, if easier, point out an similar example in the clang code.

Many thanks,


A bit of an off-topic note beforehand: Why not add a space inside of parens?

Fixits are actually pretty straightforward to implement. It’s like this:

Diag(ErrorLoc, diag::err_missing_foo) << VarName // some diagnostic, unchanged
<< FixItHint::CreateInsertion(ErrorLoc, " foo");

This creates a diagnostic with a fixit at ErrorLoc and inserts foo there. There are other fixit hints that you can use like replacement.

Hope that helps!

Hi Andy,

this sounds like a great clang-tidy check in the readability-module. You can check out the documentation () which has a section on writing the check and emitting diagnostics with fixits.

If you want to produce a standalone tool that does the job, this information will still be relevant, is it will use the same functionality of clang.

Best, Jonas