================
@@ -323,47 +323,144 @@ Interpreter::Visit(const MemberOfNode *node) {
       m_expr, errMsg, node->GetLocation(), node->GetFieldName().size());
 }
 
+
 llvm::Expected<lldb::ValueObjectSP>
 Interpreter::Visit(const ArraySubscriptNode *node) {
   auto lhs_or_err = Evaluate(node->GetBase());
   if (!lhs_or_err)
     return lhs_or_err;
   lldb::ValueObjectSP base = *lhs_or_err;
 
-  // Check to see if 'base' has a synthetic value; if so, try using that.
+  StreamString var_expr_path_strm;
   uint64_t child_idx = node->GetIndex();
-  if (lldb::ValueObjectSP synthetic = base->GetSyntheticValue()) {
-    llvm::Expected<uint32_t> num_children =
-        synthetic->GetNumChildren(child_idx + 1);
-    if (!num_children)
-      return llvm::make_error<DILDiagnosticError>(
-          m_expr, toString(num_children.takeError()), node->GetLocation());
-    if (child_idx >= *num_children) {
-      std::string message = llvm::formatv(
-          "array index {0} is not valid for \"({1}) {2}\"", child_idx,
-          base->GetTypeName().AsCString("<invalid type>"),
-          base->GetName().AsCString());
-      return llvm::make_error<DILDiagnosticError>(m_expr, message,
+  lldb::ValueObjectSP child_valobj_sp;
+  bool is_incomplete_array = false;
+  CompilerType base_type = base->GetCompilerType().GetNonReferenceType();
+  base->GetExpressionPath(var_expr_path_strm);
+  if (base_type.IsPointerType()) {
+    bool is_objc_pointer = true;
+    if (base->GetCompilerType().GetMinimumLanguage() != 
lldb::eLanguageTypeObjC)
+      is_objc_pointer = false;
+    else if (!base_type.IsPointerType())
+      is_objc_pointer = false;
+
+    if (is_objc_pointer && !m_use_synthetic) {
+      std::string errMsg =
+          llvm::formatv("\"(({0}) {1}\" is an Objective-C pointer, and cannot "
+                        "be subscripted",
+                        base->GetTypeName().AsCString("<invalid type>"),
+                        base->GetName());
+      return llvm::make_error<DILDiagnosticError>(m_expr, errMsg,
+                                                  node->GetLocation());
+    } else if (is_objc_pointer) {
+      lldb::ValueObjectSP synthetic = base->GetSyntheticValue();
+      if (!synthetic || synthetic == base) {
+        std::string errMsg =
+            llvm::formatv("\"({0}) {1}\" is not an array type",
+                          base->GetTypeName().AsCString("<invalid type>"),
+                          base->GetName());
+        return llvm::make_error<DILDiagnosticError>(m_expr, errMsg,
+                                                    node->GetLocation());
+      } else if (static_cast<uint32_t>(child_idx) >=
+                 synthetic->GetNumChildrenIgnoringErrors()) {
+        std::string errMsg =
+            llvm::formatv("array index {0} is not valid for \"({1}) {2}\"",
+                          child_idx,
+                          base->GetTypeName().AsCString("<invalid type>"),
+                          var_expr_path_strm.GetData());
+        return llvm::make_error<DILDiagnosticError>(m_expr, errMsg,
+                                                    node->GetLocation());
+      } else {
+        child_valobj_sp = synthetic->GetChildAtIndex(child_idx);
+        if (!child_valobj_sp) {
+          std::string errMsg =
+              llvm::formatv("array index {0} is not valid for \"({1}) {2}\"",
+                            child_idx,
+                            base->GetTypeName().AsCString("<invalid type>"),
+                            var_expr_path_strm.GetData());
+          return llvm::make_error<DILDiagnosticError>(m_expr, errMsg,
+                                                      node->GetLocation());
+        }
+      }
----------------
kuilpd wrote:

The code in lines 356-383 is basically the same as in lines 426-453, could we 
reuse this somehow?

https://github.com/llvm/llvm-project/pull/151605
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to