This change relaxes the requirements on the utility rewriteExprFromNumberToDuration function, so that the abseil-duration-comparison check transform expressions which are arguments of macros.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
clang-tidy/abseil/DurationComparisonCheck.cpp | ||
---|---|---|
25 ↗ | (On Diff #178511) | expr doesn't follow the usual naming conventions, I'd go with E (and fix the comment to match). |
29 ↗ | (On Diff #178511) | loc -> Loc |
test/clang-tidy/abseil-duration-comparison.cpp | ||
131 ↗ | (On Diff #178511) | Can you add another example that has one more level of macro arg expansion? e.g., #define VALUE_IF_2(e) (e) #define VALUE_IF(v, e) v ? VALUE_IF_2(e) : VALUE_IF_2(0) int a = VALUE_IF(1, 5 > absl::ToDoubleSeconds(d1)); Which makes me wonder -- is this transformation valid in all cases? For instance, how do the fixes look with this abomination? #define VALUE_IF_2(e) (e) #define VALUE_IF(v, e, type) (v ? VALUE_IF_2(absl::To##type##Seconds(e)) : 0) int a = VALUE_IF(1, d1, Double); I would expect these shenanigans to be vanishingly rare, so if this turns out to be a bad FIXIT you may want to document it as a known deficiency if you don't feel like handling such a case. |
Update tests
test/clang-tidy/abseil-duration-comparison.cpp | ||
---|---|---|
131 ↗ | (On Diff #178511) | I've added both these as test cases. The first one works as expected, and I'm pleasantly surprised to see that the second one does as well (which is to say that it doesn't transform). |
@aaron.ballman I am both grateful and sad that I don't possess the same macro creativity as you do. :)
(I'm also sad that the various clang APIs aren't as well documented as I hoped. The current solution works well, but it took a while and a bit of consultation with some other llvm'ers to get it right.)
test/clang-tidy/abseil-duration-comparison.cpp | ||
---|---|---|
146 ↗ | (On Diff #178680) | This also doesn't transform. |
LOL
(I'm also sad that the various clang APIs aren't as well documented as I hoped. The current solution works well, but it took a while and a bit of consultation with some other llvm'ers to get it right.)
As you dig into APIs like that, feel free to propose or commit new documentation that helps clarify things -- more/better docs are always appreciated!
LGTM with a minor commenting nit.
test/clang-tidy/abseil-duration-comparison.cpp | ||
---|---|---|
146 ↗ | (On Diff #178680) | Fantastic, thank you! I wouldn't expect a fix-it there due to the macro shenanigans, but I'm surprised it does not diagnose given that it expands to basically the same thing as a and a2 above: https://godbolt.org/z/D1MvGX (ignore the errors but look at the macro expansion from the diagnostics). Granted, this situation is uncommon enough, so feel free to add a FIXME comment to the a4 test case to say that it probably should be diagnosed without a fix-it hint (assuming you agree). |
test/clang-tidy/abseil-duration-comparison.cpp | ||
---|---|---|
146 ↗ | (On Diff #178680) | I actually prefer not to diagnose here, mostly because I'm not sure what value we'd actually give without a fix-it hint. "You are holding it wrong, but we can't tell you how to hold it right." Ultimately, though, I think you are right that the number of cases this occurs are vanishingly small, and I'd expect folks doing these kinds of craziness to know what they are doing (and take responsibility thereof). Perhaps that's optimistic. |