From: Daniel Jasper Date: Mon, 17 Feb 2014 07:57:46 +0000 (+0000) Subject: clang-format: Don't wrap "const" etc. of function declarations. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9531410f661ea478c0254e69d7feb4077cd76c62;p=clang clang-format: Don't wrap "const" etc. of function declarations. Generally people seem to prefer wrapping the first function parameter over wrapping the trailing tokens "const", "override" and "final". This does not extend to function-like annotations and probably not to other non-standard annotations. Before: void someLongFunction(int SomeLongParameter) const { ... } After: void someLongFunction( int SomeLongParameter) const { ... } git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@201504 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 2275557fda..b7eb3bfdb4 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1212,10 +1212,16 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Right.Type == TT_TrailingAnnotation && Right.Next && Right.Next->isNot(tok::l_paren)) { - // Breaking before a trailing annotation is bad unless it is function-like. + // Generally, breaking before a trailing annotation is bad unless it is + // function-like. It seems to be especially preferable to keep standard + // annotations (i.e. "const", "final" and "override") on the same line. // Use a slightly higher penalty after ")" so that annotations like // "const override" are kept together. - return Left.is(tok::r_paren) ? 100 : 120; + bool is_standard_annotation = Right.is(tok::kw_const) || + Right.TokenText == "override" || + Right.TokenText == "final"; + return (Left.is(tok::r_paren) ? 100 : 120) + + (is_standard_annotation ? 50 : 0); } // In for-loops, prefer breaking at ',' and ';'. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index c4793a9c30..983c455c8c 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3141,13 +3141,24 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n" " const override;"); - // Unless this would lead to the first parameter being broken. - verifyFormat("void someLongFunction(int someLongParameter)\n" - " const {}", + // Even if the first parameter has to be wrapped. + verifyFormat("void someLongFunction(\n" + " int someLongParameter) const {}", getLLVMStyleWithColumns(46)); - verifyFormat("void someLongFunction(int someLongParameter)\n" - " const {}", + verifyFormat("void someLongFunction(\n" + " int someLongParameter) const {}", + Style); + verifyFormat("void someLongFunction(\n" + " int someLongParameter) override {}", Style); + verifyFormat("void someLongFunction(\n" + " int someLongParameter) final {}", + Style); + verifyFormat("void someLongFunction(\n" + " int parameter) const override {}", + Style); + + // Unless these are unknown annotations. verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n" " aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " LONG_AND_UGLY_ANNOTATION;");