Author: Adrian Prantl
Date: 2024-06-20T10:32:06-07:00
New Revision: b8f0ca09b66716fc337448a2e43146038bf115ab

URL: 
https://github.com/llvm/llvm-project/commit/b8f0ca09b66716fc337448a2e43146038bf115ab
DIFF: 
https://github.com/llvm/llvm-project/commit/b8f0ca09b66716fc337448a2e43146038bf115ab.diff

LOG: Factor out expression result error strings.

Added: 
    lldb/include/lldb/Utility/ErrorMessages.h
    lldb/source/Utility/ErrorMessages.cpp

Modified: 
    lldb/source/Interpreter/CommandInterpreter.cpp
    
lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
    lldb/source/Utility/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/ErrorMessages.h 
b/lldb/include/lldb/Utility/ErrorMessages.h
new file mode 100644
index 0000000000000..116e29e0e6187
--- /dev/null
+++ b/lldb/include/lldb/Utility/ErrorMessages.h
@@ -0,0 +1,22 @@
+//===-- ErrorMessages.h -----------------------------------------*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_UTILITY_ERROR_MESSAGES_H
+#define LLDB_UTILITY_ERROR_MESSAGES_H
+
+#include "lldb/lldb-defines.h"
+#include "lldb/lldb-enumerations.h"
+#include <string>
+
+namespace lldb_private {
+
+/// Produce a human-readable rendition of an ExpressionResults value.
+std::string toString(lldb::ExpressionResults e);
+
+} // namespace lldb_private
+#endif

diff  --git a/lldb/source/Interpreter/CommandInterpreter.cpp 
b/lldb/source/Interpreter/CommandInterpreter.cpp
index da995de1407c4..40c915b2c94fc 100644
--- a/lldb/source/Interpreter/CommandInterpreter.cpp
+++ b/lldb/source/Interpreter/CommandInterpreter.cpp
@@ -48,6 +48,7 @@
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Host/StreamFile.h"
+#include "lldb/Utility/ErrorMessages.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/State.h"
@@ -1817,51 +1818,8 @@ CommandInterpreter::PreprocessToken(std::string 
&expr_str) {
     error = expr_result_valobj_sp->GetError();
 
   if (error.Success()) {
-    switch (expr_result) {
-    case eExpressionSetupError:
-      error.SetErrorStringWithFormat(
-          "expression setup error for the expression '%s'", expr_str.c_str());
-      break;
-    case eExpressionParseError:
-      error.SetErrorStringWithFormat(
-          "expression parse error for the expression '%s'", expr_str.c_str());
-      break;
-    case eExpressionResultUnavailable:
-      error.SetErrorStringWithFormat(
-          "expression error fetching result for the expression '%s'",
-          expr_str.c_str());
-      break;
-    case eExpressionCompleted:
-      break;
-    case eExpressionDiscarded:
-      error.SetErrorStringWithFormat(
-          "expression discarded for the expression '%s'", expr_str.c_str());
-      break;
-    case eExpressionInterrupted:
-      error.SetErrorStringWithFormat(
-          "expression interrupted for the expression '%s'", expr_str.c_str());
-      break;
-    case eExpressionHitBreakpoint:
-      error.SetErrorStringWithFormat(
-          "expression hit breakpoint for the expression '%s'",
-          expr_str.c_str());
-      break;
-    case eExpressionTimedOut:
-      error.SetErrorStringWithFormat(
-          "expression timed out for the expression '%s'", expr_str.c_str());
-      break;
-    case eExpressionStoppedForDebug:
-      error.SetErrorStringWithFormat("expression stop at entry point "
-                                     "for debugging for the "
-                                     "expression '%s'",
-                                     expr_str.c_str());
-      break;
-    case eExpressionThreadVanished:
-      error.SetErrorStringWithFormat(
-          "expression thread vanished for the expression '%s'",
-          expr_str.c_str());
-      break;
-    }
+    std::string result = lldb_private::toString(expr_result);
+    error.SetErrorString(result + "for the expression '" + expr_str + "'");
   }
   return error;
 }

diff  --git 
a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
 
b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
index 800eda925591e..5ff267720629e 100644
--- 
a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
+++ 
b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
@@ -31,6 +31,7 @@
 #include "lldb/Target/Target.h"
 #include "lldb/Target/Thread.h"
 #include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/ErrorMessages.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/Scalar.h"
@@ -201,7 +202,7 @@ AppleObjCRuntime::GetObjectDescription(Stream &strm, Value 
&value,
       exe_ctx, &wrapper_struct_addr, options, diagnostics, ret);
   if (results != eExpressionCompleted)
     return llvm::createStringError(
-        "Error evaluating Print Object function: %d.\n", results);
+        "could not evaluate print object function: " + toString(results));
 
   addr_t result_ptr = ret.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
 

diff  --git a/lldb/source/Utility/CMakeLists.txt 
b/lldb/source/Utility/CMakeLists.txt
index a3b0a405b4133..e9954d66cd1a5 100644
--- a/lldb/source/Utility/CMakeLists.txt
+++ b/lldb/source/Utility/CMakeLists.txt
@@ -39,6 +39,7 @@ add_lldb_library(lldbUtility NO_INTERNAL_DEPENDENCIES
   DataExtractor.cpp
   Diagnostics.cpp
   Environment.cpp
+  ErrorMessages.cpp
   Event.cpp
   FileSpec.cpp
   FileSpecList.cpp

diff  --git a/lldb/source/Utility/ErrorMessages.cpp 
b/lldb/source/Utility/ErrorMessages.cpp
new file mode 100644
index 0000000000000..aea5cb5f47c11
--- /dev/null
+++ b/lldb/source/Utility/ErrorMessages.cpp
@@ -0,0 +1,40 @@
+//===-- ErrorMessages.cpp 
-------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Utility/ErrorMessages.h"
+#include "llvm/Support/ErrorHandling.h"
+
+namespace lldb_private {
+
+std::string toString(lldb::ExpressionResults e) {
+  switch (e) {
+  case lldb::eExpressionSetupError:
+    return "expression setup error";
+  case lldb::eExpressionParseError:
+    return "expression parse error";
+  case lldb::eExpressionResultUnavailable:
+    return "expression error";
+  case lldb::eExpressionCompleted:
+    return "expression completed successfully";
+  case lldb::eExpressionDiscarded:
+    return "expression discarded";
+  case lldb::eExpressionInterrupted:
+    return "expression interrupted";
+  case lldb::eExpressionHitBreakpoint:
+    return "expression hit breakpoint";
+  case lldb::eExpressionTimedOut:
+    return "expression timed out";
+  case lldb::eExpressionStoppedForDebug:
+    return "expression stop at entry point for debugging";
+  case lldb::eExpressionThreadVanished:
+    return "expression thread vanished";
+  }
+  llvm_unreachable("unhandled enumerator");
+}
+
+} // namespace lldb_private


        
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to