From: Daniel Jasper Date: Mon, 14 Jan 2013 16:24:39 +0000 (+0000) Subject: Make single-line if statements optional. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6f5bb2c93c31a7977382c5079d85db8ca3267cd0;p=clang Make single-line if statements optional. Now, "if (a) return;" is only allowed, if this option is set. Also add a Chromium style which is currently identical to Google style except for this option. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172431 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index aac552b602..19c4c02f82 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -61,6 +61,9 @@ struct FormatStyle { /// initializer on its own line. bool ConstructorInitializerAllOnOneLineOrOnePerLine; + /// \brief If true, "if (a) return;" can be put on a single line. + bool AllowShortIfStatementsOnASingleLine; + /// \brief Add a space in front of an Objective-C protocol list, i.e. use /// Foo instead of Foo. bool ObjCSpaceBeforeProtocolList; @@ -78,6 +81,10 @@ FormatStyle getLLVMStyle(); /// http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml. FormatStyle getGoogleStyle(); +/// \brief Returns a format style complying with Chromium's style guide: +/// http://www.chromium.org/developers/coding-style. +FormatStyle getChromiumStyle(); + /// \brief Reformats the given \p Ranges in the token stream coming out of /// \c Lex. /// diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 4e9fd405df..a221e4cce7 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -117,6 +117,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.IndentCaseLabels = false; LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false; + LLVMStyle.AllowShortIfStatementsOnASingleLine = false; LLVMStyle.ObjCSpaceBeforeProtocolList = true; LLVMStyle.ObjCSpaceBeforeReturnType = true; return LLVMStyle; @@ -132,11 +133,18 @@ FormatStyle getGoogleStyle() { GoogleStyle.IndentCaseLabels = true; GoogleStyle.SpacesBeforeTrailingComments = 2; GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true; + GoogleStyle.AllowShortIfStatementsOnASingleLine = true; GoogleStyle.ObjCSpaceBeforeProtocolList = false; GoogleStyle.ObjCSpaceBeforeReturnType = false; return GoogleStyle; } +FormatStyle getChromiumStyle() { + FormatStyle ChromiumStyle = getGoogleStyle(); + ChromiumStyle.AllowShortIfStatementsOnASingleLine = false; + return ChromiumStyle; +} + struct OptimizationParameters { unsigned PenaltyIndentLevel; unsigned PenaltyLevelDecrease; @@ -1441,6 +1449,8 @@ private: void tryMergeSimpleIf(std::vector::iterator &I, std::vector::iterator E, unsigned Limit) { + if (!Style.AllowShortIfStatementsOnASingleLine) + return; AnnotatedLine &Line = *I; if (!fitsIntoLimit((I + 1)->First, Limit)) return; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8e55ddb5d0..1ab4a53c40 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -78,6 +78,12 @@ protected: return Style; } + FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { + FormatStyle Style = getGoogleStyle(); + Style.ColumnLimit = ColumnLimit; + return Style; + } + void verifyFormat(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { EXPECT_EQ(Code.str(), format(messUp(Code), Style)); @@ -130,16 +136,16 @@ TEST_F(FormatTest, FormatsNestedCall) { //===----------------------------------------------------------------------===// TEST_F(FormatTest, FormatIfWithoutCompountStatement) { - verifyFormat("if (true) f();\ng();"); - verifyFormat("if (a)\n if (b)\n if (c) g();\nh();"); + verifyFormat("if (true)\n f();\ng();"); + verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); - verifyFormat("if (a)\n" - " // comment\n" - " f();"); - verifyFormat("if (a) return;", getLLVMStyleWithColumns(14)); - verifyFormat("if (a)\n return;", getLLVMStyleWithColumns(13)); + verifyGoogleFormat("if (a)\n" + " // comment\n" + " f();"); + verifyFormat("if (a) return;", getGoogleStyleWithColumns(14)); + verifyFormat("if (a)\n return;", getGoogleStyleWithColumns(13)); verifyFormat("if (aaaaaaaaa)\n" - " return;", getLLVMStyleWithColumns(14)); + " return;", getGoogleStyleWithColumns(14)); } TEST_F(FormatTest, ParseIfElse) { @@ -156,7 +162,8 @@ TEST_F(FormatTest, ParseIfElse) { verifyFormat("if (true)\n" " if (true)\n" " if (true) {\n" - " if (true) f();\n" + " if (true)\n" + " f();\n" " } else {\n" " g();\n" " }\n" @@ -1461,7 +1468,8 @@ TEST_F(FormatTest, FormatObjCImplementation) { verifyFormat("@implementation Foo\n" "+ (id)init {\n" - " if (true) return nil;\n" + " if (true)\n" + " return nil;\n" "}\n" "// Look, a comment!\n" "- (int)answerWith:(int)i {\n"