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-08-16 19:20:30 +08:00
|
|
|
#include "ContinuationIndenter.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"
|
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) {
|
2013-09-04 22:09:13 +08:00
|
|
|
IO.enumCase(Value, "Cpp03", clang::format::FormatStyle::LS_Cpp03);
|
2013-05-13 20:51:40 +08:00
|
|
|
IO.enumCase(Value, "C++03", clang::format::FormatStyle::LS_Cpp03);
|
2013-09-04 22:09:13 +08:00
|
|
|
IO.enumCase(Value, "Cpp11", clang::format::FormatStyle::LS_Cpp11);
|
2013-05-13 20:51:40 +08:00
|
|
|
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-08-03 05:31:59 +08:00
|
|
|
IO.enumCase(Value, "Allman", clang::format::FormatStyle::BS_Allman);
|
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()) {
|
2013-09-03 00:39:23 +08:00
|
|
|
StringRef StylesArray[] = { "LLVM", "Google", "Chromium",
|
|
|
|
"Mozilla", "WebKit" };
|
2013-05-10 19:56:10 +08:00
|
|
|
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);
|
2013-08-13 18:58:30 +08:00
|
|
|
IO.mapOptional("ConstructorInitializerIndentWidth",
|
|
|
|
Style.ConstructorInitializerIndentWidth);
|
2013-05-07 23:32:14 +08:00
|
|
|
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-09-05 22:08:34 +08:00
|
|
|
IO.mapOptional("TabWidth", Style.TabWidth);
|
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-08-20 20:36:34 +08:00
|
|
|
IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
|
2013-08-21 16:39:01 +08:00
|
|
|
IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
|
2013-08-20 20:36:34 +08:00
|
|
|
IO.mapOptional("SpacesInCStyleCastParentheses",
|
|
|
|
Style.SpacesInCStyleCastParentheses);
|
|
|
|
IO.mapOptional("SpaceAfterControlStatementKeyword",
|
|
|
|
Style.SpaceAfterControlStatementKeyword);
|
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) {
|
2013-08-28 18:03:58 +08:00
|
|
|
Style.PenaltyBreakComment = 60;
|
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-08-13 18:58:30 +08:00
|
|
|
LLVMStyle.ConstructorInitializerIndentWidth = 4;
|
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-09-05 22:08:34 +08:00
|
|
|
LLVMStyle.TabWidth = 8;
|
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-08-20 20:36:34 +08:00
|
|
|
LLVMStyle.SpacesInParentheses = false;
|
|
|
|
LLVMStyle.SpaceInEmptyParentheses = false;
|
|
|
|
LLVMStyle.SpacesInCStyleCastParentheses = false;
|
|
|
|
LLVMStyle.SpaceAfterControlStatementKeyword = true;
|
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-08-13 18:58:30 +08:00
|
|
|
GoogleStyle.ConstructorInitializerIndentWidth = 4;
|
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-09-05 22:08:34 +08:00
|
|
|
GoogleStyle.TabWidth = 8;
|
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-08-20 20:36:34 +08:00
|
|
|
GoogleStyle.SpacesInParentheses = false;
|
|
|
|
GoogleStyle.SpaceInEmptyParentheses = false;
|
|
|
|
GoogleStyle.SpacesInCStyleCastParentheses = false;
|
|
|
|
GoogleStyle.SpaceAfterControlStatementKeyword = true;
|
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-07-01 06:29:28 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
class NoColumnLimitFormatter {
|
2012-12-04 02:12:45 +08:00
|
|
|
public:
|
2013-08-21 16:39:01 +08:00
|
|
|
NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
/// \brief Formats the line starting at \p State, simply keeping all of the
|
|
|
|
/// input's line breaking decisions.
|
2013-09-05 17:29:45 +08:00
|
|
|
void format(unsigned FirstIndent, const AnnotatedLine *Line) {
|
2013-09-06 15:54:20 +08:00
|
|
|
LineState State =
|
|
|
|
Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
while (State.NextToken != NULL) {
|
|
|
|
bool Newline =
|
|
|
|
Indenter->mustBreak(State) ||
|
|
|
|
(Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
|
|
|
|
Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
|
2013-07-24 21:10:59 +08:00
|
|
|
}
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
2013-08-21 16:39:01 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
private:
|
|
|
|
ContinuationIndenter *Indenter;
|
|
|
|
};
|
2013-07-24 21:10:59 +08:00
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
class UnwrappedLineFormatter {
|
|
|
|
public:
|
|
|
|
UnwrappedLineFormatter(ContinuationIndenter *Indenter,
|
2013-09-05 17:29:45 +08:00
|
|
|
WhitespaceManager *Whitespaces,
|
2013-08-16 19:20:30 +08:00
|
|
|
const FormatStyle &Style, const AnnotatedLine &Line)
|
2013-09-05 17:29:45 +08:00
|
|
|
: Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), Line(Line),
|
|
|
|
Count(0) {}
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
/// \brief Formats an \c UnwrappedLine and returns the penalty.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, directly applies the changes.
|
|
|
|
unsigned format(unsigned FirstIndent, bool DryRun = false) {
|
2013-09-06 15:54:20 +08:00
|
|
|
LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
|
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-09-05 17:29:45 +08:00
|
|
|
return analyzeSolutionSpace(State, DryRun);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
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
|
2013-09-05 17:29:45 +08:00
|
|
|
/// to a state where all tokens are placed. Returns the penalty.
|
|
|
|
///
|
|
|
|
/// If \p DryRun is \c false, directly applies the changes.
|
|
|
|
unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun = false) {
|
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
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned Penalty = 0;
|
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// While not empty, take first element and follow edges.
|
|
|
|
while (!Queue.empty()) {
|
2013-09-05 17:29:45 +08:00
|
|
|
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-09-05 17:29:45 +08:00
|
|
|
return 0;
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-02-01 19:00:45 +08:00
|
|
|
// Reconstruct the solution.
|
2013-09-05 17:29:45 +08:00
|
|
|
if (!DryRun)
|
|
|
|
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-09-05 17:29:45 +08:00
|
|
|
|
|
|
|
return Penalty;
|
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) {
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned Penalty = 0;
|
|
|
|
formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
|
|
|
|
Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
|
|
|
|
|
2013-05-29 23:10:11 +08:00
|
|
|
DEBUG({
|
|
|
|
if ((*I)->NewLine) {
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
llvm::dbgs() << "Penalty for placing "
|
2013-05-29 23:10:11 +08:00
|
|
|
<< (*I)->Previous->State.NextToken->Tok.getName() << ": "
|
clang-format: Add column layout formatting for braced lists
With this patch, braced lists (with more than 3 elements are formatted in a
column layout if possible). E.g.:
static const uint16_t CallerSavedRegs64Bit[] = {
X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,
X86::R8, X86::R9, X86::R10, X86::R11, 0
};
Required other changes:
- FormatTokens can now have a special role that contains extra data and can do
special formattings. A comma separated list is currently the only
implementation.
- Move penalty calculation entirely into ContinuationIndenter (there was a last
piece still in UnwrappedLineFormatter).
Review: http://llvm-reviews.chandlerc.com/D1457
llvm-svn: 189018
2013-08-22 23:00:41 +08:00
|
|
|
<< Penalty << "\n";
|
2013-05-29 23:10:11 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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-08-16 19:20:30 +08:00
|
|
|
if (NewLine && !Indenter->canBreak(PreviousNode->State))
|
2013-02-01 19:00:45 +08:00
|
|
|
return;
|
2013-08-16 19:20:30 +08:00
|
|
|
if (!NewLine && Indenter->mustBreak(PreviousNode->State))
|
2013-02-01 19:00:45 +08:00
|
|
|
return;
|
2013-02-13 18:46:36 +08:00
|
|
|
|
|
|
|
StateNode *Node = new (Allocator.Allocate())
|
|
|
|
StateNode(PreviousNode->State, NewLine, PreviousNode);
|
2013-09-05 17:29:45 +08:00
|
|
|
if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
|
|
|
|
return;
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
|
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-09-05 18:48:50 +08:00
|
|
|
/// \brief If the \p State's next token is an r_brace closing a nested block,
|
|
|
|
/// format the nested block before it.
|
2013-09-05 17:29:45 +08:00
|
|
|
///
|
|
|
|
/// Returns \c true if all children could be placed successfully and adapts
|
|
|
|
/// \p Penalty as well as \p State. If \p DryRun is false, also directly
|
|
|
|
/// creates changes using \c Whitespaces.
|
|
|
|
///
|
|
|
|
/// The crucial idea here is that children always get formatted upon
|
|
|
|
/// encountering the closing brace right after the nested block. Now, if we
|
|
|
|
/// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
|
|
|
|
/// \c false), the entire block has to be kept on the same line (which is only
|
|
|
|
/// possible if it fits on the line, only contains a single statement, etc.
|
|
|
|
///
|
|
|
|
/// If \p NewLine is true, we format the nested block on separate lines, i.e.
|
|
|
|
/// break after the "{", format all lines with correct indentation and the put
|
|
|
|
/// the closing "}" on yet another new line.
|
|
|
|
///
|
|
|
|
/// This enables us to keep the simple structure of the
|
|
|
|
/// \c UnwrappedLineFormatter, where we only have two options for each token:
|
|
|
|
/// break or don't break.
|
|
|
|
bool formatChildren(LineState &State, bool NewLine, bool DryRun,
|
|
|
|
unsigned &Penalty) {
|
|
|
|
const FormatToken &LBrace = *State.NextToken->Previous;
|
|
|
|
if (LBrace.isNot(tok::l_brace) || LBrace.BlockKind != BK_Block ||
|
|
|
|
LBrace.Children.size() == 0)
|
2013-09-05 18:48:50 +08:00
|
|
|
// The previous token does not open a block. Nothing to do. We don't
|
|
|
|
// assert so that we can simply call this function for all tokens.
|
2013-09-06 16:54:24 +08:00
|
|
|
return true;
|
2013-09-05 17:29:45 +08:00
|
|
|
|
|
|
|
if (NewLine) {
|
|
|
|
unsigned ParentIndent = State.Stack.back().Indent;
|
|
|
|
for (SmallVector<AnnotatedLine *, 1>::const_iterator
|
|
|
|
I = LBrace.Children.begin(),
|
|
|
|
E = LBrace.Children.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
unsigned Indent =
|
|
|
|
ParentIndent + ((*I)->Level - Line.Level) * Style.IndentWidth;
|
|
|
|
if (!DryRun)
|
|
|
|
Whitespaces->replaceWhitespace(
|
|
|
|
*(*I)->First, /*Newlines=*/1, /*Spaces=*/Indent,
|
|
|
|
/*StartOfTokenColumn=*/Indent, Line.InPPDirective);
|
|
|
|
UnwrappedLineFormatter Formatter(Indenter, Whitespaces, Style, **I);
|
|
|
|
Penalty += Formatter.format(Indent, DryRun);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LBrace.Children.size() > 1)
|
|
|
|
return false; // Cannot merge multiple statements into a single line.
|
|
|
|
|
|
|
|
// We can't put the closing "}" on a line with a trailing comment.
|
|
|
|
if (LBrace.Children[0]->Last->isTrailingComment())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!DryRun) {
|
|
|
|
Whitespaces->replaceWhitespace(*LBrace.Children[0]->First,
|
|
|
|
/*Newlines=*/0, /*Spaces=*/1,
|
|
|
|
/*StartOfTokenColumn=*/State.Column,
|
|
|
|
State.Line->InPPDirective);
|
|
|
|
}
|
|
|
|
|
|
|
|
State.Column += 1 + LBrace.Children[0]->Last->TotalLength;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-16 19:20:30 +08:00
|
|
|
ContinuationIndenter *Indenter;
|
2013-09-05 17:29:45 +08:00
|
|
|
WhitespaceManager *Whitespaces;
|
2012-12-04 02:12:45 +08:00
|
|
|
FormatStyle Style;
|
2013-01-14 21:08:07 +08:00
|
|
|
const AnnotatedLine &Line;
|
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;
|
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-08-29 23:21:40 +08:00
|
|
|
FormatTokenLexer(Lexer &Lex, SourceManager &SourceMgr, FormatStyle &Style,
|
2013-06-05 22:09:10 +08:00
|
|
|
encoding::Encoding Encoding)
|
2013-08-29 23:21:40 +08:00
|
|
|
: FormatTok(NULL), GreaterStashed(false), Column(0),
|
|
|
|
TrailingWhitespace(0), Lex(Lex), SourceMgr(SourceMgr), Style(Style),
|
|
|
|
IdentTable(getFormattingLangOpts()), 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.
|
2013-08-29 23:21:40 +08:00
|
|
|
// FIXME: Increment Column and set OriginalColumn.
|
2013-05-28 21:42:28 +08:00
|
|
|
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-08-29 23:21:40 +08:00
|
|
|
for (int i = 0, e = FormatTok->TokenText.size(); i != e; ++i) {
|
|
|
|
switch (FormatTok->TokenText[i]) {
|
|
|
|
case '\n':
|
|
|
|
++FormatTok->NewlinesBefore;
|
|
|
|
// FIXME: This is technically incorrect, as it could also
|
|
|
|
// be a literal backslash at the end of the line.
|
2013-09-06 16:54:24 +08:00
|
|
|
if (i == 0 || FormatTok->TokenText[i - 1] != '\\')
|
2013-08-29 23:21:40 +08:00
|
|
|
FormatTok->HasUnescapedNewline = true;
|
|
|
|
FormatTok->LastNewlineOffset = WhitespaceLength + i + 1;
|
|
|
|
Column = 0;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
++Column;
|
|
|
|
break;
|
|
|
|
case '\t':
|
2013-09-05 22:08:34 +08:00
|
|
|
Column += Style.TabWidth - Column % Style.TabWidth;
|
2013-08-29 23:21:40 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
++Column;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
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-08-29 23:21:40 +08:00
|
|
|
Column = 0;
|
2013-07-17 04:28:33 +08:00
|
|
|
FormatTok->TokenText = FormatTok->TokenText.substr(2);
|
2012-12-08 00:15:44 +08:00
|
|
|
}
|
2013-08-29 23:21:40 +08:00
|
|
|
FormatTok->OriginalColumn = Column;
|
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-08-29 23:21:40 +08:00
|
|
|
// FIXME: Add the trimmed whitespace to Column.
|
2013-07-17 04:28:33 +08:00
|
|
|
StringRef UntrimmedText = FormatTok->TokenText;
|
2013-09-07 01:24:54 +08:00
|
|
|
FormatTok->TokenText = FormatTok->TokenText.rtrim(" \t\v\f");
|
2013-07-17 04:28:33 +08:00
|
|
|
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-09-02 21:58:14 +08:00
|
|
|
if (FormatTok->isOneOf(tok::string_literal, tok::comment)) {
|
|
|
|
StringRef Text = FormatTok->TokenText;
|
|
|
|
size_t FirstNewlinePos = Text.find('\n');
|
|
|
|
if (FirstNewlinePos != StringRef::npos) {
|
2013-09-05 22:08:34 +08:00
|
|
|
// FIXME: Handle embedded tabs.
|
|
|
|
FormatTok->FirstLineColumnWidth = encoding::columnWidthWithTabs(
|
|
|
|
Text.substr(0, FirstNewlinePos), 0, Style.TabWidth, Encoding);
|
|
|
|
FormatTok->LastLineColumnWidth = encoding::columnWidthWithTabs(
|
|
|
|
Text.substr(Text.find_last_of('\n') + 1), 0, Style.TabWidth,
|
|
|
|
Encoding);
|
2013-09-02 21:58:14 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-30 01:32:57 +08:00
|
|
|
// FIXME: Add the CodePointCount to Column.
|
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-08-29 23:21:40 +08:00
|
|
|
unsigned Column;
|
2013-05-27 23:23:34 +08:00
|
|
|
unsigned TrailingWhitespace;
|
2012-12-08 00:15:44 +08:00
|
|
|
Lexer &Lex;
|
|
|
|
SourceManager &SourceMgr;
|
2013-08-29 23:21:40 +08:00
|
|
|
FormatStyle &Style;
|
2012-12-08 00:15:44 +08:00
|
|
|
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-09-05 17:29:45 +08:00
|
|
|
virtual ~Formatter() {
|
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
|
|
|
delete AnnotatedLines[i];
|
|
|
|
}
|
|
|
|
}
|
2012-12-05 05:05:31 +08:00
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
tooling::Replacements format() {
|
2013-08-29 23:21:40 +08:00
|
|
|
FormatTokenLexer Tokens(Lex, SourceMgr, Style, 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) {
|
2013-09-05 17:29:45 +08:00
|
|
|
Annotator.annotate(*AnnotatedLines[i]);
|
2013-02-06 22:22:40 +08:00
|
|
|
}
|
|
|
|
deriveLocalStyle();
|
2013-01-14 21:08:07 +08:00
|
|
|
for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
|
2013-09-05 17:29:45 +08:00
|
|
|
Annotator.calculateFormattingInformation(*AnnotatedLines[i]);
|
2013-04-10 01:46:55 +08:00
|
|
|
}
|
2013-03-13 23:53:12 +08:00
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
Annotator.setCommentLineLevels(AnnotatedLines);
|
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-09-06 15:54:20 +08:00
|
|
|
for (SmallVectorImpl<AnnotatedLine *>::iterator I = AnnotatedLines.begin(),
|
|
|
|
E = AnnotatedLines.end();
|
2013-01-11 03:49:59 +08:00
|
|
|
I != E; ++I) {
|
2013-09-05 17:29:45 +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 {
|
2013-08-29 23:21:40 +08:00
|
|
|
Indent = LevelIndent = FirstTok->OriginalColumn;
|
2013-02-09 01:38:27 +08:00
|
|
|
}
|
2013-09-05 17:29:45 +08:00
|
|
|
ContinuationIndenter Indenter(Style, SourceMgr, Whitespaces, Encoding,
|
2013-08-16 19:20:30 +08:00
|
|
|
BinPackInconclusiveFunctions);
|
|
|
|
|
|
|
|
// If everything fits on a single line, just put it there.
|
|
|
|
unsigned ColumnLimit = Style.ColumnLimit;
|
2013-09-05 17:29:45 +08:00
|
|
|
AnnotatedLine *NextLine = *(I + 1);
|
|
|
|
if ((I + 1) != E && NextLine->InPPDirective &&
|
|
|
|
!NextLine->First->HasUnescapedNewline)
|
|
|
|
ColumnLimit = getColumnLimit(TheLine.InPPDirective);
|
2013-08-16 19:20:30 +08:00
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
if (TheLine.Last->TotalLength + Indent <= ColumnLimit) {
|
2013-09-06 15:54:20 +08:00
|
|
|
LineState State =
|
|
|
|
Indenter.getInitialState(Indent, &TheLine, /*DryRun=*/false);
|
2013-08-16 19:20:30 +08:00
|
|
|
while (State.NextToken != NULL)
|
|
|
|
Indenter.addTokenToState(State, false, false);
|
|
|
|
} else if (Style.ColumnLimit == 0) {
|
|
|
|
NoColumnLimitFormatter Formatter(&Indenter);
|
2013-09-05 17:29:45 +08:00
|
|
|
Formatter.format(Indent, &TheLine);
|
2013-08-16 19:20:30 +08:00
|
|
|
} else {
|
2013-09-05 17:29:45 +08:00
|
|
|
UnwrappedLineFormatter Formatter(&Indenter, &Whitespaces, Style,
|
|
|
|
TheLine);
|
|
|
|
Formatter.format(Indent);
|
2013-08-16 19:20:30 +08:00
|
|
|
}
|
|
|
|
|
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)) {
|
2013-08-29 23:21:40 +08:00
|
|
|
unsigned LevelIndent = Tok->OriginalColumn;
|
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-09-05 17:29:45 +08:00
|
|
|
PreviousLineLastToken = TheLine.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-09-05 17:29:45 +08:00
|
|
|
if (!AnnotatedLines[i]->First->Next)
|
2013-03-13 22:41:29 +08:00
|
|
|
continue;
|
2013-09-05 17:29:45 +08:00
|
|
|
FormatToken *Tok = AnnotatedLines[i]->First->Next;
|
2013-05-29 22:47:47 +08:00
|
|
|
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-09-06 15:54:20 +08:00
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator &I,
|
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E) {
|
2013-01-16 18:41:46 +08:00
|
|
|
// We can never merge stuff if there are trailing line comments.
|
2013-09-05 17:29:45 +08:00
|
|
|
AnnotatedLine *TheLine = *I;
|
|
|
|
if (TheLine->Last->Type == TT_LineComment)
|
2013-01-16 18:41:46 +08:00
|
|
|
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.
|
2013-09-05 17:29:45 +08:00
|
|
|
Limit = TheLine->Last->TotalLength > Limit
|
|
|
|
? 0
|
|
|
|
: Limit - TheLine->Last->TotalLength;
|
2013-01-16 15:02:34 +08:00
|
|
|
|
2013-09-05 17:29:45 +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-09-05 17:29:45 +08:00
|
|
|
if (TheLine->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-09-05 17:29:45 +08:00
|
|
|
TheLine->First->is(tok::kw_if)) {
|
2013-05-16 20:12:21 +08:00
|
|
|
tryMergeSimpleControlStatement(I, E, Limit);
|
|
|
|
} else if (Style.AllowShortLoopsOnASingleLine &&
|
2013-09-05 17:29:45 +08:00
|
|
|
TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
|
2013-05-16 20:12:21 +08:00
|
|
|
tryMergeSimpleControlStatement(I, E, Limit);
|
2013-09-05 17:29:45 +08:00
|
|
|
} else if (TheLine->InPPDirective && (TheLine->First->HasUnescapedNewline ||
|
|
|
|
TheLine->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-09-06 15:54:20 +08:00
|
|
|
void tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::iterator &I,
|
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E,
|
2013-01-14 23:40:57 +08:00
|
|
|
unsigned Limit) {
|
2013-02-22 05:33:55 +08:00
|
|
|
if (Limit == 0)
|
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
AnnotatedLine &Line = **I;
|
|
|
|
if (!(*(I + 1))->InPPDirective || (*(I + 1))->First->HasUnescapedNewline)
|
2013-01-14 23:52:06 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
if (I + 2 != E && (*(I + 2))->InPPDirective &&
|
|
|
|
!(*(I + 2))->First->HasUnescapedNewline)
|
2013-01-14 23:40:57 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
if (1 + (*(I + 1))->Last->TotalLength > Limit)
|
2013-01-16 18:41:46 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
join(Line, **(++I));
|
2013-01-14 23:40:57 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
void
|
|
|
|
tryMergeSimpleControlStatement(SmallVectorImpl<AnnotatedLine *>::iterator &I,
|
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E,
|
|
|
|
unsigned Limit) {
|
2013-02-22 05:33:55 +08:00
|
|
|
if (Limit == 0)
|
|
|
|
return;
|
2013-08-08 03:20:45 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
|
2013-09-05 17:29:45 +08:00
|
|
|
(*(I + 1))->First->is(tok::l_brace))
|
2013-08-08 03:20:45 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
if ((*(I + 1))->InPPDirective != (*I)->InPPDirective ||
|
|
|
|
((*(I + 1))->InPPDirective && (*(I + 1))->First->HasUnescapedNewline))
|
2013-01-18 22:46:43 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
AnnotatedLine &Line = **I;
|
2013-01-16 15:02:34 +08:00
|
|
|
if (Line.Last->isNot(tok::r_paren))
|
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
if (1 + (*(I + 1))->Last->TotalLength > Limit)
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +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) &&
|
2013-09-05 17:29:45 +08:00
|
|
|
(*(I + 2))->First->is(tok::kw_else))
|
2013-01-14 22:14:23 +08:00
|
|
|
return;
|
2013-09-05 17:29:45 +08:00
|
|
|
join(Line, **(++I));
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
void tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::iterator &I,
|
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E,
|
2013-01-29 19:27:30 +08:00
|
|
|
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-09-05 17:29:45 +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-09-05 17:29:45 +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-09-05 17:29:45 +08:00
|
|
|
join(Line, **(I + 1));
|
2013-01-22 00:42:44 +08:00
|
|
|
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.
|
2013-09-05 17:29:45 +08:00
|
|
|
if (I + 2 == E || (*(I + 2))->Type == LT_Invalid ||
|
2013-01-22 00:42:44 +08:00
|
|
|
!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.
|
2013-09-05 17:29:45 +08:00
|
|
|
if ((*(I + 1))->Last->Type == TT_LineComment || Tok->MustBreakBefore)
|
2013-01-22 00:42:44 +08:00
|
|
|
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-09-05 17:29:45 +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-09-05 17:29:45 +08:00
|
|
|
join(Line, **(I + 1));
|
|
|
|
join(Line, **(I + 2));
|
2013-01-22 00:42:44 +08:00
|
|
|
I += 2;
|
2013-01-12 01:54:10 +08:00
|
|
|
}
|
2013-01-14 22:14:23 +08:00
|
|
|
}
|
|
|
|
|
2013-09-06 15:54:20 +08:00
|
|
|
bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::iterator I,
|
2013-01-14 22:14:23 +08:00
|
|
|
unsigned Limit) {
|
2013-09-05 17:29:45 +08:00
|
|
|
return 1 + (*(I + 1))->Last->TotalLength + 1 +
|
|
|
|
(*(I + 2))->Last->TotalLength <=
|
2013-01-22 00:42:44 +08:00
|
|
|
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-09-06 15:54:20 +08:00
|
|
|
bool touchesPPDirective(SmallVectorImpl<AnnotatedLine *>::iterator I,
|
|
|
|
SmallVectorImpl<AnnotatedLine *>::iterator E) {
|
2013-05-10 21:00:49 +08:00
|
|
|
for (; I != E; ++I) {
|
2013-09-05 17:29:45 +08:00
|
|
|
if ((*I)->First->HasUnescapedNewline)
|
2013-05-10 21:00:49 +08:00
|
|
|
return false;
|
2013-09-05 17:29:45 +08:00
|
|
|
if (touchesLine(**I))
|
2013-05-10 21:00:49 +08:00
|
|
|
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-09-05 17:29:45 +08:00
|
|
|
AnnotatedLines.push_back(new 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
|
|
|
}
|
|
|
|
|
2013-09-05 17:29:45 +08:00
|
|
|
unsigned getColumnLimit(bool InPPDirective) const {
|
|
|
|
// In preprocessor directives reserve two chars for trailing " \"
|
|
|
|
return Style.ColumnLimit - (InPPDirective ? 2 : 0);
|
|
|
|
}
|
|
|
|
|
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-09-06 15:54:20 +08:00
|
|
|
SmallVector<AnnotatedLine *, 16> 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
|