r328220 - [clang-format] Fix ObjC style guesser to also iterate over child lines

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 22 10:37:19 PDT 2018


Author: benhamilton
Date: Thu Mar 22 10:37:19 2018
New Revision: 328220

URL: http://llvm.org/viewvc/llvm-project?rev=328220&view=rev
Log:
[clang-format] Fix ObjC style guesser to also iterate over child lines

Summary:
When I wrote `ObjCHeaderStyleGuesser`, I incorrectly assumed the
correct way to iterate over all tokens in `AnnotatedLine` was to
iterate over the linked list tokens starting with
`AnnotatedLine::First`.

However, `AnnotatedLine` also contains a vector
`AnnotedLine::Children` with child `AnnotedLine`s which have their own
tokens which we need to iterate over.

Because I didn't iterate over the tokens in the children lines, the
ObjC style guesser would fail on syntax like:

  #define FOO ({ NSString *s = ... })

as the statement(s) inside { ... } are child lines.

This fixes the bug and adds a test. I confirmed the test
failed before the fix, and passed after the fix.

Test Plan: New tests added. Ran tests with:
  % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests

Reviewers: djasper, jolesiak, Wizard

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D44790

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=328220&r1=328219&r2=328220&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Mar 22 10:37:19 2018
@@ -1514,8 +1514,8 @@ private:
         "UIView",
     };
 
-    for (auto &Line : AnnotatedLines) {
-      for (FormatToken *FormatTok = Line->First; FormatTok;
+    auto LineContainsObjCCode = [&Keywords](const AnnotatedLine &Line) {
+      for (const FormatToken *FormatTok = Line.First; FormatTok;
            FormatTok = FormatTok->Next) {
         if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
              (FormatTok->isObjCAtKeyword(tok::objc_interface) ||
@@ -1536,6 +1536,15 @@ private:
           return true;
         }
       }
+      return false;
+    };
+    for (auto Line : AnnotatedLines) {
+      if (LineContainsObjCCode(*Line))
+        return true;
+      for (auto ChildLine : Line->Children) {
+        if (LineContainsObjCCode(*ChildLine))
+          return true;
+      }
     }
     return false;
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=328220&r1=328219&r2=328220&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 22 10:37:19 2018
@@ -12166,6 +12166,13 @@ TEST_F(FormatTest, GuessLanguageWithCare
       guessLanguage("foo.h", "int(^foo[(kNumEntries + 10)])(char, float);"));
 }
 
+TEST_F(FormatTest, GuessLanguageWithChildLines) {
+  EXPECT_EQ(FormatStyle::LK_Cpp,
+            guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
+  EXPECT_EQ(FormatStyle::LK_ObjC,
+            guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang




More information about the cfe-commits mailing list