diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index c13d3fbe20781ddbbee6cdc48c4c55006c5f4e04..c6fe888c4131da97679a947a949a119e996e7fbd 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -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. diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index eb74fc8f1d7b092547ac8b5ca786af6bebb57b99..68abf1cdb6065adce904c5e020c60fdb7a7b7690 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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; -} - -FormatStyle getGoogleJSStyle() { - FormatStyle GoogleJSStyle = getGoogleStyle(); - GoogleJSStyle.Language = FormatStyle::LK_JavaScript; - GoogleJSStyle.BreakBeforeTernaryOperators = false; - GoogleJSStyle.MaxEmptyLinesToKeep = 2; - GoogleJSStyle.SpacesInContainerLiterals = false; - return GoogleJSStyle; -} + if (Language == FormatStyle::LK_JavaScript) { + GoogleStyle.BreakBeforeTernaryOperators = false; + GoogleStyle.MaxEmptyLinesToKeep = 2; + GoogleStyle.SpacesInContainerLiterals = false; + } else if (Language == FormatStyle::LK_Proto) { + GoogleStyle.AllowShortFunctionsOnASingleLine = false; + } -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, FormatStyle::LanguageKind Language, 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")) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 93e3f7a5936bb4a06b9c5636b314bdc95afa39e6..5de15e3c2b9235675838b2a1d18d3b58f7e5e004 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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, GetsPredefinedStyleByName) { 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, GetsCorrectBasedOnStyle) { 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()); diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 8a9ac9e20b855d92e273c831d01d39f93926377b..c0215e7d142db86573555a396dcbb1054fb7a833 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -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, UnderstandsJavaScriptOperators) { 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) { diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 276139623e39675fda724f27220965da6bba8da1..0a4416ec268c647df17a7db0c779efdb094dcbb5 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -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); }