summaryrefslogtreecommitdiff
path: root/unittests/Basic
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:01 +0000
committerDimitry Andric <dim@FreeBSD.org>2018-07-28 11:06:01 +0000
commit486754660bb926339aefcf012a3f848592babb8b (patch)
treeecdbc446c9876f4f120f701c243373cd3cb43db3 /unittests/Basic
parent55e6d896ad333f07bb3b1ba487df214fc268a4ab (diff)
Notes
Diffstat (limited to 'unittests/Basic')
-rw-r--r--unittests/Basic/CharInfoTest.cpp5
-rw-r--r--unittests/Basic/FileManagerTest.cpp17
-rw-r--r--unittests/Basic/VirtualFileSystemTest.cpp99
3 files changed, 101 insertions, 20 deletions
diff --git a/unittests/Basic/CharInfoTest.cpp b/unittests/Basic/CharInfoTest.cpp
index 348e6ffe2b7d1..7a9d17fce65dd 100644
--- a/unittests/Basic/CharInfoTest.cpp
+++ b/unittests/Basic/CharInfoTest.cpp
@@ -432,6 +432,7 @@ TEST(CharInfoTest, isValidIdentifier) {
EXPECT_TRUE(isValidIdentifier("z"));
EXPECT_TRUE(isValidIdentifier("A"));
EXPECT_TRUE(isValidIdentifier("Z"));
+ EXPECT_TRUE(isValidIdentifier("$", /*AllowDollar=*/true));
// 2 characters, '_' suffix
EXPECT_FALSE(isValidIdentifier("._"));
@@ -448,6 +449,7 @@ TEST(CharInfoTest, isValidIdentifier) {
EXPECT_TRUE(isValidIdentifier("z_"));
EXPECT_TRUE(isValidIdentifier("A_"));
EXPECT_TRUE(isValidIdentifier("Z_"));
+ EXPECT_TRUE(isValidIdentifier("$_", /*AllowDollar=*/true));
// 2 characters, '_' prefix
EXPECT_FALSE(isValidIdentifier("_."));
@@ -464,6 +466,7 @@ TEST(CharInfoTest, isValidIdentifier) {
EXPECT_TRUE(isValidIdentifier("_z"));
EXPECT_TRUE(isValidIdentifier("_A"));
EXPECT_TRUE(isValidIdentifier("_Z"));
+ EXPECT_TRUE(isValidIdentifier("_$", /*AllowDollar=*/true));
// 3 characters, '__' prefix
EXPECT_FALSE(isValidIdentifier("__."));
@@ -480,6 +483,7 @@ TEST(CharInfoTest, isValidIdentifier) {
EXPECT_TRUE(isValidIdentifier("__z"));
EXPECT_TRUE(isValidIdentifier("__A"));
EXPECT_TRUE(isValidIdentifier("__Z"));
+ EXPECT_TRUE(isValidIdentifier("__$", /*AllowDollar=*/true));
// 3 characters, '_' prefix and suffix
EXPECT_FALSE(isValidIdentifier("_._"));
@@ -496,4 +500,5 @@ TEST(CharInfoTest, isValidIdentifier) {
EXPECT_TRUE(isValidIdentifier("_z_"));
EXPECT_TRUE(isValidIdentifier("_A_"));
EXPECT_TRUE(isValidIdentifier("_Z_"));
+ EXPECT_TRUE(isValidIdentifier("_$_", /*AllowDollar=*/true));
}
diff --git a/unittests/Basic/FileManagerTest.cpp b/unittests/Basic/FileManagerTest.cpp
index a2a6c6aebe4b0..52cb5b2f0d650 100644
--- a/unittests/Basic/FileManagerTest.cpp
+++ b/unittests/Basic/FileManagerTest.cpp
@@ -12,7 +12,6 @@
#include "clang/Basic/FileSystemStatCache.h"
#include "clang/Basic/VirtualFileSystem.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Path.h"
#include "gtest/gtest.h"
@@ -31,7 +30,7 @@ private:
llvm::StringMap<FileData, llvm::BumpPtrAllocator> StatCalls;
void InjectFileOrDirectory(const char *Path, ino_t INode, bool IsFile) {
-#ifndef LLVM_ON_WIN32
+#ifndef _WIN32
SmallString<128> NormalizedPath(Path);
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
@@ -63,7 +62,7 @@ public:
LookupResult getStat(StringRef Path, FileData &Data, bool isFile,
std::unique_ptr<vfs::File> *F,
vfs::FileSystem &FS) override {
-#ifndef LLVM_ON_WIN32
+#ifndef _WIN32
SmallString<128> NormalizedPath(Path);
llvm::sys::path::native(NormalizedPath);
Path = NormalizedPath.c_str();
@@ -143,7 +142,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
statCache->InjectDirectory("/tmp", 42);
statCache->InjectFile("/tmp/test", 43);
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
const char *DirName = "C:.";
const char *FileName = "C:test";
statCache->InjectDirectory(DirName, 44);
@@ -161,7 +160,7 @@ TEST_F(FileManagerTest, getFileReturnsValidFileEntryForExistingRealFile) {
ASSERT_TRUE(dir != nullptr);
EXPECT_EQ("/tmp", dir->getName());
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
file = manager.getFile(FileName);
ASSERT_TRUE(file != NULL);
@@ -225,7 +224,7 @@ TEST_F(FileManagerTest, getFileReturnsNULLForNonexistentFile) {
// The following tests apply to Unix-like system only.
-#ifndef LLVM_ON_WIN32
+#ifndef _WIN32
// getFile() returns the same FileEntry for real files that are aliases.
TEST_F(FileManagerTest, getFileReturnsSameFileEntryForAliasedRealFiles) {
@@ -267,7 +266,7 @@ TEST_F(FileManagerTest, addRemoveStatCache) {
// getFile() Should return the same entry as getVirtualFile if the file actually
// is a virtual file, even if the name is not exactly the same (but is after
// normalisation done by the file system, like on Windows). This can be checked
-// here by checkng the size.
+// here by checking the size.
TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
// Inject fake files into the file system.
auto statCache = llvm::make_unique<FakeStatCache>();
@@ -295,11 +294,11 @@ TEST_F(FileManagerTest, getVirtualFileWithDifferentName) {
EXPECT_EQ(123, file2->getSize());
}
-#endif // !LLVM_ON_WIN32
+#endif // !_WIN32
TEST_F(FileManagerTest, makeAbsoluteUsesVFS) {
SmallString<64> CustomWorkingDir;
-#ifdef LLVM_ON_WIN32
+#ifdef _WIN32
CustomWorkingDir = "C:";
#else
CustomWorkingDir = "/";
diff --git a/unittests/Basic/VirtualFileSystemTest.cpp b/unittests/Basic/VirtualFileSystemTest.cpp
index f9efbeaee565b..c795be07acf1b 100644
--- a/unittests/Basic/VirtualFileSystemTest.cpp
+++ b/unittests/Basic/VirtualFileSystemTest.cpp
@@ -9,6 +9,7 @@
#include "clang/Basic/VirtualFileSystem.h"
#include "llvm/ADT/Triple.h"
+#include "llvm/Config/llvm-config.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -66,6 +67,21 @@ public:
std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
return std::error_code();
}
+ // Map any symlink to "/symlink".
+ std::error_code getRealPath(const Twine &Path,
+ SmallVectorImpl<char> &Output) const override {
+ auto I = FilesAndDirs.find(Path.str());
+ if (I == FilesAndDirs.end())
+ return make_error_code(llvm::errc::no_such_file_or_directory);
+ if (I->second.isSymlink()) {
+ Output.clear();
+ Twine("/symlink").toVector(Output);
+ return std::error_code();
+ }
+ Output.clear();
+ Path.toVector(Output);
+ return std::error_code();
+ }
struct DirIterImpl : public clang::vfs::detail::DirIterImpl {
std::map<std::string, vfs::Status> &FilesAndDirs;
@@ -195,6 +211,35 @@ TEST(VirtualFileSystemTest, BaseOnlyOverlay) {
EXPECT_TRUE(Status->equivalent(*Status2));
}
+TEST(VirtualFileSystemTest, GetRealPathInOverlay) {
+ IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
+ Lower->addRegularFile("/foo");
+ Lower->addSymlink("/lower_link");
+ IntrusiveRefCntPtr<DummyFileSystem> Upper(new DummyFileSystem());
+
+ IntrusiveRefCntPtr<vfs::OverlayFileSystem> O(
+ new vfs::OverlayFileSystem(Lower));
+ O->pushOverlay(Upper);
+
+ // Regular file.
+ SmallString<16> RealPath;
+ EXPECT_FALSE(O->getRealPath("/foo", RealPath));
+ EXPECT_EQ(RealPath.str(), "/foo");
+
+ // Expect no error getting real path for symlink in lower overlay.
+ EXPECT_FALSE(O->getRealPath("/lower_link", RealPath));
+ EXPECT_EQ(RealPath.str(), "/symlink");
+
+ // Try a non-existing link.
+ EXPECT_EQ(O->getRealPath("/upper_link", RealPath),
+ errc::no_such_file_or_directory);
+
+ // Add a new symlink in upper.
+ Upper->addSymlink("/upper_link");
+ EXPECT_FALSE(O->getRealPath("/upper_link", RealPath));
+ EXPECT_EQ(RealPath.str(), "/symlink");
+}
+
TEST(VirtualFileSystemTest, OverlayFiles) {
IntrusiveRefCntPtr<DummyFileSystem> Base(new DummyFileSystem());
IntrusiveRefCntPtr<DummyFileSystem> Middle(new DummyFileSystem());
@@ -442,16 +487,17 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
ScopedDir _dd(TestDirectory + "/d/d");
ScopedDir _ddd(TestDirectory + "/d/d/d");
ScopedLink _e("no_such_file", TestDirectory + "/e");
- std::vector<StringRef> Expected = {_b, _bb, _d, _dd, _ddd};
- std::vector<std::string> Contents;
+ std::vector<StringRef> ExpectedBrokenSymlinks = {_a, _ba, _bc, _c, _e};
+ std::vector<StringRef> ExpectedNonBrokenSymlinks = {_b, _bb, _d, _dd, _ddd};
+ std::vector<std::string> VisitedBrokenSymlinks;
+ std::vector<std::string> VisitedNonBrokenSymlinks;
std::error_code EC;
for (vfs::recursive_directory_iterator I(*FS, Twine(TestDirectory), EC), E;
I != E; I.increment(EC)) {
- // Skip broken symlinks.
auto EC2 = std::make_error_code(std::errc::no_such_file_or_directory);
if (EC == EC2) {
- EC.clear();
+ VisitedBrokenSymlinks.push_back(I->getName());
continue;
}
// For bot debugging.
@@ -467,13 +513,20 @@ TEST(VirtualFileSystemTest, BrokenSymlinkRealFSRecursiveIteration) {
<< "EC message: " << EC2.message() << "\n";
}
ASSERT_FALSE(EC);
- Contents.push_back(I->getName());
+ VisitedNonBrokenSymlinks.push_back(I->getName());
}
- // Check sorted contents.
- std::sort(Contents.begin(), Contents.end());
- EXPECT_EQ(Expected.size(), Contents.size());
- EXPECT_TRUE(std::equal(Contents.begin(), Contents.end(), Expected.begin()));
+ // Check visited file names.
+ std::sort(VisitedBrokenSymlinks.begin(), VisitedBrokenSymlinks.end());
+ std::sort(VisitedNonBrokenSymlinks.begin(), VisitedNonBrokenSymlinks.end());
+ EXPECT_EQ(ExpectedBrokenSymlinks.size(), VisitedBrokenSymlinks.size());
+ EXPECT_TRUE(std::equal(VisitedBrokenSymlinks.begin(),
+ VisitedBrokenSymlinks.end(),
+ ExpectedBrokenSymlinks.begin()));
+ EXPECT_EQ(ExpectedNonBrokenSymlinks.size(), VisitedNonBrokenSymlinks.size());
+ EXPECT_TRUE(std::equal(VisitedNonBrokenSymlinks.begin(),
+ VisitedNonBrokenSymlinks.end(),
+ ExpectedNonBrokenSymlinks.begin()));
}
#endif
@@ -488,8 +541,8 @@ static void checkContents(DirIter I, ArrayRef<StringRef> ExpectedOut) {
for (DirIter E; !EC && I != E; I.increment(EC))
InputToCheck.push_back(I->getName());
- std::sort(InputToCheck.begin(), InputToCheck.end());
- std::sort(Expected.begin(), Expected.end());
+ llvm::sort(InputToCheck.begin(), InputToCheck.end());
+ llvm::sort(Expected.begin(), Expected.end());
EXPECT_EQ(InputToCheck.size(), Expected.size());
unsigned LastElt = std::min(InputToCheck.size(), Expected.size());
@@ -760,6 +813,30 @@ TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
NormalizedFS.getCurrentWorkingDirectory().get()));
}
+#if !defined(_WIN32)
+TEST_F(InMemoryFileSystemTest, GetRealPath) {
+ SmallString<16> Path;
+ EXPECT_EQ(FS.getRealPath("b", Path), errc::operation_not_permitted);
+
+ auto GetRealPath = [this](StringRef P) {
+ SmallString<16> Output;
+ auto EC = FS.getRealPath(P, Output);
+ EXPECT_FALSE(EC);
+ return Output.str().str();
+ };
+
+ FS.setCurrentWorkingDirectory("a");
+ EXPECT_EQ(GetRealPath("b"), "a/b");
+ EXPECT_EQ(GetRealPath("../b"), "b");
+ EXPECT_EQ(GetRealPath("b/./c"), "a/b/c");
+
+ FS.setCurrentWorkingDirectory("/a");
+ EXPECT_EQ(GetRealPath("b"), "/a/b");
+ EXPECT_EQ(GetRealPath("../b"), "/b");
+ EXPECT_EQ(GetRealPath("b/./c"), "/a/b/c");
+}
+#endif // _WIN32
+
TEST_F(InMemoryFileSystemTest, AddFileWithUser) {
FS.addFile("/a/b/c", 0, MemoryBuffer::getMemBuffer("abc"), 0xFEEDFACE);
auto Stat = FS.status("/a");