Skip to content

Commit 320a1d8

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. (cherry picked from commit 2c07181)
1 parent 652a422 commit 320a1d8

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
@@ -1249,7 +1249,7 @@ static void findKeepUniqueSections(COFFLinkerContext &ctx) {
12491249
const uint8_t *cur = contents.begin();
12501250
while (cur != contents.end()) {
12511251
unsigned size;
1252-
const char *err;
1252+
const char *err = nullptr;
12531253
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
12541254
if (err)
12551255
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
@@ -2229,7 +2229,7 @@ static void findKeepUniqueSections(opt::InputArgList &args) {
22292229
const uint8_t *cur = contents.begin();
22302230
while (cur != contents.end()) {
22312231
unsigned size;
2232-
const char *err;
2232+
const char *err = nullptr;
22332233
uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
22342234
if (err)
22352235
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
@@ -2992,7 +2992,7 @@ void ExportEntry::pushNode(uint64_t offset) {
29922992
ErrorAsOutParameter ErrAsOutParam(E);
29932993
const uint8_t *Ptr = Trie.begin() + offset;
29942994
NodeState State(Ptr);
2995-
const char *error;
2995+
const char *error = nullptr;
29962996
uint64_t ExportInfoSize = readULEB128(State.Current, &error);
29972997
if (error) {
29982998
*E = malformedError("export info size " + Twine(error) +
@@ -3127,7 +3127,7 @@ void ExportEntry::pushNode(uint64_t offset) {
31273127

31283128
void ExportEntry::pushDownUntilBottom() {
31293129
ErrorAsOutParameter ErrAsOutParam(E);
3130-
const char *error;
3130+
const char *error = nullptr;
31313131
while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
31323132
NodeState &Top = Stack.back();
31333133
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
@@ -2125,7 +2125,7 @@ void COFFDumper::printAddrsig() {
21252125
const uint8_t *End = AddrsigContents.bytes_end();
21262126
while (Cur != End) {
21272127
unsigned Size;
2128-
const char *Err;
2128+
const char *Err = nullptr;
21292129
uint64_t SymIndex = decodeULEB128(Cur, &Size, End, &Err);
21302130
if (Err)
21312131
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
@@ -5002,7 +5002,7 @@ static Expected<std::vector<uint64_t>> toULEB128Array(ArrayRef<uint8_t> Data) {
50025002
const uint8_t *End = Data.end();
50035003
while (Cur != End) {
50045004
unsigned Size;
5005-
const char *Err;
5005+
const char *Err = nullptr;
50065006
Ret.push_back(decodeULEB128(Cur, &Size, End, &Err));
50075007
if (Err)
50085008
return createError(Err);

0 commit comments

Comments
 (0)