Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 9 | #include "clang/AST/ASTContext.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTDiagnostic.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 11 | #include "clang/AST/ExternalASTSource.h" |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 12 | #include "clang/AST/PrettyPrinter.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 13 | #include "clang/Basic/DiagnosticIDs.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 14 | #include "clang/Basic/SourceLocation.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 15 | #include "clang/Basic/TargetInfo.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Version.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 17 | #include "clang/CodeGen/CodeGenAction.h" |
| 18 | #include "clang/CodeGen/ModuleBuilder.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 19 | #include "clang/Edit/Commit.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 20 | #include "clang/Edit/EditedSource.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | #include "clang/Edit/EditsReceiver.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
| 23 | #include "clang/Frontend/CompilerInvocation.h" |
| 24 | #include "clang/Frontend/FrontendActions.h" |
| 25 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 26 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 27 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 28 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 29 | #include "clang/Lex/Preprocessor.h" |
Sean Callanan | e2ef6e3 | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 30 | #include "clang/Parse/ParseAST.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 31 | #include "clang/Rewrite/Core/Rewriter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 33 | #include "clang/Sema/CodeCompleteConsumer.h" |
| 34 | #include "clang/Sema/Sema.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 35 | #include "clang/Sema/SemaConsumer.h" |
| 36 | |
| 37 | #include "llvm/ADT/StringRef.h" |
| 38 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Sean Callanan | 3d654b3 | 2012-09-24 22:25:51 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 4609ea8 | 2013-06-26 14:10:56 +0000 | [diff] [blame] | 40 | #include "llvm/Support/FileSystem.h" |
Sean Callanan | 880e680 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 41 | #include "llvm/Support/TargetSelect.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 42 | |
Greg Clayton | c9a078a | 2016-03-24 23:50:03 +0000 | [diff] [blame] | 43 | #pragma clang diagnostic push |
| 44 | #pragma clang diagnostic ignored "-Wglobal-constructors" |
Ed Maste | a09ed03 | 2014-03-18 18:55:06 +0000 | [diff] [blame] | 45 | #include "llvm/ExecutionEngine/MCJIT.h" |
Greg Clayton | c9a078a | 2016-03-24 23:50:03 +0000 | [diff] [blame] | 46 | #pragma clang diagnostic pop |
| 47 | |
Chandler Carruth | 1e15758 | 2013-01-02 12:20:07 +0000 | [diff] [blame] | 48 | #include "llvm/IR/LLVMContext.h" |
| 49 | #include "llvm/IR/Module.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 50 | #include "llvm/Support/DynamicLibrary.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | #include "llvm/Support/ErrorHandling.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Host.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | #include "llvm/Support/MemoryBuffer.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Signals.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 55 | |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 56 | #include "ClangASTSource.h" |
Jonas Devlieghere | 63e2e59 | 2019-02-13 01:30:41 +0000 | [diff] [blame] | 57 | #include "ClangDiagnostic.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 58 | #include "ClangExpressionDeclMap.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | #include "ClangExpressionHelper.h" |
Jonas Devlieghere | 63e2e59 | 2019-02-13 01:30:41 +0000 | [diff] [blame] | 60 | #include "ClangExpressionParser.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 61 | #include "ClangModulesDeclVendor.h" |
| 62 | #include "ClangPersistentVariables.h" |
| 63 | #include "IRForTarget.h" |
Jonas Devlieghere | 63e2e59 | 2019-02-13 01:30:41 +0000 | [diff] [blame] | 64 | #include "ModuleDependencyCollector.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 65 | |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 66 | #include "lldb/Core/Debugger.h" |
| 67 | #include "lldb/Core/Disassembler.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 68 | #include "lldb/Core/Module.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 69 | #include "lldb/Core/StreamFile.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 70 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 71 | #include "lldb/Expression/IRExecutionUnit.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 72 | #include "lldb/Expression/IRInterpreter.h" |
| 73 | #include "lldb/Host/File.h" |
| 74 | #include "lldb/Host/HostInfo.h" |
| 75 | #include "lldb/Symbol/ClangASTContext.h" |
| 76 | #include "lldb/Symbol/SymbolVendor.h" |
| 77 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 78 | #include "lldb/Target/Language.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 79 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 80 | #include "lldb/Target/Process.h" |
| 81 | #include "lldb/Target/Target.h" |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 82 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 83 | #include "lldb/Utility/DataBufferHeap.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 84 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 85 | #include "lldb/Utility/Log.h" |
Jonas Devlieghere | 63e2e59 | 2019-02-13 01:30:41 +0000 | [diff] [blame] | 86 | #include "lldb/Utility/Reproducer.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 87 | #include "lldb/Utility/Stream.h" |
| 88 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 573ab90 | 2017-03-21 18:25:04 +0000 | [diff] [blame] | 89 | #include "lldb/Utility/StringList.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 90 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 91 | #include <cctype> |
| 92 | #include <memory> |
| 93 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 94 | using namespace clang; |
| 95 | using namespace llvm; |
| 96 | using namespace lldb_private; |
| 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | // Utility Methods for Clang |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks { |
| 103 | ClangModulesDeclVendor &m_decl_vendor; |
| 104 | ClangPersistentVariables &m_persistent_vars; |
| 105 | StreamString m_error_stream; |
| 106 | bool m_has_errors = false; |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 107 | |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 108 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor, |
| 110 | ClangPersistentVariables &persistent_vars) |
| 111 | : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {} |
| 112 | |
| 113 | void moduleImport(SourceLocation import_location, clang::ModuleIdPath path, |
| 114 | const clang::Module * /*null*/) override { |
Adrian Prantl | 0f30a3b | 2019-02-13 18:10:41 +0000 | [diff] [blame] | 115 | SourceModule module; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | |
Adrian Prantl | 0f30a3b | 2019-02-13 18:10:41 +0000 | [diff] [blame] | 117 | for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) |
| 118 | module.path.push_back(ConstString(component.first->getName())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | |
| 120 | StreamString error_stream; |
| 121 | |
| 122 | ClangModulesDeclVendor::ModuleVector exported_modules; |
Adrian Prantl | 0f30a3b | 2019-02-13 18:10:41 +0000 | [diff] [blame] | 123 | if (!m_decl_vendor.AddModule(module, &exported_modules, m_error_stream)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | m_has_errors = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | |
Adrian Prantl | 0f30a3b | 2019-02-13 18:10:41 +0000 | [diff] [blame] | 126 | for (ClangModulesDeclVendor::ModuleID module : exported_modules) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | m_persistent_vars.AddHandLoadedClangModule(module); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bool hasErrors() { return m_has_errors; } |
| 131 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 132 | llvm::StringRef getErrorString() { return m_error_stream.GetString(); } |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer { |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 136 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | ClangDiagnosticManagerAdapter() |
| 138 | : m_passthrough(new clang::TextDiagnosticBuffer) {} |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | ClangDiagnosticManagerAdapter( |
| 141 | const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough) |
| 142 | : m_passthrough(passthrough) {} |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | void ResetManager(DiagnosticManager *manager = nullptr) { |
| 145 | m_manager = manager; |
| 146 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 149 | const clang::Diagnostic &Info) { |
| 150 | if (m_manager) { |
| 151 | llvm::SmallVector<char, 32> diag_str; |
| 152 | Info.FormatDiagnostic(diag_str); |
| 153 | diag_str.push_back('\0'); |
| 154 | const char *data = diag_str.data(); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | lldb_private::DiagnosticSeverity severity; |
| 157 | bool make_new_diagnostic = true; |
| 158 | |
| 159 | switch (DiagLevel) { |
| 160 | case DiagnosticsEngine::Level::Fatal: |
| 161 | case DiagnosticsEngine::Level::Error: |
| 162 | severity = eDiagnosticSeverityError; |
| 163 | break; |
| 164 | case DiagnosticsEngine::Level::Warning: |
| 165 | severity = eDiagnosticSeverityWarning; |
| 166 | break; |
| 167 | case DiagnosticsEngine::Level::Remark: |
| 168 | case DiagnosticsEngine::Level::Ignored: |
| 169 | severity = eDiagnosticSeverityRemark; |
| 170 | break; |
| 171 | case DiagnosticsEngine::Level::Note: |
| 172 | m_manager->AppendMessageToDiagnostic(data); |
| 173 | make_new_diagnostic = false; |
| 174 | } |
| 175 | if (make_new_diagnostic) { |
| 176 | ClangDiagnostic *new_diagnostic = |
| 177 | new ClangDiagnostic(data, severity, Info.getID()); |
| 178 | m_manager->AddDiagnostic(new_diagnostic); |
| 179 | |
| 180 | // Don't store away warning fixits, since the compiler doesn't have |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 181 | // enough context in an expression for the warning to be useful. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | // FIXME: Should we try to filter out FixIts that apply to our generated |
| 183 | // code, and not the user's expression? |
| 184 | if (severity == eDiagnosticSeverityError) { |
| 185 | size_t num_fixit_hints = Info.getNumFixItHints(); |
| 186 | for (size_t i = 0; i < num_fixit_hints; i++) { |
| 187 | const clang::FixItHint &fixit = Info.getFixItHint(i); |
| 188 | if (!fixit.isNull()) |
| 189 | new_diagnostic->AddFixitHint(fixit); |
| 190 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 191 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | m_passthrough->HandleDiagnostic(DiagLevel, Info); |
| 196 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | void FlushDiagnostics(DiagnosticsEngine &Diags) { |
| 199 | m_passthrough->FlushDiagnostics(Diags); |
| 200 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 203 | return new ClangDiagnosticManagerAdapter(m_passthrough); |
| 204 | } |
| 205 | |
| 206 | clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 207 | |
| 208 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | DiagnosticManager *m_manager = nullptr; |
| 210 | std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough; |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
| 214 | // Implementation of ClangExpressionParser |
| 215 | //===----------------------------------------------------------------------===// |
| 216 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope, |
| 218 | Expression &expr, |
| 219 | bool generate_debug_info) |
| 220 | : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(), |
Adrian Prantl | 2305c04 | 2018-08-30 15:39:08 +0000 | [diff] [blame] | 221 | m_pp_callbacks(nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 223 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 224 | // We can't compile expressions without a target. So if the exe_scope is |
| 225 | // null or doesn't have a target, then we just need to get out of here. I'll |
| 226 | // lldb_assert and not make any of the compiler objects since |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | // I can't return errors directly from the constructor. Further calls will |
| 228 | // check if the compiler was made and |
| 229 | // bag out if it wasn't. |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 230 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | if (!exe_scope) { |
| 232 | lldb_assert(exe_scope, "Can't make an expression parser with a null scope.", |
| 233 | __FUNCTION__, __FILE__, __LINE__); |
| 234 | return; |
| 235 | } |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | lldb::TargetSP target_sp; |
| 238 | target_sp = exe_scope->CalculateTarget(); |
| 239 | if (!target_sp) { |
| 240 | lldb_assert(target_sp.get(), |
| 241 | "Can't make an expression parser with a null target.", |
| 242 | __FUNCTION__, __FILE__, __LINE__); |
| 243 | return; |
| 244 | } |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 245 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | // 1. Create a new compiler instance. |
| 247 | m_compiler.reset(new CompilerInstance()); |
Jonas Devlieghere | 63e2e59 | 2019-02-13 01:30:41 +0000 | [diff] [blame] | 248 | |
| 249 | // When capturing a reproducer, hook up the file collector with clang to |
| 250 | // collector modules and headers. |
| 251 | if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) { |
| 252 | repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>(); |
| 253 | m_compiler->setModuleDepCollector( |
| 254 | std::make_shared<ModuleDependencyCollectorAdaptor>( |
| 255 | fp.GetFileCollector())); |
| 256 | DependencyOutputOptions &opts = m_compiler->getDependencyOutputOpts(); |
| 257 | opts.IncludeSystemHeaders = true; |
| 258 | opts.IncludeModuleFiles = true; |
| 259 | } |
| 260 | |
Jonas Devlieghere | 9764b65 | 2019-02-18 20:31:18 +0000 | [diff] [blame] | 261 | // Make sure clang uses the same VFS as LLDB. |
| 262 | m_compiler->setVirtualFileSystem( |
| 263 | FileSystem::Instance().GetVirtualFileSystem()); |
| 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | lldb::LanguageType frame_lang = |
| 266 | expr.Language(); // defaults to lldb::eLanguageTypeUnknown |
| 267 | bool overridden_target_opts = false; |
| 268 | lldb_private::LanguageRuntime *lang_rt = nullptr; |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | std::string abi; |
| 271 | ArchSpec target_arch; |
| 272 | target_arch = target_sp->GetArchitecture(); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | const auto target_machine = target_arch.GetMachine(); |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 275 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 276 | // If the expression is being evaluated in the context of an existing stack |
| 277 | // frame, we introspect to see if the language runtime is available. |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame(); |
| 280 | lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 281 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 282 | // Make sure the user hasn't provided a preferred execution language with |
| 283 | // `expression --language X -- ...` |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown) |
| 285 | frame_lang = frame_sp->GetLanguage(); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 287 | if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) { |
| 288 | lang_rt = process_sp->GetLanguageRuntime(frame_lang); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 289 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 290 | log->Printf("Frame has language of type %s", |
| 291 | Language::GetNameForLanguageType(frame_lang)); |
| 292 | } |
Alp Toker | 5f83864 | 2014-07-06 05:36:57 +0000 | [diff] [blame] | 293 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | // 2. Configure the compiler with a set of default options that are |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 295 | // appropriate for most situations. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | if (target_arch.IsValid()) { |
| 297 | std::string triple = target_arch.GetTriple().str(); |
| 298 | m_compiler->getTargetOpts().Triple = triple; |
| 299 | if (log) |
| 300 | log->Printf("Using %s as the target triple", |
| 301 | m_compiler->getTargetOpts().Triple.c_str()); |
| 302 | } else { |
| 303 | // If we get here we don't have a valid target and just have to guess. |
| 304 | // Sometimes this will be ok to just use the host target triple (when we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 305 | // evaluate say "2+3", but other expressions like breakpoint conditions and |
| 306 | // other things that _are_ target specific really shouldn't just be using |
| 307 | // the host triple. In such a case the language runtime should expose an |
| 308 | // overridden options set (3), below. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); |
| 310 | if (log) |
| 311 | log->Printf("Using default target triple of %s", |
| 312 | m_compiler->getTargetOpts().Triple.c_str()); |
| 313 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 314 | // Now add some special fixes for known architectures: Any arm32 iOS |
| 315 | // environment, but not on arm64 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 316 | if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos && |
| 317 | m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos && |
| 318 | m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) { |
| 319 | m_compiler->getTargetOpts().ABI = "apcs-gnu"; |
| 320 | } |
| 321 | // Supported subsets of x86 |
| 322 | if (target_machine == llvm::Triple::x86 || |
| 323 | target_machine == llvm::Triple::x86_64) { |
| 324 | m_compiler->getTargetOpts().Features.push_back("+sse"); |
| 325 | m_compiler->getTargetOpts().Features.push_back("+sse2"); |
| 326 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 327 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 328 | // Set the target CPU to generate code for. This will be empty for any CPU |
| 329 | // that doesn't really need to make a special |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | // CPU string. |
| 331 | m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU(); |
Ewan Crawford | 7648dd3 | 2016-03-10 12:38:55 +0000 | [diff] [blame] | 332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 333 | // Set the target ABI |
| 334 | abi = GetClangTargetABI(target_arch); |
| 335 | if (!abi.empty()) |
| 336 | m_compiler->getTargetOpts().ABI = abi; |
| 337 | |
| 338 | // 3. Now allow the runtime to provide custom configuration options for the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 339 | // target. In this case, a specialized language runtime is available and we |
| 340 | // can query it for extra options. For 99% of use cases, this will not be |
| 341 | // needed and should be provided when basic platform detection is not enough. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | if (lang_rt) |
| 343 | overridden_target_opts = |
| 344 | lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts()); |
| 345 | |
| 346 | if (overridden_target_opts) |
Pavel Labath | 6302bf6 | 2017-02-13 11:03:17 +0000 | [diff] [blame] | 347 | if (log && log->GetVerbose()) { |
| 348 | LLDB_LOGV( |
| 349 | log, "Using overridden target options for the expression evaluation"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 350 | |
| 351 | auto opts = m_compiler->getTargetOpts(); |
Pavel Labath | 6302bf6 | 2017-02-13 11:03:17 +0000 | [diff] [blame] | 352 | LLDB_LOGV(log, "Triple: '{0}'", opts.Triple); |
| 353 | LLDB_LOGV(log, "CPU: '{0}'", opts.CPU); |
| 354 | LLDB_LOGV(log, "FPMath: '{0}'", opts.FPMath); |
| 355 | LLDB_LOGV(log, "ABI: '{0}'", opts.ABI); |
| 356 | LLDB_LOGV(log, "LinkerVersion: '{0}'", opts.LinkerVersion); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten"); |
| 358 | StringList::LogDump(log, opts.Features, "Features"); |
Sean Callanan | c7b6506 | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 359 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 360 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | // 4. Create and install the target on the compiler. |
| 362 | m_compiler->createDiagnostics(); |
| 363 | auto target_info = TargetInfo::CreateTargetInfo( |
| 364 | m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts); |
| 365 | if (log) { |
| 366 | log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign()); |
| 367 | log->Printf("Target datalayout string: '%s'", |
| 368 | target_info->getDataLayout().getStringRepresentation().c_str()); |
| 369 | log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str()); |
| 370 | log->Printf("Target vector alignment: %d", |
| 371 | target_info->getMaxVectorAlign()); |
| 372 | } |
| 373 | m_compiler->setTarget(target_info); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 375 | assert(m_compiler->hasTarget()); |
Tamas Berghammer | dccbfaf | 2015-03-31 10:21:50 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | // 5. Set language options. |
| 378 | lldb::LanguageType language = expr.Language(); |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 379 | LangOptions &lang_opts = m_compiler->getLangOpts(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 380 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | switch (language) { |
| 382 | case lldb::eLanguageTypeC: |
| 383 | case lldb::eLanguageTypeC89: |
| 384 | case lldb::eLanguageTypeC99: |
| 385 | case lldb::eLanguageTypeC11: |
| 386 | // FIXME: the following language option is a temporary workaround, |
| 387 | // to "ask for C, get C++." |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 388 | // For now, the expression parser must use C++ anytime the language is a C |
| 389 | // family language, because the expression parser uses features of C++ to |
| 390 | // capture values. |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 391 | lang_opts.CPlusPlus = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | break; |
| 393 | case lldb::eLanguageTypeObjC: |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 394 | lang_opts.ObjC = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | // FIXME: the following language option is a temporary workaround, |
| 396 | // to "ask for ObjC, get ObjC++" (see comment above). |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 397 | lang_opts.CPlusPlus = true; |
Davide Italiano | 89419f8 | 2017-12-15 00:50:43 +0000 | [diff] [blame] | 398 | |
| 399 | // Clang now sets as default C++14 as the default standard (with |
| 400 | // GNU extensions), so we do the same here to avoid mismatches that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 401 | // cause compiler error when evaluating expressions (e.g. nullptr not found |
| 402 | // as it's a C++11 feature). Currently lldb evaluates C++14 as C++11 (see |
| 403 | // two lines below) so we decide to be consistent with that, but this could |
| 404 | // be re-evaluated in the future. |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 405 | lang_opts.CPlusPlus11 = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 406 | break; |
| 407 | case lldb::eLanguageTypeC_plus_plus: |
| 408 | case lldb::eLanguageTypeC_plus_plus_11: |
| 409 | case lldb::eLanguageTypeC_plus_plus_14: |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 410 | lang_opts.CPlusPlus11 = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | m_compiler->getHeaderSearchOpts().UseLibcxx = true; |
| 412 | LLVM_FALLTHROUGH; |
| 413 | case lldb::eLanguageTypeC_plus_plus_03: |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 414 | lang_opts.CPlusPlus = true; |
Aleksandr Urakov | f335188 | 2018-12-04 09:51:29 +0000 | [diff] [blame] | 415 | if (process_sp) |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 416 | lang_opts.ObjC = |
Aleksandr Urakov | f335188 | 2018-12-04 09:51:29 +0000 | [diff] [blame] | 417 | process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC) != nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | break; |
| 419 | case lldb::eLanguageTypeObjC_plus_plus: |
| 420 | case lldb::eLanguageTypeUnknown: |
| 421 | default: |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 422 | lang_opts.ObjC = true; |
| 423 | lang_opts.CPlusPlus = true; |
| 424 | lang_opts.CPlusPlus11 = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | m_compiler->getHeaderSearchOpts().UseLibcxx = true; |
| 426 | break; |
| 427 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 428 | |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 429 | lang_opts.Bool = true; |
| 430 | lang_opts.WChar = true; |
| 431 | lang_opts.Blocks = true; |
| 432 | lang_opts.DebuggerSupport = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | true; // Features specifically for debugger clients |
| 434 | if (expr.DesiredResultType() == Expression::eResultTypeId) |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 435 | lang_opts.DebuggerCastResultToId = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 436 | |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 437 | lang_opts.CharIsSigned = ArchSpec(m_compiler->getTargetOpts().Triple.c_str()) |
| 438 | .CharIsSignedByDefault(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 439 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 440 | // Spell checking is a nice feature, but it ends up completing a lot of types |
| 441 | // that we didn't strictly speaking need to complete. As a result, we spend a |
| 442 | // long time parsing and importing debug information. |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 443 | lang_opts.SpellChecking = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 444 | |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 445 | if (process_sp && lang_opts.ObjC) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | if (process_sp->GetObjCLanguageRuntime()) { |
| 447 | if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == |
| 448 | ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2) |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 449 | lang_opts.ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | else |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 451 | lang_opts.ObjCRuntime.set(ObjCRuntime::FragileMacOSX, |
| 452 | VersionTuple(10, 7)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | |
| 454 | if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing()) |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 455 | lang_opts.DebuggerObjCLiteral = true; |
Sean Callanan | c3a1600 | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 456 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 457 | } |
Greg Clayton | f83f32d | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 458 | |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 459 | lang_opts.ThreadsafeStatics = false; |
| 460 | lang_opts.AccessControl = false; // Debuggers get universal access |
| 461 | lang_opts.DollarIdents = true; // $ indicates a persistent variable name |
Raphael Isemann | d424c2a | 2018-08-30 20:56:58 +0000 | [diff] [blame] | 462 | // We enable all builtin functions beside the builtins from libc/libm (e.g. |
| 463 | // 'fopen'). Those libc functions are already correctly handled by LLDB, and |
| 464 | // additionally enabling them as expandable builtins is breaking Clang. |
Raphael Isemann | 0d649c6 | 2019-01-25 22:41:31 +0000 | [diff] [blame] | 465 | lang_opts.NoBuiltin = true; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 466 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | // Set CodeGen options |
| 468 | m_compiler->getCodeGenOpts().EmitDeclMetadata = true; |
| 469 | m_compiler->getCodeGenOpts().InstrumentFunctions = false; |
| 470 | m_compiler->getCodeGenOpts().DisableFPElim = true; |
| 471 | m_compiler->getCodeGenOpts().OmitLeafFramePointer = false; |
| 472 | if (generate_debug_info) |
| 473 | m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); |
| 474 | else |
| 475 | m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 477 | // Disable some warnings. |
| 478 | m_compiler->getDiagnostics().setSeverityForGroup( |
| 479 | clang::diag::Flavor::WarningOrError, "unused-value", |
| 480 | clang::diag::Severity::Ignored, SourceLocation()); |
| 481 | m_compiler->getDiagnostics().setSeverityForGroup( |
| 482 | clang::diag::Flavor::WarningOrError, "odr", |
| 483 | clang::diag::Severity::Ignored, SourceLocation()); |
Alp Toker | 10933d1 | 2014-06-12 11:14:32 +0000 | [diff] [blame] | 484 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 485 | // Inform the target of the language options |
| 486 | // |
| 487 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 488 | // created. This complexity should be lifted elsewhere. |
| 489 | m_compiler->getTarget().adjust(m_compiler->getLangOpts()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 490 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 491 | // 6. Set up the diagnostic buffer for reporting errors |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 492 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | // 7. Set up the source management objects inside the compiler |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 496 | m_compiler->createFileManager(); |
Jonas Devlieghere | ff04b3d | 2019-02-14 18:05:43 +0000 | [diff] [blame] | 497 | if (!m_compiler->hasSourceManager()) |
| 498 | m_compiler->createSourceManager(m_compiler->getFileManager()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | m_compiler->createPreprocessor(TU_Complete); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 500 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 501 | if (ClangModulesDeclVendor *decl_vendor = |
| 502 | target_sp->GetClangModulesDeclVendor()) { |
| 503 | ClangPersistentVariables *clang_persistent_vars = |
| 504 | llvm::cast<ClangPersistentVariables>( |
| 505 | target_sp->GetPersistentExpressionStateForLanguage( |
| 506 | lldb::eLanguageTypeC)); |
| 507 | std::unique_ptr<PPCallbacks> pp_callbacks( |
| 508 | new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars)); |
| 509 | m_pp_callbacks = |
| 510 | static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get()); |
| 511 | m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks)); |
| 512 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 513 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 514 | // 8. Most of this we get from the CompilerInstance, but we also want to give |
| 515 | // the context an ExternalASTSource. |
Raphael Isemann | 778b308 | 2018-08-23 20:40:45 +0000 | [diff] [blame] | 516 | |
| 517 | auto &PP = m_compiler->getPreprocessor(); |
| 518 | auto &builtin_context = PP.getBuiltinInfo(); |
| 519 | builtin_context.initializeBuiltins(PP.getIdentifierTable(), |
| 520 | m_compiler->getLangOpts()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 521 | |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame] | 522 | m_compiler->createASTContext(); |
| 523 | clang::ASTContext &ast_context = m_compiler->getASTContext(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 524 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | ClangExpressionHelper *type_system_helper = |
| 526 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 527 | ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | if (decl_map) { |
| 530 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source( |
| 531 | decl_map->CreateProxy()); |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame] | 532 | decl_map->InstallASTContext(ast_context, m_compiler->getFileManager()); |
| 533 | ast_context.setExternalSource(ast_source); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | m_ast_context.reset( |
| 537 | new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str())); |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame] | 538 | m_ast_context->setASTContext(&ast_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | |
| 540 | std::string module_name("$__lldb_module"); |
| 541 | |
| 542 | m_llvm_context.reset(new LLVMContext()); |
| 543 | m_code_generator.reset(CreateLLVMCodeGen( |
| 544 | m_compiler->getDiagnostics(), module_name, |
| 545 | m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(), |
| 546 | m_compiler->getCodeGenOpts(), *m_llvm_context)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | ClangExpressionParser::~ClangExpressionParser() {} |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 550 | |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 551 | namespace { |
| 552 | |
| 553 | //---------------------------------------------------------------------- |
| 554 | /// @class CodeComplete |
| 555 | /// |
| 556 | /// A code completion consumer for the clang Sema that is responsible for |
| 557 | /// creating the completion suggestions when a user requests completion |
| 558 | /// of an incomplete `expr` invocation. |
| 559 | //---------------------------------------------------------------------- |
| 560 | class CodeComplete : public CodeCompleteConsumer { |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 561 | CodeCompletionTUInfo m_info; |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 562 | |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 563 | std::string m_expr; |
| 564 | unsigned m_position = 0; |
Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 565 | CompletionRequest &m_request; |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 566 | /// The printing policy we use when printing declarations for our completion |
| 567 | /// descriptions. |
| 568 | clang::PrintingPolicy m_desc_policy; |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 569 | |
| 570 | /// Returns true if the given character can be used in an identifier. |
| 571 | /// This also returns true for numbers because for completion we usually |
| 572 | /// just iterate backwards over iterators. |
| 573 | /// |
| 574 | /// Note: lldb uses '$' in its internal identifiers, so we also allow this. |
| 575 | static bool IsIdChar(char c) { |
| 576 | return c == '_' || std::isalnum(c) || c == '$'; |
| 577 | } |
| 578 | |
| 579 | /// Returns true if the given character is used to separate arguments |
| 580 | /// in the command line of lldb. |
| 581 | static bool IsTokenSeparator(char c) { return c == ' ' || c == '\t'; } |
| 582 | |
| 583 | /// Drops all tokens in front of the expression that are unrelated for |
| 584 | /// the completion of the cmd line. 'unrelated' means here that the token |
| 585 | /// is not interested for the lldb completion API result. |
| 586 | StringRef dropUnrelatedFrontTokens(StringRef cmd) { |
| 587 | if (cmd.empty()) |
| 588 | return cmd; |
| 589 | |
| 590 | // If we are at the start of a word, then all tokens are unrelated to |
| 591 | // the current completion logic. |
| 592 | if (IsTokenSeparator(cmd.back())) |
| 593 | return StringRef(); |
| 594 | |
| 595 | // Remove all previous tokens from the string as they are unrelated |
| 596 | // to completing the current token. |
| 597 | StringRef to_remove = cmd; |
| 598 | while (!to_remove.empty() && !IsTokenSeparator(to_remove.back())) { |
| 599 | to_remove = to_remove.drop_back(); |
| 600 | } |
| 601 | cmd = cmd.drop_front(to_remove.size()); |
| 602 | |
| 603 | return cmd; |
| 604 | } |
| 605 | |
| 606 | /// Removes the last identifier token from the given cmd line. |
| 607 | StringRef removeLastToken(StringRef cmd) { |
| 608 | while (!cmd.empty() && IsIdChar(cmd.back())) { |
| 609 | cmd = cmd.drop_back(); |
| 610 | } |
| 611 | return cmd; |
| 612 | } |
| 613 | |
| 614 | /// Attemps to merge the given completion from the given position into the |
| 615 | /// existing command. Returns the completion string that can be returned to |
| 616 | /// the lldb completion API. |
| 617 | std::string mergeCompletion(StringRef existing, unsigned pos, |
| 618 | StringRef completion) { |
| 619 | StringRef existing_command = existing.substr(0, pos); |
| 620 | // We rewrite the last token with the completion, so let's drop that |
| 621 | // token from the command. |
| 622 | existing_command = removeLastToken(existing_command); |
| 623 | // We also should remove all previous tokens from the command as they |
| 624 | // would otherwise be added to the completion that already has the |
| 625 | // completion. |
| 626 | existing_command = dropUnrelatedFrontTokens(existing_command); |
| 627 | return existing_command.str() + completion.str(); |
| 628 | } |
| 629 | |
| 630 | public: |
| 631 | /// Constructs a CodeComplete consumer that can be attached to a Sema. |
| 632 | /// @param[out] matches |
| 633 | /// The list of matches that the lldb completion API expects as a result. |
| 634 | /// This may already contain matches, so it's only allowed to append |
| 635 | /// to this variable. |
| 636 | /// @param[out] expr |
| 637 | /// The whole expression string that we are currently parsing. This |
| 638 | /// string needs to be equal to the input the user typed, and NOT the |
| 639 | /// final code that Clang is parsing. |
| 640 | /// @param[out] position |
| 641 | /// The character position of the user cursor in the `expr` parameter. |
| 642 | /// |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 643 | CodeComplete(CompletionRequest &request, clang::LangOptions ops, |
| 644 | std::string expr, unsigned position) |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 645 | : CodeCompleteConsumer(CodeCompleteOptions(), false), |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 646 | m_info(std::make_shared<GlobalCodeCompletionAllocator>()), m_expr(expr), |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 647 | m_position(position), m_request(request), m_desc_policy(ops) { |
| 648 | |
| 649 | // Ensure that the printing policy is producing a description that is as |
| 650 | // short as possible. |
| 651 | m_desc_policy.SuppressScope = true; |
| 652 | m_desc_policy.SuppressTagKeyword = true; |
| 653 | m_desc_policy.FullyQualifiedName = false; |
| 654 | m_desc_policy.TerseOutput = true; |
| 655 | m_desc_policy.IncludeNewlines = false; |
| 656 | m_desc_policy.UseVoidForZeroParams = false; |
| 657 | m_desc_policy.Bool = true; |
| 658 | } |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 659 | |
| 660 | /// Deregisters and destroys this code-completion consumer. |
| 661 | virtual ~CodeComplete() {} |
| 662 | |
| 663 | /// \name Code-completion filtering |
| 664 | /// Check if the result should be filtered out. |
| 665 | bool isResultFilteredOut(StringRef Filter, |
| 666 | CodeCompletionResult Result) override { |
| 667 | // This code is mostly copied from CodeCompleteConsumer. |
| 668 | switch (Result.Kind) { |
| 669 | case CodeCompletionResult::RK_Declaration: |
| 670 | return !( |
| 671 | Result.Declaration->getIdentifier() && |
| 672 | Result.Declaration->getIdentifier()->getName().startswith(Filter)); |
| 673 | case CodeCompletionResult::RK_Keyword: |
| 674 | return !StringRef(Result.Keyword).startswith(Filter); |
| 675 | case CodeCompletionResult::RK_Macro: |
| 676 | return !Result.Macro->getName().startswith(Filter); |
| 677 | case CodeCompletionResult::RK_Pattern: |
| 678 | return !StringRef(Result.Pattern->getAsString()).startswith(Filter); |
| 679 | } |
| 680 | // If we trigger this assert or the above switch yields a warning, then |
| 681 | // CodeCompletionResult has been enhanced with more kinds of completion |
| 682 | // results. Expand the switch above in this case. |
| 683 | assert(false && "Unknown completion result type?"); |
| 684 | // If we reach this, then we should just ignore whatever kind of unknown |
| 685 | // result we got back. We probably can't turn it into any kind of useful |
| 686 | // completion suggestion with the existing code. |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | /// \name Code-completion callbacks |
| 691 | /// Process the finalized code-completion results. |
| 692 | void ProcessCodeCompleteResults(Sema &SemaRef, CodeCompletionContext Context, |
| 693 | CodeCompletionResult *Results, |
| 694 | unsigned NumResults) override { |
| 695 | |
| 696 | // The Sema put the incomplete token we try to complete in here during |
| 697 | // lexing, so we need to retrieve it here to know what we are completing. |
| 698 | StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter(); |
| 699 | |
| 700 | // Iterate over all the results. Filter out results we don't want and |
| 701 | // process the rest. |
| 702 | for (unsigned I = 0; I != NumResults; ++I) { |
| 703 | // Filter the results with the information from the Sema. |
| 704 | if (!Filter.empty() && isResultFilteredOut(Filter, Results[I])) |
| 705 | continue; |
| 706 | |
| 707 | CodeCompletionResult &R = Results[I]; |
| 708 | std::string ToInsert; |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 709 | std::string Description; |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 710 | // Handle the different completion kinds that come from the Sema. |
| 711 | switch (R.Kind) { |
| 712 | case CodeCompletionResult::RK_Declaration: { |
| 713 | const NamedDecl *D = R.Declaration; |
| 714 | ToInsert = R.Declaration->getNameAsString(); |
| 715 | // If we have a function decl that has no arguments we want to |
| 716 | // complete the empty parantheses for the user. If the function has |
| 717 | // arguments, we at least complete the opening bracket. |
| 718 | if (const FunctionDecl *F = dyn_cast<FunctionDecl>(D)) { |
| 719 | if (F->getNumParams() == 0) |
| 720 | ToInsert += "()"; |
| 721 | else |
| 722 | ToInsert += "("; |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 723 | raw_string_ostream OS(Description); |
| 724 | F->print(OS, m_desc_policy, false); |
| 725 | OS.flush(); |
| 726 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
| 727 | Description = V->getType().getAsString(m_desc_policy); |
| 728 | } else if (const FieldDecl *F = dyn_cast<FieldDecl>(D)) { |
| 729 | Description = F->getType().getAsString(m_desc_policy); |
| 730 | } else if (const NamespaceDecl *N = dyn_cast<NamespaceDecl>(D)) { |
| 731 | // If we try to complete a namespace, then we can directly append |
| 732 | // the '::'. |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 733 | if (!N->isAnonymousNamespace()) |
| 734 | ToInsert += "::"; |
| 735 | } |
| 736 | break; |
| 737 | } |
| 738 | case CodeCompletionResult::RK_Keyword: |
| 739 | ToInsert = R.Keyword; |
| 740 | break; |
| 741 | case CodeCompletionResult::RK_Macro: |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 742 | ToInsert = R.Macro->getName().str(); |
| 743 | break; |
| 744 | case CodeCompletionResult::RK_Pattern: |
| 745 | ToInsert = R.Pattern->getTypedText(); |
| 746 | break; |
| 747 | } |
| 748 | // At this point all information is in the ToInsert string. |
| 749 | |
| 750 | // We also filter some internal lldb identifiers here. The user |
| 751 | // shouldn't see these. |
| 752 | if (StringRef(ToInsert).startswith("$__lldb_")) |
| 753 | continue; |
| 754 | if (!ToInsert.empty()) { |
| 755 | // Merge the suggested Token into the existing command line to comply |
| 756 | // with the kind of result the lldb API expects. |
| 757 | std::string CompletionSuggestion = |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 758 | mergeCompletion(m_expr, m_position, ToInsert); |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 759 | m_request.AddCompletion(CompletionSuggestion, Description); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /// \param S the semantic-analyzer object for which code-completion is being |
| 765 | /// done. |
| 766 | /// |
| 767 | /// \param CurrentArg the index of the current argument. |
| 768 | /// |
| 769 | /// \param Candidates an array of overload candidates. |
| 770 | /// |
| 771 | /// \param NumCandidates the number of overload candidates |
| 772 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 773 | OverloadCandidate *Candidates, |
| 774 | unsigned NumCandidates, |
| 775 | SourceLocation OpenParLoc) override { |
| 776 | // At the moment we don't filter out any overloaded candidates. |
| 777 | } |
| 778 | |
| 779 | CodeCompletionAllocator &getAllocator() override { |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 780 | return m_info.getAllocator(); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Raphael Isemann | 39ec6e7 | 2018-08-30 19:47:53 +0000 | [diff] [blame] | 783 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return m_info; } |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 784 | }; |
| 785 | } // namespace |
| 786 | |
Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 787 | bool ClangExpressionParser::Complete(CompletionRequest &request, unsigned line, |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 788 | unsigned pos, unsigned typed_pos) { |
| 789 | DiagnosticManager mgr; |
| 790 | // We need the raw user expression here because that's what the CodeComplete |
| 791 | // class uses to provide completion suggestions. |
| 792 | // However, the `Text` method only gives us the transformed expression here. |
| 793 | // To actually get the raw user input here, we have to cast our expression to |
| 794 | // the LLVMUserExpression which exposes the right API. This should never fail |
| 795 | // as we always have a ClangUserExpression whenever we call this. |
| 796 | LLVMUserExpression &llvm_expr = *static_cast<LLVMUserExpression *>(&m_expr); |
Raphael Isemann | 9dd34c8 | 2018-09-17 12:06:07 +0000 | [diff] [blame] | 797 | CodeComplete CC(request, m_compiler->getLangOpts(), llvm_expr.GetUserText(), |
| 798 | typed_pos); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 799 | // We don't need a code generator for parsing. |
| 800 | m_code_generator.reset(); |
| 801 | // Start parsing the expression with our custom code completion consumer. |
| 802 | ParseInternal(mgr, &CC, line, pos); |
| 803 | return true; |
| 804 | } |
| 805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 806 | unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) { |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 807 | return ParseInternal(diagnostic_manager); |
| 808 | } |
| 809 | |
| 810 | unsigned |
| 811 | ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager, |
| 812 | CodeCompleteConsumer *completion_consumer, |
| 813 | unsigned completion_line, |
| 814 | unsigned completion_column) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 815 | ClangDiagnosticManagerAdapter *adapter = |
| 816 | static_cast<ClangDiagnosticManagerAdapter *>( |
| 817 | m_compiler->getDiagnostics().getClient()); |
| 818 | clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough(); |
| 819 | diag_buf->FlushDiagnostics(m_compiler->getDiagnostics()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 820 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 821 | adapter->ResetManager(&diagnostic_manager); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 823 | const char *expr_text = m_expr.Text(); |
Rafael Espindola | 9cb97b6 | 2014-05-21 15:08:27 +0000 | [diff] [blame] | 824 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 825 | clang::SourceManager &source_mgr = m_compiler->getSourceManager(); |
| 826 | bool created_main_file = false; |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 827 | |
| 828 | // Clang wants to do completion on a real file known by Clang's file manager, |
| 829 | // so we have to create one to make this work. |
| 830 | // TODO: We probably could also simulate to Clang's file manager that there |
| 831 | // is a real file that contains our code. |
| 832 | bool should_create_file = completion_consumer != nullptr; |
| 833 | |
| 834 | // We also want a real file on disk if we generate full debug info. |
| 835 | should_create_file |= m_compiler->getCodeGenOpts().getDebugInfo() == |
| 836 | codegenoptions::FullDebugInfo; |
| 837 | |
| 838 | if (should_create_file) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | int temp_fd = -1; |
Stella Stamenova | b3f44ad | 2018-12-10 17:23:28 +0000 | [diff] [blame] | 840 | llvm::SmallString<128> result_path; |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 841 | if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr"); |
| 843 | std::string temp_source_path = tmpdir_file_spec.GetPath(); |
| 844 | llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path); |
| 845 | } else { |
| 846 | llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path); |
| 847 | } |
| 848 | |
| 849 | if (temp_fd != -1) { |
| 850 | lldb_private::File file(temp_fd, true); |
| 851 | const size_t expr_text_len = strlen(expr_text); |
| 852 | size_t bytes_written = expr_text_len; |
| 853 | if (file.Write(expr_text, bytes_written).Success()) { |
| 854 | if (bytes_written == expr_text_len) { |
| 855 | file.Close(); |
Jonas Devlieghere | ff04b3d | 2019-02-14 18:05:43 +0000 | [diff] [blame] | 856 | source_mgr.setMainFileID(source_mgr.createFileID( |
| 857 | m_compiler->getFileManager().getFile(result_path), |
| 858 | SourceLocation(), SrcMgr::C_User)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | created_main_file = true; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 860 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 861 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 862 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 863 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 864 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 865 | if (!created_main_file) { |
| 866 | std::unique_ptr<MemoryBuffer> memory_buffer = |
| 867 | MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__); |
| 868 | source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer))); |
| 869 | } |
| 870 | |
| 871 | diag_buf->BeginSourceFile(m_compiler->getLangOpts(), |
| 872 | &m_compiler->getPreprocessor()); |
| 873 | |
| 874 | ClangExpressionHelper *type_system_helper = |
| 875 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 876 | |
| 877 | ASTConsumer *ast_transformer = |
| 878 | type_system_helper->ASTTransformer(m_code_generator.get()); |
| 879 | |
| 880 | if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap()) |
| 881 | decl_map->InstallCodeGenerator(m_code_generator.get()); |
| 882 | |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 883 | // If we want to parse for code completion, we need to attach our code |
| 884 | // completion consumer to the Sema and specify a completion position. |
| 885 | // While parsing the Sema will call this consumer with the provided |
| 886 | // completion suggestions. |
| 887 | if (completion_consumer) { |
| 888 | auto main_file = source_mgr.getFileEntryForID(source_mgr.getMainFileID()); |
| 889 | auto &PP = m_compiler->getPreprocessor(); |
| 890 | // Lines and columns start at 1 in Clang, but code completion positions are |
| 891 | // indexed from 0, so we need to add 1 to the line and column here. |
| 892 | ++completion_line; |
| 893 | ++completion_column; |
| 894 | PP.SetCodeCompletionPoint(main_file, completion_line, completion_column); |
| 895 | } |
| 896 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 897 | if (ast_transformer) { |
| 898 | ast_transformer->Initialize(m_compiler->getASTContext()); |
| 899 | ParseAST(m_compiler->getPreprocessor(), ast_transformer, |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 900 | m_compiler->getASTContext(), false, TU_Complete, |
| 901 | completion_consumer); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 902 | } else { |
| 903 | m_code_generator->Initialize(m_compiler->getASTContext()); |
| 904 | ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 905 | m_compiler->getASTContext(), false, TU_Complete, |
| 906 | completion_consumer); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | diag_buf->EndSourceFile(); |
| 910 | |
| 911 | unsigned num_errors = diag_buf->getNumErrors(); |
| 912 | |
| 913 | if (m_pp_callbacks && m_pp_callbacks->hasErrors()) { |
| 914 | num_errors++; |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 915 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 916 | "while importing modules:"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 917 | diagnostic_manager.AppendMessageToDiagnostic( |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 918 | m_pp_callbacks->getErrorString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | if (!num_errors) { |
| 922 | if (type_system_helper->DeclMap() && |
| 923 | !type_system_helper->DeclMap()->ResolveUnknownTypes()) { |
| 924 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 925 | "Couldn't infer the type of a variable"); |
| 926 | num_errors++; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 927 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 928 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 929 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 930 | if (!num_errors) { |
| 931 | type_system_helper->CommitPersistentDecls(); |
| 932 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 933 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 934 | adapter->ResetManager(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 935 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 936 | return num_errors; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 939 | std::string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 940 | ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) { |
| 941 | std::string abi; |
| 942 | |
| 943 | if (target_arch.IsMIPS()) { |
| 944 | switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) { |
| 945 | case ArchSpec::eMIPSABI_N64: |
| 946 | abi = "n64"; |
| 947 | break; |
| 948 | case ArchSpec::eMIPSABI_N32: |
| 949 | abi = "n32"; |
| 950 | break; |
| 951 | case ArchSpec::eMIPSABI_O32: |
| 952 | abi = "o32"; |
| 953 | break; |
| 954 | default: |
| 955 | break; |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 956 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 957 | } |
| 958 | return abi; |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 959 | } |
| 960 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 961 | bool ClangExpressionParser::RewriteExpression( |
| 962 | DiagnosticManager &diagnostic_manager) { |
| 963 | clang::SourceManager &source_manager = m_compiler->getSourceManager(); |
| 964 | clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(), |
| 965 | nullptr); |
| 966 | clang::edit::Commit commit(editor); |
| 967 | clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts()); |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | class RewritesReceiver : public edit::EditsReceiver { |
| 970 | Rewriter &rewrite; |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 971 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 972 | public: |
| 973 | RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {} |
| 974 | |
| 975 | void insert(SourceLocation loc, StringRef text) override { |
| 976 | rewrite.InsertText(loc, text); |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 977 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 978 | void replace(CharSourceRange range, StringRef text) override { |
| 979 | rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 980 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 981 | }; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 982 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 983 | RewritesReceiver rewrites_receiver(rewriter); |
| 984 | |
| 985 | const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics(); |
| 986 | size_t num_diags = diagnostics.size(); |
| 987 | if (num_diags == 0) |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 988 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 989 | |
| 990 | for (const Diagnostic *diag : diagnostic_manager.Diagnostics()) { |
| 991 | const ClangDiagnostic *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag); |
| 992 | if (diagnostic && diagnostic->HasFixIts()) { |
| 993 | for (const FixItHint &fixit : diagnostic->FixIts()) { |
| 994 | // This is cobbed from clang::Rewrite::FixItRewriter. |
| 995 | if (fixit.CodeToInsert.empty()) { |
| 996 | if (fixit.InsertFromRange.isValid()) { |
| 997 | commit.insertFromRange(fixit.RemoveRange.getBegin(), |
| 998 | fixit.InsertFromRange, /*afterToken=*/false, |
| 999 | fixit.BeforePreviousInsertions); |
| 1000 | } else |
| 1001 | commit.remove(fixit.RemoveRange); |
| 1002 | } else { |
| 1003 | if (fixit.RemoveRange.isTokenRange() || |
| 1004 | fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd()) |
| 1005 | commit.replace(fixit.RemoveRange, fixit.CodeToInsert); |
| 1006 | else |
| 1007 | commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert, |
| 1008 | /*afterToken=*/false, fixit.BeforePreviousInsertions); |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | // FIXME - do we want to try to propagate specific errors here? |
| 1015 | if (!commit.isCommitable()) |
| 1016 | return false; |
| 1017 | else if (!editor.commit(commit)) |
| 1018 | return false; |
| 1019 | |
| 1020 | // Now play all the edits, and stash the result in the diagnostic manager. |
| 1021 | editor.applyRewrites(rewrites_receiver); |
| 1022 | RewriteBuffer &main_file_buffer = |
| 1023 | rewriter.getEditBuffer(source_manager.getMainFileID()); |
| 1024 | |
| 1025 | std::string fixed_expression; |
| 1026 | llvm::raw_string_ostream out_stream(fixed_expression); |
| 1027 | |
| 1028 | main_file_buffer.write(out_stream); |
| 1029 | out_stream.flush(); |
| 1030 | diagnostic_manager.SetFixedExpression(fixed_expression); |
| 1031 | |
| 1032 | return true; |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1035 | static bool FindFunctionInModule(ConstString &mangled_name, |
| 1036 | llvm::Module *module, const char *orig_name) { |
| 1037 | for (const auto &func : module->getFunctionList()) { |
| 1038 | const StringRef &name = func.getName(); |
| 1039 | if (name.find(orig_name) != StringRef::npos) { |
| 1040 | mangled_name.SetString(name); |
| 1041 | return true; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1042 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1043 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 1044 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1045 | return false; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1046 | } |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1047 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1048 | lldb_private::Status ClangExpressionParser::PrepareForExecution( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1049 | lldb::addr_t &func_addr, lldb::addr_t &func_end, |
| 1050 | lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx, |
| 1051 | bool &can_interpret, ExecutionPolicy execution_policy) { |
| 1052 | func_addr = LLDB_INVALID_ADDRESS; |
| 1053 | func_end = LLDB_INVALID_ADDRESS; |
| 1054 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 1055 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1056 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1058 | std::unique_ptr<llvm::Module> llvm_module_up( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | m_code_generator->ReleaseModule()); |
| 1060 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1061 | if (!llvm_module_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1062 | err.SetErrorToGenericError(); |
| 1063 | err.SetErrorString("IR doesn't contain a module"); |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1064 | return err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | ConstString function_name; |
| 1068 | |
| 1069 | if (execution_policy != eExecutionPolicyTopLevel) { |
| 1070 | // Find the actual name of the function (it's often mangled somehow) |
| 1071 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1072 | if (!FindFunctionInModule(function_name, llvm_module_up.get(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1073 | m_expr.FunctionName())) { |
| 1074 | err.SetErrorToGenericError(); |
| 1075 | err.SetErrorStringWithFormat("Couldn't find %s() in the module", |
| 1076 | m_expr.FunctionName()); |
| 1077 | return err; |
| 1078 | } else { |
| 1079 | if (log) |
| 1080 | log->Printf("Found function %s for %s", function_name.AsCString(), |
| 1081 | m_expr.FunctionName()); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | SymbolContext sc; |
| 1086 | |
| 1087 | if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) { |
| 1088 | sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything); |
| 1089 | } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) { |
| 1090 | sc.target_sp = target_sp; |
| 1091 | } |
| 1092 | |
| 1093 | LLVMUserExpression::IRPasses custom_passes; |
| 1094 | { |
| 1095 | auto lang = m_expr.Language(); |
| 1096 | if (log) |
Bruce Mitchener | 4ebdee0 | 2018-05-29 09:10:46 +0000 | [diff] [blame] | 1097 | log->Printf("%s - Current expression language is %s\n", __FUNCTION__, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1098 | Language::GetNameForLanguageType(lang)); |
Jim Ingham | 2256215 | 2016-11-03 23:42:09 +0000 | [diff] [blame] | 1099 | lldb::ProcessSP process_sp = exe_ctx.GetProcessSP(); |
| 1100 | if (process_sp && lang != lldb::eLanguageTypeUnknown) { |
| 1101 | auto runtime = process_sp->GetLanguageRuntime(lang); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | if (runtime) |
| 1103 | runtime->GetIRPasses(custom_passes); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | if (custom_passes.EarlyPasses) { |
| 1108 | if (log) |
| 1109 | log->Printf("%s - Running Early IR Passes from LanguageRuntime on " |
| 1110 | "expression module '%s'", |
| 1111 | __FUNCTION__, m_expr.FunctionName()); |
| 1112 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1113 | custom_passes.EarlyPasses->run(*llvm_module_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 1116 | execution_unit_sp = std::make_shared<IRExecutionUnit>( |
| 1117 | m_llvm_context, // handed off here |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 1118 | llvm_module_up, // handed off here |
| 1119 | function_name, exe_ctx.GetTargetSP(), sc, |
Jonas Devlieghere | 796ac80 | 2019-02-11 23:13:08 +0000 | [diff] [blame] | 1120 | m_compiler->getTargetOpts().Features); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1121 | |
| 1122 | ClangExpressionHelper *type_system_helper = |
| 1123 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 1124 | ClangExpressionDeclMap *decl_map = |
| 1125 | type_system_helper->DeclMap(); // result can be NULL |
| 1126 | |
| 1127 | if (decl_map) { |
| 1128 | Stream *error_stream = NULL; |
| 1129 | Target *target = exe_ctx.GetTargetPtr(); |
| 1130 | error_stream = target->GetDebugger().GetErrorFile().get(); |
| 1131 | |
| 1132 | IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(), |
| 1133 | *execution_unit_sp, *error_stream, |
| 1134 | function_name.AsCString()); |
| 1135 | |
| 1136 | bool ir_can_run = |
| 1137 | ir_for_target.runOnModule(*execution_unit_sp->GetModule()); |
| 1138 | |
| 1139 | if (!ir_can_run) { |
| 1140 | err.SetErrorString( |
| 1141 | "The expression could not be prepared to run in the target"); |
| 1142 | return err; |
| 1143 | } |
| 1144 | |
| 1145 | Process *process = exe_ctx.GetProcessPtr(); |
| 1146 | |
| 1147 | if (execution_policy != eExecutionPolicyAlways && |
| 1148 | execution_policy != eExecutionPolicyTopLevel) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1149 | lldb_private::Status interpret_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | |
| 1151 | bool interpret_function_calls = |
| 1152 | !process ? false : process->CanInterpretFunctionCalls(); |
| 1153 | can_interpret = IRInterpreter::CanInterpret( |
| 1154 | *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), |
| 1155 | interpret_error, interpret_function_calls); |
| 1156 | |
| 1157 | if (!can_interpret && execution_policy == eExecutionPolicyNever) { |
| 1158 | err.SetErrorStringWithFormat("Can't run the expression locally: %s", |
| 1159 | interpret_error.AsCString()); |
| 1160 | return err; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (!process && execution_policy == eExecutionPolicyAlways) { |
| 1165 | err.SetErrorString("Expression needed to run in the target, but the " |
| 1166 | "target can't be run"); |
| 1167 | return err; |
| 1168 | } |
| 1169 | |
| 1170 | if (!process && execution_policy == eExecutionPolicyTopLevel) { |
| 1171 | err.SetErrorString("Top-level code needs to be inserted into a runnable " |
| 1172 | "target, but the target can't be run"); |
| 1173 | return err; |
| 1174 | } |
| 1175 | |
| 1176 | if (execution_policy == eExecutionPolicyAlways || |
| 1177 | (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) { |
| 1178 | if (m_expr.NeedsValidation() && process) { |
| 1179 | if (!process->GetDynamicCheckers()) { |
| 1180 | DynamicCheckerFunctions *dynamic_checkers = |
| 1181 | new DynamicCheckerFunctions(); |
| 1182 | |
| 1183 | DiagnosticManager install_diagnostics; |
| 1184 | |
| 1185 | if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) { |
| 1186 | if (install_diagnostics.Diagnostics().size()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1187 | err.SetErrorString(install_diagnostics.GetString().c_str()); |
Raphael Isemann | 262dd8c | 2018-09-11 13:59:47 +0000 | [diff] [blame] | 1188 | else |
| 1189 | err.SetErrorString("couldn't install checkers, unknown error"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1190 | |
| 1191 | return err; |
| 1192 | } |
| 1193 | |
| 1194 | process->SetDynamicCheckers(dynamic_checkers); |
| 1195 | |
| 1196 | if (log) |
Raphael Isemann | f75a9dc | 2019-02-11 21:45:33 +0000 | [diff] [blame] | 1197 | log->Printf("== [ClangExpressionParser::PrepareForExecution] " |
| 1198 | "Finished installing dynamic checkers =="); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), |
| 1202 | function_name.AsCString()); |
| 1203 | |
| 1204 | llvm::Module *module = execution_unit_sp->GetModule(); |
| 1205 | if (!module || !ir_dynamic_checks.runOnModule(*module)) { |
| 1206 | err.SetErrorToGenericError(); |
| 1207 | err.SetErrorString("Couldn't add dynamic checks to the expression"); |
| 1208 | return err; |
| 1209 | } |
| 1210 | |
| 1211 | if (custom_passes.LatePasses) { |
| 1212 | if (log) |
| 1213 | log->Printf("%s - Running Late IR Passes from LanguageRuntime on " |
| 1214 | "expression module '%s'", |
| 1215 | __FUNCTION__, m_expr.FunctionName()); |
| 1216 | |
| 1217 | custom_passes.LatePasses->run(*module); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | if (execution_policy == eExecutionPolicyAlways || |
| 1223 | execution_policy == eExecutionPolicyTopLevel || !can_interpret) { |
| 1224 | execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); |
| 1225 | } |
| 1226 | } else { |
| 1227 | execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); |
| 1228 | } |
| 1229 | |
| 1230 | return err; |
| 1231 | } |
| 1232 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1233 | lldb_private::Status ClangExpressionParser::RunStaticInitializers( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1234 | lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1235 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1236 | |
| 1237 | lldbassert(execution_unit_sp.get()); |
| 1238 | lldbassert(exe_ctx.HasThreadScope()); |
| 1239 | |
| 1240 | if (!execution_unit_sp.get()) { |
| 1241 | err.SetErrorString( |
| 1242 | "can't run static initializers for a NULL execution unit"); |
| 1243 | return err; |
| 1244 | } |
| 1245 | |
| 1246 | if (!exe_ctx.HasThreadScope()) { |
| 1247 | err.SetErrorString("can't run static initializers without a thread"); |
| 1248 | return err; |
| 1249 | } |
| 1250 | |
| 1251 | std::vector<lldb::addr_t> static_initializers; |
| 1252 | |
| 1253 | execution_unit_sp->GetStaticInitializers(static_initializers); |
| 1254 | |
| 1255 | for (lldb::addr_t static_initializer : static_initializers) { |
| 1256 | EvaluateExpressionOptions options; |
| 1257 | |
| 1258 | lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction( |
| 1259 | exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(), |
| 1260 | llvm::ArrayRef<lldb::addr_t>(), options)); |
| 1261 | |
| 1262 | DiagnosticManager execution_errors; |
| 1263 | lldb::ExpressionResults results = |
| 1264 | exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan( |
| 1265 | exe_ctx, call_static_initializer, options, execution_errors); |
| 1266 | |
| 1267 | if (results != lldb::eExpressionCompleted) { |
| 1268 | err.SetErrorStringWithFormat("couldn't run static initializer: %s", |
| 1269 | execution_errors.GetString().c_str()); |
| 1270 | return err; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | return err; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 1275 | } |