[PATCH] D120712: [clang-format][docs] handle explicit enum values

Konrad Wilhelm Kleine via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 1 03:53:20 PST 2022


kwk created this revision.
kwk added a reviewer: FederAndInk.
kwk requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If there was this enum definition before:

  struct FormatStyle {
    //...
  
    /// Different styles for aligning after open brackets.
    enum WhateverStyle : unsigned char {
      /// Foo
      WS_Bar = 5
    };
  };

We would output the following in
`clang/docs/ClangFormatStyleOptions.rst`
when running `cd ~/llvm-project/clang/docs/tools &&
./dump_format_style.py`:

  * ``WS_Bar = 5`` (in configuration: ``Bar = 5``)
    Foo.

With this patch, we change it to something that looks more like what we
are accustomed to:

  * ``WS_Bar`` (in configuration: ``Bar``)
    Foo.

This is a theoretical change because we don't have format style enums
that currently explicitly select a value. But while I was doing some
research on how to extend the `FormatStyle` I noticed this behavior and
thought it would make a small change.

You can experiment with and without this change by simply running 
`dump_format_style.py` while setting `AIAS_Left` to `AIAS_Left = 0`
in `clang/include/clang/Format/Format.h`. Without this change in
`Format.h` you shouldn't see any change being made to
`clang/docs/ClangFormatStyleOptions.rst`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120712

Files:
  clang/docs/tools/dump_format_style.py


Index: clang/docs/tools/dump_format_style.py
===================================================================
--- clang/docs/tools/dump_format_style.py
+++ clang/docs/tools/dump_format_style.py
@@ -159,10 +159,20 @@
     self.comment = comment
     self.config = config
 
+  @property
+  def clean_name(self) -> str:
+    # In case the enum value has an explicit value (e.g. enum foo {bar = 42};)
+    # we remove everything after the equal sign and just use "bar". 
+    return self.name.split("=", 1)[0].strip()
+
+  @property
+  def clean_config(self) -> str:
+    return re.sub('.*_', '', self.config).split("=", 1)[0].strip()
+
   def __str__(self):
     return '* ``%s`` (in configuration: ``%s``)\n%s' % (
-        self.name,
-        re.sub('.*_', '', self.config),
+        self.clean_name,
+        self.clean_config,
         doxygen2rst(indent(self.comment, 2)))
 
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120712.412040.patch
Type: text/x-patch
Size: 882 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220301/77fe3665/attachment-0001.bin>


More information about the cfe-commits mailing list