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