Hi all,
I wanted to ask about what seems like an implied coding standard that is not followed in the code in all places:
LLVM Coding Standards — LLVM 20.0.0git documentation says " For example, readability is also harmed if an if /else chain does not use braced bodies for either all or none of its members".
I see there was a big thread about this a while back but came to no conclusion.
I do find reading code like this:
if (EllipsisLines < StringRef(ElidedLines).count('\n')) {
for (unsigned i = 0; i < EllipsisLines; ++i) {
WithColor(OS, raw_ostream::BLACK, /*Bold=*/true)
<< right_justify(".", LabelWidth);
OS << '\n';
}
} else
OS << ElidedLines;
or
if (isa<WsloopOp>(nested)) {
if (!llvm::dyn_cast_if_present<ParallelOp>((*this)->getParentOp()))
return emitError() << "an 'omp.wsloop' nested wrapper is only allowed "
"when 'omp.parallel' is the direct parent";
} else if (!isa<SimdOp>(nested))
return emitError() << "only supported nested wrappers are 'omp.simd' and "
"'omp.wsloop'";
difficult and it seems the coding standard implies that the else/else if here should have a brace because the if has it. Since the earlier thread had not reached any conclusion, I am proposing we just make this case clearer by adding another example in the coding standard section for this case. There is already:
// Use braces for the `if` block to keep it uniform with the `else` block.
if (isa<FunctionDecl>(D)) {
handleFunctionDecl(D);
} else {
// In this `else` case, it is necessary that we explain the situation with
// this surprisingly long comment, so it would be unclear without the braces
// whether the following statement is in the scope of the `if`.
handleOtherDecl(D);
}
we will just add a complementary example:
// Use braces for the `else` block to keep it uniform with the `if` block.
if (isa<FunctionDecl>(D)) {
verifyFunctionDecl(D);
handleFunctionDecl(D);
} else {
handleOtherDecl(D);
}
This rule seems to be violated quite a bit in the LLVM codebase: git grep "} else$" | wc -l gives 2706. and git grep "} else if (.*)$" | wc -l gives 741. But the additional example will atleast clarify it (assuming I am reading the coding standard correctly).