Skip to content

Commit c970d17

Browse files
committed
Fix review comments
1 parent a696829 commit c970d17

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ llvm::json::Object CreateRunInTerminalReverseRequest(
889889
if (!stdio.empty()) {
890890
req_args.push_back("--stdio");
891891
std::stringstream ss;
892-
for (const auto &file : stdio) {
892+
for (const std::optional<std::string> &file : stdio) {
893893
if (file)
894894
ss << *file;
895895
ss << ":";

lldb/tools/lldb-dap/tool/lldb-dap.cpp

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lldb/Utility/UriParser.h"
2626
#include "lldb/lldb-forward.h"
2727
#include "llvm/ADT/ArrayRef.h"
28+
#include "llvm/ADT/DenseMap.h"
2829
#include "llvm/ADT/ScopeExit.h"
2930
#include "llvm/ADT/SmallVector.h"
3031
#include "llvm/ADT/StringExtras.h"
@@ -44,8 +45,10 @@
4445
#include "llvm/Support/WithColor.h"
4546
#include "llvm/Support/raw_ostream.h"
4647
#include <condition_variable>
48+
#include <cstddef>
4749
#include <cstdio>
4850
#include <cstdlib>
51+
#include <exception>
4952
#include <fcntl.h>
5053
#include <map>
5154
#include <memory>
@@ -146,52 +149,69 @@ static void PrintVersion() {
146149
}
147150

148151
#if not defined(_WIN32)
149-
static llvm::Error RedirectToFile(int fd, llvm::StringRef file, bool read,
150-
bool write) {
151-
if (!read && !write)
152+
struct FDGroup {
153+
int GetFlags() const {
154+
if (read && write)
155+
return O_NOCTTY | O_CREAT | O_RDWR;
156+
if (read)
157+
return O_NOCTTY | O_RDONLY;
158+
return O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
159+
}
160+
161+
std::vector<int> fds;
162+
bool read = false;
163+
bool write = false;
164+
};
165+
166+
static llvm::Error RedirectToFile(const FDGroup &fdg, llvm::StringRef file) {
167+
if (!fdg.read && !fdg.write)
152168
return llvm::Error::success();
153-
int flags = 0;
154-
if (read && write)
155-
flags = O_NOCTTY | O_CREAT | O_RDWR;
156-
else if (read)
157-
flags = O_NOCTTY | O_RDONLY;
158-
else
159-
flags = O_NOCTTY | O_CREAT | O_WRONLY | O_TRUNC;
160-
int target_fd = lldb_private::FileSystem::Instance().Open(file.str().c_str(),
161-
flags, 0666);
169+
int target_fd = lldb_private::FileSystem::Instance().Open(
170+
file.str().c_str(), fdg.GetFlags(), 0666);
162171
if (target_fd == -1)
163172
return llvm::errorCodeToError(
164173
std::error_code(errno, std::generic_category()));
165-
if (target_fd == fd)
166-
return llvm::Error::success();
167-
if (dup2(target_fd, fd) == -1)
168-
return llvm::errorCodeToError(
169-
std::error_code(errno, std::generic_category()));
170-
close(target_fd);
174+
for (int fd : fdg.fds) {
175+
if (target_fd == fd)
176+
continue;
177+
if (::dup2(target_fd, fd) == -1)
178+
return llvm::errorCodeToError(
179+
std::error_code(errno, std::generic_category()));
180+
}
181+
::close(target_fd);
171182
return llvm::Error::success();
172183
}
173184

174185
static llvm::Error
175186
SetupIORedirection(const llvm::SmallVectorImpl<llvm::StringRef> &files) {
176-
for (std::size_t i = 0; i < files.size(); i++) {
187+
llvm::SmallDenseMap<llvm::StringRef, FDGroup> groups;
188+
for (size_t i = 0; i < files.size(); i++) {
177189
if (files[i].empty())
178190
continue;
191+
auto group = groups.find(files[i]);
192+
if (group == groups.end())
193+
group = groups.insert({files[i], {{static_cast<int>(i)}}}).first;
194+
else
195+
group->second.fds.push_back(i);
179196
switch (i) {
180197
case 0:
181-
if (llvm::Error err = RedirectToFile(i, files[i], true, false))
182-
return err;
198+
group->second.read = true;
183199
break;
184200
case 1:
185201
case 2:
186-
if (llvm::Error err = RedirectToFile(i, files[i], false, true))
187-
return err;
202+
group->second.write = true;
188203
break;
189204
default:
190-
if (llvm::Error err = RedirectToFile(i, files[i], true, true))
191-
return err;
205+
group->second.read = true;
206+
group->second.write = true;
192207
break;
193208
}
194209
}
210+
for (const auto &[file, group] : groups) {
211+
if (llvm::Error err = RedirectToFile(group, file))
212+
return llvm::createStringError(
213+
llvm::formatv("{0}: {1}", file, llvm::toString(std::move(err))));
214+
}
195215
return llvm::Error::success();
196216
}
197217
#endif

0 commit comments

Comments
 (0)