]> granicus.if.org Git - clang/commitdiff
clang-format: Fix hanging nested blocks in macros.
authorDaniel Jasper <djasper@google.com>
Tue, 12 May 2015 10:16:02 +0000 (10:16 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 12 May 2015 10:16:02 +0000 (10:16 +0000)
Before:
  #define MACRO()                     \
    Debug(aaa, /* force line break */ \
          {                           \
      int i;                          \
      int j;                          \
          })

After:
  #define MACRO()                     \
    Debug(aaa, /* force line break */ \
          {                           \
            int i;                    \
            int j;                    \
          })

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

lib/Format/UnwrappedLineFormatter.cpp
unittests/Format/FormatTest.cpp

index b01d989f88fd21ef0f60cfd1b0e02e376a3025fb..43f65dd04b2d934ea5c07d610b2d70a229f8743f 100644 (file)
@@ -39,7 +39,7 @@ public:
   LevelIndentTracker(const FormatStyle &Style,
                      const AdditionalKeywords &Keywords, unsigned StartLevel,
                      int AdditionalIndent)
-      : Style(Style), Keywords(Keywords) {
+      : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
     for (unsigned i = 0; i != StartLevel; ++i)
       IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
   }
@@ -52,7 +52,7 @@ public:
   void nextLine(const AnnotatedLine &Line) {
     Offset = getIndentOffset(*Line.First);
     if (Line.InPPDirective) {
-      Indent = Line.Level * Style.IndentWidth;
+      Indent = Line.Level * Style.IndentWidth + AdditionalIndent;
     } else {
       while (IndentForLevel.size() <= Line.Level)
         IndentForLevel.push_back(-1);
@@ -110,6 +110,8 @@ private:
   const FormatStyle &Style;
   const AdditionalKeywords &Keywords;
 
+  unsigned AdditionalIndent;
+
   /// \brief The indent in characters for each level.
   std::vector<int> IndentForLevel;
 
index 22217c06f4cc8f57dc296f3806a1636e14a1fe1e..e6dee38fb08bc0ec68981842380a71b7d7917bce 100644 (file)
@@ -3284,6 +3284,18 @@ TEST_F(FormatTest, LayoutNestedBlocks) {
   verifyNoCrash("^{v^{a}}");
 }
 
+TEST_F(FormatTest, FormatNestedBlocksInMacros) {
+  EXPECT_EQ("#define MACRO()                     \\\n"
+            "  Debug(aaa, /* force line break */ \\\n"
+            "        {                           \\\n"
+            "          int i;                    \\\n"
+            "          int j;                    \\\n"
+            "        })",
+            format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
+                   "          {  int   i;  int  j;   })",
+                   getGoogleStyle()));
+}
+
 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
   EXPECT_EQ("DEBUG({\n"
             "  int i;\n"