summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp
diff options
context:
space:
mode:
authorGuillaume Papin <guillaume.papin@epitech.eu>2013-08-20 13:05:57 +0000
committerGuillaume Papin <guillaume.papin@epitech.eu>2013-08-20 13:05:57 +0000
commit1593c7b20a8a27121450b493a131f3bb297cbbb5 (patch)
tree38cdc9352f1acaca639868de30b18ac08ecfd890 /clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp
parent2b41a82e61977479bd0cb98706030e652c76e3a6 (diff)
downloadbcm5719-llvm-1593c7b20a8a27121450b493a131f3bb297cbbb5.tar.gz
bcm5719-llvm-1593c7b20a8a27121450b493a131f3bb297cbbb5.zip
Revert "cpp11-migrate: Add a class to support include directives modifications"
This reverts commit r188791. The Windows bots are still broken. llvm-svn: 188795
Diffstat (limited to 'clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp')
-rw-r--r--clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp377
1 files changed, 0 insertions, 377 deletions
diff --git a/clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp b/clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp
deleted file mode 100644
index 501092e6a44..00000000000
--- a/clang-tools-extra/unittests/cpp11-migrate/IncludeDirectivesTest.cpp
+++ /dev/null
@@ -1,377 +0,0 @@
-//===- cpp11-migrate/IncludeDirectivesTest.cpp ----------------------------===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "Core/IncludeDirectives.h"
-#include "gtest/gtest.h"
-#include "VirtualFileHelper.h"
-
-#include "clang/Frontend/CompilerInstance.h"
-#include "clang/Frontend/FrontendActions.h"
-
-using namespace llvm;
-using namespace clang;
-
-/// \brief A convenience method around \c tooling::runToolOnCodeWithArgs() that
-/// add the current directory to the include search paths.
-///
-/// The current directory './' is used when mapping virtual files as they aren't
-/// found otherwise. This function allows the tests code to refer to the include
-/// without the path component.
-static void applyActionOnCode(FrontendAction *ToolAction, StringRef Code) {
- std::vector<std::string> Args(1, "-I./");
-
- EXPECT_TRUE(tooling::runToolOnCodeWithArgs(ToolAction, Code, Args));
-}
-
-namespace {
-class TestAddIncludeAction : public PreprocessOnlyAction {
-public:
- TestAddIncludeAction(StringRef Include, tooling::Replacements &Replaces,
- const char *HeaderToModify = 0)
- : Include(Include), Replaces(Replaces), HeaderToModify(HeaderToModify) {
- // some headers that the tests can include
- addVirtualFile("./foo-inner.h", "#pragma once\n");
- addVirtualFile("./foo.h", "#pragma once\n"
- "#include <foo-inner.h>\n");
- addVirtualFile("./bar-inner.h", "#pragma once\n");
- addVirtualFile("./bar.h", "#pragma once\n"
- "#include <bar-inner.h>\n");
- addVirtualFile("./xmacro.def", "X(Val1)\n"
- "X(Val2)\n"
- "X(Val3)\n");
- }
-
- /// \brief Map additional virtual files.
- ///
- /// \warning Note that you probably want \p FileName to contains the prefix
- /// \c "./" so that it can be found by \c applyActionOnCode().
- void addVirtualFile(StringRef FileName, StringRef Content) {
- VFHelper.mapFile(FileName, Content);
- }
-
-private:
- virtual bool BeginSourceFileAction(CompilerInstance &CI,
- StringRef FileName) LLVM_OVERRIDE {
- if (!PreprocessOnlyAction::BeginSourceFileAction(CI, FileName))
- return false;
- VFHelper.mapVirtualFiles(CI.getSourceManager());
-
- FileToModify = HeaderToModify ? HeaderToModify : FileName;
- FileIncludes.reset(new IncludeDirectives(CI));
- return true;
- }
-
- virtual void EndSourceFileAction() LLVM_OVERRIDE {
- const tooling::Replacement &Replace =
- FileIncludes->addAngledInclude(FileToModify, Include);
- if (Replace.isApplicable())
- Replaces.insert(Replace);
- }
-
- StringRef Include;
- VirtualFileHelper VFHelper;
- tooling::Replacements &Replaces;
- OwningPtr<IncludeDirectives> FileIncludes;
- StringRef FileToModify;
- // if non-null, add the include directives in this file instead of the main
- // file.
- const char *HeaderToModify;
-};
-
-std::string addIncludeInCode(StringRef Include, StringRef Code) {
- tooling::Replacements Replaces;
-
- applyActionOnCode(new TestAddIncludeAction(Include, Replaces), Code);
- return tooling::applyAllReplacements(Code, Replaces);
-}
-} // end anonymous namespace
-
-TEST(IncludeDirectivesTest, endOfLinesVariants) {
- EXPECT_EQ("#include <foo.h>\n"
- "#include <bar>\n",
- addIncludeInCode("bar", "#include <foo.h>\n"));
- EXPECT_EQ("#include <foo.h>\r\n"
- "#include <bar>\r\n",
- addIncludeInCode("bar", "#include <foo.h>\r\n"));
- EXPECT_EQ("#include <foo.h>\r"
- "#include <bar>\r",
- addIncludeInCode("bar", "#include <foo.h>\r"));
-}
-
-TEST(IncludeDirectivesTest, ppToken) {
- EXPECT_EQ("#define FOO <foo.h>\n"
- "#include FOO\n"
- "#include <bar>\n"
- "int i;\n",
- addIncludeInCode("bar", "#define FOO <foo.h>\n"
- "#include FOO\n"
- "int i;\n"));
-}
-
-TEST(IncludeDirectivesTest, noFileHeader) {
- EXPECT_EQ("#include <bar>\n"
- "\n"
- "int foo;\n",
- addIncludeInCode("bar", "int foo;\n"));
-}
-
-TEST(IncludeDirectivesTest, commentBeforeTopMostCode) {
- EXPECT_EQ("#include <bar>\n"
- "\n"
- "// Foo\n"
- "int foo;\n",
- addIncludeInCode("bar", "// Foo\n"
- "int foo;\n"));
-}
-
-TEST(IncludeDirectivesTest, multiLineComment) {
- EXPECT_EQ("#include <foo.h> /* \n */\n"
- "#include <bar>\n",
- addIncludeInCode("bar", "#include <foo.h> /* \n */\n"));
- EXPECT_EQ("#include <foo.h> /* \n */"
- "\n#include <bar>",
- addIncludeInCode("bar", "#include <foo.h> /* \n */"));
-}
-
-TEST(IncludeDirectivesTest, multilineCommentWithTrailingSpace) {
- EXPECT_EQ("#include <foo.h> /*\n*/ \n"
- "#include <bar>\n",
- addIncludeInCode("bar", "#include <foo.h> /*\n*/ \n"));
- EXPECT_EQ("#include <foo.h> /*\n*/ "
- "\n#include <bar>",
- addIncludeInCode("bar", "#include <foo.h> /*\n*/ "));
-}
-
-TEST(IncludeDirectivesTest, fileHeaders) {
- EXPECT_EQ("// this is a header\n"
- "// some license stuff here\n"
- "\n"
- "#include <bar>\n"
- "\n"
- "/// \\brief Foo\n"
- "int foo;\n",
- addIncludeInCode("bar", "// this is a header\n"
- "// some license stuff here\n"
- "\n"
- "/// \\brief Foo\n"
- "int foo;\n"));
-}
-
-TEST(IncludeDirectivesTest, preferablyAngledNextToAngled) {
- EXPECT_EQ("#include <foo.h>\n"
- "#include <bar>\n"
- "#include \"bar.h\"\n",
- addIncludeInCode("bar", "#include <foo.h>\n"
- "#include \"bar.h\"\n"));
- EXPECT_EQ("#include \"foo.h\"\n"
- "#include \"bar.h\"\n"
- "#include <bar>\n",
- addIncludeInCode("bar", "#include \"foo.h\"\n"
- "#include \"bar.h\"\n"));
-}
-
-TEST(IncludeDirectivesTest, avoidDuplicates) {
- EXPECT_EQ("#include <foo.h>\n",
- addIncludeInCode("foo.h", "#include <foo.h>\n"));
-}
-
-// Tests includes in the middle of the code are ignored.
-TEST(IncludeDirectivesTest, ignoreHeadersMeantForMultipleInclusion) {
- std::string Expected = "#include \"foo.h\"\n"
- "#include <bar>\n"
- "\n"
- "enum Kind {\n"
- "#define X(A) K_##A,\n"
- "#include \"xmacro.def\"\n"
- "#undef X\n"
- " K_NUM_KINDS\n"
- "};\n";
- std::string Result =
- addIncludeInCode("bar", "#include \"foo.h\"\n"
- "\n"
- "enum Kind {\n"
- "#define X(A) K_##A,\n"
- "#include \"xmacro.def\"\n"
- "#undef X\n"
- " K_NUM_KINDS\n"
- "};\n");
- EXPECT_EQ(Expected, Result);
-}
-
-namespace {
-TestAddIncludeAction *makeIndirectTestsAction(const char *HeaderToModify,
- tooling::Replacements &Replaces) {
- StringRef IncludeToAdd = "c.h";
- TestAddIncludeAction *TestAction =
- new TestAddIncludeAction(IncludeToAdd, Replaces, HeaderToModify);
- TestAction->addVirtualFile("./c.h", "#pragma once\n");
- TestAction->addVirtualFile("./a.h", "#pragma once\n"
- "#include <c.h>\n");
- TestAction->addVirtualFile("./b.h", "#pragma once\n");
- return TestAction;
-}
-} // end anonymous namespace
-
-TEST(IncludeDirectivesTest, indirectIncludes) {
- // In TestAddIncludeAction 'foo.h' includes 'foo-inner.h'. Check that we
- // aren't including foo-inner.h again.
- EXPECT_EQ(
- "#include <foo.h>\n",
- addIncludeInCode("foo-inner.h", "#include <foo.h>\n"));
-
- tooling::Replacements Replaces;
- StringRef Code = "#include <a.h>\n"
- "#include <b.h>\n";
- std::vector<std::string> Args(1, "-I./");
-
- // a.h already includes c.h
- {
- FrontendAction *Action = makeIndirectTestsAction("./a.h", Replaces);
- EXPECT_TRUE(tooling::runToolOnCodeWithArgs(Action, Code, Args));
- EXPECT_EQ(unsigned(0), Replaces.size());
- }
-
- // c.h is included before b.h but b.h doesn't include c.h directly, so check
- // that it will be inserted.
- {
- FrontendAction *Action = makeIndirectTestsAction("./b.h", Replaces);
- EXPECT_TRUE(tooling::runToolOnCodeWithArgs(Action, Code, Args));
- EXPECT_EQ("#include <c.h>\n\n\n",
- tooling::applyAllReplacements("\n", Replaces));
- }
-}
-
-/// \brief Convenience method to test header guards detection implementation.
-static std::string addIncludeInGuardedHeader(StringRef IncludeToAdd,
- StringRef GuardedHeaderCode) {
- const char *GuardedHeaderName = "./guarded.h";
- tooling::Replacements Replaces;
- TestAddIncludeAction *TestAction =
- new TestAddIncludeAction(IncludeToAdd, Replaces, GuardedHeaderName);
- TestAction->addVirtualFile(GuardedHeaderName, GuardedHeaderCode);
- applyActionOnCode(TestAction, "#include <guarded.h>\n");
- return tooling::applyAllReplacements(GuardedHeaderCode, Replaces);
-}
-
-TEST(IncludeDirectivesTest, insertInsideIncludeGuard) {
- EXPECT_EQ("#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#include <foo>\n"
- "\n"
- "struct foo {};\n"
- "\n"
- "#endif // GUARD_H\n",
- addIncludeInGuardedHeader("foo", "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo {};\n"
- "\n"
- "#endif // GUARD_H\n"));
-}
-
-TEST(IncludeDirectivesTest, guardAndHeader) {
- EXPECT_EQ("// File header\n"
- "\n"
- "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#include <foo>\n"
- "\n"
- "struct foo {};\n"
- "\n"
- "#endif // GUARD_H\n",
- addIncludeInGuardedHeader("foo", "// File header\n"
- "\n"
- "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo {};\n"
- "\n"
- "#endif // GUARD_H\n"));
-}
-
-TEST(IncludeDirectivesTest, fullHeaderFitsAsAPreamble) {
- EXPECT_EQ("#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#include <foo>\n"
- "\n"
- "#define FOO 1\n"
- "\n"
- "#endif // GUARD_H\n",
- addIncludeInGuardedHeader("foo", "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#define FOO 1\n"
- "\n"
- "#endif // GUARD_H\n"));
-}
-
-TEST(IncludeDirectivesTest, codeBeforeIfndef) {
- EXPECT_EQ("#include <foo>\n"
- "\n"
- "int bar;\n"
- "\n"
- "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo;"
- "\n"
- "#endif // GUARD_H\n",
- addIncludeInGuardedHeader("foo", "int bar;\n"
- "\n"
- "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo;"
- "\n"
- "#endif // GUARD_H\n"));
-}
-
-TEST(IncludeDirectivesTest, codeAfterEndif) {
- EXPECT_EQ("#include <foo>\n"
- "\n"
- "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo;"
- "\n"
- "#endif // GUARD_H\n"
- "\n"
- "int bar;\n",
- addIncludeInGuardedHeader("foo", "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "struct foo;"
- "\n"
- "#endif // GUARD_H\n"
- "\n"
- "int bar;\n"));
-}
-
-TEST(IncludeDirectivesTest, headerGuardWithInclude) {
- EXPECT_EQ("#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#include <bar.h>\n"
- "#include <foo>\n"
- "\n"
- "struct foo;\n"
- "\n"
- "#endif // GUARD_H\n",
- addIncludeInGuardedHeader("foo", "#ifndef GUARD_H\n"
- "#define GUARD_H\n"
- "\n"
- "#include <bar.h>\n"
- "\n"
- "struct foo;\n"
- "\n"
- "#endif // GUARD_H\n"));
-}
OpenPOWER on IntegriCloud