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" |
Greg Clayton | 1674b12 | 2010-07-21 22:12:05 +0000 | [diff] [blame] | 58 | #include "lldb/Core/ClangForward.h" |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 59 | #include "lldb/Core/DataBufferHeap.h" |
| 60 | #include "lldb/Core/Disassembler.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | #include "lldb/Expression/ClangExpression.h" |
| 62 | #include "lldb/Expression/ClangASTSource.h" |
Sean Callanan | 8c6934d | 2010-07-01 20:08:22 +0000 | [diff] [blame] | 63 | #include "lldb/Expression/ClangResultSynthesizer.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | #include "lldb/Expression/ClangStmtVisitor.h" |
Sean Callanan | 5cf4a1c | 2010-07-03 01:35:46 +0000 | [diff] [blame] | 65 | #include "lldb/Expression/IRForTarget.h" |
Sean Callanan | dcb658b | 2010-07-02 21:09:36 +0000 | [diff] [blame] | 66 | #include "lldb/Expression/IRToDWARF.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | #include "lldb/Symbol/ClangASTContext.h" |
| 68 | #include "lldb/Expression/RecordingMemoryManager.h" |
| 69 | #include "lldb/Target/ExecutionContext.h" |
| 70 | #include "lldb/Target/Process.h" |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 71 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | #include "lldb/Core/StreamString.h" |
| 74 | #include "lldb/Host/Mutex.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | using namespace lldb_private; |
| 78 | using namespace clang; |
| 79 | using namespace llvm; |
| 80 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | |
| 82 | //===----------------------------------------------------------------------===// |
| 83 | // Utility Methods |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | |
| 86 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 87 | llvm::sys::Path P = |
| 88 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 89 | (void*)(intptr_t) GetBuiltinIncludePath); |
| 90 | |
| 91 | if (!P.isEmpty()) { |
| 92 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 93 | P.eraseComponent(); // Remove /bin from foo/bin |
| 94 | |
| 95 | // Get foo/lib/clang/<version>/include |
| 96 | P.appendComponent("lib"); |
| 97 | P.appendComponent("clang"); |
| 98 | P.appendComponent(CLANG_VERSION_STRING); |
| 99 | P.appendComponent("include"); |
| 100 | } |
| 101 | |
| 102 | return P.str(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | //===----------------------------------------------------------------------===// |
| 107 | // Main driver |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 110 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 111 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 113 | Diags.Report(diag::err_fe_error_backend) << Message; |
| 114 | |
| 115 | // We cannot recover from llvm errors. |
| 116 | exit(1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static FrontendAction *CreateFrontendBaseAction(CompilerInstance &CI) { |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 120 | using namespace clang::frontend; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 122 | switch (CI.getFrontendOpts().ProgramAction) { |
| 123 | default: |
| 124 | llvm_unreachable("Invalid program action!"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 126 | case ASTDump: return new ASTDumpAction(); |
| 127 | case ASTPrint: return new ASTPrintAction(); |
| 128 | case ASTPrintXML: return new ASTPrintXMLAction(); |
| 129 | case ASTView: return new ASTViewAction(); |
| 130 | case BoostCon: return new BoostConAction(); |
| 131 | case DumpRawTokens: return new DumpRawTokensAction(); |
| 132 | case DumpTokens: return new DumpTokensAction(); |
| 133 | case EmitAssembly: return new EmitAssemblyAction(); |
| 134 | case EmitBC: return new EmitBCAction(); |
| 135 | case EmitHTML: return new HTMLPrintAction(); |
| 136 | case EmitLLVM: return new EmitLLVMAction(); |
| 137 | case EmitLLVMOnly: return new EmitLLVMOnlyAction(); |
| 138 | case EmitCodeGenOnly: return new EmitCodeGenOnlyAction(); |
| 139 | case EmitObj: return new EmitObjAction(); |
| 140 | case FixIt: return new FixItAction(); |
| 141 | case GeneratePCH: return new GeneratePCHAction(); |
| 142 | case GeneratePTH: return new GeneratePTHAction(); |
| 143 | case InheritanceView: return new InheritanceViewAction(); |
| 144 | case InitOnly: return new InitOnlyAction(); |
| 145 | case ParseSyntaxOnly: return new SyntaxOnlyAction(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 147 | case PluginAction: { |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 148 | for (FrontendPluginRegistry::iterator it = |
| 149 | FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end(); |
| 150 | it != ie; ++it) { |
| 151 | if (it->getName() == CI.getFrontendOpts().ActionName) { |
Greg Clayton | 960d6a4 | 2010-08-03 00:35:52 +0000 | [diff] [blame] | 152 | llvm::OwningPtr<PluginASTAction> P(it->instantiate()); |
| 153 | if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs)) |
| 154 | return 0; |
| 155 | return P.take(); |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | } |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 158 | |
| 159 | CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) |
| 160 | << CI.getFrontendOpts().ActionName; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | case PrintDeclContext: return new DeclContextPrintAction(); |
| 165 | case PrintPreamble: return new PrintPreambleAction(); |
| 166 | case PrintPreprocessedInput: return new PrintPreprocessedAction(); |
| 167 | case RewriteMacros: return new RewriteMacrosAction(); |
| 168 | case RewriteObjC: return new RewriteObjCAction(); |
| 169 | case RewriteTest: return new RewriteTestAction(); |
| 170 | case RunAnalysis: return new AnalysisAction(); |
| 171 | case RunPreprocessorOnly: return new PreprocessOnlyAction(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static FrontendAction *CreateFrontendAction(CompilerInstance &CI) { |
| 176 | // Create the underlying action. |
| 177 | FrontendAction *Act = CreateFrontendBaseAction(CI); |
| 178 | if (!Act) |
| 179 | return 0; |
| 180 | |
| 181 | // If there are any AST files to merge, create a frontend action |
| 182 | // adaptor to perform the merge. |
| 183 | if (!CI.getFrontendOpts().ASTMergeFiles.empty()) |
| 184 | Act = new ASTMergeAction(Act, &CI.getFrontendOpts().ASTMergeFiles[0], |
| 185 | CI.getFrontendOpts().ASTMergeFiles.size()); |
| 186 | |
| 187 | return Act; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | //---------------------------------------------------------------------- |
| 191 | // ClangExpression constructor |
| 192 | //---------------------------------------------------------------------- |
| 193 | ClangExpression::ClangExpression(const char *target_triple, |
| 194 | ClangExpressionDeclMap *decl_map) : |
| 195 | m_target_triple (), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 196 | m_decl_map (decl_map), |
| 197 | m_clang_ap (), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | m_code_generator_ptr (NULL), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 199 | m_jit_mm_ptr (NULL), |
| 200 | m_execution_engine (), |
| 201 | m_jitted_functions () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | { |
| 203 | if (target_triple && target_triple[0]) |
| 204 | m_target_triple = target_triple; |
| 205 | else |
| 206 | m_target_triple = llvm::sys::getHostTriple(); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | //---------------------------------------------------------------------- |
| 211 | // Destructor |
| 212 | //---------------------------------------------------------------------- |
| 213 | ClangExpression::~ClangExpression() |
| 214 | { |
| 215 | if (m_code_generator_ptr && !m_execution_engine.get()) |
| 216 | delete m_code_generator_ptr; |
| 217 | } |
| 218 | |
| 219 | bool |
| 220 | ClangExpression::CreateCompilerInstance (bool &IsAST) |
| 221 | { |
| 222 | // Initialize targets first, so that --version shows registered targets. |
| 223 | static struct InitializeLLVM { |
| 224 | InitializeLLVM() { |
| 225 | llvm::InitializeAllTargets(); |
| 226 | llvm::InitializeAllAsmPrinters(); |
| 227 | } |
| 228 | } InitializeLLVM; |
| 229 | |
| 230 | // 1. Create a new compiler instance. |
| 231 | m_clang_ap.reset(new CompilerInstance()); |
| 232 | m_clang_ap->setLLVMContext(new LLVMContext()); |
| 233 | |
| 234 | // 2. Set options. |
| 235 | |
| 236 | // Parse expressions as Objective C++ regardless of context. |
| 237 | // Our hook into Clang's lookup mechanism only works in C++. |
| 238 | m_clang_ap->getLangOpts().CPlusPlus = true; |
| 239 | m_clang_ap->getLangOpts().ObjC1 = true; |
Sean Callanan | 051052f | 2010-07-02 22:22:28 +0000 | [diff] [blame] | 240 | m_clang_ap->getLangOpts().ThreadsafeStatics = false; |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 241 | m_clang_ap->getLangOpts().AccessControl = false; // Debuggers get universal access |
Sean Callanan | a48fe16 | 2010-08-11 03:57:18 +0000 | [diff] [blame^] | 242 | m_clang_ap->getLangOpts().DollarIdents = true; // $ indicates a persistent variable name |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 243 | |
| 244 | // Set CodeGen options |
| 245 | m_clang_ap->getCodeGenOpts().EmitDeclMetadata = true; |
Sean Callanan | bc2928a | 2010-08-03 00:23:29 +0000 | [diff] [blame] | 246 | m_clang_ap->getCodeGenOpts().InstrumentFunctions = false; |
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, |
Greg Clayton | 6e71340 | 2010-07-30 20:30:44 +0000 | [diff] [blame] | 346 | *builtin_ap.get(), |
| 347 | 0); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | |
| 349 | llvm::OwningPtr<ExternalASTSource> ASTSource(new ClangASTSource(*Context, *m_decl_map)); |
| 350 | |
| 351 | if (m_decl_map) |
| 352 | { |
| 353 | Context->setExternalSource(ASTSource); |
| 354 | } |
| 355 | |
| 356 | m_clang_ap->setASTContext(Context); |
| 357 | |
| 358 | FileID memory_buffer_file_id = m_clang_ap->getSourceManager().createMainFileIDForMemBuffer (memory_buffer); |
| 359 | std::string module_name("test_func"); |
| 360 | text_diagnostic_buffer.BeginSourceFile(m_clang_ap->getLangOpts(), &m_clang_ap->getPreprocessor()); |
| 361 | |
| 362 | if (m_code_generator_ptr) |
| 363 | delete m_code_generator_ptr; |
| 364 | |
| 365 | m_code_generator_ptr = CreateLLVMCodeGen(m_clang_ap->getDiagnostics(), |
| 366 | module_name, |
| 367 | m_clang_ap->getCodeGenOpts(), |
| 368 | m_clang_ap->getLLVMContext()); |
| 369 | |
| 370 | |
| 371 | // - CodeGeneration ASTConsumer (include/clang/ModuleBuilder.h), which will be passed in when you call... |
| 372 | // - Call clang::ParseAST (in lib/Sema/ParseAST.cpp) to parse the buffer. The CodeGenerator will generate code for __dbg_expr. |
| 373 | // - 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] | 374 | |
| 375 | if (add_result_var) |
| 376 | { |
| 377 | ClangResultSynthesizer result_synthesizer(m_code_generator_ptr); |
| 378 | ParseAST(m_clang_ap->getPreprocessor(), &result_synthesizer, m_clang_ap->getASTContext()); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | ParseAST(m_clang_ap->getPreprocessor(), m_code_generator_ptr, m_clang_ap->getASTContext()); |
| 383 | } |
| 384 | |
Sean Callanan | 848960c | 2010-06-23 23:18:04 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | text_diagnostic_buffer.EndSourceFile(); |
| 387 | |
| 388 | //compiler_instance->getASTContext().getTranslationUnitDecl()->dump(); |
| 389 | |
| 390 | //if (compiler_instance->getFrontendOpts().ShowStats) { |
| 391 | // compiler_instance->getFileManager().PrintStats(); |
| 392 | // fprintf(stderr, "\n"); |
| 393 | //} |
| 394 | |
| 395 | // This code resolves the setClient above. |
| 396 | m_clang_ap->getDiagnostics().setClient(0); |
| 397 | |
| 398 | TextDiagnosticBuffer::const_iterator diag_iterator; |
| 399 | |
| 400 | int num_errors = 0; |
| 401 | |
| 402 | #ifdef COUNT_WARNINGS_AND_ERRORS |
| 403 | int num_warnings = 0; |
| 404 | |
| 405 | for (diag_iterator = text_diagnostic_buffer.warn_begin(); |
| 406 | diag_iterator != text_diagnostic_buffer.warn_end(); |
| 407 | ++diag_iterator) |
| 408 | num_warnings++; |
| 409 | |
| 410 | for (diag_iterator = text_diagnostic_buffer.err_begin(); |
| 411 | diag_iterator != text_diagnostic_buffer.err_end(); |
| 412 | ++diag_iterator) |
| 413 | num_errors++; |
| 414 | |
| 415 | if (num_warnings || num_errors) |
| 416 | { |
| 417 | if (num_warnings) |
| 418 | stream.Printf("%u warning%s%s", num_warnings, (num_warnings == 1 ? "" : "s"), (num_errors ? " and " : "")); |
| 419 | if (num_errors) |
| 420 | stream.Printf("%u error%s", num_errors, (num_errors == 1 ? "" : "s")); |
| 421 | stream.Printf("\n"); |
| 422 | } |
| 423 | #endif |
| 424 | |
| 425 | for (diag_iterator = text_diagnostic_buffer.warn_begin(); |
| 426 | diag_iterator != text_diagnostic_buffer.warn_end(); |
| 427 | ++diag_iterator) |
| 428 | stream.Printf("warning: %s\n", (*diag_iterator).second.c_str()); |
| 429 | |
| 430 | num_errors = 0; |
| 431 | |
| 432 | for (diag_iterator = text_diagnostic_buffer.err_begin(); |
| 433 | diag_iterator != text_diagnostic_buffer.err_end(); |
| 434 | ++diag_iterator) |
| 435 | { |
| 436 | num_errors++; |
| 437 | stream.Printf("error: %s\n", (*diag_iterator).second.c_str()); |
| 438 | } |
| 439 | |
| 440 | return num_errors; |
| 441 | } |
| 442 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | unsigned |
| 444 | ClangExpression::ConvertExpressionToDWARF (ClangExpressionVariableList& expr_local_variable_list, |
| 445 | StreamString &dwarf_opcode_strm) |
| 446 | { |
| 447 | CompilerInstance *compiler_instance = GetCompilerInstance(); |
| 448 | |
| 449 | DeclarationName hack_func_name(&compiler_instance->getASTContext().Idents.get("___clang_expr")); |
| 450 | DeclContext::lookup_result result = compiler_instance->getASTContext().getTranslationUnitDecl()->lookup(hack_func_name); |
| 451 | |
| 452 | if (result.first != result.second) |
| 453 | { |
| 454 | Decl *decl = *result.first; |
| 455 | Stmt *decl_stmt = decl->getBody(); |
| 456 | if (decl_stmt) |
| 457 | { |
| 458 | ClangStmtVisitor visitor(compiler_instance->getASTContext(), expr_local_variable_list, m_decl_map, dwarf_opcode_strm); |
| 459 | |
| 460 | visitor.Visit (decl_stmt); |
| 461 | } |
| 462 | } |
| 463 | return 0; |
| 464 | } |
| 465 | |
Sean Callanan | 5cf4a1c | 2010-07-03 01:35:46 +0000 | [diff] [blame] | 466 | bool |
Sean Callanan | dcb658b | 2010-07-02 21:09:36 +0000 | [diff] [blame] | 467 | ClangExpression::ConvertIRToDWARF (ClangExpressionVariableList &expr_local_variable_list, |
Sean Callanan | 848960c | 2010-06-23 23:18:04 +0000 | [diff] [blame] | 468 | StreamString &dwarf_opcode_strm) |
| 469 | { |
| 470 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 471 | |
| 472 | llvm::Module *module = m_code_generator_ptr->GetModule(); |
| 473 | |
| 474 | if (!module) |
| 475 | { |
| 476 | if (log) |
| 477 | log->Printf("IR doesn't contain a module"); |
| 478 | |
| 479 | return 1; |
| 480 | } |
| 481 | |
Sean Callanan | dcb658b | 2010-07-02 21:09:36 +0000 | [diff] [blame] | 482 | 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] | 483 | |
Sean Callanan | dcb658b | 2010-07-02 21:09:36 +0000 | [diff] [blame] | 484 | return ir_to_dwarf.runOnModule(*module); |
Sean Callanan | 848960c | 2010-06-23 23:18:04 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 487 | bool |
Sean Callanan | 5cf4a1c | 2010-07-03 01:35:46 +0000 | [diff] [blame] | 488 | ClangExpression::PrepareIRForTarget (ClangExpressionVariableList &expr_local_variable_list) |
| 489 | { |
| 490 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 491 | |
| 492 | llvm::Module *module = m_code_generator_ptr->GetModule(); |
| 493 | |
| 494 | if (!module) |
| 495 | { |
| 496 | if (log) |
| 497 | log->Printf("IR doesn't contain a module"); |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | |
Sean Callanan | 8bce665 | 2010-07-13 21:41:46 +0000 | [diff] [blame] | 502 | llvm::Triple target_triple = m_clang_ap->getTarget().getTriple(); |
| 503 | |
| 504 | std::string err; |
| 505 | |
| 506 | const llvm::Target *target = llvm::TargetRegistry::lookupTarget(m_target_triple, err); |
| 507 | |
| 508 | if (!target) |
| 509 | { |
| 510 | if (log) |
| 511 | log->Printf("Couldn't find a target for %s", m_target_triple.c_str()); |
| 512 | |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | std::auto_ptr<llvm::TargetMachine> target_machine(target->createTargetMachine(m_target_triple, "")); |
| 517 | |
| 518 | 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] | 519 | |
| 520 | return ir_for_target.runOnModule(*module); |
| 521 | } |
| 522 | |
| 523 | bool |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 524 | ClangExpression::JITFunction (const ExecutionContext &exc_context, const char *name) |
| 525 | { |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 526 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 527 | |
| 528 | llvm::Module *module = m_code_generator_ptr->GetModule(); |
| 529 | |
| 530 | if (module) |
| 531 | { |
| 532 | std::string error; |
| 533 | |
| 534 | if (m_jit_mm_ptr == NULL) |
| 535 | m_jit_mm_ptr = new RecordingMemoryManager(); |
| 536 | |
| 537 | //llvm::InitializeNativeTarget(); |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 538 | |
| 539 | if (log) |
| 540 | { |
| 541 | const char *relocation_model_string; |
| 542 | |
| 543 | switch (llvm::TargetMachine::getRelocationModel()) |
| 544 | { |
Sean Callanan | 1e7089a | 2010-07-29 19:03:08 +0000 | [diff] [blame] | 545 | case llvm::Reloc::Default: |
| 546 | relocation_model_string = "Default"; |
| 547 | break; |
| 548 | case llvm::Reloc::Static: |
| 549 | relocation_model_string = "Static"; |
| 550 | break; |
| 551 | case llvm::Reloc::PIC_: |
| 552 | relocation_model_string = "PIC_"; |
| 553 | break; |
| 554 | case llvm::Reloc::DynamicNoPIC: |
| 555 | relocation_model_string = "DynamicNoPIC"; |
| 556 | break; |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | log->Printf("Target machine's relocation model: %s", relocation_model_string); |
| 560 | } |
Sean Callanan | 1e7089a | 2010-07-29 19:03:08 +0000 | [diff] [blame] | 561 | |
| 562 | if (m_execution_engine.get() == 0) |
| 563 | m_execution_engine.reset(llvm::ExecutionEngine::createJIT (module, |
| 564 | &error, |
| 565 | m_jit_mm_ptr, |
| 566 | CodeGenOpt::Default, |
| 567 | true, |
| 568 | CodeModel::Small)); // set to small so RIP-relative relocations work in PIC |
| 569 | |
| 570 | m_execution_engine->DisableLazyCompilation(); |
| 571 | llvm::Function *function = module->getFunction (llvm::StringRef (name)); |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 572 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 573 | // We don't actually need the function pointer here, this just forces it to get resolved. |
| 574 | void *fun_ptr = m_execution_engine->getPointerToFunction(function); |
| 575 | // Note, you probably won't get here on error, since the LLVM JIT tends to just |
| 576 | // exit on error at present... So be careful. |
| 577 | if (fun_ptr == 0) |
| 578 | return false; |
| 579 | m_jitted_functions.push_back(ClangExpression::JittedFunction(name, (lldb::addr_t) fun_ptr)); |
| 580 | |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | |
| 585 | bool |
| 586 | ClangExpression::WriteJITCode (const ExecutionContext &exc_context) |
| 587 | { |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 588 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 589 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 590 | if (m_jit_mm_ptr == NULL) |
| 591 | return false; |
| 592 | |
| 593 | if (exc_context.process == NULL) |
| 594 | return false; |
| 595 | |
| 596 | // Look over the regions allocated for the function compiled. The JIT |
| 597 | // tries to allocate the functions & stubs close together, so we should try to |
| 598 | // write them that way too... |
| 599 | // For now I only write functions with no stubs, globals, exception tables, |
| 600 | // etc. So I only need to write the functions. |
| 601 | |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 602 | size_t alloc_size = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 603 | std::map<uint8_t *, uint8_t *>::iterator fun_pos, fun_end = m_jit_mm_ptr->m_functions.end(); |
| 604 | for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++) |
| 605 | { |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 606 | alloc_size += (*fun_pos).second - (*fun_pos).first; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | Error error; |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 610 | lldb::addr_t target_addr = exc_context.process->AllocateMemory (alloc_size, lldb::ePermissionsReadable|lldb::ePermissionsExecutable, error); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 611 | |
| 612 | if (target_addr == LLDB_INVALID_ADDRESS) |
| 613 | return false; |
| 614 | |
| 615 | lldb::addr_t cursor = target_addr; |
| 616 | for (fun_pos = m_jit_mm_ptr->m_functions.begin(); fun_pos != fun_end; fun_pos++) |
| 617 | { |
Sean Callanan | 321fe9e | 2010-07-28 01:00:59 +0000 | [diff] [blame] | 618 | if (log) |
| 619 | log->Printf("Reading [%p-%p] from m_functions", fun_pos->first, fun_pos->second); |
| 620 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 621 | lldb::addr_t lstart = (lldb::addr_t) (*fun_pos).first; |
| 622 | lldb::addr_t lend = (lldb::addr_t) (*fun_pos).second; |
| 623 | size_t size = lend - lstart; |
| 624 | exc_context.process->WriteMemory(cursor, (void *) lstart, size, error); |
| 625 | m_jit_mm_ptr->AddToLocalToRemoteMap (lstart, size, cursor); |
| 626 | cursor += size; |
| 627 | } |
| 628 | |
| 629 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 630 | |
| 631 | for (pos = m_jitted_functions.begin(); pos != end; pos++) |
| 632 | { |
| 633 | (*pos).m_remote_addr = m_jit_mm_ptr->GetRemoteAddressForLocal ((*pos).m_local_addr); |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | lldb::addr_t |
| 639 | ClangExpression::GetFunctionAddress (const char *name) |
| 640 | { |
| 641 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 642 | |
| 643 | for (pos = m_jitted_functions.begin(); pos < end; pos++) |
| 644 | { |
| 645 | if (strcmp ((*pos).m_name.c_str(), name) == 0) |
| 646 | return (*pos).m_remote_addr; |
| 647 | } |
| 648 | return LLDB_INVALID_ADDRESS; |
| 649 | } |
| 650 | |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 651 | Error |
| 652 | ClangExpression::DisassembleFunction (Stream &stream, ExecutionContext &exe_ctx, const char *name) |
| 653 | { |
| 654 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 655 | |
| 656 | Error ret; |
| 657 | |
| 658 | ret.Clear(); |
| 659 | |
| 660 | lldb::addr_t func_local_addr = LLDB_INVALID_ADDRESS; |
| 661 | lldb::addr_t func_remote_addr = LLDB_INVALID_ADDRESS; |
| 662 | |
| 663 | std::vector<JittedFunction>::iterator pos, end = m_jitted_functions.end(); |
| 664 | |
| 665 | for (pos = m_jitted_functions.begin(); pos < end; pos++) |
| 666 | { |
| 667 | if (strcmp(pos->m_name.c_str(), name) == 0) |
| 668 | { |
| 669 | func_local_addr = pos->m_local_addr; |
| 670 | func_remote_addr = pos->m_remote_addr; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (func_local_addr == LLDB_INVALID_ADDRESS) |
| 675 | { |
| 676 | ret.SetErrorToGenericError(); |
| 677 | ret.SetErrorStringWithFormat("Couldn't find function %s for disassembly", name); |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | if(log) |
| 682 | log->Printf("Found function, has local address 0x%llx and remote address 0x%llx", (uint64_t)func_local_addr, (uint64_t)func_remote_addr); |
| 683 | |
| 684 | std::pair <lldb::addr_t, lldb::addr_t> func_range; |
| 685 | |
| 686 | func_range = m_jit_mm_ptr->GetRemoteRangeForLocal(func_local_addr); |
| 687 | |
| 688 | if (func_range.first == 0 && func_range.second == 0) |
| 689 | { |
| 690 | ret.SetErrorToGenericError(); |
| 691 | ret.SetErrorStringWithFormat("Couldn't find code range for function %s", name); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | if(log) |
| 696 | log->Printf("Function's code range is [0x%llx-0x%llx]", func_range.first, func_range.second); |
| 697 | |
| 698 | if (!exe_ctx.target) |
| 699 | { |
| 700 | ret.SetErrorToGenericError(); |
| 701 | ret.SetErrorString("Couldn't find the target"); |
| 702 | } |
| 703 | |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 704 | lldb::DataBufferSP buffer_sp(new DataBufferHeap(func_range.second - func_remote_addr, 0)); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 705 | |
| 706 | Error err; |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 707 | exe_ctx.process->ReadMemory(func_remote_addr, buffer_sp->GetBytes(), buffer_sp->GetByteSize(), err); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 708 | |
| 709 | if (!err.Success()) |
| 710 | { |
| 711 | ret.SetErrorToGenericError(); |
| 712 | ret.SetErrorStringWithFormat("Couldn't read from process: %s", err.AsCString("unknown error")); |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | ArchSpec arch(exe_ctx.target->GetArchitecture()); |
| 717 | |
| 718 | Disassembler *disassembler = Disassembler::FindPlugin(arch); |
| 719 | |
| 720 | if (disassembler == NULL) |
| 721 | { |
| 722 | ret.SetErrorToGenericError(); |
| 723 | ret.SetErrorStringWithFormat("Unable to find disassembler plug-in for %s architecture.", arch.AsCString()); |
| 724 | return ret; |
| 725 | } |
| 726 | |
| 727 | if (!exe_ctx.process) |
| 728 | { |
| 729 | ret.SetErrorToGenericError(); |
| 730 | ret.SetErrorString("Couldn't find the process"); |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | DataExtractor extractor(buffer_sp, |
| 735 | exe_ctx.process->GetByteOrder(), |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 736 | exe_ctx.target->GetArchitecture().GetAddressByteSize()); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 737 | |
| 738 | if(log) |
| 739 | { |
| 740 | log->Printf("Function data has contents:"); |
| 741 | extractor.PutToLog (log, |
| 742 | 0, |
| 743 | extractor.GetByteSize(), |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 744 | func_remote_addr, |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 745 | 16, |
| 746 | DataExtractor::TypeUInt8); |
| 747 | } |
| 748 | |
| 749 | disassembler->DecodeInstructions(extractor, 0, UINT32_MAX); |
| 750 | |
| 751 | Disassembler::InstructionList &instruction_list = disassembler->GetInstructionList(); |
| 752 | |
| 753 | uint32_t bytes_offset = 0; |
| 754 | |
| 755 | for (uint32_t instruction_index = 0, num_instructions = instruction_list.GetSize(); |
| 756 | instruction_index < num_instructions; |
| 757 | ++instruction_index) |
| 758 | { |
| 759 | Disassembler::Instruction *instruction = instruction_list.GetInstructionAtIndex(instruction_index); |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 760 | Address addr(NULL, func_remote_addr + bytes_offset); |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 761 | instruction->Dump (&stream, |
Sean Callanan | 32824aa | 2010-07-23 22:19:18 +0000 | [diff] [blame] | 762 | &addr, |
Sean Callanan | 8541f2f | 2010-07-23 02:19:15 +0000 | [diff] [blame] | 763 | &extractor, |
| 764 | bytes_offset, |
| 765 | exe_ctx, |
| 766 | true); |
| 767 | stream.PutChar('\n'); |
| 768 | bytes_offset += instruction->GetByteSize(); |
| 769 | } |
| 770 | |
| 771 | return ret; |
| 772 | } |
| 773 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 774 | unsigned |
| 775 | ClangExpression::Compile() |
| 776 | { |
| 777 | Mutex::Locker locker(GetClangMutex ()); |
| 778 | bool IsAST = false; |
| 779 | |
| 780 | if (CreateCompilerInstance(IsAST)) |
| 781 | { |
| 782 | // Validate/process some options |
| 783 | if (m_clang_ap->getHeaderSearchOpts().Verbose) |
| 784 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 785 | << " based upon " << PACKAGE_STRING |
| 786 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 787 | |
| 788 | // Enforce certain implications. |
| 789 | if (!m_clang_ap->getFrontendOpts().ViewClassInheritance.empty()) |
| 790 | m_clang_ap->getFrontendOpts().ProgramAction = frontend::InheritanceView; |
| 791 | // if (!compiler_instance->getFrontendOpts().FixItSuffix.empty()) |
| 792 | // compiler_instance->getFrontendOpts().ProgramAction = frontend::FixIt; |
| 793 | |
| 794 | 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] | 795 | |
| 796 | // If we aren't using an AST file, setup the file and source managers and |
| 797 | // the preprocessor. |
| 798 | if (!IsAST) { |
| 799 | if (!i) { |
| 800 | // Create a file manager object to provide access to and cache the |
| 801 | // filesystem. |
| 802 | m_clang_ap->createFileManager(); |
| 803 | |
| 804 | // Create the source manager. |
| 805 | m_clang_ap->createSourceManager(); |
| 806 | } else { |
| 807 | // Reset the ID tables if we are reusing the SourceManager. |
| 808 | m_clang_ap->getSourceManager().clearIDTables(); |
| 809 | } |
| 810 | |
| 811 | // Create the preprocessor. |
| 812 | m_clang_ap->createPreprocessor(); |
| 813 | } |
| 814 | |
| 815 | llvm::OwningPtr<FrontendAction> Act(CreateFrontendAction(*m_clang_ap.get())); |
| 816 | if (!Act) |
| 817 | break; |
| 818 | |
Greg Clayton | e41c4b2 | 2010-06-13 17:34:29 +0000 | [diff] [blame] | 819 | if (Act->BeginSourceFile(*m_clang_ap, |
| 820 | m_clang_ap->getFrontendOpts().Inputs[i].second, |
| 821 | m_clang_ap->getFrontendOpts().Inputs[i].first)) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | Act->Execute(); |
| 823 | Act->EndSourceFile(); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | if (m_clang_ap->getDiagnosticOpts().ShowCarets) |
| 828 | { |
| 829 | unsigned NumWarnings = m_clang_ap->getDiagnostics().getNumWarnings(); |
| 830 | unsigned NumErrors = m_clang_ap->getDiagnostics().getNumErrors() - |
| 831 | m_clang_ap->getDiagnostics().getNumErrorsSuppressed(); |
| 832 | |
| 833 | if (NumWarnings || NumErrors) |
| 834 | { |
| 835 | if (NumWarnings) |
| 836 | fprintf (stderr, "%u warning%s%s", NumWarnings, (NumWarnings == 1 ? "" : "s"), (NumErrors ? " and " : "")); |
| 837 | if (NumErrors) |
| 838 | fprintf (stderr, "%u error%s", NumErrors, (NumErrors == 1 ? "" : "s")); |
| 839 | fprintf (stderr, " generated.\n"); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (m_clang_ap->getFrontendOpts().ShowStats) { |
| 844 | m_clang_ap->getFileManager().PrintStats(); |
| 845 | fprintf(stderr, "\n"); |
| 846 | } |
| 847 | |
| 848 | // Return the appropriate status when verifying diagnostics. |
| 849 | // |
| 850 | // FIXME: If we could make getNumErrors() do the right thing, we wouldn't need |
| 851 | // this. |
| 852 | if (m_clang_ap->getDiagnosticOpts().VerifyDiagnostics) |
| 853 | return static_cast<VerifyDiagnosticsClient&>(m_clang_ap->getDiagnosticClient()).HadErrors(); |
| 854 | |
| 855 | return m_clang_ap->getDiagnostics().getNumErrors(); |
| 856 | } |
| 857 | return 1; |
| 858 | } |