Skip to content

Commit

Permalink
[clang-format] Fix crash with ObjC Blocks
Browse files Browse the repository at this point in the history
Fixes #54367
Fixes #54368

Differential Revision: https://reviews.llvm.org/D121596
  • Loading branch information
HazardyKnusperkeks committed Mar 15, 2022
1 parent d4c39f1 commit 1db8112
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -783,7 +783,7 @@ class AnnotatingParser {
OpeningBrace.ParentBracket = Contexts.back().ContextKind;

if (Contexts.back().CaretFound)
OpeningBrace.setType(TT_ObjCBlockLBrace);
OpeningBrace.overwriteFixedType(TT_ObjCBlockLBrace);
Contexts.back().CaretFound = false;

ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
Expand Down
16 changes: 16 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Expand Up @@ -644,6 +644,22 @@ TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
EXPECT_TOKEN(Tokens[4], tok::r_brace, TT_InlineASMBrace);
}

TEST_F(TokenAnnotatorTest, UnderstandsObjCBlock) {
auto Tokens = annotate("int (^)() = ^ ()\n"
" external_source_symbol() { //\n"
" return 1;\n"
"};");
ASSERT_EQ(Tokens.size(), 21u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_ObjCBlockLParen);
EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_ObjCBlockLBrace);

Tokens = annotate("int *p = ^int*(){ //\n"
" return nullptr;\n"
"}();");
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_ObjCBlockLBrace);
}

} // namespace
} // namespace format
} // namespace clang

0 comments on commit 1db8112

Please sign in to comment.