[Lldb-commits] [lldb] r356090 - Fix an invalid static cast in ClangExpressionParser.cpp

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 13 12:46:30 PDT 2019


Author: adrian
Date: Wed Mar 13 12:46:30 2019
New Revision: 356090

URL: http://llvm.org/viewvc/llvm-project?rev=356090&view=rev
Log:
Fix an invalid static cast in ClangExpressionParser.cpp

This was found by the green dragon sanitizer bot.

rdar://problem/48536644

Differential Revision: https://reviews.llvm.org/D59314

Modified:
    lldb/trunk/include/lldb/Expression/Expression.h
    lldb/trunk/include/lldb/Expression/FunctionCaller.h
    lldb/trunk/include/lldb/Expression/LLVMUserExpression.h
    lldb/trunk/include/lldb/Expression/UserExpression.h
    lldb/trunk/include/lldb/Expression/UtilityFunction.h
    lldb/trunk/source/Expression/Expression.cpp
    lldb/trunk/source/Expression/FunctionCaller.cpp
    lldb/trunk/source/Expression/LLVMUserExpression.cpp
    lldb/trunk/source/Expression/UserExpression.cpp
    lldb/trunk/source/Expression/UtilityFunction.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h

Modified: lldb/trunk/include/lldb/Expression/Expression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/Expression.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/Expression.h (original)
+++ lldb/trunk/include/lldb/Expression/Expression.h Wed Mar 13 12:46:30 2019
@@ -34,11 +34,22 @@ class RecordingMemoryManager;
 //----------------------------------------------------------------------
 class Expression {
 public:
+  /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
+  enum ExpressionKind {
+    eKindFunctionCaller,
+    eKindClangFunctionCaller,
+    eKindUserExpression,
+    eKindLLVMUserExpression,
+    eKindClangUserExpression,
+    eKindUtilityFunction,
+    eKindClangUtilityFunction,
+  };
+
   enum ResultType { eResultTypeAny, eResultTypeId };
 
-  Expression(Target &target);
+  Expression(Target &target, ExpressionKind kind);
 
-  Expression(ExecutionContextScope &exe_scope);
+  Expression(ExecutionContextScope &exe_scope, ExpressionKind kind);
 
   //------------------------------------------------------------------
   /// Destructor
@@ -103,6 +114,12 @@ public:
 
   virtual ExpressionTypeSystemHelper *GetTypeSystemHelper() { return nullptr; }
 
+  /// LLVM-style RTTI support.
+  ExpressionKind getKind() const { return m_kind; }
+  
+private:
+  /// LLVM-style RTTI support.
+  const ExpressionKind m_kind;
 protected:
   lldb::TargetWP m_target_wp; /// Expression's always have to have a target...
   lldb::ProcessWP m_jit_process_wp; /// An expression might have a process, but

Modified: lldb/trunk/include/lldb/Expression/FunctionCaller.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/FunctionCaller.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/FunctionCaller.h (original)
+++ lldb/trunk/include/lldb/Expression/FunctionCaller.h Wed Mar 13 12:46:30 2019
@@ -57,6 +57,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class FunctionCaller : public Expression {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindFunctionCaller;
+  }
+  
   //------------------------------------------------------------------
   /// Constructor
   ///

Modified: lldb/trunk/include/lldb/Expression/LLVMUserExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/LLVMUserExpression.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/LLVMUserExpression.h (original)
+++ lldb/trunk/include/lldb/Expression/LLVMUserExpression.h Wed Mar 13 12:46:30 2019
@@ -33,6 +33,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class LLVMUserExpression : public UserExpression {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindLLVMUserExpression;
+  }
+
   // The IRPasses struct is filled in by a runtime after an expression is
   // compiled and can be used to to run fixups/analysis passes as required.
   // EarlyPasses are run on the generated module before lldb runs its own IR
@@ -48,7 +53,8 @@ public:
   LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
                      llvm::StringRef prefix, lldb::LanguageType language,
                      ResultType desired_type,
-                     const EvaluateExpressionOptions &options);
+                     const EvaluateExpressionOptions &options,
+                     ExpressionKind kind);
   ~LLVMUserExpression() override;
 
   bool FinalizeJITExecution(

Modified: lldb/trunk/include/lldb/Expression/UserExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/UserExpression.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/UserExpression.h (original)
+++ lldb/trunk/include/lldb/Expression/UserExpression.h Wed Mar 13 12:46:30 2019
@@ -36,6 +36,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class UserExpression : public Expression {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindUserExpression;
+  }
+  
   enum { kDefaultTimeout = 500000u };
 
   //------------------------------------------------------------------
@@ -60,7 +65,8 @@ public:
   UserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
                  llvm::StringRef prefix, lldb::LanguageType language,
                  ResultType desired_type,
-                 const EvaluateExpressionOptions &options);
+                 const EvaluateExpressionOptions &options,
+                 ExpressionKind kind);
 
   //------------------------------------------------------------------
   /// Destructor

Modified: lldb/trunk/include/lldb/Expression/UtilityFunction.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Expression/UtilityFunction.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Expression/UtilityFunction.h (original)
+++ lldb/trunk/include/lldb/Expression/UtilityFunction.h Wed Mar 13 12:46:30 2019
@@ -31,6 +31,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class UtilityFunction : public Expression {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindUtilityFunction;
+  }
+  
   //------------------------------------------------------------------
   /// Constructor
   ///
@@ -41,7 +46,7 @@ public:
   ///     The name of the function, as used in the text.
   //------------------------------------------------------------------
   UtilityFunction(ExecutionContextScope &exe_scope, const char *text,
-                  const char *name);
+                  const char *name, ExpressionKind kind);
 
   ~UtilityFunction() override;
 

Modified: lldb/trunk/source/Expression/Expression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/Expression.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Expression/Expression.cpp (original)
+++ lldb/trunk/source/Expression/Expression.cpp Wed Mar 13 12:46:30 2019
@@ -12,16 +12,18 @@
 
 using namespace lldb_private;
 
-Expression::Expression(Target &target)
-    : m_target_wp(target.shared_from_this()),
+Expression::Expression(Target &target, ExpressionKind kind)
+    : m_kind(kind),
+      m_target_wp(target.shared_from_this()),
       m_jit_start_addr(LLDB_INVALID_ADDRESS),
       m_jit_end_addr(LLDB_INVALID_ADDRESS) {
   // Can't make any kind of expression without a target.
   assert(m_target_wp.lock());
 }
 
-Expression::Expression(ExecutionContextScope &exe_scope)
-    : m_target_wp(exe_scope.CalculateTarget()),
+Expression::Expression(ExecutionContextScope &exe_scope, ExpressionKind kind)
+    : m_kind(kind),
+      m_target_wp(exe_scope.CalculateTarget()),
       m_jit_start_addr(LLDB_INVALID_ADDRESS),
       m_jit_end_addr(LLDB_INVALID_ADDRESS) {
   assert(m_target_wp.lock());

Modified: lldb/trunk/source/Expression/FunctionCaller.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/FunctionCaller.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Expression/FunctionCaller.cpp (original)
+++ lldb/trunk/source/Expression/FunctionCaller.cpp Wed Mar 13 12:46:30 2019
@@ -37,7 +37,8 @@ FunctionCaller::FunctionCaller(Execution
                                const Address &functionAddress,
                                const ValueList &arg_value_list,
                                const char *name)
-    : Expression(exe_scope), m_execution_unit_sp(), m_parser(),
+    : Expression(exe_scope, eKindFunctionCaller),
+      m_execution_unit_sp(), m_parser(),
       m_jit_module_wp(), m_name(name ? name : "<unknown>"),
       m_function_ptr(NULL), m_function_addr(functionAddress),
       m_function_return_type(return_type),

Modified: lldb/trunk/source/Expression/LLVMUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/LLVMUserExpression.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Expression/LLVMUserExpression.cpp (original)
+++ lldb/trunk/source/Expression/LLVMUserExpression.cpp Wed Mar 13 12:46:30 2019
@@ -41,8 +41,10 @@ LLVMUserExpression::LLVMUserExpression(E
                                        llvm::StringRef prefix,
                                        lldb::LanguageType language,
                                        ResultType desired_type,
-                                       const EvaluateExpressionOptions &options)
-    : UserExpression(exe_scope, expr, prefix, language, desired_type, options),
+                                       const EvaluateExpressionOptions &options,
+                                       ExpressionKind kind)
+    : UserExpression(exe_scope, expr, prefix, language, desired_type, options,
+                     kind),
       m_stack_frame_bottom(LLDB_INVALID_ADDRESS),
       m_stack_frame_top(LLDB_INVALID_ADDRESS), m_allow_cxx(false),
       m_allow_objc(false), m_transformed_text(), m_execution_unit_sp(),

Modified: lldb/trunk/source/Expression/UserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/UserExpression.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Expression/UserExpression.cpp (original)
+++ lldb/trunk/source/Expression/UserExpression.cpp Wed Mar 13 12:46:30 2019
@@ -48,8 +48,9 @@ UserExpression::UserExpression(Execution
                                llvm::StringRef expr, llvm::StringRef prefix,
                                lldb::LanguageType language,
                                ResultType desired_type,
-                               const EvaluateExpressionOptions &options)
-    : Expression(exe_scope), m_expr_text(expr), m_expr_prefix(prefix),
+                               const EvaluateExpressionOptions &options,
+                               ExpressionKind kind)
+    : Expression(exe_scope, kind), m_expr_text(expr), m_expr_prefix(prefix),
       m_language(language), m_desired_type(desired_type), m_options(options) {}
 
 UserExpression::~UserExpression() {}

Modified: lldb/trunk/source/Expression/UtilityFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Expression/UtilityFunction.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Expression/UtilityFunction.cpp (original)
+++ lldb/trunk/source/Expression/UtilityFunction.cpp Wed Mar 13 12:46:30 2019
@@ -39,8 +39,10 @@ using namespace lldb;
 ///     The name of the function, as used in the text.
 //------------------------------------------------------------------
 UtilityFunction::UtilityFunction(ExecutionContextScope &exe_scope,
-                                 const char *text, const char *name)
-    : Expression(exe_scope), m_execution_unit_sp(), m_jit_module_wp(),
+                                 const char *text, const char *name,
+                                 ExpressionKind kind)
+    : Expression(exe_scope, kind),
+      m_execution_unit_sp(), m_jit_module_wp(),
       m_function_text(),
       m_function_name(name) {}
 

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp Wed Mar 13 12:46:30 2019
@@ -492,8 +492,8 @@ ClangExpressionParser::ClangExpressionPa
   // long time parsing and importing debug information.
   lang_opts.SpellChecking = false;
 
-  auto &clang_expr = *static_cast<ClangUserExpression *>(&m_expr);
-  if (clang_expr.DidImportCxxModules()) {
+  auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr);
+  if (clang_expr && clang_expr->DidImportCxxModules()) {
     LLDB_LOG(log, "Adding lang options for importing C++ modules");
 
     lang_opts.Modules = true;

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangFunctionCaller.h Wed Mar 13 12:46:30 2019
@@ -61,6 +61,11 @@ class ClangExpressionParser;
 class ClangFunctionCaller : public FunctionCaller {
   friend class ASTStructExtractor;
 
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindClangFunctionCaller;
+  }
+
   class ClangFunctionCallerHelper : public ClangExpressionHelper {
   public:
     ClangFunctionCallerHelper(ClangFunctionCaller &owner) : m_owner(owner) {}

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp Wed Mar 13 12:46:30 2019
@@ -64,7 +64,7 @@ ClangUserExpression::ClangUserExpression
     ResultType desired_type, const EvaluateExpressionOptions &options,
     ValueObject *ctx_obj)
     : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
-                         options),
+                         options, eKindClangUserExpression),
       m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
                                                     eExecutionPolicyTopLevel),
       m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUserExpression.h Wed Mar 13 12:46:30 2019
@@ -40,6 +40,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class ClangUserExpression : public LLVMUserExpression {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindClangUserExpression;
+  }
+
   enum { kDefaultTimeout = 500000u };
 
   class ClangUserExpressionHelper : public ClangExpressionHelper {

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp Wed Mar 13 12:46:30 2019
@@ -40,7 +40,7 @@ using namespace lldb_private;
 //------------------------------------------------------------------
 ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope,
                                            const char *text, const char *name)
-    : UtilityFunction(exe_scope, text, name) {
+    : UtilityFunction(exe_scope, text, name, eKindClangUtilityFunction) {
   m_function_text.assign(ClangExpressionSourceCode::g_expression_prefix);
   if (text && text[0])
     m_function_text.append(text);

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h?rev=356090&r1=356089&r2=356090&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.h Wed Mar 13 12:46:30 2019
@@ -36,6 +36,11 @@ namespace lldb_private {
 //----------------------------------------------------------------------
 class ClangUtilityFunction : public UtilityFunction {
 public:
+  /// LLVM-style RTTI support.
+  static bool classof(const Expression *E) {
+    return E->getKind() == eKindClangUtilityFunction;
+  }
+
   class ClangUtilityFunctionHelper : public ClangExpressionHelper {
   public:
     ClangUtilityFunctionHelper() {}




More information about the lldb-commits mailing list