[clang] [clang-format] Don't crash on incomplete template declaration (PR #173433)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 24 05:06:29 PST 2025
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/173433
>From c7bc9e36f67976c44857f26d3f282b630ae496c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Wed, 24 Dec 2025 01:12:59 +0100
Subject: [PATCH] [clang-format] Don't crash on incomplete template declaration
---
clang/lib/Format/TokenAnnotator.cpp | 10 ++++++----
clang/unittests/Format/FormatTest.cpp | 2 ++
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index f6341ff0305a9..1123ae98c02ca 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3711,7 +3711,7 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line,
if (Tok->is(TT_AttributeLSquare)) {
Tok = Tok->MatchingParen;
if (!Tok)
- break;
+ return nullptr;
continue;
}
@@ -3743,6 +3743,8 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line,
return nullptr;
Tok = Tok->MatchingParen;
+ if (!Tok)
+ return nullptr;
continue;
}
@@ -3751,7 +3753,7 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line,
if (Tok->is(tok::coloncolon)) {
Tok = Tok->Next;
if (!Tok)
- break;
+ return nullptr;
}
// Skip to the unqualified part of the name.
@@ -3765,12 +3767,12 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line,
if (Tok->is(tok::tilde)) {
Tok = Tok->Next;
if (!Tok)
- break;
+ return nullptr;
}
// Make sure the name is not already annotated, e.g. as NamespaceMacro.
if (Tok->isNot(tok::identifier) || Tok->isNot(TT_Unknown))
- break;
+ return nullptr;
Name = Tok;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5cdac66d1dcbd..f5f23388453ec 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -29053,6 +29053,8 @@ TEST_F(FormatTest, KeywordedFunctionLikeMacros) {
Style);
}
+TEST_F(FormatTest, UnbalancedAngleBrackets) { verifyFormat("template <"); }
+
} // namespace
} // namespace test
} // namespace format
More information about the cfe-commits
mailing list