From 57de589877407ca3173f127b4d5583fda677c84b Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Wed, 5 Oct 2016 15:49:01 +0000 Subject: [PATCH] Make DeletedLines local variables in checkEmptyNamespace. Summary: Patch by Sam McCall! Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D25162 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@283332 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 11 ++++++----- unittests/Format/CleanupTest.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index d58e6adb99..2a47c2f5cf 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1041,11 +1041,12 @@ private: // Iterate through all lines and remove any empty (nested) namespaces. void checkEmptyNamespace(SmallVectorImpl &AnnotatedLines) { + std::set DeletedLines; for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) { auto &Line = *AnnotatedLines[i]; if (Line.startsWith(tok::kw_namespace) || Line.startsWith(tok::kw_inline, tok::kw_namespace)) { - checkEmptyNamespace(AnnotatedLines, i, i); + checkEmptyNamespace(AnnotatedLines, i, i, DeletedLines); } } @@ -1063,7 +1064,8 @@ private: // sets \p NewLine to the last line checked. // Returns true if the current namespace is empty. bool checkEmptyNamespace(SmallVectorImpl &AnnotatedLines, - unsigned CurrentLine, unsigned &NewLine) { + unsigned CurrentLine, unsigned &NewLine, + std::set &DeletedLines) { unsigned InitLine = CurrentLine, End = AnnotatedLines.size(); if (Style.BraceWrapping.AfterNamespace) { // If the left brace is in a new line, we should consume it first so that @@ -1083,7 +1085,8 @@ private: if (AnnotatedLines[CurrentLine]->startsWith(tok::kw_namespace) || AnnotatedLines[CurrentLine]->startsWith(tok::kw_inline, tok::kw_namespace)) { - if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine)) + if (!checkEmptyNamespace(AnnotatedLines, CurrentLine, NewLine, + DeletedLines)) return false; CurrentLine = NewLine; continue; @@ -1208,8 +1211,6 @@ private: // Tokens to be deleted. std::set DeletedTokens; - // The line numbers of lines to be deleted. - std::set DeletedLines; }; struct IncludeDirective { diff --git a/unittests/Format/CleanupTest.cpp b/unittests/Format/CleanupTest.cpp index 25fa9539f2..77c4d9be81 100644 --- a/unittests/Format/CleanupTest.cpp +++ b/unittests/Format/CleanupTest.cpp @@ -119,6 +119,24 @@ TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) { EXPECT_EQ(Expected, Result); } +TEST_F(CleanupTest, EmptyNamespaceAroundConditionalCompilation) { + std::string Code = "#ifdef A\n" + "int a;\n" + "int b;\n" + "#else\n" + "#endif\n" + "namespace {}"; + std::string Expected = "#ifdef A\n" + "int a;\n" + "int b;\n" + "#else\n" + "#endif\n"; + std::vector Ranges(1, tooling::Range(0, Code.size())); + FormatStyle Style = getLLVMStyle(); + std::string Result = cleanup(Code, Ranges, Style); + EXPECT_EQ(Expected, Result); +} + TEST_F(CleanupTest, CtorInitializationSimpleRedundantComma) { std::string Code = "class A {\nA() : , {} };"; std::string Expected = "class A {\nA() {} };"; -- 2.40.0