This is an archive of the discontinued LLVM Phabricator instance.

[Format] Make it easy to add new format::FormatStyle::LanguageStandard. NFCI
ClosedPublic

Authored by MaskRay on Jul 23 2019, 7:21 PM.

Diff Detail

Event Timeline

MaskRay created this revision.Jul 23 2019, 7:21 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 23 2019, 7:21 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

LGTM, but I don't actively work in this codebase so I really can't say. I'll wait to hear from some other more active clang-format reviewers.

lib/Format/TokenAnnotator.cpp
2862

Just a note: I don't know what the original intent of https://github.com/llvm/llvm-project/commit/dd978ae0e11 was, but in D65043 I modified this condition to be Style.Standard == FormatStyle::LS_Cpp03 || Style.Standard == FormatStyle::LS_Auto, because I believe that mirrors the current behavior exactly. With this change, a user that specified a standard of FormatStyle::LS_Auto will experience a change in behavior.

sammccall accepted this revision.Jul 24 2019, 12:46 AM

I haven't really understood D65043 yet, but I think this is a good change either way.

lib/Format/TokenAnnotator.cpp
2862

If I'm reading the code right, Formatter::analyzecalls deriveLocalStyle first, which replaces LS_Auto with a concrete style. Then later, it calls TokenAnnotator::calculateFormattingInformation which is what ultimately calls spaceRequiredBefore. So I think we never see Auto here, and the two forms are equivalent.

2862

nit: consider < Cpp11, or <= Cpp03? Fits in the spirit of this patch :-)

This revision is now accepted and ready to land.Jul 24 2019, 12:46 AM
MaskRay updated this revision to Diff 211436.Jul 24 2019, 12:58 AM

Change to < Cpp11

MaskRay marked an inline comment as done.Jul 24 2019, 1:00 AM
MaskRay added inline comments.
lib/Format/TokenAnnotator.cpp
2862

@modocache LS_Auto will be changed here:

// lib/Format/Formt.cpp:1348
    if (Style.Standard == FormatStyle::LS_Auto)
      Style.Standard = hasCpp03IncompatibleFormat(AnnotatedLines)
                           ? FormatStyle::LS_Cpp11
                           : FormatStyle::LS_Cpp03;

So when you add Cpp2a, be careful here :)

MaskRay updated this revision to Diff 211437.Jul 24 2019, 1:02 AM

Fix another place that should use < FormatStyle::LS_Cpp11 instead`

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJul 24 2019, 1:07 AM
sammccall added inline comments.Jul 24 2019, 1:29 AM
lib/Format/Format.cpp
2369–2372

Looking at this more, this is pretty subtle: Style *can* be LS_Auto here. So we're relying on the fact that Auto is the max value in the enum.

It's probably worth making this more explicit, e.g.

LanguageStandard LexingStd = Style.Standard == LS_Auto ? LS_Cpp20 : Style.Standard;
LangOpts.CPlusPlus11 = LexingStd >= FormatStyle::LS_Cpp11;
etc

(a comment would also be fine, but I think the code is at least as clear here)