Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Remove YAML hack to emit a BasedOnStyle comment #89228

Merged
merged 2 commits into from
Apr 25, 2024

Conversation

jasilvanus
Copy link
Contributor

When serializing a formatting style to YAML, we were emitting a comment # BasedOnStyle: <style> if the serialized formatting style matches one of the known styles. This is useful, but mis-uses the YAML API.

An upcoming change to fix keys with special characters by quoting them breaks this,
and will emit a non-comment key '# BasedOnStyle': <style> instead.
(#88763)

Thus, remove this hack.
Instead, we emit an ordinary key OrigBasedOnStyle that is ignored when reading back the data.
An alternative would be to just remove it completely.

Ideally, we'd emit a proper comment, but our YAML API doesn't support that.

When serializing a formatting style to YAML, we were emitting a
comment `# BasedOnStyle: <style>` if the serialized formatting
style matches one of the known styles. This is useful,
but mis-uses the YAML API.

An upcoming change to fix keys with special characters by quoting them
breaks this, and will emit a non-comment *key* `'# BasedOnStyle': <style>`
instead.
(llvm#88763)

Thus, remove this hack.
Instead, we emit an ordinary key OrigBasedOnStyle that is ignored
when reading back the data. An alternative would be to just remove
it completely.

Ideally, we'd emit a proper comment, but our YAML API doesn't support
that.
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 18, 2024

@llvm/pr-subscribers-clang-format

Author: Jannik Silvanus (jasilvanus)

Changes

When serializing a formatting style to YAML, we were emitting a comment # BasedOnStyle: &lt;style&gt; if the serialized formatting style matches one of the known styles. This is useful, but mis-uses the YAML API.

An upcoming change to fix keys with special characters by quoting them breaks this,
and will emit a non-comment key '# BasedOnStyle': &lt;style&gt; instead.
(#88763)

Thus, remove this hack.
Instead, we emit an ordinary key OrigBasedOnStyle that is ignored when reading back the data.
An alternative would be to just remove it completely.

Ideally, we'd emit a proper comment, but our YAML API doesn't support that.


Full diff: https://github.com/llvm/llvm-project/pull/89228.diff

1 Files Affected:

  • (modified) clang/lib/Format/Format.cpp (+7-1)
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ccb2c9190e2eff..9b311343387ead 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -807,12 +807,18 @@ template <> struct MappingTraits<FormatStyle> {
         FormatStyle PredefinedStyle;
         if (getPredefinedStyle(StyleName, Style.Language, &PredefinedStyle) &&
             Style == PredefinedStyle) {
-          IO.mapOptional("# BasedOnStyle", StyleName);
+          // For convenience, emit the info which style this matches. However,
+          // setting BasedOnStyle will override all other keys when importing,
+          // so we set a helper key that is ignored when importing.
+          // Ideally, we'd want a YAML comment here, but that's not supported.
+          IO.mapOptional("OrigBasedOnStyle", StyleName);
           BasedOnStyle = StyleName;
           break;
         }
       }
     } else {
+      StringRef OrigBasedOnStyle; // ignored
+      IO.mapOptional("OrigBasedOnStyle", OrigBasedOnStyle);
       IO.mapOptional("BasedOnStyle", BasedOnStyle);
       if (!BasedOnStyle.empty()) {
         FormatStyle::LanguageKind OldLanguage = Style.Language;

Copy link
Contributor

@mydeveloperday mydeveloperday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't feel correct to me...

clang/lib/Format/Format.cpp Outdated Show resolved Hide resolved
clang/lib/Format/Format.cpp Show resolved Hide resolved
@jasilvanus
Copy link
Contributor Author

jasilvanus commented Apr 19, 2024

This doesn't feel correct to me...

I'd also be fine with just removing the comment.

Edit: On second thought, I'd actually be more comfortable with removing the comment to prevent folks confused by the ignored extra key. But I'm not sure how important this info in the written YAML is, and whether it is relevant at all. Feedback by clang-format folks on that would be appreciated.

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is, is there any real benefit of putting the commented style in the output?

clang/lib/Format/Format.cpp Outdated Show resolved Hide resolved
clang/lib/Format/Format.cpp Show resolved Hide resolved
This may cause more confusion than helping.
If we really need a comment, we can add YAML
support for comments and add the comment back.
@jasilvanus
Copy link
Contributor Author

I've removed the dummy field and the comment now. This should prevent confusion.
I don't know whether anyone actually uses the emitted comment, there is no explanation,
and tests pass without it.

I suggest that if we later learn that there are valid uses that require the comment, we should add proper comment
support in the YAML writer, and add a test for it.

@mydeveloperday
Copy link
Contributor

I'm ok with removing it if @owenca and @HazardyKnusperkeks are.

Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a use case for that comment, and thus I see no problem in removing it.

@jasilvanus jasilvanus merged commit 2c5d7a8 into llvm:main Apr 25, 2024
3 of 4 checks passed
@jasilvanus jasilvanus deleted the jsilvanu/clang-format-comment branch April 25, 2024 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants