summaryrefslogtreecommitdiff
path: root/unittests/Frontend
diff options
context:
space:
mode:
authorRaphael Isemann <teemperor@gmail.com>2017-09-12 16:54:53 +0000
committerRaphael Isemann <teemperor@gmail.com>2017-09-12 16:54:53 +0000
commit7b30399eefaa0491f80963acf3ba44468e5c71c2 (patch)
tree380334e81e6db9badfac557781cb1f5bdaa7ca4d /unittests/Frontend
parent995f76732604b0527735026439ea396312e3de38 (diff)
downloadclang-7b30399eefaa0491f80963acf3ba44468e5c71c2.tar.gz
Use the VFS from the CompilerInvocation by default
Summary: The CompilerInstance should create its default VFS from its CompilerInvocation. Right now the user has to manually create the VFS before creating the FileManager even though `-ivfsoverlay file.yaml` was passed via the CompilerInvocation (which is exactly how we worked around this issue in `FrontendAction.cpp` so far). This patch uses the invocation's VFS by default and also tests this behavior now from the point of view of a program that uses the clang API. Reviewers: benlangmuir, v.g.vassilev Reviewed By: v.g.vassilev Subscribers: mgorny, cfe-commits, v.g.vassilev Differential Revision: https://reviews.llvm.org/D37416 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@313049 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Frontend')
-rw-r--r--unittests/Frontend/CMakeLists.txt1
-rw-r--r--unittests/Frontend/CompilerInstanceTest.cpp74
2 files changed, 75 insertions, 0 deletions
diff --git a/unittests/Frontend/CMakeLists.txt b/unittests/Frontend/CMakeLists.txt
index 81a98280fd..e3a21f57ba 100644
--- a/unittests/Frontend/CMakeLists.txt
+++ b/unittests/Frontend/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
add_clang_unittest(FrontendTests
ASTUnitTest.cpp
+ CompilerInstanceTest.cpp
FrontendActionTest.cpp
CodeGenActionTest.cpp
PCHPreambleTest.cpp
diff --git a/unittests/Frontend/CompilerInstanceTest.cpp b/unittests/Frontend/CompilerInstanceTest.cpp
new file mode 100644
index 0000000000..30775bc3f7
--- /dev/null
+++ b/unittests/Frontend/CompilerInstanceTest.cpp
@@ -0,0 +1,74 @@
+//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+
+TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
+ // Create a temporary VFS overlay yaml file.
+ int FD;
+ SmallString<256> FileName;
+ ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName));
+ tool_output_file File(FileName, FD);
+
+ SmallString<256> CurrentPath;
+ sys::fs::current_path(CurrentPath);
+ sys::fs::make_absolute(CurrentPath, FileName);
+
+ // Mount the VFS file itself on the path 'virtual.file'. Makes this test
+ // a bit shorter than creating a new dummy file just for this purpose.
+ const std::string CurrentPathStr = CurrentPath.str();
+ const std::string FileNameStr = FileName.str();
+ const char *VFSYaml = "{ 'version': 0, 'roots': [\n"
+ " { 'name': '%s',\n"
+ " 'type': 'directory',\n"
+ " 'contents': [\n"
+ " { 'name': 'vfs-virtual.file', 'type': 'file',\n"
+ " 'external-contents': '%s'\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "]}\n";
+ File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str());
+ File.os().flush();
+
+ // Create a CompilerInvocation that uses this overlay file.
+ const std::string VFSArg = "-ivfsoverlay" + FileNameStr;
+ const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"};
+
+ IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
+ CompilerInstance::createDiagnostics(new DiagnosticOptions());
+
+ std::shared_ptr<CompilerInvocation> CInvok =
+ createInvocationFromCommandLine(Args, Diags);
+
+ if (!CInvok)
+ FAIL() << "could not create compiler invocation";
+ // Create a minimal CompilerInstance which should use the VFS we specified
+ // in the CompilerInvocation (as we don't explicitly set our own).
+ CompilerInstance Instance;
+ Instance.setDiagnostics(Diags.get());
+ Instance.setInvocation(CInvok);
+ Instance.createFileManager();
+
+ // Check if the virtual file exists which means that our VFS is used by the
+ // CompilerInstance.
+ ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file"));
+}
+
+} // anonymous namespace