Skip to content

Commit ff7e5f2

Browse files
authored
Merge pull request #1321 from apple/swift/tensorflow-merge
swift/tensorflow merge (2020-06-09)
2 parents 7a5fc24 + 55d27a5 commit ff7e5f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+895
-239
lines changed

lldb/include/lldb/Host/HostInfoBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class HostInfoBase {
9292
static bool ComputePathRelativeToLibrary(FileSpec &file_spec,
9393
llvm::StringRef dir);
9494

95+
static FileSpec GetXcodeContentsDirectory() { return {}; }
96+
static FileSpec GetXcodeDeveloperDirectory() { return {}; }
97+
9598
/// Return the directory containing a specific Xcode SDK.
9699
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk) { return {}; }
97100

lldb/include/lldb/Host/macosx/HostInfoMacOSX.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class HostInfoMacOSX : public HostInfoPosix {
3232
static bool GetOSBuildString(std::string &s);
3333
static bool GetOSKernelDescription(std::string &s);
3434
static FileSpec GetProgramFileSpec();
35-
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
35+
static FileSpec GetXcodeContentsDirectory();
36+
static FileSpec GetXcodeDeveloperDirectory();
3637

3738
/// Query xcrun to find an Xcode SDK directory.
3839
static llvm::StringRef GetXcodeSDKPath(XcodeSDK sdk);

lldb/include/lldb/Utility/XcodeSDK.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class XcodeSDK {
8787
static std::string GetCanonicalName(Info info);
8888
/// Return the best-matching SDK type for a specific triple.
8989
static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple);
90+
91+
static std::string FindXcodeContentsDirectoryInPath(llvm::StringRef path);
9092
};
9193

9294
} // namespace lldb_private

lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,82 @@ static void ParseOSVersion(llvm::VersionTuple &version, NSString *Key) {
297297
}
298298
}
299299

300+
FileSpec HostInfoMacOSX::GetXcodeContentsDirectory() {
301+
static FileSpec g_xcode_contents_path;
302+
static std::once_flag g_once_flag;
303+
std::call_once(g_once_flag, [&]() {
304+
// Try the shlib dir first.
305+
if (FileSpec fspec = HostInfo::GetShlibDir()) {
306+
if (FileSystem::Instance().Exists(fspec)) {
307+
std::string xcode_contents_dir =
308+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
309+
if (!xcode_contents_dir.empty()) {
310+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
311+
return;
312+
}
313+
}
314+
}
315+
316+
if (const char *developer_dir_env_var = getenv("DEVELOPER_DIR")) {
317+
FileSpec fspec(developer_dir_env_var);
318+
if (FileSystem::Instance().Exists(fspec)) {
319+
// FIXME: This looks like it couldn't possibly work!
320+
std::string xcode_contents_dir =
321+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
322+
if (!xcode_contents_dir.empty()) {
323+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
324+
return;
325+
}
326+
}
327+
}
328+
329+
FileSpec fspec(HostInfo::GetXcodeSDKPath(XcodeSDK::GetAnyMacOS()));
330+
if (fspec) {
331+
if (FileSystem::Instance().Exists(fspec)) {
332+
std::string xcode_contents_dir =
333+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath());
334+
if (!xcode_contents_dir.empty()) {
335+
g_xcode_contents_path = FileSpec(xcode_contents_dir);
336+
return;
337+
}
338+
}
339+
}
340+
});
341+
return g_xcode_contents_path;
342+
}
343+
344+
lldb_private::FileSpec HostInfoMacOSX::GetXcodeDeveloperDirectory() {
345+
static lldb_private::FileSpec g_developer_directory;
346+
static llvm::once_flag g_once_flag;
347+
llvm::call_once(g_once_flag, []() {
348+
if (FileSpec fspec = GetXcodeContentsDirectory()) {
349+
fspec.AppendPathComponent("Developer");
350+
if (FileSystem::Instance().Exists(fspec))
351+
g_developer_directory = fspec;
352+
}
353+
});
354+
return g_developer_directory;
355+
}
356+
300357
static std::string GetXcodeSDK(XcodeSDK sdk) {
301358
XcodeSDK::Info info = sdk.Parse();
302359
std::string sdk_name = XcodeSDK::GetCanonicalName(info);
303360
auto find_sdk = [](std::string sdk_name) -> std::string {
304-
std::string xcrun_cmd = "xcrun --show-sdk-path --sdk " + sdk_name;
361+
std::string xcrun_cmd;
362+
Environment env = Host::GetEnvironment();
363+
std::string developer_dir = env.lookup("DEVELOPER_DIR");
364+
if (developer_dir.empty())
365+
if (FileSpec fspec = HostInfo::GetShlibDir())
366+
if (FileSystem::Instance().Exists(fspec)) {
367+
FileSpec path(
368+
XcodeSDK::FindXcodeContentsDirectoryInPath(fspec.GetPath()));
369+
if (path.RemoveLastPathComponent())
370+
developer_dir = path.GetPath();
371+
}
372+
if (!developer_dir.empty())
373+
xcrun_cmd = "/usr/bin/env DEVELOPER_DIR=\"" + developer_dir + "\" ";
374+
xcrun_cmd += "xcrun --show-sdk-path --sdk " + sdk_name;
375+
305376
int status = 0;
306377
int signo = 0;
307378
std::string output_str;

lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -684,15 +684,8 @@ bool DynamicLoaderDarwin::AddModulesUsingImageInfos(
684684
// Update the module's platform with the DYLD info.
685685
ArchSpec dyld_spec = image_infos[idx].GetArchitecture();
686686
if (dyld_spec.GetTriple().getOS() == llvm::Triple::IOS &&
687-
dyld_spec.GetTriple().getEnvironment() == llvm::Triple::MacABI) {
687+
dyld_spec.GetTriple().getEnvironment() == llvm::Triple::MacABI)
688688
image_module_sp->MergeArchitecture(dyld_spec);
689-
const auto &target_triple = target.GetArchitecture().GetTriple();
690-
// If dyld reports the process as being loaded as MACCATALYST,
691-
// force-update the target's architecture to MACCATALYST.
692-
if (!(target_triple.getOS() == llvm::Triple::IOS &&
693-
target_triple.getEnvironment() == llvm::Triple::MacABI))
694-
target.SetArchitecture(dyld_spec);
695-
}
696689
}
697690
}
698691

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,41 @@ void SwiftASTManipulator::WrapExpression(
9090
__builtin_logger_initialize()
9191
)";
9292

93-
// The debug function declarations need only be declared once per session - on the first REPL call.
94-
// This code assumes that the first call is the first REPL call; don't call playground once then playground || repl again
93+
// The debug function declarations need only be declared once per session -
94+
// on the first REPL call. This code assumes that the first call is the
95+
// first REPL call; don't call playground once then playground || repl
96+
// again
9597
bool first_expression = options.GetPreparePlaygroundStubFunctions();
9698

97-
const char *playground_prefix = first_expression ? playground_logger_declarations : "";
99+
const char *playground_prefix =
100+
first_expression ? playground_logger_declarations : "";
98101

99102
if (pound_file && pound_line) {
100103
wrapped_stream.Printf("%s#sourceLocation(file: \"%s\", line: %u)\n%s\n",
101104
playground_prefix, pound_file, pound_line,
102105
orig_text);
103106
} else {
104-
// In 2017+, xcode playgrounds send orig_text that starts with a module loading prefix (not the above prefix), then a sourceLocation specifier that indicates the page name, and then the page body text.
105-
// The first_body_line mechanism in this function cannot be used to compensate for the playground_prefix added here, since it incorrectly continues to apply even after sourceLocation directives are read frmo the orig_text.
106-
// To make sure playgrounds work correctly whether or not they supply their own sourceLocation, create a dummy sourceLocation here with a fake filename that starts counting the first line of orig_text as line 1.
107+
// In 2017+, xcode playgrounds send orig_text that starts with a module
108+
// loading prefix (not the above prefix), then a sourceLocation specifier
109+
// that indicates the page name, and then the page body text. The
110+
// first_body_line mechanism in this function cannot be used to
111+
// compensate for the playground_prefix added here, since it incorrectly
112+
// continues to apply even after sourceLocation directives are read from
113+
// the orig_text. To make sure playgrounds work correctly whether or not
114+
// they supply their own sourceLocation, create a dummy sourceLocation
115+
// here with a fake filename that starts counting the first line of
116+
// orig_text as line 1.
107117
wrapped_stream.Printf("%s#sourceLocation(file: \"%s\", line: %u)\n%s\n",
108118
playground_prefix, "Playground.swift", 1,
109119
orig_text);
110120
}
111121
first_body_line = 1;
112122
return;
113-
} else if (repl) { // repl but not playground.
123+
}
124+
125+
assert(!playground && "Playground mode not expected");
126+
127+
if (repl) {
114128
if (pound_file && pound_line) {
115129
wrapped_stream.Printf("#sourceLocation(file: \"%s\", line: %u)\n%s\n",
116130
llvm::sys::path::filename(pound_file).str().c_str(),
@@ -122,87 +136,85 @@ __builtin_logger_initialize()
122136
return;
123137
}
124138

125-
std::string expr_source_path;
139+
assert(!playground && !repl && "Playground/REPL mode not expected");
126140

127141
if (pound_file && pound_line) {
128142
fixed_text.Printf("#sourceLocation(file: \"%s\", line: %u)\n%s\n",
129143
pound_file, pound_line, orig_text);
130144
text = fixed_text.GetString().data();
131145
} else if (generate_debug_info) {
146+
std::string expr_source_path;
132147
if (SwiftASTManipulator::SaveExpressionTextToTempFile(orig_text, options,
133-
expr_source_path)) {
148+
expr_source_path)) {
134149
fixed_text.Printf("#sourceLocation(file: \"%s\", line: 1)\n%s\n",
135150
expr_source_path.c_str(), orig_text);
136151
text = fixed_text.GetString().data();
137152
}
138153
}
139154

140155
// Note: All the wrapper functions we make are marked with the
141-
// @LLDBDebuggerFunction macro so that the compiler
142-
// can do whatever special treatment it need to do on them. If you add new
143-
// variants be sure to mark them this way.
144-
// Also, any function that might end up being in an extension of swift class
145-
// needs to be marked final, since otherwise
146-
// the compiler might try to dispatch them dynamically, which it can't do
147-
// correctly for these functions.
148-
149-
llvm::SmallString<32> buffer;
150-
llvm::raw_svector_ostream os(buffer);
156+
// @LLDBDebuggerFunction macro so that the compiler can do whatever special
157+
// treatment it need to do on them. If you add new variants be sure to mark
158+
// them this way. Also, any function that might end up being in an extension
159+
// of swift class needs to be marked final, since otherwise the compiler
160+
// might try to dispatch them dynamically, which it can't do correctly for
161+
// these functions.
162+
163+
std::string availability = "";
151164
if (!os_version.empty())
152-
os << "@available(" << os_version << ", *)";
153-
std::string availability = os.str();
165+
availability = (llvm::Twine("@available(") + os_version + ", *)").str();
154166

155167
StreamString wrapped_expr_text;
156-
wrapped_expr_text.Printf("do\n"
157-
"{\n"
158-
"%s%s%s\n" // Don't indent the code so error columns
159-
// match up with errors from compiler
160-
"}\n"
161-
"catch (let __lldb_tmp_error)\n"
162-
"{\n"
163-
" var %s = __lldb_tmp_error\n"
164-
"}\n",
168+
169+
// Avoid indenting user code: this makes column information from compiler
170+
// errors match up with what the user typed.
171+
wrapped_expr_text.Printf(R"(
172+
do {
173+
%s%s%s
174+
} catch (let __lldb_tmp_error) {
175+
var %s = __lldb_tmp_error
176+
}
177+
)",
165178
GetUserCodeStartMarker(), text,
166179
GetUserCodeEndMarker(), GetErrorName());
167180

168-
if (needs_object_ptr | static_method) {
181+
if (needs_object_ptr || static_method) {
169182
const char *func_decorator = "";
170183
if (static_method) {
171184
if (is_class)
172185
func_decorator = "final class";
173186
else
174187
func_decorator = "static";
175-
} else if (is_class &&
176-
!(weak_self)) {
188+
} else if (is_class && !weak_self) {
177189
func_decorator = "final";
178190
} else {
179191
func_decorator = "mutating";
180192
}
181193

182194
const char *optional_extension =
183-
(weak_self)
184-
? "Swift.Optional where Wrapped == "
185-
: "";
186-
187-
wrapped_stream.Printf(
188-
"extension %s$__lldb_context {\n"
189-
" @LLDBDebuggerFunction %s\n"
190-
" %s func $__lldb_wrapped_expr_%u(_ $__lldb_arg : "
191-
"UnsafeMutablePointer<Any>) {\n"
192-
"%s" // This is the expression text (with newlines).
193-
" }\n"
194-
"}\n"
195-
"%s\n"
196-
"func $__lldb_expr(_ $__lldb_arg : UnsafeMutablePointer<Any>) {\n"
197-
" do {\n"
198-
" $__lldb_injected_self.$__lldb_wrapped_expr_%u(\n"
199-
" $__lldb_arg\n"
200-
" )\n"
201-
" }\n"
202-
"}\n",
203-
optional_extension, availability.c_str(), func_decorator,
204-
current_counter, wrapped_expr_text.GetData(), availability.c_str(),
205-
current_counter);
195+
weak_self ? "Swift.Optional where Wrapped == " : "";
196+
197+
// The expression text is inserted into the body of $__lldb_wrapped_expr_%u.
198+
wrapped_stream.Printf(R"(
199+
extension %s$__lldb_context {
200+
@LLDBDebuggerFunction %s
201+
%s func $__lldb_wrapped_expr_%u(_ $__lldb_arg : UnsafeMutablePointer<Any>) {
202+
%s
203+
}
204+
}
205+
%s
206+
func $__lldb_expr(_ $__lldb_arg : UnsafeMutablePointer<Any>) {
207+
do {
208+
$__lldb_injected_self.$__lldb_wrapped_expr_%u(
209+
$__lldb_arg
210+
)
211+
}
212+
}
213+
)",
214+
optional_extension, availability.c_str(),
215+
func_decorator, current_counter,
216+
wrapped_expr_text.GetData(), availability.c_str(),
217+
current_counter);
206218

207219
first_body_line = 5;
208220
} else {

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,9 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12301230

12311231
// Create the source file. Note, we disable delayed parsing for the
12321232
// swift expression parser.
1233-
swift::SourceFile *source_file = new (*ast_context) swift::SourceFile(
1234-
module, source_file_kind, buffer_id, /*Keep tokens*/ false,
1235-
/*KeepSyntaxTree*/ false,
1236-
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
1233+
swift::SourceFile *source_file = new (*ast_context)
1234+
swift::SourceFile(module, source_file_kind, buffer_id,
1235+
swift::SourceFile::ParsingFlags::DisableDelayedBodies);
12371236
module.addFile(*source_file);
12381237

12391238
// Swift Modules that rely on shared libraries (not frameworks)

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,8 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
571571
repl_module = swift_ast->CreateModule(completion_module_info, error,
572572
importInfo);
573573
llvm::Optional<unsigned> bufferID;
574-
swift::SourceFile *repl_source_file = new (*ast)
575-
swift::SourceFile(*repl_module, swift::SourceFileKind::Main, bufferID,
576-
/*Keep tokens*/false);
574+
swift::SourceFile *repl_source_file = new (*ast) swift::SourceFile(
575+
*repl_module, swift::SourceFileKind::Main, bufferID);
577576
repl_module->addFile(*repl_source_file);
578577
swift::performImportResolution(*repl_source_file);
579578
m_completion_module_initialized = true;

0 commit comments

Comments
 (0)