Skip to content

Commit 6dcf51a

Browse files
committed
Auto merge of #38701 - karpinski:rustllvm-style, r=brson
Making code style consistent for src/rustllvm (#38688) Part of #38688 r? @brson
2 parents 6c9bb42 + 72ebc02 commit 6dcf51a

File tree

4 files changed

+1306
-1572
lines changed

4 files changed

+1306
-1572
lines changed

src/rustllvm/ArchiveWrapper.cpp

+138-145
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,51 @@ struct RustArchiveMember {
2121
const char *name;
2222
Archive::Child child;
2323

24-
RustArchiveMember(): filename(NULL), name(NULL),
24+
RustArchiveMember()
25+
: filename(nullptr), name(nullptr),
2526
#if LLVM_VERSION_GE(3, 8)
26-
child(NULL, NULL, NULL)
27+
child(nullptr, nullptr, nullptr)
2728
#else
28-
child(NULL, NULL)
29+
child(nullptr, nullptr)
2930
#endif
30-
{}
31+
{
32+
}
3133
~RustArchiveMember() {}
3234
};
3335

34-
3536
struct RustArchiveIterator {
36-
bool first;
37-
Archive::child_iterator cur;
38-
Archive::child_iterator end;
37+
bool first;
38+
Archive::child_iterator cur;
39+
Archive::child_iterator end;
3940
#if LLVM_VERSION_GE(3, 9)
40-
Error err;
41+
Error err;
4142

42-
RustArchiveIterator() : first(true), err(Error::success()) { }
43+
RustArchiveIterator() : first(true), err(Error::success()) {}
4344
#else
44-
RustArchiveIterator() : first(true) { }
45+
RustArchiveIterator() : first(true) {}
4546
#endif
4647
};
4748

4849
enum class LLVMRustArchiveKind {
49-
Other,
50-
GNU,
51-
MIPS64,
52-
BSD,
53-
COFF,
50+
Other,
51+
GNU,
52+
MIPS64,
53+
BSD,
54+
COFF,
5455
};
5556

56-
static Archive::Kind
57-
from_rust(LLVMRustArchiveKind kind)
58-
{
59-
switch (kind) {
60-
case LLVMRustArchiveKind::GNU:
61-
return Archive::K_GNU;
62-
case LLVMRustArchiveKind::MIPS64:
63-
return Archive::K_MIPS64;
64-
case LLVMRustArchiveKind::BSD:
65-
return Archive::K_BSD;
66-
case LLVMRustArchiveKind::COFF:
67-
return Archive::K_COFF;
68-
default:
69-
llvm_unreachable("Bad ArchiveKind.");
57+
static Archive::Kind from_rust(LLVMRustArchiveKind kind) {
58+
switch (kind) {
59+
case LLVMRustArchiveKind::GNU:
60+
return Archive::K_GNU;
61+
case LLVMRustArchiveKind::MIPS64:
62+
return Archive::K_MIPS64;
63+
case LLVMRustArchiveKind::BSD:
64+
return Archive::K_BSD;
65+
case LLVMRustArchiveKind::COFF:
66+
return Archive::K_COFF;
67+
default:
68+
llvm_unreachable("Bad ArchiveKind.");
7069
}
7170
}
7271

@@ -76,174 +75,166 @@ typedef Archive::Child *LLVMRustArchiveChildRef;
7675
typedef Archive::Child const *LLVMRustArchiveChildConstRef;
7776
typedef RustArchiveIterator *LLVMRustArchiveIteratorRef;
7877

79-
extern "C" LLVMRustArchiveRef
80-
LLVMRustOpenArchive(char *path) {
81-
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or = MemoryBuffer::getFile(path,
82-
-1,
83-
false);
84-
if (!buf_or) {
85-
LLVMRustSetLastError(buf_or.getError().message().c_str());
86-
return nullptr;
87-
}
78+
extern "C" LLVMRustArchiveRef LLVMRustOpenArchive(char *path) {
79+
ErrorOr<std::unique_ptr<MemoryBuffer>> buf_or =
80+
MemoryBuffer::getFile(path, -1, false);
81+
if (!buf_or) {
82+
LLVMRustSetLastError(buf_or.getError().message().c_str());
83+
return nullptr;
84+
}
8885

8986
#if LLVM_VERSION_LE(3, 8)
90-
ErrorOr<std::unique_ptr<Archive>> archive_or =
87+
ErrorOr<std::unique_ptr<Archive>> archive_or =
9188
#else
92-
Expected<std::unique_ptr<Archive>> archive_or =
89+
Expected<std::unique_ptr<Archive>> archive_or =
9390
#endif
94-
Archive::create(buf_or.get()->getMemBufferRef());
91+
Archive::create(buf_or.get()->getMemBufferRef());
9592

96-
if (!archive_or) {
93+
if (!archive_or) {
9794
#if LLVM_VERSION_LE(3, 8)
98-
LLVMRustSetLastError(archive_or.getError().message().c_str());
95+
LLVMRustSetLastError(archive_or.getError().message().c_str());
9996
#else
100-
LLVMRustSetLastError(toString(archive_or.takeError()).c_str());
97+
LLVMRustSetLastError(toString(archive_or.takeError()).c_str());
10198
#endif
102-
return nullptr;
103-
}
99+
return nullptr;
100+
}
104101

105-
OwningBinary<Archive> *ret = new OwningBinary<Archive>(
106-
std::move(archive_or.get()), std::move(buf_or.get()));
102+
OwningBinary<Archive> *ret = new OwningBinary<Archive>(
103+
std::move(archive_or.get()), std::move(buf_or.get()));
107104

108-
return ret;
105+
return ret;
109106
}
110107

111-
extern "C" void
112-
LLVMRustDestroyArchive(LLVMRustArchiveRef ar) {
113-
delete ar;
114-
}
108+
extern "C" void LLVMRustDestroyArchive(LLVMRustArchiveRef ar) { delete ar; }
115109

116110
extern "C" LLVMRustArchiveIteratorRef
117111
LLVMRustArchiveIteratorNew(LLVMRustArchiveRef ra) {
118-
Archive *ar = ra->getBinary();
119-
RustArchiveIterator *rai = new RustArchiveIterator();
112+
Archive *ar = ra->getBinary();
113+
RustArchiveIterator *rai = new RustArchiveIterator();
120114
#if LLVM_VERSION_LE(3, 8)
121-
rai->cur = ar->child_begin();
115+
rai->cur = ar->child_begin();
122116
#else
123-
rai->cur = ar->child_begin(rai->err);
124-
if (rai->err) {
125-
LLVMRustSetLastError(toString(std::move(rai->err)).c_str());
126-
delete rai;
127-
return NULL;
128-
}
117+
rai->cur = ar->child_begin(rai->err);
118+
if (rai->err) {
119+
LLVMRustSetLastError(toString(std::move(rai->err)).c_str());
120+
delete rai;
121+
return nullptr;
122+
}
129123
#endif
130-
rai->end = ar->child_end();
131-
return rai;
124+
rai->end = ar->child_end();
125+
return rai;
132126
}
133127

134128
extern "C" LLVMRustArchiveChildConstRef
135129
LLVMRustArchiveIteratorNext(LLVMRustArchiveIteratorRef rai) {
136-
if (rai->cur == rai->end) return nullptr;
137-
138-
// Advancing the iterator validates the next child, and this can
139-
// uncover an error. LLVM requires that we check all Errors,
140-
// so we only advance the iterator if we actually need to fetch
141-
// the next child.
142-
// This means we must not advance the iterator in the *first* call,
143-
// but instead advance it *before* fetching the child in all later calls.
144-
if (!rai->first) {
145-
++rai->cur;
130+
if (rai->cur == rai->end)
131+
return nullptr;
132+
133+
// Advancing the iterator validates the next child, and this can
134+
// uncover an error. LLVM requires that we check all Errors,
135+
// so we only advance the iterator if we actually need to fetch
136+
// the next child.
137+
// This means we must not advance the iterator in the *first* call,
138+
// but instead advance it *before* fetching the child in all later calls.
139+
if (!rai->first) {
140+
++rai->cur;
146141
#if LLVM_VERSION_GE(3, 9)
147-
if (rai->err) {
148-
LLVMRustSetLastError(toString(std::move(rai->err)).c_str());
149-
return nullptr;
150-
}
151-
#endif
152-
} else {
153-
rai->first = false;
142+
if (rai->err) {
143+
LLVMRustSetLastError(toString(std::move(rai->err)).c_str());
144+
return nullptr;
154145
}
146+
#endif
147+
} else {
148+
rai->first = false;
149+
}
155150

156-
if (rai->cur == rai->end) return nullptr;
151+
if (rai->cur == rai->end)
152+
return nullptr;
157153

158154
#if LLVM_VERSION_EQ(3, 8)
159-
const ErrorOr<Archive::Child>* cur = rai->cur.operator->();
160-
if (!*cur) {
161-
LLVMRustSetLastError(cur->getError().message().c_str());
162-
return nullptr;
163-
}
164-
const Archive::Child &child = cur->get();
155+
const ErrorOr<Archive::Child> *cur = rai->cur.operator->();
156+
if (!*cur) {
157+
LLVMRustSetLastError(cur->getError().message().c_str());
158+
return nullptr;
159+
}
160+
const Archive::Child &child = cur->get();
165161
#else
166-
const Archive::Child &child = *rai->cur.operator->();
162+
const Archive::Child &child = *rai->cur.operator->();
167163
#endif
168-
Archive::Child *ret = new Archive::Child(child);
164+
Archive::Child *ret = new Archive::Child(child);
169165

170-
return ret;
166+
return ret;
171167
}
172168

173-
extern "C" void
174-
LLVMRustArchiveChildFree(LLVMRustArchiveChildRef child) {
175-
delete child;
169+
extern "C" void LLVMRustArchiveChildFree(LLVMRustArchiveChildRef child) {
170+
delete child;
176171
}
177172

178-
extern "C" void
179-
LLVMRustArchiveIteratorFree(LLVMRustArchiveIteratorRef rai) {
180-
delete rai;
173+
extern "C" void LLVMRustArchiveIteratorFree(LLVMRustArchiveIteratorRef rai) {
174+
delete rai;
181175
}
182176

183-
extern "C" const char*
177+
extern "C" const char *
184178
LLVMRustArchiveChildName(LLVMRustArchiveChildConstRef child, size_t *size) {
185179
#if LLVM_VERSION_GE(4, 0)
186-
Expected<StringRef> name_or_err = child->getName();
187-
if (!name_or_err) {
188-
// rustc_llvm currently doesn't use this error string, but it might be useful
189-
// in the future, and in the mean time this tells LLVM that the error was
190-
// not ignored and that it shouldn't abort the process.
191-
LLVMRustSetLastError(toString(name_or_err.takeError()).c_str());
192-
return NULL;
193-
}
180+
Expected<StringRef> name_or_err = child->getName();
181+
if (!name_or_err) {
182+
// rustc_llvm currently doesn't use this error string, but it might be useful
183+
// in the future, and in the mean time this tells LLVM that the error was
184+
// not ignored and that it shouldn't abort the process.
185+
LLVMRustSetLastError(toString(name_or_err.takeError()).c_str());
186+
return nullptr;
187+
}
194188
#else
195-
ErrorOr<StringRef> name_or_err = child->getName();
196-
if (name_or_err.getError())
197-
return NULL;
189+
ErrorOr<StringRef> name_or_err = child->getName();
190+
if (name_or_err.getError())
191+
return nullptr;
198192
#endif
199-
StringRef name = name_or_err.get();
200-
*size = name.size();
201-
return name.data();
193+
StringRef name = name_or_err.get();
194+
*size = name.size();
195+
return name.data();
202196
}
203197

204-
extern "C" const char*
205-
LLVMRustArchiveChildData(LLVMRustArchiveChildRef child, size_t *size) {
206-
StringRef buf;
198+
extern "C" const char *LLVMRustArchiveChildData(LLVMRustArchiveChildRef child,
199+
size_t *size) {
200+
StringRef buf;
207201
#if LLVM_VERSION_GE(4, 0)
208-
Expected<StringRef> buf_or_err = child->getBuffer();
209-
if (!buf_or_err) {
210-
LLVMRustSetLastError(toString(buf_or_err.takeError()).c_str());
211-
return NULL;
212-
}
202+
Expected<StringRef> buf_or_err = child->getBuffer();
203+
if (!buf_or_err) {
204+
LLVMRustSetLastError(toString(buf_or_err.takeError()).c_str());
205+
return nullptr;
206+
}
213207
#else
214-
ErrorOr<StringRef> buf_or_err = child->getBuffer();
215-
if (buf_or_err.getError()) {
216-
LLVMRustSetLastError(buf_or_err.getError().message().c_str());
217-
return NULL;
218-
}
208+
ErrorOr<StringRef> buf_or_err = child->getBuffer();
209+
if (buf_or_err.getError()) {
210+
LLVMRustSetLastError(buf_or_err.getError().message().c_str());
211+
return nullptr;
212+
}
219213
#endif
220-
buf = buf_or_err.get();
221-
*size = buf.size();
222-
return buf.data();
214+
buf = buf_or_err.get();
215+
*size = buf.size();
216+
return buf.data();
223217
}
224218

225219
extern "C" LLVMRustArchiveMemberRef
226220
LLVMRustArchiveMemberNew(char *Filename, char *Name,
227-
LLVMRustArchiveChildRef child) {
228-
RustArchiveMember *Member = new RustArchiveMember;
229-
Member->filename = Filename;
230-
Member->name = Name;
231-
if (child)
232-
Member->child = *child;
233-
return Member;
221+
LLVMRustArchiveChildRef child) {
222+
RustArchiveMember *Member = new RustArchiveMember;
223+
Member->filename = Filename;
224+
Member->name = Name;
225+
if (child)
226+
Member->child = *child;
227+
return Member;
234228
}
235229

236-
extern "C" void
237-
LLVMRustArchiveMemberFree(LLVMRustArchiveMemberRef Member) {
238-
delete Member;
230+
extern "C" void LLVMRustArchiveMemberFree(LLVMRustArchiveMemberRef Member) {
231+
delete Member;
239232
}
240233

241234
extern "C" LLVMRustResult
242-
LLVMRustWriteArchive(char *Dst,
243-
size_t NumMembers,
235+
LLVMRustWriteArchive(char *Dst, size_t NumMembers,
244236
const LLVMRustArchiveMemberRef *NewMembers,
245-
bool WriteSymbtab,
246-
LLVMRustArchiveKind rust_kind) {
237+
bool WriteSymbtab, LLVMRustArchiveKind rust_kind) {
247238

248239
#if LLVM_VERSION_LE(3, 8)
249240
std::vector<NewArchiveIterator> Members;
@@ -257,7 +248,8 @@ LLVMRustWriteArchive(char *Dst,
257248
assert(Member->name);
258249
if (Member->filename) {
259250
#if LLVM_VERSION_GE(3, 9)
260-
Expected<NewArchiveMember> MOrErr = NewArchiveMember::getFile(Member->filename, true);
251+
Expected<NewArchiveMember> MOrErr =
252+
NewArchiveMember::getFile(Member->filename, true);
261253
if (!MOrErr) {
262254
LLVMRustSetLastError(toString(MOrErr.takeError()).c_str());
263255
return LLVMRustResult::Failure;
@@ -272,7 +264,8 @@ LLVMRustWriteArchive(char *Dst,
272264
#if LLVM_VERSION_LE(3, 8)
273265
Members.push_back(NewArchiveIterator(Member->child, Member->name));
274266
#else
275-
Expected<NewArchiveMember> MOrErr = NewArchiveMember::getOldMember(Member->child, true);
267+
Expected<NewArchiveMember> MOrErr =
268+
NewArchiveMember::getOldMember(Member->child, true);
276269
if (!MOrErr) {
277270
LLVMRustSetLastError(toString(MOrErr.takeError()).c_str());
278271
return LLVMRustResult::Failure;

0 commit comments

Comments
 (0)