From: Daniel Jasper Date: Fri, 10 Nov 2017 17:11:18 +0000 (+0000) Subject: [clang-format] Handle leading comments in using declaration X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=98efdc1da46347d20d688cf3b60d251035074e89;p=clang [clang-format] Handle leading comments in using declaration This fixes clang-format internal assertion for the following code: /* override */ using std::string; Patch by Igor Sugak. Thank you. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@317901 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UsingDeclarationsSorter.cpp b/lib/Format/UsingDeclarationsSorter.cpp index 72151137ee..2c124ec78b 100644 --- a/lib/Format/UsingDeclarationsSorter.cpp +++ b/lib/Format/UsingDeclarationsSorter.cpp @@ -172,15 +172,17 @@ std::pair UsingDeclarationsSorter::analyze( tooling::Replacements Fixes; SmallVector UsingDeclarations; for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) { + const auto *FirstTok = AnnotatedLines[I]->First; if (AnnotatedLines[I]->InPPDirective || - !AnnotatedLines[I]->startsWith(tok::kw_using) || - AnnotatedLines[I]->First->Finalized) { + !AnnotatedLines[I]->startsWith(tok::kw_using) || FirstTok->Finalized) { endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); continue; } - if (AnnotatedLines[I]->First->NewlinesBefore > 1) + if (FirstTok->NewlinesBefore > 1) endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); - std::string Label = computeUsingDeclarationLabel(AnnotatedLines[I]->First); + const auto *UsingTok = + FirstTok->is(tok::comment) ? FirstTok->getNextNonComment() : FirstTok; + std::string Label = computeUsingDeclarationLabel(UsingTok); if (Label.empty()) { endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes); continue; diff --git a/unittests/Format/UsingDeclarationsSorterTest.cpp b/unittests/Format/UsingDeclarationsSorterTest.cpp index e72076a8e3..37427a51f8 100644 --- a/unittests/Format/UsingDeclarationsSorterTest.cpp +++ b/unittests/Format/UsingDeclarationsSorterTest.cpp @@ -348,6 +348,13 @@ TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) { {tooling::Range(19, 1)})); } +TEST_F(UsingDeclarationsSorterTest, SortsUsingDeclarationsWithLeadingkComments) { + EXPECT_EQ("/* comment */ using a;\n" + "/* comment */ using b;", + sortUsingDeclarations("/* comment */ using b;\n" + "/* comment */ using a;")); +} + } // end namespace } // end namespace format } // end namespace clang