[Lldb-commits] [lldb] 8f7e41d - [lldb][NFCI] Minor cleanups to StructuredData::GetObjectForDotSeparatedPath
Alex Langford via lldb-commits
lldb-commits at lists.llvm.org
Thu Jul 6 08:54:34 PDT 2023
Author: Alex Langford
Date: 2023-07-06T08:51:54-07:00
New Revision: 8f7e41d0400ecc41f8120e330baed15b0d203036
URL: https://github.com/llvm/llvm-project/commit/8f7e41d0400ecc41f8120e330baed15b0d203036
DIFF: https://github.com/llvm/llvm-project/commit/8f7e41d0400ecc41f8120e330baed15b0d203036.diff
LOG: [lldb][NFCI] Minor cleanups to StructuredData::GetObjectForDotSeparatedPath
This accomplishes a few minor things:
- Removed unnecessary uses of `this->`
- Removed an unnecessary std::string allocation.
- Removed some nesting to improve readability using early returns where
it makes sense.
- Replaced `strtoul` with `llvm::to_integer` which avoids another
std::string allocation.
- Removed braces from single statement conditions, removed
else-after-returns.
Differential Revision: https://reviews.llvm.org/D154534
Added:
Modified:
lldb/source/Utility/StructuredData.cpp
Removed:
################################################################################
diff --git a/lldb/source/Utility/StructuredData.cpp b/lldb/source/Utility/StructuredData.cpp
index 674a9fa927806a..be50df323facdd 100644
--- a/lldb/source/Utility/StructuredData.cpp
+++ b/lldb/source/Utility/StructuredData.cpp
@@ -104,36 +104,34 @@ static StructuredData::ObjectSP ParseJSONArray(json::Array *array) {
StructuredData::ObjectSP
StructuredData::Object::GetObjectForDotSeparatedPath(llvm::StringRef path) {
- if (this->GetType() == lldb::eStructuredDataTypeDictionary) {
+ if (GetType() == lldb::eStructuredDataTypeDictionary) {
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('.');
- std::string key = match.first.str();
- ObjectSP value = this->GetAsDictionary()->GetValueForKey(key);
- if (value.get()) {
- // Do we have additional words to descend? If not, return the value
- // we're at right now.
- if (match.second.empty()) {
- return value;
- } else {
- return value->GetObjectForDotSeparatedPath(match.second);
- }
- }
- return ObjectSP();
+ llvm::StringRef key = match.first;
+ ObjectSP value = GetAsDictionary()->GetValueForKey(key);
+ if (!value)
+ return {};
+
+ // Do we have additional words to descend? If not, return the value
+ // we're at right now.
+ if (match.second.empty())
+ return value;
+
+ return value->GetObjectForDotSeparatedPath(match.second);
}
- if (this->GetType() == lldb::eStructuredDataTypeArray) {
+ if (GetType() == lldb::eStructuredDataTypeArray) {
std::pair<llvm::StringRef, llvm::StringRef> match = path.split('[');
- if (match.second.empty()) {
- return this->shared_from_this();
- }
- errno = 0;
- uint64_t val = strtoul(match.second.str().c_str(), nullptr, 10);
- if (errno == 0) {
- return this->GetAsArray()->GetItemAtIndex(val);
- }
- return ObjectSP();
+ if (match.second.empty())
+ return shared_from_this();
+
+ uint64_t val = 0;
+ if (!llvm::to_integer(match.second, val, /* Base = */ 10))
+ return {};
+
+ return GetAsArray()->GetItemAtIndex(val);
}
- return this->shared_from_this();
+ return shared_from_this();
}
void StructuredData::Object::DumpToStdout(bool pretty_print) const {
More information about the lldb-commits
mailing list