head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC4:1.1.1.4
	netbsd-11-0-RC3:1.1.1.4
	netbsd-11-0-RC2:1.1.1.4
	netbsd-11-0-RC1:1.1.1.4
	perseant-exfatfs-base-20250801:1.1.1.4
	netbsd-11:1.1.1.4.0.10
	netbsd-11-base:1.1.1.4
	netbsd-10-1-RELEASE:1.1.1.4
	perseant-exfatfs-base-20240630:1.1.1.4
	perseant-exfatfs:1.1.1.4.0.8
	perseant-exfatfs-base:1.1.1.4
	netbsd-8-3-RELEASE:1.1.1.1
	netbsd-9-4-RELEASE:1.1.1.3
	netbsd-10-0-RELEASE:1.1.1.4
	netbsd-10-0-RC6:1.1.1.4
	netbsd-10-0-RC5:1.1.1.4
	netbsd-10-0-RC4:1.1.1.4
	netbsd-10-0-RC3:1.1.1.4
	netbsd-10-0-RC2:1.1.1.4
	netbsd-10-0-RC1:1.1.1.4
	netbsd-10:1.1.1.4.0.6
	netbsd-10-base:1.1.1.4
	netbsd-9-3-RELEASE:1.1.1.3
	cjep_sun2x:1.1.1.4.0.4
	cjep_sun2x-base:1.1.1.4
	cjep_staticlib_x-base1:1.1.1.4
	netbsd-9-2-RELEASE:1.1.1.3
	cjep_staticlib_x:1.1.1.4.0.2
	cjep_staticlib_x-base:1.1.1.4
	netbsd-9-1-RELEASE:1.1.1.3
	phil-wifi-20200421:1.1.1.4
	phil-wifi-20200411:1.1.1.4
	phil-wifi-20200406:1.1.1.4
	netbsd-8-2-RELEASE:1.1.1.1
	netbsd-9-0-RELEASE:1.1.1.3
	netbsd-9-0-RC2:1.1.1.3
	netbsd-9-0-RC1:1.1.1.3
	netbsd-9:1.1.1.3.0.2
	netbsd-9-base:1.1.1.3
	phil-wifi-20190609:1.1.1.3
	netbsd-8-1-RELEASE:1.1.1.1
	netbsd-8-1-RC1:1.1.1.1
	pgoyette-compat-merge-20190127:1.1.1.2.2.1
	pgoyette-compat-20190127:1.1.1.3
	pgoyette-compat-20190118:1.1.1.3
	pgoyette-compat-1226:1.1.1.3
	pgoyette-compat-1126:1.1.1.3
	pgoyette-compat-1020:1.1.1.3
	pgoyette-compat-0930:1.1.1.3
	pgoyette-compat-0906:1.1.1.3
	pgoyette-compat-0728:1.1.1.3
	clang-337282:1.1.1.3
	netbsd-8-0-RELEASE:1.1.1.1
	phil-wifi:1.1.1.2.0.4
	phil-wifi-base:1.1.1.2
	pgoyette-compat-0625:1.1.1.2
	netbsd-8-0-RC2:1.1.1.1
	pgoyette-compat-0521:1.1.1.2
	pgoyette-compat-0502:1.1.1.2
	pgoyette-compat-0422:1.1.1.2
	netbsd-8-0-RC1:1.1.1.1
	pgoyette-compat-0415:1.1.1.2
	pgoyette-compat-0407:1.1.1.2
	pgoyette-compat-0330:1.1.1.2
	pgoyette-compat-0322:1.1.1.2
	pgoyette-compat-0315:1.1.1.2
	pgoyette-compat:1.1.1.2.0.2
	pgoyette-compat-base:1.1.1.2
	clang-319952:1.1.1.2
	matt-nb8-mediatek:1.1.1.1.0.12
	matt-nb8-mediatek-base:1.1.1.1
	clang-309604:1.1.1.2
	perseant-stdc-iso10646:1.1.1.1.0.10
	perseant-stdc-iso10646-base:1.1.1.1
	netbsd-8:1.1.1.1.0.8
	netbsd-8-base:1.1.1.1
	prg-localcount2-base3:1.1.1.1
	prg-localcount2-base2:1.1.1.1
	prg-localcount2-base1:1.1.1.1
	prg-localcount2:1.1.1.1.0.6
	prg-localcount2-base:1.1.1.1
	pgoyette-localcount-20170426:1.1.1.1
	bouyer-socketcan-base1:1.1.1.1
	pgoyette-localcount:1.1.1.1.0.4
	pgoyette-localcount-20170320:1.1.1.1
	clang-294123:1.1.1.1
	bouyer-socketcan:1.1.1.1.0.2
	bouyer-socketcan-base:1.1.1.1
	clang-291444:1.1.1.1
	LLVM:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2017.01.11.10.33.06;	author joerg;	state Exp;
branches
	1.1.1.1;
next	;
commitid	CNnUNfII1jgNmxBz;

1.1.1.1
date	2017.01.11.10.33.06;	author joerg;	state Exp;
branches
	1.1.1.1.4.1;
next	1.1.1.2;
commitid	CNnUNfII1jgNmxBz;

1.1.1.2
date	2017.08.01.19.34.42;	author joerg;	state Exp;
branches
	1.1.1.2.2.1
	1.1.1.2.4.1;
next	1.1.1.3;
commitid	pMuDy65V0VicSx1A;

1.1.1.3
date	2018.07.17.18.32.13;	author joerg;	state Exp;
branches;
next	1.1.1.4;
commitid	wDzL46ALjrCZgwKA;

1.1.1.4
date	2019.11.13.22.23.10;	author joerg;	state dead;
branches;
next	;
commitid	QD8YATxuNG34YJKB;

1.1.1.1.4.1
date	2017.01.11.10.33.06;	author pgoyette;	state dead;
branches;
next	1.1.1.1.4.2;
commitid	jjw7cAwgyKq7RfKz;

1.1.1.1.4.2
date	2017.03.20.06.53.39;	author pgoyette;	state Exp;
branches;
next	;
commitid	jjw7cAwgyKq7RfKz;

1.1.1.2.2.1
date	2018.07.28.04.34.18;	author pgoyette;	state Exp;
branches;
next	;
commitid	1UP1xAIUxv1ZgRLA;

1.1.1.2.4.1
date	2019.06.10.21.46.46;	author christos;	state Exp;
branches;
next	1.1.1.2.4.2;
commitid	jtc8rnCzWiEEHGqB;

1.1.1.2.4.2
date	2020.04.13.07.50.38;	author martin;	state dead;
branches;
next	;
commitid	X01YhRUPVUDaec4C;


desc
@@


1.1
log
@Initial revision
@
text
@//===- 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 "../Tooling/ReplacementTest.h"
#include "../Tooling/RewriterTestContext.h"
#include "clang/Tooling/Core/Replacement.h"

#include "gtest/gtest.h"

using clang::tooling::ReplacementTest;
using clang::tooling::toReplacements;

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);

    auto Result = applyAllReplacements(Code, Replaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  // Returns code after cleanup around \p Offsets.
  std::string cleanupAroundOffsets(llvm::ArrayRef<unsigned> Offsets,
                                   llvm::StringRef Code) {
    std::vector<tooling::Range> Ranges;
    for (auto Offset : Offsets)
      Ranges.push_back(tooling::Range(Offset, 0));
    return cleanup(Code, Ranges);
  }
};

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"
                         "}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code));
}

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()));
  EXPECT_EQ(Expected, cleanup(Code, Ranges));
}

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".
  EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code));
}

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);
}

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<tooling::Range> 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()  {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code));

  Code = "class A {\nA() : x(1), {} };";
  Expected = "class A {\nA() : x(1) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code));

  Code = "class A {\nA() :,,,,{} };";
  Expected = "class A {\nA() {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}

TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
  std::string Code = "class A {\nA() : =default; };";
  std::string Expected = "class A {\nA()  =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));

  Code = "class A {\nA() : , =default; };";
  Expected = "class A {\nA()  =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}

TEST_F(CleanupTest, ListRedundantComma) {
  std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }";
  std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));

  Code = "int main() { f(1,,2,3,,4);}";
  Expected = "int main() { f(1,2,3,4);}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code));
}

TEST_F(CleanupTest, TrailingCommaInParens) {
  std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}";
  std::string Expected = "int main() { f(1,2,3,f(1,2),4);}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code));
}

TEST_F(CleanupTest, TrailingCommaInBraces) {
  // Trainling comma is allowed in brace list.
  // If there was trailing comma in the original code, then trailing comma is
  // preserved. In this example, element between the last two commas is deleted
  // causing the second-last comma to be redundant.
  std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }";
  std::string Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));

  // If there was no trailing comma in the original code, then trainling comma
  // introduced by replacements should be cleaned up. In this example, the
  // element after the last comma is deleted causing the last comma to be
  // redundant.
  Code = "void f() { std::vector<int> v = {1,2,3,}; }";
  // FIXME: redundant trailing comma should be removed.
  Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));

  // Still no trailing comma in the original code, but two elements are deleted,
  // which makes it seems like there was trailing comma.
  Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }";
  // FIXME: redundant trailing comma should also be removed.
  Expected = "void f() { std::vector<int> v = {1, 2, 3,  }; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code));
}

TEST_F(CleanupTest, CtorInitializationBracesInParens) {
  std::string Code = "class A {\nA() : x({1}),, {} };";
  std::string Expected = "class A {\nA() : x({1}) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code));
}

TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
  std::string Code =
      "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  std::string Expected =
      "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  // Set the affected range to be "int x = 0", which does not intercept the
  // constructor initialization list.
  std::vector<tooling::Range> Ranges(1, tooling::Range(42, 9));
  std::string Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);

  Code = "class A {\nA() : x(1), {} };";
  Expected = "class A {\nA() : x(1), {} };";
  // No range. Fixer should do nothing.
  Ranges.clear();
  Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);
}

TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
  std::string Code =
      "class A {\nA() : x({1}), /* comment */, /* comment */ {} };";
  std::string Expected = "class A {\nA() : x({1}) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code));

  Code = "class A {\nA() : x({1}), // comment\n {} };";
  Expected = "class A {\nA() : x({1})\n {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code));

  Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
  Expected = "class A {\nA() : x({1}),  y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code));

  Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
  Expected = "class A {\nA() : x({1}), \n y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));

  Code = "class A {\nA() : , // comment\n y(1),{} };";
  Expected = "class A {\nA() :  // comment\n y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code));

  Code = "class A {\nA() // comment\n : ,,{} };";
  Expected = "class A {\nA() // comment\n {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));

  Code = "class A {\nA() // comment\n : ,,=default; };";
  Expected = "class A {\nA() // comment\n =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
}

TEST_F(CleanupTest, CtorInitializerInNamespace) {
  std::string Code = "namespace A {\n"
                     "namespace B {\n" // missing r_brace
                     "} // namespace A\n\n"
                     "namespace C {\n"
                     "class A { A() : x(0),, {} };\n"
                     "inline namespace E { namespace { } }\n"
                     "}";
  std::string Expected = "namespace A {\n"
                         "\n\n\nnamespace C {\n"
                         "class A { A() : x(0) {} };\n   \n"
                         "}";
  std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  std::string Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);
}

class CleanUpReplacementsTest : public ReplacementTest {
protected:
  tooling::Replacement createReplacement(unsigned Offset, unsigned Length,
                                         StringRef Text) {
    return tooling::Replacement(FileName, Offset, Length, Text);
  }

  tooling::Replacement createInsertion(StringRef IncludeDirective) {
    return createReplacement(UINT_MAX, 0, IncludeDirective);
  }

  tooling::Replacement createDeletion(StringRef HeaderName) {
    return createReplacement(UINT_MAX, 1, HeaderName);
  }

  inline std::string apply(StringRef Code,
                           const tooling::Replacements Replaces) {
    auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
    EXPECT_TRUE(static_cast<bool>(CleanReplaces))
        << llvm::toString(CleanReplaces.takeError()) << "\n";
    auto Result = applyAllReplacements(Code, *CleanReplaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  inline std::string formatAndApply(StringRef Code,
                                    const tooling::Replacements Replaces) {

    auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
    EXPECT_TRUE(static_cast<bool>(CleanReplaces))
        << llvm::toString(CleanReplaces.takeError()) << "\n";
    auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style);
    EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
        << llvm::toString(FormattedReplaces.takeError()) << "\n";
    auto Result = applyAllReplacements(Code, *FormattedReplaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  int getOffset(StringRef Code, int Line, int Column) {
    RewriterTestContext Context;
    FileID ID = Context.createInMemoryFile(FileName, Code);
    auto DecomposedLocation =
        Context.Sources.getDecomposedLoc(Context.getLocation(ID, Line, Column));
    return DecomposedLocation.second;
  }

  const std::string FileName = "fix.cpp";
  FormatStyle Style = getLLVMStyle();
};

TEST_F(CleanUpReplacementsTest, FixOnlyAffectedCodeAfterReplacements) {
  std::string Code = "namespace A {\n"
                     "namespace B {\n"
                     "  int x;\n"
                     "} // namespace B\n"
                     "} // namespace A\n"
                     "\n"
                     "namespace C {\n"
                     "namespace D { int i; }\n"
                     "inline namespace E { namespace { int y; } }\n"
                     "int x=     0;"
                     "}";
  std::string Expected = "\n\nnamespace C {\n"
                         "namespace D { int i; }\n\n"
                         "int x=     0;"
                         "}";
  tooling::Replacements Replaces =
      toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""),
                      createReplacement(getOffset(Code, 9, 34), 6, "")});

  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithoutDefine) {
  std::string Code = "int main() {}";
  std::string Expected = "#include \"a.h\"\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithDefine) {
  std::string Code = "#ifndef A_H\n"
                     "#define A_H\n"
                     "class A {};\n"
                     "#define MMM 123\n"
                     "#endif";
  std::string Expected = "#ifndef A_H\n"
                         "#define A_H\n"
                         "#include \"b.h\"\n"
                         "class A {};\n"
                         "#define MMM 123\n"
                         "#endif";

  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"b.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertBeforeCategoryWithLowerPriority) {
  std::string Code = "#ifndef A_H\n"
                     "#define A_H\n"
                     "\n"
                     "\n"
                     "\n"
                     "#include <vector>\n"
                     "class A {};\n"
                     "#define MMM 123\n"
                     "#endif";
  std::string Expected = "#ifndef A_H\n"
                         "#define A_H\n"
                         "\n"
                         "\n"
                         "\n"
                         "#include \"a.h\"\n"
                         "#include <vector>\n"
                         "class A {};\n"
                         "#define MMM 123\n"
                         "#endif";

  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertAfterMainHeader) {
  std::string Code = "#include \"fix.h\"\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include \"fix.h\"\n"
                         "#include <a>\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <a>")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertBeforeSystemHeaderLLVM) {
  std::string Code = "#include <memory>\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include \"z.h\"\n"
                         "#include <memory>\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"z.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertAfterSystemHeaderGoogle) {
  std::string Code = "#include <memory>\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include <memory>\n"
                         "#include \"z.h\"\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"z.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertOneIncludeLLVMStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n"
                     "#include \"clang/Format/Format.h\"\n"
                     "#include <memory>\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"d.h\"\n"
                         "#include \"clang/Format/Format.h\"\n"
                         "#include \"llvm/x/y.h\"\n"
                         "#include <memory>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"d.h\""),
                      createInsertion("#include \"llvm/x/y.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n"
                     "#include \"clang/Format/Format.h\"\n"
                     "#include <memory>\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"new/new.h\"\n"
                         "#include \"clang/Format/Format.h\"\n"
                         "#include <memory>\n"
                         "#include <list>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <list>"),
                      createInsertion("#include \"new/new.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertNewSystemIncludeGoogleStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "\n"
                     "#include \"y/a.h\"\n"
                     "#include \"z/b.h\"\n";
  // FIXME: inserting after the empty line following the main header might be
  // prefered.
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include <vector>\n"
                         "\n"
                         "#include \"y/a.h\"\n"
                         "#include \"z/b.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "\n"
                     "#include <vector>\n"
                     "\n"
                     "#include \"y/a.h\"\n"
                     "#include \"z/b.h\"\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "\n"
                         "#include <vector>\n"
                         "#include <list>\n"
                         "\n"
                         "#include \"y/a.h\"\n"
                         "#include \"z/b.h\"\n"
                         "#include \"x/x.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <list>"),
                      createInsertion("#include \"x/x.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
  std::string Code = "\nint x;";
  std::string Expected = "\n#include \"fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "int x;";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
       createInsertion("#include \"b.h\""),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"fix.h\"")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
  std::string Code = "\nint x;";
  std::string Expected = "\n#include \"fix.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n"
                         "int x;";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
       createInsertion("#include \"b.h\""),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"fix.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {
  std::string Code = "\n"
                     "int x;\n"
                     "int    a;\n"
                     "int    a;\n"
                     "int    a;";

  std::string Expected = "\n#include \"x.h\"\n"
                         "#include \"y.h\"\n"
                         "#include \"clang/x/x.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "int    a;\n"
                         "int b;\n"
                         "int    a;";
  tooling::Replacements Replaces = toReplacements(
      {createReplacement(getOffset(Code, 4, 8), 1, "b"),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"clang/x/x.h\""),
       createInsertion("#include \"y.h\""),
       createInsertion("#include \"x.h\"")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NotConfusedByDefine) {
  std::string Code = "void f() {}\n"
                     "#define A \\\n"
                     "  int i;";
  std::string Expected = "#include <vector>\n"
                         "void f() {}\n"
                         "#define A \\\n"
                         "  int i;";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkippedTopComment) {
  std::string Code = "// comment\n"
                     "\n"
                     "   // comment\n";
  std::string Expected = "// comment\n"
                         "\n"
                         "   // comment\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkippedMixedComments) {
  std::string Code = "// comment\n"
                     "// comment \\\n"
                     " comment continued\n"
                     "/*\n"
                     "* comment\n"
                     "*/\n";
  std::string Expected = "// comment\n"
                         "// comment \\\n"
                         " comment continued\n"
                         "/*\n"
                         "* comment\n"
                         "*/\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, MultipleBlockCommentsInOneLine) {
  std::string Code = "/*\n"
                     "* comment\n"
                     "*/ /* comment\n"
                     "*/\n"
                     "\n\n"
                     "/* c1 */ /*c2 */\n";
  std::string Expected = "/*\n"
                         "* comment\n"
                         "*/ /* comment\n"
                         "*/\n"
                         "\n\n"
                         "/* c1 */ /*c2 */\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CodeAfterComments) {
  std::string Code = "/*\n"
                     "* comment\n"
                     "*/ /* comment\n"
                     "*/\n"
                     "\n\n"
                     "/* c1 */ /*c2 */\n"
                     "\n"
                     "int x;\n";
  std::string Expected = "/*\n"
                         "* comment\n"
                         "*/ /* comment\n"
                         "*/\n"
                         "\n\n"
                         "/* c1 */ /*c2 */\n"
                         "\n"
                         "#include <vector>\n"
                         "int x;\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FakeHeaderGuardIfDef) {
  std::string Code = "// comment \n"
                     "#ifdef X\n"
                     "#define X\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifdef X\n"
                         "#define X\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, RealHeaderGuardAfterComments) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "#define X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#ifndef X\n"
                         "#define X\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, IfNDefWithNoDefine) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifndef X\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FakeHeaderGuard) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "#define 1\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifndef X\n"
                         "#define 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, HeaderGuardWithComment) {
  std::string Code = "// comment \n"
                     "#ifndef X // comment\n"
                     "// comment\n"
                     "/* comment\n"
                     "*/\n"
                     "/* comment */ #define X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#ifndef X // comment\n"
                         "// comment\n"
                         "/* comment\n"
                         "*/\n"
                         "/* comment */ #define X\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, EmptyCode) {
  std::string Code = "";
  std::string Expected = "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {
  std::string Code = "#include <map>";
  std::string Expected = "#include <map>\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
  std::string Code = "#include <map>";
  std::string Expected =
      "#include <map>\n#include <string>\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <string>"),
                      createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {
  std::string Code = "#include \"a.h\"\n"
                     "#include <vector>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>"),
                      createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {
  std::string Code = "#include \"a.h\"\n"
                     "#include <vector>\n";
  // FIXME: this might not be the best behavior.
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"vector\"\n"
                         "#include <vector>\n"
                         "#include <a.h>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"vector\""),
                      createInsertion("#include <a.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SimpleDeleteIncludes) {
  std::string Code = "#include \"abc.h\"\n"
                     "#include \"xyz.h\" // comment\n"
                     "#include \"xyz\"\n"
                     "int x;\n";
  std::string Expected = "#include \"xyz\"\n"
                         "int x;\n";
  tooling::Replacements Replaces =
      toReplacements({createDeletion("abc.h"), createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, DeleteAllCode) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include <xyz.h>";
  std::string Expected = "";
  tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, DeleteAllIncludesWithSameNameIfNoType) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include \"xyz\"\n"
                     "#include <xyz.h>\n";
  std::string Expected = "#include \"xyz\"\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, OnlyDeleteHeaderWithType) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include \"xyz\"\n"
                     "#include <xyz.h>";
  std::string Expected = "#include \"xyz.h\"\n"
                         "#include \"xyz\"\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("<xyz.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertionAndDeleteHeader) {
  std::string Code = "#include \"a.h\"\n"
                     "\n"
                     "#include <vector>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "\n"
                         "#include <map>\n";
  tooling::Replacements Replaces = toReplacements(
      {createDeletion("<vector>"), createInsertion("#include <map>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionAfterCode) {
  std::string Code = "#include \"a.h\"\n"
                     "void f() {}\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "void f() {}\n"
                         "#include \"b.h\"\n";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionInStringLiteral) {
  std::string Code = "#include \"a.h\"\n"
                     "const char[] = R\"(\n"
                     "#include \"b.h\"\n"
                     ")\";\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "const char[] = R\"(\n"
                         "#include \"b.h\"\n"
                         ")\";\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionAfterOtherDirective) {
  std::string Code = "#include \"a.h\"\n"
                     "#ifdef X\n"
                     "#include \"b.h\"\n"
                     "#endif\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "#ifdef X\n"
                         "#include \"b.h\"\n"
                         "#endif\n";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanInsertAfterLongSystemInclude) {
  std::string Code = "#include \"a.h\"\n"
                     "// comment\n\n"
                     "#include <a/b/c/d/e.h>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "// comment\n\n"
                         "#include <a/b/c/d/e.h>\n"
                         "#include <x.h>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <x.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanInsertAfterComment) {
  std::string Code = "#include \"a.h\"\n"
                     "// Comment\n"
                     "\n"
                     "/* Comment */\n"
                     "// Comment\n"
                     "\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "// Comment\n"
                         "\n"
                         "/* Comment */\n"
                         "// Comment\n"
                         "\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, LongCommentsInTheBeginningOfFile) {
  std::string Code = "// Loooooooooooooooooooooooooong comment\n"
                     "// Loooooooooooooooooooooooooong comment\n"
                     "// Loooooooooooooooooooooooooong comment\n"
                     "#include <string>\n"
                     "#include <vector>\n"
                     "\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n";
  std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
                         "// Loooooooooooooooooooooooooong comment\n"
                         "// Loooooooooooooooooooooooooong comment\n"
                         "#include <string>\n"
                         "#include <vector>\n"
                         "\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"third.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"third.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanDeleteAfterCode) {
  std::string Code = "#include \"a.h\"\n"
                     "void f() {}\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "void f() {}\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("\"b.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

} // end namespace
} // end namespace format
} // end namespace clang
@


1.1.1.1
log
@Import Clang pre-4.0.0 r291444.
@
text
@@


1.1.1.2
log
@Import clang r309604 from branches/release_50
@
text
@d39 1
a39 2
                                   llvm::StringRef Code,
                                   const FormatStyle &Style = getLLVMStyle()) {
d43 1
a43 1
    return cleanup(Code, Ranges, Style);
a173 8
TEST_F(CleanupTest, NoCleanupsForJavaScript) {
  std::string Code = "function f() { var x = [a, b, , c]; }";
  std::string Expected = "function f() { var x = [a, b, , c]; }";
  const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_JavaScript);

  EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code, Style));
}

d295 1
a295 1
                           const tooling::Replacements &Replaces) {
d305 2
a306 1
                                    const tooling::Replacements &Replaces) {
@


1.1.1.2.4.1
log
@Sync with HEAD
@
text
@d361 113
a477 1
                     "#include \"z.h\"\n"
a483 1
                         "#include \"z.h\"\n"
d485 2
a486 2
                         "#include <list>\n"
                         "#include <memory>\n";
d493 18
d520 1
a521 1
                         "#include <vector>\n"
a522 1
                         "#include \"x/x.h\"\n"
d524 2
a525 1
                         "#include \"z/b.h\"\n";
a567 13
TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
  std::string Code = "#include <map>";
  // FIXME: a better behavior is to only append on newline to Code, but this
  // case should be rare in practice.
  std::string Expected =
      "#include <map>\n#include <string>\n\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <string>"),
                      createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}


d593 219
d824 27
d863 111
@


1.1.1.2.4.2
log
@Mostly merge changes from HEAD upto 20200411
@
text
@@


1.1.1.2.2.1
log
@Sync with HEAD
@
text
@d361 113
a477 1
                     "#include \"z.h\"\n"
a483 1
                         "#include \"z.h\"\n"
d485 2
a486 2
                         "#include <list>\n"
                         "#include <memory>\n";
d493 18
d520 1
a521 1
                         "#include <vector>\n"
a522 1
                         "#include \"x/x.h\"\n"
d524 2
a525 1
                         "#include \"z/b.h\"\n";
a567 13
TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
  std::string Code = "#include <map>";
  // FIXME: a better behavior is to only append on newline to Code, but this
  // case should be rare in practice.
  std::string Expected =
      "#include <map>\n#include <string>\n\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <string>"),
                      createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}


d593 219
d824 27
d863 111
@


1.1.1.3
log
@Import clang r337282 from trunk
@
text
@d361 113
a477 1
                     "#include \"z.h\"\n"
a483 1
                         "#include \"z.h\"\n"
d485 2
a486 2
                         "#include <list>\n"
                         "#include <memory>\n";
d493 18
d520 1
a521 1
                         "#include <vector>\n"
a522 1
                         "#include \"x/x.h\"\n"
d524 2
a525 1
                         "#include \"z/b.h\"\n";
a567 13
TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
  std::string Code = "#include <map>";
  // FIXME: a better behavior is to only append on newline to Code, but this
  // case should be rare in practice.
  std::string Expected =
      "#include <map>\n#include <string>\n\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <string>"),
                      createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}


d593 219
d824 27
d863 111
@


1.1.1.4
log
@Mark old LLVM instance as dead.
@
text
@@


1.1.1.1.4.1
log
@file CleanupTest.cpp was added on branch pgoyette-localcount on 2017-03-20 06:53:39 +0000
@
text
@d1 968
@


1.1.1.1.4.2
log
@Sync with HEAD
@
text
@a0 968
//===- 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 "../Tooling/ReplacementTest.h"
#include "../Tooling/RewriterTestContext.h"
#include "clang/Tooling/Core/Replacement.h"

#include "gtest/gtest.h"

using clang::tooling::ReplacementTest;
using clang::tooling::toReplacements;

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);

    auto Result = applyAllReplacements(Code, Replaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  // Returns code after cleanup around \p Offsets.
  std::string cleanupAroundOffsets(llvm::ArrayRef<unsigned> Offsets,
                                   llvm::StringRef Code) {
    std::vector<tooling::Range> Ranges;
    for (auto Offset : Offsets)
      Ranges.push_back(tooling::Range(Offset, 0));
    return cleanup(Code, Ranges);
  }
};

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"
                         "}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code));
}

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()));
  EXPECT_EQ(Expected, cleanup(Code, Ranges));
}

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".
  EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code));
}

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);
}

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<tooling::Range> 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()  {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code));

  Code = "class A {\nA() : x(1), {} };";
  Expected = "class A {\nA() : x(1) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code));

  Code = "class A {\nA() :,,,,{} };";
  Expected = "class A {\nA() {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}

TEST_F(CleanupTest, CtorInitializationSimpleRedundantColon) {
  std::string Code = "class A {\nA() : =default; };";
  std::string Expected = "class A {\nA()  =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));

  Code = "class A {\nA() : , =default; };";
  Expected = "class A {\nA()  =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code));
}

TEST_F(CleanupTest, ListRedundantComma) {
  std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }";
  std::string Expected = "void f() { std::vector<int> v = {1,2,3,{4,5}}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));

  Code = "int main() { f(1,,2,3,,4);}";
  Expected = "int main() { f(1,2,3,4);}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code));
}

TEST_F(CleanupTest, TrailingCommaInParens) {
  std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}";
  std::string Expected = "int main() { f(1,2,3,f(1,2),4);}";
  EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code));
}

TEST_F(CleanupTest, TrailingCommaInBraces) {
  // Trainling comma is allowed in brace list.
  // If there was trailing comma in the original code, then trailing comma is
  // preserved. In this example, element between the last two commas is deleted
  // causing the second-last comma to be redundant.
  std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }";
  std::string Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));

  // If there was no trailing comma in the original code, then trainling comma
  // introduced by replacements should be cleaned up. In this example, the
  // element after the last comma is deleted causing the last comma to be
  // redundant.
  Code = "void f() { std::vector<int> v = {1,2,3,}; }";
  // FIXME: redundant trailing comma should be removed.
  Expected = "void f() { std::vector<int> v = {1,2,3,}; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code));

  // Still no trailing comma in the original code, but two elements are deleted,
  // which makes it seems like there was trailing comma.
  Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }";
  // FIXME: redundant trailing comma should also be removed.
  Expected = "void f() { std::vector<int> v = {1, 2, 3,  }; }";
  EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code));
}

TEST_F(CleanupTest, CtorInitializationBracesInParens) {
  std::string Code = "class A {\nA() : x({1}),, {} };";
  std::string Expected = "class A {\nA() : x({1}) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code));
}

TEST_F(CleanupTest, RedundantCommaNotInAffectedRanges) {
  std::string Code =
      "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  std::string Expected =
      "class A {\nA() : x({1}), /* comment */, { int x = 0; } };";
  // Set the affected range to be "int x = 0", which does not intercept the
  // constructor initialization list.
  std::vector<tooling::Range> Ranges(1, tooling::Range(42, 9));
  std::string Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);

  Code = "class A {\nA() : x(1), {} };";
  Expected = "class A {\nA() : x(1), {} };";
  // No range. Fixer should do nothing.
  Ranges.clear();
  Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);
}

TEST_F(CleanupTest, RemoveCommentsAroundDeleteCode) {
  std::string Code =
      "class A {\nA() : x({1}), /* comment */, /* comment */ {} };";
  std::string Expected = "class A {\nA() : x({1}) {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code));

  Code = "class A {\nA() : x({1}), // comment\n {} };";
  Expected = "class A {\nA() : x({1})\n {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code));

  Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };";
  Expected = "class A {\nA() : x({1}),  y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code));

  Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };";
  Expected = "class A {\nA() : x({1}), \n y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code));

  Code = "class A {\nA() : , // comment\n y(1),{} };";
  Expected = "class A {\nA() :  // comment\n y(1){} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code));

  Code = "class A {\nA() // comment\n : ,,{} };";
  Expected = "class A {\nA() // comment\n {} };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));

  Code = "class A {\nA() // comment\n : ,,=default; };";
  Expected = "class A {\nA() // comment\n =default; };";
  EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code));
}

TEST_F(CleanupTest, CtorInitializerInNamespace) {
  std::string Code = "namespace A {\n"
                     "namespace B {\n" // missing r_brace
                     "} // namespace A\n\n"
                     "namespace C {\n"
                     "class A { A() : x(0),, {} };\n"
                     "inline namespace E { namespace { } }\n"
                     "}";
  std::string Expected = "namespace A {\n"
                         "\n\n\nnamespace C {\n"
                         "class A { A() : x(0) {} };\n   \n"
                         "}";
  std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  std::string Result = cleanup(Code, Ranges);
  EXPECT_EQ(Expected, Result);
}

class CleanUpReplacementsTest : public ReplacementTest {
protected:
  tooling::Replacement createReplacement(unsigned Offset, unsigned Length,
                                         StringRef Text) {
    return tooling::Replacement(FileName, Offset, Length, Text);
  }

  tooling::Replacement createInsertion(StringRef IncludeDirective) {
    return createReplacement(UINT_MAX, 0, IncludeDirective);
  }

  tooling::Replacement createDeletion(StringRef HeaderName) {
    return createReplacement(UINT_MAX, 1, HeaderName);
  }

  inline std::string apply(StringRef Code,
                           const tooling::Replacements Replaces) {
    auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
    EXPECT_TRUE(static_cast<bool>(CleanReplaces))
        << llvm::toString(CleanReplaces.takeError()) << "\n";
    auto Result = applyAllReplacements(Code, *CleanReplaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  inline std::string formatAndApply(StringRef Code,
                                    const tooling::Replacements Replaces) {

    auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style);
    EXPECT_TRUE(static_cast<bool>(CleanReplaces))
        << llvm::toString(CleanReplaces.takeError()) << "\n";
    auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style);
    EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
        << llvm::toString(FormattedReplaces.takeError()) << "\n";
    auto Result = applyAllReplacements(Code, *FormattedReplaces);
    EXPECT_TRUE(static_cast<bool>(Result));
    return *Result;
  }

  int getOffset(StringRef Code, int Line, int Column) {
    RewriterTestContext Context;
    FileID ID = Context.createInMemoryFile(FileName, Code);
    auto DecomposedLocation =
        Context.Sources.getDecomposedLoc(Context.getLocation(ID, Line, Column));
    return DecomposedLocation.second;
  }

  const std::string FileName = "fix.cpp";
  FormatStyle Style = getLLVMStyle();
};

TEST_F(CleanUpReplacementsTest, FixOnlyAffectedCodeAfterReplacements) {
  std::string Code = "namespace A {\n"
                     "namespace B {\n"
                     "  int x;\n"
                     "} // namespace B\n"
                     "} // namespace A\n"
                     "\n"
                     "namespace C {\n"
                     "namespace D { int i; }\n"
                     "inline namespace E { namespace { int y; } }\n"
                     "int x=     0;"
                     "}";
  std::string Expected = "\n\nnamespace C {\n"
                         "namespace D { int i; }\n\n"
                         "int x=     0;"
                         "}";
  tooling::Replacements Replaces =
      toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""),
                      createReplacement(getOffset(Code, 9, 34), 6, "")});

  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithoutDefine) {
  std::string Code = "int main() {}";
  std::string Expected = "#include \"a.h\"\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoExistingIncludeWithDefine) {
  std::string Code = "#ifndef A_H\n"
                     "#define A_H\n"
                     "class A {};\n"
                     "#define MMM 123\n"
                     "#endif";
  std::string Expected = "#ifndef A_H\n"
                         "#define A_H\n"
                         "#include \"b.h\"\n"
                         "class A {};\n"
                         "#define MMM 123\n"
                         "#endif";

  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"b.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertBeforeCategoryWithLowerPriority) {
  std::string Code = "#ifndef A_H\n"
                     "#define A_H\n"
                     "\n"
                     "\n"
                     "\n"
                     "#include <vector>\n"
                     "class A {};\n"
                     "#define MMM 123\n"
                     "#endif";
  std::string Expected = "#ifndef A_H\n"
                         "#define A_H\n"
                         "\n"
                         "\n"
                         "\n"
                         "#include \"a.h\"\n"
                         "#include <vector>\n"
                         "class A {};\n"
                         "#define MMM 123\n"
                         "#endif";

  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertAfterMainHeader) {
  std::string Code = "#include \"fix.h\"\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include \"fix.h\"\n"
                         "#include <a>\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <a>")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertBeforeSystemHeaderLLVM) {
  std::string Code = "#include <memory>\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include \"z.h\"\n"
                         "#include <memory>\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"z.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertAfterSystemHeaderGoogle) {
  std::string Code = "#include <memory>\n"
                     "\n"
                     "int main() {}";
  std::string Expected = "#include <memory>\n"
                         "#include \"z.h\"\n"
                         "\n"
                         "int main() {}";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"z.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertOneIncludeLLVMStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n"
                     "#include \"clang/Format/Format.h\"\n"
                     "#include <memory>\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"d.h\"\n"
                         "#include \"clang/Format/Format.h\"\n"
                         "#include \"llvm/x/y.h\"\n"
                         "#include <memory>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"d.h\""),
                      createInsertion("#include \"llvm/x/y.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesLLVMStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n"
                     "#include \"clang/Format/Format.h\"\n"
                     "#include <memory>\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"new/new.h\"\n"
                         "#include \"clang/Format/Format.h\"\n"
                         "#include <memory>\n"
                         "#include <list>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <list>"),
                      createInsertion("#include \"new/new.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertNewSystemIncludeGoogleStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "\n"
                     "#include \"y/a.h\"\n"
                     "#include \"z/b.h\"\n";
  // FIXME: inserting after the empty line following the main header might be
  // prefered.
  std::string Expected = "#include \"x/fix.h\"\n"
                         "#include <vector>\n"
                         "\n"
                         "#include \"y/a.h\"\n"
                         "#include \"z/b.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleIncludesGoogleStyle) {
  std::string Code = "#include \"x/fix.h\"\n"
                     "\n"
                     "#include <vector>\n"
                     "\n"
                     "#include \"y/a.h\"\n"
                     "#include \"z/b.h\"\n";
  std::string Expected = "#include \"x/fix.h\"\n"
                         "\n"
                         "#include <vector>\n"
                         "#include <list>\n"
                         "\n"
                         "#include \"y/a.h\"\n"
                         "#include \"z/b.h\"\n"
                         "#include \"x/x.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <list>"),
                      createInsertion("#include \"x/x.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortLLVM) {
  std::string Code = "\nint x;";
  std::string Expected = "\n#include \"fix.h\"\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "int x;";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
       createInsertion("#include \"b.h\""),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"fix.h\"")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertMultipleNewHeadersAndSortGoogle) {
  std::string Code = "\nint x;";
  std::string Expected = "\n#include \"fix.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n"
                         "int x;";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"a.h\""), createInsertion("#include \"c.h\""),
       createInsertion("#include \"b.h\""),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"fix.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FormatCorrectLineWhenHeadersAreInserted) {
  std::string Code = "\n"
                     "int x;\n"
                     "int    a;\n"
                     "int    a;\n"
                     "int    a;";

  std::string Expected = "\n#include \"x.h\"\n"
                         "#include \"y.h\"\n"
                         "#include \"clang/x/x.h\"\n"
                         "#include <list>\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "int    a;\n"
                         "int b;\n"
                         "int    a;";
  tooling::Replacements Replaces = toReplacements(
      {createReplacement(getOffset(Code, 4, 8), 1, "b"),
       createInsertion("#include <vector>"), createInsertion("#include <list>"),
       createInsertion("#include \"clang/x/x.h\""),
       createInsertion("#include \"y.h\""),
       createInsertion("#include \"x.h\"")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NotConfusedByDefine) {
  std::string Code = "void f() {}\n"
                     "#define A \\\n"
                     "  int i;";
  std::string Expected = "#include <vector>\n"
                         "void f() {}\n"
                         "#define A \\\n"
                         "  int i;";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, formatAndApply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkippedTopComment) {
  std::string Code = "// comment\n"
                     "\n"
                     "   // comment\n";
  std::string Expected = "// comment\n"
                         "\n"
                         "   // comment\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkippedMixedComments) {
  std::string Code = "// comment\n"
                     "// comment \\\n"
                     " comment continued\n"
                     "/*\n"
                     "* comment\n"
                     "*/\n";
  std::string Expected = "// comment\n"
                         "// comment \\\n"
                         " comment continued\n"
                         "/*\n"
                         "* comment\n"
                         "*/\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, MultipleBlockCommentsInOneLine) {
  std::string Code = "/*\n"
                     "* comment\n"
                     "*/ /* comment\n"
                     "*/\n"
                     "\n\n"
                     "/* c1 */ /*c2 */\n";
  std::string Expected = "/*\n"
                         "* comment\n"
                         "*/ /* comment\n"
                         "*/\n"
                         "\n\n"
                         "/* c1 */ /*c2 */\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CodeAfterComments) {
  std::string Code = "/*\n"
                     "* comment\n"
                     "*/ /* comment\n"
                     "*/\n"
                     "\n\n"
                     "/* c1 */ /*c2 */\n"
                     "\n"
                     "int x;\n";
  std::string Expected = "/*\n"
                         "* comment\n"
                         "*/ /* comment\n"
                         "*/\n"
                         "\n\n"
                         "/* c1 */ /*c2 */\n"
                         "\n"
                         "#include <vector>\n"
                         "int x;\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FakeHeaderGuardIfDef) {
  std::string Code = "// comment \n"
                     "#ifdef X\n"
                     "#define X\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifdef X\n"
                         "#define X\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, RealHeaderGuardAfterComments) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "#define X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#ifndef X\n"
                         "#define X\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, IfNDefWithNoDefine) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifndef X\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, FakeHeaderGuard) {
  std::string Code = "// comment \n"
                     "#ifndef X\n"
                     "#define 1\n";
  std::string Expected = "// comment \n"
                         "#include <vector>\n"
                         "#ifndef X\n"
                         "#define 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, HeaderGuardWithComment) {
  std::string Code = "// comment \n"
                     "#ifndef X // comment\n"
                     "// comment\n"
                     "/* comment\n"
                     "*/\n"
                     "/* comment */ #define X\n"
                     "int x;\n"
                     "#define Y 1\n";
  std::string Expected = "// comment \n"
                         "#ifndef X // comment\n"
                         "// comment\n"
                         "/* comment\n"
                         "*/\n"
                         "/* comment */ #define X\n"
                         "#include <vector>\n"
                         "int x;\n"
                         "#define Y 1\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, EmptyCode) {
  std::string Code = "";
  std::string Expected = "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {
  std::string Code = "#include <map>";
  std::string Expected = "#include <map>\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCodeMultipleInsertions) {
  std::string Code = "#include <map>";
  std::string Expected =
      "#include <map>\n#include <string>\n#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <string>"),
                      createInsertion("#include <vector>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {
  std::string Code = "#include \"a.h\"\n"
                     "#include <vector>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include <vector>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <vector>"),
                      createInsertion("#include \"a.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {
  std::string Code = "#include \"a.h\"\n"
                     "#include <vector>\n";
  // FIXME: this might not be the best behavior.
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"vector\"\n"
                         "#include <vector>\n"
                         "#include <a.h>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"vector\""),
                      createInsertion("#include <a.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, SimpleDeleteIncludes) {
  std::string Code = "#include \"abc.h\"\n"
                     "#include \"xyz.h\" // comment\n"
                     "#include \"xyz\"\n"
                     "int x;\n";
  std::string Expected = "#include \"xyz\"\n"
                         "int x;\n";
  tooling::Replacements Replaces =
      toReplacements({createDeletion("abc.h"), createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, DeleteAllCode) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include <xyz.h>";
  std::string Expected = "";
  tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, DeleteAllIncludesWithSameNameIfNoType) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include \"xyz\"\n"
                     "#include <xyz.h>\n";
  std::string Expected = "#include \"xyz\"\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("xyz.h")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, OnlyDeleteHeaderWithType) {
  std::string Code = "#include \"xyz.h\"\n"
                     "#include \"xyz\"\n"
                     "#include <xyz.h>";
  std::string Expected = "#include \"xyz.h\"\n"
                         "#include \"xyz\"\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("<xyz.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, InsertionAndDeleteHeader) {
  std::string Code = "#include \"a.h\"\n"
                     "\n"
                     "#include <vector>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "\n"
                         "#include <map>\n";
  tooling::Replacements Replaces = toReplacements(
      {createDeletion("<vector>"), createInsertion("#include <map>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionAfterCode) {
  std::string Code = "#include \"a.h\"\n"
                     "void f() {}\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "void f() {}\n"
                         "#include \"b.h\"\n";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionInStringLiteral) {
  std::string Code = "#include \"a.h\"\n"
                     "const char[] = R\"(\n"
                     "#include \"b.h\"\n"
                     ")\";\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "const char[] = R\"(\n"
                         "#include \"b.h\"\n"
                         ")\";\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, NoInsertionAfterOtherDirective) {
  std::string Code = "#include \"a.h\"\n"
                     "#ifdef X\n"
                     "#include \"b.h\"\n"
                     "#endif\n";
  std::string Expected = "#include \"a.h\"\n"
                         "#include \"c.h\"\n"
                         "#ifdef X\n"
                         "#include \"b.h\"\n"
                         "#endif\n";
  tooling::Replacements Replaces = toReplacements(
      {createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanInsertAfterLongSystemInclude) {
  std::string Code = "#include \"a.h\"\n"
                     "// comment\n\n"
                     "#include <a/b/c/d/e.h>\n";
  std::string Expected = "#include \"a.h\"\n"
                         "// comment\n\n"
                         "#include <a/b/c/d/e.h>\n"
                         "#include <x.h>\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include <x.h>")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanInsertAfterComment) {
  std::string Code = "#include \"a.h\"\n"
                     "// Comment\n"
                     "\n"
                     "/* Comment */\n"
                     "// Comment\n"
                     "\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "// Comment\n"
                         "\n"
                         "/* Comment */\n"
                         "// Comment\n"
                         "\n"
                         "#include \"b.h\"\n"
                         "#include \"c.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"c.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, LongCommentsInTheBeginningOfFile) {
  std::string Code = "// Loooooooooooooooooooooooooong comment\n"
                     "// Loooooooooooooooooooooooooong comment\n"
                     "// Loooooooooooooooooooooooooong comment\n"
                     "#include <string>\n"
                     "#include <vector>\n"
                     "\n"
                     "#include \"a.h\"\n"
                     "#include \"b.h\"\n";
  std::string Expected = "// Loooooooooooooooooooooooooong comment\n"
                         "// Loooooooooooooooooooooooooong comment\n"
                         "// Loooooooooooooooooooooooooong comment\n"
                         "#include <string>\n"
                         "#include <vector>\n"
                         "\n"
                         "#include \"a.h\"\n"
                         "#include \"b.h\"\n"
                         "#include \"third.h\"\n";
  tooling::Replacements Replaces =
      toReplacements({createInsertion("#include \"third.h\"")});
  Style = format::getGoogleStyle(format::FormatStyle::LanguageKind::LK_Cpp);
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

TEST_F(CleanUpReplacementsTest, CanDeleteAfterCode) {
  std::string Code = "#include \"a.h\"\n"
                     "void f() {}\n"
                     "#include \"b.h\"\n";
  std::string Expected = "#include \"a.h\"\n"
                         "void f() {}\n";
  tooling::Replacements Replaces = toReplacements({createDeletion("\"b.h\"")});
  EXPECT_EQ(Expected, apply(Code, Replaces));
}

} // end namespace
} // end namespace format
} // end namespace clang
@


