[PATCH] D84306: [clang-format][NFC] Be more careful about the layout of FormatToken.

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 23 02:27:52 PDT 2020


MyDeveloperDay added inline comments.


================
Comment at: clang/lib/Format/FormatToken.h:177
   /// Indicates that this is the first token of the file.
-  bool IsFirst = false;
+  unsigned IsFirst : 1;
 
----------------
educate me, why

```
unsigned IsFirst : 1;
```

here and not

```
bool IsFirst : 1;
```

is that equivalent? (I'm literally not sure myself), I wrote a little test just to remind myself how this stuff works.

```
#include <iostream>

class Foo
{
public:
    Foo()
      : A(true)
      , B(false)
      , C(true)
    {
    }
    bool A : 1;
    bool B : 1;
    bool C : 1;
};

class Bar
{
public:
    Bar()
      : A(true)
      , B(false)
      , C(true)
    {
    }
    unsigned A : 1;
    unsigned B : 1;
    unsigned C : 1;
};

class Fuz
{
public:
    Fuz()
      : A(true)
      , B(false)
      , C(true)
    {
    }
    bool A;
    bool B;
    bool C;
};

class Baz
{
public:
    Baz()
      : A(true)
      , B(false)
      , C(true)
    {
    }
    unsigned A;
    unsigned B;
    unsigned C;
};

int
main(int argc, char *argv[])
{
    std::cerr << "Foo " << sizeof(Foo) << "\n";
    std::cerr << "Bar " <<sizeof(Bar) << "\n";
    std::cerr << "Fuz " <<sizeof(Fuz) << "\n";
    std::cerr << "Baz " <<sizeof(Baz) << "\n";

    return 0;
}
```

When run gives the following:

```
Foo 1
Bar 4
Fuz 3
Baz 12
```

So I guess my question is could there be more space savings if using bool IsFirst:1  (and the others), I'd also think that would help clarity a little (or did I misunderstand?)




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D84306/new/

https://reviews.llvm.org/D84306





More information about the cfe-commits mailing list