clang-format: Add more options to namespace indentation.
With this patch, clang-format can be configured to:
* not indent in namespace at all (former behavior).
* indent in namespace as in other blocks.
* indent only in inner namespaces (as required by WebKit style).
Also fix alignment of access specifiers in WebKit style.
Patch started by Marek Kurdej. Thank you!
llvm-svn: 187540
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4a28cca2..61d0614 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -842,10 +842,10 @@
" 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"
@@ -1565,6 +1565,37 @@
"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;"); }
@@ -5485,6 +5516,14 @@
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
}
@@ -5586,7 +5625,7 @@
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"
@@ -5632,6 +5671,13 @@
" , 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"