diff options
author | Eric Liu <ioeric@google.com> | 2016-04-25 15:09:22 +0000 |
---|---|---|
committer | Eric Liu <ioeric@google.com> | 2016-04-25 15:09:22 +0000 |
commit | 4cfb88a936a60c09ceeee336b73b1fb9a6d80a2d (patch) | |
tree | 5f4ae40eff88c26b9eca545fd8a74ed5cf9f1610 /clang/unittests/Format/CleanupTest.cpp | |
parent | 06c14ec31e738ba8cd7c87449389f3e5d0b4c5ea (diff) | |
download | bcm5719-llvm-4cfb88a936a60c09ceeee336b73b1fb9a6d80a2d.tar.gz bcm5719-llvm-4cfb88a936a60c09ceeee336b73b1fb9a6d80a2d.zip |
Added Fixer implementation and fix() interface in clang-format for removing redundant code.
Summary:
After applying replacements, redundant code like extra commas or empty namespaces
might be introduced. Fixer can detect and remove any redundant code introduced by replacements.
The current implementation only handles redundant commas.
Reviewers: djasper, klimek
Subscribers: ioeric, mprobst, klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D18551
llvm-svn: 267416
Diffstat (limited to 'clang/unittests/Format/CleanupTest.cpp')
-rw-r--r-- | clang/unittests/Format/CleanupTest.cpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/clang/unittests/Format/CleanupTest.cpp b/clang/unittests/Format/CleanupTest.cpp new file mode 100644 index 00000000000..5ea8e651a38 --- /dev/null +++ b/clang/unittests/Format/CleanupTest.cpp @@ -0,0 +1,118 @@ +//===- unittest/Format/CleanupTest.cpp - Code cleanup unit tests ----------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "clang/Format/Format.h" + +#include "clang/Tooling/Core/Replacement.h" + +#include "gtest/gtest.h" + +namespace clang { +namespace format { +namespace { + +class CleanupTest : public ::testing::Test { +protected: + std::string cleanup(llvm::StringRef Code, + const std::vector<tooling::Range> &Ranges, + const FormatStyle &Style = getLLVMStyle()) { + tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges); + + std::string Result = applyAllReplacements(Code, Replaces); + EXPECT_NE("", Result); + return Result; + } +}; + +TEST_F(CleanupTest, DeleteEmptyNamespaces) { + std::string Code = "namespace A {\n" + "namespace B {\n" + "} // namespace B\n" + "} // namespace A\n\n" + "namespace C {\n" + "namespace D { int i; }\n" + "inline namespace E { namespace { } }\n" + "}"; + std::string Expected = "\n\n\n\n\nnamespace C {\n" + "namespace D { int i; }\n \n" + "}"; + std::vector<tooling::Range> Ranges; + Ranges.push_back(tooling::Range(28, 0)); + Ranges.push_back(tooling::Range(91, 6)); + Ranges.push_back(tooling::Range(132, 0)); + std::string Result = cleanup(Code, Ranges); + EXPECT_EQ(Expected, Result); +} + +TEST_F(CleanupTest, NamespaceWithSyntaxError) { + std::string Code = "namespace A {\n" + "namespace B {\n" // missing r_brace + "} // namespace A\n\n" + "namespace C {\n" + "namespace D int i; }\n" + "inline namespace E { namespace { } }\n" + "}"; + std::string Expected = "namespace A {\n" + "\n\n\nnamespace C {\n" + "namespace D int i; }\n \n" + "}"; + std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); + std::string Result = cleanup(Code, Ranges); + EXPECT_EQ(Expected, Result); +} + +TEST_F(CleanupTest, EmptyNamespaceNotAffected) { + std::string Code = "namespace A {\n\n" + "namespace {\n\n}}"; + // Even though the namespaces are empty, but the inner most empty namespace + // block is not affected by the changed ranges. + std::string Expected = "namespace A {\n\n" + "namespace {\n\n}}"; + // Set the changed range to be the second "\n". + std::vector<tooling::Range> Ranges(1, tooling::Range(14, 0)); + std::string Result = cleanup(Code, Ranges); + EXPECT_EQ(Expected, Result); +} + +TEST_F(CleanupTest, EmptyNamespaceWithCommentsNoBreakBeforeBrace) { + std::string Code = "namespace A {\n" + "namespace B {\n" + "// Yo\n" + "} // namespace B\n" + "} // namespace A\n" + "namespace C { // Yo\n" + "}"; + std::string Expected = "\n\n\n\n\n\n"; + std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); + std::string Result = cleanup(Code, Ranges); + EXPECT_EQ(Expected, Result); +} + +TEST_F(CleanupTest, EmptyNamespaceWithCommentsBreakBeforeBrace) { + std::string Code = "namespace A\n" + "/* Yo */ {\n" + "namespace B\n" + "{\n" + "// Yo\n" + "} // namespace B\n" + "} // namespace A\n" + "namespace C\n" + "{ // Yo\n" + "}\n"; + std::string Expected = "\n\n\n\n\n\n\n\n\n\n"; + std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); + FormatStyle Style = getLLVMStyle(); + Style.BraceWrapping.AfterNamespace = true; + std::string Result = cleanup(Code, Ranges, Style); + EXPECT_EQ(Expected, Result); +} + +} // end namespace +} // end namespace format +} // end namespace clang |