Skip to content

Commit 2c07181

Browse files
committed
[LEB128] Don't initialize error on success
This change removes an unnecessary branch from a hot path. It's also questionable API to override any previous error unconditonally.
1 parent 076bd22 commit 2c07181

File tree

7 files changed

+13
-11
lines changed

7 files changed

+13
-11
lines changed

lld/COFF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ static void findKeepUniqueSections(COFFLinkerContext &ctx) {
12581258
const uint8_t *cur = contents.begin();
12591259
while (cur != contents.end()) {
12601260
unsigned size;
1261-
const char *err;
1261+
const char *err = nullptr;
12621262
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
12631263
if (err)
12641264
fatal(toString(obj) + ": could not decode addrsig section: " + err);

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) {
22962296
const uint8_t *cur = contents.begin();
22972297
while (cur != contents.end()) {
22982298
unsigned size;
2299-
const char *err;
2299+
const char *err = nullptr;
23002300
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
23012301
if (err)
23022302
fatal(toString(f) + ": could not decode addrsig section: " + err);

llvm/include/llvm/Support/LEB128.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ inline unsigned encodeULEB128(uint64_t Value, uint8_t *p,
125125
}
126126

127127
/// Utility function to decode a ULEB128 value.
128+
///
129+
/// If \p error is non-null, it will point to a static error message,
130+
/// if an error occured. It will not be modified on success.
128131
inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
129132
const uint8_t *end = nullptr,
130133
const char **error = nullptr) {
131134
const uint8_t *orig_p = p;
132135
uint64_t Value = 0;
133136
unsigned Shift = 0;
134-
if (error)
135-
*error = nullptr;
136137
do {
137138
if (p == end) {
138139
if (error)
@@ -158,15 +159,16 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
158159
}
159160

160161
/// Utility function to decode a SLEB128 value.
162+
///
163+
/// If \p error is non-null, it will point to a static error message,
164+
/// if an error occured. It will not be modified on success.
161165
inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
162166
const uint8_t *end = nullptr,
163167
const char **error = nullptr) {
164168
const uint8_t *orig_p = p;
165169
int64_t Value = 0;
166170
unsigned Shift = 0;
167171
uint8_t Byte;
168-
if (error)
169-
*error = nullptr;
170172
do {
171173
if (p == end) {
172174
if (error)

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,7 +2996,7 @@ void ExportEntry::pushNode(uint64_t offset) {
29962996
ErrorAsOutParameter ErrAsOutParam(E);
29972997
const uint8_t *Ptr = Trie.begin() + offset;
29982998
NodeState State(Ptr);
2999-
const char *error;
2999+
const char *error = nullptr;
30003000
uint64_t ExportInfoSize = readULEB128(State.Current, &error);
30013001
if (error) {
30023002
*E = malformedError("export info size " + Twine(error) +
@@ -3131,7 +3131,7 @@ void ExportEntry::pushNode(uint64_t offset) {
31313131

31323132
void ExportEntry::pushDownUntilBottom() {
31333133
ErrorAsOutParameter ErrAsOutParam(E);
3134-
const char *error;
3134+
const char *error = nullptr;
31353135
while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
31363136
NodeState &Top = Stack.back();
31373137
CumulativeString.resize(Top.ParentStringLength);

llvm/lib/Support/DataExtractor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err,
202202
if (isError(Err))
203203
return T();
204204

205-
const char *error;
205+
const char *error = nullptr;
206206
unsigned bytes_read;
207207
T result =
208208
Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);

llvm/tools/llvm-readobj/COFFDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ void COFFDumper::printAddrsig() {
21262126
const uint8_t *End = AddrsigContents.bytes_end();
21272127
while (Cur != End) {
21282128
unsigned Size;
2129-
const char *Err;
2129+
const char *Err = nullptr;
21302130
uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
21312131
if (Err)
21322132
reportError(createError(Err), Obj->getFileName());

llvm/tools/llvm-readobj/ELFDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5004,7 +5004,7 @@ static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
50045004
const uint8_t *End = Data.end();
50055005
while (Cur != End) {
50065006
unsigned Size;
5007-
const char *Err;
5007+
const char *Err = nullptr;
50085008
Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
50095009
if (Err)
50105010
return createError(Err);

0 commit comments

Comments
 (0)