blob: fc8cebe83853359543b276835e21056a778199af [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- ClangExpression.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// C Includes
11#include <stdio.h>
12#if HAVE_SYS_TYPES_H
13# include <sys/types.h>
14#endif
15
16// C++ Includes
17#include <cstdlib>
18#include <string>
19#include <map>
20
21// Other libraries and framework includes
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/ExternalASTSource.h"
24#include "clang/Basic/FileManager.h"
25#include "clang/Basic/TargetInfo.h"
26#include "clang/Basic/Version.h"
27#include "clang/CodeGen/ModuleBuilder.h"
28#include "clang/Driver/CC1Options.h"
29#include "clang/Driver/OptTable.h"
30#include "clang/Frontend/CodeGenAction.h"
31#include "clang/Frontend/CompilerInstance.h"
32#include "clang/Frontend/CompilerInvocation.h"
33#include "clang/Frontend/FrontendActions.h"
34#include "clang/Frontend/FrontendDiagnostic.h"
35#include "clang/Frontend/FrontendPluginRegistry.h"
36#include "clang/Frontend/TextDiagnosticBuffer.h"
37#include "clang/Frontend/TextDiagnosticPrinter.h"
38#include "clang/Frontend/VerifyDiagnosticsClient.h"
39#include "clang/Lex/Preprocessor.h"
40#include "clang/Sema/ParseAST.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000041#include "clang/Sema/SemaConsumer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000042#include "llvm/ExecutionEngine/ExecutionEngine.h"
43#include "llvm/ExecutionEngine/JIT.h"
44#include "llvm/Module.h"
45#include "llvm/ADT/StringRef.h"
46#include "llvm/LLVMContext.h"
47#include "llvm/Support/MemoryBuffer.h"
48#include "llvm/System/DynamicLibrary.h"
49#include "llvm/System/Host.h"
50#include "llvm/System/Signals.h"
51#include "llvm/Target/TargetSelect.h"
52
53// Project includes
Sean Callanan848960c2010-06-23 23:18:04 +000054#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000055#include "lldb/Expression/ClangExpression.h"
56#include "lldb/Expression/ClangASTSource.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000057#include "lldb/Expression/ClangResultSynthesizer.h"
Chris Lattner24943d22010-06-08 16:52:24 +000058#include "lldb/Expression/ClangStmtVisitor.h"
59#include "lldb/Symbol/ClangASTContext.h"
60#include "lldb/Expression/RecordingMemoryManager.h"
61#include "lldb/Target/ExecutionContext.h"
62#include "lldb/Target/Process.h"
63
Chris Lattner24943d22010-06-08 16:52:24 +000064#include "lldb/Core/StreamString.h"
65#include "lldb/Host/Mutex.h"
66#include "lldb/Core/dwarf.h"
67
68
69using namespace lldb_private;
70using namespace clang;
71using namespace llvm;
72
73namespace clang {
74
75class AnalyzerOptions;
76class CodeGenOptions;
77class DependencyOutputOptions;
78class DiagnosticOptions;
79class FrontendOptions;
80class HeaderSearchOptions;
81class LangOptions;
82class PreprocessorOptions;
83class PreprocessorOutputOptions;
84class TargetInfo;
85class TargetOptions;
86
87} // end namespace clang
88
89
90
91//===----------------------------------------------------------------------===//
92// Utility Methods
93//===----------------------------------------------------------------------===//
94
95std::string GetBuiltinIncludePath(const char *Argv0) {
96 llvm::sys::Path P =
97 llvm::sys::Path::GetMainExecutable(Argv0,
98 (void*)(intptr_t) GetBuiltinIncludePath);
99
100 if (!P.isEmpty()) {
101 P.eraseComponent(); // Remove /clang from foo/bin/clang
102 P.eraseComponent(); // Remove /bin from foo/bin
103
104 // Get foo/lib/clang/<version>/include
105 P.appendComponent("lib");
106 P.appendComponent("clang");
107 P.appendComponent(CLANG_VERSION_STRING);
108 P.appendComponent("include");
109 }
110
111 return P.str();
112}
113
114
115//===----------------------------------------------------------------------===//
116// Main driver
117//===----------------------------------------------------------------------===//
118
119void LLVMErrorHandler(void *UserData, const std::string &Message) {
120 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
121
122 Diags.Report(diag::err_fe_error_backend) << Message;
123
124 // We cannot recover from llvm errors.
125 exit(1);
126}
127
128static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) {
129 using namespace clang::frontend;
130
131 switch (CI.getFrontendOpts().ProgramAction) {
132 default:
133 llvm_unreachable("Invalid program action!");
134
135 case ASTDump: return new ASTDumpAction();
136 case ASTPrint: return new ASTPrintAction();
137 case ASTPrintXML: return new ASTPrintXMLAction();
138 case ASTView: return new ASTViewAction();
139 case DumpRawTokens: return new DumpRawTokensAction();
140 case DumpTokens: return new DumpTokensAction();
141 case EmitAssembly: return new EmitAssemblyAction();
142 case EmitBC: return new EmitBCAction();
143 case EmitHTML: return new HTMLPrintAction();
144 case EmitLLVM: return new EmitLLVMAction();
145 case EmitLLVMOnly: return new EmitLLVMOnlyAction();
146 case EmitObj: return new EmitObjAction();
147 case FixIt: return new FixItAction();
148 case GeneratePCH: return new GeneratePCHAction();
149 case GeneratePTH: return new GeneratePTHAction();
150 case InheritanceView: return new InheritanceViewAction();
151 case InitOnly: return new InitOnlyAction();
152 case ParseNoop: return new ParseOnlyAction();
153 case ParsePrintCallbacks: return new PrintParseAction();
154 case ParseSyntaxOnly: return new SyntaxOnlyAction();
155
156 case PluginAction: {
157 if (CI.getFrontendOpts().ActionName == "help") {
158 llvm::errs() << "clang -cc1 plugins:\n";
159 for (FrontendPluginRegistry::iterator it =
160 FrontendPluginRegistry::begin(),
161 ie = FrontendPluginRegistry::end();
162 it != ie; ++it)
163 llvm::errs() << " " << it->getName() << " - " << it->getDesc() << "\n";
164 return 0;
165 }
166
167 for (FrontendPluginRegistry::iterator it =
168 FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
169 it != ie; ++it) {
170 if (it->getName() == CI.getFrontendOpts().ActionName)
171 return it->instantiate();
172 }
173
174 CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name)
175 << CI.getFrontendOpts().ActionName;
176 return 0;
177 }
178
179 case PrintDeclContext: return new DeclContextPrintAction();
180 case PrintPreprocessedInput: return new PrintPreprocessedAction();
181 case RewriteMacros: return new RewriteMacrosAction();
182 case RewriteObjC: return new RewriteObjCAction();
183 case RewriteTest: return new RewriteTestAction();
184 case RunAnalysis: return new AnalysisAction();
185 case RunPreprocessorOnly: return new PreprocessOnlyAction();
186 }
187}
188
189//----------------------------------------------------------------------
190// ClangExpression constructor
191//----------------------------------------------------------------------
192ClangExpression::ClangExpression(const char *target_triple,
193 ClangExpressionDeclMap *decl_map) :
194 m_target_triple (),
195 m_jit_mm_ptr (NULL),
196 m_code_generator_ptr (NULL),
197 m_decl_map (decl_map)
198{
199 if (target_triple && target_triple[0])
200 m_target_triple = target_triple;
201 else
202 m_target_triple = llvm::sys::getHostTriple();
203}
204
205
206//----------------------------------------------------------------------
207// Destructor
208//----------------------------------------------------------------------
209ClangExpression::~ClangExpression()
210{
211 if (m_code_generator_ptr && !m_execution_engine.get())
212 delete m_code_generator_ptr;
213}
214
215bool
216ClangExpression::CreateCompilerInstance (bool &IsAST)
217{
218 // Initialize targets first, so that --version shows registered targets.
219 static struct InitializeLLVM {
220 InitializeLLVM() {
221 llvm::InitializeAllTargets();
222 llvm::InitializeAllAsmPrinters();
223 }
224 } InitializeLLVM;
225
226 // 1. Create a new compiler instance.
227 m_clang_ap.reset(new CompilerInstance());
228 m_clang_ap->setLLVMContext(new LLVMContext());
229
230 // 2. Set options.
231
232 // Parse expressions as Objective C++ regardless of context.
233 // Our hook into Clang's lookup mechanism only works in C++.
234 m_clang_ap->getLangOpts().CPlusPlus = true;
235 m_clang_ap->getLangOpts().ObjC1 = true;
236
237 // Disable some warnings.
238 m_clang_ap->getDiagnosticOpts().Warnings.push_back("no-unused-value");
239
240 // Set the target triple.
241 m_clang_ap->getTargetOpts().Triple = m_target_triple;
242
243 // 3. Set up various important bits of infrastructure.
244
245 m_clang_ap->createDiagnostics(0, 0);
246 m_clang_ap->getLangOpts().CPlusPlus = true;
247
248 // Create the target instance.
249 m_clang_ap->setTarget(TargetInfo::CreateTargetInfo(m_clang_ap->getDiagnostics(),
250 m_clang_ap->getTargetOpts()));
251 if (!m_clang_ap->hasTarget())
252 {
253 m_clang_ap.reset();
254 return false;
255 }
256
257 // Inform the target of the language options
258 //
259 // FIXME: We shouldn't need to do this, the target should be immutable once
260 // created. This complexity should be lifted elsewhere.
261 m_clang_ap->getTarget().setForcedLangOptions(m_clang_ap->getLangOpts());
262
263 return m_clang_ap.get();
264}
265
266Mutex &
267ClangExpression::GetClangMutex ()
268{
269 static Mutex g_clang_mutex(Mutex::eMutexTypeRecursive); // Control access to the clang compiler
270 return g_clang_mutex;
271}
272
273
274clang::ASTContext *
275ClangExpression::GetASTContext ()
276{
277 CompilerInstance *compiler_instance = GetCompilerInstance();
278 if (compiler_instance)
279 return &compiler_instance->getASTContext();
280 return NULL;
281}
282
283unsigned
Sean Callanan8c6934d2010-07-01 20:08:22 +0000284ClangExpression::ParseExpression (const char *expr_text,
285 Stream &stream,
286 bool add_result_var)
Chris Lattner24943d22010-06-08 16:52:24 +0000287{
288 // HACK: for now we have to make a function body around our expression
289 // since there is no way to parse a single expression line in LLVM/Clang.
Sean Callanan8c6934d2010-07-01 20:08:22 +0000290 std::string func_expr("extern \"C\" void ___clang_expr()\n{\n\t");
Chris Lattner24943d22010-06-08 16:52:24 +0000291 func_expr.append(expr_text);
292 func_expr.append(";\n}");
Sean Callanan8c6934d2010-07-01 20:08:22 +0000293 return ParseBareExpression (func_expr, stream, add_result_var);
Chris Lattner24943d22010-06-08 16:52:24 +0000294
295}
296
297unsigned
Sean Callanan8c6934d2010-07-01 20:08:22 +0000298ClangExpression::ParseBareExpression (llvm::StringRef expr_text,
299 Stream &stream,
300 bool add_result_var)
Chris Lattner24943d22010-06-08 16:52:24 +0000301{
302 Mutex::Locker locker(GetClangMutex ());
303
304 TextDiagnosticBuffer text_diagnostic_buffer;
305
306 bool IsAST = false;
307 if (!CreateCompilerInstance (IsAST))
308 {
309 stream.Printf("error: couldn't create compiler instance\n");
310 return 1;
311 }
312
313 // This code is matched below by a setClient to NULL.
314 // We cannot return out of this code without doing that.
315 m_clang_ap->getDiagnostics().setClient(&text_diagnostic_buffer);
316 text_diagnostic_buffer.FlushDiagnostics (m_clang_ap->getDiagnostics());
317
318 MemoryBuffer *memory_buffer = MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__);
319
320 if (!m_clang_ap->hasSourceManager())
321 m_clang_ap->createSourceManager();
322
323 m_clang_ap->createFileManager();
324 m_clang_ap->createPreprocessor();
325
326 // Build the ASTContext. Most of this we inherit from the
327 // CompilerInstance, but we also want to give the context
328 // an ExternalASTSource.
329 SelectorTable selector_table;
330 std::auto_ptr<Builtin::Context> builtin_ap(new Builtin::Context(m_clang_ap->getTarget()));
331 ASTContext *Context = new ASTContext(m_clang_ap->getLangOpts(),
332 m_clang_ap->getSourceManager(),
333 m_clang_ap->getTarget(),
334 m_clang_ap->getPreprocessor().getIdentifierTable(),
335 selector_table,
336 *builtin_ap.get());
337
338 llvm::OwningPtr<ExternalASTSource> ASTSource(new ClangASTSource(*Context, *m_decl_map));
339
340 if (m_decl_map)
341 {
342 Context->setExternalSource(ASTSource);
343 }
344
345 m_clang_ap->setASTContext(Context);
346
347 FileID memory_buffer_file_id = m_clang_ap->getSourceManager().createMainFileIDForMemBuffer (memory_buffer);
348 std::string module_name("test_func");
349 text_diagnostic_buffer.BeginSourceFile(m_clang_ap->getLangOpts(), &m_clang_ap->getPreprocessor());
350
351 if (m_code_generator_ptr)
352 delete m_code_generator_ptr;
353
354 m_code_generator_ptr = CreateLLVMCodeGen(m_clang_ap->getDiagnostics(),
355 module_name,
356 m_clang_ap->getCodeGenOpts(),
357 m_clang_ap->getLLVMContext());
358
359
360 // - CodeGeneration ASTConsumer (include/clang/ModuleBuilder.h), which will be passed in when you call...
361 // - Call clang::ParseAST (in lib/Sema/ParseAST.cpp) to parse the buffer. The CodeGenerator will generate code for __dbg_expr.
362 // - Once ParseAST completes, you can grab the llvm::Module from the CodeGenerator, which will have an llvm::Function you can hand off to the JIT.
Sean Callanan8c6934d2010-07-01 20:08:22 +0000363
364 if (add_result_var)
365 {
366 ClangResultSynthesizer result_synthesizer(m_code_generator_ptr);
367 ParseAST(m_clang_ap->getPreprocessor(), &result_synthesizer, m_clang_ap->getASTContext());
368 }
369 else
370 {
371 ParseAST(m_clang_ap->getPreprocessor(), m_code_generator_ptr, m_clang_ap->getASTContext());
372 }
373
Sean Callanan848960c2010-06-23 23:18:04 +0000374
Chris Lattner24943d22010-06-08 16:52:24 +0000375 text_diagnostic_buffer.EndSourceFile();
376
377 //compiler_instance->getASTContext().getTranslationUnitDecl()->dump();
378
379 //if (compiler_instance->getFrontendOpts().ShowStats) {
380 // compiler_instance->getFileManager().PrintStats();
381 // fprintf(stderr, "\n");
382 //}
383
384 // This code resolves the setClient above.
385 m_clang_ap->getDiagnostics().setClient(0);
386
387 TextDiagnosticBuffer::const_iterator diag_iterator;
388
389 int num_errors = 0;
390
391#ifdef COUNT_WARNINGS_AND_ERRORS
392 int num_warnings = 0;
393
394 for (diag_iterator = text_diagnostic_buffer.warn_begin();
395 diag_iterator != text_diagnostic_buffer.warn_end();
396 ++diag_iterator)
397 num_warnings++;
398
399 for (diag_iterator = text_diagnostic_buffer.err_begin();
400 diag_iterator != text_diagnostic_buffer.err_end();
401 ++diag_iterator)
402 num_errors++;
403
404 if (num_warnings || num_errors)
405 {
406 if (num_warnings)
407 stream.Printf("%u warning%s%s", num_warnings, (num_warnings == 1 ? "" : "s"), (num_errors ? " and " : ""));
408 if (num_errors)
409 stream.Printf("%u error%s", num_errors, (num_errors == 1 ? "" : "s"));
410 stream.Printf("\n");
411 }
412#endif
413
414 for (diag_iterator = text_diagnostic_buffer.warn_begin();
415 diag_iterator != text_diagnostic_buffer.warn_end();
416 ++diag_iterator)
417 stream.Printf("warning: %s\n", (*diag_iterator).second.c_str());
418
419 num_errors = 0;
420
421 for (diag_iterator = text_diagnostic_buffer.err_begin();
422 diag_iterator != text_diagnostic_buffer.err_end();
423 ++diag_iterator)
424 {
425 num_errors++;
426 stream.Printf("error: %s\n", (*diag_iterator).second.c_str());
427 }
428
429 return num_errors;
430}
431
Chris Lattner24943d22010-06-08 16:52:24 +0000432static FrontendAction *
433CreateFrontendAction(CompilerInstance &CI)
434{
435 // Create the underlying action.
436 FrontendAction *Act = CreateFrontendBaseAction(CI);
437 if (!Act)
438 return 0;
439
440 // If there are any AST files to merge, create a frontend action
441 // adaptor to perform the merge.
442 if (!CI.getFrontendOpts().ASTMergeFiles.empty())
443 Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0],
444 CI.getFrontendOpts().ASTMergeFiles.size());
445
446 return Act;
447}
448
449
450unsigned
451ClangExpression::ConvertExpressionToDWARF (ClangExpressionVariableList& expr_local_variable_list,
452 StreamString &dwarf_opcode_strm)
453{
454 CompilerInstance *compiler_instance = GetCompilerInstance();
455
456 DeclarationName hack_func_name(&compiler_instance->getASTContext().Idents.get("___clang_expr"));
457 DeclContext::lookup_result result = compiler_instance->getASTContext().getTranslationUnitDecl()->lookup(hack_func_name);
458
459 if (result.first != result.second)
460 {
461 Decl *decl = *result.first;
462 Stmt *decl_stmt = decl->getBody();
463 if (decl_stmt)
464 {
465 ClangStmtVisitor visitor(compiler_instance->getASTContext(), expr_local_variable_list, m_decl_map, dwarf_opcode_strm);
466
467 visitor.Visit (decl_stmt);
468 }
469 }
470 return 0;
471}
472
Sean Callanan848960c2010-06-23 23:18:04 +0000473unsigned
474ClangExpression::ConvertIRToDWARF (ClangExpressionVariableList &excpr_local_variable_list,
475 StreamString &dwarf_opcode_strm)
476{
477 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
478
479 llvm::Module *module = m_code_generator_ptr->GetModule();
480
481 if (!module)
482 {
483 if (log)
484 log->Printf("IR doesn't contain a module");
485
486 return 1;
487 }
488
Sean Callanan8c6934d2010-07-01 20:08:22 +0000489 llvm::Function* function = module->getFunction(StringRef("___clang_expr"));
490
491 if (!function)
Sean Callanan848960c2010-06-23 23:18:04 +0000492 {
Sean Callanan848960c2010-06-23 23:18:04 +0000493 if (log)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000494 log->Printf("Couldn't find ___clang_expr() in the module");
495
496 return 1;
497 }
498
499 if (log)
500 log->Printf("IR for %s:", function->getName().str().c_str());
501
502 llvm::Function::iterator bbi;
503
504 for (bbi = function->begin();
505 bbi != function->end();
506 ++bbi)
507 {
508 llvm::BasicBlock &bb = *bbi;
Sean Callanan848960c2010-06-23 23:18:04 +0000509
Sean Callanan8c6934d2010-07-01 20:08:22 +0000510 llvm::BasicBlock::iterator ii;
Sean Callanan848960c2010-06-23 23:18:04 +0000511
Sean Callanan8c6934d2010-07-01 20:08:22 +0000512 for (ii = bb.begin();
513 ii != bb.end();
514 ++ii)
Sean Callanan848960c2010-06-23 23:18:04 +0000515 {
Sean Callanan8c6934d2010-07-01 20:08:22 +0000516 llvm::Instruction &inst = *ii;
517
518 std::string s;
519 llvm::raw_string_ostream os(s);
520
521 inst.print(os);
522
523 if (log)
524 log->Printf(" %s", s.c_str());
Sean Callanan848960c2010-06-23 23:18:04 +0000525 }
Sean Callanan5e173d42010-06-24 00:47:05 +0000526 }
Sean Callanan848960c2010-06-23 23:18:04 +0000527
528 return 0;
529}
530
Chris Lattner24943d22010-06-08 16:52:24 +0000531bool
532ClangExpression::JITFunction (const ExecutionContext &exc_context, const char *name)
533{
534
535 llvm::Module *module = m_code_generator_ptr->GetModule();
536
537 if (module)
538 {
539 std::string error;
540
541 if (m_jit_mm_ptr == NULL)
542 m_jit_mm_ptr = new RecordingMemoryManager();
543
544 //llvm::InitializeNativeTarget();
545 if (m_execution_engine.get() == 0)
546 m_execution_engine.reset(llvm::ExecutionEngine::createJIT (module, &error, m_jit_mm_ptr));
547 m_execution_engine->DisableLazyCompilation();
548 llvm::Function *function = module->getFunction (llvm::StringRef (name));
549
550 // We don't actually need the function pointer here, this just forces it to get resolved.
551 void *fun_ptr = m_execution_engine->getPointerToFunction(function);
552 // Note, you probably won't get here on error, since the LLVM JIT tends to just
553 // exit on error at present... So be careful.
554 if (fun_ptr == 0)
555 return false;
556 m_jitted_functions.push_back(ClangExpression::JittedFunction(name, (lldb::addr_t) fun_ptr));
557
558 }
559 return true;
560}
561
562bool
563ClangExpression::WriteJITCode (const ExecutionContext &exc_context)
564{
565 if (m_jit_mm_ptr == NULL)
566 return false;
567
568 if (exc_context.process == NULL)
569 return false;
570
571 // Look over the regions allocated for the function compiled. The JIT
572 // tries to allocate the functions & stubs close together, so we should try to
573 // write them that way too...
574 // For now I only write functions with no stubs, globals, exception tables,
575 // etc. So I only need to write the functions.
576
577 size_t size = 0;
578 std::map<uint8_t *, uint8_t *>::iterator fun_pos, fun_end = m_jit_mm_ptr->m_functions.end();
579 for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++)
580 {
581 size += (*fun_pos).second - (*fun_pos).first;
582 }
583
584 Error error;
585 lldb::addr_t target_addr = exc_context.process->AllocateMemory (size, lldb::ePermissionsReadable|lldb::ePermissionsExecutable, error);
586
587 if (target_addr == LLDB_INVALID_ADDRESS)
588 return false;
589
590 lldb::addr_t cursor = target_addr;
591 for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++)
592 {
593 lldb::addr_t lstart = (lldb::addr_t) (*fun_pos).first;
594 lldb::addr_t lend = (lldb::addr_t) (*fun_pos).second;
595 size_t size = lend - lstart;
596 exc_context.process->WriteMemory(cursor, (void *) lstart, size, error);
597 m_jit_mm_ptr->AddToLocalToRemoteMap (lstart, size, cursor);
598 cursor += size;
599 }
600
601 std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end();
602
603 for (pos = m_jitted_functions.begin(); pos != end; pos++)
604 {
605 (*pos).m_remote_addr = m_jit_mm_ptr->GetRemoteAddressForLocal ((*pos).m_local_addr);
606 }
607 return true;
608}
609
610lldb::addr_t
611ClangExpression::GetFunctionAddress (const char *name)
612{
613 std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end();
614
615 for (pos = m_jitted_functions.begin(); pos < end; pos++)
616 {
617 if (strcmp ((*pos).m_name.c_str(), name) == 0)
618 return (*pos).m_remote_addr;
619 }
620 return LLDB_INVALID_ADDRESS;
621}
622
623unsigned
624ClangExpression::Compile()
625{
626 Mutex::Locker locker(GetClangMutex ());
627 bool IsAST = false;
628
629 if (CreateCompilerInstance(IsAST))
630 {
631 // Validate/process some options
632 if (m_clang_ap->getHeaderSearchOpts().Verbose)
633 llvm::errs() << "clang-cc version " CLANG_VERSION_STRING
634 << " based upon " << PACKAGE_STRING
635 << " hosted on " << llvm::sys::getHostTriple() << "\n";
636
637 // Enforce certain implications.
638 if (!m_clang_ap->getFrontendOpts().ViewClassInheritance.empty())
639 m_clang_ap->getFrontendOpts().ProgramAction = frontend::InheritanceView;
640// if (!compiler_instance->getFrontendOpts().FixItSuffix.empty())
641// compiler_instance->getFrontendOpts().ProgramAction = frontend::FixIt;
642
643 for (unsigned i = 0, e = m_clang_ap->getFrontendOpts().Inputs.size(); i != e; ++i) {
Chris Lattner24943d22010-06-08 16:52:24 +0000644
645 // If we aren't using an AST file, setup the file and source managers and
646 // the preprocessor.
647 if (!IsAST) {
648 if (!i) {
649 // Create a file manager object to provide access to and cache the
650 // filesystem.
651 m_clang_ap->createFileManager();
652
653 // Create the source manager.
654 m_clang_ap->createSourceManager();
655 } else {
656 // Reset the ID tables if we are reusing the SourceManager.
657 m_clang_ap->getSourceManager().clearIDTables();
658 }
659
660 // Create the preprocessor.
661 m_clang_ap->createPreprocessor();
662 }
663
664 llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*m_clang_ap.get()));
665 if (!Act)
666 break;
667
Greg Claytone41c4b22010-06-13 17:34:29 +0000668 if (Act->BeginSourceFile(*m_clang_ap,
669 m_clang_ap->getFrontendOpts().Inputs[i].second,
670 m_clang_ap->getFrontendOpts().Inputs[i].first)) {
Chris Lattner24943d22010-06-08 16:52:24 +0000671 Act->Execute();
672 Act->EndSourceFile();
673 }
674 }
675
676 if (m_clang_ap->getDiagnosticOpts().ShowCarets)
677 {
678 unsigned NumWarnings = m_clang_ap->getDiagnostics().getNumWarnings();
679 unsigned NumErrors = m_clang_ap->getDiagnostics().getNumErrors() -
680 m_clang_ap->getDiagnostics().getNumErrorsSuppressed();
681
682 if (NumWarnings || NumErrors)
683 {
684 if (NumWarnings)
685 fprintf (stderr, "%u warning%s%s", NumWarnings, (NumWarnings == 1 ? "" : "s"), (NumErrors ? " and " : ""));
686 if (NumErrors)
687 fprintf (stderr, "%u error%s", NumErrors, (NumErrors == 1 ? "" : "s"));
688 fprintf (stderr, " generated.\n");
689 }
690 }
691
692 if (m_clang_ap->getFrontendOpts().ShowStats) {
693 m_clang_ap->getFileManager().PrintStats();
694 fprintf(stderr, "\n");
695 }
696
697 // Return the appropriate status when verifying diagnostics.
698 //
699 // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need
700 // this.
701 if (m_clang_ap->getDiagnosticOpts().VerifyDiagnostics)
702 return static_cast<VerifyDiagnosticsClient&>(m_clang_ap->getDiagnosticClient()).HadErrors();
703
704 return m_clang_ap->getDiagnostics().getNumErrors();
705 }
706 return 1;
707}