[Expression] Move IRDynamicChecks to ClangExpressionParser
authorAlex Langford <apl@fb.com>
Fri, 12 Jul 2019 00:58:02 +0000 (00:58 +0000)
committerAlex Langford <apl@fb.com>
Fri, 12 Jul 2019 00:58:02 +0000 (00:58 +0000)
Summary:
IRDynamicChecks in its current form is specific to Clang since it deals
with the C language family. It is possible that we may want to
instrument code generated for other languages, but we can factor in a
more general mechanism to do so at a later time.

This decouples ObCLanguageRuntime from Expression!

Reviewers: compnerd, clayborg, jingham, JDevlieghere

Subscribers: mgorny, lldb-commits

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

llvm-svn: 365853

lldb/include/lldb/Expression/DynamicCheckerFunctions.h [new file with mode: 0644]
lldb/source/Expression/CMakeLists.txt
lldb/source/Plugins/ExpressionParser/Clang/CMakeLists.txt
lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp
lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp [moved from lldb/source/Expression/IRDynamicChecks.cpp with 96% similarity]
lldb/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.h [moved from lldb/include/lldb/Expression/IRDynamicChecks.h with 79% similarity]
lldb/source/Target/Process.cpp
lldb/source/Target/ThreadPlanCallUserExpression.cpp

diff --git a/lldb/include/lldb/Expression/DynamicCheckerFunctions.h b/lldb/include/lldb/Expression/DynamicCheckerFunctions.h
new file mode 100644 (file)
index 0000000..7be24b6
--- /dev/null
@@ -0,0 +1,62 @@
+//===-- DynamicCheckerFunctions.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 liblldb_DynamicCheckerFunctions_h_
+#define liblldb_DynamicCheckerFunctions_h_
+
+#include "lldb/lldb-types.h"
+
+namespace lldb_private {
+
+class DiagnosticManager;
+class ExecutionContext;
+
+/// Encapsulates dynamic check functions used by expressions.
+///
+/// Each of the utility functions encapsulated in this class is responsible
+/// for validating some data that an expression is about to use.  Examples
+/// are:
+///
+/// a = *b;     // check that b is a valid pointer
+/// [b init];   // check that b is a valid object to send "init" to
+///
+/// The class installs each checker function into the target process and makes
+/// it available to IRDynamicChecks to use.
+class DynamicCheckerFunctions {
+public:
+  enum DynamicCheckerFunctionsKind {
+    DCF_Clang,
+  };
+
+  DynamicCheckerFunctions(DynamicCheckerFunctionsKind kind) : m_kind(kind) {}
+  virtual ~DynamicCheckerFunctions() = default;
+
+  /// Install the utility functions into a process.  This binds the instance
+  /// of DynamicCheckerFunctions to that process.
+  ///
+  /// \param[in] diagnostic_manager
+  ///     A diagnostic manager to report errors to.
+  ///
+  /// \param[in] exe_ctx
+  ///     The execution context to install the functions into.
+  ///
+  /// \return
+  ///     True on success; false on failure, or if the functions have
+  ///     already been installed.
+  virtual bool Install(DiagnosticManager &diagnostic_manager,
+                       ExecutionContext &exe_ctx) = 0;
+  virtual bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) = 0;
+
+  DynamicCheckerFunctionsKind GetKind() const { return m_kind; }
+
+private:
+  const DynamicCheckerFunctionsKind m_kind;
+};
+} // namespace lldb_private
+
+#endif // liblldb_DynamicCheckerFunctions_h_
index fead8d9..7e2f19e 100644 (file)
@@ -8,7 +8,6 @@ add_lldb_library(lldbExpression
   Expression.cpp
   ExpressionVariable.cpp
   FunctionCaller.cpp
-  IRDynamicChecks.cpp
   IRExecutionUnit.cpp
   IRInterpreter.cpp
   IRMemoryMap.cpp
index 084c5b0..16010bc 100644 (file)
@@ -19,6 +19,7 @@ add_lldb_library(lldbPluginExpressionParserClang PLUGIN
   ClangUserExpression.cpp
   ClangUtilityFunction.cpp
   IRForTarget.cpp
+  IRDynamicChecks.cpp
 
   DEPENDS
   ${tablegen_deps}
index a2bc387..1c7f931 100644 (file)
@@ -62,6 +62,7 @@
 #include "ClangHost.h"
 #include "ClangModulesDeclVendor.h"
 #include "ClangPersistentVariables.h"
+#include "IRDynamicChecks.h"
 #include "IRForTarget.h"
 #include "ModuleDependencyCollector.h"
 
@@ -69,7 +70,6 @@
 #include "lldb/Core/Disassembler.h"
 #include "lldb/Core/Module.h"
 #include "lldb/Core/StreamFile.h"
-#include "lldb/Expression/IRDynamicChecks.h"
 #include "lldb/Expression/IRExecutionUnit.h"
 #include "lldb/Expression/IRInterpreter.h"
 #include "lldb/Host/File.h"
@@ -1281,8 +1281,8 @@ lldb_private::Status ClangExpressionParser::PrepareForExecution(
         (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) {
       if (m_expr.NeedsValidation() && process) {
         if (!process->GetDynamicCheckers()) {
-          DynamicCheckerFunctions *dynamic_checkers =
-              new DynamicCheckerFunctions();
+          ClangDynamicCheckerFunctions *dynamic_checkers =
+              new ClangDynamicCheckerFunctions();
 
           DiagnosticManager install_diagnostics;
 
@@ -1302,23 +1302,26 @@ lldb_private::Status ClangExpressionParser::PrepareForExecution(
                         "Finished installing dynamic checkers ==");
         }
 
-        IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(),
-                                          function_name.AsCString());
+        if (auto *checker_funcs = llvm::dyn_cast<ClangDynamicCheckerFunctions>(
+                process->GetDynamicCheckers())) {
+          IRDynamicChecks ir_dynamic_checks(*checker_funcs,
+                                            function_name.AsCString());
 
-        llvm::Module *module = execution_unit_sp->GetModule();
-        if (!module || !ir_dynamic_checks.runOnModule(*module)) {
-          err.SetErrorToGenericError();
-          err.SetErrorString("Couldn't add dynamic checks to the expression");
-          return err;
-        }
+          llvm::Module *module = execution_unit_sp->GetModule();
+          if (!module || !ir_dynamic_checks.runOnModule(*module)) {
+            err.SetErrorToGenericError();
+            err.SetErrorString("Couldn't add dynamic checks to the expression");
+            return err;
+          }
 
-        if (custom_passes.LatePasses) {
-          if (log)
-            log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
-                        "expression module '%s'",
-                        __FUNCTION__, m_expr.FunctionName());
+          if (custom_passes.LatePasses) {
+            if (log)
+              log->Printf("%s - Running Late IR Passes from LanguageRuntime on "
+                          "expression module '%s'",
+                          __FUNCTION__, m_expr.FunctionName());
 
-          custom_passes.LatePasses->run(*module);
+            custom_passes.LatePasses->run(*module);
+          }
         }
       }
     }
@@ -14,7 +14,7 @@
 #include "llvm/IR/Value.h"
 #include "llvm/Support/raw_ostream.h"
 
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "IRDynamicChecks.h"
 
 #include "lldb/Expression/UtilityFunction.h"
 #include "lldb/Target/ExecutionContext.h"
@@ -40,12 +40,13 @@ static const char g_valid_pointer_check_text[] =
     "    unsigned char $__lldb_local_val = *$__lldb_arg_ptr;\n"
     "}";
 
-DynamicCheckerFunctions::DynamicCheckerFunctions() = default;
+ClangDynamicCheckerFunctions::ClangDynamicCheckerFunctions()
+    : DynamicCheckerFunctions(DCF_Clang) {}
 
-DynamicCheckerFunctions::~DynamicCheckerFunctions() = default;
+ClangDynamicCheckerFunctions::~ClangDynamicCheckerFunctions() = default;
 
-bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
-                                      ExecutionContext &exe_ctx) {
+bool ClangDynamicCheckerFunctions::Install(
+    DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx) {
   Status error;
   m_valid_pointer_check.reset(
       exe_ctx.GetTargetRef().GetUtilityFunctionForLanguage(
@@ -75,8 +76,8 @@ bool DynamicCheckerFunctions::Install(DiagnosticManager &diagnostic_manager,
   return true;
 }
 
-bool DynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
-                                                    Stream &message) {
+bool ClangDynamicCheckerFunctions::DoCheckersExplainStop(lldb::addr_t addr,
+                                                         Stream &message) {
   // FIXME: We have to get the checkers to know why they scotched the call in
   // more detail,
   // so we can print a better message here.
@@ -533,8 +534,8 @@ private:
   llvm::FunctionCallee m_objc_object_check_func;
 };
 
-IRDynamicChecks::IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
-                                 const char *func_name)
+IRDynamicChecks::IRDynamicChecks(
+    ClangDynamicCheckerFunctions &checker_functions, const char *func_name)
     : ModulePass(ID), m_func_name(func_name),
       m_checker_functions(checker_functions) {}
 
@@ -9,46 +9,32 @@
 #ifndef liblldb_IRDynamicChecks_h_
 #define liblldb_IRDynamicChecks_h_
 
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/lldb-types.h"
 #include "llvm/Pass.h"
 
 namespace llvm {
 class BasicBlock;
-class CallInst;
-class Constant;
-class Function;
-class Instruction;
 class Module;
-class DataLayout;
-class Value;
 }
 
 namespace lldb_private {
 
-class ClangExpressionDeclMap;
 class ExecutionContext;
 class Stream;
 
-/// \class DynamicCheckerFunctions IRDynamicChecks.h
-/// "lldb/Expression/IRDynamicChecks.h" Encapsulates dynamic check functions
-/// used by expressions.
-///
-/// Each of the utility functions encapsulated in this class is responsible
-/// for validating some data that an expression is about to use.  Examples
-/// are:
-///
-/// a = *b;     // check that b is a valid pointer [b init];   // check that b
-/// is a valid object to send "init" to
-///
-/// The class installs each checker function into the target process and makes
-/// it available to IRDynamicChecks to use.
-class DynamicCheckerFunctions {
+class ClangDynamicCheckerFunctions
+    : public lldb_private::DynamicCheckerFunctions {
 public:
   /// Constructor
-  DynamicCheckerFunctions();
+  ClangDynamicCheckerFunctions();
 
   /// Destructor
-  ~DynamicCheckerFunctions();
+  virtual ~ClangDynamicCheckerFunctions();
+
+  static bool classof(const DynamicCheckerFunctions *checker_funcs) {
+    return checker_funcs->GetKind() == DCF_Clang;
+  }
 
   /// Install the utility functions into a process.  This binds the instance
   /// of DynamicCheckerFunctions to that process.
@@ -63,9 +49,9 @@ public:
   ///     True on success; false on failure, or if the functions have
   ///     already been installed.
   bool Install(DiagnosticManager &diagnostic_manager,
-               ExecutionContext &exe_ctx);
+               ExecutionContext &exe_ctx) override;
 
-  bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message);
+  bool DoCheckersExplainStop(lldb::addr_t addr, Stream &message) override;
 
   std::shared_ptr<UtilityFunction> m_valid_pointer_check;
   std::shared_ptr<UtilityFunction> m_objc_object_check;
@@ -94,7 +80,7 @@ public:
   /// \param[in] decl_map
   ///     The mapping used to look up entities in the target process. In
   ///     this case, used to find objc_msgSend
-  IRDynamicChecks(DynamicCheckerFunctions &checker_functions,
+  IRDynamicChecks(ClangDynamicCheckerFunctions &checker_functions,
                   const char *func_name = "$__lldb_expr");
 
   /// Destructor
@@ -136,7 +122,7 @@ private:
   bool FindDataLoads(llvm::Module &M, llvm::BasicBlock &BB);
 
   std::string m_func_name; ///< The name of the function to add checks to
-  DynamicCheckerFunctions
+  ClangDynamicCheckerFunctions
       &m_checker_functions; ///< The checker functions for the process
 };
 
index cd27392..6c634db 100644 (file)
@@ -22,7 +22,7 @@
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/StreamFile.h"
 #include "lldb/Expression/DiagnosticManager.h"
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/Expression/UserExpression.h"
 #include "lldb/Expression/UtilityFunction.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"
index 2998daa..864808a 100644 (file)
@@ -13,7 +13,7 @@
 #include "lldb/Breakpoint/BreakpointLocation.h"
 #include "lldb/Core/Address.h"
 #include "lldb/Expression/DiagnosticManager.h"
-#include "lldb/Expression/IRDynamicChecks.h"
+#include "lldb/Expression/DynamicCheckerFunctions.h"
 #include "lldb/Expression/UserExpression.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Target/LanguageRuntime.h"