]> granicus.if.org Git - clang/commitdiff
Implements UseTab for clang-format.
authorManuel Klimek <klimek@google.com>
Mon, 13 May 2013 09:22:11 +0000 (09:22 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 13 May 2013 09:22:11 +0000 (09:22 +0000)
This is required for kernel linux kernel style formatting.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181693 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Format/Format.h
lib/Format/Format.cpp
lib/Format/WhitespaceManager.cpp
lib/Format/WhitespaceManager.h
unittests/Format/FormatTest.cpp

index dd8f6661801c6698e0ea99b875ba0d982f1ea232..79e26d0a0c6aa13be823024992b8b9c4b20df2b2 100644 (file)
@@ -97,6 +97,10 @@ struct FormatStyle {
   /// \brief The number of characters to use for indentation.
   unsigned IndentWidth;
 
+  /// \brief If true, \c IndentWidth consecutive spaces will be replaced with
+  /// tab characters.
+  bool UseTab;
+
   bool operator==(const FormatStyle &R) const {
     return AccessModifierOffset == R.AccessModifierOffset &&
            AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
@@ -117,7 +121,8 @@ struct FormatStyle {
            PointerBindsToType == R.PointerBindsToType &&
            SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments &&
            Standard == R.Standard &&
-           IndentWidth == IndentWidth;
+           IndentWidth == R.IndentWidth &&
+           UseTab == R.UseTab;
   }
 
 };
index f3ca9c371f1ecb1d67da1bae0a11ae74d0c38549..2f2f095780dc0847a05153e58087c5feb075f9a0 100644 (file)
@@ -86,6 +86,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
                    Style.SpacesBeforeTrailingComments);
     IO.mapOptional("Standard", Style.Standard);
     IO.mapOptional("IndentWidth", Style.IndentWidth);
+    IO.mapOptional("UseTab", Style.UseTab);
   }
 };
 }
@@ -113,6 +114,7 @@ FormatStyle getLLVMStyle() {
   LLVMStyle.SpacesBeforeTrailingComments = 1;
   LLVMStyle.Standard = FormatStyle::LS_Cpp03;
   LLVMStyle.IndentWidth = 2;
+  LLVMStyle.UseTab = false;
   return LLVMStyle;
 }
 
@@ -135,6 +137,7 @@ FormatStyle getGoogleStyle() {
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
   GoogleStyle.IndentWidth = 2;
+  GoogleStyle.UseTab = false;
   return GoogleStyle;
 }
 
index a75c592bfeda6b574afc17641c9da452e4d75382..21e38b1e979e9e750f3f55e66878c40ccbbb8016 100644 (file)
@@ -122,7 +122,7 @@ void WhitespaceManager::addUntouchableComment(unsigned Column) {
 
 std::string WhitespaceManager::getNewLineText(unsigned NewLines,
                                               unsigned Spaces) {
-  return std::string(NewLines, '\n') + std::string(Spaces, ' ');
+  return std::string(NewLines, '\n') + getIndentText(Spaces);
 }
 
 std::string WhitespaceManager::getNewLineText(unsigned NewLines,
@@ -139,7 +139,15 @@ std::string WhitespaceManager::getNewLineText(unsigned NewLines,
       Offset = 0;
     }
   }
-  return NewLineText + std::string(Spaces, ' ');
+  return NewLineText + getIndentText(Spaces);
+}
+
+std::string WhitespaceManager::getIndentText(unsigned Spaces) {
+  if (!Style.UseTab) {
+    return std::string(Spaces, ' ');
+  }
+  return std::string(Spaces / Style.IndentWidth, '\t') +
+         std::string(Spaces % Style.IndentWidth, ' ');
 }
 
 void WhitespaceManager::alignComments() {
index 5f3dc55edaccb28cd13d86f43c75052d5798aeeb..1b24b3f6d79d8b3eb79223198ab1c45425807fd5 100644 (file)
@@ -78,6 +78,8 @@ private:
                              unsigned WhitespaceStartColumn,
                              unsigned EscapedNewlineColumn);
 
+  std::string getIndentText(unsigned Spaces);
+
   /// \brief Structure to store tokens for later layout and alignment.
   struct StoredToken {
     StoredToken(SourceLocation ReplacementLoc, unsigned ReplacementLength,
index 931d29dd3e7c8d1ba2872b4ccde7a3862d750cd8..ce79a1c97b99c407c99669810a18026de4338bb5 100644 (file)
@@ -4016,6 +4016,27 @@ TEST_F(FormatTest, ConfigurableIndentWidth) {
                EightIndent);
 }
 
+TEST_F(FormatTest, ConfigurableUseOfTab) {
+  FormatStyle Tab = getLLVMStyleWithColumns(42);
+  Tab.IndentWidth = 8;
+  Tab.UseTab = true;
+  Tab.AlignEscapedNewlinesLeft = true;
+  verifyFormat("class X {\n"
+               "\tvoid f() {\n"
+               "\t\tsomeFunction(parameter1,\n"
+               "\t\t\t     parameter2);\n"
+               "\t}\n"
+               "};",
+               Tab);
+  verifyFormat("#define A                        \\\n"
+               "\tvoid f() {               \\\n"
+               "\t\tsomeFunction(    \\\n"
+               "\t\t    parameter1,  \\\n"
+               "\t\t    parameter2); \\\n"
+               "\t}",
+               Tab);
+}
+
 bool allStylesEqual(ArrayRef<FormatStyle> Styles) {
   for (size_t i = 1; i < Styles.size(); ++i)
     if (!(Styles[0] == Styles[i]))
@@ -4070,6 +4091,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
   CHECK_PARSE_BOOL(IndentCaseLabels);
   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
   CHECK_PARSE_BOOL(PointerBindsToType);
+  CHECK_PARSE_BOOL(UseTab);
 
   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);