/// Switch statement body is always indented one level more than case labels.
bool IndentCaseLabels;
+ enum NamespaceIndentationKind {
+ NI_None, // Don't indent in namespaces.
+ NI_Inner, // Indent only in inner namespaces (nested in other namespaces).
+ NI_All // Indent in all namespaces.
+ };
+
+ /// \brief The indentation used for namespaces.
+ NamespaceIndentationKind NamespaceIndentation;
+
/// \brief The number of spaces to before trailing line comments.
unsigned SpacesBeforeTrailingComments;
IndentCaseLabels == R.IndentCaseLabels &&
IndentWidth == R.IndentWidth &&
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
+ NamespaceIndentation == R.NamespaceIndentation &&
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
PenaltyBreakComment == R.PenaltyBreakComment &&
PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess &&
}
};
+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);
+ }
+};
+
template <> struct MappingTraits<clang::format::FormatStyle> {
static void mapping(llvm::yaml::IO &IO, clang::format::FormatStyle &Style) {
if (IO.outputting()) {
Style.ExperimentalAutoDetectBinPacking);
IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
+ IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
IO.mapOptional("PenaltyBreakComment", Style.PenaltyBreakComment);
LLVMStyle.IndentFunctionDeclarationAfterType = false;
LLVMStyle.IndentWidth = 2;
LLVMStyle.MaxEmptyLinesToKeep = 1;
+ LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PointerBindsToType = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
GoogleStyle.IndentFunctionDeclarationAfterType = true;
GoogleStyle.IndentWidth = 2;
GoogleStyle.MaxEmptyLinesToKeep = 1;
+ GoogleStyle.NamespaceIndentation = FormatStyle::NI_None;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
GoogleStyle.PointerBindsToType = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
FormatStyle getWebKitStyle() {
FormatStyle Style = getLLVMStyle();
- Style.ColumnLimit = 0;
- Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+ Style.AccessModifierOffset = -4;
Style.BreakBeforeBinaryOperators = true;
+ Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
Style.BreakConstructorInitializersBeforeComma = true;
+ Style.ColumnLimit = 0;
Style.IndentWidth = 4;
+ Style.NamespaceIndentation = FormatStyle::NI_Inner;
Style.PointerBindsToType = true;
return Style;
}
FormatTok = Tokens->setPosition(StoredPosition);
}
-void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
- unsigned AddLevels) {
+void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel) {
assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
unsigned InitialLevel = Line->Level;
nextToken();
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
MustBeDeclaration);
- Line->Level += AddLevels;
+ if (AddLevel)
+ ++Line->Level;
parseLevel(/*HasOpeningBrace=*/true);
if (!FormatTok->Tok.is(tok::r_brace)) {
if (FormatTok->Tok.is(tok::string_literal)) {
nextToken();
if (FormatTok->Tok.is(tok::l_brace)) {
- parseBlock(/*MustBeDeclaration=*/true, 0);
+ parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
addUnwrappedLine();
return;
}
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
addUnwrappedLine();
- parseBlock(/*MustBeDeclaration=*/true, 0);
+ bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
+ (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
+ DeclarationScopeStack.size() > 1);
+ parseBlock(/*MustBeDeclaration=*/true, AddLevel);
// Munch the semicolon after a namespace. This is more common than one would
// think. Puttin the semicolon into its own line is very ugly.
if (FormatTok->Tok.is(tok::semi))
if (FormatTok->Tok.is(tok::l_paren))
parseParens();
if (FormatTok->Tok.is(tok::l_brace)) {
- parseBlock(/*MustBeDeclaration=*/false, 1);
+ parseBlock(/*MustBeDeclaration=*/false);
addUnwrappedLine();
} else {
addUnwrappedLine();
private:
void parseFile();
void parseLevel(bool HasOpeningBrace);
- void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1);
+ void parseBlock(bool MustBeDeclaration, bool AddLevel = true);
void parsePPDirective();
void parsePPDefine();
void parsePPIf();
" 1.1.1. to keep the formatting.\n"
" */"));
EXPECT_EQ("/*\n"
- "Don't try to outdent if there's not enough inentation.\n"
+ "Don't try to outdent if there's not enough indentation.\n"
"*/",
format(" /*\n"
- " Don't try to outdent if there's not enough inentation.\n"
+ " Don't try to outdent if there's not enough indentation.\n"
" */"));
EXPECT_EQ("int i; /* Comment with empty...\n"
"int i;\n"
"} // my_namespace\n"
"#endif // HEADER_GUARD"));
+
+ FormatStyle Style = getLLVMStyle();
+ Style.NamespaceIndentation = FormatStyle::NI_All;
+ EXPECT_EQ("namespace out {\n"
+ " int i;\n"
+ " namespace in {\n"
+ " int i;\n"
+ " } // namespace\n"
+ "} // namespace",
+ format("namespace out {\n"
+ "int i;\n"
+ "namespace in {\n"
+ "int i;\n"
+ "} // namespace\n"
+ "} // namespace",
+ Style));
+
+ Style.NamespaceIndentation = FormatStyle::NI_Inner;
+ EXPECT_EQ("namespace out {\n"
+ "int i;\n"
+ "namespace in {\n"
+ " int i;\n"
+ "} // namespace\n"
+ "} // namespace",
+ format("namespace out {\n"
+ "int i;\n"
+ "namespace in {\n"
+ "int i;\n"
+ "} // namespace\n"
+ "} // namespace",
+ Style));
}
TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
FormatStyle::BS_Stroustrup);
+ Style.NamespaceIndentation = FormatStyle::NI_All;
+ CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
+ FormatStyle::NI_None);
+ CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
+ FormatStyle::NI_Inner);
+ CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
+ FormatStyle::NI_All);
+
#undef CHECK_PARSE
#undef CHECK_PARSE_BOOL
}
verifyFormat("namespace outer {\n"
"int i;\n"
"namespace inner {\n"
- "int i;\n" // FIXME: This should be indented.
+ " int i;\n"
"} // namespace inner\n"
"} // namespace outer\n"
"namespace other_outer {\n"
" , aaaaaaaaaaaaaaaaaaaaaaa()\n{\n}",
Style);
+ // Access specifiers should be aligned left.
+ verifyFormat("class C {\n"
+ "public:\n"
+ " int i;\n"
+ "};",
+ Style);
+
// Do not align comments.
// FIXME: Implement option to suppress comment alignment.
// verifyFormat("int a; // Do not\n"