blob: ad17c4fd1f4860a3b299c4ac6b8a6b406e79ac09 [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
10#include "lldb/Expression/ClangExpressionParser.h"
11
12#include "lldb/Core/ArchSpec.h"
13#include "lldb/Core/DataBufferHeap.h"
Sean Callanan3989fb92011-01-27 01:07:04 +000014#include "lldb/Core/Debugger.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000015#include "lldb/Core/Disassembler.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000016#include "lldb/Core/Log.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000017#include "lldb/Core/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000018#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000019#include "lldb/Core/StreamFile.h"
Sean Callanan6961e872010-09-01 00:58:00 +000020#include "lldb/Core/StreamString.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000021#include "lldb/Expression/ClangASTSource.h"
22#include "lldb/Expression/ClangExpression.h"
Sean Callanan77502262011-05-12 23:54:16 +000023#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callananc8278af2014-12-05 01:27:35 +000024#include "lldb/Expression/ClangModulesDeclVendor.h"
Sean Callananf0c5aeb2015-04-20 16:31:29 +000025#include "lldb/Expression/ClangPersistentVariables.h"
Sean Callanan8dfb68e2013-03-19 00:10:07 +000026#include "lldb/Expression/IRExecutionUnit.h"
Sean Callanan6961e872010-09-01 00:58:00 +000027#include "lldb/Expression/IRDynamicChecks.h"
Sean Callanan1582ee62013-04-18 22:06:33 +000028#include "lldb/Expression/IRInterpreter.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000029#include "lldb/Host/File.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000030#include "lldb/Host/HostInfo.h"
Greg Clayton23f8c952014-03-24 23:10:19 +000031#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000032#include "lldb/Target/ExecutionContext.h"
Sean Callananc3a16002011-01-17 23:42:46 +000033#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000034#include "lldb/Target/Process.h"
35#include "lldb/Target/Target.h"
36
37#include "clang/AST/ASTContext.h"
38#include "clang/AST/ExternalASTSource.h"
39#include "clang/Basic/FileManager.h"
40#include "clang/Basic/TargetInfo.h"
41#include "clang/Basic/Version.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000042#include "clang/CodeGen/CodeGenAction.h"
43#include "clang/CodeGen/ModuleBuilder.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000044#include "clang/Frontend/CompilerInstance.h"
45#include "clang/Frontend/CompilerInvocation.h"
46#include "clang/Frontend/FrontendActions.h"
47#include "clang/Frontend/FrontendDiagnostic.h"
48#include "clang/Frontend/FrontendPluginRegistry.h"
49#include "clang/Frontend/TextDiagnosticBuffer.h"
50#include "clang/Frontend/TextDiagnosticPrinter.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000051#include "clang/Lex/Preprocessor.h"
Sean Callanane2ef6e32010-09-23 03:01:22 +000052#include "clang/Parse/ParseAST.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000053#include "clang/Rewrite/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000054#include "clang/Sema/SemaConsumer.h"
Sean Callananfb0b7582011-03-15 00:17:19 +000055#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000056
57#include "llvm/ADT/StringRef.h"
58#include "llvm/ExecutionEngine/ExecutionEngine.h"
Sean Callanan3d654b32012-09-24 22:25:51 +000059#include "llvm/Support/Debug.h"
Rafael Espindola4609ea82013-06-26 14:10:56 +000060#include "llvm/Support/FileSystem.h"
Sean Callanan880e6802011-10-07 23:18:13 +000061#include "llvm/Support/TargetSelect.h"
Greg Clayton70b57652011-05-15 01:25:55 +000062
Ed Mastea09ed032014-03-18 18:55:06 +000063#include "llvm/ExecutionEngine/MCJIT.h"
Chandler Carruth1e157582013-01-02 12:20:07 +000064#include "llvm/IR/LLVMContext.h"
65#include "llvm/IR/Module.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000066#include "llvm/Support/ErrorHandling.h"
67#include "llvm/Support/MemoryBuffer.h"
Greg Clayton38a61402010-12-02 23:20:03 +000068#include "llvm/Support/DynamicLibrary.h"
69#include "llvm/Support/Host.h"
70#include "llvm/Support/Signals.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000071
72using namespace clang;
73using namespace llvm;
74using namespace lldb_private;
75
76//===----------------------------------------------------------------------===//
77// Utility Methods for Clang
78//===----------------------------------------------------------------------===//
79
80std::string GetBuiltinIncludePath(const char *Argv0) {
Rafael Espindola4609ea82013-06-26 14:10:56 +000081 SmallString<128> P(llvm::sys::fs::getMainExecutable(
82 Argv0, (void *)(intptr_t) GetBuiltinIncludePath));
83
84 if (!P.empty()) {
85 llvm::sys::path::remove_filename(P); // Remove /clang from foo/bin/clang
86 llvm::sys::path::remove_filename(P); // Remove /bin from foo/bin
87
Sean Callanan1a8d4092010-08-27 01:01:44 +000088 // Get foo/lib/clang/<version>/include
Rafael Espindola4609ea82013-06-26 14:10:56 +000089 llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
90 "include");
Sean Callanan1a8d4092010-08-27 01:01:44 +000091 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000092
Sean Callanan1a8d4092010-08-27 01:01:44 +000093 return P.str();
94}
95
Sean Callananc8278af2014-12-05 01:27:35 +000096class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
97{
Sean Callananf0c5aeb2015-04-20 16:31:29 +000098 ClangModulesDeclVendor &m_decl_vendor;
99 ClangPersistentVariables &m_persistent_vars;
100 StreamString m_error_stream;
101 bool m_has_errors = false;
Sean Callananc8278af2014-12-05 01:27:35 +0000102public:
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000103 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor,
104 ClangPersistentVariables &persistent_vars) :
105 m_decl_vendor(decl_vendor),
106 m_persistent_vars(persistent_vars)
Sean Callananc8278af2014-12-05 01:27:35 +0000107 {
108 }
109
110 virtual void moduleImport(SourceLocation import_location,
111 ModuleIdPath path,
Zachary Turner22ac8712014-12-05 22:54:56 +0000112 const clang::Module * /*null*/)
Sean Callananc8278af2014-12-05 01:27:35 +0000113 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000114 std::vector<ConstString> string_path;
Sean Callananc8278af2014-12-05 01:27:35 +0000115
116 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
117 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000118 string_path.push_back(ConstString(component.first->getName()));
Sean Callananc8278af2014-12-05 01:27:35 +0000119 }
120
121 StreamString error_stream;
122
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000123 ClangModulesDeclVendor::ModuleVector exported_modules;
124
125 if (!m_decl_vendor.AddModule(string_path, &exported_modules, m_error_stream))
Sean Callananc8278af2014-12-05 01:27:35 +0000126 {
127 m_has_errors = true;
128 }
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000129
130 for (ClangModulesDeclVendor::ModuleID module : exported_modules)
131 {
132 m_persistent_vars.AddHandLoadedClangModule(module);
133 }
Sean Callananc8278af2014-12-05 01:27:35 +0000134 }
135
136 bool hasErrors()
137 {
138 return m_has_errors;
139 }
140
141 const std::string &getErrorString()
142 {
143 return m_error_stream.GetString();
144 }
145};
146
Sean Callanan1a8d4092010-08-27 01:01:44 +0000147//===----------------------------------------------------------------------===//
148// Implementation of ClangExpressionParser
149//===----------------------------------------------------------------------===//
150
Greg Clayton514487e2011-02-15 21:59:32 +0000151ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
Greg Clayton23f8c952014-03-24 23:10:19 +0000152 ClangExpression &expr,
153 bool generate_debug_info) :
Greg Clayton514487e2011-02-15 21:59:32 +0000154 m_expr (expr),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000155 m_compiler (),
Zachary Turner22ac8712014-12-05 22:54:56 +0000156 m_code_generator (),
157 m_pp_callbacks(nullptr)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000158{
Sean Callanan1a8d4092010-08-27 01:01:44 +0000159 // 1. Create a new compiler instance.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000160 m_compiler.reset(new CompilerInstance());
161
Sean Callanan3d654b32012-09-24 22:25:51 +0000162 // 2. Install the target.
163
164 lldb::TargetSP target_sp;
165 if (exe_scope)
166 target_sp = exe_scope->CalculateTarget();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000167
Sean Callanan3d654b32012-09-24 22:25:51 +0000168 // TODO: figure out what to really do when we don't have a valid target.
169 // Sometimes this will be ok to just use the host target triple (when we
170 // evaluate say "2+3", but other expressions like breakpoint conditions
171 // and other things that _are_ target specific really shouldn't just be
172 // using the host triple. This needs to be fixed in a better way.
173 if (target_sp && target_sp->GetArchitecture().IsValid())
174 {
175 std::string triple = target_sp->GetArchitecture().GetTriple().str();
Sean Callanan3d654b32012-09-24 22:25:51 +0000176 m_compiler->getTargetOpts().Triple = triple;
177 }
178 else
179 {
180 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
181 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000182
Sean Callananb45a6f02013-02-21 01:04:23 +0000183 if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
184 target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
185 {
186 m_compiler->getTargetOpts().Features.push_back("+sse");
187 m_compiler->getTargetOpts().Features.push_back("+sse2");
188 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000189
Jason Molendaa3329782014-03-29 18:54:20 +0000190 // Any arm32 iOS environment, but not on arm64
Sean Callanan60400ec2014-06-23 21:00:25 +0000191 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
192 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
193 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
Jason Molendaa3329782014-03-29 18:54:20 +0000194 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000195 m_compiler->getTargetOpts().ABI = "apcs-gnu";
Jason Molendaa3329782014-03-29 18:54:20 +0000196 }
197
Sean Callananb1de8dd2013-01-22 02:20:20 +0000198 m_compiler->createDiagnostics();
Jason Molendaa3329782014-03-29 18:54:20 +0000199
Sean Callanan3d654b32012-09-24 22:25:51 +0000200 // Create the target instance.
Alp Toker5f838642014-07-06 05:36:57 +0000201 m_compiler->setTarget(TargetInfo::CreateTargetInfo(
202 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
203
Sean Callanan3d654b32012-09-24 22:25:51 +0000204 assert (m_compiler->hasTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000205
Sean Callanan3d654b32012-09-24 22:25:51 +0000206 // 3. Set options.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000207
Sean Callananc7b65062011-11-07 23:35:40 +0000208 lldb::LanguageType language = expr.Language();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000209
Sean Callananc7b65062011-11-07 23:35:40 +0000210 switch (language)
211 {
212 case lldb::eLanguageTypeC:
213 break;
214 case lldb::eLanguageTypeObjC:
215 m_compiler->getLangOpts().ObjC1 = true;
216 m_compiler->getLangOpts().ObjC2 = true;
217 break;
218 case lldb::eLanguageTypeC_plus_plus:
219 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000220 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000221 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000222 break;
223 case lldb::eLanguageTypeObjC_plus_plus:
224 default:
225 m_compiler->getLangOpts().ObjC1 = true;
226 m_compiler->getLangOpts().ObjC2 = true;
227 m_compiler->getLangOpts().CPlusPlus = true;
Chandler Carruth38336a12013-01-02 12:55:00 +0000228 m_compiler->getLangOpts().CPlusPlus11 = true;
Jim Ingham26c7bf92014-10-11 00:38:27 +0000229 m_compiler->getHeaderSearchOpts().UseLibcxx = true;
Sean Callananc7b65062011-11-07 23:35:40 +0000230 break;
231 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000232
Sean Callananaa0f9cb2012-10-17 22:09:59 +0000233 m_compiler->getLangOpts().Bool = true;
234 m_compiler->getLangOpts().WChar = true;
Sean Callananeeb43842013-04-01 22:12:37 +0000235 m_compiler->getLangOpts().Blocks = true;
Sean Callanan226b70c2012-03-08 02:39:03 +0000236 m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
237 if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
238 m_compiler->getLangOpts().DebuggerCastResultToId = true;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000239
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000240 m_compiler->getLangOpts().CharIsSigned =
241 ArchSpec(m_compiler->getTargetOpts().Triple.c_str()).CharIsSignedByDefault();
242
Sean Callanan0765c822012-04-17 00:49:48 +0000243 // Spell checking is a nice feature, but it ends up completing a
244 // lot of types that we didn't strictly speaking need to complete.
245 // As a result, we spend a long time parsing and importing debug
246 // information.
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000247 m_compiler->getLangOpts().SpellChecking = false;
248
Greg Claytond9e416c2012-02-18 05:35:26 +0000249 lldb::ProcessSP process_sp;
Greg Clayton514487e2011-02-15 21:59:32 +0000250 if (exe_scope)
Greg Claytond9e416c2012-02-18 05:35:26 +0000251 process_sp = exe_scope->CalculateProcess();
Greg Clayton514487e2011-02-15 21:59:32 +0000252
Greg Claytond9e416c2012-02-18 05:35:26 +0000253 if (process_sp && m_compiler->getLangOpts().ObjC1)
Sean Callananc3a16002011-01-17 23:42:46 +0000254 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000255 if (process_sp->GetObjCLanguageRuntime())
Sean Callananc3a16002011-01-17 23:42:46 +0000256 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000257 if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
Sean Callanan3d654b32012-09-24 22:25:51 +0000258 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
259 else
260 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan226b70c2012-03-08 02:39:03 +0000262 if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
Sean Callanan226b70c2012-03-08 02:39:03 +0000263 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
Sean Callananc3a16002011-01-17 23:42:46 +0000264 }
265 }
Greg Claytonf83f32d2011-01-15 01:32:14 +0000266
Sean Callanan1a8d4092010-08-27 01:01:44 +0000267 m_compiler->getLangOpts().ThreadsafeStatics = false;
268 m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
269 m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000270
Sean Callanan1a8d4092010-08-27 01:01:44 +0000271 // Set CodeGen options
272 m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
273 m_compiler->getCodeGenOpts().InstrumentFunctions = false;
Greg Claytonbc3122e2013-11-04 19:50:49 +0000274 m_compiler->getCodeGenOpts().DisableFPElim = true;
275 m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
Greg Clayton23f8c952014-03-24 23:10:19 +0000276 if (generate_debug_info)
277 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
278 else
279 m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000280
Sean Callanan1a8d4092010-08-27 01:01:44 +0000281 // Disable some warnings.
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000282 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000283 "unused-value", clang::diag::Severity::Ignored, SourceLocation());
Hafiz Abid Qadeer101e4902014-08-07 12:54:20 +0000284 m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
Alp Toker10933d12014-06-12 11:14:32 +0000285 "odr", clang::diag::Severity::Ignored, SourceLocation());
286
Sean Callanan1a8d4092010-08-27 01:01:44 +0000287 // Inform the target of the language options
288 //
289 // FIXME: We shouldn't need to do this, the target should be immutable once
290 // created. This complexity should be lifted elsewhere.
Sylvestre Ledru3cc9d632014-07-06 17:50:36 +0000291 m_compiler->getTarget().adjust(m_compiler->getLangOpts());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000292
Sean Callanan1a8d4092010-08-27 01:01:44 +0000293 // 4. Set up the diagnostic buffer for reporting errors
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000294
Sean Callanane2ef6e32010-09-23 03:01:22 +0000295 m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000296
Sean Callanan1a8d4092010-08-27 01:01:44 +0000297 // 5. Set up the source management objects inside the compiler
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000298
Greg Clayton38a61402010-12-02 23:20:03 +0000299 clang::FileSystemOptions file_system_options;
300 m_file_manager.reset(new clang::FileManager(file_system_options));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000301
Sean Callanan1a8d4092010-08-27 01:01:44 +0000302 if (!m_compiler->hasSourceManager())
Greg Clayton38a61402010-12-02 23:20:03 +0000303 m_compiler->createSourceManager(*m_file_manager.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000304
Sean Callanan1a8d4092010-08-27 01:01:44 +0000305 m_compiler->createFileManager();
Ben Langmuir9f0bac52014-03-07 08:31:36 +0000306 m_compiler->createPreprocessor(TU_Complete);
Sean Callananc8278af2014-12-05 01:27:35 +0000307
308 if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
309 {
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000310 std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor, target_sp->GetPersistentVariables()));
Sean Callananc8278af2014-12-05 01:27:35 +0000311 m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
312 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
313 }
314
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000315 // 6. Most of this we get from the CompilerInstance, but we
Sean Callanan1a8d4092010-08-27 01:01:44 +0000316 // also want to give the context an ExternalASTSource.
Sean Callanan6abfabf2010-11-19 20:20:02 +0000317 m_selector_table.reset(new SelectorTable());
Sean Callanan880e6802011-10-07 23:18:13 +0000318 m_builtin_context.reset(new Builtin::Context());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000319
Greg Clayton7b0992d2013-04-18 22:45:39 +0000320 std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
Sean Callananf0c5aeb2015-04-20 16:31:29 +0000321 m_compiler->getSourceManager(),
322 m_compiler->getPreprocessor().getIdentifierTable(),
323 *m_selector_table.get(),
324 *m_builtin_context.get()));
Sean Callananc8278af2014-12-05 01:27:35 +0000325
Alp Tokercf55e882014-05-03 15:05:45 +0000326 ast_context->InitBuiltinTypes(m_compiler->getTarget());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000327
Sean Callanan1a8d4092010-08-27 01:01:44 +0000328 ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000329
Sean Callanan1a8d4092010-08-27 01:01:44 +0000330 if (decl_map)
331 {
Sylvestre Ledru7ba631f2014-02-27 10:46:57 +0000332 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
Sean Callananeddeb3b2011-10-28 23:38:38 +0000333 decl_map->InstallASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000334 ast_context->setExternalSource(ast_source);
335 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Greg Claytond8d4a572015-08-11 21:38:15 +0000337 m_ast_context.reset(new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str()));
338 m_ast_context->setASTContext(ast_context.get());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000339 m_compiler->setASTContext(ast_context.release());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000340
Greg Clayton7b462cc2010-10-15 22:48:33 +0000341 std::string module_name("$__lldb_module");
Sean Callanan1a8d4092010-08-27 01:01:44 +0000342
Sean Callananfb0b7582011-03-15 00:17:19 +0000343 m_llvm_context.reset(new LLVMContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000344 m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
345 module_name,
Tamas Berghammer0b736f12015-06-30 09:26:52 +0000346 m_compiler->getHeaderSearchOpts(),
347 m_compiler->getPreprocessorOpts(),
Sean Callanan1a8d4092010-08-27 01:01:44 +0000348 m_compiler->getCodeGenOpts(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000349 *m_llvm_context));
Sean Callanan1a8d4092010-08-27 01:01:44 +0000350}
351
352ClangExpressionParser::~ClangExpressionParser()
353{
354}
355
356unsigned
357ClangExpressionParser::Parse (Stream &stream)
358{
Sean Callanane2ef6e32010-09-23 03:01:22 +0000359 TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000360
Sean Callanane2ef6e32010-09-23 03:01:22 +0000361 diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000362
Greg Clayton23f8c952014-03-24 23:10:19 +0000363 const char *expr_text = m_expr.Text();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000364
365 clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
Greg Clayton23f8c952014-03-24 23:10:19 +0000366 bool created_main_file = false;
367 if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
368 {
369 std::string temp_source_path;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000370
Zachary Turnercb5742b2014-10-20 17:46:56 +0000371 int temp_fd = -1;
372 llvm::SmallString<PATH_MAX> result_path;
Greg Clayton23f8c952014-03-24 23:10:19 +0000373 FileSpec tmpdir_file_spec;
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000374 if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Clayton23f8c952014-03-24 23:10:19 +0000375 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000376 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
Pavel Labath3a29f8b2015-07-28 09:18:32 +0000377 temp_source_path = tmpdir_file_spec.GetPath();
Zachary Turnercb5742b2014-10-20 17:46:56 +0000378 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000379 }
380 else
381 {
Zachary Turnercb5742b2014-10-20 17:46:56 +0000382 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
Greg Clayton23f8c952014-03-24 23:10:19 +0000383 }
Jason Molendabc464ee2014-10-16 23:10:03 +0000384
385 if (temp_fd != -1)
Greg Clayton23f8c952014-03-24 23:10:19 +0000386 {
Jason Molendabc464ee2014-10-16 23:10:03 +0000387 lldb_private::File file (temp_fd, true);
Greg Clayton23f8c952014-03-24 23:10:19 +0000388 const size_t expr_text_len = strlen(expr_text);
389 size_t bytes_written = expr_text_len;
390 if (file.Write(expr_text, bytes_written).Success())
391 {
392 if (bytes_written == expr_text_len)
393 {
394 file.Close();
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000395 SourceMgr.setMainFileID(SourceMgr.createFileID(
Zachary Turnercb5742b2014-10-20 17:46:56 +0000396 m_file_manager->getFile(result_path),
Rafael Espindola9cb97b62014-05-21 15:08:27 +0000397 SourceLocation(), SrcMgr::C_User));
Greg Clayton23f8c952014-03-24 23:10:19 +0000398 created_main_file = true;
399 }
400 }
401 }
402 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000403
Greg Clayton23f8c952014-03-24 23:10:19 +0000404 if (!created_main_file)
405 {
Rafael Espindola2443e702014-08-27 20:09:08 +0000406 std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
Sean Callanan302be672014-08-30 02:24:56 +0000407 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
Greg Clayton23f8c952014-03-24 23:10:19 +0000408 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000409
Sean Callanane2ef6e32010-09-23 03:01:22 +0000410 diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000411
Sean Callanan1a8d4092010-08-27 01:01:44 +0000412 ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000413
Sean Callanan80c97592015-05-01 00:47:29 +0000414 if (ClangExpressionDeclMap *decl_map = m_expr.DeclMap())
415 decl_map->InstallCodeGenerator(m_code_generator.get());
Chaoren Lin57998de2015-08-19 01:24:57 +0000416
Sean Callanan1a8d4092010-08-27 01:01:44 +0000417 if (ast_transformer)
Chaoren Lin57998de2015-08-19 01:24:57 +0000418 {
419 ast_transformer->Initialize(m_compiler->getASTContext());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000420 ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000421 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000422 else
Chaoren Lin57998de2015-08-19 01:24:57 +0000423 {
424 m_code_generator->Initialize(m_compiler->getASTContext());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000425 ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
Chaoren Lin57998de2015-08-19 01:24:57 +0000426 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000427
Sean Callanane2ef6e32010-09-23 03:01:22 +0000428 diag_buf->EndSourceFile();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000429
Sean Callanan1a8d4092010-08-27 01:01:44 +0000430 TextDiagnosticBuffer::const_iterator diag_iterator;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000431
Sean Callanan1a8d4092010-08-27 01:01:44 +0000432 int num_errors = 0;
Sean Callananc8278af2014-12-05 01:27:35 +0000433
Zachary Turner22ac8712014-12-05 22:54:56 +0000434 if (m_pp_callbacks && m_pp_callbacks->hasErrors())
Sean Callananc8278af2014-12-05 01:27:35 +0000435 {
436 num_errors++;
437
438 stream.PutCString(m_pp_callbacks->getErrorString().c_str());
439 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000440
Sean Callanane2ef6e32010-09-23 03:01:22 +0000441 for (diag_iterator = diag_buf->warn_begin();
442 diag_iterator != diag_buf->warn_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000443 ++diag_iterator)
444 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000445
Sean Callanane2ef6e32010-09-23 03:01:22 +0000446 for (diag_iterator = diag_buf->err_begin();
447 diag_iterator != diag_buf->err_end();
Sean Callanan1a8d4092010-08-27 01:01:44 +0000448 ++diag_iterator)
449 {
450 num_errors++;
451 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
452 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000453
Sean Callanan57bbc6e2010-11-01 20:28:09 +0000454 for (diag_iterator = diag_buf->note_begin();
455 diag_iterator != diag_buf->note_end();
456 ++diag_iterator)
457 stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000458
Sean Callanan77502262011-05-12 23:54:16 +0000459 if (!num_errors)
460 {
461 if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
462 {
463 stream.Printf("error: Couldn't infer the type of a variable\n");
464 num_errors++;
465 }
466 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000467
Sean Callanan1a8d4092010-08-27 01:01:44 +0000468 return num_errors;
469}
470
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000471static bool FindFunctionInModule (ConstString &mangled_name,
Sean Callananfc55f5d2010-09-21 00:44:12 +0000472 llvm::Module *module,
473 const char *orig_name)
474{
475 for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
476 fi != fe;
477 ++fi)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000478 {
Sean Callananfc55f5d2010-09-21 00:44:12 +0000479 if (fi->getName().str().find(orig_name) != std::string::npos)
480 {
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000481 mangled_name.SetCString(fi->getName().str().c_str());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000482 return true;
483 }
484 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000485
Sean Callananfc55f5d2010-09-21 00:44:12 +0000486 return false;
487}
488
Sean Callanan1a8d4092010-08-27 01:01:44 +0000489Error
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000490ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000491 lldb::addr_t &func_end,
Greg Clayton23f8c952014-03-24 23:10:19 +0000492 std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000493 ExecutionContext &exe_ctx,
Sean Callanan1582ee62013-04-18 22:06:33 +0000494 bool &can_interpret,
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000495 ExecutionPolicy execution_policy)
Sean Callanan1a8d4092010-08-27 01:01:44 +0000496{
Greg Clayton22a939a2011-01-19 23:00:49 +0000497 func_addr = LLDB_INVALID_ADDRESS;
498 func_end = LLDB_INVALID_ADDRESS;
Greg Clayton5160ce52013-03-27 23:08:40 +0000499 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfc55f5d2010-09-21 00:44:12 +0000500
Sean Callanan1a8d4092010-08-27 01:01:44 +0000501 Error err;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000502
Greg Clayton23f8c952014-03-24 23:10:19 +0000503 std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
Sean Callanan1a8d4092010-08-27 01:01:44 +0000504
Greg Clayton23f8c952014-03-24 23:10:19 +0000505 if (!llvm_module_ap.get())
Sean Callanan1a8d4092010-08-27 01:01:44 +0000506 {
507 err.SetErrorToGenericError();
508 err.SetErrorString("IR doesn't contain a module");
509 return err;
510 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000511
Sean Callananfc55f5d2010-09-21 00:44:12 +0000512 // Find the actual name of the function (it's often mangled somehow)
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000513
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000514 ConstString function_name;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000515
Greg Clayton23f8c952014-03-24 23:10:19 +0000516 if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
Sean Callananfc55f5d2010-09-21 00:44:12 +0000517 {
518 err.SetErrorToGenericError();
519 err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
520 return err;
521 }
522 else
523 {
Enrico Granata20edcdb2011-07-19 18:03:25 +0000524 if (log)
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000525 log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
Sean Callananfc55f5d2010-09-21 00:44:12 +0000526 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000527
Greg Clayton23f8c952014-03-24 23:10:19 +0000528 execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
529 llvm_module_ap, // handed off here
530 function_name,
531 exe_ctx.GetTargetSP(),
532 m_compiler->getTargetOpts().Features));
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000533
Sean Callanan1a8d4092010-08-27 01:01:44 +0000534 ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000535
Sean Callanan1a8d4092010-08-27 01:01:44 +0000536 if (decl_map)
537 {
Sean Callanan3989fb92011-01-27 01:07:04 +0000538 Stream *error_stream = NULL;
Greg Claytonc14ee322011-09-22 04:58:26 +0000539 Target *target = exe_ctx.GetTargetPtr();
540 if (target)
Greg Clayton44d93782014-01-27 23:43:24 +0000541 error_stream = target->GetDebugger().GetErrorFile().get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000542
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000543 IRForTarget ir_for_target(decl_map,
Sean Callanan9e6ed532010-09-13 21:34:21 +0000544 m_expr.NeedsVariableResolution(),
Greg Clayton23f8c952014-03-24 23:10:19 +0000545 *execution_unit_sp,
Sean Callanan3989fb92011-01-27 01:07:04 +0000546 error_stream,
Sean Callanan8dfb68e2013-03-19 00:10:07 +0000547 function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000548
Greg Clayton23f8c952014-03-24 23:10:19 +0000549 bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000550
Sean Callanan1582ee62013-04-18 22:06:33 +0000551 Error interpret_error;
Greg Claytonc14ee322011-09-22 04:58:26 +0000552 Process *process = exe_ctx.GetProcessPtr();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000553
Ewan Crawford90ff7912015-07-14 10:56:58 +0000554 bool interpret_function_calls = !process ? false : process->CanInterpretFunctionCalls();
555 can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error, interpret_function_calls);
556
557
Sean Callanan1582ee62013-04-18 22:06:33 +0000558 if (!ir_can_run)
Sean Callanan3bfdaa22011-09-15 02:13:07 +0000559 {
Sean Callanan3d654b32012-09-24 22:25:51 +0000560 err.SetErrorString("The expression could not be prepared to run in the target");
Sean Callanan3d654b32012-09-24 22:25:51 +0000561 return err;
562 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000563
Sean Callanan1582ee62013-04-18 22:06:33 +0000564 if (!can_interpret && execution_policy == eExecutionPolicyNever)
Sean Callanan6961e872010-09-01 00:58:00 +0000565 {
Sean Callanan1582ee62013-04-18 22:06:33 +0000566 err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
567 return err;
568 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000569
Sean Callanan1582ee62013-04-18 22:06:33 +0000570 if (!process && execution_policy == eExecutionPolicyAlways)
571 {
572 err.SetErrorString("Expression needed to run in the target, but the target can't be run");
573 return err;
574 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000575
Sean Callanan1582ee62013-04-18 22:06:33 +0000576 if (execution_policy == eExecutionPolicyAlways || !can_interpret)
577 {
Sean Callanane8cde682013-05-18 00:38:20 +0000578 if (m_expr.NeedsValidation() && process)
579 {
580 if (!process->GetDynamicCheckers())
Sean Callanan90539452011-09-20 23:01:51 +0000581 {
Sean Callanane8cde682013-05-18 00:38:20 +0000582 DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000583
Sean Callanane8cde682013-05-18 00:38:20 +0000584 StreamString install_errors;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000585
Sean Callanane8cde682013-05-18 00:38:20 +0000586 if (!dynamic_checkers->Install(install_errors, exe_ctx))
587 {
588 if (install_errors.GetString().empty())
589 err.SetErrorString ("couldn't install checkers, unknown error");
590 else
591 err.SetErrorString (install_errors.GetString().c_str());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000592
Sean Callanane8cde682013-05-18 00:38:20 +0000593 return err;
594 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000595
Sean Callanane8cde682013-05-18 00:38:20 +0000596 process->SetDynamicCheckers(dynamic_checkers);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000597
Sean Callanane8cde682013-05-18 00:38:20 +0000598 if (log)
599 log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
Sean Callanan90539452011-09-20 23:01:51 +0000600 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000601
Sean Callanan1582ee62013-04-18 22:06:33 +0000602 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000603
Greg Clayton23f8c952014-03-24 23:10:19 +0000604 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
Sean Callanan1582ee62013-04-18 22:06:33 +0000605 {
606 err.SetErrorToGenericError();
607 err.SetErrorString("Couldn't add dynamic checks to the expression");
608 return err;
609 }
Sean Callanan90539452011-09-20 23:01:51 +0000610 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000611
Greg Clayton23f8c952014-03-24 23:10:19 +0000612 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000613 }
Sean Callanan1a8d4092010-08-27 01:01:44 +0000614 }
Sean Callanan1582ee62013-04-18 22:06:33 +0000615 else
616 {
Greg Clayton23f8c952014-03-24 23:10:19 +0000617 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
Sean Callanan1582ee62013-04-18 22:06:33 +0000618 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000619
Sean Callanan1a8d4092010-08-27 01:01:44 +0000620 return err;
621}
Greg Clayton23f8c952014-03-24 23:10:19 +0000622
623bool
624ClangExpressionParser::GetGenerateDebugInfo () const
625{
626 if (m_compiler)
627 return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;
628 return false;
629}