[PATCH] Fixes bug 20559 - SBPO_Always does not work for sizeof

Roman Kashitsyn romankashicin at gmail.com
Wed Aug 6 07:03:12 PDT 2014


Hi djasper,

http://llvm.org/bugs/show_bug.cgi?id=20559

clang-format: interpret some keywords as function calls and apply
the SBPO_Always option accordingly.

Before:
  auto f (int x) -> decltype(x) { return sizeof(x); }

  int g () noexcept(someCall ());

  static_assert(sizeof(char) == 1, "Your compiler is broken");

After:
  auto f (int x) -> decltype (x) { return sizeof (x); }

  int g () noexcept (someCall ());

  static_assert (sizeof (char) == 1, "Your compiler is broken");

http://reviews.llvm.org/D4809

Files:
  lib/Format/FormatToken.h
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/FormatToken.h
===================================================================
--- lib/Format/FormatToken.h
+++ lib/Format/FormatToken.h
@@ -353,6 +353,25 @@
     return is(tok::comment) && (!Next || Next->NewlinesBefore > 0);
   }
 
+  /// \brief Returns \c true if this is a keyword which usage looks
+  /// like a function call. E.g. sizeof, typeid, static_assert.
+  bool isFunctionLikeKeyword() const {
+    switch (Tok.getKind()) {
+    case tok::kw_throw:
+    case tok::kw_typeid:
+    case tok::kw_return:
+    case tok::kw_sizeof:
+    case tok::kw_alignof:
+    case tok::kw_alignas:
+    case tok::kw_decltype:
+    case tok::kw_noexcept:
+    case tok::kw_static_assert:
+      return true;
+    default:
+      return false;
+    }
+  }
+
   prec::Level getPrecedence() const {
     return getBinOpPrecedence(Tok.getKind(), true, true);
   }
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1549,7 +1549,8 @@
                           tok::kw_switch, tok::kw_catch, tok::kw_case) ||
              Left.IsForEachMacro)) ||
            (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
-            Left.isOneOf(tok::identifier, tok::kw___attribute) &&
+            (Left.isOneOf(tok::identifier, tok::kw___attribute) ||
+             Left.isFunctionLikeKeyword()) &&
             Line.Type != LT_PreprocessorDirective);
   }
   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -7544,6 +7544,13 @@
                "  break;\n"
                "}", NoSpace);
   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
+  verifyFormat("size_t x = sizeof(x);", NoSpace);
+  verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
+  verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
+  verifyFormat("alignas(128) char a[128];", NoSpace);
+  verifyFormat("size_t x = alignof(MyType);", NoSpace);
+  verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
+  verifyFormat("int f() throw(Deprecated);", NoSpace);
 
   FormatStyle Space = getLLVMStyle();
   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
@@ -7581,6 +7588,13 @@
                "#endif",
                Space);
   verifyFormat("auto i = std::make_unique<int> (5);", Space);
+  verifyFormat("size_t x = sizeof (x);", Space);
+  verifyFormat("auto f (int x) -> decltype (x);", Space);
+  verifyFormat("int f (T x) noexcept (x.create ());", Space);
+  verifyFormat("alignas (128) char a[128];", Space);
+  verifyFormat("size_t x = alignof (MyType);", Space);
+  verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
+  verifyFormat("int f () throw (Deprecated);", Space);
 }
 
 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4809.12234.patch
Type: text/x-patch
Size: 3032 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140806/d7b1994e/attachment.bin>


More information about the cfe-commits mailing list