blob: 88cfe5f0819197a0bb540c37093945a4bd4f7788 [file] [log] [blame]
Jim Inghamea401ec2019-03-06 22:43:25 +00001//===-- ClangExpressionSourceCode.cpp ---------------------------*- C++ -*-===//
Sean Callanan9bc83842011-09-26 18:45:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan9bc83842011-09-26 18:45:31 +00006//
7//===----------------------------------------------------------------------===//
8
Jim Inghamea401ec2019-03-06 22:43:25 +00009#include "ClangExpressionSourceCode.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000010
Raphael Isemann71569d02019-05-02 10:12:56 +000011#include "clang/Basic/CharInfo.h"
Raphael Isemann01718662019-07-10 21:04:01 +000012#include "clang/Basic/SourceManager.h"
13#include "clang/Lex/Lexer.h"
Raphael Isemann71569d02019-05-02 10:12:56 +000014#include "llvm/ADT/StringRef.h"
15
Sean Callanan4dbb2712015-09-25 20:35:58 +000016#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
17#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Symbol/Block.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000019#include "lldb/Symbol/CompileUnit.h"
20#include "lldb/Symbol/DebugMacros.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000021#include "lldb/Symbol/TypeSystem.h"
Siva Chandra03ff5c82016-02-05 19:10:04 +000022#include "lldb/Symbol/VariableList.h"
Jason Molendaa3329782014-03-29 18:54:20 +000023#include "lldb/Target/ExecutionContext.h"
Siva Chandra03ff5c82016-02-05 19:10:04 +000024#include "lldb/Target/Language.h"
Jason Molendaa3329782014-03-29 18:54:20 +000025#include "lldb/Target/Platform.h"
Sean Callanan68f85e72015-04-30 21:49:58 +000026#include "lldb/Target/StackFrame.h"
Jason Molendaa3329782014-03-29 18:54:20 +000027#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/StreamString.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000029
30using namespace lldb_private;
31
Jim Inghamea401ec2019-03-06 22:43:25 +000032const char *ClangExpressionSourceCode::g_expression_prefix = R"(
Raphael Isemanne21fc872019-07-18 17:58:04 +000033#ifndef offsetof
34#define offsetof(t, d) __builtin_offsetof(t, d)
35#endif
Sean Callanan68f85e72015-04-30 21:49:58 +000036#ifndef NULL
Greg Clayton399107a2013-02-13 23:57:48 +000037#define NULL (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000038#endif
39#ifndef Nil
Greg Clayton399107a2013-02-13 23:57:48 +000040#define Nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000041#endif
42#ifndef nil
Greg Clayton399107a2013-02-13 23:57:48 +000043#define nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000044#endif
45#ifndef YES
Greg Clayton399107a2013-02-13 23:57:48 +000046#define YES ((BOOL)1)
Sean Callanan68f85e72015-04-30 21:49:58 +000047#endif
48#ifndef NO
Greg Clayton399107a2013-02-13 23:57:48 +000049#define NO ((BOOL)0)
Sean Callanan68f85e72015-04-30 21:49:58 +000050#endif
Reid Kleckner56d861f2014-07-29 21:59:33 +000051typedef __INT8_TYPE__ int8_t;
52typedef __UINT8_TYPE__ uint8_t;
53typedef __INT16_TYPE__ int16_t;
54typedef __UINT16_TYPE__ uint16_t;
55typedef __INT32_TYPE__ int32_t;
56typedef __UINT32_TYPE__ uint32_t;
57typedef __INT64_TYPE__ int64_t;
58typedef __UINT64_TYPE__ uint64_t;
59typedef __INTPTR_TYPE__ intptr_t;
60typedef __UINTPTR_TYPE__ uintptr_t;
Greg Clayton399107a2013-02-13 23:57:48 +000061typedef __SIZE_TYPE__ size_t;
62typedef __PTRDIFF_TYPE__ ptrdiff_t;
63typedef unsigned short unichar;
Kate Stonecc391a02015-01-21 23:39:16 +000064extern "C"
65{
66 int printf(const char * __restrict, ...);
67}
Greg Clayton399107a2013-02-13 23:57:48 +000068)";
Sean Callanana199e822012-09-13 23:35:30 +000069
Jim Inghame5ee6f02016-03-29 22:00:08 +000070static const char *c_start_marker = " /*LLDB_BODY_START*/\n ";
Kate Stoneb9c1b512016-09-06 20:57:50 +000071static const char *c_end_marker = ";\n /*LLDB_BODY_END*/\n";
Jim Inghame5ee6f02016-03-29 22:00:08 +000072
Siva Chandrad8335e92015-12-16 00:22:08 +000073namespace {
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075class AddMacroState {
76 enum State {
77 CURRENT_FILE_NOT_YET_PUSHED,
78 CURRENT_FILE_PUSHED,
79 CURRENT_FILE_POPPED
80 };
Siva Chandrad8335e92015-12-16 00:22:08 +000081
82public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
84 : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
85 m_current_file_line(current_file_line) {}
Siva Chandrad8335e92015-12-16 00:22:08 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 void StartFile(const FileSpec &file) {
88 m_file_stack.push_back(file);
89 if (file == m_current_file)
90 m_state = CURRENT_FILE_PUSHED;
91 }
92
93 void EndFile() {
94 if (m_file_stack.size() == 0)
95 return;
96
97 FileSpec old_top = m_file_stack.back();
98 m_file_stack.pop_back();
99 if (old_top == m_current_file)
100 m_state = CURRENT_FILE_POPPED;
101 }
102
Adrian Prantl05097242018-04-30 16:49:04 +0000103 // An entry is valid if it occurs before the current line in the current
104 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 bool IsValidEntry(uint32_t line) {
106 switch (m_state) {
107 case CURRENT_FILE_NOT_YET_PUSHED:
108 return true;
109 case CURRENT_FILE_PUSHED:
Adrian Prantl05097242018-04-30 16:49:04 +0000110 // If we are in file included in the current file, the entry should be
111 // added.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (m_file_stack.back() != m_current_file)
113 return true;
114
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000115 return line < m_current_file_line;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 default:
117 return false;
Siva Chandrad8335e92015-12-16 00:22:08 +0000118 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 }
Siva Chandrad8335e92015-12-16 00:22:08 +0000120
121private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000122 std::vector<FileSpec> m_file_stack;
123 State m_state;
124 FileSpec m_current_file;
125 uint32_t m_current_file_line;
Siva Chandrad8335e92015-12-16 00:22:08 +0000126};
127
128} // anonymous namespace
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
131 AddMacroState &state, StreamString &stream) {
132 if (dm == nullptr)
133 return;
134
135 for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
136 const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
137 uint32_t line;
138
139 switch (entry.GetType()) {
140 case DebugMacroEntry::DEFINE:
141 if (state.IsValidEntry(entry.GetLineNumber()))
142 stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
143 else
Siva Chandrad8335e92015-12-16 00:22:08 +0000144 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 break;
146 case DebugMacroEntry::UNDEF:
147 if (state.IsValidEntry(entry.GetLineNumber()))
148 stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
149 else
150 return;
151 break;
152 case DebugMacroEntry::START_FILE:
153 line = entry.GetLineNumber();
154 if (state.IsValidEntry(line))
155 state.StartFile(entry.GetFileSpec(comp_unit));
156 else
157 return;
158 break;
159 case DebugMacroEntry::END_FILE:
160 state.EndFile();
161 break;
162 case DebugMacroEntry::INDIRECT:
163 AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
164 break;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000165 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 // This is an unknown/invalid entry. Ignore.
167 break;
168 }
169 }
170}
171
Raphael Isemann01718662019-07-10 21:04:01 +0000172namespace {
173/// Allows checking if a token is contained in a given expression.
174class TokenVerifier {
175 /// The tokens we found in the expression.
176 llvm::StringSet<> m_tokens;
Raphael Isemann71569d02019-05-02 10:12:56 +0000177
Raphael Isemann01718662019-07-10 21:04:01 +0000178public:
179 TokenVerifier(std::string body);
180 /// Returns true iff the given expression body contained a token with the
181 /// given content.
182 bool hasToken(llvm::StringRef token) const {
183 return m_tokens.find(token) != m_tokens.end();
Raphael Isemann71569d02019-05-02 10:12:56 +0000184 }
Raphael Isemann01718662019-07-10 21:04:01 +0000185};
186} // namespace
187
188TokenVerifier::TokenVerifier(std::string body) {
189 using namespace clang;
190
191 // We only care about tokens and not their original source locations. If we
192 // move the whole expression to only be in one line we can simplify the
193 // following code that extracts the token contents.
194 std::replace(body.begin(), body.end(), '\n', ' ');
195 std::replace(body.begin(), body.end(), '\r', ' ');
196
197 FileSystemOptions file_opts;
198 FileManager file_mgr(file_opts,
199 FileSystem::Instance().GetVirtualFileSystem());
200
201 // Let's build the actual source code Clang needs and setup some utility
202 // objects.
203 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
204 llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(
205 new DiagnosticOptions());
206 DiagnosticsEngine diags(diag_ids, diags_opts);
207 clang::SourceManager SM(diags, file_mgr);
208 auto buf = llvm::MemoryBuffer::getMemBuffer(body);
209
210 FileID FID = SM.createFileID(clang::SourceManager::Unowned, buf.get());
211
212 // Let's just enable the latest ObjC and C++ which should get most tokens
213 // right.
214 LangOptions Opts;
215 Opts.ObjC = true;
216 Opts.DollarIdents = true;
217 Opts.CPlusPlus17 = true;
218 Opts.LineComment = true;
219
220 Lexer lex(FID, buf.get(), SM, Opts);
221
222 Token token;
223 bool exit = false;
224 while (!exit) {
225 // Returns true if this is the last token we get from the lexer.
226 exit = lex.LexFromRawLexer(token);
227
228 // Extract the column number which we need to extract the token content.
229 // Our expression is just one line, so we don't need to handle any line
230 // numbers here.
231 bool invalid = false;
232 unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
233 if (invalid)
234 continue;
235 // Column numbers start at 1, but indexes in our string start at 0.
236 --start;
237
238 // Annotations don't have a length, so let's skip them.
239 if (token.isAnnotation())
240 continue;
241
242 // Extract the token string from our source code and store it.
243 std::string token_str = body.substr(start, token.getLength());
244 if (token_str.empty())
245 continue;
246 m_tokens.insert(token_str);
247 }
Raphael Isemann71569d02019-05-02 10:12:56 +0000248}
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
Raphael Isemann71569d02019-05-02 10:12:56 +0000251 StreamString &stream,
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000252 const std::string &expr,
253 lldb::LanguageType wrapping_language) {
Raphael Isemann01718662019-07-10 21:04:01 +0000254 TokenVerifier tokens(expr);
255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
257 lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
258
259 ConstString var_name = var_sp->GetName();
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000260
261
262 // We can check for .block_descriptor w/o checking for langauge since this
263 // is not a valid identifier in either C or C++.
Raphael Isemann090a5b22019-05-03 21:01:45 +0000264 if (!var_name || var_name == ".block_descriptor")
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265 continue;
266
Raphael Isemann01718662019-07-10 21:04:01 +0000267 if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
Raphael Isemann71569d02019-05-02 10:12:56 +0000268 continue;
269
Raphael Isemann090a5b22019-05-03 21:01:45 +0000270 if ((var_name == "self" || var_name == "_cmd") &&
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000271 (wrapping_language == lldb::eLanguageTypeObjC ||
272 wrapping_language == lldb::eLanguageTypeObjC_plus_plus))
273 continue;
274
Raphael Isemann090a5b22019-05-03 21:01:45 +0000275 if (var_name == "this" &&
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000276 wrapping_language == lldb::eLanguageTypeC_plus_plus)
277 continue;
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
280 }
281}
282
Raphael Isemann71569d02019-05-02 10:12:56 +0000283bool ClangExpressionSourceCode::GetText(
284 std::string &text, lldb::LanguageType wrapping_language, bool static_method,
285 ExecutionContext &exe_ctx, bool add_locals, bool force_add_all_locals,
286 llvm::ArrayRef<std::string> modules) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287 const char *target_specific_defines = "typedef signed char BOOL;\n";
288 std::string module_macros;
289
290 Target *target = exe_ctx.GetTargetPtr();
291 if (target) {
292 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) {
293 target_specific_defines = "typedef bool BOOL;\n";
294 }
295 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
296 if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
297 static ConstString g_platform_ios_simulator("ios-simulator");
298 if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
299 target_specific_defines = "typedef bool BOOL;\n";
300 }
301 }
302 }
303
304 if (ClangModulesDeclVendor *decl_vendor =
305 target->GetClangModulesDeclVendor()) {
306 ClangPersistentVariables *persistent_vars =
307 llvm::cast<ClangPersistentVariables>(
308 target->GetPersistentExpressionStateForLanguage(
309 lldb::eLanguageTypeC));
310 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
311 persistent_vars->GetHandLoadedClangModules();
312 ClangModulesDeclVendor::ModuleVector modules_for_macros;
313
314 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
315 modules_for_macros.push_back(module);
316 }
317
318 if (target->GetEnableAutoImportClangModules()) {
319 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
320 if (Block *block = frame->GetFrameBlock()) {
321 SymbolContext sc;
322
323 block->CalculateSymbolContext(&sc);
324
325 if (sc.comp_unit) {
326 StreamString error_stream;
327
328 decl_vendor->AddModulesForCompileUnit(
329 *sc.comp_unit, modules_for_macros, error_stream);
330 }
331 }
332 }
333 }
334
335 decl_vendor->ForEachMacro(
336 modules_for_macros,
337 [&module_macros](const std::string &expansion) -> bool {
338 module_macros.append(expansion);
339 module_macros.append("\n");
340 return false;
341 });
342 }
343 }
344
345 StreamString debug_macros_stream;
346 StreamString lldb_local_var_decls;
347 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
348 const SymbolContext &sc = frame->GetSymbolContext(
349 lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
350
351 if (sc.comp_unit && sc.line_entry.IsValid()) {
352 DebugMacros *dm = sc.comp_unit->GetDebugMacros();
353 if (dm) {
354 AddMacroState state(sc.line_entry.file, sc.line_entry.line);
355 AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
356 }
357 }
358
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000359 if (add_locals)
360 if (target->GetInjectLocalVariables(&exe_ctx)) {
361 lldb::VariableListSP var_list_sp =
362 frame->GetInScopeVariableList(false, true);
363 AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
364 force_add_all_locals ? "" : m_body,
365 wrapping_language);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 }
368
369 if (m_wrap) {
370 switch (wrapping_language) {
371 default:
372 return false;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000373 case lldb::eLanguageTypeC:
374 case lldb::eLanguageTypeC_plus_plus:
375 case lldb::eLanguageTypeObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 break;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000377 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000379 // Generate a list of @import statements that will import the specified
380 // module into our expression.
381 std::string module_imports;
382 for (const std::string &module : modules) {
383 module_imports.append("@import ");
384 module_imports.append(module);
385 module_imports.append(";\n");
386 }
387
Kate Stoneb9c1b512016-09-06 20:57:50 +0000388 StreamString wrap_stream;
389
390 wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
391 debug_macros_stream.GetData(), g_expression_prefix,
392 target_specific_defines, m_prefix.c_str());
393
394 // First construct a tagged form of the user expression so we can find it
395 // later:
396 std::string tagged_body;
397 switch (wrapping_language) {
398 default:
399 tagged_body = m_body;
400 break;
401 case lldb::eLanguageTypeC:
402 case lldb::eLanguageTypeC_plus_plus:
403 case lldb::eLanguageTypeObjC:
404 tagged_body.append(c_start_marker);
405 tagged_body.append(m_body);
406 tagged_body.append(c_end_marker);
407 break;
408 }
409 switch (wrapping_language) {
410 default:
411 break;
412 case lldb::eLanguageTypeC:
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000413 wrap_stream.Printf("%s"
414 "void \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415 "%s(void *$__lldb_arg) \n"
416 "{ \n"
417 " %s; \n"
418 "%s"
419 "} \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000420 module_imports.c_str(), m_name.c_str(),
421 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 break;
423 case lldb::eLanguageTypeC_plus_plus:
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000424 wrap_stream.Printf("%s"
425 "void \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 "$__lldb_class::%s(void *$__lldb_arg) \n"
427 "{ \n"
428 " %s; \n"
429 "%s"
430 "} \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000431 module_imports.c_str(), m_name.c_str(),
432 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 break;
434 case lldb::eLanguageTypeObjC:
435 if (static_method) {
436 wrap_stream.Printf(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000437 "%s"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 "@interface $__lldb_objc_class ($__lldb_category) \n"
439 "+(void)%s:(void *)$__lldb_arg; \n"
440 "@end \n"
441 "@implementation $__lldb_objc_class ($__lldb_category) \n"
442 "+(void)%s:(void *)$__lldb_arg \n"
443 "{ \n"
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000444 " %s; \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 "%s"
446 "} \n"
447 "@end \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000448 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000449 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 } else {
451 wrap_stream.Printf(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000452 "%s"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 "@interface $__lldb_objc_class ($__lldb_category) \n"
454 "-(void)%s:(void *)$__lldb_arg; \n"
455 "@end \n"
456 "@implementation $__lldb_objc_class ($__lldb_category) \n"
457 "-(void)%s:(void *)$__lldb_arg \n"
458 "{ \n"
Raphael Isemann090a5b22019-05-03 21:01:45 +0000459 " %s; \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 "%s"
461 "} \n"
462 "@end \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000463 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
Shafik Yaghmoure5cbe782019-05-03 19:59:22 +0000464 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 }
466 break;
467 }
468
469 text = wrap_stream.GetString();
470 } else {
471 text.append(m_body);
472 }
473
474 return true;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000475}
476
Jim Inghamea401ec2019-03-06 22:43:25 +0000477bool ClangExpressionSourceCode::GetOriginalBodyBounds(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 std::string transformed_text, lldb::LanguageType wrapping_language,
479 size_t &start_loc, size_t &end_loc) {
480 const char *start_marker;
481 const char *end_marker;
482
483 switch (wrapping_language) {
484 default:
485 return false;
486 case lldb::eLanguageTypeC:
487 case lldb::eLanguageTypeC_plus_plus:
488 case lldb::eLanguageTypeObjC:
489 start_marker = c_start_marker;
490 end_marker = c_end_marker;
491 break;
492 }
493
494 start_loc = transformed_text.find(start_marker);
495 if (start_loc == std::string::npos)
496 return false;
497 start_loc += strlen(start_marker);
498 end_loc = transformed_text.find(end_marker);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000499 return end_loc != std::string::npos;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500}