[cfe-dev] clang-format and Sun Java formatting style

Mart Sõmermaa via cfe-dev cfe-dev at lists.llvm.org
Fri Mar 11 14:34:11 PST 2016


Hello!

I've been a long-time clang-format user for C++ and it works really well -
so heartfelt thanks for it!

Now that BreakAfterJavaFieldAnnotations has been added in 3.9, it becomes
also quite usable for Java.

So I experimented a bit with v3.9.0-r262937 on a Java project, but found a
couple of issues.

My goal was to get as close as possible to vanilla Sun Java formatting
style.
Here are the .clang-format rules that should achieve this, to the best of
my knowledge (see e.g.
https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/sun_checks.xml
):

AccessModifierOffset: -4
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Attach
BreakAfterJavaFieldAnnotations: true
ColumnLimit: 120 <-- *this is the only non-standard part*
IndentCaseLabels: true
IndentWidth: 4
MaxEmptyLinesToKeep: 1
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpacesInParentheses: false
TabWidth: 4
UseTab: Never

Issues:

1. No break before lambda body

Eclipse formats Java 8 lambdas consistently with the "line break after
brace" style, but clang-format does not.

Compare:

clang-format with the configuration above:

        final Button logsButton = new Button("Logs");
        logsButton.addClickListener((e) -> {
getNavigator().navigateTo("logs"); });
        actions.addComponent(logsButton);

Eclipse:

        final Button logsButton = new Button("Logs");
        logsButton.addClickListener((e) -> {
            getNavigator().navigateTo("logs");
        });
        actions.addComponent(logsButton);

What do you think, is clang-format behavior justified? Shouldn't
"AllowShortFunctionsOnASingleLine: None" force a break?
Maybe even a separate configuration parameter
AllowShortLambdasOnASingleLine should be added?

2. Odd formatting of a short interface declaration

Given the following interface declaration as formatted by Eclipse:

public interface LoginView {
    void showError(String message);
}

for some reason, clang-format formats this on one line:

public interface LoginView { void showError(String message); }

This is really odd for Java-accustomed eyes. I didn't find any other
AllowShort... directives besides the ones listed above to turn this off.

Again, what do you think, is clang-format behavior justified?
Should we add a configuration parameter to control this
(AllowShortClassesOnASingleLine or some such)?

---

It would be really nice if clang-format could support the Sun vanilla style
and Eclipse and clang-format could be made to co-exist, so that their
formatting would be identical if configured properly.

Thanks in advance for any comments,
Mart
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160312/99a5042c/attachment.html>


More information about the cfe-dev mailing list