r200652 - clang-format: Let chromium style inherit google style's javascript tweaks.

Nico Weber nicolasweber at gmx.de
Sun Feb 2 12:50:45 PST 2014


Author: nico
Date: Sun Feb  2 14:50:45 2014
New Revision: 200652

URL: http://llvm.org/viewvc/llvm-project?rev=200652&view=rev
Log:
clang-format: Let chromium style inherit google style's javascript tweaks.

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp
    cfe/trunk/unittests/Format/FormatTestJS.cpp
    cfe/trunk/unittests/Format/FormatTestProto.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=200652&r1=200651&r2=200652&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Sun Feb  2 14:50:45 2014
@@ -360,22 +360,15 @@ struct FormatStyle {
 /// http://llvm.org/docs/CodingStandards.html.
 FormatStyle getLLVMStyle();
 
-/// \brief Returns a format style complying with Google's C++ style guide:
+/// \brief Returns a format style complying with one of Google's style guides:
 /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml.
-FormatStyle getGoogleStyle();
-
-/// \brief Returns a format style complying with Google's JavaScript style
-/// guide:
 /// http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml.
-FormatStyle getGoogleJSStyle();
-
-/// \brief Returns a format style complying with Google's Protocol Buffer style:
 /// https://developers.google.com/protocol-buffers/docs/style.
-FormatStyle getGoogleProtoStyle();
+FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with Chromium's style guide:
 /// http://www.chromium.org/developers/coding-style.
-FormatStyle getChromiumStyle();
+FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language);
 
 /// \brief Returns a format style complying with Mozilla's style guide:
 /// https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style.

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=200652&r1=200651&r2=200652&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Sun Feb  2 14:50:45 2014
@@ -294,8 +294,10 @@ FormatStyle getLLVMStyle() {
   return LLVMStyle;
 }
 
-FormatStyle getGoogleStyle() {
+FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
   FormatStyle GoogleStyle = getLLVMStyle();
+  GoogleStyle.Language = Language;
+
   GoogleStyle.AccessModifierOffset = -1;
   GoogleStyle.AlignEscapedNewlinesLeft = true;
   GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
@@ -316,27 +318,19 @@ FormatStyle getGoogleStyle() {
   GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
   GoogleStyle.PenaltyBreakBeforeFirstCallParameter = 1;
 
-  return GoogleStyle;
-}
+  if (Language == FormatStyle::LK_JavaScript) {
+    GoogleStyle.BreakBeforeTernaryOperators = false;
+    GoogleStyle.MaxEmptyLinesToKeep = 2;
+    GoogleStyle.SpacesInContainerLiterals = false;
+  } else if (Language == FormatStyle::LK_Proto) {
+    GoogleStyle.AllowShortFunctionsOnASingleLine = false;
+  }
 
-FormatStyle getGoogleJSStyle() {
-  FormatStyle GoogleJSStyle = getGoogleStyle();
-  GoogleJSStyle.Language = FormatStyle::LK_JavaScript;
-  GoogleJSStyle.BreakBeforeTernaryOperators = false;
-  GoogleJSStyle.MaxEmptyLinesToKeep = 2;
-  GoogleJSStyle.SpacesInContainerLiterals = false;
-  return GoogleJSStyle;
-}
-
-FormatStyle getGoogleProtoStyle() {
-  FormatStyle GoogleProtoStyle = getGoogleStyle();
-  GoogleProtoStyle.Language = FormatStyle::LK_Proto;
-  GoogleProtoStyle.AllowShortFunctionsOnASingleLine = false;
-  return GoogleProtoStyle;
+  return GoogleStyle;
 }
 
-FormatStyle getChromiumStyle() {
-  FormatStyle ChromiumStyle = getGoogleStyle();
+FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
+  FormatStyle ChromiumStyle = getGoogleStyle(Language);
   ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
   ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
   ChromiumStyle.AllowShortLoopsOnASingleLine = false;
@@ -389,20 +383,11 @@ bool getPredefinedStyle(StringRef Name,
   if (Name.equals_lower("llvm")) {
     *Style = getLLVMStyle();
   } else if (Name.equals_lower("chromium")) {
-    *Style = getChromiumStyle();
+    *Style = getChromiumStyle(Language);
   } else if (Name.equals_lower("mozilla")) {
     *Style = getMozillaStyle();
   } else if (Name.equals_lower("google")) {
-    switch (Language) {
-    case FormatStyle::LK_JavaScript:
-      *Style = getGoogleJSStyle();
-      break;
-    case FormatStyle::LK_Proto:
-      *Style = getGoogleProtoStyle();
-      break;
-    default:
-      *Style = getGoogleStyle();
-    }
+    *Style = getGoogleStyle(Language);
   } else if (Name.equals_lower("webkit")) {
     *Style = getWebKitStyle();
   } else if (Name.equals_lower("gnu")) {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=200652&r1=200651&r2=200652&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun Feb  2 14:50:45 2014
@@ -17,6 +17,10 @@
 namespace clang {
 namespace format {
 
+FormatStyle getGoogleStyle() {
+  return getGoogleStyle(FormatStyle::LK_Cpp);
+}
+
 class FormatTest : public ::testing::Test {
 protected:
   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
@@ -5783,7 +5787,7 @@ TEST_F(FormatTest, FormatObjCMethodExpr)
                "                  backing:NSBackingStoreBuffered\n"
                "                    defer:NO]);\n"
                "}",
-               getChromiumStyle());
+               getChromiumStyle(FormatStyle::LK_Cpp));
   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
                "                             with:contentsNativeView];");
 
@@ -7250,14 +7254,14 @@ TEST_F(FormatTest, GetsPredefinedStyleBy
   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
   EXPECT_ALL_STYLES_EQUAL(Styles);
 
-  Styles[0] = getGoogleJSStyle();
+  Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   EXPECT_TRUE(
       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
   EXPECT_TRUE(
       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
   EXPECT_ALL_STYLES_EQUAL(Styles);
 
-  Styles[0] = getChromiumStyle();
+  Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
   EXPECT_ALL_STYLES_EQUAL(Styles);
@@ -7290,7 +7294,7 @@ TEST_F(FormatTest, GetsCorrectBasedOnSty
   EXPECT_ALL_STYLES_EQUAL(Styles);
 
   Styles.resize(5);
-  Styles[0] = getGoogleJSStyle();
+  Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
   Styles[1] = getLLVMStyle();
   Styles[1].Language = FormatStyle::LK_JavaScript;
   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());

Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=200652&r1=200651&r2=200652&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Sun Feb  2 14:50:45 2014
@@ -31,19 +31,19 @@ protected:
     return Result;
   }
 
-  static std::string format(llvm::StringRef Code,
-                            const FormatStyle &Style = getGoogleJSStyle()) {
+  static std::string format(llvm::StringRef Code, const FormatStyle &Style) {
     return format(Code, 0, Code.size(), Style);
   }
 
   static FormatStyle getGoogleJSStyleWithColumns(unsigned ColumnLimit) {
-    FormatStyle Style = getGoogleJSStyle();
+    FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
     Style.ColumnLimit = ColumnLimit;
     return Style;
   }
 
-  static void verifyFormat(llvm::StringRef Code,
-                           const FormatStyle &Style = getGoogleJSStyle()) {
+  static void verifyFormat(
+      llvm::StringRef Code,
+      const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript)) {
     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
   }
 };
@@ -82,6 +82,9 @@ TEST_F(FormatTestJS, UnderstandsJavaScri
 TEST_F(FormatTestJS, SpacesInContainerLiterals) {
   verifyFormat("var arr = [1, 2, 3];");
   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
+
+  verifyFormat("var obj = {a: 1, b: 2, c: 3};",
+               getChromiumStyle(FormatStyle::LK_JavaScript));
 }
 
 TEST_F(FormatTestJS, SingleQuoteStrings) {

Modified: cfe/trunk/unittests/Format/FormatTestProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestProto.cpp?rev=200652&r1=200651&r2=200652&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestProto.cpp Sun Feb  2 14:50:45 2014
@@ -32,7 +32,7 @@ protected:
   }
 
   static std::string format(llvm::StringRef Code) {
-    FormatStyle Style = getGoogleProtoStyle();
+    FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
     Style.ColumnLimit = 60; // To make writing tests easier.
     return format(Code, 0, Code.size(), Style);
   }





More information about the cfe-commits mailing list