From: Matthias Gehre Date: Tue, 4 Apr 2017 20:11:13 +0000 (+0000) Subject: [clang-format] fix crash in NamespaceEndCommentsFixer (PR32438) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dad9866debccdb9ef2929373263d8a733a824016;p=clang [clang-format] fix crash in NamespaceEndCommentsFixer (PR32438) Summary: The new test case was crashing before. Now it passes as expected. Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D31441 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299465 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/NamespaceEndCommentsFixer.cpp b/lib/Format/NamespaceEndCommentsFixer.cpp index af751100ec..88cf123c18 100644 --- a/lib/Format/NamespaceEndCommentsFixer.cpp +++ b/lib/Format/NamespaceEndCommentsFixer.cpp @@ -133,7 +133,7 @@ tooling::Replacements NamespaceEndCommentsFixer::analyze( // Detect "(inline)? namespace" in the beginning of a line. if (NamespaceTok->is(tok::kw_inline)) NamespaceTok = NamespaceTok->getNextNonComment(); - if (NamespaceTok->isNot(tok::kw_namespace)) + if (!NamespaceTok || NamespaceTok->isNot(tok::kw_namespace)) continue; FormatToken *RBraceTok = EndLine->First; if (RBraceTok->Finalized) diff --git a/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/unittests/Format/NamespaceEndCommentsFixerTest.cpp index 48ecdb038e..912638f456 100644 --- a/unittests/Format/NamespaceEndCommentsFixerTest.cpp +++ b/unittests/Format/NamespaceEndCommentsFixerTest.cpp @@ -582,6 +582,21 @@ TEST_F(NamespaceEndCommentsFixerTest, "} // namespace\n" "}")); } + +TEST_F(NamespaceEndCommentsFixerTest, HandlesInlineAtEndOfLine_PR32438) { + EXPECT_EQ("template struct a {};\n" + "struct a b() {\n" + "}\n" + "#define c inline\n" + "void d() {\n" + "}\n", + fixNamespaceEndComments("template struct a {};\n" + "struct a b() {\n" + "}\n" + "#define c inline\n" + "void d() {\n" + "}\n")); +} } // end namespace } // end namespace format } // end namespace clang