Skip to content

Commit cac35ca

Browse files
authored
Merge pull request #24171 from gmittert/SourceKitTests
Fix SourceKit/CursorInfo Tests on Windows
2 parents 42092c5 + 7da91d5 commit cac35ca

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

test/SourceKit/CursorInfo/cursor_symlink.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ln doesn't create a proper symlink on Windows
2+
// XFAIL: windows
13
// RUN: %empty-directory(%t.dir)
24
// RUN: echo "let foo = 0" > %t.dir/real.swift
35
// RUN: ln -s %t.dir/real.swift %t.dir/linked.swift

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "clang/Lex/Preprocessor.h"
3232
#include "llvm/ADT/APInt.h"
3333
#include "llvm/ADT/Hashing.h"
34+
#include "llvm/Support/ConvertUTF.h"
3435
#include "llvm/Support/ErrorHandling.h"
3536
#include "llvm/Support/Path.h"
3637
#include "llvm/Support/raw_ostream.h"
@@ -849,19 +850,28 @@ std::string SwiftLangSupport::resolvePathSymlinks(StringRef FilePath) {
849850

850851
return InputPath;
851852
#else
852-
char full_path[MAX_PATH];
853+
wchar_t full_path[MAX_PATH] = {0};
854+
llvm::SmallVector<llvm::UTF16, 50> utf16Path;
855+
llvm::convertUTF8ToUTF16String(InputPath.c_str(), utf16Path);
853856

854-
HANDLE fileHandle = CreateFileA(
855-
InputPath.c_str(), GENERIC_READ, 0, nullptr, OPEN_EXISTING,
856-
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
857+
HANDLE fileHandle = CreateFileW(
858+
(LPCWSTR)utf16Path.data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
859+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
857860

858861
if (fileHandle == INVALID_HANDLE_VALUE)
859862
return InputPath;
860863

861-
DWORD success = GetFinalPathNameByHandleA(
862-
fileHandle, full_path, sizeof(full_path), FILE_NAME_NORMALIZED);
864+
bool success = GetFinalPathNameByHandleW(fileHandle, full_path, MAX_PATH,
865+
FILE_NAME_NORMALIZED);
863866
CloseHandle(fileHandle);
864-
return (success ? full_path : InputPath);
867+
std::string utf8Path;
868+
if (success) {
869+
llvm::ArrayRef<char> pathRef((const char *)full_path,
870+
(const char *)(full_path + MAX_PATH));
871+
success &= llvm::convertUTF16ToUTF8String(pathRef, utf8Path);
872+
}
873+
874+
return (success ? utf8Path : InputPath);
865875
#endif
866876
}
867877

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@
1212

1313
#include "sourcekitd/sourcekitd.h"
1414

15-
#include "TestOptions.h"
1615
#include "SourceKit/Support/Concurrency.h"
17-
#include "clang/Rewrite/Core/RewriteBuffer.h"
16+
#include "TestOptions.h"
1817
#include "swift/Demangling/ManglingMacros.h"
18+
#include "clang/Rewrite/Core/RewriteBuffer.h"
1919
#include "llvm/ADT/ArrayRef.h"
2020
#include "llvm/ADT/Optional.h"
2121
#include "llvm/ADT/StringSwitch.h"
2222
#include "llvm/Support/CommandLine.h"
23+
#include "llvm/Support/ConvertUTF.h"
24+
#include "llvm/Support/FileSystem.h"
25+
#include "llvm/Support/FormatVariadic.h"
2326
#include "llvm/Support/MemoryBuffer.h"
2427
#include "llvm/Support/Regex.h"
25-
#include "llvm/Support/raw_ostream.h"
2628
#include "llvm/Support/Signals.h"
27-
#include "llvm/Support/FileSystem.h"
28-
#include "llvm/Support/FormatVariadic.h"
29+
#include "llvm/Support/raw_ostream.h"
2930
#include <fstream>
3031
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
3132
#include <unistd.h>
@@ -48,11 +49,31 @@ namespace {
4849
int STDOUT_FILENO = _fileno(stdout);
4950
const constexpr size_t MAXPATHLEN = MAX_PATH + 1;
5051
char *realpath(const char *path, char *resolved_path) {
51-
DWORD dwLength = GetFullPathNameA(path, 0, nullptr, nullptr);
52-
if (dwLength == 0)
52+
wchar_t full_path[MAXPATHLEN] = {0};
53+
llvm::SmallVector<llvm::UTF16, 50> utf16Path;
54+
llvm::convertUTF8ToUTF16String(path, utf16Path);
55+
56+
HANDLE fileHandle = CreateFileW(
57+
(LPCWSTR)utf16Path.data(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
58+
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
59+
60+
if (fileHandle == INVALID_HANDLE_VALUE)
5361
return nullptr;
54-
if ((resolved_path = static_cast<char *>(malloc(dwLength + 1))))
55-
GetFullPathNameA(path, dwLength, resolved_path, nullptr);
62+
DWORD success = GetFinalPathNameByHandleW(fileHandle, full_path, MAX_PATH,
63+
FILE_NAME_NORMALIZED);
64+
CloseHandle(fileHandle);
65+
if (!success) return nullptr;
66+
67+
std::string utf8Path;
68+
llvm::ArrayRef<char> pathRef((const char *)full_path,
69+
(const char *)(full_path + MAX_PATH));
70+
if (!llvm::convertUTF16ToUTF8String(pathRef, utf8Path))
71+
return nullptr;
72+
73+
if (!resolved_path) {
74+
resolved_path = static_cast<char *>(malloc(utf8Path.length() + 1));
75+
}
76+
std::copy(std::begin(utf8Path), std::end(utf8Path), resolved_path);
5677
return resolved_path;
5778
}
5879
}

0 commit comments

Comments
 (0)