]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Expression/ClangExpressionParser.cpp
Revert LLDB compatibility changes for Clang 3.5 API
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Expression / ClangExpressionParser.cpp
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
10 #include "lldb/lldb-python.h"
11
12 #include "lldb/Expression/ClangExpressionParser.h"
13
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/DataBufferHeap.h"
16 #include "lldb/Core/Debugger.h"
17 #include "lldb/Core/Disassembler.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Core/StreamFile.h"
21 #include "lldb/Core/StreamString.h"
22 #include "lldb/Expression/ClangASTSource.h"
23 #include "lldb/Expression/ClangExpression.h"
24 #include "lldb/Expression/ClangExpressionDeclMap.h"
25 #include "lldb/Expression/ClangModulesDeclVendor.h"
26 #include "lldb/Expression/IRExecutionUnit.h"
27 #include "lldb/Expression/IRDynamicChecks.h"
28 #include "lldb/Expression/IRInterpreter.h"
29 #include "lldb/Host/File.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Symbol/SymbolVendor.h"
32 #include "lldb/Target/ExecutionContext.h"
33 #include "lldb/Target/ObjCLanguageRuntime.h"
34 #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"
42 #include "clang/CodeGen/CodeGenAction.h"
43 #include "clang/CodeGen/ModuleBuilder.h"
44 #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"
51 #include "clang/Lex/Preprocessor.h"
52 #include "clang/Parse/ParseAST.h"
53 #include "clang/Rewrite/Frontend/FrontendActions.h"
54 #include "clang/Sema/SemaConsumer.h"
55 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
56
57 #include "llvm/ADT/StringRef.h"
58 #include "llvm/ExecutionEngine/ExecutionEngine.h"
59 #include "llvm/Support/Debug.h"
60 #include "llvm/Support/FileSystem.h"
61 #include "llvm/Support/TargetSelect.h"
62
63 #include "llvm/ExecutionEngine/MCJIT.h"
64 #include "llvm/IR/LLVMContext.h"
65 #include "llvm/IR/Module.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/MemoryBuffer.h"
68 #include "llvm/Support/DynamicLibrary.h"
69 #include "llvm/Support/Host.h"
70 #include "llvm/Support/Signals.h"
71
72 using namespace clang;
73 using namespace llvm;
74 using namespace lldb_private;
75
76 //===----------------------------------------------------------------------===//
77 // Utility Methods for Clang
78 //===----------------------------------------------------------------------===//
79
80 std::string GetBuiltinIncludePath(const char *Argv0) {
81     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
88         // Get foo/lib/clang/<version>/include
89         llvm::sys::path::append(P, "lib", "clang", CLANG_VERSION_STRING,
90                                 "include");
91     }
92
93     return P.str();
94 }
95
96 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks
97 {
98     ClangModulesDeclVendor  &m_decl_vendor;
99     StreamString             m_error_stream;
100     bool                     m_has_errors = false;
101 public:
102     LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor) :
103         m_decl_vendor(decl_vendor)
104     {
105     }
106     
107     virtual void moduleImport(SourceLocation import_location,
108                               ModuleIdPath path,
109                               const clang::Module * /*null*/)
110     {
111         std::vector<llvm::StringRef> string_path;
112         
113         for (const std::pair<IdentifierInfo *, SourceLocation> &component : path)
114         {
115             string_path.push_back(component.first->getName());
116         }
117      
118         StreamString error_stream;
119         
120         if (!m_decl_vendor.AddModule(string_path, m_error_stream))
121         {
122             m_has_errors = true;
123         }
124     }
125     
126     bool hasErrors()
127     {
128         return m_has_errors;
129     }
130     
131     const std::string &getErrorString()
132     {
133         return m_error_stream.GetString();
134     }
135 };
136
137 //===----------------------------------------------------------------------===//
138 // Implementation of ClangExpressionParser
139 //===----------------------------------------------------------------------===//
140
141 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
142                                               ClangExpression &expr,
143                                               bool generate_debug_info) :
144     m_expr (expr),
145     m_compiler (),
146     m_code_generator (),
147     m_pp_callbacks(nullptr)
148 {
149     // 1. Create a new compiler instance.
150     m_compiler.reset(new CompilerInstance());
151
152     // 2. Install the target.
153
154     lldb::TargetSP target_sp;
155     if (exe_scope)
156         target_sp = exe_scope->CalculateTarget();
157
158     // TODO: figure out what to really do when we don't have a valid target.
159     // Sometimes this will be ok to just use the host target triple (when we
160     // evaluate say "2+3", but other expressions like breakpoint conditions
161     // and other things that _are_ target specific really shouldn't just be
162     // using the host triple. This needs to be fixed in a better way.
163     if (target_sp && target_sp->GetArchitecture().IsValid())
164     {
165         std::string triple = target_sp->GetArchitecture().GetTriple().str();
166         m_compiler->getTargetOpts().Triple = triple;
167     }
168     else
169     {
170         m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
171     }
172
173     if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
174         target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
175     {
176         m_compiler->getTargetOpts().Features.push_back("+sse");
177         m_compiler->getTargetOpts().Features.push_back("+sse2");
178     }
179
180     // Any arm32 iOS environment, but not on arm64
181     if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos &&
182         m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos &&
183         m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
184     {
185         m_compiler->getTargetOpts().ABI = "apcs-gnu";
186     }
187
188     m_compiler->createDiagnostics();
189
190     // Create the target instance.
191     m_compiler->setTarget(TargetInfo::CreateTargetInfo(
192         m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts));
193
194     assert (m_compiler->hasTarget());
195
196     // 3. Set options.
197
198     lldb::LanguageType language = expr.Language();
199
200     switch (language)
201     {
202     case lldb::eLanguageTypeC:
203         break;
204     case lldb::eLanguageTypeObjC:
205         m_compiler->getLangOpts().ObjC1 = true;
206         m_compiler->getLangOpts().ObjC2 = true;
207         break;
208     case lldb::eLanguageTypeC_plus_plus:
209         m_compiler->getLangOpts().CPlusPlus = true;
210         m_compiler->getLangOpts().CPlusPlus11 = true;
211         m_compiler->getHeaderSearchOpts().UseLibcxx = true;
212         break;
213     case lldb::eLanguageTypeObjC_plus_plus:
214     default:
215         m_compiler->getLangOpts().ObjC1 = true;
216         m_compiler->getLangOpts().ObjC2 = true;
217         m_compiler->getLangOpts().CPlusPlus = true;
218         m_compiler->getLangOpts().CPlusPlus11 = true;
219         m_compiler->getHeaderSearchOpts().UseLibcxx = true;
220         break;
221     }
222
223     m_compiler->getLangOpts().Bool = true;
224     m_compiler->getLangOpts().WChar = true;
225     m_compiler->getLangOpts().Blocks = true;
226     m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
227     if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
228         m_compiler->getLangOpts().DebuggerCastResultToId = true;
229
230     // Spell checking is a nice feature, but it ends up completing a
231     // lot of types that we didn't strictly speaking need to complete.
232     // As a result, we spend a long time parsing and importing debug
233     // information.
234     m_compiler->getLangOpts().SpellChecking = false;
235
236     lldb::ProcessSP process_sp;
237     if (exe_scope)
238         process_sp = exe_scope->CalculateProcess();
239
240     if (process_sp && m_compiler->getLangOpts().ObjC1)
241     {
242         if (process_sp->GetObjCLanguageRuntime())
243         {
244             if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
245                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
246             else
247                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
248
249             if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
250                 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
251         }
252     }
253
254     m_compiler->getLangOpts().ThreadsafeStatics = false;
255     m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
256     m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
257
258     // Set CodeGen options
259     m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
260     m_compiler->getCodeGenOpts().InstrumentFunctions = false;
261     m_compiler->getCodeGenOpts().DisableFPElim = true;
262     m_compiler->getCodeGenOpts().OmitLeafFramePointer = false;
263     if (generate_debug_info)
264         m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::FullDebugInfo);
265     else
266         m_compiler->getCodeGenOpts().setDebugInfo(CodeGenOptions::NoDebugInfo);
267
268     // Disable some warnings.
269     m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
270         "unused-value", clang::diag::Severity::Ignored, SourceLocation());
271     m_compiler->getDiagnostics().setSeverityForGroup(clang::diag::Flavor::WarningOrError,
272         "odr", clang::diag::Severity::Ignored, SourceLocation());
273
274     // Inform the target of the language options
275     //
276     // FIXME: We shouldn't need to do this, the target should be immutable once
277     // created. This complexity should be lifted elsewhere.
278     m_compiler->getTarget().adjust(m_compiler->getLangOpts());
279
280     // 4. Set up the diagnostic buffer for reporting errors
281
282     m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
283
284     // 5. Set up the source management objects inside the compiler
285
286     clang::FileSystemOptions file_system_options;
287     m_file_manager.reset(new clang::FileManager(file_system_options));
288
289     if (!m_compiler->hasSourceManager())
290         m_compiler->createSourceManager(*m_file_manager.get());
291
292     m_compiler->createFileManager();
293     m_compiler->createPreprocessor(TU_Complete);
294     
295     if (ClangModulesDeclVendor *decl_vendor = target_sp->GetClangModulesDeclVendor())
296     {
297         std::unique_ptr<PPCallbacks> pp_callbacks(new LLDBPreprocessorCallbacks(*decl_vendor));
298         m_pp_callbacks = static_cast<LLDBPreprocessorCallbacks*>(pp_callbacks.get());
299         m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks));
300     }
301         
302     // 6. Most of this we get from the CompilerInstance, but we
303     // also want to give the context an ExternalASTSource.
304     m_selector_table.reset(new SelectorTable());
305     m_builtin_context.reset(new Builtin::Context());
306
307     std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
308                                                                  m_compiler->getSourceManager(),
309                                                                  m_compiler->getPreprocessor().getIdentifierTable(),
310                                                                  *m_selector_table.get(),
311                                                                  *m_builtin_context.get()));
312     
313     ast_context->InitBuiltinTypes(m_compiler->getTarget());
314
315     ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
316
317     if (decl_map)
318     {
319         llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
320         decl_map->InstallASTContext(ast_context.get());
321         ast_context->setExternalSource(ast_source);
322     }
323
324     m_compiler->setASTContext(ast_context.release());
325
326     std::string module_name("$__lldb_module");
327
328     m_llvm_context.reset(new LLVMContext());
329     m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
330                                              module_name,
331                                              m_compiler->getCodeGenOpts(),
332                                              m_compiler->getTargetOpts(),
333                                              *m_llvm_context));
334 }
335
336 ClangExpressionParser::~ClangExpressionParser()
337 {
338 }
339
340 unsigned
341 ClangExpressionParser::Parse (Stream &stream)
342 {
343     TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
344
345     diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
346
347     const char *expr_text = m_expr.Text();
348
349     clang::SourceManager &SourceMgr = m_compiler->getSourceManager();
350     bool created_main_file = false;
351     if (m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo)
352     {
353         std::string temp_source_path;
354
355         int temp_fd = -1;
356         llvm::SmallString<PATH_MAX> result_path;
357         FileSpec tmpdir_file_spec;
358         if (HostInfo::GetLLDBPath(lldb::ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
359         {
360             tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr");
361             temp_source_path = std::move(tmpdir_file_spec.GetPath());
362             llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path);
363         }
364         else
365         {
366             llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path);
367         }
368         
369         if (temp_fd != -1)
370         {
371             lldb_private::File file (temp_fd, true);
372             const size_t expr_text_len = strlen(expr_text);
373             size_t bytes_written = expr_text_len;
374             if (file.Write(expr_text, bytes_written).Success())
375             {
376                 if (bytes_written == expr_text_len)
377                 {
378                     file.Close();
379                     SourceMgr.setMainFileID(SourceMgr.createFileID(
380                         m_file_manager->getFile(result_path),
381                         SourceLocation(), SrcMgr::C_User));
382                     created_main_file = true;
383                 }
384             }
385         }
386     }
387
388     if (!created_main_file)
389     {
390         std::unique_ptr<MemoryBuffer> memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
391         SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(memory_buffer)));
392     }
393
394     diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
395
396     ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
397
398     if (ast_transformer)
399         ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
400     else
401         ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());
402
403     diag_buf->EndSourceFile();
404
405     TextDiagnosticBuffer::const_iterator diag_iterator;
406
407     int num_errors = 0;
408     
409     if (m_pp_callbacks && m_pp_callbacks->hasErrors())
410     {
411         num_errors++;
412         
413         stream.PutCString(m_pp_callbacks->getErrorString().c_str());
414     }
415
416     for (diag_iterator = diag_buf->warn_begin();
417          diag_iterator != diag_buf->warn_end();
418          ++diag_iterator)
419         stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
420
421     for (diag_iterator = diag_buf->err_begin();
422          diag_iterator != diag_buf->err_end();
423          ++diag_iterator)
424     {
425         num_errors++;
426         stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
427     }
428
429     for (diag_iterator = diag_buf->note_begin();
430          diag_iterator != diag_buf->note_end();
431          ++diag_iterator)
432         stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
433
434     if (!num_errors)
435     {
436         if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
437         {
438             stream.Printf("error: Couldn't infer the type of a variable\n");
439             num_errors++;
440         }
441     }
442
443     return num_errors;
444 }
445
446 static bool FindFunctionInModule (ConstString &mangled_name,
447                                   llvm::Module *module,
448                                   const char *orig_name)
449 {
450     for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
451          fi != fe;
452          ++fi)
453     {
454         if (fi->getName().str().find(orig_name) != std::string::npos)
455         {
456             mangled_name.SetCString(fi->getName().str().c_str());
457             return true;
458         }
459     }
460
461     return false;
462 }
463
464 Error
465 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr,
466                                             lldb::addr_t &func_end,
467                                             std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
468                                             ExecutionContext &exe_ctx,
469                                             bool &can_interpret,
470                                             ExecutionPolicy execution_policy)
471 {
472         func_addr = LLDB_INVALID_ADDRESS;
473         func_end = LLDB_INVALID_ADDRESS;
474     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
475
476     Error err;
477
478     std::unique_ptr<llvm::Module> llvm_module_ap (m_code_generator->ReleaseModule());
479
480     if (!llvm_module_ap.get())
481     {
482         err.SetErrorToGenericError();
483         err.SetErrorString("IR doesn't contain a module");
484         return err;
485     }
486
487     // Find the actual name of the function (it's often mangled somehow)
488
489     ConstString function_name;
490
491     if (!FindFunctionInModule(function_name, llvm_module_ap.get(), m_expr.FunctionName()))
492     {
493         err.SetErrorToGenericError();
494         err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
495         return err;
496     }
497     else
498     {
499         if (log)
500             log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
501     }
502
503     execution_unit_sp.reset(new IRExecutionUnit (m_llvm_context, // handed off here
504                                                  llvm_module_ap, // handed off here
505                                                  function_name,
506                                                  exe_ctx.GetTargetSP(),
507                                                  m_compiler->getTargetOpts().Features));
508
509     ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
510
511     if (decl_map)
512     {
513         Stream *error_stream = NULL;
514         Target *target = exe_ctx.GetTargetPtr();
515         if (target)
516             error_stream = target->GetDebugger().GetErrorFile().get();
517
518         IRForTarget ir_for_target(decl_map,
519                                   m_expr.NeedsVariableResolution(),
520                                   *execution_unit_sp,
521                                   error_stream,
522                                   function_name.AsCString());
523
524         bool ir_can_run = ir_for_target.runOnModule(*execution_unit_sp->GetModule());
525
526         Error interpret_error;
527
528         can_interpret = IRInterpreter::CanInterpret(*execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), interpret_error);
529
530         Process *process = exe_ctx.GetProcessPtr();
531
532         if (!ir_can_run)
533         {
534             err.SetErrorString("The expression could not be prepared to run in the target");
535             return err;
536         }
537
538         if (!can_interpret && execution_policy == eExecutionPolicyNever)
539         {
540             err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
541             return err;
542         }
543
544         if (!process && execution_policy == eExecutionPolicyAlways)
545         {
546             err.SetErrorString("Expression needed to run in the target, but the target can't be run");
547             return err;
548         }
549
550         if (execution_policy == eExecutionPolicyAlways || !can_interpret)
551         {
552             if (m_expr.NeedsValidation() && process)
553             {
554                 if (!process->GetDynamicCheckers())
555                 {
556                     DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
557
558                     StreamString install_errors;
559
560                     if (!dynamic_checkers->Install(install_errors, exe_ctx))
561                     {
562                         if (install_errors.GetString().empty())
563                             err.SetErrorString ("couldn't install checkers, unknown error");
564                         else
565                             err.SetErrorString (install_errors.GetString().c_str());
566
567                         return err;
568                     }
569
570                     process->SetDynamicCheckers(dynamic_checkers);
571
572                     if (log)
573                         log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
574                 }
575
576                 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
577
578                 if (!ir_dynamic_checks.runOnModule(*execution_unit_sp->GetModule()))
579                 {
580                     err.SetErrorToGenericError();
581                     err.SetErrorString("Couldn't add dynamic checks to the expression");
582                     return err;
583                 }
584             }
585
586             execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
587         }
588     }
589     else
590     {
591         execution_unit_sp->GetRunnableInfo(err, func_addr, func_end);
592     }
593
594     return err;
595 }
596
597 bool
598 ClangExpressionParser::GetGenerateDebugInfo () const
599 {
600     if (m_compiler)
601         return m_compiler->getCodeGenOpts().getDebugInfo() == CodeGenOptions::FullDebugInfo;
602     return false;
603 }