Skip to content

Commit ea7a27b

Browse files
committedMar 27, 2018
[clang-format] Refine ObjC guesser to handle child lines of child lines
Summary: This fixes an issue brought up by djasper@ in his review of D44790. We handled top-level child lines, but if those child lines themselves had child lines, we didn't handle them. Rather than use recursion (which could blow out the stack), I use a DenseSet to hold the set of lines we haven't yet checked (since order doesn't matter), and update the set to add the children of each line as we check it. Test Plan: New tests added. Confirmed tests failed before fix and passed after fix. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44831 llvm-svn: 328628
1 parent d54e7aa commit ea7a27b

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed
 

‎clang/lib/Format/Format.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -1514,8 +1514,8 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
15141514
"UIView",
15151515
};
15161516

1517-
auto LineContainsObjCCode = [&Keywords](const AnnotatedLine &Line) {
1518-
for (const FormatToken *FormatTok = Line.First; FormatTok;
1517+
for (auto Line : AnnotatedLines) {
1518+
for (const FormatToken *FormatTok = Line->First; FormatTok;
15191519
FormatTok = FormatTok->Next) {
15201520
if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
15211521
(FormatTok->isObjCAtKeyword(tok::objc_interface) ||
@@ -1535,14 +1535,7 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
15351535
TT_ObjCMethodSpecifier, TT_ObjCProperty)) {
15361536
return true;
15371537
}
1538-
}
1539-
return false;
1540-
};
1541-
for (auto Line : AnnotatedLines) {
1542-
if (LineContainsObjCCode(*Line))
1543-
return true;
1544-
for (auto ChildLine : Line->Children) {
1545-
if (LineContainsObjCCode(*ChildLine))
1538+
if (guessIsObjC(Line->Children, Keywords))
15461539
return true;
15471540
}
15481541
}

‎clang/unittests/Format/FormatTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -12159,6 +12159,12 @@ TEST_F(FormatTest, GuessLanguageWithChildLines) {
1215912159
guessLanguage("foo.h", "#define FOO ({ std::string s; })"));
1216012160
EXPECT_EQ(FormatStyle::LK_ObjC,
1216112161
guessLanguage("foo.h", "#define FOO ({ NSString *s; })"));
12162+
EXPECT_EQ(
12163+
FormatStyle::LK_Cpp,
12164+
guessLanguage("foo.h", "#define FOO ({ foo(); ({ std::string s; }) })"));
12165+
EXPECT_EQ(
12166+
FormatStyle::LK_ObjC,
12167+
guessLanguage("foo.h", "#define FOO ({ foo(); ({ NSString *s; }) })"));
1216212168
}
1216312169

1216412170
} // end namespace

0 commit comments

Comments
 (0)
Please sign in to comment.