2012-12-04 02:12:45 +08:00
|
|
|
//===--- Format.cpp - Format C++ code -------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file implements functions declared in Format.h. This will be
|
|
|
|
/// split into separate files as we go.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-16 22:55:28 +08:00
|
|
|
#define DEBUG_TYPE "format-formatter"
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
#include "BreakableToken.h"
|
2013-01-30 05:01:14 +08:00
|
|
|
#include "TokenAnnotator.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "UnwrappedLineParser.h"
|
2013-04-15 22:28:00 +08:00
|
|
|
#include "WhitespaceManager.h"
|
2013-05-16 18:40:07 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-12-21 18:20:02 +08:00
|
|
|
#include "clang/Basic/OperatorPrecedence.h"
|
2013-01-02 18:28:36 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
#include "clang/Format/Format.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
2013-03-27 19:52:18 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-02-13 18:46:36 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2013-01-16 22:55:28 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2013-05-07 23:32:14 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2013-02-13 18:46:36 +08:00
|
|
|
#include <queue>
|
2012-12-04 21:02:32 +08:00
|
|
|
#include <string>
|
|
|
|
|
2013-05-07 23:32:14 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<clang::format::FormatStyle::LanguageStandard> {
|
2013-05-13 20:51:40 +08:00
|
|
|
static void enumeration(IO &IO,
|
|
|
|
clang::format::FormatStyle::LanguageStandard &Value) {
|
|
|
|
IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
|
|
|
|
IO.enumCase(Value, "C++11", clang::format::FormatStyle::LS_Cpp11);
|
|
|
|
IO.enumCase(Value, "Auto", clang::format::FormatStyle::LS_Auto);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-14 17:30:02 +08:00
|
|
|
template <>
|
2013-05-13 20:51:40 +08:00
|
|
|
struct ScalarEnumerationTraits<clang::format::FormatStyle::BraceBreakingStyle> {
|
|
|
|
static void
|
|
|
|
enumeration(IO &IO, clang::format::FormatStyle::BraceBreakingStyle &Value) {
|
|
|
|
IO.enumCase(Value, "Attach", clang::format::FormatStyle::BS_Attach);
|
|
|
|
IO.enumCase(Value, "Linux", clang::format::FormatStyle::BS_Linux);
|
|
|
|
IO.enumCase(Value, "Stroustrup", clang::format::FormatStyle::BS_Stroustrup);
|
2013-05-07 23:32:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-08-01 07:16:02 +08:00
|
|
|
template <>
|
|
|
|
struct ScalarEnumerationTraits<
|
|
|
|
clang::format::FormatStyle::NamespaceIndentationKind> {
|
|
|
|
static void
|
|
|
|
enumeration(IO &IO,
|
|
|
|
clang::format::FormatStyle::NamespaceIndentationKind &Value) {
|
|
|
|
IO.enumCase(Value, "None", clang::format::FormatStyle::NI_None);
|
|
|
|
IO.enumCase(Value, "Inner", clang::format::FormatStyle::NI_Inner);
|
|
|
|
IO.enumCase(Value, "All", clang::format::FormatStyle::NI_All);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-07 23:32:14 +08:00
|
|
|
template <> struct MappingTraits<clang::format::FormatStyle> {
|
|
|
|
static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
|
2013-05-10 19:56:10 +08:00
|
|
|
if (IO.outputting()) {
|
|
|
|
StringRef StylesArray[] = { "LLVM", "Google", "Chromium", "Mozilla" };
|
|
|
|
ArrayRef<StringRef> Styles(StylesArray);
|
|
|
|
for (size_t i = 0, e = Styles.size(); i < e; ++i) {
|
|
|
|
StringRef StyleName(Styles[i]);
|
2013-05-19 08:53:30 +08:00
|
|
|
clang::format::FormatStyle PredefinedStyle;
|
|
|
|
if (clang::format::getPredefinedStyle(StyleName, &PredefinedStyle) &&
|
|
|
|
Style == PredefinedStyle) {
|
2013-05-10 19:56:10 +08:00
|
|
|
IO.mapOptional("# BasedOnStyle", StyleName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-07 23:32:14 +08:00
|
|
|
StringRef BasedOnStyle;
|
|
|
|
IO.mapOptional("BasedOnStyle", BasedOnStyle);
|
|
|
|
if (!BasedOnStyle.empty())
|
2013-05-19 08:53:30 +08:00
|
|
|
if (!clang::format::getPredefinedStyle(BasedOnStyle, &Style)) {
|
|
|
|
IO.setError(Twine("Unknown value for BasedOnStyle: ", BasedOnStyle));
|
|
|
|
return;
|
|
|
|
}
|
2013-05-07 23:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
|
|
|
|
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
|
2013-08-01 07:55:15 +08:00
|
|
|
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
|
|
|
|
Style.AllowAllParametersOfDeclarationOnNextLine);
|
|
|
|
IO.mapOptional("AllowShortIfStatementsOnASingleLine",
|
|
|
|
Style.AllowShortIfStatementsOnASingleLine);
|
2013-05-16 20:12:21 +08:00
|
|
|
IO.mapOptional("AllowShortLoopsOnASingleLine",
|
|
|
|
Style.AllowShortLoopsOnASingleLine);
|
2013-05-29 20:07:31 +08:00
|
|
|
IO.mapOptional("AlwaysBreakTemplateDeclarations",
|
|
|
|
Style.AlwaysBreakTemplateDeclarations);
|
2013-07-04 20:02:44 +08:00
|
|
|
IO.mapOptional("AlwaysBreakBeforeMultilineStrings",
|
|
|
|
Style.AlwaysBreakBeforeMultilineStrings);
|
2013-07-27 00:56:36 +08:00
|
|
|
IO.mapOptional("BreakBeforeBinaryOperators",
|
|
|
|
Style.BreakBeforeBinaryOperators);
|
|
|
|
IO.mapOptional("BreakConstructorInitializersBeforeComma",
|
|
|
|
Style.BreakConstructorInitializersBeforeComma);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
|
|
|
|
IO.mapOptional("ColumnLimit", Style.ColumnLimit);
|
|
|
|
IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
|
|
|
|
Style.ConstructorInitializerAllOnOneLineOrOnePerLine);
|
|
|
|
IO.mapOptional("DerivePointerBinding", Style.DerivePointerBinding);
|
2013-07-10 22:02:49 +08:00
|
|
|
IO.mapOptional("ExperimentalAutoDetectBinPacking",
|
|
|
|
Style.ExperimentalAutoDetectBinPacking);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
|
|
|
|
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
|
2013-08-01 07:16:02 +08:00
|
|
|
IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("ObjCSpaceBeforeProtocolList",
|
|
|
|
Style.ObjCSpaceBeforeProtocolList);
|
2013-06-08 00:02:52 +08:00
|
|
|
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
|
|
|
|
IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString);
|
2013-07-12 04:41:21 +08:00
|
|
|
IO.mapOptional("PenaltyBreakFirstLessLess",
|
|
|
|
Style.PenaltyBreakFirstLessLess);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
|
|
|
|
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
|
|
|
|
Style.PenaltyReturnTypeOnItsOwnLine);
|
|
|
|
IO.mapOptional("PointerBindsToType", Style.PointerBindsToType);
|
|
|
|
IO.mapOptional("SpacesBeforeTrailingComments",
|
|
|
|
Style.SpacesBeforeTrailingComments);
|
2013-07-17 02:22:10 +08:00
|
|
|
IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
|
2013-05-07 23:32:14 +08:00
|
|
|
IO.mapOptional("Standard", Style.Standard);
|
2013-05-13 16:42:42 +08:00
|
|
|
IO.mapOptional("IndentWidth", Style.IndentWidth);
|
2013-05-13 17:22:11 +08:00
|
|
|
IO.mapOptional("UseTab", Style.UseTab);
|
2013-05-13 20:51:40 +08:00
|
|
|
IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces);
|
2013-06-22 01:25:42 +08:00
|
|
|
IO.mapOptional("IndentFunctionDeclarationAfterType",
|
|
|
|
Style.IndentFunctionDeclarationAfterType);
|
2013-05-07 23:32:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-07-12 04:41:21 +08:00
|
|
|
void setDefaultPenalties(FormatStyle &Style) {
|
|
|
|
Style.PenaltyBreakComment = 45;
|
2013-07-15 22:33:14 +08:00
|
|
|
Style.PenaltyBreakFirstLessLess = 120;
|
2013-07-12 04:41:21 +08:00
|
|
|
Style.PenaltyBreakString = 1000;
|
|
|
|
Style.PenaltyExcessCharacter = 1000000;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle getLLVMStyle() {
|
|
|
|
FormatStyle LLVMStyle;
|
|
|
|
LLVMStyle.AccessModifierOffset = -2;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.AlignEscapedNewlinesLeft = false;
|
2013-08-01 07:55:15 +08:00
|
|
|
LLVMStyle.AlignTrailingComments = true;
|
2013-01-30 00:03:49 +08:00
|
|
|
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
|
2013-01-15 00:24:39 +08:00
|
|
|
LLVMStyle.AllowShortIfStatementsOnASingleLine = false;
|
2013-05-16 20:12:21 +08:00
|
|
|
LLVMStyle.AllowShortLoopsOnASingleLine = false;
|
2013-07-04 20:02:44 +08:00
|
|
|
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
|
2013-07-27 00:56:36 +08:00
|
|
|
LLVMStyle.AlwaysBreakTemplateDeclarations = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.BinPackParameters = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
LLVMStyle.BreakBeforeBinaryOperators = false;
|
|
|
|
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
|
|
|
|
LLVMStyle.BreakConstructorInitializersBeforeComma = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.ColumnLimit = 80;
|
|
|
|
LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
|
2013-07-27 00:56:36 +08:00
|
|
|
LLVMStyle.Cpp11BracedListStyle = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.DerivePointerBinding = false;
|
2013-07-10 22:02:49 +08:00
|
|
|
LLVMStyle.ExperimentalAutoDetectBinPacking = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.IndentCaseLabels = false;
|
2013-07-27 00:56:36 +08:00
|
|
|
LLVMStyle.IndentFunctionDeclarationAfterType = false;
|
|
|
|
LLVMStyle.IndentWidth = 2;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.MaxEmptyLinesToKeep = 1;
|
2013-08-01 07:16:02 +08:00
|
|
|
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
|
2013-01-11 04:12:55 +08:00
|
|
|
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
|
2013-05-06 22:11:27 +08:00
|
|
|
LLVMStyle.PointerBindsToType = false;
|
|
|
|
LLVMStyle.SpacesBeforeTrailingComments = 1;
|
|
|
|
LLVMStyle.Standard = FormatStyle::LS_Cpp03;
|
2013-05-13 17:22:11 +08:00
|
|
|
LLVMStyle.UseTab = false;
|
2013-07-12 04:41:21 +08:00
|
|
|
|
|
|
|
setDefaultPenalties(LLVMStyle);
|
|
|
|
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
return LLVMStyle;
|
|
|
|
}
|
|
|
|
|
|
|
|
FormatStyle getGoogleStyle() {
|
|
|
|
FormatStyle GoogleStyle;
|
|
|
|
GoogleStyle.AccessModifierOffset = -1;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.AlignEscapedNewlinesLeft = true;
|
2013-08-01 07:55:15 +08:00
|
|
|
GoogleStyle.AlignTrailingComments = true;
|
2013-01-30 00:03:49 +08:00
|
|
|
GoogleStyle.AllowAllParametersOfDeclarationOnNextLine = true;
|
2013-04-24 21:46:00 +08:00
|
|
|
GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
|
2013-05-24 02:05:18 +08:00
|
|
|
GoogleStyle.AllowShortLoopsOnASingleLine = true;
|
2013-07-04 20:02:44 +08:00
|
|
|
GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
GoogleStyle.AlwaysBreakTemplateDeclarations = true;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.BinPackParameters = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
GoogleStyle.BreakBeforeBinaryOperators = false;
|
|
|
|
GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
|
|
|
|
GoogleStyle.BreakConstructorInitializersBeforeComma = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.ColumnLimit = 80;
|
|
|
|
GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
GoogleStyle.Cpp11BracedListStyle = true;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.DerivePointerBinding = true;
|
2013-07-10 22:02:49 +08:00
|
|
|
GoogleStyle.ExperimentalAutoDetectBinPacking = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.IndentCaseLabels = true;
|
2013-07-27 00:56:36 +08:00
|
|
|
GoogleStyle.IndentFunctionDeclarationAfterType = true;
|
|
|
|
GoogleStyle.IndentWidth = 2;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.MaxEmptyLinesToKeep = 1;
|
2013-08-01 07:16:02 +08:00
|
|
|
GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
|
2013-01-11 04:12:55 +08:00
|
|
|
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
|
2013-05-06 22:11:27 +08:00
|
|
|
GoogleStyle.PointerBindsToType = true;
|
|
|
|
GoogleStyle.SpacesBeforeTrailingComments = 2;
|
|
|
|
GoogleStyle.Standard = FormatStyle::LS_Auto;
|
2013-05-13 17:22:11 +08:00
|
|
|
GoogleStyle.UseTab = false;
|
2013-07-12 04:41:21 +08:00
|
|
|
|
|
|
|
setDefaultPenalties(GoogleStyle);
|
|
|
|
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
return GoogleStyle;
|
|
|
|
}
|
|
|
|
|
2013-01-15 00:24:39 +08:00
|
|
|
FormatStyle getChromiumStyle() {
|
|
|
|
FormatStyle ChromiumStyle = getGoogleStyle();
|
2013-01-30 00:03:49 +08:00
|
|
|
ChromiumStyle.AllowAllParametersOfDeclarationOnNextLine = false;
|
2013-04-24 21:46:00 +08:00
|
|
|
ChromiumStyle.AllowShortIfStatementsOnASingleLine = false;
|
2013-05-16 20:12:21 +08:00
|
|
|
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
|
2013-02-27 17:47:53 +08:00
|
|
|
ChromiumStyle.BinPackParameters = false;
|
2013-02-06 22:22:40 +08:00
|
|
|
ChromiumStyle.DerivePointerBinding = false;
|
2013-07-27 00:56:36 +08:00
|
|
|
ChromiumStyle.Standard = FormatStyle::LS_Cpp03;
|
2013-01-15 00:24:39 +08:00
|
|
|
return ChromiumStyle;
|
|
|
|
}
|
|
|
|
|
2013-05-06 22:11:27 +08:00
|
|
|
FormatStyle getMozillaStyle() {
|
|
|
|
FormatStyle MozillaStyle = getLLVMStyle();
|
|
|
|
MozillaStyle.AllowAllParametersOfDeclarationOnNextLine = false;
|
|
|
|
MozillaStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
|
|
|
|
MozillaStyle.DerivePointerBinding = true;
|
|
|
|
MozillaStyle.IndentCaseLabels = true;
|
|
|
|
MozillaStyle.ObjCSpaceBeforeProtocolList = false;
|
|
|
|
MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
|
|
|
|
MozillaStyle.PointerBindsToType = true;
|
|
|
|
return MozillaStyle;
|
|
|
|
}
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
FormatStyle getWebKitStyle() {
|
|
|
|
FormatStyle Style = getLLVMStyle();
|
2013-08-01 07:16:02 +08:00
|
|
|
Style.AccessModifierOffset = -4;
|
2013-08-01 07:55:15 +08:00
|
|
|
Style.AlignTrailingComments = false;
|
2013-07-27 00:56:36 +08:00
|
|
|
Style.BreakBeforeBinaryOperators = true;
|
2013-08-01 07:16:02 +08:00
|
|
|
Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
|
2013-07-27 00:56:36 +08:00
|
|
|
Style.BreakConstructorInitializersBeforeComma = true;
|
2013-08-01 07:16:02 +08:00
|
|
|
Style.ColumnLimit = 0;
|
2013-07-27 00:56:36 +08:00
|
|
|
Style.IndentWidth = 4;
|
2013-08-01 07:16:02 +08:00
|
|
|
Style.NamespaceIndentation = FormatStyle::NI_Inner;
|
2013-07-24 21:10:59 +08:00
|
|
|
Style.PointerBindsToType = true;
|
|
|
|
return Style;
|
|
|
|
}
|
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
bool getPredefinedStyle(StringRef Name, FormatStyle *Style) {
|
2013-05-07 23:32:14 +08:00
|
|
|
if (Name.equals_lower("llvm"))
|
2013-05-19 08:53:30 +08:00
|
|
|
*Style = getLLVMStyle();
|
|
|
|
else if (Name.equals_lower("chromium"))
|
|
|
|
*Style = getChromiumStyle();
|
|
|
|
else if (Name.equals_lower("mozilla"))
|
|
|
|
*Style = getMozillaStyle();
|
|
|
|
else if (Name.equals_lower("google"))
|
|
|
|
*Style = getGoogleStyle();
|
2013-07-24 21:10:59 +08:00
|
|
|
else if (Name.equals_lower("webkit"))
|
|
|
|
*Style = getWebKitStyle();
|
2013-05-19 08:53:30 +08:00
|
|
|
else
|
|
|
|
return false;
|
2013-05-07 23:32:14 +08:00
|
|
|
|
2013-05-19 08:53:30 +08:00
|
|
|
return true;
|
2013-05-07 23:32:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::error_code parseConfiguration(StringRef Text, FormatStyle *Style) {
|
2013-05-20 23:18:01 +08:00
|
|
|
if (Text.trim().empty())
|
|
|
|
return llvm::make_error_code(llvm::errc::invalid_argument);
|
2013-05-07 23:32:14 +08:00
|
|
|
llvm::yaml::Input Input(Text);
|
|
|
|
Input >> *Style;
|
|
|
|
return Input.error();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string configurationAsText(const FormatStyle &Style) {
|
|
|
|
std::string Text;
|
|
|
|
llvm::raw_string_ostream Stream(Text);
|
|
|
|
llvm::yaml::Output Output(Stream);
|
|
|
|
// We use the same mapping method for input and output, so we need a non-const
|
|
|
|
// reference here.
|
|
|
|
FormatStyle NonConstStyle = Style;
|
|
|
|
Output << NonConstStyle;
|
2013-05-13 20:56:35 +08:00
|
|
|
return Stream.str();
|
2013-05-07 23:32:14 +08:00
|
|
|
}
|
|
|
|
|
2013-02-08 16:22:00 +08:00
|
|
|
// Returns the length of everything up to the first possible line break after
|
|
|
|
// the ), ], } or > matching \c Tok.
|
2013-05-29 22:47:47 +08:00
|
|
|
static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
|
2013-02-08 16:22:00 +08:00
|
|
|
if (Tok.MatchingParen == NULL)
|
|
|
|
return 0;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *End = Tok.MatchingParen;
|
|
|
|
while (End->Next && !End->Next->CanBreakBefore) {
|
|
|
|
End = End->Next;
|
2013-02-08 16:22:00 +08:00
|
|
|
}
|
|
|
|
return End->TotalLength - Tok.TotalLength + 1;
|
|
|
|
}
|
|
|
|
|
2013-07-01 06:29:28 +08:00
|
|
|
namespace {
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class UnwrappedLineFormatter {
|
|
|
|
public:
|
2013-01-11 03:17:33 +08:00
|
|
|
UnwrappedLineFormatter(const FormatStyle &Style, SourceManager &SourceMgr,
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &Line, unsigned FirstIndent,
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *RootToken,
|
2013-06-05 22:09:10 +08:00
|
|
|
WhitespaceManager &Whitespaces,
|
2013-07-10 22:02:49 +08:00
|
|
|
encoding::Encoding Encoding,
|
|
|
|
bool BinPackInconclusiveFunctions)
|
2012-12-19 05:05:13 +08:00
|
|
|
: Style(Style), SourceMgr(SourceMgr), Line(Line),
|
2013-01-18 16:44:07 +08:00
|
|
|
FirstIndent(FirstIndent), RootToken(RootToken),
|
2013-07-10 22:02:49 +08:00
|
|
|
Whitespaces(Whitespaces), Count(0), Encoding(Encoding),
|
|
|
|
BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
/// \brief Formats an \c UnwrappedLine.
|
2013-05-22 20:51:29 +08:00
|
|
|
void format(const AnnotatedLine *NextLine) {
|
2012-12-06 17:56:08 +08:00
|
|
|
// Initialize state dependent on indent.
|
2013-01-11 18:22:12 +08:00
|
|
|
LineState State;
|
2013-01-11 02:45:26 +08:00
|
|
|
State.Column = FirstIndent;
|
2013-05-29 22:47:47 +08:00
|
|
|
State.NextToken = RootToken;
|
2013-07-08 22:34:09 +08:00
|
|
|
State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
|
|
|
|
/*AvoidBinPacking=*/false,
|
|
|
|
/*NoLineBreak=*/false));
|
2012-12-21 22:37:20 +08:00
|
|
|
State.LineContainsContinuedForLoopSection = false;
|
2013-02-08 23:28:42 +08:00
|
|
|
State.ParenLevel = 0;
|
2013-02-20 23:25:48 +08:00
|
|
|
State.StartOfStringLiteral = 0;
|
2013-02-18 19:05:07 +08:00
|
|
|
State.StartOfLineLevel = State.ParenLevel;
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
State.LowestLevelOnLine = State.ParenLevel;
|
2013-05-22 13:27:42 +08:00
|
|
|
State.IgnoreStackForComparison = false;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
|
|
|
// The first token has already been indented and thus consumed.
|
2013-07-12 19:37:05 +08:00
|
|
|
moveStateToNextToken(State, /*DryRun=*/false, /*Newline=*/false);
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
if (Style.ColumnLimit == 0) {
|
|
|
|
formatWithoutColumnLimit(State);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// If everything fits on a single line, just put it there.
|
2013-02-28 19:05:57 +08:00
|
|
|
unsigned ColumnLimit = Style.ColumnLimit;
|
|
|
|
if (NextLine && NextLine->InPPDirective &&
|
2013-05-29 22:47:47 +08:00
|
|
|
!NextLine->First->HasUnescapedNewline)
|
2013-02-28 19:05:57 +08:00
|
|
|
ColumnLimit = getColumnLimit();
|
|
|
|
if (Line.Last->TotalLength <= ColumnLimit - FirstIndent) {
|
2013-02-01 19:00:45 +08:00
|
|
|
while (State.NextToken != NULL) {
|
2012-12-19 05:05:13 +08:00
|
|
|
addTokenToState(false, false, State);
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-02-01 19:00:45 +08:00
|
|
|
|
2013-02-08 16:22:00 +08:00
|
|
|
// If the ObjC method declaration does not fit on a line, we should format
|
|
|
|
// it with one arg per line.
|
|
|
|
if (Line.Type == LT_ObjCMethodDecl)
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// Find best solution in solution space.
|
2013-05-22 20:51:29 +08:00
|
|
|
analyzeSolutionSpace(State);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-05-29 22:47:47 +08:00
|
|
|
void DebugTokenState(const FormatToken &FormatTok) {
|
|
|
|
const Token &Tok = FormatTok.Tok;
|
2013-05-10 19:56:10 +08:00
|
|
|
llvm::dbgs() << StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
|
2013-01-29 19:27:30 +08:00
|
|
|
Tok.getLength());
|
2013-05-10 19:56:10 +08:00
|
|
|
llvm::dbgs();
|
2013-01-16 22:55:28 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
struct ParenState {
|
Fix some linebreak decisions in Google format.
Before:
f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaa);
aaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaa, aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaa));
After:
f(aaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);
aaaaaaa(aaaaaaaaaaaaa,
aaaaaaaaaaaaa,
aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));
llvm-svn: 174363
2013-02-05 17:41:21 +08:00
|
|
|
ParenState(unsigned Indent, unsigned LastSpace, bool AvoidBinPacking,
|
2013-04-22 15:59:53 +08:00
|
|
|
bool NoLineBreak)
|
2013-02-08 23:28:42 +08:00
|
|
|
: Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
|
|
|
|
BreakBeforeClosingBrace(false), QuestionColumn(0),
|
2013-02-08 16:22:00 +08:00
|
|
|
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
|
2013-04-22 15:59:53 +08:00
|
|
|
NoLineBreak(NoLineBreak), ColonPos(0), StartOfFunctionCall(0),
|
2013-07-12 19:19:37 +08:00
|
|
|
StartOfArraySubscripts(0), NestedNameSpecifierContinuation(0),
|
|
|
|
CallContinuation(0), VariablePos(0), ContainsLineBreak(false) {}
|
2012-12-25 00:43:00 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
/// \brief The position to which a specific parenthesis level needs to be
|
|
|
|
/// indented.
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned Indent;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
/// \brief The position of the last space on each level.
|
|
|
|
///
|
|
|
|
/// Used e.g. to break like:
|
|
|
|
/// functionCall(Parameter, otherCall(
|
|
|
|
/// OtherParameter));
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned LastSpace;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
/// \brief The position the first "<<" operator encountered on each level.
|
|
|
|
///
|
|
|
|
/// Used to align "<<" operators. 0 if no such operator has been encountered
|
|
|
|
/// on a level.
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned FirstLessLess;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
Fixes layout of right braces.
We now decide whether a newline should go before the closing brace
depending on whether a newline was inserted after the opening brace.
For example, we now insert a newline before '};' in:
static SomeClass WithALoooooooooooooooooooongName = {
100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"
};
... while not inserting a newline here:
static SomeClass = { a, b, c, d, e, f, g, h, i, j,
looooooooooooooooooooooooooooooooooongname,
looooooooooooooooooooooooooooooong };
Also fixes the formating of (column limit 25):
int x = {
avariable,
b(alongervariable)
};
llvm-svn: 172076
2013-01-10 23:58:26 +08:00
|
|
|
/// \brief Whether a newline needs to be inserted before the block's closing
|
|
|
|
/// brace.
|
|
|
|
///
|
|
|
|
/// We only want to insert a newline before the closing brace if there also
|
|
|
|
/// was a newline after the beginning left brace.
|
2013-01-11 18:22:12 +08:00
|
|
|
bool BreakBeforeClosingBrace;
|
|
|
|
|
2013-01-28 20:45:14 +08:00
|
|
|
/// \brief The column of a \c ? in a conditional expression;
|
|
|
|
unsigned QuestionColumn;
|
|
|
|
|
2013-01-31 22:59:26 +08:00
|
|
|
/// \brief Avoid bin packing, i.e. multiple parameters/elements on multiple
|
|
|
|
/// lines, in this context.
|
|
|
|
bool AvoidBinPacking;
|
|
|
|
|
|
|
|
/// \brief Break after the next comma (or all the commas in this context if
|
|
|
|
/// \c AvoidBinPacking is \c true).
|
2013-02-08 16:22:00 +08:00
|
|
|
bool BreakBeforeParameter;
|
2013-01-31 22:59:26 +08:00
|
|
|
|
2013-04-22 15:59:53 +08:00
|
|
|
/// \brief Line breaking in this context would break a formatting rule.
|
|
|
|
bool NoLineBreak;
|
2013-01-11 19:37:55 +08:00
|
|
|
|
2013-02-05 18:07:47 +08:00
|
|
|
/// \brief The position of the colon in an ObjC method declaration/call.
|
|
|
|
unsigned ColonPos;
|
2013-02-20 20:56:39 +08:00
|
|
|
|
2013-03-02 00:48:32 +08:00
|
|
|
/// \brief The start of the most recent function in a builder-type call.
|
|
|
|
unsigned StartOfFunctionCall;
|
|
|
|
|
2013-07-12 19:19:37 +08:00
|
|
|
/// \brief Contains the start of array subscript expressions, so that they
|
|
|
|
/// can be aligned.
|
|
|
|
unsigned StartOfArraySubscripts;
|
|
|
|
|
2013-04-02 22:33:13 +08:00
|
|
|
/// \brief If a nested name specifier was broken over multiple lines, this
|
|
|
|
/// contains the start column of the second line. Otherwise 0.
|
|
|
|
unsigned NestedNameSpecifierContinuation;
|
|
|
|
|
|
|
|
/// \brief If a call expression was broken over multiple lines, this
|
|
|
|
/// contains the start column of the second line. Otherwise 0.
|
|
|
|
unsigned CallContinuation;
|
|
|
|
|
2013-04-03 21:36:17 +08:00
|
|
|
/// \brief The column of the first variable name in a variable declaration.
|
|
|
|
///
|
|
|
|
/// Used to align further variables if necessary.
|
|
|
|
unsigned VariablePos;
|
|
|
|
|
2013-07-08 22:25:23 +08:00
|
|
|
/// \brief \c true if this \c ParenState already contains a line-break.
|
2013-05-13 17:19:24 +08:00
|
|
|
///
|
2013-07-08 22:25:23 +08:00
|
|
|
/// The first line break in a certain \c ParenState causes extra penalty so
|
|
|
|
/// that clang-format prefers similar breaks, i.e. breaks in the same
|
|
|
|
/// parenthesis.
|
|
|
|
bool ContainsLineBreak;
|
2013-05-13 17:19:24 +08:00
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
bool operator<(const ParenState &Other) const {
|
|
|
|
if (Indent != Other.Indent)
|
2013-01-11 22:23:32 +08:00
|
|
|
return Indent < Other.Indent;
|
2013-01-11 18:22:12 +08:00
|
|
|
if (LastSpace != Other.LastSpace)
|
|
|
|
return LastSpace < Other.LastSpace;
|
|
|
|
if (FirstLessLess != Other.FirstLessLess)
|
|
|
|
return FirstLessLess < Other.FirstLessLess;
|
2013-01-11 19:37:55 +08:00
|
|
|
if (BreakBeforeClosingBrace != Other.BreakBeforeClosingBrace)
|
|
|
|
return BreakBeforeClosingBrace;
|
2013-01-28 20:45:14 +08:00
|
|
|
if (QuestionColumn != Other.QuestionColumn)
|
|
|
|
return QuestionColumn < Other.QuestionColumn;
|
2013-01-31 22:59:26 +08:00
|
|
|
if (AvoidBinPacking != Other.AvoidBinPacking)
|
|
|
|
return AvoidBinPacking;
|
2013-02-08 16:22:00 +08:00
|
|
|
if (BreakBeforeParameter != Other.BreakBeforeParameter)
|
|
|
|
return BreakBeforeParameter;
|
2013-04-22 15:59:53 +08:00
|
|
|
if (NoLineBreak != Other.NoLineBreak)
|
|
|
|
return NoLineBreak;
|
2013-02-05 18:07:47 +08:00
|
|
|
if (ColonPos != Other.ColonPos)
|
|
|
|
return ColonPos < Other.ColonPos;
|
2013-03-02 00:48:32 +08:00
|
|
|
if (StartOfFunctionCall != Other.StartOfFunctionCall)
|
|
|
|
return StartOfFunctionCall < Other.StartOfFunctionCall;
|
2013-07-12 19:19:37 +08:00
|
|
|
if (StartOfArraySubscripts != Other.StartOfArraySubscripts)
|
|
|
|
return StartOfArraySubscripts < Other.StartOfArraySubscripts;
|
2013-04-02 22:33:13 +08:00
|
|
|
if (CallContinuation != Other.CallContinuation)
|
|
|
|
return CallContinuation < Other.CallContinuation;
|
2013-04-03 21:36:17 +08:00
|
|
|
if (VariablePos != Other.VariablePos)
|
|
|
|
return VariablePos < Other.VariablePos;
|
2013-07-08 22:25:23 +08:00
|
|
|
if (ContainsLineBreak != Other.ContainsLineBreak)
|
|
|
|
return ContainsLineBreak < Other.ContainsLineBreak;
|
2013-01-12 15:36:22 +08:00
|
|
|
return false;
|
2013-01-11 18:22:12 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief The current state when indenting a unwrapped line.
|
|
|
|
///
|
|
|
|
/// As the indenting tries different combinations this is copied by value.
|
|
|
|
struct LineState {
|
|
|
|
/// \brief The number of used columns in the current line.
|
|
|
|
unsigned Column;
|
|
|
|
|
|
|
|
/// \brief The token that needs to be next formatted.
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *NextToken;
|
2013-01-11 18:22:12 +08:00
|
|
|
|
2012-12-21 22:37:20 +08:00
|
|
|
/// \brief \c true if this line contains a continued for-loop section.
|
|
|
|
bool LineContainsContinuedForLoopSection;
|
|
|
|
|
2013-02-08 23:28:42 +08:00
|
|
|
/// \brief The level of nesting inside (), [], <> and {}.
|
|
|
|
unsigned ParenLevel;
|
|
|
|
|
2013-02-18 19:05:07 +08:00
|
|
|
/// \brief The \c ParenLevel at the start of this line.
|
|
|
|
unsigned StartOfLineLevel;
|
|
|
|
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
/// \brief The lowest \c ParenLevel on the current line.
|
|
|
|
unsigned LowestLevelOnLine;
|
2013-05-27 19:50:16 +08:00
|
|
|
|
2013-02-20 23:25:48 +08:00
|
|
|
/// \brief The start column of the string literal, if we're in a string
|
|
|
|
/// literal sequence, 0 otherwise.
|
|
|
|
unsigned StartOfStringLiteral;
|
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
/// \brief A stack keeping track of properties applying to parenthesis
|
|
|
|
/// levels.
|
|
|
|
std::vector<ParenState> Stack;
|
|
|
|
|
2013-05-22 13:27:42 +08:00
|
|
|
/// \brief Ignore the stack of \c ParenStates for state comparison.
|
|
|
|
///
|
|
|
|
/// In long and deeply nested unwrapped lines, the current algorithm can
|
|
|
|
/// be insufficient for finding the best formatting with a reasonable amount
|
|
|
|
/// of time and memory. Setting this flag will effectively lead to the
|
|
|
|
/// algorithm not analyzing some combinations. However, these combinations
|
|
|
|
/// rarely contain the optimal solution: In short, accepting a higher
|
|
|
|
/// penalty early would need to lead to different values in the \c
|
|
|
|
/// ParenState stack (in an otherwise identical state) and these different
|
|
|
|
/// values would need to lead to a significant amount of avoided penalty
|
|
|
|
/// later.
|
|
|
|
///
|
|
|
|
/// FIXME: Come up with a better algorithm instead.
|
|
|
|
bool IgnoreStackForComparison;
|
|
|
|
|
2013-01-11 18:22:12 +08:00
|
|
|
/// \brief Comparison operator to be able to used \c LineState in \c map.
|
|
|
|
bool operator<(const LineState &Other) const {
|
2013-02-19 17:28:55 +08:00
|
|
|
if (NextToken != Other.NextToken)
|
|
|
|
return NextToken < Other.NextToken;
|
|
|
|
if (Column != Other.Column)
|
|
|
|
return Column < Other.Column;
|
|
|
|
if (LineContainsContinuedForLoopSection !=
|
2013-04-15 22:28:00 +08:00
|
|
|
Other.LineContainsContinuedForLoopSection)
|
2012-12-21 22:37:20 +08:00
|
|
|
return LineContainsContinuedForLoopSection;
|
2013-02-19 17:28:55 +08:00
|
|
|
if (ParenLevel != Other.ParenLevel)
|
|
|
|
return ParenLevel < Other.ParenLevel;
|
|
|
|
if (StartOfLineLevel != Other.StartOfLineLevel)
|
|
|
|
return StartOfLineLevel < Other.StartOfLineLevel;
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
if (LowestLevelOnLine != Other.LowestLevelOnLine)
|
|
|
|
return LowestLevelOnLine < Other.LowestLevelOnLine;
|
2013-02-20 23:25:48 +08:00
|
|
|
if (StartOfStringLiteral != Other.StartOfStringLiteral)
|
|
|
|
return StartOfStringLiteral < Other.StartOfStringLiteral;
|
2013-05-22 13:27:42 +08:00
|
|
|
if (IgnoreStackForComparison || Other.IgnoreStackForComparison)
|
|
|
|
return false;
|
2013-02-19 17:28:55 +08:00
|
|
|
return Stack < Other.Stack;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
/// \brief Formats the line starting at \p State, simply keeping all of the
|
|
|
|
/// input's line breaking decisions.
|
|
|
|
void formatWithoutColumnLimit(LineState &State) {
|
|
|
|
while (State.NextToken != NULL) {
|
2013-07-27 00:56:36 +08:00
|
|
|
bool Newline = mustBreak(State) ||
|
|
|
|
(canBreak(State) && State.NextToken->NewlinesBefore > 0);
|
2013-07-24 21:10:59 +08:00
|
|
|
addTokenToState(Newline, /*DryRun=*/false, State);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:54:30 +08:00
|
|
|
/// \brief Appends the next token to \p State and updates information
|
|
|
|
/// necessary for indentation.
|
|
|
|
///
|
2013-06-26 10:42:46 +08:00
|
|
|
/// Puts the token on the current line if \p Newline is \c false and adds a
|
2012-12-04 22:54:30 +08:00
|
|
|
/// line break and necessary indentation otherwise.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, also creates and stores the required
|
|
|
|
/// \c Replacement.
|
2013-02-20 18:15:13 +08:00
|
|
|
unsigned addTokenToState(bool Newline, bool DryRun, LineState &State) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *State.NextToken->Previous;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-07-12 04:41:21 +08:00
|
|
|
// Extra penalty that needs to be added because of the way certain line
|
|
|
|
// breaks are chosen.
|
|
|
|
unsigned ExtraPenalty = 0;
|
|
|
|
|
2013-03-20 23:58:10 +08:00
|
|
|
if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
|
2013-05-23 18:56:37 +08:00
|
|
|
// FIXME: Is this correct?
|
2013-05-29 22:47:47 +08:00
|
|
|
int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
|
|
|
|
State.NextToken->WhitespaceRange.getEnd()) -
|
|
|
|
SourceMgr.getSpellingColumnNumber(
|
|
|
|
State.NextToken->WhitespaceRange.getBegin());
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Column += WhitespaceLength + State.NextToken->CodePointCount;
|
2013-05-29 22:47:47 +08:00
|
|
|
State.NextToken = State.NextToken->Next;
|
2013-02-20 18:15:13 +08:00
|
|
|
return 0;
|
2013-02-01 19:00:45 +08:00
|
|
|
}
|
|
|
|
|
2013-04-03 15:21:51 +08:00
|
|
|
// If we are continuing an expression, we want to indent an extra 4 spaces.
|
|
|
|
unsigned ContinuationIndent =
|
2013-04-02 22:33:13 +08:00
|
|
|
std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (Newline) {
|
2013-07-08 22:25:23 +08:00
|
|
|
State.Stack.back().ContainsLineBreak = true;
|
2013-01-10 19:52:21 +08:00
|
|
|
if (Current.is(tok::r_brace)) {
|
2013-07-09 17:06:29 +08:00
|
|
|
if (Current.BlockKind == BK_BracedInit)
|
|
|
|
State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
|
|
|
|
else
|
2013-07-12 05:27:40 +08:00
|
|
|
State.Column = FirstIndent;
|
2013-01-09 15:06:56 +08:00
|
|
|
} else if (Current.is(tok::string_literal) &&
|
2013-02-20 23:25:48 +08:00
|
|
|
State.StartOfStringLiteral != 0) {
|
|
|
|
State.Column = State.StartOfStringLiteral;
|
2013-02-18 19:59:17 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-01-09 15:06:56 +08:00
|
|
|
} else if (Current.is(tok::lessless) &&
|
2013-02-08 23:28:42 +08:00
|
|
|
State.Stack.back().FirstLessLess != 0) {
|
|
|
|
State.Column = State.Stack.back().FirstLessLess;
|
2013-05-28 19:30:49 +08:00
|
|
|
} else if (Current.isOneOf(tok::period, tok::arrow) &&
|
|
|
|
Current.Type != TT_DesignatedInitializerPeriod) {
|
2013-04-03 15:21:51 +08:00
|
|
|
if (State.Stack.back().CallContinuation == 0) {
|
|
|
|
State.Column = ContinuationIndent;
|
2013-04-02 22:33:13 +08:00
|
|
|
State.Stack.back().CallContinuation = State.Column;
|
2013-04-03 15:21:51 +08:00
|
|
|
} else {
|
|
|
|
State.Column = State.Stack.back().CallContinuation;
|
|
|
|
}
|
2013-01-28 20:45:14 +08:00
|
|
|
} else if (Current.Type == TT_ConditionalExpr) {
|
|
|
|
State.Column = State.Stack.back().QuestionColumn;
|
2013-04-03 21:36:17 +08:00
|
|
|
} else if (Previous.is(tok::comma) &&
|
|
|
|
State.Stack.back().VariablePos != 0) {
|
|
|
|
State.Column = State.Stack.back().VariablePos;
|
Allow breaking between a type and name in variable declarations.
This fixes llvm.org/PR14967 and is generall necessary to avoid
situations where the column limit is exceeded. The challenge is
restricting such lines splits, otherwise clang-format suddenly starts
breaking at bad places.
Before:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string,
SomeOtherTemplateParameter> &ReallyReallyLongParameterName,
const SomeType<string,
SomeOtherTemplateParameter> &AnotherLongParameterName) {}
After:
ReallyLongReturnType<TemplateParam1, TemplateParam2>
ReallyReallyLongFunctionName(
const std::string &SomeParameter,
const SomeType<string, SomeOtherTemplateParameter> &
ReallyReallyLongParameterName,
const SomeType<string, SomeOtherTemplateParameter> &
AnotherLongParameterName) {}
llvm-svn: 175999
2013-02-25 02:54:32 +08:00
|
|
|
} else if (Previous.ClosesTemplateDeclaration ||
|
2013-07-09 15:43:55 +08:00
|
|
|
((Current.Type == TT_StartOfName ||
|
|
|
|
Current.is(tok::kw_operator)) &&
|
|
|
|
State.ParenLevel == 0 &&
|
2013-06-22 01:25:42 +08:00
|
|
|
(!Style.IndentFunctionDeclarationAfterType ||
|
|
|
|
Line.StartsDefinition))) {
|
2013-04-02 22:33:13 +08:00
|
|
|
State.Column = State.Stack.back().Indent;
|
2013-02-05 18:07:47 +08:00
|
|
|
} else if (Current.Type == TT_ObjCSelectorName) {
|
2013-06-05 22:09:10 +08:00
|
|
|
if (State.Stack.back().ColonPos > Current.CodePointCount) {
|
|
|
|
State.Column = State.Stack.back().ColonPos - Current.CodePointCount;
|
2013-02-05 18:07:47 +08:00
|
|
|
} else {
|
|
|
|
State.Column = State.Stack.back().Indent;
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Stack.back().ColonPos = State.Column + Current.CodePointCount;
|
2013-02-05 18:07:47 +08:00
|
|
|
}
|
2013-07-12 19:19:37 +08:00
|
|
|
} else if (Current.is(tok::l_square) &&
|
|
|
|
Current.Type != TT_ObjCMethodExpr) {
|
|
|
|
if (State.Stack.back().StartOfArraySubscripts != 0)
|
|
|
|
State.Column = State.Stack.back().StartOfArraySubscripts;
|
|
|
|
else
|
|
|
|
State.Column = ContinuationIndent;
|
2013-05-08 18:00:18 +08:00
|
|
|
} else if (Current.Type == TT_StartOfName ||
|
|
|
|
Previous.isOneOf(tok::coloncolon, tok::equal) ||
|
2013-04-02 22:33:13 +08:00
|
|
|
Previous.Type == TT_ObjCMethodExpr) {
|
2013-04-03 15:21:51 +08:00
|
|
|
State.Column = ContinuationIndent;
|
2012-12-21 22:37:20 +08:00
|
|
|
} else {
|
2013-02-08 23:28:42 +08:00
|
|
|
State.Column = State.Stack.back().Indent;
|
2013-04-03 15:21:51 +08:00
|
|
|
// Ensure that we fall back to indenting 4 spaces instead of just
|
|
|
|
// flushing continuations left.
|
2013-04-02 22:33:13 +08:00
|
|
|
if (State.Column == FirstIndent)
|
|
|
|
State.Column += 4;
|
2012-12-21 22:37:20 +08:00
|
|
|
}
|
|
|
|
|
2013-02-15 19:07:25 +08:00
|
|
|
if (Current.is(tok::question))
|
2013-02-24 05:01:55 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
2013-05-08 22:12:04 +08:00
|
|
|
if ((Previous.isOneOf(tok::comma, tok::semi) &&
|
|
|
|
!State.Stack.back().AvoidBinPacking) ||
|
|
|
|
Previous.Type == TT_BinaryOperator)
|
2013-02-08 16:22:00 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2013-05-15 17:35:08 +08:00
|
|
|
if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2013-01-31 22:59:26 +08:00
|
|
|
|
2013-01-03 02:33:23 +08:00
|
|
|
if (!DryRun) {
|
2013-02-26 21:10:34 +08:00
|
|
|
unsigned NewLines = 1;
|
2013-06-13 03:04:12 +08:00
|
|
|
if (Current.is(tok::comment))
|
2013-05-29 22:47:47 +08:00
|
|
|
NewLines = std::max(
|
|
|
|
NewLines,
|
|
|
|
std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1));
|
2013-05-22 20:51:29 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
|
|
|
|
State.Column, Line.InPPDirective);
|
2013-01-03 02:33:23 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-07-11 21:48:16 +08:00
|
|
|
if (!Current.isTrailingComment())
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-05-28 19:30:49 +08:00
|
|
|
if (Current.isOneOf(tok::arrow, tok::period) &&
|
|
|
|
Current.Type != TT_DesignatedInitializerPeriod)
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Stack.back().LastSpace += Current.CodePointCount;
|
2013-02-18 19:05:07 +08:00
|
|
|
State.StartOfLineLevel = State.ParenLevel;
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
State.LowestLevelOnLine = State.ParenLevel;
|
2013-02-24 05:01:55 +08:00
|
|
|
|
|
|
|
// Any break on this level means that the parent level has been broken
|
|
|
|
// and we need to avoid bin packing there.
|
|
|
|
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
}
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *TokenBefore = Current.getPreviousNonComment();
|
2013-04-16 06:36:37 +08:00
|
|
|
if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
|
2013-05-15 17:35:08 +08:00
|
|
|
TokenBefore->Type != TT_TemplateCloser &&
|
2013-05-08 22:12:04 +08:00
|
|
|
TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
|
2013-02-27 17:47:53 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
|
2013-02-24 05:01:55 +08:00
|
|
|
// If we break after {, we should also break before the corresponding }.
|
|
|
|
if (Previous.is(tok::l_brace))
|
|
|
|
State.Stack.back().BreakBeforeClosingBrace = true;
|
|
|
|
|
|
|
|
if (State.Stack.back().AvoidBinPacking) {
|
|
|
|
// If we are breaking after '(', '{', '<', this is not bin packing
|
|
|
|
// unless AllowAllParametersOfDeclarationOnNextLine is false.
|
2013-05-15 04:39:56 +08:00
|
|
|
if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
|
|
|
|
Previous.Type == TT_BinaryOperator) ||
|
2013-02-24 05:01:55 +08:00
|
|
|
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
|
|
|
|
Line.MustBeDeclaration))
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
}
|
2013-07-12 04:41:21 +08:00
|
|
|
|
|
|
|
// Breaking before the first "<<" is generally not desirable.
|
|
|
|
if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
|
|
|
|
ExtraPenalty += Style.PenaltyBreakFirstLessLess;
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
} else {
|
2013-02-25 23:59:54 +08:00
|
|
|
if (Current.is(tok::equal) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
(RootToken->is(tok::kw_for) || State.ParenLevel == 0) &&
|
Improve formatting of multi-variable DeclStmts.
This fixed llvm.org/PR15670
Before:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc,
d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc,
*d = ddddddddddddddddddd;
After:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc, d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc, *d = ddddddddddddddddddd;
llvm-svn: 178860
2013-04-05 17:38:50 +08:00
|
|
|
State.Stack.back().VariablePos == 0) {
|
|
|
|
State.Stack.back().VariablePos = State.Column;
|
|
|
|
// Move over * and & if they are bound to the variable name.
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *Tok = &Previous;
|
2013-06-05 22:09:10 +08:00
|
|
|
while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) {
|
|
|
|
State.Stack.back().VariablePos -= Tok->CodePointCount;
|
Improve formatting of multi-variable DeclStmts.
This fixed llvm.org/PR15670
Before:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc,
d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc,
*d = ddddddddddddddddddd;
After:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc, d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc, *d = ddddddddddddddddddd;
llvm-svn: 178860
2013-04-05 17:38:50 +08:00
|
|
|
if (Tok->SpacesRequiredBefore != 0)
|
|
|
|
break;
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok = Tok->Previous;
|
Improve formatting of multi-variable DeclStmts.
This fixed llvm.org/PR15670
Before:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc,
d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc,
*d = ddddddddddddddddddd;
After:
aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,
c = cccccccccccccccccccc, d = dddddddddddddddddddd;
aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,
*c = ccccccccccccccccccc, *d = ddddddddddddddddddd;
llvm-svn: 178860
2013-04-05 17:38:50 +08:00
|
|
|
}
|
2013-04-03 21:36:17 +08:00
|
|
|
if (Previous.PartOfMultiVariableDeclStmt)
|
|
|
|
State.Stack.back().LastSpace = State.Stack.back().VariablePos;
|
|
|
|
}
|
2012-12-21 22:37:20 +08:00
|
|
|
|
2013-02-11 20:36:37 +08:00
|
|
|
unsigned Spaces = State.NextToken->SpacesRequiredBefore;
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
if (!DryRun)
|
2013-05-22 20:51:29 +08:00
|
|
|
Whitespaces.replaceWhitespace(Current, 0, Spaces,
|
|
|
|
State.Column + Spaces);
|
2012-12-04 22:54:30 +08:00
|
|
|
|
2013-02-05 18:07:47 +08:00
|
|
|
if (Current.Type == TT_ObjCSelectorName &&
|
|
|
|
State.Stack.back().ColonPos == 0) {
|
|
|
|
if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Column + Spaces + Current.CodePointCount)
|
2013-02-05 18:07:47 +08:00
|
|
|
State.Stack.back().ColonPos =
|
|
|
|
State.Stack.back().Indent + Current.LongestObjCSelectorName;
|
|
|
|
else
|
|
|
|
State.Stack.back().ColonPos =
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Column + Spaces + Current.CodePointCount;
|
2013-02-05 18:07:47 +08:00
|
|
|
}
|
|
|
|
|
2013-04-10 17:49:49 +08:00
|
|
|
if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
|
2013-04-09 04:33:42 +08:00
|
|
|
Current.Type != TT_LineComment)
|
2013-02-08 23:28:42 +08:00
|
|
|
State.Stack.back().Indent = State.Column + Spaces;
|
2013-04-22 15:59:53 +08:00
|
|
|
if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
|
|
|
|
State.Stack.back().AvoidBinPacking)
|
|
|
|
State.Stack.back().NoLineBreak = true;
|
2013-01-16 22:59:02 +08:00
|
|
|
|
2012-12-06 17:56:08 +08:00
|
|
|
State.Column += Spaces;
|
2013-04-03 21:36:17 +08:00
|
|
|
if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
|
2013-01-24 04:41:06 +08:00
|
|
|
// Treat the condition inside an if as if it was a second function
|
|
|
|
// parameter, i.e. let nested calls have an indent of 4.
|
|
|
|
State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
|
2013-03-20 20:37:50 +08:00
|
|
|
else if (Previous.is(tok::comma))
|
2013-01-24 04:41:06 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-01-28 20:45:14 +08:00
|
|
|
else if ((Previous.Type == TT_BinaryOperator ||
|
2013-01-28 21:31:35 +08:00
|
|
|
Previous.Type == TT_ConditionalExpr ||
|
|
|
|
Previous.Type == TT_CtorInitializerColon) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
!(Previous.getPrecedence() == prec::Assignment &&
|
2013-05-27 20:45:09 +08:00
|
|
|
Current.FakeLParens.empty()))
|
|
|
|
// Always indent relative to the RHS of the expression unless this is a
|
|
|
|
// simple assignment without binary expression on the RHS.
|
2013-01-28 17:35:24 +08:00
|
|
|
State.Stack.back().LastSpace = State.Column;
|
2013-02-14 16:42:54 +08:00
|
|
|
else if (Previous.Type == TT_InheritanceColon)
|
|
|
|
State.Stack.back().Indent = State.Column;
|
2013-07-09 19:57:27 +08:00
|
|
|
else if (Previous.opensScope()) {
|
|
|
|
// If a function has multiple parameters (including a single parameter
|
2013-07-09 22:36:48 +08:00
|
|
|
// that is a binary expression) or a trailing call, indent all
|
2013-07-09 19:57:27 +08:00
|
|
|
// parameters from the opening parenthesis. This avoids confusing
|
|
|
|
// indents like:
|
|
|
|
// OuterFunction(InnerFunctionCall(
|
|
|
|
// ParameterToInnerFunction),
|
|
|
|
// SecondParameterToOuterFunction);
|
|
|
|
bool HasMultipleParameters = !Current.FakeLParens.empty();
|
|
|
|
bool HasTrailingCall = false;
|
|
|
|
if (Previous.MatchingParen) {
|
|
|
|
const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
|
|
|
|
if (Next && Next->isOneOf(tok::period, tok::arrow))
|
|
|
|
HasTrailingCall = true;
|
|
|
|
}
|
|
|
|
if (HasMultipleParameters || HasTrailingCall)
|
|
|
|
State.Stack.back().LastSpace = State.Column;
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-16 22:59:02 +08:00
|
|
|
|
2013-07-12 19:37:05 +08:00
|
|
|
return moveStateToNextToken(State, DryRun, Newline) + ExtraPenalty;
|
2012-12-04 22:54:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Mark the next token as consumed in \p State and modify its stacks
|
|
|
|
/// accordingly.
|
2013-07-12 19:37:05 +08:00
|
|
|
unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
2013-01-11 18:22:12 +08:00
|
|
|
assert(State.Stack.size());
|
2012-12-06 17:56:08 +08:00
|
|
|
|
2013-02-14 16:42:54 +08:00
|
|
|
if (Current.Type == TT_InheritanceColon)
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
2013-01-11 18:22:12 +08:00
|
|
|
if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
|
|
|
|
State.Stack.back().FirstLessLess = State.Column;
|
2013-07-12 19:19:37 +08:00
|
|
|
if (Current.is(tok::l_square) &&
|
|
|
|
State.Stack.back().StartOfArraySubscripts == 0)
|
|
|
|
State.Stack.back().StartOfArraySubscripts = State.Column;
|
2013-01-28 20:45:14 +08:00
|
|
|
if (Current.is(tok::question))
|
|
|
|
State.Stack.back().QuestionColumn = State.Column;
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
if (!Current.opensScope() && !Current.closesScope())
|
|
|
|
State.LowestLevelOnLine =
|
|
|
|
std::min(State.LowestLevelOnLine, State.ParenLevel);
|
|
|
|
if (Current.isOneOf(tok::period, tok::arrow) &&
|
|
|
|
Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
|
|
|
|
State.Stack.back().StartOfFunctionCall =
|
|
|
|
Current.LastInChainOfCalls ? 0
|
|
|
|
: State.Column + Current.CodePointCount;
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
if (Current.Type == TT_CtorInitializerColon) {
|
2013-05-13 16:42:42 +08:00
|
|
|
// Indent 2 from the column, so:
|
|
|
|
// SomeClass::SomeClass()
|
|
|
|
// : First(...), ...
|
|
|
|
// Next(...)
|
|
|
|
// ^ line up here.
|
2013-07-27 00:56:36 +08:00
|
|
|
if (!Style.BreakConstructorInitializersBeforeComma)
|
|
|
|
State.Stack.back().Indent = State.Column + 2;
|
Allow breaking between type and name in for loops.
This fixes llvm.org/PR15033.
Also: Always break before a parameter, if the previous parameter was
split over multiple lines. This was necessary to make the right
decisions in for-loops, almost always makes the code more readable and
also fixes llvm.org/PR14873.
Before:
for (llvm::ArrayRef<NamedDecl *>::iterator I = FD->getDeclsInPrototypeScope()
.begin(), E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc), d, bar(e, f));
After:
for (llvm::ArrayRef<NamedDecl *>::iterator
I = FD->getDeclsInPrototypeScope().begin(),
E = FD->getDeclsInPrototypeScope().end();
I != E; ++I) {
}
foo(bar(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
ccccccccccccccccccccccccccccc),
d, bar(e, f));
llvm-svn: 175741
2013-02-21 23:00:29 +08:00
|
|
|
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
|
|
|
|
State.Stack.back().AvoidBinPacking = true;
|
|
|
|
State.Stack.back().BreakBeforeParameter = false;
|
2013-01-31 22:59:26 +08:00
|
|
|
}
|
2013-04-03 15:21:51 +08:00
|
|
|
|
2013-04-09 04:33:42 +08:00
|
|
|
// If return returns a binary expression, align after it.
|
|
|
|
if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
|
|
|
|
State.Stack.back().LastSpace = State.Column + 7;
|
|
|
|
|
2013-04-03 15:21:51 +08:00
|
|
|
// In ObjC method declaration we align on the ":" of parameters, but we need
|
|
|
|
// to ensure that we indent parameters on subsequent lines by at least 4.
|
2013-04-02 22:33:13 +08:00
|
|
|
if (Current.Type == TT_ObjCMethodSpecifier)
|
|
|
|
State.Stack.back().Indent += 4;
|
2012-12-06 17:56:08 +08:00
|
|
|
|
2013-02-08 23:28:42 +08:00
|
|
|
// Insert scopes created by fake parenthesis.
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *Previous = Current.getPreviousNonComment();
|
2013-04-09 04:33:42 +08:00
|
|
|
// Don't add extra indentation for the first fake parenthesis after
|
|
|
|
// 'return', assignements or opening <({[. The indentation for these cases
|
|
|
|
// is special cased.
|
|
|
|
bool SkipFirstExtraIndent =
|
|
|
|
Current.is(tok::kw_return) ||
|
2013-04-10 17:49:49 +08:00
|
|
|
(Previous && (Previous->opensScope() ||
|
2013-05-29 22:47:47 +08:00
|
|
|
Previous->getPrecedence() == prec::Assignment));
|
2013-07-08 11:55:09 +08:00
|
|
|
for (SmallVectorImpl<prec::Level>::const_reverse_iterator
|
2013-04-09 04:33:42 +08:00
|
|
|
I = Current.FakeLParens.rbegin(),
|
|
|
|
E = Current.FakeLParens.rend();
|
|
|
|
I != E; ++I) {
|
2013-02-08 23:28:42 +08:00
|
|
|
ParenState NewParenState = State.Stack.back();
|
2013-07-08 22:25:23 +08:00
|
|
|
NewParenState.ContainsLineBreak = false;
|
2013-04-09 04:33:42 +08:00
|
|
|
NewParenState.Indent =
|
|
|
|
std::max(std::max(State.Column, NewParenState.Indent),
|
|
|
|
State.Stack.back().LastSpace);
|
|
|
|
|
|
|
|
// Always indent conditional expressions. Never indent expression where
|
|
|
|
// the 'operator' is ',', ';' or an assignment (i.e. *I <=
|
|
|
|
// prec::Assignment) as those have different indentation rules. Indent
|
|
|
|
// other expression, unless the indentation needs to be skipped.
|
|
|
|
if (*I == prec::Conditional ||
|
2013-07-27 00:56:36 +08:00
|
|
|
(!SkipFirstExtraIndent && *I > prec::Assignment &&
|
|
|
|
!Style.BreakBeforeBinaryOperators))
|
2013-04-09 04:33:42 +08:00
|
|
|
NewParenState.Indent += 4;
|
2013-04-10 17:49:49 +08:00
|
|
|
if (Previous && !Previous->opensScope())
|
2013-04-09 04:33:42 +08:00
|
|
|
NewParenState.BreakBeforeParameter = false;
|
2013-02-08 23:28:42 +08:00
|
|
|
State.Stack.push_back(NewParenState);
|
2013-04-09 04:33:42 +08:00
|
|
|
SkipFirstExtraIndent = false;
|
2013-02-08 23:28:42 +08:00
|
|
|
}
|
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
// If we encounter an opening (, [, { or <, we add a level to our stacks to
|
2012-12-04 22:54:30 +08:00
|
|
|
// prepare for the following tokens.
|
2013-04-10 17:49:49 +08:00
|
|
|
if (Current.opensScope()) {
|
2013-01-11 18:22:12 +08:00
|
|
|
unsigned NewIndent;
|
2013-05-13 17:19:24 +08:00
|
|
|
unsigned LastSpace = State.Stack.back().LastSpace;
|
2013-01-31 22:59:26 +08:00
|
|
|
bool AvoidBinPacking;
|
2013-01-10 22:36:46 +08:00
|
|
|
if (Current.is(tok::l_brace)) {
|
2013-07-17 02:22:10 +08:00
|
|
|
NewIndent =
|
|
|
|
LastSpace + (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
|
2013-07-04 22:47:51 +08:00
|
|
|
const FormatToken *NextNoComment = Current.getNextNonComment();
|
2013-05-28 19:30:49 +08:00
|
|
|
AvoidBinPacking = NextNoComment &&
|
|
|
|
NextNoComment->Type == TT_DesignatedInitializerPeriod;
|
2013-01-10 22:36:46 +08:00
|
|
|
} else {
|
2013-05-13 17:19:24 +08:00
|
|
|
NewIndent =
|
|
|
|
4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
|
2013-07-10 22:02:49 +08:00
|
|
|
AvoidBinPacking = !Style.BinPackParameters ||
|
|
|
|
(Style.ExperimentalAutoDetectBinPacking &&
|
|
|
|
(Current.PackingKind == PPK_OnePerLine ||
|
|
|
|
(!BinPackInconclusiveFunctions &&
|
|
|
|
Current.PackingKind == PPK_Inconclusive)));
|
2013-01-10 22:36:46 +08:00
|
|
|
}
|
2013-04-25 21:31:51 +08:00
|
|
|
|
2013-05-13 17:19:24 +08:00
|
|
|
State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
|
|
|
|
State.Stack.back().NoLineBreak));
|
2013-02-08 23:28:42 +08:00
|
|
|
++State.ParenLevel;
|
2012-12-04 22:54:30 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-02-08 16:22:00 +08:00
|
|
|
// If this '[' opens an ObjC call, determine whether all parameters fit into
|
|
|
|
// one line and put one per line if they don't.
|
|
|
|
if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
|
|
|
|
Current.MatchingParen != NULL) {
|
|
|
|
if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
|
|
|
|
State.Stack.back().BreakBeforeParameter = true;
|
|
|
|
}
|
|
|
|
|
2012-12-24 21:43:52 +08:00
|
|
|
// If we encounter a closing ), ], } or >, we can remove a level from our
|
2012-12-04 22:54:30 +08:00
|
|
|
// stacks.
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Current.isOneOf(tok::r_paren, tok::r_square) ||
|
2013-05-29 22:47:47 +08:00
|
|
|
(Current.is(tok::r_brace) && State.NextToken != RootToken) ||
|
2013-01-08 22:56:18 +08:00
|
|
|
State.NextToken->Type == TT_TemplateCloser) {
|
2013-01-11 18:22:12 +08:00
|
|
|
State.Stack.pop_back();
|
2013-02-08 23:28:42 +08:00
|
|
|
--State.ParenLevel;
|
|
|
|
}
|
2013-07-12 19:19:37 +08:00
|
|
|
if (Current.is(tok::r_square)) {
|
|
|
|
// If this ends the array subscript expr, reset the corresponding value.
|
|
|
|
const FormatToken *NextNonComment = Current.getNextNonComment();
|
|
|
|
if (NextNonComment && NextNonComment->isNot(tok::l_square))
|
2013-07-15 22:33:14 +08:00
|
|
|
State.Stack.back().StartOfArraySubscripts = 0;
|
2013-07-12 19:19:37 +08:00
|
|
|
}
|
2013-02-08 23:28:42 +08:00
|
|
|
|
|
|
|
// Remove scopes created by fake parenthesis.
|
|
|
|
for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
|
2013-04-05 03:31:00 +08:00
|
|
|
unsigned VariablePos = State.Stack.back().VariablePos;
|
2013-02-08 23:28:42 +08:00
|
|
|
State.Stack.pop_back();
|
2013-04-05 03:31:00 +08:00
|
|
|
State.Stack.back().VariablePos = VariablePos;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-10 22:36:46 +08:00
|
|
|
|
2013-05-14 04:50:15 +08:00
|
|
|
if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
|
2013-02-20 23:25:48 +08:00
|
|
|
State.StartOfStringLiteral = State.Column;
|
2013-05-14 04:50:15 +08:00
|
|
|
} else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
|
|
|
|
tok::string_literal)) {
|
2013-05-16 12:26:02 +08:00
|
|
|
State.StartOfStringLiteral = 0;
|
2013-02-20 23:25:48 +08:00
|
|
|
}
|
|
|
|
|
2013-06-05 22:09:10 +08:00
|
|
|
State.Column += Current.CodePointCount;
|
2013-02-20 18:15:13 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
State.NextToken = State.NextToken->Next;
|
2013-01-10 22:36:46 +08:00
|
|
|
|
2013-07-12 19:37:05 +08:00
|
|
|
if (!Newline && Style.AlwaysBreakBeforeMultilineStrings &&
|
2013-08-02 19:01:15 +08:00
|
|
|
Current.is(tok::string_literal) && Current.CanBreakBefore)
|
2013-07-12 19:37:05 +08:00
|
|
|
return 0;
|
|
|
|
|
2013-02-20 18:15:13 +08:00
|
|
|
return breakProtrudingToken(Current, State, DryRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If the current token sticks out over the end of the line, break
|
|
|
|
/// it if possible.
|
2013-05-14 17:04:24 +08:00
|
|
|
///
|
|
|
|
/// \returns An extra penalty if a token was broken, otherwise 0.
|
|
|
|
///
|
2013-07-01 21:42:42 +08:00
|
|
|
/// The returned penalty will cover the cost of the additional line breaks and
|
|
|
|
/// column limit violation in all lines except for the last one. The penalty
|
|
|
|
/// for the column limit violation in the last line (and in single line
|
|
|
|
/// tokens) is handled in \c addNextStateToQueue.
|
2013-05-29 22:47:47 +08:00
|
|
|
unsigned breakProtrudingToken(const FormatToken &Current, LineState &State,
|
2013-05-22 20:51:29 +08:00
|
|
|
bool DryRun) {
|
2013-04-15 22:28:00 +08:00
|
|
|
llvm::OwningPtr<BreakableToken> Token;
|
2013-06-05 22:09:10 +08:00
|
|
|
unsigned StartColumn = State.Column - Current.CodePointCount;
|
2013-05-28 21:42:28 +08:00
|
|
|
unsigned OriginalStartColumn =
|
2013-05-29 22:47:47 +08:00
|
|
|
SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) -
|
2013-05-28 21:42:28 +08:00
|
|
|
1;
|
2013-05-27 23:23:34 +08:00
|
|
|
|
2013-05-16 20:59:13 +08:00
|
|
|
if (Current.is(tok::string_literal) &&
|
|
|
|
Current.Type != TT_ImplicitStringLiteral) {
|
2013-04-15 22:28:00 +08:00
|
|
|
// Only break up default narrow strings.
|
2013-06-14 19:46:10 +08:00
|
|
|
if (!Current.TokenText.startswith("\""))
|
2013-04-15 22:28:00 +08:00
|
|
|
return 0;
|
2013-07-17 05:06:13 +08:00
|
|
|
// Don't break string literals with escaped newlines. As clang-format must
|
|
|
|
// not change the string's content, it is unlikely that we'll end up with
|
|
|
|
// a better format.
|
|
|
|
if (Current.TokenText.find("\\\n") != StringRef::npos)
|
|
|
|
return 0;
|
2013-07-17 04:28:33 +08:00
|
|
|
// Exempts unterminated string literals from line breaking. The user will
|
|
|
|
// likely want to terminate the string before any line breaking is done.
|
|
|
|
if (Current.IsUnterminatedLiteral)
|
|
|
|
return 0;
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
Token.reset(new BreakableStringLiteral(Current, StartColumn,
|
|
|
|
Line.InPPDirective, Encoding));
|
2013-07-17 07:47:22 +08:00
|
|
|
} else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
|
2013-06-14 19:46:10 +08:00
|
|
|
Token.reset(new BreakableBlockComment(
|
2013-06-05 22:09:10 +08:00
|
|
|
Style, Current, StartColumn, OriginalStartColumn, !Current.Previous,
|
2013-06-14 19:46:10 +08:00
|
|
|
Line.InPPDirective, Encoding));
|
2013-05-06 18:24:51 +08:00
|
|
|
} else if (Current.Type == TT_LineComment &&
|
2013-05-29 22:47:47 +08:00
|
|
|
(Current.Previous == NULL ||
|
|
|
|
Current.Previous->Type != TT_ImplicitStringLiteral)) {
|
2013-07-17 05:06:13 +08:00
|
|
|
// Don't break line comments with escaped newlines. These look like
|
|
|
|
// separate line comments, but in fact contain a single line comment with
|
|
|
|
// multiple lines including leading whitespace and the '//' markers.
|
|
|
|
//
|
|
|
|
// FIXME: If we want to handle them correctly, we'll need to adjust
|
|
|
|
// leading whitespace in consecutive lines when changing indentation of
|
|
|
|
// the first line similar to what we do with block comments.
|
|
|
|
StringRef::size_type EscapedNewlinePos = Current.TokenText.find("\\\n");
|
|
|
|
if (EscapedNewlinePos != StringRef::npos) {
|
|
|
|
State.Column =
|
|
|
|
StartColumn +
|
|
|
|
encoding::getCodePointCount(
|
|
|
|
Current.TokenText.substr(0, EscapedNewlinePos), Encoding) +
|
|
|
|
1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-14 19:46:10 +08:00
|
|
|
Token.reset(new BreakableLineComment(Current, StartColumn,
|
|
|
|
Line.InPPDirective, Encoding));
|
2013-04-15 22:28:00 +08:00
|
|
|
} else {
|
2013-05-22 20:51:29 +08:00
|
|
|
return 0;
|
2013-04-15 22:28:00 +08:00
|
|
|
}
|
2013-06-14 19:46:10 +08:00
|
|
|
if (Current.UnbreakableTailLength >= getColumnLimit())
|
2013-05-14 17:04:24 +08:00
|
|
|
return 0;
|
2013-04-15 22:28:00 +08:00
|
|
|
|
2013-06-20 03:50:11 +08:00
|
|
|
unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength;
|
2013-04-15 22:28:00 +08:00
|
|
|
bool BreakInserted = false;
|
2013-02-20 18:15:13 +08:00
|
|
|
unsigned Penalty = 0;
|
2013-06-20 03:50:11 +08:00
|
|
|
unsigned RemainingTokenColumns = 0;
|
2013-05-27 23:23:34 +08:00
|
|
|
for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
|
|
|
|
LineIndex != EndIndex; ++LineIndex) {
|
2013-06-14 19:46:10 +08:00
|
|
|
if (!DryRun)
|
|
|
|
Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
|
2013-04-15 22:28:00 +08:00
|
|
|
unsigned TailOffset = 0;
|
2013-06-20 03:50:11 +08:00
|
|
|
RemainingTokenColumns = Token->getLineLengthAfterSplit(
|
2013-06-08 00:02:52 +08:00
|
|
|
LineIndex, TailOffset, StringRef::npos);
|
2013-06-05 22:09:10 +08:00
|
|
|
while (RemainingTokenColumns > RemainingSpace) {
|
2013-04-15 22:28:00 +08:00
|
|
|
BreakableToken::Split Split =
|
2013-05-22 20:51:29 +08:00
|
|
|
Token->getSplit(LineIndex, TailOffset, getColumnLimit());
|
2013-07-01 21:42:42 +08:00
|
|
|
if (Split.first == StringRef::npos) {
|
|
|
|
// The last line's penalty is handled in addNextStateToQueue().
|
|
|
|
if (LineIndex < EndIndex - 1)
|
|
|
|
Penalty += Style.PenaltyExcessCharacter *
|
|
|
|
(RemainingTokenColumns - RemainingSpace);
|
2013-04-15 22:28:00 +08:00
|
|
|
break;
|
2013-07-01 21:42:42 +08:00
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
assert(Split.first != 0);
|
2013-06-05 22:09:10 +08:00
|
|
|
unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
|
2013-06-08 00:02:52 +08:00
|
|
|
LineIndex, TailOffset + Split.first + Split.second,
|
|
|
|
StringRef::npos);
|
2013-06-05 22:09:10 +08:00
|
|
|
assert(NewRemainingTokenColumns < RemainingTokenColumns);
|
2013-06-14 19:46:10 +08:00
|
|
|
if (!DryRun)
|
|
|
|
Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
|
2013-06-08 00:02:52 +08:00
|
|
|
Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
|
|
|
|
: Style.PenaltyBreakComment;
|
|
|
|
unsigned ColumnsUsed =
|
|
|
|
Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
|
|
|
|
if (ColumnsUsed > getColumnLimit()) {
|
|
|
|
Penalty +=
|
|
|
|
Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit());
|
|
|
|
}
|
2013-04-15 22:28:00 +08:00
|
|
|
TailOffset += Split.first + Split.second;
|
2013-06-05 22:09:10 +08:00
|
|
|
RemainingTokenColumns = NewRemainingTokenColumns;
|
2013-04-15 22:28:00 +08:00
|
|
|
BreakInserted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 03:50:11 +08:00
|
|
|
State.Column = RemainingTokenColumns;
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
if (BreakInserted) {
|
2013-06-17 20:59:44 +08:00
|
|
|
// If we break the token inside a parameter list, we need to break before
|
|
|
|
// the next parameter on all levels, so that the next parameter is clearly
|
|
|
|
// visible. Line comments already introduce a break.
|
|
|
|
if (Current.Type != TT_LineComment) {
|
|
|
|
for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
|
|
|
|
State.Stack[i].BreakBeforeParameter = true;
|
|
|
|
}
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
State.Stack.back().LastSpace = StartColumn;
|
2013-02-20 18:15:13 +08:00
|
|
|
}
|
|
|
|
return Penalty;
|
|
|
|
}
|
|
|
|
|
2013-01-09 18:16:05 +08:00
|
|
|
unsigned getColumnLimit() {
|
2013-04-15 22:28:00 +08:00
|
|
|
// In preprocessor directives reserve two chars for trailing " \"
|
|
|
|
return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
|
2013-01-09 18:16:05 +08:00
|
|
|
}
|
|
|
|
|
2013-02-13 18:46:36 +08:00
|
|
|
/// \brief An edge in the solution space from \c Previous->State to \c State,
|
|
|
|
/// inserting a newline dependent on the \c NewLine.
|
|
|
|
struct StateNode {
|
|
|
|
StateNode(const LineState &State, bool NewLine, StateNode *Previous)
|
2013-02-22 05:33:55 +08:00
|
|
|
: State(State), NewLine(NewLine), Previous(Previous) {}
|
2013-02-13 18:46:36 +08:00
|
|
|
LineState State;
|
|
|
|
bool NewLine;
|
|
|
|
StateNode *Previous;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
|
|
|
|
///
|
|
|
|
/// In case of equal penalties, we want to prefer states that were inserted
|
|
|
|
/// first. During state generation we make sure that we insert states first
|
|
|
|
/// that break the line as late as possible.
|
|
|
|
typedef std::pair<unsigned, unsigned> OrderedPenalty;
|
2013-02-01 19:00:45 +08:00
|
|
|
|
2013-02-13 18:46:36 +08:00
|
|
|
/// \brief An item in the prioritized BFS search queue. The \c StateNode's
|
|
|
|
/// \c State has the given \c OrderedPenalty.
|
|
|
|
typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
|
|
|
|
|
|
|
|
/// \brief The BFS queue type.
|
|
|
|
typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
|
|
|
|
std::greater<QueueItem> > QueueType;
|
2013-02-01 19:00:45 +08:00
|
|
|
|
|
|
|
/// \brief Analyze the entire solution space starting from \p InitialState.
|
2012-12-04 02:12:45 +08:00
|
|
|
///
|
2013-02-01 19:00:45 +08:00
|
|
|
/// This implements a variant of Dijkstra's algorithm on the graph that spans
|
|
|
|
/// the solution space (\c LineStates are the nodes). The algorithm tries to
|
|
|
|
/// find the shortest path (the one with lowest penalty) from \p InitialState
|
|
|
|
/// to a state where all tokens are placed.
|
2013-05-22 20:51:29 +08:00
|
|
|
void analyzeSolutionSpace(LineState &InitialState) {
|
2013-02-13 18:46:36 +08:00
|
|
|
std::set<LineState> Seen;
|
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// Insert start element into queue.
|
2013-02-14 22:26:07 +08:00
|
|
|
StateNode *Node =
|
2013-02-13 18:46:36 +08:00
|
|
|
new (Allocator.Allocate()) StateNode(InitialState, false, NULL);
|
|
|
|
Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
|
|
|
|
++Count;
|
2013-02-01 19:00:45 +08:00
|
|
|
|
|
|
|
// While not empty, take first element and follow edges.
|
|
|
|
while (!Queue.empty()) {
|
2013-02-13 18:46:36 +08:00
|
|
|
unsigned Penalty = Queue.top().first.first;
|
2013-02-14 22:26:07 +08:00
|
|
|
StateNode *Node = Queue.top().second;
|
2013-02-13 18:46:36 +08:00
|
|
|
if (Node->State.NextToken == NULL) {
|
2013-05-10 19:56:10 +08:00
|
|
|
DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
|
2013-02-01 19:00:45 +08:00
|
|
|
break;
|
2013-02-04 15:21:18 +08:00
|
|
|
}
|
2013-02-13 18:46:36 +08:00
|
|
|
Queue.pop();
|
2013-02-01 19:00:45 +08:00
|
|
|
|
2013-05-22 13:27:42 +08:00
|
|
|
// Cut off the analysis of certain solutions if the analysis gets too
|
|
|
|
// complex. See description of IgnoreStackForComparison.
|
|
|
|
if (Count > 10000)
|
|
|
|
Node->State.IgnoreStackForComparison = true;
|
|
|
|
|
2013-02-13 18:46:36 +08:00
|
|
|
if (!Seen.insert(Node->State).second)
|
|
|
|
// State already examined with lower penalty.
|
|
|
|
continue;
|
2013-02-01 19:00:45 +08:00
|
|
|
|
2013-06-26 08:30:14 +08:00
|
|
|
addNextStateToQueue(Penalty, Node, /*NewLine=*/false);
|
|
|
|
addNextStateToQueue(Penalty, Node, /*NewLine=*/true);
|
2013-02-01 19:00:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Queue.empty())
|
|
|
|
// We were unable to find a solution, do nothing.
|
|
|
|
// FIXME: Add diagnostic?
|
2013-05-22 20:51:29 +08:00
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// Reconstruct the solution.
|
2013-02-13 18:46:36 +08:00
|
|
|
reconstructPath(InitialState, Queue.top().second);
|
2013-05-10 19:56:10 +08:00
|
|
|
DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
|
|
|
|
DEBUG(llvm::dbgs() << "---\n");
|
2013-02-13 18:46:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void reconstructPath(LineState &State, StateNode *Current) {
|
2013-05-29 23:10:11 +08:00
|
|
|
std::deque<StateNode *> Path;
|
|
|
|
// We do not need a break before the initial token.
|
|
|
|
while (Current->Previous) {
|
|
|
|
Path.push_front(Current);
|
|
|
|
Current = Current->Previous;
|
|
|
|
}
|
|
|
|
for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
DEBUG({
|
|
|
|
if ((*I)->NewLine) {
|
|
|
|
llvm::dbgs() << "Penalty for splitting before "
|
|
|
|
<< (*I)->Previous->State.NextToken->Tok.getName() << ": "
|
|
|
|
<< (*I)->Previous->State.NextToken->SplitPenalty << "\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addTokenToState((*I)->NewLine, false, State);
|
|
|
|
}
|
2013-02-01 19:00:45 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-02-13 18:54:19 +08:00
|
|
|
/// \brief Add the following state to the analysis queue \c Queue.
|
2013-02-01 19:00:45 +08:00
|
|
|
///
|
2013-02-13 18:54:19 +08:00
|
|
|
/// Assume the current state is \p PreviousNode and has been reached with a
|
2013-02-01 19:00:45 +08:00
|
|
|
/// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
|
2013-02-13 18:54:19 +08:00
|
|
|
void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
|
|
|
|
bool NewLine) {
|
2013-02-13 18:46:36 +08:00
|
|
|
if (NewLine && !canBreak(PreviousNode->State))
|
2013-02-01 19:00:45 +08:00
|
|
|
return;
|
2013-02-13 18:46:36 +08:00
|
|
|
if (!NewLine && mustBreak(PreviousNode->State))
|
2013-02-01 19:00:45 +08:00
|
|
|
return;
|
2013-07-08 22:25:23 +08:00
|
|
|
if (NewLine) {
|
|
|
|
if (!PreviousNode->State.Stack.back().ContainsLineBreak)
|
|
|
|
Penalty += 15;
|
2013-02-13 18:46:36 +08:00
|
|
|
Penalty += PreviousNode->State.NextToken->SplitPenalty;
|
2013-07-08 22:25:23 +08:00
|
|
|
}
|
2013-02-13 18:46:36 +08:00
|
|
|
|
|
|
|
StateNode *Node = new (Allocator.Allocate())
|
|
|
|
StateNode(PreviousNode->State, NewLine, PreviousNode);
|
2013-02-20 18:15:13 +08:00
|
|
|
Penalty += addTokenToState(NewLine, true, Node->State);
|
2013-02-13 18:46:36 +08:00
|
|
|
if (Node->State.Column > getColumnLimit()) {
|
|
|
|
unsigned ExcessCharacters = Node->State.Column - getColumnLimit();
|
2013-02-04 15:21:18 +08:00
|
|
|
Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
|
2012-12-05 22:57:28 +08:00
|
|
|
}
|
2013-02-13 18:46:36 +08:00
|
|
|
|
|
|
|
Queue.push(QueueItem(OrderedPenalty(Penalty, Count), Node));
|
|
|
|
++Count;
|
2013-02-01 19:00:45 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
/// \brief Returns \c true, if a line break after \p State is allowed.
|
|
|
|
bool canBreak(const LineState &State) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
|
|
|
assert(&Previous == Current.Previous);
|
2013-05-17 17:35:01 +08:00
|
|
|
if (!Current.CanBreakBefore &&
|
|
|
|
!(Current.is(tok::r_brace) &&
|
2013-02-01 19:00:45 +08:00
|
|
|
State.Stack.back().BreakBeforeClosingBrace))
|
|
|
|
return false;
|
2013-05-17 17:35:01 +08:00
|
|
|
// The opening "{" of a braced list has to be on the same line as the first
|
|
|
|
// element if it is nested in another braced init list or function call.
|
|
|
|
if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
Previous.Previous &&
|
|
|
|
Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
|
2013-05-17 17:35:01 +08:00
|
|
|
return false;
|
2013-05-27 19:50:16 +08:00
|
|
|
// This prevents breaks like:
|
|
|
|
// ...
|
|
|
|
// SomeParameter, OtherParameter).DoSomething(
|
|
|
|
// ...
|
|
|
|
// As they hide "DoSomething" and are generally bad for readability.
|
Improve detection for preventing certain kind of formatting patterns.
This is a better implementation of r183097. The main purpose is to
prevent certain constructs to be formatted "like a block of text".
Before:
aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa = new aaaaaaaaaaaaa<
aaaaaaaaaa, aaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
After:
aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>* aaaa =
new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(
bbbbbbbbbbbbbbbbbbbbbbbb);
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] =
(*cccccccccccccccc)[
dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];
llvm-svn: 185687
2013-07-05 17:14:35 +08:00
|
|
|
if (Previous.opensScope() &&
|
|
|
|
State.LowestLevelOnLine < State.StartOfLineLevel)
|
2013-05-27 19:50:16 +08:00
|
|
|
return false;
|
2013-04-22 15:59:53 +08:00
|
|
|
return !State.Stack.back().NoLineBreak;
|
2013-02-01 19:00:45 +08:00
|
|
|
}
|
2012-12-17 22:34:14 +08:00
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
/// \brief Returns \c true, if a line break after \p State is mandatory.
|
|
|
|
bool mustBreak(const LineState &State) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken &Current = *State.NextToken;
|
|
|
|
const FormatToken &Previous = *Current.Previous;
|
2013-05-08 22:12:04 +08:00
|
|
|
if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
|
2013-02-01 19:00:45 +08:00
|
|
|
return true;
|
2013-07-17 02:22:10 +08:00
|
|
|
if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) &&
|
|
|
|
State.Stack.back().BreakBeforeClosingBrace)
|
|
|
|
return true;
|
2013-05-08 22:12:04 +08:00
|
|
|
if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
|
2013-02-01 19:00:45 +08:00
|
|
|
return true;
|
2013-07-27 00:56:36 +08:00
|
|
|
if (Style.BreakConstructorInitializersBeforeComma) {
|
|
|
|
if (Previous.Type == TT_CtorInitializerComma)
|
|
|
|
return false;
|
|
|
|
if (Current.Type == TT_CtorInitializerComma)
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 22:12:04 +08:00
|
|
|
if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
|
2013-08-02 06:05:00 +08:00
|
|
|
(Current.Type == TT_ConditionalExpr &&
|
|
|
|
!(Current.is(tok::colon) && Previous.is(tok::question)))) &&
|
2013-02-08 16:22:00 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter &&
|
2013-05-08 22:12:04 +08:00
|
|
|
!Current.isTrailingComment() &&
|
|
|
|
!Current.isOneOf(tok::r_paren, tok::r_brace))
|
|
|
|
return true;
|
2013-07-17 23:38:19 +08:00
|
|
|
if (Style.AlwaysBreakBeforeMultilineStrings &&
|
|
|
|
State.Column > State.Stack.back().Indent &&
|
|
|
|
Current.is(tok::string_literal) && Previous.isNot(tok::lessless) &&
|
|
|
|
Previous.Type != TT_InlineASMColon &&
|
|
|
|
((Current.getNextNonComment() &&
|
|
|
|
Current.getNextNonComment()->is(tok::string_literal)) ||
|
|
|
|
(Current.TokenText.find("\\\n") != StringRef::npos)))
|
|
|
|
return true;
|
2013-05-08 22:12:04 +08:00
|
|
|
|
2013-07-27 00:56:36 +08:00
|
|
|
if (!Style.BreakBeforeBinaryOperators) {
|
|
|
|
// If we need to break somewhere inside the LHS of a binary expression, we
|
|
|
|
// should also break after the operator. Otherwise, the formatting would
|
|
|
|
// hide the operator precedence, e.g. in:
|
|
|
|
// if (aaaaaaaaaaaaaa ==
|
|
|
|
// bbbbbbbbbbbbbb && c) {..
|
|
|
|
// For comparisons, we only apply this rule, if the LHS is a binary
|
|
|
|
// expression itself as otherwise, the line breaks seem superfluous.
|
|
|
|
// We need special cases for ">>" which we have split into two ">" while
|
|
|
|
// lexing in order to make template parsing easier.
|
|
|
|
//
|
|
|
|
// FIXME: We'll need something similar for styles that break before binary
|
|
|
|
// operators.
|
|
|
|
bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
|
|
|
|
Previous.getPrecedence() == prec::Equality) &&
|
|
|
|
Previous.Previous && Previous.Previous->Type !=
|
|
|
|
TT_BinaryOperator; // For >>.
|
|
|
|
bool LHSIsBinaryExpr =
|
|
|
|
Previous.Previous && Previous.Previous->FakeRParens > 0;
|
|
|
|
if (Previous.Type == TT_BinaryOperator &&
|
|
|
|
(!IsComparison || LHSIsBinaryExpr) &&
|
|
|
|
Current.Type != TT_BinaryOperator && // For >>.
|
|
|
|
!Current.isTrailingComment() &&
|
|
|
|
!Previous.isOneOf(tok::lessless, tok::question) &&
|
|
|
|
Previous.getPrecedence() != prec::Assignment &&
|
|
|
|
State.Stack.back().BreakBeforeParameter)
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 22:12:04 +08:00
|
|
|
|
2013-07-12 23:14:05 +08:00
|
|
|
// Same as above, but for the first "<<" operator.
|
|
|
|
if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
|
|
|
|
State.Stack.back().FirstLessLess == 0)
|
|
|
|
return true;
|
|
|
|
|
2013-02-08 16:22:00 +08:00
|
|
|
// FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
|
|
|
|
// out whether it is the first parameter. Clean this up.
|
2013-05-08 22:12:04 +08:00
|
|
|
if (Current.Type == TT_ObjCSelectorName &&
|
|
|
|
Current.LongestObjCSelectorName == 0 &&
|
2013-02-08 16:22:00 +08:00
|
|
|
State.Stack.back().BreakBeforeParameter)
|
2013-02-05 18:07:47 +08:00
|
|
|
return true;
|
2013-05-08 22:12:04 +08:00
|
|
|
if ((Current.Type == TT_CtorInitializerColon ||
|
|
|
|
(Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
|
2013-03-14 21:45:21 +08:00
|
|
|
return true;
|
2013-05-08 22:12:04 +08:00
|
|
|
|
2013-07-09 15:43:55 +08:00
|
|
|
if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
|
|
|
|
Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter &&
|
|
|
|
State.ParenLevel == 0)
|
2013-05-15 17:35:08 +08:00
|
|
|
return true;
|
2013-02-01 19:00:45 +08:00
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-03-15 22:57:30 +08:00
|
|
|
// Returns the total number of columns required for the remaining tokens.
|
|
|
|
unsigned getRemainingLength(const LineState &State) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (State.NextToken && State.NextToken->Previous)
|
|
|
|
return Line.Last->TotalLength - State.NextToken->Previous->TotalLength;
|
2013-03-15 22:57:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle Style;
|
|
|
|
SourceManager &SourceMgr;
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &Line;
|
2013-01-11 02:45:26 +08:00
|
|
|
const unsigned FirstIndent;
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *RootToken;
|
2013-01-18 16:44:07 +08:00
|
|
|
WhitespaceManager &Whitespaces;
|
2013-02-13 18:54:19 +08:00
|
|
|
|
|
|
|
llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
|
|
|
|
QueueType Queue;
|
|
|
|
// Increasing count of \c StateNode items we have created. This is used
|
|
|
|
// to create a deterministic order independent of the container.
|
|
|
|
unsigned Count;
|
2013-06-05 22:09:10 +08:00
|
|
|
encoding::Encoding Encoding;
|
2013-07-10 22:02:49 +08:00
|
|
|
bool BinPackInconclusiveFunctions;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
class FormatTokenLexer {
|
2012-12-08 00:15:44 +08:00
|
|
|
public:
|
2013-06-05 22:09:10 +08:00
|
|
|
FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr,
|
|
|
|
encoding::Encoding Encoding)
|
2013-05-28 19:55:06 +08:00
|
|
|
: FormatTok(NULL), GreaterStashed(false), TrailingWhitespace(0), Lex(Lex),
|
2013-08-02 01:58:23 +08:00
|
|
|
SourceMgr(SourceMgr), IdentTable(getFormattingLangOpts()),
|
2013-06-05 22:09:10 +08:00
|
|
|
Encoding(Encoding) {
|
2012-12-08 00:15:44 +08:00
|
|
|
Lex.SetKeepWhitespaceMode(true);
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
ArrayRef<FormatToken *> lex() {
|
|
|
|
assert(Tokens.empty());
|
|
|
|
do {
|
|
|
|
Tokens.push_back(getNextToken());
|
|
|
|
} while (Tokens.back()->Tok.isNot(tok::eof));
|
|
|
|
return Tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierTable &getIdentTable() { return IdentTable; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
FormatToken *getNextToken() {
|
2012-12-08 00:15:44 +08:00
|
|
|
if (GreaterStashed) {
|
2013-05-28 21:42:28 +08:00
|
|
|
// Create a synthesized second '>' token.
|
|
|
|
Token Greater = FormatTok->Tok;
|
|
|
|
FormatTok = new (Allocator.Allocate()) FormatToken;
|
|
|
|
FormatTok->Tok = Greater;
|
2013-05-23 18:56:37 +08:00
|
|
|
SourceLocation GreaterLocation =
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->Tok.getLocation().getLocWithOffset(1);
|
|
|
|
FormatTok->WhitespaceRange =
|
|
|
|
SourceRange(GreaterLocation, GreaterLocation);
|
2013-06-08 01:45:07 +08:00
|
|
|
FormatTok->TokenText = ">";
|
2013-06-05 22:09:10 +08:00
|
|
|
FormatTok->CodePointCount = 1;
|
2012-12-08 00:15:44 +08:00
|
|
|
GreaterStashed = false;
|
|
|
|
return FormatTok;
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok = new (Allocator.Allocate()) FormatToken;
|
2013-07-17 04:28:33 +08:00
|
|
|
readRawToken(*FormatTok);
|
2013-05-27 23:23:34 +08:00
|
|
|
SourceLocation WhitespaceStart =
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->Tok.getLocation().getLocWithOffset(-TrailingWhitespace);
|
2013-05-23 18:56:37 +08:00
|
|
|
if (SourceMgr.getFileOffset(WhitespaceStart) == 0)
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->IsFirst = true;
|
2012-12-08 00:15:44 +08:00
|
|
|
|
|
|
|
// Consume and record whitespace until we find a significant token.
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned WhitespaceLength = TrailingWhitespace;
|
2013-05-28 19:55:06 +08:00
|
|
|
while (FormatTok->Tok.is(tok::unknown)) {
|
2013-07-17 04:28:33 +08:00
|
|
|
unsigned Newlines = FormatTok->TokenText.count('\n');
|
2013-03-04 21:43:19 +08:00
|
|
|
if (Newlines > 0)
|
2013-07-17 04:28:33 +08:00
|
|
|
FormatTok->LastNewlineOffset =
|
|
|
|
WhitespaceLength + FormatTok->TokenText.rfind('\n') + 1;
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->NewlinesBefore += Newlines;
|
2013-07-17 04:28:33 +08:00
|
|
|
unsigned EscapedNewlines = FormatTok->TokenText.count("\\\n");
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->HasUnescapedNewline |= EscapedNewlines != Newlines;
|
|
|
|
WhitespaceLength += FormatTok->Tok.getLength();
|
2012-12-08 00:15:44 +08:00
|
|
|
|
2013-07-17 04:28:33 +08:00
|
|
|
readRawToken(*FormatTok);
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
2013-01-07 15:56:50 +08:00
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
// In case the token starts with escaped newlines, we want to
|
|
|
|
// take them into account as whitespace - this pattern is quite frequent
|
|
|
|
// in macro definitions.
|
|
|
|
// FIXME: What do we want to do with other escaped spaces, and escaped
|
|
|
|
// spaces or newlines in the middle of tokens?
|
|
|
|
// FIXME: Add a more explicit test.
|
2013-07-17 04:28:33 +08:00
|
|
|
while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
|
|
|
|
FormatTok->TokenText[1] == '\n') {
|
2013-05-28 19:55:06 +08:00
|
|
|
// FIXME: ++FormatTok->NewlinesBefore is missing...
|
2013-05-23 18:56:37 +08:00
|
|
|
WhitespaceLength += 2;
|
2013-07-17 04:28:33 +08:00
|
|
|
FormatTok->TokenText = FormatTok->TokenText.substr(2);
|
2012-12-08 00:15:44 +08:00
|
|
|
}
|
|
|
|
|
2013-06-08 01:45:07 +08:00
|
|
|
TrailingWhitespace = 0;
|
|
|
|
if (FormatTok->Tok.is(tok::comment)) {
|
2013-07-17 04:28:33 +08:00
|
|
|
StringRef UntrimmedText = FormatTok->TokenText;
|
|
|
|
FormatTok->TokenText = FormatTok->TokenText.rtrim();
|
|
|
|
TrailingWhitespace = UntrimmedText.size() - FormatTok->TokenText.size();
|
2013-06-08 01:45:07 +08:00
|
|
|
} else if (FormatTok->Tok.is(tok::raw_identifier)) {
|
2013-07-17 04:28:33 +08:00
|
|
|
IdentifierInfo &Info = IdentTable.get(FormatTok->TokenText);
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->Tok.setIdentifierInfo(&Info);
|
|
|
|
FormatTok->Tok.setKind(Info.getTokenID());
|
2013-06-08 01:45:07 +08:00
|
|
|
} else if (FormatTok->Tok.is(tok::greatergreater)) {
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->Tok.setKind(tok::greater);
|
2013-07-17 04:28:33 +08:00
|
|
|
FormatTok->TokenText = FormatTok->TokenText.substr(0, 1);
|
2012-12-08 00:15:44 +08:00
|
|
|
GreaterStashed = true;
|
|
|
|
}
|
|
|
|
|
2013-06-08 01:45:07 +08:00
|
|
|
// Now FormatTok is the next non-whitespace token.
|
2013-07-17 04:28:33 +08:00
|
|
|
FormatTok->CodePointCount =
|
|
|
|
encoding::getCodePointCount(FormatTok->TokenText, Encoding);
|
2013-06-05 22:09:10 +08:00
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatTok->WhitespaceRange = SourceRange(
|
2013-05-23 18:56:37 +08:00
|
|
|
WhitespaceStart, WhitespaceStart.getLocWithOffset(WhitespaceLength));
|
2012-12-08 00:15:44 +08:00
|
|
|
return FormatTok;
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *FormatTok;
|
2012-12-08 00:15:44 +08:00
|
|
|
bool GreaterStashed;
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned TrailingWhitespace;
|
2012-12-08 00:15:44 +08:00
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
IdentifierTable IdentTable;
|
2013-06-05 22:09:10 +08:00
|
|
|
encoding::Encoding Encoding;
|
2013-05-28 19:55:06 +08:00
|
|
|
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
|
|
|
|
SmallVector<FormatToken *, 16> Tokens;
|
2012-12-08 00:15:44 +08:00
|
|
|
|
2013-07-17 04:28:33 +08:00
|
|
|
void readRawToken(FormatToken &Tok) {
|
|
|
|
Lex.LexFromRawLexer(Tok.Tok);
|
|
|
|
Tok.TokenText = StringRef(SourceMgr.getCharacterData(Tok.Tok.getLocation()),
|
|
|
|
Tok.Tok.getLength());
|
|
|
|
|
|
|
|
// For formatting, treat unterminated string literals like normal string
|
|
|
|
// literals.
|
|
|
|
if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
|
|
|
|
Tok.TokenText[0] == '"') {
|
|
|
|
Tok.Tok.setKind(tok::string_literal);
|
|
|
|
Tok.IsUnterminatedLiteral = true;
|
|
|
|
}
|
2012-12-08 00:15:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
class Formatter : public UnwrappedLineConsumer {
|
|
|
|
public:
|
2013-05-15 16:14:19 +08:00
|
|
|
Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr,
|
2012-12-04 02:12:45 +08:00
|
|
|
const std::vector<CharSourceRange> &Ranges)
|
2013-05-15 16:14:19 +08:00
|
|
|
: Style(Style), Lex(Lex), SourceMgr(SourceMgr),
|
2013-06-05 22:09:10 +08:00
|
|
|
Whitespaces(SourceMgr, Style), Ranges(Ranges),
|
|
|
|
Encoding(encoding::detectEncoding(Lex.getBuffer())) {
|
2013-07-15 22:33:14 +08:00
|
|
|
DEBUG(llvm::dbgs() << "File encoding: "
|
|
|
|
<< (Encoding == encoding::Encoding_UTF8 ? "UTF8"
|
|
|
|
: "unknown")
|
|
|
|
<< "\n");
|
2013-06-05 22:09:10 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-11 22:23:32 +08:00
|
|
|
virtual ~Formatter() {}
|
2012-12-05 05:05:31 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
tooling::Replacements format() {
|
2013-06-05 22:09:10 +08:00
|
|
|
FormatTokenLexer Tokens(Lex, SourceMgr, Encoding);
|
2013-05-28 19:55:06 +08:00
|
|
|
|
|
|
|
UnwrappedLineParser Parser(Style, Tokens.lex(), *this);
|
2013-04-12 22:13:36 +08:00
|
|
|
bool StructuralError = Parser.parse();
|
2013-06-05 22:09:10 +08:00
|
|
|
TokenAnnotator Annotator(Style, Tokens.getIdentTable().get("in"));
|
2013-02-06 22:22:40 +08:00
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
|
|
|
Annotator.annotate(AnnotatedLines[i]);
|
|
|
|
}
|
|
|
|
deriveLocalStyle();
|
2013-01-14 21:08:07 +08:00
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
2013-02-06 22:22:40 +08:00
|
|
|
Annotator.calculateFormattingInformation(AnnotatedLines[i]);
|
2013-04-10 01:46:55 +08:00
|
|
|
}
|
2013-03-13 23:53:12 +08:00
|
|
|
|
2013-04-10 01:46:55 +08:00
|
|
|
// Adapt level to the next line if this is a comment.
|
|
|
|
// FIXME: Can/should this be done in the UnwrappedLineParser?
|
2013-07-04 22:47:51 +08:00
|
|
|
const AnnotatedLine *NextNonCommentLine = NULL;
|
2013-04-10 01:46:55 +08:00
|
|
|
for (unsigned i = AnnotatedLines.size() - 1; i > 0; --i) {
|
2013-07-04 22:47:51 +08:00
|
|
|
if (NextNonCommentLine && AnnotatedLines[i].First->is(tok::comment) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
!AnnotatedLines[i].First->Next)
|
2013-07-04 22:47:51 +08:00
|
|
|
AnnotatedLines[i].Level = NextNonCommentLine->Level;
|
2013-04-10 01:46:55 +08:00
|
|
|
else
|
2013-07-08 22:34:09 +08:00
|
|
|
NextNonCommentLine = AnnotatedLines[i].First->isNot(tok::r_brace)
|
|
|
|
? &AnnotatedLines[i]
|
|
|
|
: NULL;
|
2013-01-14 21:08:07 +08:00
|
|
|
}
|
2013-04-10 01:46:55 +08:00
|
|
|
|
2013-02-09 01:38:27 +08:00
|
|
|
std::vector<int> IndentForLevel;
|
2013-02-14 17:58:41 +08:00
|
|
|
bool PreviousLineWasTouched = false;
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *PreviousLineLastToken = 0;
|
2013-05-10 21:00:49 +08:00
|
|
|
bool FormatPPDirective = false;
|
2013-01-14 21:08:07 +08:00
|
|
|
for (std::vector<AnnotatedLine>::iterator I = AnnotatedLines.begin(),
|
|
|
|
E = AnnotatedLines.end();
|
2013-01-11 03:49:59 +08:00
|
|
|
I != E; ++I) {
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &TheLine = *I;
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *FirstTok = TheLine.First;
|
|
|
|
int Offset = getIndentOffset(*TheLine.First);
|
2013-05-10 21:00:49 +08:00
|
|
|
|
|
|
|
// Check whether this line is part of a formatted preprocessor directive.
|
2013-05-28 21:42:28 +08:00
|
|
|
if (FirstTok->HasUnescapedNewline)
|
2013-05-10 21:00:49 +08:00
|
|
|
FormatPPDirective = false;
|
|
|
|
if (!FormatPPDirective && TheLine.InPPDirective &&
|
|
|
|
(touchesLine(TheLine) || touchesPPDirective(I + 1, E)))
|
|
|
|
FormatPPDirective = true;
|
|
|
|
|
2013-05-14 17:30:02 +08:00
|
|
|
// Determine indent and try to merge multiple unwrapped lines.
|
2013-02-09 01:38:27 +08:00
|
|
|
while (IndentForLevel.size() <= TheLine.Level)
|
|
|
|
IndentForLevel.push_back(-1);
|
|
|
|
IndentForLevel.resize(TheLine.Level + 1);
|
2013-05-14 17:30:02 +08:00
|
|
|
unsigned Indent = getIndent(IndentForLevel, TheLine.Level);
|
|
|
|
if (static_cast<int>(Indent) + Offset >= 0)
|
|
|
|
Indent += Offset;
|
|
|
|
tryFitMultipleLinesInOne(Indent, I, E);
|
|
|
|
|
2013-05-28 21:42:28 +08:00
|
|
|
bool WasMoved = PreviousLineWasTouched && FirstTok->NewlinesBefore == 0;
|
2013-05-29 22:47:47 +08:00
|
|
|
if (TheLine.First->is(tok::eof)) {
|
2013-03-02 02:11:39 +08:00
|
|
|
if (PreviousLineWasTouched) {
|
2013-05-28 21:42:28 +08:00
|
|
|
unsigned NewLines = std::min(FirstTok->NewlinesBefore, 1u);
|
2013-05-29 22:47:47 +08:00
|
|
|
Whitespaces.replaceWhitespace(*TheLine.First, NewLines, /*Indent*/ 0,
|
2013-05-22 20:51:29 +08:00
|
|
|
/*TargetColumn*/ 0);
|
2013-03-02 02:11:39 +08:00
|
|
|
}
|
|
|
|
} else if (TheLine.Type != LT_Invalid &&
|
2013-05-10 21:00:49 +08:00
|
|
|
(WasMoved || FormatPPDirective || touchesLine(TheLine))) {
|
2013-02-14 17:58:41 +08:00
|
|
|
unsigned LevelIndent = getIndent(IndentForLevel, TheLine.Level);
|
2013-05-28 21:42:28 +08:00
|
|
|
if (FirstTok->WhitespaceRange.isValid() &&
|
2013-04-12 22:13:36 +08:00
|
|
|
// Insert a break even if there is a structural error in case where
|
|
|
|
// we break apart a line consisting of multiple unwrapped lines.
|
2013-05-28 21:42:28 +08:00
|
|
|
(FirstTok->NewlinesBefore == 0 || !StructuralError)) {
|
2013-05-29 22:47:47 +08:00
|
|
|
formatFirstToken(*TheLine.First, PreviousLineLastToken, Indent,
|
2013-05-22 20:51:29 +08:00
|
|
|
TheLine.InPPDirective);
|
2013-04-12 22:13:36 +08:00
|
|
|
} else {
|
|
|
|
Indent = LevelIndent =
|
2013-05-28 21:42:28 +08:00
|
|
|
SourceMgr.getSpellingColumnNumber(FirstTok->Tok.getLocation()) -
|
|
|
|
1;
|
2013-02-09 01:38:27 +08:00
|
|
|
}
|
2013-01-14 21:08:07 +08:00
|
|
|
UnwrappedLineFormatter Formatter(Style, SourceMgr, TheLine, Indent,
|
2013-07-10 22:02:49 +08:00
|
|
|
TheLine.First, Whitespaces, Encoding,
|
|
|
|
BinPackInconclusiveFunctions);
|
2013-05-22 20:51:29 +08:00
|
|
|
Formatter.format(I + 1 != E ? &*(I + 1) : NULL);
|
2013-02-09 01:38:27 +08:00
|
|
|
IndentForLevel[TheLine.Level] = LevelIndent;
|
2013-02-14 17:58:41 +08:00
|
|
|
PreviousLineWasTouched = true;
|
2013-01-11 03:49:59 +08:00
|
|
|
} else {
|
2013-05-22 20:51:29 +08:00
|
|
|
// Format the first token if necessary, and notify the WhitespaceManager
|
|
|
|
// about the unchanged whitespace.
|
2013-05-29 22:47:47 +08:00
|
|
|
for (const FormatToken *Tok = TheLine.First; Tok != NULL;
|
|
|
|
Tok = Tok->Next) {
|
|
|
|
if (Tok == TheLine.First &&
|
|
|
|
(Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
|
|
|
|
unsigned LevelIndent =
|
|
|
|
SourceMgr.getSpellingColumnNumber(Tok->Tok.getLocation()) - 1;
|
2013-05-22 20:51:29 +08:00
|
|
|
// Remove trailing whitespace of the previous line if it was
|
|
|
|
// touched.
|
|
|
|
if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine)) {
|
|
|
|
formatFirstToken(*Tok, PreviousLineLastToken, LevelIndent,
|
|
|
|
TheLine.InPPDirective);
|
|
|
|
} else {
|
2013-05-29 22:47:47 +08:00
|
|
|
Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (static_cast<int>(LevelIndent) - Offset >= 0)
|
|
|
|
LevelIndent -= Offset;
|
|
|
|
if (Tok->isNot(tok::comment))
|
|
|
|
IndentForLevel[TheLine.Level] = LevelIndent;
|
|
|
|
} else {
|
2013-05-29 22:47:47 +08:00
|
|
|
Whitespaces.addUntouchableToken(*Tok, TheLine.InPPDirective);
|
2013-05-22 20:51:29 +08:00
|
|
|
}
|
2013-02-13 00:51:23 +08:00
|
|
|
}
|
2013-02-14 17:58:41 +08:00
|
|
|
// If we did not reformat this unwrapped line, the column at the end of
|
|
|
|
// the last token is unchanged - thus, we can calculate the end of the
|
|
|
|
// last token.
|
|
|
|
PreviousLineWasTouched = false;
|
2013-01-11 03:49:59 +08:00
|
|
|
}
|
2013-03-28 01:08:02 +08:00
|
|
|
PreviousLineLastToken = I->Last;
|
2013-01-11 03:49:59 +08:00
|
|
|
}
|
2013-01-18 16:44:07 +08:00
|
|
|
return Whitespaces.generateReplacements();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-03-13 22:41:29 +08:00
|
|
|
void deriveLocalStyle() {
|
|
|
|
unsigned CountBoundToVariable = 0;
|
|
|
|
unsigned CountBoundToType = 0;
|
|
|
|
bool HasCpp03IncompatibleFormat = false;
|
2013-07-10 22:02:49 +08:00
|
|
|
bool HasBinPackedFunction = false;
|
|
|
|
bool HasOnePerLineFunction = false;
|
2013-03-13 22:41:29 +08:00
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (!AnnotatedLines[i].First->Next)
|
2013-03-13 22:41:29 +08:00
|
|
|
continue;
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Tok = AnnotatedLines[i].First->Next;
|
|
|
|
while (Tok->Next) {
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Tok->Type == TT_PointerOrReference) {
|
2013-05-29 22:47:47 +08:00
|
|
|
bool SpacesBefore =
|
|
|
|
Tok->WhitespaceRange.getBegin() != Tok->WhitespaceRange.getEnd();
|
|
|
|
bool SpacesAfter = Tok->Next->WhitespaceRange.getBegin() !=
|
|
|
|
Tok->Next->WhitespaceRange.getEnd();
|
2013-03-13 22:41:29 +08:00
|
|
|
if (SpacesBefore && !SpacesAfter)
|
|
|
|
++CountBoundToVariable;
|
|
|
|
else if (!SpacesBefore && SpacesAfter)
|
|
|
|
++CountBoundToType;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok->Type == TT_TemplateCloser &&
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok->Previous->Type == TT_TemplateCloser &&
|
|
|
|
Tok->WhitespaceRange.getBegin() == Tok->WhitespaceRange.getEnd())
|
2013-03-13 22:41:29 +08:00
|
|
|
HasCpp03IncompatibleFormat = true;
|
2013-07-10 22:02:49 +08:00
|
|
|
|
|
|
|
if (Tok->PackingKind == PPK_BinPacked)
|
|
|
|
HasBinPackedFunction = true;
|
|
|
|
if (Tok->PackingKind == PPK_OnePerLine)
|
|
|
|
HasOnePerLineFunction = true;
|
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok = Tok->Next;
|
2013-03-13 22:41:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Style.DerivePointerBinding) {
|
|
|
|
if (CountBoundToType > CountBoundToVariable)
|
|
|
|
Style.PointerBindsToType = true;
|
|
|
|
else if (CountBoundToType < CountBoundToVariable)
|
|
|
|
Style.PointerBindsToType = false;
|
|
|
|
}
|
|
|
|
if (Style.Standard == FormatStyle::LS_Auto) {
|
|
|
|
Style.Standard = HasCpp03IncompatibleFormat ? FormatStyle::LS_Cpp11
|
|
|
|
: FormatStyle::LS_Cpp03;
|
|
|
|
}
|
2013-07-10 22:02:49 +08:00
|
|
|
BinPackInconclusiveFunctions =
|
|
|
|
HasBinPackedFunction || !HasOnePerLineFunction;
|
2013-03-13 22:41:29 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 01:38:27 +08:00
|
|
|
/// \brief Get the indent of \p Level from \p IndentForLevel.
|
|
|
|
///
|
|
|
|
/// \p IndentForLevel must contain the indent for the level \c l
|
|
|
|
/// at \p IndentForLevel[l], or a value < 0 if the indent for
|
|
|
|
/// that level is unknown.
|
2013-02-14 22:26:07 +08:00
|
|
|
unsigned getIndent(const std::vector<int> IndentForLevel, unsigned Level) {
|
2013-02-09 01:38:27 +08:00
|
|
|
if (IndentForLevel[Level] != -1)
|
|
|
|
return IndentForLevel[Level];
|
2013-02-09 03:53:32 +08:00
|
|
|
if (Level == 0)
|
|
|
|
return 0;
|
2013-05-13 16:42:42 +08:00
|
|
|
return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
|
2013-02-09 01:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the offset of the line relatively to the level.
|
|
|
|
///
|
|
|
|
/// For example, 'public:' labels in classes are offset by 1 or 2
|
|
|
|
/// characters to the left from their level.
|
2013-05-29 22:47:47 +08:00
|
|
|
int getIndentOffset(const FormatToken &RootToken) {
|
2013-03-28 01:08:02 +08:00
|
|
|
if (RootToken.isAccessSpecifier(false) || RootToken.isObjCAccessSpecifier())
|
2013-02-09 01:38:27 +08:00
|
|
|
return Style.AccessModifierOffset;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-12 01:54:10 +08:00
|
|
|
/// \brief Tries to merge lines into one.
|
|
|
|
///
|
|
|
|
/// This will change \c Line and \c AnnotatedLine to contain the merged line,
|
|
|
|
/// if possible; note that \c I will be incremented when lines are merged.
|
2013-01-16 18:41:46 +08:00
|
|
|
void tryFitMultipleLinesInOne(unsigned Indent,
|
2013-01-14 21:08:07 +08:00
|
|
|
std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E) {
|
2013-01-16 18:41:46 +08:00
|
|
|
// We can never merge stuff if there are trailing line comments.
|
|
|
|
if (I->Last->Type == TT_LineComment)
|
|
|
|
return;
|
|
|
|
|
2013-07-24 21:10:59 +08:00
|
|
|
if (Indent > Style.ColumnLimit)
|
|
|
|
return;
|
|
|
|
|
2013-02-28 19:05:57 +08:00
|
|
|
unsigned Limit = Style.ColumnLimit - Indent;
|
2013-02-22 05:33:55 +08:00
|
|
|
// If we already exceed the column limit, we set 'Limit' to 0. The different
|
|
|
|
// tryMerge..() functions can then decide whether to still do merging.
|
|
|
|
Limit = I->Last->TotalLength > Limit ? 0 : Limit - I->Last->TotalLength;
|
2013-01-16 15:02:34 +08:00
|
|
|
|
2013-01-21 22:18:28 +08:00
|
|
|
if (I + 1 == E || (I + 1)->Type == LT_Invalid)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-14 22:14:23 +08:00
|
|
|
|
2013-05-15 22:09:55 +08:00
|
|
|
if (I->Last->is(tok::l_brace)) {
|
2013-01-14 22:14:23 +08:00
|
|
|
tryMergeSimpleBlock(I, E, Limit);
|
2013-05-16 20:12:21 +08:00
|
|
|
} else if (Style.AllowShortIfStatementsOnASingleLine &&
|
2013-05-29 22:47:47 +08:00
|
|
|
I->First->is(tok::kw_if)) {
|
2013-05-16 20:12:21 +08:00
|
|
|
tryMergeSimpleControlStatement(I, E, Limit);
|
|
|
|
} else if (Style.AllowShortLoopsOnASingleLine &&
|
2013-05-29 22:47:47 +08:00
|
|
|
I->First->isOneOf(tok::kw_for, tok::kw_while)) {
|
2013-05-16 20:12:21 +08:00
|
|
|
tryMergeSimpleControlStatement(I, E, Limit);
|
2013-05-29 22:47:47 +08:00
|
|
|
} else if (I->InPPDirective &&
|
|
|
|
(I->First->HasUnescapedNewline || I->First->IsFirst)) {
|
2013-01-14 23:40:57 +08:00
|
|
|
tryMergeSimplePPDirective(I, E, Limit);
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-01-14 23:40:57 +08:00
|
|
|
void tryMergeSimplePPDirective(std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit) {
|
2013-02-22 05:33:55 +08:00
|
|
|
if (Limit == 0)
|
|
|
|
return;
|
2013-01-14 23:40:57 +08:00
|
|
|
AnnotatedLine &Line = *I;
|
2013-05-29 22:47:47 +08:00
|
|
|
if (!(I + 1)->InPPDirective || (I + 1)->First->HasUnescapedNewline)
|
2013-01-14 23:52:06 +08:00
|
|
|
return;
|
2013-01-14 23:40:57 +08:00
|
|
|
if (I + 2 != E && (I + 2)->InPPDirective &&
|
2013-05-29 22:47:47 +08:00
|
|
|
!(I + 2)->First->HasUnescapedNewline)
|
2013-01-14 23:40:57 +08:00
|
|
|
return;
|
2013-01-22 00:42:44 +08:00
|
|
|
if (1 + (I + 1)->Last->TotalLength > Limit)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-01-14 23:40:57 +08:00
|
|
|
join(Line, *(++I));
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:12:21 +08:00
|
|
|
void tryMergeSimpleControlStatement(std::vector<AnnotatedLine>::iterator &I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit) {
|
2013-02-22 05:33:55 +08:00
|
|
|
if (Limit == 0)
|
|
|
|
return;
|
2013-01-18 22:46:43 +08:00
|
|
|
if ((I + 1)->InPPDirective != I->InPPDirective ||
|
2013-05-29 22:47:47 +08:00
|
|
|
((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
|
2013-01-18 22:46:43 +08:00
|
|
|
return;
|
2013-01-14 22:14:23 +08:00
|
|
|
AnnotatedLine &Line = *I;
|
2013-01-16 15:02:34 +08:00
|
|
|
if (Line.Last->isNot(tok::r_paren))
|
|
|
|
return;
|
2013-01-22 00:42:44 +08:00
|
|
|
if (1 + (I + 1)->Last->TotalLength > Limit)
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-05-29 22:47:47 +08:00
|
|
|
if ((I + 1)->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
|
|
|
|
tok::kw_while) ||
|
|
|
|
(I + 1)->First->Type == TT_LineComment)
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
|
|
|
// Only inline simple if's (no nested if or else).
|
2013-05-29 22:47:47 +08:00
|
|
|
if (I + 2 != E && Line.First->is(tok::kw_if) &&
|
|
|
|
(I + 2)->First->is(tok::kw_else))
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
|
|
|
join(Line, *(++I));
|
|
|
|
}
|
|
|
|
|
|
|
|
void tryMergeSimpleBlock(std::vector<AnnotatedLine>::iterator &I,
|
2013-01-29 19:27:30 +08:00
|
|
|
std::vector<AnnotatedLine>::iterator E,
|
|
|
|
unsigned Limit) {
|
2013-05-15 22:09:55 +08:00
|
|
|
// No merging if the brace already is on the next line.
|
|
|
|
if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
|
|
|
|
return;
|
|
|
|
|
2013-01-12 01:54:10 +08:00
|
|
|
// First, check that the current line allows merging. This is the case if
|
|
|
|
// we're not in a control flow statement and the last token is an opening
|
|
|
|
// brace.
|
2013-01-14 22:14:23 +08:00
|
|
|
AnnotatedLine &Line = *I;
|
2013-05-29 22:47:47 +08:00
|
|
|
if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace,
|
|
|
|
tok::kw_else, tok::kw_try, tok::kw_catch,
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
tok::kw_for,
|
2013-05-29 22:47:47 +08:00
|
|
|
// This gets rid of all ObjC @ keywords and methods.
|
|
|
|
tok::at, tok::minus, tok::plus))
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
FormatToken *Tok = (I + 1)->First;
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
|
2013-07-04 22:47:51 +08:00
|
|
|
(Tok->getNextNonComment() == NULL ||
|
|
|
|
Tok->getNextNonComment()->is(tok::semi))) {
|
2013-02-22 05:33:55 +08:00
|
|
|
// We merge empty blocks even if the line exceeds the column limit.
|
2013-02-11 20:36:37 +08:00
|
|
|
Tok->SpacesRequiredBefore = 0;
|
2013-02-22 05:33:55 +08:00
|
|
|
Tok->CanBreakBefore = true;
|
2013-01-22 00:42:44 +08:00
|
|
|
join(Line, *(I + 1));
|
|
|
|
I += 1;
|
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
2013-05-31 22:56:20 +08:00
|
|
|
} else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) {
|
2013-01-22 00:42:44 +08:00
|
|
|
// Check that we still have three lines and they fit into the limit.
|
|
|
|
if (I + 2 == E || (I + 2)->Type == LT_Invalid ||
|
|
|
|
!nextTwoLinesFitInto(I, Limit))
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-01-12 01:54:10 +08:00
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
// Second, check that the next line does not contain any braces - if it
|
|
|
|
// does, readability declines when putting it into a single line.
|
|
|
|
if ((I + 1)->Last->Type == TT_LineComment || Tok->MustBreakBefore)
|
|
|
|
return;
|
|
|
|
do {
|
2013-03-13 22:41:29 +08:00
|
|
|
if (Tok->isOneOf(tok::l_brace, tok::r_brace))
|
2013-01-22 00:42:44 +08:00
|
|
|
return;
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok = Tok->Next;
|
2013-01-22 00:42:44 +08:00
|
|
|
} while (Tok != NULL);
|
|
|
|
|
|
|
|
// Last, check that the third line contains a single closing brace.
|
2013-05-29 22:47:47 +08:00
|
|
|
Tok = (I + 2)->First;
|
2013-07-04 22:47:51 +08:00
|
|
|
if (Tok->getNextNonComment() != NULL || Tok->isNot(tok::r_brace) ||
|
2013-01-22 00:42:44 +08:00
|
|
|
Tok->MustBreakBefore)
|
|
|
|
return;
|
2013-01-14 21:08:07 +08:00
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
join(Line, *(I + 1));
|
|
|
|
join(Line, *(I + 2));
|
|
|
|
I += 2;
|
2013-01-12 01:54:10 +08:00
|
|
|
}
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool nextTwoLinesFitInto(std::vector<AnnotatedLine>::iterator I,
|
|
|
|
unsigned Limit) {
|
2013-01-22 00:42:44 +08:00
|
|
|
return 1 + (I + 1)->Last->TotalLength + 1 + (I + 2)->Last->TotalLength <=
|
|
|
|
Limit;
|
2013-01-12 01:54:10 +08:00
|
|
|
}
|
|
|
|
|
2013-01-14 21:08:07 +08:00
|
|
|
void join(AnnotatedLine &A, const AnnotatedLine &B) {
|
2013-05-29 22:47:47 +08:00
|
|
|
assert(!A.Last->Next);
|
|
|
|
assert(!B.First->Previous);
|
|
|
|
A.Last->Next = B.First;
|
|
|
|
B.First->Previous = A.Last;
|
|
|
|
unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
|
|
|
|
for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
|
|
|
|
Tok->TotalLength += LengthA;
|
|
|
|
A.Last = Tok;
|
2013-01-14 21:08:07 +08:00
|
|
|
}
|
2012-12-05 01:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-03-13 15:49:51 +08:00
|
|
|
bool touchesRanges(const CharSourceRange &Range) {
|
2012-12-04 02:12:45 +08:00
|
|
|
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
|
2013-03-08 04:50:00 +08:00
|
|
|
if (!SourceMgr.isBeforeInTranslationUnit(Range.getEnd(),
|
2013-01-11 03:49:59 +08:00
|
|
|
Ranges[i].getBegin()) &&
|
|
|
|
!SourceMgr.isBeforeInTranslationUnit(Ranges[i].getEnd(),
|
2013-03-08 04:50:00 +08:00
|
|
|
Range.getBegin()))
|
2013-01-11 03:49:59 +08:00
|
|
|
return true;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-11 03:49:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 04:50:00 +08:00
|
|
|
bool touchesLine(const AnnotatedLine &TheLine) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *First = TheLine.First;
|
|
|
|
const FormatToken *Last = TheLine.Last;
|
2013-05-14 18:31:09 +08:00
|
|
|
CharSourceRange LineRange = CharSourceRange::getCharRange(
|
2013-05-23 18:56:37 +08:00
|
|
|
First->WhitespaceRange.getBegin().getLocWithOffset(
|
|
|
|
First->LastNewlineOffset),
|
2013-06-08 01:45:07 +08:00
|
|
|
Last->Tok.getLocation().getLocWithOffset(Last->TokenText.size() - 1));
|
2013-03-08 04:50:00 +08:00
|
|
|
return touchesRanges(LineRange);
|
|
|
|
}
|
|
|
|
|
2013-05-10 21:00:49 +08:00
|
|
|
bool touchesPPDirective(std::vector<AnnotatedLine>::iterator I,
|
|
|
|
std::vector<AnnotatedLine>::iterator E) {
|
|
|
|
for (; I != E; ++I) {
|
2013-05-29 22:47:47 +08:00
|
|
|
if (I->First->HasUnescapedNewline)
|
2013-05-10 21:00:49 +08:00
|
|
|
return false;
|
|
|
|
if (touchesLine(*I))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 04:50:00 +08:00
|
|
|
bool touchesEmptyLineBefore(const AnnotatedLine &TheLine) {
|
2013-05-29 22:47:47 +08:00
|
|
|
const FormatToken *First = TheLine.First;
|
2013-03-08 04:50:00 +08:00
|
|
|
CharSourceRange LineRange = CharSourceRange::getCharRange(
|
2013-05-23 18:56:37 +08:00
|
|
|
First->WhitespaceRange.getBegin(),
|
|
|
|
First->WhitespaceRange.getBegin().getLocWithOffset(
|
|
|
|
First->LastNewlineOffset));
|
2013-03-08 04:50:00 +08:00
|
|
|
return touchesRanges(LineRange);
|
|
|
|
}
|
|
|
|
|
2013-01-11 03:49:59 +08:00
|
|
|
virtual void consumeUnwrappedLine(const UnwrappedLine &TheLine) {
|
2013-01-16 17:10:19 +08:00
|
|
|
AnnotatedLines.push_back(AnnotatedLine(TheLine));
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-11 02:45:26 +08:00
|
|
|
/// \brief Add a new line and the required indent before the first Token
|
|
|
|
/// of the \c UnwrappedLine if there was no structural parsing error.
|
|
|
|
/// Returns the indent level of the \c UnwrappedLine.
|
2013-05-29 22:47:47 +08:00
|
|
|
void formatFirstToken(const FormatToken &RootToken,
|
|
|
|
const FormatToken *PreviousToken, unsigned Indent,
|
2013-05-22 20:51:29 +08:00
|
|
|
bool InPPDirective) {
|
2013-01-29 19:27:30 +08:00
|
|
|
unsigned Newlines =
|
2013-05-29 22:47:47 +08:00
|
|
|
std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
|
2013-06-04 00:16:41 +08:00
|
|
|
// Remove empty lines before "}" where applicable.
|
|
|
|
if (RootToken.is(tok::r_brace) &&
|
|
|
|
(!RootToken.Next ||
|
|
|
|
(RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
|
|
|
|
Newlines = std::min(Newlines, 1u);
|
2013-05-29 22:47:47 +08:00
|
|
|
if (Newlines == 0 && !RootToken.IsFirst)
|
2013-01-11 02:45:26 +08:00
|
|
|
Newlines = 1;
|
|
|
|
|
2013-05-22 20:51:29 +08:00
|
|
|
// Insert extra new line before access specifiers.
|
|
|
|
if (PreviousToken && PreviousToken->isOneOf(tok::semi, tok::r_brace) &&
|
2013-05-29 22:47:47 +08:00
|
|
|
RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
|
2013-05-22 20:51:29 +08:00
|
|
|
++Newlines;
|
2013-03-28 01:08:02 +08:00
|
|
|
|
2013-05-29 22:47:47 +08:00
|
|
|
Whitespaces.replaceWhitespace(
|
|
|
|
RootToken, Newlines, Indent, Indent,
|
|
|
|
InPPDirective && !RootToken.HasUnescapedNewline);
|
2013-01-11 02:45:26 +08:00
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle Style;
|
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
2013-01-18 16:44:07 +08:00
|
|
|
WhitespaceManager Whitespaces;
|
2012-12-04 02:12:45 +08:00
|
|
|
std::vector<CharSourceRange> Ranges;
|
2013-01-14 21:08:07 +08:00
|
|
|
std::vector<AnnotatedLine> AnnotatedLines;
|
2013-06-05 22:09:10 +08:00
|
|
|
|
|
|
|
encoding::Encoding Encoding;
|
2013-07-10 22:02:49 +08:00
|
|
|
bool BinPackInconclusiveFunctions;
|
2012-12-04 02:12:45 +08:00
|
|
|
};
|
|
|
|
|
2013-07-01 06:29:28 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-04-15 22:28:00 +08:00
|
|
|
tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
|
|
|
|
SourceManager &SourceMgr,
|
2013-05-15 16:14:19 +08:00
|
|
|
std::vector<CharSourceRange> Ranges) {
|
|
|
|
Formatter formatter(Style, Lex, SourceMgr, Ranges);
|
2012-12-04 02:12:45 +08:00
|
|
|
return formatter.format();
|
|
|
|
}
|
|
|
|
|
2013-05-16 18:40:07 +08:00
|
|
|
tooling::Replacements reformat(const FormatStyle &Style, StringRef Code,
|
|
|
|
std::vector<tooling::Range> Ranges,
|
|
|
|
StringRef FileName) {
|
|
|
|
FileManager Files((FileSystemOptions()));
|
|
|
|
DiagnosticsEngine Diagnostics(
|
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
|
|
|
|
new DiagnosticOptions);
|
|
|
|
SourceManager SourceMgr(Diagnostics, Files);
|
|
|
|
llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, FileName);
|
|
|
|
const clang::FileEntry *Entry =
|
|
|
|
Files.getVirtualFile(FileName, Buf->getBufferSize(), 0);
|
|
|
|
SourceMgr.overrideFileContents(Entry, Buf);
|
|
|
|
FileID ID =
|
|
|
|
SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User);
|
2013-06-28 20:51:24 +08:00
|
|
|
Lexer Lex(ID, SourceMgr.getBuffer(ID), SourceMgr,
|
|
|
|
getFormattingLangOpts(Style.Standard));
|
2013-05-16 18:40:07 +08:00
|
|
|
SourceLocation StartOfFile = SourceMgr.getLocForStartOfFile(ID);
|
|
|
|
std::vector<CharSourceRange> CharRanges;
|
|
|
|
for (unsigned i = 0, e = Ranges.size(); i != e; ++i) {
|
|
|
|
SourceLocation Start = StartOfFile.getLocWithOffset(Ranges[i].getOffset());
|
|
|
|
SourceLocation End = Start.getLocWithOffset(Ranges[i].getLength());
|
|
|
|
CharRanges.push_back(CharSourceRange::getCharRange(Start, End));
|
|
|
|
}
|
|
|
|
return reformat(Style, Lex, SourceMgr, CharRanges);
|
|
|
|
}
|
|
|
|
|
2013-06-28 20:51:24 +08:00
|
|
|
LangOptions getFormattingLangOpts(FormatStyle::LanguageStandard Standard) {
|
2013-01-10 21:08:12 +08:00
|
|
|
LangOptions LangOpts;
|
|
|
|
LangOpts.CPlusPlus = 1;
|
2013-06-28 20:51:24 +08:00
|
|
|
LangOpts.CPlusPlus11 = Standard == FormatStyle::LS_Cpp03 ? 0 : 1;
|
2013-03-22 18:01:29 +08:00
|
|
|
LangOpts.LineComment = 1;
|
2013-01-10 21:08:12 +08:00
|
|
|
LangOpts.Bool = 1;
|
|
|
|
LangOpts.ObjC1 = 1;
|
|
|
|
LangOpts.ObjC2 = 1;
|
|
|
|
return LangOpts;
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
} // namespace format
|
|
|
|
} // namespace clang
|