1 //===-- ClangExpressionParser.cpp -----------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/AST/ASTContext.h" 10 #include "clang/AST/ASTDiagnostic.h" 11 #include "clang/AST/ExternalASTSource.h" 12 #include "clang/AST/PrettyPrinter.h" 13 #include "clang/Basic/Builtins.h" 14 #include "clang/Basic/DiagnosticIDs.h" 15 #include "clang/Basic/SourceLocation.h" 16 #include "clang/Basic/TargetInfo.h" 17 #include "clang/Basic/Version.h" 18 #include "clang/CodeGen/CodeGenAction.h" 19 #include "clang/CodeGen/ModuleBuilder.h" 20 #include "clang/Edit/Commit.h" 21 #include "clang/Edit/EditedSource.h" 22 #include "clang/Edit/EditsReceiver.h" 23 #include "clang/Frontend/CompilerInstance.h" 24 #include "clang/Frontend/CompilerInvocation.h" 25 #include "clang/Frontend/FrontendActions.h" 26 #include "clang/Frontend/FrontendDiagnostic.h" 27 #include "clang/Frontend/FrontendPluginRegistry.h" 28 #include "clang/Frontend/TextDiagnosticBuffer.h" 29 #include "clang/Frontend/TextDiagnosticPrinter.h" 30 #include "clang/Lex/Preprocessor.h" 31 #include "clang/Parse/ParseAST.h" 32 #include "clang/Rewrite/Core/Rewriter.h" 33 #include "clang/Rewrite/Frontend/FrontendActions.h" 34 #include "clang/Sema/CodeCompleteConsumer.h" 35 #include "clang/Sema/Sema.h" 36 #include "clang/Sema/SemaConsumer.h" 37 38 #include "llvm/ADT/StringRef.h" 39 #include "llvm/ExecutionEngine/ExecutionEngine.h" 40 #include "llvm/Support/CrashRecoveryContext.h" 41 #include "llvm/Support/Debug.h" 42 #include "llvm/Support/FileSystem.h" 43 #include "llvm/Support/TargetSelect.h" 44 45 #include "llvm/IR/LLVMContext.h" 46 #include "llvm/IR/Module.h" 47 #include "llvm/Support/DynamicLibrary.h" 48 #include "llvm/Support/ErrorHandling.h" 49 #include "llvm/Support/Host.h" 50 #include "llvm/Support/MemoryBuffer.h" 51 #include "llvm/Support/Signals.h" 52 53 #include "ClangDiagnostic.h" 54 #include "ClangExpressionParser.h" 55 #include "ClangUserExpression.h" 56 57 #include "ASTUtils.h" 58 #include "ClangASTSource.h" 59 #include "ClangDiagnostic.h" 60 #include "ClangExpressionDeclMap.h" 61 #include "ClangExpressionHelper.h" 62 #include "ClangExpressionParser.h" 63 #include "ClangHost.h" 64 #include "ClangModulesDeclVendor.h" 65 #include "ClangPersistentVariables.h" 66 #include "IRDynamicChecks.h" 67 #include "IRForTarget.h" 68 #include "ModuleDependencyCollector.h" 69 70 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 71 #include "lldb/Core/Debugger.h" 72 #include "lldb/Core/Disassembler.h" 73 #include "lldb/Core/Module.h" 74 #include "lldb/Core/StreamFile.h" 75 #include "lldb/Expression/IRExecutionUnit.h" 76 #include "lldb/Expression/IRInterpreter.h" 77 #include "lldb/Host/File.h" 78 #include "lldb/Host/HostInfo.h" 79 #include "lldb/Symbol/SymbolVendor.h" 80 #include "lldb/Target/ExecutionContext.h" 81 #include "lldb/Target/Language.h" 82 #include "lldb/Target/Process.h" 83 #include "lldb/Target/Target.h" 84 #include "lldb/Target/ThreadPlanCallFunction.h" 85 #include "lldb/Utility/DataBufferHeap.h" 86 #include "lldb/Utility/LLDBAssert.h" 87 #include "lldb/Utility/Log.h" 88 #include "lldb/Utility/ReproducerProvider.h" 89 #include "lldb/Utility/Stream.h" 90 #include "lldb/Utility/StreamString.h" 91 #include "lldb/Utility/StringList.h" 92 93 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" 94 #include "Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h" 95 96 #include <cctype> 97 #include <memory> 98 99 using namespace clang; 100 using namespace llvm; 101 using namespace lldb_private; 102 103 //===----------------------------------------------------------------------===// 104 // Utility Methods for Clang 105 //===----------------------------------------------------------------------===// 106 107 class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks { 108 ClangModulesDeclVendor &m_decl_vendor; 109 ClangPersistentVariables &m_persistent_vars; 110 clang::SourceManager &m_source_mgr; 111 StreamString m_error_stream; 112 bool m_has_errors = false; 113 114 public: 115 LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor, 116 ClangPersistentVariables &persistent_vars, 117 clang::SourceManager &source_mgr) 118 : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars), 119 m_source_mgr(source_mgr) {} 120 121 void moduleImport(SourceLocation import_location, clang::ModuleIdPath path, 122 const clang::Module * /*null*/) override { 123 // Ignore modules that are imported in the wrapper code as these are not 124 // loaded by the user. 125 llvm::StringRef filename = 126 m_source_mgr.getPresumedLoc(import_location).getFilename(); 127 if (filename == ClangExpressionSourceCode::g_prefix_file_name) 128 return; 129 130 SourceModule module; 131 132 for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) 133 module.path.push_back(ConstString(component.first->getName())); 134 135 StreamString error_stream; 136 137 ClangModulesDeclVendor::ModuleVector exported_modules; 138 if (!m_decl_vendor.AddModule(module, &exported_modules, m_error_stream)) 139 m_has_errors = true; 140 141 for (ClangModulesDeclVendor::ModuleID module : exported_modules) 142 m_persistent_vars.AddHandLoadedClangModule(module); 143 } 144 145 bool hasErrors() { return m_has_errors; } 146 147 llvm::StringRef getErrorString() { return m_error_stream.GetString(); } 148 }; 149 150 static void AddAllFixIts(ClangDiagnostic *diag, const clang::Diagnostic &Info) { 151 for (auto &fix_it : Info.getFixItHints()) { 152 if (fix_it.isNull()) 153 continue; 154 diag->AddFixitHint(fix_it); 155 } 156 } 157 158 class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer { 159 public: 160 ClangDiagnosticManagerAdapter(DiagnosticOptions &opts) { 161 DiagnosticOptions *options = new DiagnosticOptions(opts); 162 options->ShowPresumedLoc = true; 163 options->ShowLevel = false; 164 m_os = std::make_shared<llvm::raw_string_ostream>(m_output); 165 m_passthrough = 166 std::make_shared<clang::TextDiagnosticPrinter>(*m_os, options); 167 } 168 169 void ResetManager(DiagnosticManager *manager = nullptr) { 170 m_manager = manager; 171 } 172 173 /// Returns the last ClangDiagnostic message that the DiagnosticManager 174 /// received or a nullptr if the DiagnosticMangager hasn't seen any 175 /// Clang diagnostics yet. 176 ClangDiagnostic *MaybeGetLastClangDiag() const { 177 if (m_manager->Diagnostics().empty()) 178 return nullptr; 179 lldb_private::Diagnostic *diag = m_manager->Diagnostics().back().get(); 180 ClangDiagnostic *clang_diag = dyn_cast<ClangDiagnostic>(diag); 181 return clang_diag; 182 } 183 184 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 185 const clang::Diagnostic &Info) override { 186 if (!m_manager) { 187 // We have no DiagnosticManager before/after parsing but we still could 188 // receive diagnostics (e.g., by the ASTImporter failing to copy decls 189 // when we move the expression result ot the ScratchASTContext). Let's at 190 // least log these diagnostics until we find a way to properly render 191 // them and display them to the user. 192 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 193 if (log) { 194 llvm::SmallVector<char, 32> diag_str; 195 Info.FormatDiagnostic(diag_str); 196 diag_str.push_back('\0'); 197 const char *plain_diag = diag_str.data(); 198 LLDB_LOG(log, "Received diagnostic outside parsing: {0}", plain_diag); 199 } 200 return; 201 } 202 203 // Update error/warning counters. 204 DiagnosticConsumer::HandleDiagnostic(DiagLevel, Info); 205 206 // Render diagnostic message to m_output. 207 m_output.clear(); 208 m_passthrough->HandleDiagnostic(DiagLevel, Info); 209 m_os->flush(); 210 211 lldb_private::DiagnosticSeverity severity; 212 bool make_new_diagnostic = true; 213 214 switch (DiagLevel) { 215 case DiagnosticsEngine::Level::Fatal: 216 case DiagnosticsEngine::Level::Error: 217 severity = eDiagnosticSeverityError; 218 break; 219 case DiagnosticsEngine::Level::Warning: 220 severity = eDiagnosticSeverityWarning; 221 break; 222 case DiagnosticsEngine::Level::Remark: 223 case DiagnosticsEngine::Level::Ignored: 224 severity = eDiagnosticSeverityRemark; 225 break; 226 case DiagnosticsEngine::Level::Note: 227 m_manager->AppendMessageToDiagnostic(m_output); 228 make_new_diagnostic = false; 229 230 // 'note:' diagnostics for errors and warnings can also contain Fix-Its. 231 // We add these Fix-Its to the last error diagnostic to make sure 232 // that we later have all Fix-Its related to an 'error' diagnostic when 233 // we apply them to the user expression. 234 auto *clang_diag = MaybeGetLastClangDiag(); 235 // If we don't have a previous diagnostic there is nothing to do. 236 // If the previous diagnostic already has its own Fix-Its, assume that 237 // the 'note:' Fix-It is just an alternative way to solve the issue and 238 // ignore these Fix-Its. 239 if (!clang_diag || clang_diag->HasFixIts()) 240 break; 241 // Ignore all Fix-Its that are not associated with an error. 242 if (clang_diag->GetSeverity() != eDiagnosticSeverityError) 243 break; 244 AddAllFixIts(clang_diag, Info); 245 break; 246 } 247 if (make_new_diagnostic) { 248 // ClangDiagnostic messages are expected to have no whitespace/newlines 249 // around them. 250 std::string stripped_output = 251 std::string(llvm::StringRef(m_output).trim()); 252 253 auto new_diagnostic = std::make_unique<ClangDiagnostic>( 254 stripped_output, severity, Info.getID()); 255 256 // Don't store away warning fixits, since the compiler doesn't have 257 // enough context in an expression for the warning to be useful. 258 // FIXME: Should we try to filter out FixIts that apply to our generated 259 // code, and not the user's expression? 260 if (severity == eDiagnosticSeverityError) 261 AddAllFixIts(new_diagnostic.get(), Info); 262 263 m_manager->AddDiagnostic(std::move(new_diagnostic)); 264 } 265 } 266 267 void BeginSourceFile(const LangOptions &LO, const Preprocessor *PP) override { 268 m_passthrough->BeginSourceFile(LO, PP); 269 } 270 271 void EndSourceFile() override { m_passthrough->EndSourceFile(); } 272 273 private: 274 DiagnosticManager *m_manager = nullptr; 275 std::shared_ptr<clang::TextDiagnosticPrinter> m_passthrough; 276 /// Output stream of m_passthrough. 277 std::shared_ptr<llvm::raw_string_ostream> m_os; 278 /// Output string filled by m_os. 279 std::string m_output; 280 }; 281 282 static void SetupModuleHeaderPaths(CompilerInstance *compiler, 283 std::vector<std::string> include_directories, 284 lldb::TargetSP target_sp) { 285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 286 287 HeaderSearchOptions &search_opts = compiler->getHeaderSearchOpts(); 288 289 for (const std::string &dir : include_directories) { 290 search_opts.AddPath(dir, frontend::System, false, true); 291 LLDB_LOG(log, "Added user include dir: {0}", dir); 292 } 293 294 llvm::SmallString<128> module_cache; 295 const auto &props = ModuleList::GetGlobalModuleListProperties(); 296 props.GetClangModulesCachePath().GetPath(module_cache); 297 search_opts.ModuleCachePath = std::string(module_cache.str()); 298 LLDB_LOG(log, "Using module cache path: {0}", module_cache.c_str()); 299 300 search_opts.ResourceDir = GetClangResourceDir().GetPath(); 301 302 search_opts.ImplicitModuleMaps = true; 303 } 304 305 /// Iff the given identifier is a C++ keyword, remove it from the 306 /// identifier table (i.e., make the token a normal identifier). 307 static void RemoveCppKeyword(IdentifierTable &idents, llvm::StringRef token) { 308 // FIXME: 'using' is used by LLDB for local variables, so we can't remove 309 // this keyword without breaking this functionality. 310 if (token == "using") 311 return; 312 // GCC's '__null' is used by LLDB to define NULL/Nil/nil. 313 if (token == "__null") 314 return; 315 316 LangOptions cpp_lang_opts; 317 cpp_lang_opts.CPlusPlus = true; 318 cpp_lang_opts.CPlusPlus11 = true; 319 cpp_lang_opts.CPlusPlus20 = true; 320 321 clang::IdentifierInfo &ii = idents.get(token); 322 // The identifier has to be a C++-exclusive keyword. if not, then there is 323 // nothing to do. 324 if (!ii.isCPlusPlusKeyword(cpp_lang_opts)) 325 return; 326 // If the token is already an identifier, then there is nothing to do. 327 if (ii.getTokenID() == clang::tok::identifier) 328 return; 329 // Otherwise the token is a C++ keyword, so turn it back into a normal 330 // identifier. 331 ii.revertTokenIDToIdentifier(); 332 } 333 334 /// Remove all C++ keywords from the given identifier table. 335 static void RemoveAllCppKeywords(IdentifierTable &idents) { 336 #define KEYWORD(NAME, FLAGS) RemoveCppKeyword(idents, llvm::StringRef(#NAME)); 337 #include "clang/Basic/TokenKinds.def" 338 } 339 340 /// Configures Clang diagnostics for the expression parser. 341 static void SetupDefaultClangDiagnostics(CompilerInstance &compiler) { 342 // List of Clang warning groups that are not useful when parsing expressions. 343 const std::vector<const char *> groupsToIgnore = { 344 "unused-value", 345 "odr", 346 "unused-getter-return-value", 347 }; 348 for (const char *group : groupsToIgnore) { 349 compiler.getDiagnostics().setSeverityForGroup( 350 clang::diag::Flavor::WarningOrError, group, 351 clang::diag::Severity::Ignored, SourceLocation()); 352 } 353 } 354 355 //===----------------------------------------------------------------------===// 356 // Implementation of ClangExpressionParser 357 //===----------------------------------------------------------------------===// 358 359 ClangExpressionParser::ClangExpressionParser( 360 ExecutionContextScope *exe_scope, Expression &expr, 361 bool generate_debug_info, std::vector<std::string> include_directories, 362 std::string filename) 363 : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(), 364 m_pp_callbacks(nullptr), 365 m_include_directories(std::move(include_directories)), 366 m_filename(std::move(filename)) { 367 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 368 369 // We can't compile expressions without a target. So if the exe_scope is 370 // null or doesn't have a target, then we just need to get out of here. I'll 371 // lldbassert and not make any of the compiler objects since 372 // I can't return errors directly from the constructor. Further calls will 373 // check if the compiler was made and 374 // bag out if it wasn't. 375 376 if (!exe_scope) { 377 lldbassert(exe_scope && 378 "Can't make an expression parser with a null scope."); 379 return; 380 } 381 382 lldb::TargetSP target_sp; 383 target_sp = exe_scope->CalculateTarget(); 384 if (!target_sp) { 385 lldbassert(target_sp.get() && 386 "Can't make an expression parser with a null target."); 387 return; 388 } 389 390 // 1. Create a new compiler instance. 391 m_compiler = std::make_unique<CompilerInstance>(); 392 393 // When capturing a reproducer, hook up the file collector with clang to 394 // collector modules and headers. 395 if (repro::Generator *g = repro::Reproducer::Instance().GetGenerator()) { 396 repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>(); 397 m_compiler->setModuleDepCollector( 398 std::make_shared<ModuleDependencyCollectorAdaptor>( 399 fp.GetFileCollector())); 400 DependencyOutputOptions &opts = m_compiler->getDependencyOutputOpts(); 401 opts.IncludeSystemHeaders = true; 402 opts.IncludeModuleFiles = true; 403 } 404 405 // Make sure clang uses the same VFS as LLDB. 406 m_compiler->createFileManager(FileSystem::Instance().GetVirtualFileSystem()); 407 408 lldb::LanguageType frame_lang = 409 expr.Language(); // defaults to lldb::eLanguageTypeUnknown 410 bool overridden_target_opts = false; 411 lldb_private::LanguageRuntime *lang_rt = nullptr; 412 413 std::string abi; 414 ArchSpec target_arch; 415 target_arch = target_sp->GetArchitecture(); 416 417 const auto target_machine = target_arch.GetMachine(); 418 419 // If the expression is being evaluated in the context of an existing stack 420 // frame, we introspect to see if the language runtime is available. 421 422 lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame(); 423 lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); 424 425 // Make sure the user hasn't provided a preferred execution language with 426 // `expression --language X -- ...` 427 if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown) 428 frame_lang = frame_sp->GetLanguage(); 429 430 if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) { 431 lang_rt = process_sp->GetLanguageRuntime(frame_lang); 432 LLDB_LOGF(log, "Frame has language of type %s", 433 Language::GetNameForLanguageType(frame_lang)); 434 } 435 436 // 2. Configure the compiler with a set of default options that are 437 // appropriate for most situations. 438 if (target_arch.IsValid()) { 439 std::string triple = target_arch.GetTriple().str(); 440 m_compiler->getTargetOpts().Triple = triple; 441 LLDB_LOGF(log, "Using %s as the target triple", 442 m_compiler->getTargetOpts().Triple.c_str()); 443 } else { 444 // If we get here we don't have a valid target and just have to guess. 445 // Sometimes this will be ok to just use the host target triple (when we 446 // evaluate say "2+3", but other expressions like breakpoint conditions and 447 // other things that _are_ target specific really shouldn't just be using 448 // the host triple. In such a case the language runtime should expose an 449 // overridden options set (3), below. 450 m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); 451 LLDB_LOGF(log, "Using default target triple of %s", 452 m_compiler->getTargetOpts().Triple.c_str()); 453 } 454 // Now add some special fixes for known architectures: Any arm32 iOS 455 // environment, but not on arm64 456 if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos && 457 m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos && 458 m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) { 459 m_compiler->getTargetOpts().ABI = "apcs-gnu"; 460 } 461 // Supported subsets of x86 462 if (target_machine == llvm::Triple::x86 || 463 target_machine == llvm::Triple::x86_64) { 464 m_compiler->getTargetOpts().Features.push_back("+sse"); 465 m_compiler->getTargetOpts().Features.push_back("+sse2"); 466 } 467 468 // Set the target CPU to generate code for. This will be empty for any CPU 469 // that doesn't really need to make a special 470 // CPU string. 471 m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU(); 472 473 // Set the target ABI 474 abi = GetClangTargetABI(target_arch); 475 if (!abi.empty()) 476 m_compiler->getTargetOpts().ABI = abi; 477 478 // 3. Now allow the runtime to provide custom configuration options for the 479 // target. In this case, a specialized language runtime is available and we 480 // can query it for extra options. For 99% of use cases, this will not be 481 // needed and should be provided when basic platform detection is not enough. 482 // FIXME: Generalize this. Only RenderScriptRuntime currently supports this 483 // currently. Hardcoding this isn't ideal but it's better than LanguageRuntime 484 // having knowledge of clang::TargetOpts. 485 if (auto *renderscript_rt = 486 llvm::dyn_cast_or_null<RenderScriptRuntime>(lang_rt)) 487 overridden_target_opts = 488 renderscript_rt->GetOverrideExprOptions(m_compiler->getTargetOpts()); 489 490 if (overridden_target_opts) 491 if (log && log->GetVerbose()) { 492 LLDB_LOGV( 493 log, "Using overridden target options for the expression evaluation"); 494 495 auto opts = m_compiler->getTargetOpts(); 496 LLDB_LOGV(log, "Triple: '{0}'", opts.Triple); 497 LLDB_LOGV(log, "CPU: '{0}'", opts.CPU); 498 LLDB_LOGV(log, "FPMath: '{0}'", opts.FPMath); 499 LLDB_LOGV(log, "ABI: '{0}'", opts.ABI); 500 LLDB_LOGV(log, "LinkerVersion: '{0}'", opts.LinkerVersion); 501 StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten"); 502 StringList::LogDump(log, opts.Features, "Features"); 503 } 504 505 // 4. Create and install the target on the compiler. 506 m_compiler->createDiagnostics(); 507 // Limit the number of error diagnostics we emit. 508 // A value of 0 means no limit for both LLDB and Clang. 509 m_compiler->getDiagnostics().setErrorLimit(target_sp->GetExprErrorLimit()); 510 511 auto target_info = TargetInfo::CreateTargetInfo( 512 m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts); 513 if (log) { 514 LLDB_LOGF(log, "Using SIMD alignment: %d", 515 target_info->getSimdDefaultAlign()); 516 LLDB_LOGF(log, "Target datalayout string: '%s'", 517 target_info->getDataLayout().getStringRepresentation().c_str()); 518 LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str()); 519 LLDB_LOGF(log, "Target vector alignment: %d", 520 target_info->getMaxVectorAlign()); 521 } 522 m_compiler->setTarget(target_info); 523 524 assert(m_compiler->hasTarget()); 525 526 // 5. Set language options. 527 lldb::LanguageType language = expr.Language(); 528 LangOptions &lang_opts = m_compiler->getLangOpts(); 529 530 switch (language) { 531 case lldb::eLanguageTypeC: 532 case lldb::eLanguageTypeC89: 533 case lldb::eLanguageTypeC99: 534 case lldb::eLanguageTypeC11: 535 // FIXME: the following language option is a temporary workaround, 536 // to "ask for C, get C++." 537 // For now, the expression parser must use C++ anytime the language is a C 538 // family language, because the expression parser uses features of C++ to 539 // capture values. 540 lang_opts.CPlusPlus = true; 541 break; 542 case lldb::eLanguageTypeObjC: 543 lang_opts.ObjC = true; 544 // FIXME: the following language option is a temporary workaround, 545 // to "ask for ObjC, get ObjC++" (see comment above). 546 lang_opts.CPlusPlus = true; 547 548 // Clang now sets as default C++14 as the default standard (with 549 // GNU extensions), so we do the same here to avoid mismatches that 550 // cause compiler error when evaluating expressions (e.g. nullptr not found 551 // as it's a C++11 feature). Currently lldb evaluates C++14 as C++11 (see 552 // two lines below) so we decide to be consistent with that, but this could 553 // be re-evaluated in the future. 554 lang_opts.CPlusPlus11 = true; 555 break; 556 case lldb::eLanguageTypeC_plus_plus: 557 case lldb::eLanguageTypeC_plus_plus_11: 558 case lldb::eLanguageTypeC_plus_plus_14: 559 lang_opts.CPlusPlus11 = true; 560 m_compiler->getHeaderSearchOpts().UseLibcxx = true; 561 LLVM_FALLTHROUGH; 562 case lldb::eLanguageTypeC_plus_plus_03: 563 lang_opts.CPlusPlus = true; 564 if (process_sp) 565 lang_opts.ObjC = 566 process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC) != nullptr; 567 break; 568 case lldb::eLanguageTypeObjC_plus_plus: 569 case lldb::eLanguageTypeUnknown: 570 default: 571 lang_opts.ObjC = true; 572 lang_opts.CPlusPlus = true; 573 lang_opts.CPlusPlus11 = true; 574 m_compiler->getHeaderSearchOpts().UseLibcxx = true; 575 break; 576 } 577 578 lang_opts.Bool = true; 579 lang_opts.WChar = true; 580 lang_opts.Blocks = true; 581 lang_opts.DebuggerSupport = 582 true; // Features specifically for debugger clients 583 if (expr.DesiredResultType() == Expression::eResultTypeId) 584 lang_opts.DebuggerCastResultToId = true; 585 586 lang_opts.CharIsSigned = ArchSpec(m_compiler->getTargetOpts().Triple.c_str()) 587 .CharIsSignedByDefault(); 588 589 // Spell checking is a nice feature, but it ends up completing a lot of types 590 // that we didn't strictly speaking need to complete. As a result, we spend a 591 // long time parsing and importing debug information. 592 lang_opts.SpellChecking = false; 593 594 auto *clang_expr = dyn_cast<ClangUserExpression>(&m_expr); 595 if (clang_expr && clang_expr->DidImportCxxModules()) { 596 LLDB_LOG(log, "Adding lang options for importing C++ modules"); 597 598 lang_opts.Modules = true; 599 // We want to implicitly build modules. 600 lang_opts.ImplicitModules = true; 601 // To automatically import all submodules when we import 'std'. 602 lang_opts.ModulesLocalVisibility = false; 603 604 // We use the @import statements, so we need this: 605 // FIXME: We could use the modules-ts, but that currently doesn't work. 606 lang_opts.ObjC = true; 607 608 // Options we need to parse libc++ code successfully. 609 // FIXME: We should ask the driver for the appropriate default flags. 610 lang_opts.GNUMode = true; 611 lang_opts.GNUKeywords = true; 612 lang_opts.DoubleSquareBracketAttributes = true; 613 lang_opts.CPlusPlus11 = true; 614 615 // The Darwin libc expects this macro to be set. 616 lang_opts.GNUCVersion = 40201; 617 618 SetupModuleHeaderPaths(m_compiler.get(), m_include_directories, 619 target_sp); 620 } 621 622 if (process_sp && lang_opts.ObjC) { 623 if (auto *runtime = ObjCLanguageRuntime::Get(*process_sp)) { 624 if (runtime->GetRuntimeVersion() == 625 ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2) 626 lang_opts.ObjCRuntime.set(ObjCRuntime::MacOSX, VersionTuple(10, 7)); 627 else 628 lang_opts.ObjCRuntime.set(ObjCRuntime::FragileMacOSX, 629 VersionTuple(10, 7)); 630 631 if (runtime->HasNewLiteralsAndIndexing()) 632 lang_opts.DebuggerObjCLiteral = true; 633 } 634 } 635 636 lang_opts.ThreadsafeStatics = false; 637 lang_opts.AccessControl = false; // Debuggers get universal access 638 lang_opts.DollarIdents = true; // $ indicates a persistent variable name 639 // We enable all builtin functions beside the builtins from libc/libm (e.g. 640 // 'fopen'). Those libc functions are already correctly handled by LLDB, and 641 // additionally enabling them as expandable builtins is breaking Clang. 642 lang_opts.NoBuiltin = true; 643 644 // Set CodeGen options 645 m_compiler->getCodeGenOpts().EmitDeclMetadata = true; 646 m_compiler->getCodeGenOpts().InstrumentFunctions = false; 647 m_compiler->getCodeGenOpts().setFramePointer( 648 CodeGenOptions::FramePointerKind::All); 649 if (generate_debug_info) 650 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); 651 else 652 m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo); 653 654 // Disable some warnings. 655 SetupDefaultClangDiagnostics(*m_compiler); 656 657 // Inform the target of the language options 658 // 659 // FIXME: We shouldn't need to do this, the target should be immutable once 660 // created. This complexity should be lifted elsewhere. 661 m_compiler->getTarget().adjust(m_compiler->getLangOpts()); 662 663 // 6. Set up the diagnostic buffer for reporting errors 664 665 auto diag_mgr = new ClangDiagnosticManagerAdapter( 666 m_compiler->getDiagnostics().getDiagnosticOptions()); 667 m_compiler->getDiagnostics().setClient(diag_mgr); 668 669 // 7. Set up the source management objects inside the compiler 670 m_compiler->createFileManager(); 671 if (!m_compiler->hasSourceManager()) 672 m_compiler->createSourceManager(m_compiler->getFileManager()); 673 m_compiler->createPreprocessor(TU_Complete); 674 675 switch (language) { 676 case lldb::eLanguageTypeC: 677 case lldb::eLanguageTypeC89: 678 case lldb::eLanguageTypeC99: 679 case lldb::eLanguageTypeC11: 680 case lldb::eLanguageTypeObjC: 681 // This is not a C++ expression but we enabled C++ as explained above. 682 // Remove all C++ keywords from the PP so that the user can still use 683 // variables that have C++ keywords as names (e.g. 'int template;'). 684 RemoveAllCppKeywords(m_compiler->getPreprocessor().getIdentifierTable()); 685 break; 686 default: 687 break; 688 } 689 690 if (ClangModulesDeclVendor *decl_vendor = 691 target_sp->GetClangModulesDeclVendor()) { 692 if (auto *clang_persistent_vars = llvm::cast<ClangPersistentVariables>( 693 target_sp->GetPersistentExpressionStateForLanguage( 694 lldb::eLanguageTypeC))) { 695 std::unique_ptr<PPCallbacks> pp_callbacks( 696 new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars, 697 m_compiler->getSourceManager())); 698 m_pp_callbacks = 699 static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get()); 700 m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks)); 701 } 702 } 703 704 // 8. Most of this we get from the CompilerInstance, but we also want to give 705 // the context an ExternalASTSource. 706 707 auto &PP = m_compiler->getPreprocessor(); 708 auto &builtin_context = PP.getBuiltinInfo(); 709 builtin_context.initializeBuiltins(PP.getIdentifierTable(), 710 m_compiler->getLangOpts()); 711 712 m_compiler->createASTContext(); 713 clang::ASTContext &ast_context = m_compiler->getASTContext(); 714 715 m_ast_context = std::make_unique<TypeSystemClang>( 716 "Expression ASTContext for '" + m_filename + "'", ast_context); 717 718 std::string module_name("$__lldb_module"); 719 720 m_llvm_context = std::make_unique<LLVMContext>(); 721 m_code_generator.reset(CreateLLVMCodeGen( 722 m_compiler->getDiagnostics(), module_name, 723 m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(), 724 m_compiler->getCodeGenOpts(), *m_llvm_context)); 725 } 726 727 ClangExpressionParser::~ClangExpressionParser() {} 728 729 namespace { 730 731 /// \class CodeComplete 732 /// 733 /// A code completion consumer for the clang Sema that is responsible for 734 /// creating the completion suggestions when a user requests completion 735 /// of an incomplete `expr` invocation. 736 class CodeComplete : public CodeCompleteConsumer { 737 CodeCompletionTUInfo m_info; 738 739 std::string m_expr; 740 unsigned m_position = 0; 741 /// The printing policy we use when printing declarations for our completion 742 /// descriptions. 743 clang::PrintingPolicy m_desc_policy; 744 745 struct CompletionWithPriority { 746 CompletionResult::Completion completion; 747 /// See CodeCompletionResult::Priority; 748 unsigned Priority; 749 750 /// Establishes a deterministic order in a list of CompletionWithPriority. 751 /// The order returned here is the order in which the completions are 752 /// displayed to the user. 753 bool operator<(const CompletionWithPriority &o) const { 754 // High priority results should come first. 755 if (Priority != o.Priority) 756 return Priority > o.Priority; 757 758 // Identical priority, so just make sure it's a deterministic order. 759 return completion.GetUniqueKey() < o.completion.GetUniqueKey(); 760 } 761 }; 762 763 /// The stored completions. 764 /// Warning: These are in a non-deterministic order until they are sorted 765 /// and returned back to the caller. 766 std::vector<CompletionWithPriority> m_completions; 767 768 /// Returns true if the given character can be used in an identifier. 769 /// This also returns true for numbers because for completion we usually 770 /// just iterate backwards over iterators. 771 /// 772 /// Note: lldb uses '$' in its internal identifiers, so we also allow this. 773 static bool IsIdChar(char c) { 774 return c == '_' || std::isalnum(c) || c == '$'; 775 } 776 777 /// Returns true if the given character is used to separate arguments 778 /// in the command line of lldb. 779 static bool IsTokenSeparator(char c) { return c == ' ' || c == '\t'; } 780 781 /// Drops all tokens in front of the expression that are unrelated for 782 /// the completion of the cmd line. 'unrelated' means here that the token 783 /// is not interested for the lldb completion API result. 784 StringRef dropUnrelatedFrontTokens(StringRef cmd) const { 785 if (cmd.empty()) 786 return cmd; 787 788 // If we are at the start of a word, then all tokens are unrelated to 789 // the current completion logic. 790 if (IsTokenSeparator(cmd.back())) 791 return StringRef(); 792 793 // Remove all previous tokens from the string as they are unrelated 794 // to completing the current token. 795 StringRef to_remove = cmd; 796 while (!to_remove.empty() && !IsTokenSeparator(to_remove.back())) { 797 to_remove = to_remove.drop_back(); 798 } 799 cmd = cmd.drop_front(to_remove.size()); 800 801 return cmd; 802 } 803 804 /// Removes the last identifier token from the given cmd line. 805 StringRef removeLastToken(StringRef cmd) const { 806 while (!cmd.empty() && IsIdChar(cmd.back())) { 807 cmd = cmd.drop_back(); 808 } 809 return cmd; 810 } 811 812 /// Attempts to merge the given completion from the given position into the 813 /// existing command. Returns the completion string that can be returned to 814 /// the lldb completion API. 815 std::string mergeCompletion(StringRef existing, unsigned pos, 816 StringRef completion) const { 817 StringRef existing_command = existing.substr(0, pos); 818 // We rewrite the last token with the completion, so let's drop that 819 // token from the command. 820 existing_command = removeLastToken(existing_command); 821 // We also should remove all previous tokens from the command as they 822 // would otherwise be added to the completion that already has the 823 // completion. 824 existing_command = dropUnrelatedFrontTokens(existing_command); 825 return existing_command.str() + completion.str(); 826 } 827 828 public: 829 /// Constructs a CodeComplete consumer that can be attached to a Sema. 830 /// 831 /// \param[out] expr 832 /// The whole expression string that we are currently parsing. This 833 /// string needs to be equal to the input the user typed, and NOT the 834 /// final code that Clang is parsing. 835 /// \param[out] position 836 /// The character position of the user cursor in the `expr` parameter. 837 /// 838 CodeComplete(clang::LangOptions ops, std::string expr, unsigned position) 839 : CodeCompleteConsumer(CodeCompleteOptions()), 840 m_info(std::make_shared<GlobalCodeCompletionAllocator>()), m_expr(expr), 841 m_position(position), m_desc_policy(ops) { 842 843 // Ensure that the printing policy is producing a description that is as 844 // short as possible. 845 m_desc_policy.SuppressScope = true; 846 m_desc_policy.SuppressTagKeyword = true; 847 m_desc_policy.FullyQualifiedName = false; 848 m_desc_policy.TerseOutput = true; 849 m_desc_policy.IncludeNewlines = false; 850 m_desc_policy.UseVoidForZeroParams = false; 851 m_desc_policy.Bool = true; 852 } 853 854 /// \name Code-completion filtering 855 /// Check if the result should be filtered out. 856 bool isResultFilteredOut(StringRef Filter, 857 CodeCompletionResult Result) override { 858 // This code is mostly copied from CodeCompleteConsumer. 859 switch (Result.Kind) { 860 case CodeCompletionResult::RK_Declaration: 861 return !( 862 Result.Declaration->getIdentifier() && 863 Result.Declaration->getIdentifier()->getName().startswith(Filter)); 864 case CodeCompletionResult::RK_Keyword: 865 return !StringRef(Result.Keyword).startswith(Filter); 866 case CodeCompletionResult::RK_Macro: 867 return !Result.Macro->getName().startswith(Filter); 868 case CodeCompletionResult::RK_Pattern: 869 return !StringRef(Result.Pattern->getAsString()).startswith(Filter); 870 } 871 // If we trigger this assert or the above switch yields a warning, then 872 // CodeCompletionResult has been enhanced with more kinds of completion 873 // results. Expand the switch above in this case. 874 assert(false && "Unknown completion result type?"); 875 // If we reach this, then we should just ignore whatever kind of unknown 876 // result we got back. We probably can't turn it into any kind of useful 877 // completion suggestion with the existing code. 878 return true; 879 } 880 881 private: 882 /// Generate the completion strings for the given CodeCompletionResult. 883 /// Note that this function has to process results that could come in 884 /// non-deterministic order, so this function should have no side effects. 885 /// To make this easier to enforce, this function and all its parameters 886 /// should always be const-qualified. 887 /// \return Returns llvm::None if no completion should be provided for the 888 /// given CodeCompletionResult. 889 llvm::Optional<CompletionWithPriority> 890 getCompletionForResult(const CodeCompletionResult &R) const { 891 std::string ToInsert; 892 std::string Description; 893 // Handle the different completion kinds that come from the Sema. 894 switch (R.Kind) { 895 case CodeCompletionResult::RK_Declaration: { 896 const NamedDecl *D = R.Declaration; 897 ToInsert = R.Declaration->getNameAsString(); 898 // If we have a function decl that has no arguments we want to 899 // complete the empty parantheses for the user. If the function has 900 // arguments, we at least complete the opening bracket. 901 if (const FunctionDecl *F = dyn_cast<FunctionDecl>(D)) { 902 if (F->getNumParams() == 0) 903 ToInsert += "()"; 904 else 905 ToInsert += "("; 906 raw_string_ostream OS(Description); 907 F->print(OS, m_desc_policy, false); 908 OS.flush(); 909 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { 910 Description = V->getType().getAsString(m_desc_policy); 911 } else if (const FieldDecl *F = dyn_cast<FieldDecl>(D)) { 912 Description = F->getType().getAsString(m_desc_policy); 913 } else if (const NamespaceDecl *N = dyn_cast<NamespaceDecl>(D)) { 914 // If we try to complete a namespace, then we can directly append 915 // the '::'. 916 if (!N->isAnonymousNamespace()) 917 ToInsert += "::"; 918 } 919 break; 920 } 921 case CodeCompletionResult::RK_Keyword: 922 ToInsert = R.Keyword; 923 break; 924 case CodeCompletionResult::RK_Macro: 925 ToInsert = R.Macro->getName().str(); 926 break; 927 case CodeCompletionResult::RK_Pattern: 928 ToInsert = R.Pattern->getTypedText(); 929 break; 930 } 931 // We also filter some internal lldb identifiers here. The user 932 // shouldn't see these. 933 if (llvm::StringRef(ToInsert).startswith("$__lldb_")) 934 return llvm::None; 935 if (ToInsert.empty()) 936 return llvm::None; 937 // Merge the suggested Token into the existing command line to comply 938 // with the kind of result the lldb API expects. 939 std::string CompletionSuggestion = 940 mergeCompletion(m_expr, m_position, ToInsert); 941 942 CompletionResult::Completion completion(CompletionSuggestion, Description, 943 CompletionMode::Normal); 944 return {{completion, R.Priority}}; 945 } 946 947 public: 948 /// Adds the completions to the given CompletionRequest. 949 void GetCompletions(CompletionRequest &request) { 950 // Bring m_completions into a deterministic order and pass it on to the 951 // CompletionRequest. 952 llvm::sort(m_completions); 953 954 for (const CompletionWithPriority &C : m_completions) 955 request.AddCompletion(C.completion.GetCompletion(), 956 C.completion.GetDescription(), 957 C.completion.GetMode()); 958 } 959 960 /// \name Code-completion callbacks 961 /// Process the finalized code-completion results. 962 void ProcessCodeCompleteResults(Sema &SemaRef, CodeCompletionContext Context, 963 CodeCompletionResult *Results, 964 unsigned NumResults) override { 965 966 // The Sema put the incomplete token we try to complete in here during 967 // lexing, so we need to retrieve it here to know what we are completing. 968 StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter(); 969 970 // Iterate over all the results. Filter out results we don't want and 971 // process the rest. 972 for (unsigned I = 0; I != NumResults; ++I) { 973 // Filter the results with the information from the Sema. 974 if (!Filter.empty() && isResultFilteredOut(Filter, Results[I])) 975 continue; 976 977 CodeCompletionResult &R = Results[I]; 978 llvm::Optional<CompletionWithPriority> CompletionAndPriority = 979 getCompletionForResult(R); 980 if (!CompletionAndPriority) 981 continue; 982 m_completions.push_back(*CompletionAndPriority); 983 } 984 } 985 986 /// \param S the semantic-analyzer object for which code-completion is being 987 /// done. 988 /// 989 /// \param CurrentArg the index of the current argument. 990 /// 991 /// \param Candidates an array of overload candidates. 992 /// 993 /// \param NumCandidates the number of overload candidates 994 void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, 995 OverloadCandidate *Candidates, 996 unsigned NumCandidates, 997 SourceLocation OpenParLoc) override { 998 // At the moment we don't filter out any overloaded candidates. 999 } 1000 1001 CodeCompletionAllocator &getAllocator() override { 1002 return m_info.getAllocator(); 1003 } 1004 1005 CodeCompletionTUInfo &getCodeCompletionTUInfo() override { return m_info; } 1006 }; 1007 } // namespace 1008 1009 bool ClangExpressionParser::Complete(CompletionRequest &request, unsigned line, 1010 unsigned pos, unsigned typed_pos) { 1011 DiagnosticManager mgr; 1012 // We need the raw user expression here because that's what the CodeComplete 1013 // class uses to provide completion suggestions. 1014 // However, the `Text` method only gives us the transformed expression here. 1015 // To actually get the raw user input here, we have to cast our expression to 1016 // the LLVMUserExpression which exposes the right API. This should never fail 1017 // as we always have a ClangUserExpression whenever we call this. 1018 ClangUserExpression *llvm_expr = cast<ClangUserExpression>(&m_expr); 1019 CodeComplete CC(m_compiler->getLangOpts(), llvm_expr->GetUserText(), 1020 typed_pos); 1021 // We don't need a code generator for parsing. 1022 m_code_generator.reset(); 1023 // Start parsing the expression with our custom code completion consumer. 1024 ParseInternal(mgr, &CC, line, pos); 1025 CC.GetCompletions(request); 1026 return true; 1027 } 1028 1029 unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) { 1030 return ParseInternal(diagnostic_manager); 1031 } 1032 1033 unsigned 1034 ClangExpressionParser::ParseInternal(DiagnosticManager &diagnostic_manager, 1035 CodeCompleteConsumer *completion_consumer, 1036 unsigned completion_line, 1037 unsigned completion_column) { 1038 ClangDiagnosticManagerAdapter *adapter = 1039 static_cast<ClangDiagnosticManagerAdapter *>( 1040 m_compiler->getDiagnostics().getClient()); 1041 1042 adapter->ResetManager(&diagnostic_manager); 1043 1044 const char *expr_text = m_expr.Text(); 1045 1046 clang::SourceManager &source_mgr = m_compiler->getSourceManager(); 1047 bool created_main_file = false; 1048 1049 // Clang wants to do completion on a real file known by Clang's file manager, 1050 // so we have to create one to make this work. 1051 // TODO: We probably could also simulate to Clang's file manager that there 1052 // is a real file that contains our code. 1053 bool should_create_file = completion_consumer != nullptr; 1054 1055 // We also want a real file on disk if we generate full debug info. 1056 should_create_file |= m_compiler->getCodeGenOpts().getDebugInfo() == 1057 codegenoptions::FullDebugInfo; 1058 1059 if (should_create_file) { 1060 int temp_fd = -1; 1061 llvm::SmallString<128> result_path; 1062 if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) { 1063 tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr"); 1064 std::string temp_source_path = tmpdir_file_spec.GetPath(); 1065 llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path); 1066 } else { 1067 llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path); 1068 } 1069 1070 if (temp_fd != -1) { 1071 lldb_private::NativeFile file(temp_fd, File::eOpenOptionWrite, true); 1072 const size_t expr_text_len = strlen(expr_text); 1073 size_t bytes_written = expr_text_len; 1074 if (file.Write(expr_text, bytes_written).Success()) { 1075 if (bytes_written == expr_text_len) { 1076 file.Close(); 1077 if (auto fileEntry = m_compiler->getFileManager().getOptionalFileRef( 1078 result_path)) { 1079 source_mgr.setMainFileID(source_mgr.createFileID( 1080 *fileEntry, 1081 SourceLocation(), SrcMgr::C_User)); 1082 created_main_file = true; 1083 } 1084 } 1085 } 1086 } 1087 } 1088 1089 if (!created_main_file) { 1090 std::unique_ptr<MemoryBuffer> memory_buffer = 1091 MemoryBuffer::getMemBufferCopy(expr_text, m_filename); 1092 source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer))); 1093 } 1094 1095 adapter->BeginSourceFile(m_compiler->getLangOpts(), 1096 &m_compiler->getPreprocessor()); 1097 1098 ClangExpressionHelper *type_system_helper = 1099 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); 1100 1101 // If we want to parse for code completion, we need to attach our code 1102 // completion consumer to the Sema and specify a completion position. 1103 // While parsing the Sema will call this consumer with the provided 1104 // completion suggestions. 1105 if (completion_consumer) { 1106 auto main_file = source_mgr.getFileEntryForID(source_mgr.getMainFileID()); 1107 auto &PP = m_compiler->getPreprocessor(); 1108 // Lines and columns start at 1 in Clang, but code completion positions are 1109 // indexed from 0, so we need to add 1 to the line and column here. 1110 ++completion_line; 1111 ++completion_column; 1112 PP.SetCodeCompletionPoint(main_file, completion_line, completion_column); 1113 } 1114 1115 ASTConsumer *ast_transformer = 1116 type_system_helper->ASTTransformer(m_code_generator.get()); 1117 1118 std::unique_ptr<clang::ASTConsumer> Consumer; 1119 if (ast_transformer) { 1120 Consumer = std::make_unique<ASTConsumerForwarder>(ast_transformer); 1121 } else if (m_code_generator) { 1122 Consumer = std::make_unique<ASTConsumerForwarder>(m_code_generator.get()); 1123 } else { 1124 Consumer = std::make_unique<ASTConsumer>(); 1125 } 1126 1127 clang::ASTContext &ast_context = m_compiler->getASTContext(); 1128 1129 m_compiler->setSema(new Sema(m_compiler->getPreprocessor(), ast_context, 1130 *Consumer, TU_Complete, completion_consumer)); 1131 m_compiler->setASTConsumer(std::move(Consumer)); 1132 1133 if (ast_context.getLangOpts().Modules) { 1134 m_compiler->createASTReader(); 1135 m_ast_context->setSema(&m_compiler->getSema()); 1136 } 1137 1138 ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); 1139 if (decl_map) { 1140 decl_map->InstallCodeGenerator(&m_compiler->getASTConsumer()); 1141 decl_map->InstallDiagnosticManager(diagnostic_manager); 1142 1143 clang::ExternalASTSource *ast_source = decl_map->CreateProxy(); 1144 1145 if (ast_context.getExternalSource()) { 1146 auto module_wrapper = 1147 new ExternalASTSourceWrapper(ast_context.getExternalSource()); 1148 1149 auto ast_source_wrapper = new ExternalASTSourceWrapper(ast_source); 1150 1151 auto multiplexer = 1152 new SemaSourceWithPriorities(*module_wrapper, *ast_source_wrapper); 1153 IntrusiveRefCntPtr<ExternalASTSource> Source(multiplexer); 1154 ast_context.setExternalSource(Source); 1155 } else { 1156 ast_context.setExternalSource(ast_source); 1157 } 1158 decl_map->InstallASTContext(*m_ast_context); 1159 } 1160 1161 // Check that the ASTReader is properly attached to ASTContext and Sema. 1162 if (ast_context.getLangOpts().Modules) { 1163 assert(m_compiler->getASTContext().getExternalSource() && 1164 "ASTContext doesn't know about the ASTReader?"); 1165 assert(m_compiler->getSema().getExternalSource() && 1166 "Sema doesn't know about the ASTReader?"); 1167 } 1168 1169 { 1170 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema( 1171 &m_compiler->getSema()); 1172 ParseAST(m_compiler->getSema(), false, false); 1173 } 1174 1175 // Make sure we have no pointer to the Sema we are about to destroy. 1176 if (ast_context.getLangOpts().Modules) 1177 m_ast_context->setSema(nullptr); 1178 // Destroy the Sema. This is necessary because we want to emulate the 1179 // original behavior of ParseAST (which also destroys the Sema after parsing). 1180 m_compiler->setSema(nullptr); 1181 1182 adapter->EndSourceFile(); 1183 1184 unsigned num_errors = adapter->getNumErrors(); 1185 1186 if (m_pp_callbacks && m_pp_callbacks->hasErrors()) { 1187 num_errors++; 1188 diagnostic_manager.PutString(eDiagnosticSeverityError, 1189 "while importing modules:"); 1190 diagnostic_manager.AppendMessageToDiagnostic( 1191 m_pp_callbacks->getErrorString()); 1192 } 1193 1194 if (!num_errors) { 1195 type_system_helper->CommitPersistentDecls(); 1196 } 1197 1198 adapter->ResetManager(); 1199 1200 return num_errors; 1201 } 1202 1203 std::string 1204 ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) { 1205 std::string abi; 1206 1207 if (target_arch.IsMIPS()) { 1208 switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) { 1209 case ArchSpec::eMIPSABI_N64: 1210 abi = "n64"; 1211 break; 1212 case ArchSpec::eMIPSABI_N32: 1213 abi = "n32"; 1214 break; 1215 case ArchSpec::eMIPSABI_O32: 1216 abi = "o32"; 1217 break; 1218 default: 1219 break; 1220 } 1221 } 1222 return abi; 1223 } 1224 1225 /// Applies the given Fix-It hint to the given commit. 1226 static void ApplyFixIt(const FixItHint &fixit, clang::edit::Commit &commit) { 1227 // This is cobbed from clang::Rewrite::FixItRewriter. 1228 if (fixit.CodeToInsert.empty()) { 1229 if (fixit.InsertFromRange.isValid()) { 1230 commit.insertFromRange(fixit.RemoveRange.getBegin(), 1231 fixit.InsertFromRange, /*afterToken=*/false, 1232 fixit.BeforePreviousInsertions); 1233 return; 1234 } 1235 commit.remove(fixit.RemoveRange); 1236 return; 1237 } 1238 if (fixit.RemoveRange.isTokenRange() || 1239 fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd()) { 1240 commit.replace(fixit.RemoveRange, fixit.CodeToInsert); 1241 return; 1242 } 1243 commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert, 1244 /*afterToken=*/false, fixit.BeforePreviousInsertions); 1245 } 1246 1247 bool ClangExpressionParser::RewriteExpression( 1248 DiagnosticManager &diagnostic_manager) { 1249 clang::SourceManager &source_manager = m_compiler->getSourceManager(); 1250 clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(), 1251 nullptr); 1252 clang::edit::Commit commit(editor); 1253 clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts()); 1254 1255 class RewritesReceiver : public edit::EditsReceiver { 1256 Rewriter &rewrite; 1257 1258 public: 1259 RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {} 1260 1261 void insert(SourceLocation loc, StringRef text) override { 1262 rewrite.InsertText(loc, text); 1263 } 1264 void replace(CharSourceRange range, StringRef text) override { 1265 rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text); 1266 } 1267 }; 1268 1269 RewritesReceiver rewrites_receiver(rewriter); 1270 1271 const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics(); 1272 size_t num_diags = diagnostics.size(); 1273 if (num_diags == 0) 1274 return false; 1275 1276 for (const auto &diag : diagnostic_manager.Diagnostics()) { 1277 const auto *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag.get()); 1278 if (!diagnostic) 1279 continue; 1280 if (!diagnostic->HasFixIts()) 1281 continue; 1282 for (const FixItHint &fixit : diagnostic->FixIts()) 1283 ApplyFixIt(fixit, commit); 1284 } 1285 1286 // FIXME - do we want to try to propagate specific errors here? 1287 if (!commit.isCommitable()) 1288 return false; 1289 else if (!editor.commit(commit)) 1290 return false; 1291 1292 // Now play all the edits, and stash the result in the diagnostic manager. 1293 editor.applyRewrites(rewrites_receiver); 1294 RewriteBuffer &main_file_buffer = 1295 rewriter.getEditBuffer(source_manager.getMainFileID()); 1296 1297 std::string fixed_expression; 1298 llvm::raw_string_ostream out_stream(fixed_expression); 1299 1300 main_file_buffer.write(out_stream); 1301 out_stream.flush(); 1302 diagnostic_manager.SetFixedExpression(fixed_expression); 1303 1304 return true; 1305 } 1306 1307 static bool FindFunctionInModule(ConstString &mangled_name, 1308 llvm::Module *module, const char *orig_name) { 1309 for (const auto &func : module->getFunctionList()) { 1310 const StringRef &name = func.getName(); 1311 if (name.find(orig_name) != StringRef::npos) { 1312 mangled_name.SetString(name); 1313 return true; 1314 } 1315 } 1316 1317 return false; 1318 } 1319 1320 lldb_private::Status ClangExpressionParser::PrepareForExecution( 1321 lldb::addr_t &func_addr, lldb::addr_t &func_end, 1322 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx, 1323 bool &can_interpret, ExecutionPolicy execution_policy) { 1324 func_addr = LLDB_INVALID_ADDRESS; 1325 func_end = LLDB_INVALID_ADDRESS; 1326 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1327 1328 lldb_private::Status err; 1329 1330 std::unique_ptr<llvm::Module> llvm_module_up( 1331 m_code_generator->ReleaseModule()); 1332 1333 if (!llvm_module_up) { 1334 err.SetErrorToGenericError(); 1335 err.SetErrorString("IR doesn't contain a module"); 1336 return err; 1337 } 1338 1339 ConstString function_name; 1340 1341 if (execution_policy != eExecutionPolicyTopLevel) { 1342 // Find the actual name of the function (it's often mangled somehow) 1343 1344 if (!FindFunctionInModule(function_name, llvm_module_up.get(), 1345 m_expr.FunctionName())) { 1346 err.SetErrorToGenericError(); 1347 err.SetErrorStringWithFormat("Couldn't find %s() in the module", 1348 m_expr.FunctionName()); 1349 return err; 1350 } else { 1351 LLDB_LOGF(log, "Found function %s for %s", function_name.AsCString(), 1352 m_expr.FunctionName()); 1353 } 1354 } 1355 1356 SymbolContext sc; 1357 1358 if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) { 1359 sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything); 1360 } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) { 1361 sc.target_sp = target_sp; 1362 } 1363 1364 LLVMUserExpression::IRPasses custom_passes; 1365 { 1366 auto lang = m_expr.Language(); 1367 LLDB_LOGF(log, "%s - Current expression language is %s\n", __FUNCTION__, 1368 Language::GetNameForLanguageType(lang)); 1369 lldb::ProcessSP process_sp = exe_ctx.GetProcessSP(); 1370 if (process_sp && lang != lldb::eLanguageTypeUnknown) { 1371 auto runtime = process_sp->GetLanguageRuntime(lang); 1372 if (runtime) 1373 runtime->GetIRPasses(custom_passes); 1374 } 1375 } 1376 1377 if (custom_passes.EarlyPasses) { 1378 LLDB_LOGF(log, 1379 "%s - Running Early IR Passes from LanguageRuntime on " 1380 "expression module '%s'", 1381 __FUNCTION__, m_expr.FunctionName()); 1382 1383 custom_passes.EarlyPasses->run(*llvm_module_up); 1384 } 1385 1386 execution_unit_sp = std::make_shared<IRExecutionUnit>( 1387 m_llvm_context, // handed off here 1388 llvm_module_up, // handed off here 1389 function_name, exe_ctx.GetTargetSP(), sc, 1390 m_compiler->getTargetOpts().Features); 1391 1392 ClangExpressionHelper *type_system_helper = 1393 dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); 1394 ClangExpressionDeclMap *decl_map = 1395 type_system_helper->DeclMap(); // result can be NULL 1396 1397 if (decl_map) { 1398 StreamString error_stream; 1399 IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(), 1400 *execution_unit_sp, error_stream, 1401 function_name.AsCString()); 1402 1403 if (!ir_for_target.runOnModule(*execution_unit_sp->GetModule())) { 1404 err.SetErrorString(error_stream.GetString()); 1405 return err; 1406 } 1407 1408 Process *process = exe_ctx.GetProcessPtr(); 1409 1410 if (execution_policy != eExecutionPolicyAlways && 1411 execution_policy != eExecutionPolicyTopLevel) { 1412 lldb_private::Status interpret_error; 1413 1414 bool interpret_function_calls = 1415 !process ? false : process->CanInterpretFunctionCalls(); 1416 can_interpret = IRInterpreter::CanInterpret( 1417 *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), 1418 interpret_error, interpret_function_calls); 1419 1420 if (!can_interpret && execution_policy == eExecutionPolicyNever) { 1421 err.SetErrorStringWithFormat( 1422 "Can't evaluate the expression without a running target due to: %s", 1423 interpret_error.AsCString()); 1424 return err; 1425 } 1426 } 1427 1428 if (!process && execution_policy == eExecutionPolicyAlways) { 1429 err.SetErrorString("Expression needed to run in the target, but the " 1430 "target can't be run"); 1431 return err; 1432 } 1433 1434 if (!process && execution_policy == eExecutionPolicyTopLevel) { 1435 err.SetErrorString("Top-level code needs to be inserted into a runnable " 1436 "target, but the target can't be run"); 1437 return err; 1438 } 1439 1440 if (execution_policy == eExecutionPolicyAlways || 1441 (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) { 1442 if (m_expr.NeedsValidation() && process) { 1443 if (!process->GetDynamicCheckers()) { 1444 ClangDynamicCheckerFunctions *dynamic_checkers = 1445 new ClangDynamicCheckerFunctions(); 1446 1447 DiagnosticManager install_diagnostics; 1448 1449 if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) { 1450 if (install_diagnostics.Diagnostics().size()) 1451 err.SetErrorString(install_diagnostics.GetString().c_str()); 1452 else 1453 err.SetErrorString("couldn't install checkers, unknown error"); 1454 1455 return err; 1456 } 1457 1458 process->SetDynamicCheckers(dynamic_checkers); 1459 1460 LLDB_LOGF(log, "== [ClangExpressionParser::PrepareForExecution] " 1461 "Finished installing dynamic checkers =="); 1462 } 1463 1464 if (auto *checker_funcs = llvm::dyn_cast<ClangDynamicCheckerFunctions>( 1465 process->GetDynamicCheckers())) { 1466 IRDynamicChecks ir_dynamic_checks(*checker_funcs, 1467 function_name.AsCString()); 1468 1469 llvm::Module *module = execution_unit_sp->GetModule(); 1470 if (!module || !ir_dynamic_checks.runOnModule(*module)) { 1471 err.SetErrorToGenericError(); 1472 err.SetErrorString("Couldn't add dynamic checks to the expression"); 1473 return err; 1474 } 1475 1476 if (custom_passes.LatePasses) { 1477 LLDB_LOGF(log, 1478 "%s - Running Late IR Passes from LanguageRuntime on " 1479 "expression module '%s'", 1480 __FUNCTION__, m_expr.FunctionName()); 1481 1482 custom_passes.LatePasses->run(*module); 1483 } 1484 } 1485 } 1486 } 1487 1488 if (execution_policy == eExecutionPolicyAlways || 1489 execution_policy == eExecutionPolicyTopLevel || !can_interpret) { 1490 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); 1491 } 1492 } else { 1493 execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); 1494 } 1495 1496 return err; 1497 } 1498 1499 lldb_private::Status ClangExpressionParser::RunStaticInitializers( 1500 lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) { 1501 lldb_private::Status err; 1502 1503 lldbassert(execution_unit_sp.get()); 1504 lldbassert(exe_ctx.HasThreadScope()); 1505 1506 if (!execution_unit_sp.get()) { 1507 err.SetErrorString( 1508 "can't run static initializers for a NULL execution unit"); 1509 return err; 1510 } 1511 1512 if (!exe_ctx.HasThreadScope()) { 1513 err.SetErrorString("can't run static initializers without a thread"); 1514 return err; 1515 } 1516 1517 std::vector<lldb::addr_t> static_initializers; 1518 1519 execution_unit_sp->GetStaticInitializers(static_initializers); 1520 1521 for (lldb::addr_t static_initializer : static_initializers) { 1522 EvaluateExpressionOptions options; 1523 1524 lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction( 1525 exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(), 1526 llvm::ArrayRef<lldb::addr_t>(), options)); 1527 1528 DiagnosticManager execution_errors; 1529 lldb::ExpressionResults results = 1530 exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan( 1531 exe_ctx, call_static_initializer, options, execution_errors); 1532 1533 if (results != lldb::eExpressionCompleted) { 1534 err.SetErrorStringWithFormat("couldn't run static initializer: %s", 1535 execution_errors.GetString().c_str()); 1536 return err; 1537 } 1538 } 1539 1540 return err; 1541 } 1542