[PATCH] D37813: clang-format: better handle namespace macros

Francois Ferrand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 22 09:27:14 PDT 2019


Typz added a comment.

The patch goal is indeed to indent the content of namespace-macro blocks like the content of any 'regular' namespace. So it should look like the content of a namespace, possibly depending on the choose style options. To sumarize, here is a detailed summary of the observable differences between macros vs normal namespace.

**Without this patch**, clang-format does not understand the macro call actually defines a namespace, thus:

- the content of the namespace-macro block is indented, even when `Style.NamespaceIndentation = FormatStyle::NI_None`

  TESTSUITE(A) {
    int i;                  // <--- should not be indented
  }
  namespace B {
  int j;
  }



- similarly for nested namespaces, when  `Style.NamespaceIndentation = FormatStyle::NI_Inner` :

  TESTSUITE(A) {
  TESTSUITE(B) {
      int i;                // <--- should be indented 2-chars only
  }
  }
  namespace C {
  namespace D {
    int j;
  }
  }

- There is no automatic fix of namespace end comment when `Style.FixNamespaceComments = true`

  TESTSUITE(A) {
    int i;
  }                         // <--- should have a namespace end comment
  namespace B {
    int j;
  } // namespace B

- Multiple nested namespaces are not compacted when `Style.CompactNamespaces = true`

  TESTSUITE(A) {
  TESTSUITE(B) {             //<--- should be merged with previous line
      int i;
  }
  }                         // <--- should be merged with previous line (and have a "combined" namespace end comment)
  namespace A { namespace B {
  int j;
  } // namespace A::B



---

**This patch fixes all these points**, which hopefully leads to the exact same formatting when using the namespace keyword or the namespace-macros:

  // Style.NamespaceIndentation = FormatStyle::NI_None
  TESTSUITE(A) {
  int i;
  }
  namespace B {
  int j;
  }
  
  // Style.NamespaceIndentation = FormatStyle::NI_Inner
  TESTSUITE(A) {
  TESTSUITE(B) {
    int i;
  }
  }
  namespace C {
  namespace D {
    int j;
  }
  }
  
  // Style.FixNamespaceComments = true
  TESTSUITE(A) {
    int i;
  } // TESTSUITE(A)
  namespace B {
    int j;
  } // namespace B
  
  // Style.CompactNamespaces = true
  TESTSUITE(A) { TESTSUITE(B) {
    int i;
  }} // TESTSUITE(A::B)
  namespace A { namespace B {
    int j;
  }} // namespace A::B

I did not see any issue in my testing or reviewing the code, but if you see a place in the tests where it does not match this, please point it out !


Repository:
  rC Clang

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

https://reviews.llvm.org/D37813





More information about the cfe-commits mailing list