blob: 0b6d952b76b22f1a82d558c797ae3e787c72cea6 [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- 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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Sean Callanan1a8d4092010-08-27 01:01:44 +000012#include "lldb/Expression/ClangExpressionParser.h"
13
14#include "lldb/Core/ArchSpec.h"
15#include "lldb/Core/DataBufferHeap.h"
Sean Callanan3989fb92011-01-27 01:07:04 +000016#include "lldb/Core/Debugger.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000017#include "lldb/Core/Disassembler.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000018#include "lldb/Core/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000019#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000020#include "lldb/Core/StreamFile.h"
Sean Callanan6961e872010-09-01 00:58:00 +000021#include "lldb/Core/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000022#include "lldb/Expression/ClangASTSource.h"
23#include "lldb/Expression/ClangExpression.h"
Sean Callanan77502262011-05-12 23:54:16 +000024#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000025#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan6961e872010-09-01 00:58:00 +000026#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000027#include "lldb/Expression/IRInterpreter.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000028#include "lldb/Host/File.h"
29#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000030#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000031#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Target/Process.h"
33#include "lldb/Target/Target.h"
34
35#include "clang/AST/ASTContext.h"
36#include "clang/AST/ExternalASTSource.h"
37#include "clang/Basic/FileManager.h"
38#include "clang/Basic/TargetInfo.h"
39#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000040#include "clang/CodeGen/CodeGenAction.h"
41#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000042#include "clang/Frontend/CompilerInstance.h"
43#include "clang/Frontend/CompilerInvocation.h"
44#include "clang/Frontend/FrontendActions.h"
45#include "clang/Frontend/FrontendDiagnostic.h"
46#include "clang/Frontend/FrontendPluginRegistry.h"
47#include "clang/Frontend/TextDiagnosticBuffer.h"
48#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000049#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000050#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000051#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000052#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000053#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000054
55#include "llvm/ADT/StringRef.h"
56#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000057#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000058#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000059#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000060
Ed Mastea09ed032014-03-18 18:55:06 +000061#include "llvm/ExecutionEngine/MCJIT.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000062#include "llvm/IR/LLVMContext.h"
63#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000064#include "llvm/Support/ErrorHandling.h"
65#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000066#include "llvm/Support/DynamicLibrary.h"
67#include "llvm/Support/Host.h"
68#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000069
70using namespace clang;
71using namespace llvm;
72using namespace lldb_private;
73
74//===----------------------------------------------------------------------===//
75// Utility Methods for Clang
76//===----------------------------------------------------------------------===//
77
78std::string GetBuiltinIncludePath(const char *Argv0) {
Rafael Espindola4609ea82013-06-26 14:10:56 +000079 SmallString<128> P(llvm::sys::fs::getMainExecutable(
80 Argv0, (void *)(intptr_t) GetBuiltinIncludePath));
81
82 if (!P.empty()) {
83 llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang
84 llvm::sys::path::remove_filename(P); // Remove /bin from foo/bin
85
Sean Callanan1a8d4092010-08-27 01:01:44 +000086 // Get foo/lib/clang/<version>/include
Rafael Espindola4609ea82013-06-26 14:10:56 +000087 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
88 "include");
Sean Callanan1a8d4092010-08-27 01:01:44 +000089 }
90
91 return P.str();
92}
93
Sean Callanan1a8d4092010-08-27 01:01:44 +000094//===----------------------------------------------------------------------===//
95// Implementation of ClangExpressionParser
96//===----------------------------------------------------------------------===//
97
Greg Clayton514487e2011-02-15 21:59:32 +000098ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
Greg Clayton23f8c952014-03-24 23:10:19 +000099 ClangExpression &expr,
100 bool generate_debug_info) :
Greg Clayton514487e2011-02-15 21:59:32 +0000101 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000102 m_compiler (),
Greg Claytone01e07b2013-04-18 18:10:51 +0000103 m_code_generator ()
Sean Callanan1a8d4092010-08-27 01:01:44 +0000104{
105 // Initialize targets first, so that --version shows registered targets.
106 static struct InitializeLLVM {
107 InitializeLLVM() {
108 llvm::InitializeAllTargets();
109 llvm::InitializeAllAsmPrinters();
Sean Callanancc427fa2011-07-30 02:42:06 +0000110 llvm::InitializeAllTargetMCs();
Sean Callanana5230ce2011-12-01 04:31:46 +0000111 llvm::InitializeAllDisassemblers();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000112 }
113 } InitializeLLVM;
Sean Callanan3d654b32012-09-24 22:25:51 +0000114
Sean Callanan1a8d4092010-08-27 01:01:44 +0000115 // 1. Create a new compiler instance.
116 m_compiler.reset(new CompilerInstance());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000117
Sean Callanan3d654b32012-09-24 22:25:51 +0000118 // 2. Install the target.
119
120 lldb::TargetSP target_sp;
121 if (exe_scope)
122 target_sp = exe_scope->CalculateTarget();
123
124 // TODO: figure out what to really do when we don't have a valid target.
125 // Sometimes this will be ok to just use the host target triple (when we
126 // evaluate say "2+3", but other expressions like breakpoint conditions
127 // and other things that _are_ target specific really shouldn't just be
128 // using the host triple. This needs to be fixed in a better way.
129 if (target_sp && target_sp->GetArchitecture().IsValid())
130 {
131 std::string triple = target_sp->GetArchitecture().GetTriple().str();
Sean Callanan3d654b32012-09-24 22:25:51 +0000132 m_compiler->getTargetOpts().Triple = triple;
133 }
134 else
135 {
136 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
137 }
138
Sean Callananb45a6f02013-02-21 01:04:23 +0000139 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
140 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
141 {
142 m_compiler->getTargetOpts().Features.push_back("+sse");
143 m_compiler->getTargetOpts().Features.push_back("+sse2");
144 }
145
Jason Molendaa3329782014-03-29 18:54:20 +0000146 // Any arm32 iOS environment, but not on arm64
Sean Callanan60400ec2014-06-23 21:00:25 +0000147 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
148 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
149 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
Jason Molendaa3329782014-03-29 18:54:20 +0000150 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000151 m_compiler->getTargetOpts().ABI = "apcs-gnu";
Jason Molendaa3329782014-03-29 18:54:20 +0000152 }
153
Sean Callananb1de8dd2013-01-22 02:20:20 +0000154 m_compiler->createDiagnostics();
Jason Molendaa3329782014-03-29 18:54:20 +0000155
Sean Callanan3d654b32012-09-24 22:25:51 +0000156 // Create the target instance.
Alp Toker5f838642014-07-06 05:36:57 +0000157 m_compiler->setTarget(TargetInfo::CreateTargetInfo(
158 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
159
Sean Callanan3d654b32012-09-24 22:25:51 +0000160 assert (m_compiler->hasTarget());
161
162 // 3. Set options.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000163
Sean Callananc7b65062011-11-07 23:35:40 +0000164 lldb::LanguageType language = expr.Language();
Greg Claytonf83f32d2011-01-15 01:32:14 +0000165
Sean Callananc7b65062011-11-07 23:35:40 +0000166 switch (language)
167 {
168 case lldb::eLanguageTypeC:
169 break;
170 case lldb::eLanguageTypeObjC:
171 m_compiler->getLangOpts().ObjC1 = true;
172 m_compiler->getLangOpts().ObjC2 = true;
173 break;
174 case lldb::eLanguageTypeC_plus_plus:
175 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000176 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000177 break;
178 case lldb::eLanguageTypeObjC_plus_plus:
179 default:
180 m_compiler->getLangOpts().ObjC1 = true;
181 m_compiler->getLangOpts().ObjC2 = true;
182 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000183 m_compiler->getLangOpts().CPlusPlus11 = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000184 break;
185 }
Sean Callananc3a16002011-01-17 23:42:46 +0000186
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000187 m_compiler->getLangOpts().Bool = true;
188 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000189 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000190 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
191 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
192 m_compiler->getLangOpts().DebuggerCastResultToId = true;
193
Sean Callanan0765c822012-04-17 00:49:48 +0000194 // Spell checking is a nice feature, but it ends up completing a
195 // lot of types that we didn't strictly speaking need to complete.
196 // As a result, we spend a long time parsing and importing debug
197 // information.
198 m_compiler->getLangOpts().SpellChecking = false;
199
Greg Claytond9e416c2012-02-18 05:35:26 +0000200 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000201 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000202 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000203
Greg Claytond9e416c2012-02-18 05:35:26 +0000204 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000205 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000206 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000207 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000208 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000209 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
210 else
211 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sean Callanan226b70c2012-03-08 02:39:03 +0000212
213 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000214 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000215 }
216 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000217
Sean Callanan1a8d4092010-08-27 01:01:44 +0000218 m_compiler->getLangOpts().ThreadsafeStatics = false;
219 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
220 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sean Callanan20bb3aa2011-12-21 22:22:58 +0000221
Sean Callanan1a8d4092010-08-27 01:01:44 +0000222 // Set CodeGen options
223 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
224 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000225 m_compiler->getCodeGenOpts().DisableFPElim = true;
226 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000227 if (generate_debug_info)
228 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
229 else
230 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000231
232 // Disable some warnings.
Alp Toker10933d12014-06-12 11:14:32 +0000233 m_compiler->getDiagnostics().setSeverityForGroup(
234 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
235 m_compiler->getDiagnostics().setSeverityForGroup(
236 "odr", clang::diag::Severity::Ignored, SourceLocation());
237
Sean Callanan1a8d4092010-08-27 01:01:44 +0000238 // Inform the target of the language options
239 //
240 // FIXME: We shouldn't need to do this, the target should be immutable once
241 // created. This complexity should be lifted elsewhere.
242 m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
243
244 // 4. Set up the diagnostic buffer for reporting errors
245
Sean Callanane2ef6e32010-09-23 03:01:22 +0000246 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000247
248 // 5. Set up the source management objects inside the compiler
249
Greg Clayton38a61402010-12-02 23:20:03 +0000250 clang::FileSystemOptions file_system_options;
251 m_file_manager.reset(new clang::FileManager(file_system_options));
Sean Callanan79439e82010-11-18 02:56:27 +0000252
Sean Callanan1a8d4092010-08-27 01:01:44 +0000253 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000254 m_compiler->createSourceManager(*m_file_manager.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000255
256 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000257 m_compiler->createPreprocessor(TU_Complete);
Sean Callanan1a8d4092010-08-27 01:01:44 +0000258
259 // 6. Most of this we get from the CompilerInstance, but we
260 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000261 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000262 m_builtin_context.reset(new Builtin::Context());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000263
Greg Clayton7b0992d2013-04-18 22:45:39 +0000264 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Greg Claytone01e07b2013-04-18 18:10:51 +0000265 m_compiler->getSourceManager(),
Greg Claytone01e07b2013-04-18 18:10:51 +0000266 m_compiler->getPreprocessor().getIdentifierTable(),
267 *m_selector_table.get(),
Alp Tokercf55e882014-05-03 15:05:45 +0000268 *m_builtin_context.get()));
269 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000270
271 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
272
273 if (decl_map)
274 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000275 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000276 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000277 ast_context->setExternalSource(ast_source);
278 }
279
280 m_compiler->setASTContext(ast_context.release());
281
Greg Clayton7b462cc2010-10-15 22:48:33 +0000282 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000283
Sean Callananfb0b7582011-03-15 00:17:19 +0000284 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000285 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
286 module_name,
287 m_compiler->getCodeGenOpts(),
Filipe Cabecinhas55fcb102013-02-14 22:02:57 +0000288 m_compiler->getTargetOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000289 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000290}
291
292ClangExpressionParser::~ClangExpressionParser()
293{
294}
295
296unsigned
297ClangExpressionParser::Parse (Stream &stream)
298{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000299 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
300
301 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000302
Greg Clayton23f8c952014-03-24 23:10:19 +0000303 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000304
305 clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
Greg Clayton23f8c952014-03-24 23:10:19 +0000306 bool created_main_file = false;
307 if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
308 {
309 std::string temp_source_path;
310
311 FileSpec tmpdir_file_spec;
312 if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
313 {
314 tmpdir_file_spec.GetFilename().SetCString("expr.XXXXXX");
315 temp_source_path = std::move(tmpdir_file_spec.GetPath());
316 }
317 else
318 {
319 temp_source_path = "/tmp/expr.XXXXXX";
320 }
321
322 if (mktemp(&temp_source_path[0]))
323 {
324 lldb_private::File file (temp_source_path.c_str(),
325 File::eOpenOptionWrite | File::eOpenOptionCanCreateNewOnly,
326 lldb::eFilePermissionsFileDefault);
327 const size_t expr_text_len = strlen(expr_text);
328 size_t bytes_written = expr_text_len;
329 if (file.Write(expr_text, bytes_written).Success())
330 {
331 if (bytes_written == expr_text_len)
332 {
333 file.Close();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000334 SourceMgr.setMainFileID(SourceMgr.createFileID(
335 m_file_manager->getFile(temp_source_path),
336 SourceLocation(), SrcMgr::C_User));
Greg Clayton23f8c952014-03-24 23:10:19 +0000337 created_main_file = true;
338 }
339 }
340 }
341 }
342
343 if (!created_main_file)
344 {
345 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000346 SourceMgr.setMainFileID(SourceMgr.createFileID(memory_buffer));
Greg Clayton23f8c952014-03-24 23:10:19 +0000347 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000348
Sean Callanane2ef6e32010-09-23 03:01:22 +0000349 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000350
351 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
352
353 if (ast_transformer)
354 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
355 else
356 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
357
Sean Callanane2ef6e32010-09-23 03:01:22 +0000358 diag_buf->EndSourceFile();
Sean Callanan77502262011-05-12 23:54:16 +0000359
Sean Callanan1a8d4092010-08-27 01:01:44 +0000360 TextDiagnosticBuffer::const_iterator diag_iterator;
361
362 int num_errors = 0;
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000363
Sean Callanane2ef6e32010-09-23 03:01:22 +0000364 for (diag_iterator = diag_buf->warn_begin();
365 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000366 ++diag_iterator)
367 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
368
369 num_errors = 0;
370
Sean Callanane2ef6e32010-09-23 03:01:22 +0000371 for (diag_iterator = diag_buf->err_begin();
372 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000373 ++diag_iterator)
374 {
375 num_errors++;
376 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
377 }
378
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000379 for (diag_iterator = diag_buf->note_begin();
380 diag_iterator != diag_buf->note_end();
381 ++diag_iterator)
382 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
383
Sean Callanan77502262011-05-12 23:54:16 +0000384 if (!num_errors)
385 {
386 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
387 {
388 stream.Printf("error: Couldn't infer the type of a variable\n");
389 num_errors++;
390 }
391 }
392
Sean Callanan1a8d4092010-08-27 01:01:44 +0000393 return num_errors;
394}
395
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000396static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000397 llvm::Module *module,
398 const char *orig_name)
399{
400 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
401 fi != fe;
402 ++fi)
403 {
404 if (fi->getName().str().find(orig_name) != std::string::npos)
405 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000406 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000407 return true;
408 }
409 }
410
411 return false;
412}
413
Sean Callanan1a8d4092010-08-27 01:01:44 +0000414Error
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000415ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000416 lldb::addr_t &func_end,
Greg Clayton23f8c952014-03-24 23:10:19 +0000417 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000418 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000419 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000420 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000421{
Greg Clayton22a939a2011-01-19 23:00:49 +0000422 func_addr = LLDB_INVALID_ADDRESS;
423 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000425
Sean Callanan1a8d4092010-08-27 01:01:44 +0000426 Error err;
427
Greg Clayton23f8c952014-03-24 23:10:19 +0000428 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000429
Greg Clayton23f8c952014-03-24 23:10:19 +0000430 if (!llvm_module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000431 {
432 err.SetErrorToGenericError();
433 err.SetErrorString("IR doesn't contain a module");
434 return err;
435 }
436
Sean Callananfc55f5d2010-09-21 00:44:12 +0000437 // Find the actual name of the function (it's often mangled somehow)
438
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000439 ConstString function_name;
Sean Callananfc55f5d2010-09-21 00:44:12 +0000440
Greg Clayton23f8c952014-03-24 23:10:19 +0000441 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000442 {
443 err.SetErrorToGenericError();
444 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
445 return err;
446 }
447 else
448 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000449 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000450 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000451 }
452
Greg Clayton23f8c952014-03-24 23:10:19 +0000453 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
454 llvm_module_ap, // handed off here
455 function_name,
456 exe_ctx.GetTargetSP(),
457 m_compiler->getTargetOpts().Features));
458
Sean Callanan1a8d4092010-08-27 01:01:44 +0000459 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
460
461 if (decl_map)
462 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000463 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000464 Target *target = exe_ctx.GetTargetPtr();
465 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000466 error_stream = target->GetDebugger().GetErrorFile().get();
Sean Callanan3989fb92011-01-27 01:07:04 +0000467
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000468 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000469 m_expr.NeedsVariableResolution(),
Greg Clayton23f8c952014-03-24 23:10:19 +0000470 *execution_unit_sp,
Sean Callanan3989fb92011-01-27 01:07:04 +0000471 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000472 function_name.AsCString());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000473
Greg Clayton23f8c952014-03-24 23:10:19 +0000474 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
Sean Callanan6961e872010-09-01 00:58:00 +0000475
Sean Callanan1582ee62013-04-18 22:06:33 +0000476 Error interpret_error;
Sean Callanan175a0d02012-01-24 22:06:48 +0000477
Greg Clayton23f8c952014-03-24 23:10:19 +0000478 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error);
Sean Callanan63697e52011-05-07 01:06:41 +0000479
Greg Claytonc14ee322011-09-22 04:58:26 +0000480 Process *process = exe_ctx.GetProcessPtr();
Sean Callanan1582ee62013-04-18 22:06:33 +0000481
482 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000483 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000484 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000485 return err;
486 }
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000487
Sean Callanan1582ee62013-04-18 22:06:33 +0000488 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000489 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000490 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
491 return err;
492 }
493
494 if (!process && execution_policy == eExecutionPolicyAlways)
495 {
496 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
497 return err;
498 }
499
500 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
501 {
Sean Callanane8cde682013-05-18 00:38:20 +0000502 if (m_expr.NeedsValidation() && process)
503 {
504 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000505 {
Sean Callanane8cde682013-05-18 00:38:20 +0000506 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sean Callanan90539452011-09-20 23:01:51 +0000507
Sean Callanane8cde682013-05-18 00:38:20 +0000508 StreamString install_errors;
509
510 if (!dynamic_checkers->Install(install_errors, exe_ctx))
511 {
512 if (install_errors.GetString().empty())
513 err.SetErrorString ("couldn't install checkers, unknown error");
514 else
515 err.SetErrorString (install_errors.GetString().c_str());
516
517 return err;
518 }
519
520 process->SetDynamicCheckers(dynamic_checkers);
521
522 if (log)
523 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000524 }
525
Sean Callanan1582ee62013-04-18 22:06:33 +0000526 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
527
Greg Clayton23f8c952014-03-24 23:10:19 +0000528 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000529 {
530 err.SetErrorToGenericError();
531 err.SetErrorString("Couldn't add dynamic checks to the expression");
532 return err;
533 }
Sean Callanan90539452011-09-20 23:01:51 +0000534 }
535
Greg Clayton23f8c952014-03-24 23:10:19 +0000536 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000537 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000538 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000539 else
540 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000541 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan1582ee62013-04-18 22:06:33 +0000542 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000543
Sean Callanan1a8d4092010-08-27 01:01:44 +0000544 return err;
545}
Greg Clayton23f8c952014-03-24 23:10:19 +0000546
547bool
548ClangExpressionParser::GetGenerateDebugInfo () const
549{
550 if (m_compiler)
551 return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;
552 return false;
553}