Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
39648 views
//===-- ClangExpressionSourceCode.cpp -------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ClangExpressionSourceCode.h"910#include "ClangExpressionUtil.h"1112#include "clang/Basic/CharInfo.h"13#include "clang/Basic/FileManager.h"14#include "clang/Basic/SourceManager.h"15#include "clang/Lex/Lexer.h"16#include "llvm/ADT/ScopeExit.h"17#include "llvm/ADT/StringRef.h"1819#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"20#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"21#include "lldb/Symbol/Block.h"22#include "lldb/Symbol/CompileUnit.h"23#include "lldb/Symbol/DebugMacros.h"24#include "lldb/Symbol/TypeSystem.h"25#include "lldb/Symbol/VariableList.h"26#include "lldb/Target/ExecutionContext.h"27#include "lldb/Target/Language.h"28#include "lldb/Target/Platform.h"29#include "lldb/Target/StackFrame.h"30#include "lldb/Target/Target.h"31#include "lldb/Utility/StreamString.h"32#include "lldb/lldb-forward.h"3334using namespace lldb_private;3536#define PREFIX_NAME "<lldb wrapper prefix>"37#define SUFFIX_NAME "<lldb wrapper suffix>"3839const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;4041const char *ClangExpressionSourceCode::g_expression_prefix =42"#line 1 \"" PREFIX_NAME R"("43#ifndef offsetof44#define offsetof(t, d) __builtin_offsetof(t, d)45#endif46#ifndef NULL47#define NULL (__null)48#endif49#ifndef Nil50#define Nil (__null)51#endif52#ifndef nil53#define nil (__null)54#endif55#ifndef YES56#define YES ((BOOL)1)57#endif58#ifndef NO59#define NO ((BOOL)0)60#endif61typedef __INT8_TYPE__ int8_t;62typedef __UINT8_TYPE__ uint8_t;63typedef __INT16_TYPE__ int16_t;64typedef __UINT16_TYPE__ uint16_t;65typedef __INT32_TYPE__ int32_t;66typedef __UINT32_TYPE__ uint32_t;67typedef __INT64_TYPE__ int64_t;68typedef __UINT64_TYPE__ uint64_t;69typedef __INTPTR_TYPE__ intptr_t;70typedef __UINTPTR_TYPE__ uintptr_t;71typedef __SIZE_TYPE__ size_t;72typedef __PTRDIFF_TYPE__ ptrdiff_t;73typedef unsigned short unichar;74extern "C"75{76int printf(const char * __restrict, ...);77}78)";7980const char *ClangExpressionSourceCode::g_expression_suffix =81"\n;\n#line 1 \"" SUFFIX_NAME "\"\n";8283namespace {8485class AddMacroState {86enum State {87CURRENT_FILE_NOT_YET_PUSHED,88CURRENT_FILE_PUSHED,89CURRENT_FILE_POPPED90};9192public:93AddMacroState(const FileSpec ¤t_file, const uint32_t current_file_line)94: m_current_file(current_file), m_current_file_line(current_file_line) {}9596void StartFile(const FileSpec &file) {97m_file_stack.push_back(file);98if (file == m_current_file)99m_state = CURRENT_FILE_PUSHED;100}101102void EndFile() {103if (m_file_stack.size() == 0)104return;105106FileSpec old_top = m_file_stack.back();107m_file_stack.pop_back();108if (old_top == m_current_file)109m_state = CURRENT_FILE_POPPED;110}111112// An entry is valid if it occurs before the current line in the current113// file.114bool IsValidEntry(uint32_t line) {115switch (m_state) {116case CURRENT_FILE_NOT_YET_PUSHED:117return true;118case CURRENT_FILE_PUSHED:119// If we are in file included in the current file, the entry should be120// added.121if (m_file_stack.back() != m_current_file)122return true;123124return line < m_current_file_line;125default:126return false;127}128}129130private:131std::vector<FileSpec> m_file_stack;132State m_state = CURRENT_FILE_NOT_YET_PUSHED;133FileSpec m_current_file;134uint32_t m_current_file_line;135};136137} // anonymous namespace138139static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,140AddMacroState &state, StreamString &stream) {141if (dm == nullptr)142return;143144// The macros directives below can potentially redefine builtin macros of the145// Clang instance which parses the user expression. The Clang diagnostics146// caused by this are not useful for the user as the source code here is147// generated by LLDB.148stream << "#pragma clang diagnostic push\n";149stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";150stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";151auto pop_warning = llvm::make_scope_exit([&stream](){152stream << "#pragma clang diagnostic pop\n";153});154155for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {156const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);157uint32_t line;158159switch (entry.GetType()) {160case DebugMacroEntry::DEFINE:161if (state.IsValidEntry(entry.GetLineNumber()))162stream.Printf("#define %s\n", entry.GetMacroString().AsCString());163else164return;165break;166case DebugMacroEntry::UNDEF:167if (state.IsValidEntry(entry.GetLineNumber()))168stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());169else170return;171break;172case DebugMacroEntry::START_FILE:173line = entry.GetLineNumber();174if (state.IsValidEntry(line))175state.StartFile(entry.GetFileSpec(comp_unit));176else177return;178break;179case DebugMacroEntry::END_FILE:180state.EndFile();181break;182case DebugMacroEntry::INDIRECT:183AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);184break;185default:186// This is an unknown/invalid entry. Ignore.187break;188}189}190}191192lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(193llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,194llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)195: ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {196// Use #line markers to pretend that we have a single-line source file197// containing only the user expression. This will hide our wrapper code198// from the user when we render diagnostics with Clang.199m_start_marker = "#line 1 \"" + filename.str() + "\"\n";200m_end_marker = g_expression_suffix;201}202203namespace {204/// Allows checking if a token is contained in a given expression.205class TokenVerifier {206/// The tokens we found in the expression.207llvm::StringSet<> m_tokens;208209public:210TokenVerifier(std::string body);211/// Returns true iff the given expression body contained a token with the212/// given content.213bool hasToken(llvm::StringRef token) const {214return m_tokens.contains(token);215}216};217218// If we're evaluating from inside a lambda that captures a 'this' pointer,219// add a "using" declaration to 'stream' for each capture used in the220// expression (tokenized by 'verifier').221//222// If no 'this' capture exists, generate no using declarations. Instead223// capture lookups will get resolved by the same mechanism as class member224// variable lookup. That's because Clang generates an unnamed structure225// representing the lambda closure whose members are the captured variables.226void AddLambdaCaptureDecls(StreamString &stream, StackFrame *frame,227TokenVerifier const &verifier) {228assert(frame);229230if (auto thisValSP = ClangExpressionUtil::GetLambdaValueObject(frame)) {231uint32_t numChildren = thisValSP->GetNumChildrenIgnoringErrors();232for (uint32_t i = 0; i < numChildren; ++i) {233auto childVal = thisValSP->GetChildAtIndex(i);234ConstString childName(childVal ? childVal->GetName() : ConstString(""));235236if (!childName.IsEmpty() && verifier.hasToken(childName.GetStringRef()) &&237childName != "this") {238stream.Printf("using $__lldb_local_vars::%s;\n",239childName.GetCString());240}241}242}243}244245} // namespace246247TokenVerifier::TokenVerifier(std::string body) {248using namespace clang;249250// We only care about tokens and not their original source locations. If we251// move the whole expression to only be in one line we can simplify the252// following code that extracts the token contents.253std::replace(body.begin(), body.end(), '\n', ' ');254std::replace(body.begin(), body.end(), '\r', ' ');255256FileSystemOptions file_opts;257FileManager file_mgr(file_opts,258FileSystem::Instance().GetVirtualFileSystem());259260// Let's build the actual source code Clang needs and setup some utility261// objects.262llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());263llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(264new DiagnosticOptions());265DiagnosticsEngine diags(diag_ids, diags_opts);266clang::SourceManager SM(diags, file_mgr);267auto buf = llvm::MemoryBuffer::getMemBuffer(body);268269FileID FID = SM.createFileID(buf->getMemBufferRef());270271// Let's just enable the latest ObjC and C++ which should get most tokens272// right.273LangOptions Opts;274Opts.ObjC = true;275Opts.DollarIdents = true;276Opts.CPlusPlus20 = true;277Opts.LineComment = true;278279Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);280281Token token;282bool exit = false;283while (!exit) {284// Returns true if this is the last token we get from the lexer.285exit = lex.LexFromRawLexer(token);286287// Extract the column number which we need to extract the token content.288// Our expression is just one line, so we don't need to handle any line289// numbers here.290bool invalid = false;291unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);292if (invalid)293continue;294// Column numbers start at 1, but indexes in our string start at 0.295--start;296297// Annotations don't have a length, so let's skip them.298if (token.isAnnotation())299continue;300301// Extract the token string from our source code and store it.302std::string token_str = body.substr(start, token.getLength());303if (token_str.empty())304continue;305m_tokens.insert(token_str);306}307}308309void ClangExpressionSourceCode::AddLocalVariableDecls(StreamString &stream,310const std::string &expr,311StackFrame *frame) const {312assert(frame);313TokenVerifier tokens(expr);314315lldb::VariableListSP var_list_sp = frame->GetInScopeVariableList(false, true);316317for (size_t i = 0; i < var_list_sp->GetSize(); i++) {318lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);319320ConstString var_name = var_sp->GetName();321322if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction) {323AddLambdaCaptureDecls(stream, frame, tokens);324325continue;326}327328// We can check for .block_descriptor w/o checking for langauge since this329// is not a valid identifier in either C or C++.330if (!var_name || var_name == ".block_descriptor")331continue;332333if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))334continue;335336const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||337m_wrap_kind == WrapKind::ObjCStaticMethod;338if ((var_name == "self" || var_name == "_cmd") && is_objc)339continue;340341stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());342}343}344345bool ClangExpressionSourceCode::GetText(346std::string &text, ExecutionContext &exe_ctx, bool add_locals,347bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {348const char *target_specific_defines = "typedef signed char BOOL;\n";349std::string module_macros;350llvm::raw_string_ostream module_macros_stream(module_macros);351352Target *target = exe_ctx.GetTargetPtr();353if (target) {354if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||355target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {356target_specific_defines = "typedef bool BOOL;\n";357}358if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {359if (lldb::PlatformSP platform_sp = target->GetPlatform()) {360if (platform_sp->GetPluginName() == "ios-simulator") {361target_specific_defines = "typedef bool BOOL;\n";362}363}364}365366auto *persistent_vars = llvm::cast<ClangPersistentVariables>(367target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));368std::shared_ptr<ClangModulesDeclVendor> decl_vendor =369persistent_vars->GetClangModulesDeclVendor();370if (decl_vendor) {371const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =372persistent_vars->GetHandLoadedClangModules();373ClangModulesDeclVendor::ModuleVector modules_for_macros;374375for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {376modules_for_macros.push_back(module);377}378379if (target->GetEnableAutoImportClangModules()) {380if (StackFrame *frame = exe_ctx.GetFramePtr()) {381if (Block *block = frame->GetFrameBlock()) {382SymbolContext sc;383384block->CalculateSymbolContext(&sc);385386if (sc.comp_unit) {387StreamString error_stream;388389decl_vendor->AddModulesForCompileUnit(390*sc.comp_unit, modules_for_macros, error_stream);391}392}393}394}395396decl_vendor->ForEachMacro(397modules_for_macros,398[&module_macros_stream](llvm::StringRef token,399llvm::StringRef expansion) -> bool {400// Check if the macro hasn't already been defined in the401// g_expression_prefix (which defines a few builtin macros).402module_macros_stream << "#ifndef " << token << "\n";403module_macros_stream << expansion << "\n";404module_macros_stream << "#endif\n";405return false;406});407}408}409410StreamString debug_macros_stream;411StreamString lldb_local_var_decls;412if (StackFrame *frame = exe_ctx.GetFramePtr()) {413const SymbolContext &sc = frame->GetSymbolContext(414lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);415416if (sc.comp_unit && sc.line_entry.IsValid()) {417DebugMacros *dm = sc.comp_unit->GetDebugMacros();418if (dm) {419AddMacroState state(sc.line_entry.GetFile(), sc.line_entry.line);420AddMacros(dm, sc.comp_unit, state, debug_macros_stream);421}422}423424if (add_locals)425if (target->GetInjectLocalVariables(&exe_ctx)) {426AddLocalVariableDecls(lldb_local_var_decls,427force_add_all_locals ? "" : m_body, frame);428}429}430431if (m_wrap) {432// Generate a list of @import statements that will import the specified433// module into our expression.434std::string module_imports;435for (const std::string &module : modules) {436module_imports.append("@import ");437module_imports.append(module);438module_imports.append(";\n");439}440441StreamString wrap_stream;442443wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,444module_macros.c_str(), debug_macros_stream.GetData(),445target_specific_defines, m_prefix.c_str());446447// First construct a tagged form of the user expression so we can find it448// later:449std::string tagged_body;450tagged_body.append(m_start_marker);451tagged_body.append(m_body);452tagged_body.append(m_end_marker);453454switch (m_wrap_kind) {455case WrapKind::Function:456wrap_stream.Printf("%s"457"void \n"458"%s(void *$__lldb_arg) \n"459"{ \n"460" %s; \n"461"%s"462"} \n",463module_imports.c_str(), m_name.c_str(),464lldb_local_var_decls.GetData(), tagged_body.c_str());465break;466case WrapKind::CppMemberFunction:467wrap_stream.Printf("%s"468"void \n"469"$__lldb_class::%s(void *$__lldb_arg) \n"470"{ \n"471" %s; \n"472"%s"473"} \n",474module_imports.c_str(), m_name.c_str(),475lldb_local_var_decls.GetData(), tagged_body.c_str());476break;477case WrapKind::ObjCInstanceMethod:478wrap_stream.Printf(479"%s"480"@interface $__lldb_objc_class ($__lldb_category) \n"481"-(void)%s:(void *)$__lldb_arg; \n"482"@end \n"483"@implementation $__lldb_objc_class ($__lldb_category) \n"484"-(void)%s:(void *)$__lldb_arg \n"485"{ \n"486" %s; \n"487"%s"488"} \n"489"@end \n",490module_imports.c_str(), m_name.c_str(), m_name.c_str(),491lldb_local_var_decls.GetData(), tagged_body.c_str());492break;493494case WrapKind::ObjCStaticMethod:495wrap_stream.Printf(496"%s"497"@interface $__lldb_objc_class ($__lldb_category) \n"498"+(void)%s:(void *)$__lldb_arg; \n"499"@end \n"500"@implementation $__lldb_objc_class ($__lldb_category) \n"501"+(void)%s:(void *)$__lldb_arg \n"502"{ \n"503" %s; \n"504"%s"505"} \n"506"@end \n",507module_imports.c_str(), m_name.c_str(), m_name.c_str(),508lldb_local_var_decls.GetData(), tagged_body.c_str());509break;510}511512text = std::string(wrap_stream.GetString());513} else {514text.append(m_body);515}516517return true;518}519520bool ClangExpressionSourceCode::GetOriginalBodyBounds(521std::string transformed_text, size_t &start_loc, size_t &end_loc) {522start_loc = transformed_text.find(m_start_marker);523if (start_loc == std::string::npos)524return false;525start_loc += m_start_marker.size();526end_loc = transformed_text.find(m_end_marker);527return end_loc != std::string::npos;528}529530531