]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/lldb/source/Expression/ClangExpressionParser.cpp
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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/Stream.h"
19 #include "lldb/Core/StreamString.h"
20 #include "lldb/Expression/ClangASTSource.h"
21 #include "lldb/Expression/ClangExpression.h"
22 #include "lldb/Expression/ClangExpressionDeclMap.h"
23 #include "lldb/Expression/IRExecutionUnit.h"
24 #include "lldb/Expression/IRDynamicChecks.h"
25 #include "lldb/Expression/IRInterpreter.h"
26 #include "lldb/Target/ExecutionContext.h"
27 #include "lldb/Target/ObjCLanguageRuntime.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
30
31 #include "clang/AST/ASTContext.h"
32 #include "clang/AST/ExternalASTSource.h"
33 #include "clang/Basic/FileManager.h"
34 #include "clang/Basic/TargetInfo.h"
35 #include "clang/Basic/Version.h"
36 #include "clang/CodeGen/CodeGenAction.h"
37 #include "clang/CodeGen/ModuleBuilder.h"
38 #include "clang/Driver/CC1Options.h"
39 #include "clang/Frontend/CompilerInstance.h"
40 #include "clang/Frontend/CompilerInvocation.h"
41 #include "clang/Frontend/FrontendActions.h"
42 #include "clang/Frontend/FrontendDiagnostic.h"
43 #include "clang/Frontend/FrontendPluginRegistry.h"
44 #include "clang/Frontend/TextDiagnosticBuffer.h"
45 #include "clang/Frontend/TextDiagnosticPrinter.h"
46 #include "clang/Lex/Preprocessor.h"
47 #include "clang/Parse/ParseAST.h"
48 #include "clang/Rewrite/Frontend/FrontendActions.h"
49 #include "clang/Sema/SemaConsumer.h"
50 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
51
52 #include "llvm/ADT/StringRef.h"
53 #include "llvm/ExecutionEngine/ExecutionEngine.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/PathV1.h"
56 #include "llvm/Support/TargetSelect.h"
57
58 #if defined(__FreeBSD__)
59 #define USE_STANDARD_JIT
60 #endif
61
62 #if defined (USE_STANDARD_JIT)
63 #include "llvm/ExecutionEngine/JIT.h"
64 #else
65 #include "llvm/ExecutionEngine/MCJIT.h"
66 #endif
67 #include "llvm/IR/LLVMContext.h"
68 #include "llvm/IR/Module.h"
69 #include "llvm/Support/ErrorHandling.h"
70 #include "llvm/Support/MemoryBuffer.h"
71 #include "llvm/Support/DynamicLibrary.h"
72 #include "llvm/Support/Host.h"
73 #include "llvm/Support/Signals.h"
74
75 using namespace clang;
76 using namespace llvm;
77 using namespace lldb_private;
78
79 //===----------------------------------------------------------------------===//
80 // Utility Methods for Clang
81 //===----------------------------------------------------------------------===//
82
83 std::string GetBuiltinIncludePath(const char *Argv0) {
84     llvm::sys::Path P =
85     llvm::sys::Path::GetMainExecutable(Argv0,
86                                        (void*)(intptr_t) GetBuiltinIncludePath);
87     
88     if (!P.isEmpty()) {
89         P.eraseComponent();  // Remove /clang from foo/bin/clang
90         P.eraseComponent();  // Remove /bin   from foo/bin
91         
92         // Get foo/lib/clang/<version>/include
93         P.appendComponent("lib");
94         P.appendComponent("clang");
95         P.appendComponent(CLANG_VERSION_STRING);
96         P.appendComponent("include");
97     }
98     
99     return P.str();
100 }
101
102
103 //===----------------------------------------------------------------------===//
104 // Main driver for Clang
105 //===----------------------------------------------------------------------===//
106
107 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
108     DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
109     
110     Diags.Report(diag::err_fe_error_backend) << Message;
111     
112     // We cannot recover from llvm errors.
113     assert(0);
114 }
115
116 static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
117     using namespace clang::frontend;
118     
119     switch (CI.getFrontendOpts().ProgramAction) {
120         default:
121             llvm_unreachable("Invalid program action!");
122             
123         case ASTDump:                return new ASTDumpAction();
124         case ASTPrint:               return new ASTPrintAction();
125         case ASTDumpXML:             return new ASTDumpXMLAction();
126         case ASTView:                return new ASTViewAction();
127         case DumpRawTokens:          return new DumpRawTokensAction();
128         case DumpTokens:             return new DumpTokensAction();
129         case EmitAssembly:           return new EmitAssemblyAction();
130         case EmitBC:                 return new EmitBCAction();
131         case EmitHTML:               return new HTMLPrintAction();
132         case EmitLLVM:               return new EmitLLVMAction();
133         case EmitLLVMOnly:           return new EmitLLVMOnlyAction();
134         case EmitCodeGenOnly:        return new EmitCodeGenOnlyAction();
135         case EmitObj:                return new EmitObjAction();
136         case FixIt:                  return new FixItAction();
137         case GeneratePCH:            return new GeneratePCHAction();
138         case GeneratePTH:            return new GeneratePTHAction();
139         case InitOnly:               return new InitOnlyAction();
140         case ParseSyntaxOnly:        return new SyntaxOnlyAction();
141             
142         case PluginAction: {
143             for (FrontendPluginRegistry::iterator it =
144                  FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
145                  it != ie; ++it) {
146                 if (it->getName() == CI.getFrontendOpts().ActionName) {
147                     llvm::OwningPtr<PluginASTAction> P(it->instantiate());
148                     if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
149                         return 0;
150                     return P.take();
151                 }
152             }
153             
154             CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
155             << CI.getFrontendOpts().ActionName;
156             return 0;
157         }
158             
159         case PrintDeclContext:       return new DeclContextPrintAction();
160         case PrintPreamble:          return new PrintPreambleAction();
161         case PrintPreprocessedInput: return new PrintPreprocessedAction();
162         case RewriteMacros:          return new RewriteMacrosAction();
163         case RewriteObjC:            return new RewriteObjCAction();
164         case RewriteTest:            return new RewriteTestAction();
165         //case RunAnalysis:            return new AnalysisAction();
166         case RunPreprocessorOnly:    return new PreprocessOnlyAction();
167     }
168 }
169
170 static FrontendAction *CreateFrontendAction(CompilerInstance &CI) {
171     // Create the underlying action.
172     FrontendAction *Act = CreateFrontendBaseAction(CI);
173     if (!Act)
174         return 0;
175     
176     // If there are any AST files to merge, create a frontend action
177     // adaptor to perform the merge.
178     if (!CI.getFrontendOpts().ASTMergeFiles.empty())
179         Act = new ASTMergeAction(Act, CI.getFrontendOpts().ASTMergeFiles);
180     
181     return Act;
182 }
183
184 //===----------------------------------------------------------------------===//
185 // Implementation of ClangExpressionParser
186 //===----------------------------------------------------------------------===//
187
188 ClangExpressionParser::ClangExpressionParser (ExecutionContextScope *exe_scope,
189                                               ClangExpression &expr) :
190     m_expr (expr),
191     m_compiler (),
192     m_code_generator ()
193 {
194     // Initialize targets first, so that --version shows registered targets.
195     static struct InitializeLLVM {
196         InitializeLLVM() {
197             llvm::InitializeAllTargets();
198             llvm::InitializeAllAsmPrinters();
199             llvm::InitializeAllTargetMCs();
200             llvm::InitializeAllDisassemblers();
201             
202             llvm::DisablePrettyStackTrace = true;
203         }
204     } InitializeLLVM;
205     
206     // 1. Create a new compiler instance.
207     m_compiler.reset(new CompilerInstance());    
208     
209     // 2. Install the target.
210
211     lldb::TargetSP target_sp;
212     if (exe_scope)
213         target_sp = exe_scope->CalculateTarget();
214     
215     // TODO: figure out what to really do when we don't have a valid target.
216     // Sometimes this will be ok to just use the host target triple (when we
217     // evaluate say "2+3", but other expressions like breakpoint conditions
218     // and other things that _are_ target specific really shouldn't just be
219     // using the host triple. This needs to be fixed in a better way.
220     if (target_sp && target_sp->GetArchitecture().IsValid())
221     {
222         std::string triple = target_sp->GetArchitecture().GetTriple().str();
223         
224         int dash_count = 0;
225         for (size_t i = 0; i < triple.size(); ++i)
226         {
227             if (triple[i] == '-')
228                 dash_count++;
229             if (dash_count == 3)
230             {
231                 triple.resize(i);
232                 break;
233             }
234         }
235         
236         m_compiler->getTargetOpts().Triple = triple;
237     }
238     else
239     {
240         m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple();
241     }
242     
243     if (target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86 ||
244         target_sp->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
245     {
246         m_compiler->getTargetOpts().Features.push_back("+sse");
247         m_compiler->getTargetOpts().Features.push_back("+sse2");
248     }
249     
250     if (m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos)
251         m_compiler->getTargetOpts().ABI = "apcs-gnu";
252     
253     m_compiler->createDiagnostics();
254     
255     // Create the target instance.
256     m_compiler->setTarget(TargetInfo::CreateTargetInfo(m_compiler->getDiagnostics(),
257                                                        &m_compiler->getTargetOpts()));
258     
259     assert (m_compiler->hasTarget());
260     
261     // 3. Set options.
262     
263     lldb::LanguageType language = expr.Language();
264     
265     switch (language)
266     {
267     case lldb::eLanguageTypeC:
268         break;
269     case lldb::eLanguageTypeObjC:
270         m_compiler->getLangOpts().ObjC1 = true;
271         m_compiler->getLangOpts().ObjC2 = true;
272         break;
273     case lldb::eLanguageTypeC_plus_plus:
274         m_compiler->getLangOpts().CPlusPlus = true;
275         m_compiler->getLangOpts().CPlusPlus11 = true;
276         break;
277     case lldb::eLanguageTypeObjC_plus_plus:
278     default:
279         m_compiler->getLangOpts().ObjC1 = true;
280         m_compiler->getLangOpts().ObjC2 = true;
281         m_compiler->getLangOpts().CPlusPlus = true;
282         m_compiler->getLangOpts().CPlusPlus11 = true;
283         break;
284     }
285     
286     m_compiler->getLangOpts().Bool = true;
287     m_compiler->getLangOpts().WChar = true;
288     m_compiler->getLangOpts().Blocks = true;
289     m_compiler->getLangOpts().DebuggerSupport = true; // Features specifically for debugger clients
290     if (expr.DesiredResultType() == ClangExpression::eResultTypeId)
291         m_compiler->getLangOpts().DebuggerCastResultToId = true;
292     
293     // Spell checking is a nice feature, but it ends up completing a
294     // lot of types that we didn't strictly speaking need to complete.
295     // As a result, we spend a long time parsing and importing debug
296     // information.
297     m_compiler->getLangOpts().SpellChecking = false; 
298     
299     lldb::ProcessSP process_sp;
300     if (exe_scope)
301         process_sp = exe_scope->CalculateProcess();
302
303     if (process_sp && m_compiler->getLangOpts().ObjC1)
304     {
305         if (process_sp->GetObjCLanguageRuntime())
306         {
307             if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == eAppleObjC_V2)
308                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7));
309             else
310                 m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, VersionTuple(10, 7));
311             
312             if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing())
313                 m_compiler->getLangOpts().DebuggerObjCLiteral = true;
314         }
315     }
316
317     m_compiler->getLangOpts().ThreadsafeStatics = false;
318     m_compiler->getLangOpts().AccessControl = false; // Debuggers get universal access
319     m_compiler->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name
320     
321     // Set CodeGen options
322     m_compiler->getCodeGenOpts().EmitDeclMetadata = true;
323     m_compiler->getCodeGenOpts().InstrumentFunctions = false;
324     
325     // Disable some warnings.
326     m_compiler->getDiagnostics().setDiagnosticGroupMapping("unused-value", clang::diag::MAP_IGNORE, SourceLocation());
327     m_compiler->getDiagnostics().setDiagnosticGroupMapping("odr", clang::diag::MAP_IGNORE, SourceLocation());
328     
329     // Inform the target of the language options
330     //
331     // FIXME: We shouldn't need to do this, the target should be immutable once
332     // created. This complexity should be lifted elsewhere.
333     m_compiler->getTarget().setForcedLangOptions(m_compiler->getLangOpts());
334     
335     // 4. Set up the diagnostic buffer for reporting errors
336     
337     m_compiler->getDiagnostics().setClient(new clang::TextDiagnosticBuffer);
338     
339     // 5. Set up the source management objects inside the compiler
340     
341     clang::FileSystemOptions file_system_options;
342     m_file_manager.reset(new clang::FileManager(file_system_options));
343     
344     if (!m_compiler->hasSourceManager())
345         m_compiler->createSourceManager(*m_file_manager.get());
346     
347     m_compiler->createFileManager();
348     m_compiler->createPreprocessor();
349     
350     // 6. Most of this we get from the CompilerInstance, but we 
351     // also want to give the context an ExternalASTSource.
352     m_selector_table.reset(new SelectorTable());
353     m_builtin_context.reset(new Builtin::Context());
354     
355     std::unique_ptr<clang::ASTContext> ast_context(new ASTContext(m_compiler->getLangOpts(),
356                                                                  m_compiler->getSourceManager(),
357                                                                  &m_compiler->getTarget(),
358                                                                  m_compiler->getPreprocessor().getIdentifierTable(),
359                                                                  *m_selector_table.get(),
360                                                                  *m_builtin_context.get(),
361                                                                  0));
362     
363     ClangExpressionDeclMap *decl_map = m_expr.DeclMap();
364     
365     if (decl_map)
366     {
367         llvm::OwningPtr<clang::ExternalASTSource> ast_source(decl_map->CreateProxy());
368         decl_map->InstallASTContext(ast_context.get());
369         ast_context->setExternalSource(ast_source);
370     }
371     
372     m_compiler->setASTContext(ast_context.release());
373     
374     std::string module_name("$__lldb_module");
375
376     m_llvm_context.reset(new LLVMContext());
377     m_code_generator.reset(CreateLLVMCodeGen(m_compiler->getDiagnostics(),
378                                              module_name,
379                                              m_compiler->getCodeGenOpts(),
380                                              m_compiler->getTargetOpts(),
381                                              *m_llvm_context));
382 }
383
384 ClangExpressionParser::~ClangExpressionParser()
385 {
386 }
387
388 unsigned
389 ClangExpressionParser::Parse (Stream &stream)
390 {
391     TextDiagnosticBuffer *diag_buf = static_cast<TextDiagnosticBuffer*>(m_compiler->getDiagnostics().getClient());
392         
393     diag_buf->FlushDiagnostics (m_compiler->getDiagnostics());
394     
395     MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(m_expr.Text(), __FUNCTION__);
396     m_compiler->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
397     
398     diag_buf->BeginSourceFile(m_compiler->getLangOpts(), &m_compiler->getPreprocessor());
399     
400     ASTConsumer *ast_transformer = m_expr.ASTTransformer(m_code_generator.get());
401     
402     if (ast_transformer)
403         ParseAST(m_compiler->getPreprocessor(), ast_transformer, m_compiler->getASTContext());
404     else 
405         ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), m_compiler->getASTContext());    
406     
407     diag_buf->EndSourceFile();
408         
409     TextDiagnosticBuffer::const_iterator diag_iterator;
410     
411     int num_errors = 0;
412     
413     for (diag_iterator = diag_buf->warn_begin();
414          diag_iterator != diag_buf->warn_end();
415          ++diag_iterator)
416         stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
417     
418     num_errors = 0;
419     
420     for (diag_iterator = diag_buf->err_begin();
421          diag_iterator != diag_buf->err_end();
422          ++diag_iterator)
423     {
424         num_errors++;
425         stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
426     }
427     
428     for (diag_iterator = diag_buf->note_begin();
429          diag_iterator != diag_buf->note_end();
430          ++diag_iterator)
431         stream.Printf("note: %s\n", (*diag_iterator).second.c_str());
432     
433     if (!num_errors)
434     {
435         if (m_expr.DeclMap() && !m_expr.DeclMap()->ResolveUnknownTypes())
436         {
437             stream.Printf("error: Couldn't infer the type of a variable\n");
438             num_errors++;
439         }
440     }
441     
442     return num_errors;
443 }
444
445 static bool FindFunctionInModule (ConstString &mangled_name,
446                                   llvm::Module *module,
447                                   const char *orig_name)
448 {
449     for (llvm::Module::iterator fi = module->getFunctionList().begin(), fe = module->getFunctionList().end();
450          fi != fe;
451          ++fi)
452     {        
453         if (fi->getName().str().find(orig_name) != std::string::npos)
454         {
455             mangled_name.SetCString(fi->getName().str().c_str());
456             return true;
457         }
458     }
459     
460     return false;
461 }
462
463 Error
464 ClangExpressionParser::PrepareForExecution (lldb::addr_t &func_addr, 
465                                             lldb::addr_t &func_end,
466                                             std::unique_ptr<IRExecutionUnit> &execution_unit_ap,
467                                             ExecutionContext &exe_ctx,
468                                             bool &can_interpret,
469                                             ExecutionPolicy execution_policy)
470 {
471         func_addr = LLDB_INVALID_ADDRESS;
472         func_end = LLDB_INVALID_ADDRESS;
473     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
474
475     std::unique_ptr<llvm::ExecutionEngine> execution_engine_ap;
476     
477     Error err;
478     
479     std::unique_ptr<llvm::Module> module_ap (m_code_generator->ReleaseModule());
480
481     if (!module_ap.get())
482     {
483         err.SetErrorToGenericError();
484         err.SetErrorString("IR doesn't contain a module");
485         return err;
486     }
487     
488     // Find the actual name of the function (it's often mangled somehow)
489     
490     ConstString function_name;
491     
492     if (!FindFunctionInModule(function_name, module_ap.get(), m_expr.FunctionName()))
493     {
494         err.SetErrorToGenericError();
495         err.SetErrorStringWithFormat("Couldn't find %s() in the module", m_expr.FunctionName());
496         return err;
497     }
498     else
499     {
500         if (log)
501             log->Printf("Found function %s for %s", function_name.AsCString(), m_expr.FunctionName());
502     }
503     
504     m_execution_unit.reset(new IRExecutionUnit(m_llvm_context, // handed off here
505                                                module_ap, // handed off here
506                                                function_name,
507                                                exe_ctx.GetTargetSP(),
508                                                m_compiler->getTargetOpts().Features));
509         
510     ClangExpressionDeclMap *decl_map = m_expr.DeclMap(); // result can be NULL
511     
512     if (decl_map)
513     {
514         Stream *error_stream = NULL;
515         Target *target = exe_ctx.GetTargetPtr();
516         if (target)
517             error_stream = &target->GetDebugger().GetErrorStream();
518     
519         IRForTarget ir_for_target(decl_map,
520                                   m_expr.NeedsVariableResolution(),
521                                   *m_execution_unit,
522                                   error_stream,
523                                   function_name.AsCString());
524         
525         bool ir_can_run = ir_for_target.runOnModule(*m_execution_unit->GetModule());
526         
527         Error interpret_error;
528         
529         can_interpret = IRInterpreter::CanInterpret(*m_execution_unit->GetModule(), *m_execution_unit->GetFunction(), interpret_error);
530         
531         Process *process = exe_ctx.GetProcessPtr();
532         
533         if (!ir_can_run)
534         {
535             err.SetErrorString("The expression could not be prepared to run in the target");
536             return err;
537         }
538         
539         if (!can_interpret && execution_policy == eExecutionPolicyNever)
540         {
541             err.SetErrorStringWithFormat("Can't run the expression locally: %s", interpret_error.AsCString());
542             return err;
543         }
544         
545         if (!process && execution_policy == eExecutionPolicyAlways)
546         {
547             err.SetErrorString("Expression needed to run in the target, but the target can't be run");
548             return err;
549         }
550         
551         if (execution_policy == eExecutionPolicyAlways || !can_interpret)
552         {
553             if (m_expr.NeedsValidation() && process)
554             {
555                 if (!process->GetDynamicCheckers())
556                 {
557                     DynamicCheckerFunctions *dynamic_checkers = new DynamicCheckerFunctions();
558                     
559                     StreamString install_errors;
560                     
561                     if (!dynamic_checkers->Install(install_errors, exe_ctx))
562                     {
563                         if (install_errors.GetString().empty())
564                             err.SetErrorString ("couldn't install checkers, unknown error");
565                         else
566                             err.SetErrorString (install_errors.GetString().c_str());
567                         
568                         return err;
569                     }
570                     
571                     process->SetDynamicCheckers(dynamic_checkers);
572                     
573                     if (log)
574                         log->Printf("== [ClangUserExpression::Evaluate] Finished installing dynamic checkers ==");
575                 }
576                 
577                 IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), function_name.AsCString());
578                 
579                 if (!ir_dynamic_checks.runOnModule(*m_execution_unit->GetModule()))
580                 {
581                     err.SetErrorToGenericError();
582                     err.SetErrorString("Couldn't add dynamic checks to the expression");
583                     return err;
584                 }
585             }
586             
587             m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
588         }
589     }
590     else
591     {
592         m_execution_unit->GetRunnableInfo(err, func_addr, func_end);
593     }
594     
595     execution_unit_ap.reset (m_execution_unit.release());
596         
597     return err;
598 }