Skip to content

Fix SourceKit/CursorInfo Tests on Windows #24171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions test/SourceKit/CursorInfo/cursor_symlink.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ln doesn't create a proper symlink on Windows
// XFAIL: windows
// RUN: %empty-directory(%t.dir)
// RUN: echo "let foo = 0" > %t.dir/real.swift
// RUN: ln -s %t.dir/real.swift %t.dir/linked.swift
Expand Down
24 changes: 17 additions & 7 deletions tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -848,19 +849,28 @@ std::string SwiftLangSupport::resolvePathSymlinks(StringRef FilePath) {

return InputPath;
#else
char full_path[MAX_PATH];
wchar_t full_path[MAX_PATH] = {0};
llvm::SmallVector<llvm::UTF16, 50> utf16Path;
llvm::convertUTF8ToUTF16String(InputPath.c_str(), utf16Path);

HANDLE fileHandle = CreateFileA(
InputPath.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
HANDLE fileHandle = CreateFileW(
(LPCWSTR)utf16Path.data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);

if (fileHandle == INVALID_HANDLE_VALUE)
return InputPath;

DWORD success = GetFinalPathNameByHandleA(
fileHandle, full_path, sizeof(full_path), FILE_NAME_NORMALIZED);
bool success = GetFinalPathNameByHandleW(fileHandle, full_path, MAX_PATH,
FILE_NAME_NORMALIZED);
CloseHandle(fileHandle);
return (success ? full_path : InputPath);
std::string utf8Path;
if (success) {
llvm::ArrayRef<char> pathRef((const char *)full_path,
(const char *)(full_path + MAX_PATH));
success &= llvm::convertUTF16ToUTF8String(pathRef, utf8Path);
}

return (success ? utf8Path : InputPath);
#endif
}

Expand Down
39 changes: 30 additions & 9 deletions tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@

#include "sourcekitd/sourcekitd.h"

#include "TestOptions.h"
#include "SourceKit/Support/Concurrency.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"
#include "TestOptions.h"
#include "swift/Demangling/ManglingMacros.h"
#include "clang/Rewrite/Core/RewriteBuffer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
Expand All @@ -48,11 +49,31 @@ namespace {
int STDOUT_FILENO = _fileno(stdout);
const constexpr size_t MAXPATHLEN = MAX_PATH + 1;
char *realpath(const char *path, char *resolved_path) {
DWORD dwLength = GetFullPathNameA(path, 0, nullptr, nullptr);
if (dwLength == 0)
wchar_t full_path[MAXPATHLEN] = {0};
llvm::SmallVector<llvm::UTF16, 50> utf16Path;
llvm::convertUTF8ToUTF16String(path, utf16Path);

HANDLE fileHandle = CreateFileW(
(LPCWSTR)utf16Path.data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);

if (fileHandle == INVALID_HANDLE_VALUE)
return nullptr;
if ((resolved_path = static_cast<char *>(malloc(dwLength + 1))))
GetFullPathNameA(path, dwLength, resolved_path, nullptr);
DWORD success = GetFinalPathNameByHandleW(fileHandle, full_path, MAX_PATH,
FILE_NAME_NORMALIZED);
CloseHandle(fileHandle);
if (!success) return nullptr;

std::string utf8Path;
llvm::ArrayRef<char> pathRef((const char *)full_path,
(const char *)(full_path + MAX_PATH));
if (!llvm::convertUTF16ToUTF8String(pathRef, utf8Path))
return nullptr;

if (!resolved_path) {
resolved_path = static_cast<char *>(malloc(utf8Path.length() + 1));
}
std::copy(std::begin(utf8Path), std::end(utf8Path), resolved_path);
return resolved_path;
}
}
Expand Down