Skip to content

Commit 6f2a030

Browse files
committed
Address code reviews
1 parent a13c4c9 commit 6f2a030

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,22 @@ inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
14041404
return createFileError(F, Line, errorCodeToError(EC));
14051405
}
14061406

1407+
/// Concatenate a source file path with a std::error_code and string error
1408+
inline Error createFileError(const Twine &F, std::error_code EC,
1409+
const Twine &S) {
1410+
Error E = createStringError(EC, S);
1411+
return createFileError(F, std::move(E));
1412+
}
1413+
1414+
/// Concatenate a source file path with a std::error_code and string error with
1415+
/// values
1416+
template <typename... Ts>
1417+
inline Error createFileError(const Twine &F, std::error_code EC,
1418+
char const *Fmt, const Ts &...Vals) {
1419+
Error E = createStringError(EC, Fmt, Vals...);
1420+
return createFileError(F, std::move(E));
1421+
}
1422+
14071423
Error createFileError(const Twine &F, ErrorSuccess) = delete;
14081424

14091425
/// Helper for check-and-exit error handling.

llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,10 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
189189
StringRef InputFilename, Object &Obj) {
190190
for (auto &Sec : Obj.sections()) {
191191
if (Sec.Name == SecName) {
192-
if (Sec.Type == SHT_NOBITS) {
193-
Error E =
194-
createStringError(object_error::parse_failed,
195-
"cannot dump section '%s': it has no contents",
196-
SecName.str().c_str());
197-
return createFileError(InputFilename, std::move(E));
198-
}
192+
if (Sec.Type == SHT_NOBITS)
193+
return createFileError(InputFilename, object_error::parse_failed,
194+
"cannot dump section '%s': it has no contents",
195+
SecName.str().c_str());
199196
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
200197
FileOutputBuffer::create(Filename, Sec.OriginalData.size());
201198
if (!BufferOrErr)
@@ -208,10 +205,9 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
208205
return Error::success();
209206
}
210207
}
211-
Error E = createStringError(object_error::parse_failed,
212-
"section '%s' not found", SecName.str().c_str());
213208

214-
return createFileError(InputFilename, std::move(E));
209+
return createFileError(InputFilename, object_error::parse_failed,
210+
"section '%s' not found", SecName.str().c_str());
215211
}
216212

217213
Error Object::compressOrDecompressSections(const CommonConfig &Config) {
@@ -832,37 +828,32 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
832828
if (Config.ChangeSectionLMAValAll > 0 &&
833829
Seg.PAddr > std::numeric_limits<uint64_t>::max() -
834830
Config.ChangeSectionLMAValAll) {
835-
Error E = createStringError(
836-
errc::invalid_argument,
831+
return createFileError(
832+
Config.InputFilename, errc::invalid_argument,
837833
"address 0x" + Twine::utohexstr(Seg.PAddr) +
838834
" cannot be increased by 0x" +
839835
Twine::utohexstr(Config.ChangeSectionLMAValAll) +
840836
". The result would overflow");
841-
return createFileError(Config.InputFilename, std::move(E));
842837
} else if (Config.ChangeSectionLMAValAll < 0 &&
843838
Seg.PAddr < std::numeric_limits<uint64_t>::min() -
844839
Config.ChangeSectionLMAValAll) {
845-
Error E = createStringError(
846-
errc::invalid_argument,
840+
return createFileError(
841+
Config.InputFilename, errc::invalid_argument,
847842
"address 0x" + Twine::utohexstr(Seg.PAddr) +
848843
" cannot be decreased by 0x" +
849844
Twine::utohexstr(std::abs(Config.ChangeSectionLMAValAll)) +
850845
". The result would underflow");
851-
852-
return createFileError(Config.InputFilename, std::move(E));
853846
}
854847
Seg.PAddr += Config.ChangeSectionLMAValAll;
855848
}
856849
}
857850
}
858851

859852
if (!Config.ChangeSectionAddress.empty()) {
860-
if (Obj.Type != ELF::ET_REL) {
861-
Error E = createStringError(
862-
object_error::invalid_file_type,
853+
if (Obj.Type != ELF::ET_REL)
854+
return createFileError(
855+
Config.InputFilename, object_error::invalid_file_type,
863856
"cannot change section address in a non-relocatable file");
864-
return createFileError(Config.InputFilename, std::move(E));
865-
}
866857
StringMap<AddressUpdate> SectionsToUpdateAddress;
867858
for (const SectionPatternAddressUpdate &PatternUpdate :
868859
make_range(Config.ChangeSectionAddress.rbegin(),
@@ -873,26 +864,22 @@ static Error handleArgs(const CommonConfig &Config, const ELFConfig &ELFConfig,
873864
.second) {
874865
if (PatternUpdate.Update.Kind == AdjustKind::Subtract &&
875866
Sec.Addr < PatternUpdate.Update.Value) {
876-
Error E = createStringError(
877-
errc::invalid_argument,
867+
return createFileError(
868+
Config.InputFilename, errc::invalid_argument,
878869
"address 0x" + Twine::utohexstr(Sec.Addr) +
879870
" cannot be decreased by 0x" +
880871
Twine::utohexstr(PatternUpdate.Update.Value) +
881872
". The result would underflow");
882-
883-
return createFileError(Config.InputFilename, std::move(E));
884873
}
885874
if (PatternUpdate.Update.Kind == AdjustKind::Add &&
886875
Sec.Addr > std::numeric_limits<uint64_t>::max() -
887876
PatternUpdate.Update.Value) {
888-
Error E = createStringError(
889-
errc::invalid_argument,
877+
return createFileError(
878+
Config.InputFilename, errc::invalid_argument,
890879
"address 0x" + Twine::utohexstr(Sec.Addr) +
891880
" cannot be increased by 0x" +
892881
Twine::utohexstr(PatternUpdate.Update.Value) +
893882
". The result would overflow");
894-
895-
return createFileError(Config.InputFilename, std::move(E));
896883
}
897884

898885
switch (PatternUpdate.Update.Kind) {

llvm/lib/ObjCopy/MachO/MachOObjcopy.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,8 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
323323
}
324324
}
325325

326-
Error E = createStringError(object_error::parse_failed,
327-
"section '%s' not found", SecName.str().c_str());
328-
return createFileError(InputFilename, std::move(E));
326+
return createFileError(InputFilename, object_error::parse_failed,
327+
"section '%s' not found", SecName.str().c_str());
329328
}
330329

331330
static Error addSection(const NewSectionInfo &NewSection, Object &Obj) {

llvm/lib/ObjCopy/wasm/WasmObjcopy.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
5353
return Error::success();
5454
}
5555
}
56-
Error E = createStringError(errc::invalid_argument, "section '%s' not found",
57-
SecName.str().c_str());
58-
return createFileError(Filename, std::move(E));
56+
return createFileError(Filename, errc::invalid_argument,
57+
"section '%s' not found", SecName.str().c_str());
5958
}
6059

6160
static void removeSections(const CommonConfig &Config, Object &Obj) {

0 commit comments

Comments
 (0)