Skip to content

[5.9.1] Enable macros for Windows in 5.9.1 #7540

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
Oct 3, 2023
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
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,13 @@ class FileManager : public RefCountedBase<FileManager> {
/// required, which is (almost) never.
StringRef getCanonicalName(const FileEntry *File);

private:
/// Retrieve the canonical name for a given file or directory.
///
/// The first param is a key in the CanonicalNames array.
StringRef getCanonicalName(const void *Entry, StringRef Name);

public:
void PrintStats() const;
};

Expand Down
56 changes: 35 additions & 21 deletions clang/lib/Basic/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,34 +651,48 @@ void FileManager::GetUniqueIDMapping(
}

StringRef FileManager::getCanonicalName(const DirectoryEntry *Dir) {
llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
= CanonicalNames.find(Dir);
if (Known != CanonicalNames.end())
return Known->second;

StringRef CanonicalName(Dir->getName());

SmallString<4096> CanonicalNameBuf;
if (!FS->getRealPath(Dir->getName(), CanonicalNameBuf))
CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);

CanonicalNames.insert({Dir, CanonicalName});
return CanonicalName;
return getCanonicalName(Dir, Dir->getName());
}

StringRef FileManager::getCanonicalName(const FileEntry *File) {
llvm::DenseMap<const void *, llvm::StringRef>::iterator Known
= CanonicalNames.find(File);
return getCanonicalName(File, File->getName());
}

StringRef FileManager::getCanonicalName(const void *Entry, StringRef Name) {
llvm::DenseMap<const void *, llvm::StringRef>::iterator Known =
CanonicalNames.find(Entry);
if (Known != CanonicalNames.end())
return Known->second;

StringRef CanonicalName(File->getName());

SmallString<4096> CanonicalNameBuf;
if (!FS->getRealPath(File->getName(), CanonicalNameBuf))
CanonicalName = CanonicalNameBuf.str().copy(CanonicalNameStorage);
// Name comes from FileEntry/DirectoryEntry::getName(), so it is safe to
// store it in the DenseMap below.
StringRef CanonicalName(Name);

SmallString<256> AbsPathBuf;
SmallString<256> RealPathBuf;
if (!FS->getRealPath(Name, RealPathBuf)) {
if (is_style_windows(llvm::sys::path::Style::native)) {
// For Windows paths, only use the real path if it doesn't resolve
// a substitute drive, as those are used to avoid MAX_PATH issues.
AbsPathBuf = Name;
if (!FS->makeAbsolute(AbsPathBuf)) {
if (llvm::sys::path::root_name(RealPathBuf) ==
llvm::sys::path::root_name(AbsPathBuf)) {
CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
} else {
// Fallback to using the absolute path.
// Simplifying /../ is semantically valid on Windows even in the
// presence of symbolic links.
llvm::sys::path::remove_dots(AbsPathBuf, /*remove_dot_dot=*/true);
CanonicalName = AbsPathBuf.str().copy(CanonicalNameStorage);
}
}
} else {
CanonicalName = RealPathBuf.str().copy(CanonicalNameStorage);
}
}

CanonicalNames.insert({File, CanonicalName});
CanonicalNames.insert({Entry, CanonicalName});
return CanonicalName;
}

Expand Down
4 changes: 1 addition & 3 deletions clang/lib/ExtractAPI/ExtractAPIConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ struct LocationFileChecker {
if (ExternalFileEntries.count(File))
return false;

StringRef FileName = File->tryGetRealPathName().empty()
? File->getName()
: File->tryGetRealPathName();
StringRef FileName = SM.getFileManager().getCanonicalName(File);

// Try to reduce the include name the same way we tried to include it.
bool IsQuoted = false;
Expand Down
8 changes: 7 additions & 1 deletion clang/test/Lexer/case-insensitive-include-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
// This file should only include code that really needs a Windows host OS to
// run.

// Note: We must use the real path here, because the logic to detect case
// mismatches must resolve the real path to figure out the original casing.
// If we use %t and we are on a substitute drive S: mapping to C:\subst,
// then we will compare "S:\test.dir\FOO.h" to "C:\subst\test.dir\foo.h"
// and avoid emitting the diagnostic because the structure is different.

// REQUIRES: system-windows
// RUN: mkdir -p %t.dir
// RUN: touch %t.dir/foo.h
// RUN: not %clang_cl /FI\\?\%t.dir\FOO.h /WX -fsyntax-only %s 2>&1 | FileCheck %s
// RUN: not %clang_cl /FI \\?\%{t:real}.dir\FOO.h /WX -fsyntax-only %s 2>&1 | FileCheck %s

// CHECK: non-portable path to file '"\\?\
16 changes: 13 additions & 3 deletions lldb/cmake/modules/AddLLDB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,27 @@ function(add_properties_for_swift_modules target reldir)
# Workaround for a linker crash related to autolinking: rdar://77839981
set_property(TARGET ${target} APPEND_STRING PROPERTY
LINK_FLAGS " -lobjc ")

set_property(TARGET ${target} APPEND PROPERTY BUILD_RPATH "${SWIFT_BUILD_RPATH}")
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${SWIFT_INSTALL_RPATH}")
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux|Android|OpenBSD|FreeBSD")
string(REGEX MATCH "^[^-]*" arch ${LLVM_TARGET_TRIPLE})
target_link_libraries(${target} PRIVATE swiftCore-linux-${arch})
string(TOLOWER ${CMAKE_SYSTEM_NAME} platform)
set(SWIFT_BUILD_RPATH "${LLDB_SWIFT_LIBS}/${platform}")
set(SWIFT_INSTALL_RPATH "$ORIGIN/${reldir}lib/swift/${platform}")
set_property(TARGET ${target} APPEND PROPERTY BUILD_RPATH "${SWIFT_BUILD_RPATH}")
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${SWIFT_INSTALL_RPATH}")
elseif(CMAKE_SYSTEM_NAME MATCHES Windows)
if(CMAKE_SYSTEM_PROCESSOR MATCHES AMD64|amd64|x86_64)
target_link_directories(${target} PRIVATE
${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/windows/x86_64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES ARM64|arm64|aarch64)
target_link_directories(${target} PRIVATE
${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/windows/aarch64)
endif()
endif()

set_property(TARGET ${target} APPEND PROPERTY BUILD_RPATH "${SWIFT_BUILD_RPATH}")
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${SWIFT_INSTALL_RPATH}")

if (SWIFT_SWIFT_PARSER)
if (CMAKE_SYSTEM_NAME MATCHES "Darwin")
set_property(TARGET ${target}
Expand Down
9 changes: 7 additions & 2 deletions llvm/cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1688,10 +1688,15 @@ endfunction()
# use it and can't be in a lit module. Use with make_paths_relative().
string(CONCAT LLVM_LIT_PATH_FUNCTION
"# Allow generated file to be relocatable.\n"
"from pathlib import Path\n"
"import os\n"
"import platform\n"
"def path(p):\n"
" if not p: return ''\n"
" return str((Path(__file__).parent / p).resolve())\n"
" # Follows lit.util.abs_path_preserve_drive, which cannot be imported here.\n"
" if platform.system() == 'Windows':\n"
" return os.path.abspath(os.path.join(os.path.dirname(__file__), p))\n"
" else:\n"
" return os.path.realpath(os.path.join(os.path.dirname(__file__), p))\n"
)

# This function provides an automatic way to 'configure'-like generate a file
Expand Down
10 changes: 10 additions & 0 deletions llvm/docs/CommandGuide/lit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ TestRunner.py:
%/p %p but ``\`` is replaced by ``/``
%/t %t but ``\`` is replaced by ``/``
%/T %T but ``\`` is replaced by ``/``
%{s:real} %s after expanding all symbolic links and substitute drives
%{S:real} %S after expanding all symbolic links and substitute drives
%{p:real} %p after expanding all symbolic links and substitute drives
%{t:real} %t after expanding all symbolic links and substitute drives
%{T:real} %T after expanding all symbolic links and substitute drives
%{/s:real} %/s after expanding all symbolic links and substitute drives
%{/S:real} %/S after expanding all symbolic links and substitute drives
%{/p:real} %/p after expanding all symbolic links and substitute drives
%{/t:real} %/t after expanding all symbolic links and substitute drives
%{/T:real} %/T after expanding all symbolic links and substitute drives
%{/s:regex_replacement} %/s but escaped for use in the replacement of a ``s@@@`` command in sed
%{/S:regex_replacement} %/S but escaped for use in the replacement of a ``s@@@`` command in sed
%{/p:regex_replacement} %/p but escaped for use in the replacement of a ``s@@@`` command in sed
Expand Down
14 changes: 12 additions & 2 deletions llvm/docs/TestingGuide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ RUN lines:
``${fs-sep}``
Expands to the file system separator, i.e. ``/`` or ``\`` on Windows.

``%/s, %/S, %/t, %/T:``
``%/s, %/S, %/t, %/T``

Act like the corresponding substitution above but replace any ``\``
character with a ``/``. This is useful to normalize path separators.
Expand All @@ -590,7 +590,17 @@ RUN lines:

Example: ``%/s: C:/Desktop Files/foo_test.s.tmp``

``%:s, %:S, %:t, %:T:``
``%{s:real}, %{S:real}, %{t:real}, %{T:real}``
``%{/s:real}, %{/S:real}, %{/t:real}, %{/T:real}``

Act like the corresponding substitution, including with ``/``, but use
the real path by expanding all symbolic links and substitute drives.

Example: ``%s: S:\foo_test.s.tmp``

Example: ``%{/s:real}: C:/SDrive/foo_test.s.tmp``

``%:s, %:S, %:t, %:T``

Act like the corresponding substitution above but remove colons at
the beginning of Windows paths. This is useful to allow concatenation
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/Support/DynamicLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class DynamicLibrary {
/// Returns true if the object refers to a valid library.
bool isValid() const { return Data != &Invalid; }

/// Return the OS specific handle value.
void *getOSSpecificHandle() const { return Data; }

/// Searches through the library for the symbol \p symbolName. If it is
/// found, the address of that symbol is returned. If not, NULL is returned.
/// Note that NULL will also be returned if the library failed to load.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ endif( MSVC OR MINGW )
# Delay load shell32.dll if possible to speed up process startup.
set (delayload_flags)
if (MSVC)
set (delayload_flags delayimp -delayload:shell32.dll -delayload:ole32.dll)
set (delayload_flags $<$<NOT:$<LINK_LANGUAGE:Swift>>:delayimp -delayload:shell32.dll -delayload:ole32.dll>)
endif()

# Link Z3 if the user wants to build it.
Expand Down
103 changes: 52 additions & 51 deletions llvm/utils/lit/lit/TestRunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def change_dir(self, newdir):
if os.path.isabs(newdir):
self.cwd = newdir
else:
self.cwd = os.path.realpath(os.path.join(self.cwd, newdir))
self.cwd = lit.util.abs_path_preserve_drive(os.path.join(self.cwd, newdir))

class TimeoutHelper(object):
"""
Expand Down Expand Up @@ -401,7 +401,7 @@ def executeBuiltinMkdir(cmd, cmd_shenv):
dir = to_unicode(dir) if kIsWindows else to_bytes(dir)
cwd = to_unicode(cwd) if kIsWindows else to_bytes(cwd)
if not os.path.isabs(dir):
dir = os.path.realpath(os.path.join(cwd, dir))
dir = lit.util.abs_path_preserve_drive(os.path.join(cwd, dir))
if parent:
lit.util.mkdir_p(dir)
else:
Expand Down Expand Up @@ -446,7 +446,7 @@ def on_rm_error(func, path, exc_info):
path = to_unicode(path) if kIsWindows else to_bytes(path)
cwd = to_unicode(cwd) if kIsWindows else to_bytes(cwd)
if not os.path.isabs(path):
path = os.path.realpath(os.path.join(cwd, path))
path = lit.util.abs_path_preserve_drive(os.path.join(cwd, path))
if force and not os.path.exists(path):
continue
try:
Expand Down Expand Up @@ -1153,58 +1153,59 @@ def getDefaultSubstitutions(test, tmpDir, tmpBase, normalize_slashes=False):
substitutions.extend(test.config.substitutions)
tmpName = tmpBase + '.tmp'
baseName = os.path.basename(tmpBase)
substitutions.extend([('%s', sourcepath),
('%S', sourcedir),
('%p', sourcedir),
('%{pathsep}', os.pathsep),
('%t', tmpName),
('%basename_t', baseName),
('%T', tmpDir)])

substitutions.extend([
('%{fs-src-root}', pathlib.Path(sourcedir).anchor),
('%{fs-tmp-root}', pathlib.Path(tmpBase).anchor),
('%{fs-sep}', os.path.sep),
])

# "%/[STpst]" should be normalized.
substitutions.extend([
('%/s', sourcepath.replace('\\', '/')),
('%/S', sourcedir.replace('\\', '/')),
('%/p', sourcedir.replace('\\', '/')),
('%/t', tmpBase.replace('\\', '/') + '.tmp'),
('%/T', tmpDir.replace('\\', '/')),
('%/et',tmpName.replace('\\', '\\\\\\\\\\\\\\\\')),
])

# "%{/[STpst]:regex_replacement}" should be normalized like "%/[STpst]" but we're
# also in a regex replacement context of a s@@@ regex.

substitutions.append(("%{pathsep}", os.pathsep))
substitutions.append(("%basename_t", baseName))

substitutions.extend(
[
("%{fs-src-root}", pathlib.Path(sourcedir).anchor),
("%{fs-tmp-root}", pathlib.Path(tmpBase).anchor),
("%{fs-sep}", os.path.sep),
]
)

substitutions.append(("%/et", tmpName.replace("\\", "\\\\\\\\\\\\\\\\")))

def regex_escape(s):
s = s.replace('@', r'\@')
s = s.replace('&', r'\&')
return s
substitutions.extend([
('%{/s:regex_replacement}',
regex_escape(sourcepath.replace('\\', '/'))),
('%{/S:regex_replacement}',
regex_escape(sourcedir.replace('\\', '/'))),
('%{/p:regex_replacement}',
regex_escape(sourcedir.replace('\\', '/'))),
('%{/t:regex_replacement}',
regex_escape(tmpBase.replace('\\', '/')) + '.tmp'),
('%{/T:regex_replacement}',
regex_escape(tmpDir.replace('\\', '/'))),
])

# "%:[STpst]" are normalized paths without colons and without a leading
# slash.
substitutions.extend([
('%:s', colonNormalizePath(sourcepath)),
('%:S', colonNormalizePath(sourcedir)),
('%:p', colonNormalizePath(sourcedir)),
('%:t', colonNormalizePath(tmpBase + '.tmp')),
('%:T', colonNormalizePath(tmpDir)),
])

path_substitutions = [
("s", sourcepath), ("S", sourcedir), ("p", sourcedir),
("t", tmpName), ("T", tmpDir)
]
for path_substitution in path_substitutions:
letter = path_substitution[0]
path = path_substitution[1]

# Original path variant
substitutions.append(("%" + letter, path))

# Normalized path separator variant
substitutions.append(("%/" + letter, path.replace("\\", "/")))

# realpath variants
# Windows paths with substitute drives are not expanded by default
# as they are used to avoid MAX_PATH issues, but sometimes we do
# need the fully expanded path.
real_path = os.path.realpath(path)
substitutions.append(("%{" + letter + ":real}", real_path))
substitutions.append(("%{/" + letter + ":real}",
real_path.replace("\\", "/")))

# "%{/[STpst]:regex_replacement}" should be normalized like
# "%/[STpst]" but we're also in a regex replacement context
# of a s@@@ regex.
substitutions.append(
("%{/" + letter + ":regex_replacement}",
regex_escape(path.replace("\\", "/"))))

# "%:[STpst]" are normalized paths without colons and without
# a leading slash.
substitutions.append(("%:" + letter, colonNormalizePath(path)))

return substitutions

def _memoize(f):
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/lit/lit/builtin_commands/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def main(argv):
try:
for file in args:
if file != "-" and not os.path.isabs(file):
file = os.path.realpath(os.path.join(os.getcwd(), file))
file = util.abs_path_preserve_drive(file)

if flags.recursive_diff:
if file == "-":
Expand Down
Loading