MC: Cleanup MCSectionMachO::ParseSectionSpecifier

Split by comma once instead of multiple times.  Moving this upfront
makes the rest of the code considerably simpler.

No functional change.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203429 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer 2014-03-10 00:55:07 +00:00
parent 9e215b0880
commit 5d0bfc8f87

View File

@ -48,8 +48,7 @@ static const struct {
/// SectionAttrDescriptors - This is an array of descriptors for section /// SectionAttrDescriptors - This is an array of descriptors for section
/// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed /// attributes. Unlike the SectionTypeDescriptors, this is not directly indexed
/// by attribute, instead it is searched. The last entry has an AttrFlagEnd /// by attribute, instead it is searched.
/// AttrFlag value.
static const struct { static const struct {
unsigned AttrFlag; unsigned AttrFlag;
const char *AssemblerName, *EnumName; const char *AssemblerName, *EnumName;
@ -68,8 +67,6 @@ ENTRY(0 /*FIXME*/, S_ATTR_EXT_RELOC)
ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC) ENTRY(0 /*FIXME*/, S_ATTR_LOC_RELOC)
#undef ENTRY #undef ENTRY
{ 0, "none", 0 }, // used if section has no attributes but has a stub size { 0, "none", 0 }, // used if section has no attributes but has a stub size
#define AttrFlagEnd 0xffffffff // non-legal value, multiple attribute bits set
{ AttrFlagEnd, 0, 0 }
}; };
MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section, MCSectionMachO::MCSectionMachO(StringRef Segment, StringRef Section,
@ -164,14 +161,6 @@ bool MCSectionMachO::isVirtualSection() const {
getType() == MachO::S_THREAD_LOCAL_ZEROFILL); getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
} }
/// StripSpaces - This removes leading and trailing spaces from the StringRef.
static void StripSpaces(StringRef &Str) {
while (!Str.empty() && isspace(static_cast<unsigned char>(Str[0])))
Str = Str.substr(1);
while (!Str.empty() && isspace(static_cast<unsigned char>(Str.back())))
Str = Str.substr(0, Str.size()-1);
}
/// ParseSectionSpecifier - Parse the section specifier indicated by "Spec". /// ParseSectionSpecifier - Parse the section specifier indicated by "Spec".
/// This is a string that can appear after a .section directive in a mach-o /// This is a string that can appear after a .section directive in a mach-o
/// flavored .s file. If successful, this fills in the specified Out /// flavored .s file. If successful, this fills in the specified Out
@ -184,65 +173,57 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
bool &TAAParsed, // Out. bool &TAAParsed, // Out.
unsigned &StubSize) { // Out. unsigned &StubSize) { // Out.
TAAParsed = false; TAAParsed = false;
// Find the first comma.
std::pair<StringRef, StringRef> Comma = Spec.split(',');
// If there is no comma, we fail. SmallVector<StringRef, 5> SplitSpec;
if (Comma.second.empty()) Spec.split(SplitSpec, ",");
return "mach-o section specifier requires a segment and section " // Remove leading and trailing whitespace.
"separated by a comma"; auto GetEmptyOrTrim = [&SplitSpec](size_t Idx) -> StringRef {
return SplitSpec.size() > Idx ? SplitSpec[Idx].trim() : StringRef();
// Capture segment, remove leading and trailing whitespace. };
Segment = Comma.first; Segment = GetEmptyOrTrim(0);
StripSpaces(Segment); Section = GetEmptyOrTrim(1);
StringRef SectionType = GetEmptyOrTrim(2);
StringRef Attrs = GetEmptyOrTrim(3);
StringRef StubSizeStr = GetEmptyOrTrim(4);
// Verify that the segment is present and not too long. // Verify that the segment is present and not too long.
if (Segment.empty() || Segment.size() > 16) if (Segment.empty() || Segment.size() > 16)
return "mach-o section specifier requires a segment whose length is " return "mach-o section specifier requires a segment whose length is "
"between 1 and 16 characters"; "between 1 and 16 characters";
// Split the section name off from any attributes if present.
Comma = Comma.second.split(',');
// Capture section, remove leading and trailing whitespace.
Section = Comma.first;
StripSpaces(Section);
// Verify that the section is present and not too long. // Verify that the section is present and not too long.
if (Section.empty() || Section.size() > 16) if (Section.empty())
return "mach-o section specifier requires a segment and section "
"separated by a comma";
if (Section.size() > 16)
return "mach-o section specifier requires a section whose length is " return "mach-o section specifier requires a section whose length is "
"between 1 and 16 characters"; "between 1 and 16 characters";
// If there is no comma after the section, we're done. // If there is no comma after the section, we're done.
TAA = 0; TAA = 0;
StubSize = 0; StubSize = 0;
if (Comma.second.empty()) if (SectionType.empty())
return ""; return "";
// Otherwise, we need to parse the section type and attributes.
Comma = Comma.second.split(',');
// Get the section type.
StringRef SectionType = Comma.first;
StripSpaces(SectionType);
// Figure out which section type it is. // Figure out which section type it is.
unsigned TypeID; auto TypeDescriptor = std::find_if(
for (TypeID = 0; TypeID != MachO::LAST_KNOWN_SECTION_TYPE + 1; ++TypeID) std::begin(SectionTypeDescriptors), std::end(SectionTypeDescriptors),
if (SectionTypeDescriptors[TypeID].AssemblerName && [&](const decltype(*SectionTypeDescriptors) &Descriptor) {
SectionType == SectionTypeDescriptors[TypeID].AssemblerName) return Descriptor.AssemblerName &&
break; SectionType == Descriptor.AssemblerName;
});
// If we didn't find the section type, reject it. // If we didn't find the section type, reject it.
if (TypeID > MachO::LAST_KNOWN_SECTION_TYPE) if (TypeDescriptor == std::end(SectionTypeDescriptors))
return "mach-o section specifier uses an unknown section type"; return "mach-o section specifier uses an unknown section type";
// Remember the TypeID. // Remember the TypeID.
TAA = TypeID; TAA = TypeDescriptor - std::begin(SectionTypeDescriptors);
TAAParsed = true; TAAParsed = true;
// If we have no comma after the section type, there are no attributes. // If we have no comma after the section type, there are no attributes.
if (Comma.second.empty()) { if (Attrs.empty()) {
// S_SYMBOL_STUBS always require a symbol stub size specifier. // S_SYMBOL_STUBS always require a symbol stub size specifier.
if (TAA == MachO::S_SYMBOL_STUBS) if (TAA == MachO::S_SYMBOL_STUBS)
return "mach-o section specifier of type 'symbol_stubs' requires a size " return "mach-o section specifier of type 'symbol_stubs' requires a size "
@ -250,36 +231,25 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
return ""; return "";
} }
// Otherwise, we do have some attributes. Split off the size specifier if
// present.
Comma = Comma.second.split(',');
StringRef Attrs = Comma.first;
// The attribute list is a '+' separated list of attributes. // The attribute list is a '+' separated list of attributes.
std::pair<StringRef, StringRef> Plus = Attrs.split('+'); SmallVector<StringRef, 1> SectionAttrs;
Attrs.split(SectionAttrs, "+", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
while (1) { for (StringRef &SectionAttr : SectionAttrs) {
StringRef Attr = Plus.first; auto AttrDescriptorI = std::find_if(
StripSpaces(Attr); std::begin(SectionAttrDescriptors), std::end(SectionAttrDescriptors),
[&](const decltype(*SectionAttrDescriptors) &Descriptor) {
// Look up the attribute. return Descriptor.AssemblerName &&
for (unsigned i = 0; ; ++i) { SectionAttr.trim() == Descriptor.AssemblerName;
if (SectionAttrDescriptors[i].AttrFlag == AttrFlagEnd) });
if (AttrDescriptorI == std::end(SectionAttrDescriptors))
return "mach-o section specifier has invalid attribute"; return "mach-o section specifier has invalid attribute";
if (SectionAttrDescriptors[i].AssemblerName && TAA |= AttrDescriptorI->AttrFlag;
Attr == SectionAttrDescriptors[i].AssemblerName) {
TAA |= SectionAttrDescriptors[i].AttrFlag;
break;
} }
}
if (Plus.second.empty()) break;
Plus = Plus.second.split('+');
};
// Okay, we've parsed the section attributes, see if we have a stub size spec. // Okay, we've parsed the section attributes, see if we have a stub size spec.
if (Comma.second.empty()) { if (StubSizeStr.empty()) {
// S_SYMBOL_STUBS always require a symbol stub size specifier. // S_SYMBOL_STUBS always require a symbol stub size specifier.
if (TAA == MachO::S_SYMBOL_STUBS) if (TAA == MachO::S_SYMBOL_STUBS)
return "mach-o section specifier of type 'symbol_stubs' requires a size " return "mach-o section specifier of type 'symbol_stubs' requires a size "
@ -292,10 +262,6 @@ std::string MCSectionMachO::ParseSectionSpecifier(StringRef Spec, // In.
return "mach-o section specifier cannot have a stub size specified because " return "mach-o section specifier cannot have a stub size specified because "
"it does not have type 'symbol_stubs'"; "it does not have type 'symbol_stubs'";
// Okay, if we do, it must be a number.
StringRef StubSizeStr = Comma.second;
StripSpaces(StubSizeStr);
// Convert the stub size from a string to an integer. // Convert the stub size from a string to an integer.
if (StubSizeStr.getAsInteger(0, StubSize)) if (StubSizeStr.getAsInteger(0, StubSize))
return "mach-o section specifier has a malformed stub size"; return "mach-o section specifier has a malformed stub size";