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 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; } 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