r181693 - Implements UseTab for clang-format.

Manuel Klimek klimek at google.com
Mon May 13 02:22:11 PDT 2013


Author: klimek
Date: Mon May 13 04:22:11 2013
New Revision: 181693

URL: http://llvm.org/viewvc/llvm-project?rev=181693&view=rev
Log:
Implements UseTab for clang-format.

This is required for kernel linux kernel style formatting.

Modified:
    cfe/trunk/include/clang/Format/Format.h
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/WhitespaceManager.cpp
    cfe/trunk/lib/Format/WhitespaceManager.h
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=181693&r1=181692&r2=181693&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Mon May 13 04:22:11 2013
@@ -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;
   }
 
 };

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=181693&r1=181692&r2=181693&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon May 13 04:22:11 2013
@@ -86,6 +86,7 @@ template <> struct MappingTraits<clang::
                    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;
 }
 

Modified: cfe/trunk/lib/Format/WhitespaceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.cpp?rev=181693&r1=181692&r2=181693&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.cpp (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.cpp Mon May 13 04:22:11 2013
@@ -122,7 +122,7 @@ void WhitespaceManager::addUntouchableCo
 
 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::getNewLin
       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() {

Modified: cfe/trunk/lib/Format/WhitespaceManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/WhitespaceManager.h?rev=181693&r1=181692&r2=181693&view=diff
==============================================================================
--- cfe/trunk/lib/Format/WhitespaceManager.h (original)
+++ cfe/trunk/lib/Format/WhitespaceManager.h Mon May 13 04:22:11 2013
@@ -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,

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=181693&r1=181692&r2=181693&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon May 13 04:22:11 2013
@@ -4016,6 +4016,27 @@ TEST_F(FormatTest, ConfigurableIndentWid
                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);





More information about the cfe-commits mailing list