[PATCH] D44831: [clang-format] Refine ObjC guesser to handle child lines of child lines

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 23 08:08:05 PDT 2018


benhamilton updated this revision to Diff 139601.
benhamilton added a comment.

Update from correct base revision


Repository:
  rC Clang

https://reviews.llvm.org/D44831

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -12171,6 +12171,12 @@
             guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
   EXPECT_EQ(FormatStyle::LK_ObjC,
             guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
+  EXPECT_EQ(
+      FormatStyle::LK_Cpp,
+      guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
+  EXPECT_EQ(
+      FormatStyle::LK_ObjC,
+      guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
 }
 
 } // end namespace
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -31,6 +31,7 @@
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/VirtualFileSystem.h"
 #include "clang/Lex/Lexer.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Allocator.h"
@@ -1538,13 +1539,18 @@
       }
       return false;
     };
-    for (auto Line : AnnotatedLines) {
-      if (LineContainsObjCCode(*Line))
+    llvm::DenseSet<AnnotatedLine *> LinesToCheckSet;
+    LinesToCheckSet.reserve(AnnotatedLines.size());
+    LinesToCheckSet.insert(AnnotatedLines.begin(), AnnotatedLines.end());
+    while (!LinesToCheckSet.empty()) {
+      const auto NextLineIter = LinesToCheckSet.begin();
+      const auto NextLine = *NextLineIter;
+      if (LineContainsObjCCode(*NextLine))
         return true;
-      for (auto ChildLine : Line->Children) {
-        if (LineContainsObjCCode(*ChildLine))
-          return true;
-      }
+      LinesToCheckSet.erase(NextLineIter);
+      LinesToCheckSet.reserve(NextLine->Children.size());
+      LinesToCheckSet.insert(NextLine->Children.begin(),
+                             NextLine->Children.end());
     }
     return false;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44831.139601.patch
Type: text/x-patch
Size: 1979 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180323/b89abd21/attachment.bin>


More information about the cfe-commits mailing list