Fixed erroneously flagging of chained if statements when styled like this:
if (cond) { } else if (cond) { } else { }
Differential D30841
[clang-tidy] readability-misleading-indentation: fix chained if fgross on Mar 10 2017, 11:49 AM. Authored by
Details Fixed erroneously flagging of chained if statements when styled like this: if (cond) { } else if (cond) { } else { }
Diff Detail Event TimelineComment Actions Functionally LGTM! Note that while the traversal of AST Matchers are not defined in general, in this particular case of chained ifs, it is guaranteed that parent nodes will be matched before the child nodes. For this reason I think it is ok to have a state like this. Maybe this worth a comment. Performance wise maybe an llvm::DenseMap would perform better, according to the LLVM Programmer's Manual, it is a great data structure to map pointers to pointers. Comment Actions Replaced std::map with llvm::DenseMap, added comment about traversal. I just assumed it would traverse in the "right" way, is there any documentation about AST / matcher traversal? Comment Actions I do not know of any, but I remember having some non-determinism across platforms, but I think matching parent before children is a safe assumption for all platforms. The patch now looks good to me, but lets wait what @alexfh says. Comment Actions Thank you for the fix! One comment inline.
Comment Actions Now using ASTContext::getParents instead of ChainedIfs map. For some reason I thought of getParents as an expensive function to call... Comment Actions Good point. It is expensive first time you call it, since it builds parents map. My idea was that since many checks use hasParent or hasAncestor matchers, the parent map will likely be built anyway, and building a (smaller) map of chained ifs as an optimization won't make much difference. But I don't know for sure, and maybe your approach is indeed better. We can start with the parent-map based one and keep the possible optimization in mind, in case there's an evidence of performance issues. LG. I'll submit the patch for you. |
Can you just use the parent map to get the previous if in the chain?