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