Coverage Report

Created: 2023-05-31 04:38

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ClangExpressionSourceCode.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 "ClangExpressionSourceCode.h"
10
11
#include "ClangExpressionUtil.h"
12
13
#include "clang/Basic/CharInfo.h"
14
#include "clang/Basic/FileManager.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Lex/Lexer.h"
17
#include "llvm/ADT/ScopeExit.h"
18
#include "llvm/ADT/StringRef.h"
19
20
#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
21
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
22
#include "lldb/Symbol/Block.h"
23
#include "lldb/Symbol/CompileUnit.h"
24
#include "lldb/Symbol/DebugMacros.h"
25
#include "lldb/Symbol/TypeSystem.h"
26
#include "lldb/Symbol/VariableList.h"
27
#include "lldb/Target/ExecutionContext.h"
28
#include "lldb/Target/Language.h"
29
#include "lldb/Target/Platform.h"
30
#include "lldb/Target/StackFrame.h"
31
#include "lldb/Target/Target.h"
32
#include "lldb/Utility/StreamString.h"
33
#include "lldb/lldb-forward.h"
34
35
using namespace lldb_private;
36
37
#define PREFIX_NAME "<lldb wrapper prefix>"
38
#define SUFFIX_NAME "<lldb wrapper suffix>"
39
40
const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;
41
42
const char *ClangExpressionSourceCode::g_expression_prefix =
43
"#line 1 \"" PREFIX_NAME R"("
44
#ifndef offsetof
45
#define offsetof(t, d) __builtin_offsetof(t, d)
46
#endif
47
#ifndef NULL
48
#define NULL (__null)
49
#endif
50
#ifndef Nil
51
#define Nil (__null)
52
#endif
53
#ifndef nil
54
#define nil (__null)
55
#endif
56
#ifndef YES
57
#define YES ((BOOL)1)
58
#endif
59
#ifndef NO
60
#define NO ((BOOL)0)
61
#endif
62
typedef __INT8_TYPE__ int8_t;
63
typedef __UINT8_TYPE__ uint8_t;
64
typedef __INT16_TYPE__ int16_t;
65
typedef __UINT16_TYPE__ uint16_t;
66
typedef __INT32_TYPE__ int32_t;
67
typedef __UINT32_TYPE__ uint32_t;
68
typedef __INT64_TYPE__ int64_t;
69
typedef __UINT64_TYPE__ uint64_t;
70
typedef __INTPTR_TYPE__ intptr_t;
71
typedef __UINTPTR_TYPE__ uintptr_t;
72
typedef __SIZE_TYPE__ size_t;
73
typedef __PTRDIFF_TYPE__ ptrdiff_t;
74
typedef unsigned short unichar;
75
extern "C"
76
{
77
    int printf(const char * __restrict, ...);
78
}
79
)";
80
81
const char *ClangExpressionSourceCode::g_expression_suffix =
82
    "\n;\n#line 1 \"" SUFFIX_NAME "\"\n";
83
84
namespace {
85
86
class AddMacroState {
87
  enum State {
88
    CURRENT_FILE_NOT_YET_PUSHED,
89
    CURRENT_FILE_PUSHED,
90
    CURRENT_FILE_POPPED
91
  };
92
93
public:
94
  AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
95
0
      : m_current_file(current_file), m_current_file_line(current_file_line) {}
96
97
0
  void StartFile(const FileSpec &file) {
98
0
    m_file_stack.push_back(file);
99
0
    if (file == m_current_file)
100
0
      m_state = CURRENT_FILE_PUSHED;
101
0
  }
102
103
0
  void EndFile() {
104
0
    if (m_file_stack.size() == 0)
105
0
      return;
106
107
0
    FileSpec old_top = m_file_stack.back();
108
0
    m_file_stack.pop_back();
109
0
    if (old_top == m_current_file)
110
0
      m_state = CURRENT_FILE_POPPED;
111
0
  }
112
113
  // An entry is valid if it occurs before the current line in the current
114
  // file.
115
0
  bool IsValidEntry(uint32_t line) {
116
0
    switch (m_state) {
117
0
    case CURRENT_FILE_NOT_YET_PUSHED:
118
0
      return true;
119
0
    case CURRENT_FILE_PUSHED:
120
      // If we are in file included in the current file, the entry should be
121
      // added.
122
0
      if (m_file_stack.back() != m_current_file)
123
0
        return true;
124
125
0
      return line < m_current_file_line;
126
0
    default:
127
0
      return false;
128
0
    }
129
0
  }
130
131
private:
132
  std::vector<FileSpec> m_file_stack;
133
  State m_state = CURRENT_FILE_NOT_YET_PUSHED;
134
  FileSpec m_current_file;
135
  uint32_t m_current_file_line;
136
};
137
138
} // anonymous namespace
139
140
static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
141
0
                      AddMacroState &state, StreamString &stream) {
142
0
  if (dm == nullptr)
143
0
    return;
144
145
  // The macros directives below can potentially redefine builtin macros of the
146
  // Clang instance which parses the user expression. The Clang diagnostics
147
  // caused by this are not useful for the user as the source code here is
148
  // generated by LLDB.
149
0
  stream << "#pragma clang diagnostic push\n";
150
0
  stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";
151
0
  stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";
152
0
  auto pop_warning = llvm::make_scope_exit([&stream](){
153
0
    stream << "#pragma clang diagnostic pop\n";
154
0
  });
155
156
0
  for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
157
0
    const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
158
0
    uint32_t line;
159
160
0
    switch (entry.GetType()) {
161
0
    case DebugMacroEntry::DEFINE:
162
0
      if (state.IsValidEntry(entry.GetLineNumber()))
163
0
        stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
164
0
      else
165
0
        return;
166
0
      break;
167
0
    case DebugMacroEntry::UNDEF:
168
0
      if (state.IsValidEntry(entry.GetLineNumber()))
169
0
        stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
170
0
      else
171
0
        return;
172
0
      break;
173
0
    case DebugMacroEntry::START_FILE:
174
0
      line = entry.GetLineNumber();
175
0
      if (state.IsValidEntry(line))
176
0
        state.StartFile(entry.GetFileSpec(comp_unit));
177
0
      else
178
0
        return;
179
0
      break;
180
0
    case DebugMacroEntry::END_FILE:
181
0
      state.EndFile();
182
0
      break;
183
0
    case DebugMacroEntry::INDIRECT:
184
0
      AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
185
0
      break;
186
0
    default:
187
      // This is an unknown/invalid entry. Ignore.
188
0
      break;
189
0
    }
190
0
  }
191
0
}
192
193
lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(
194
    llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
195
    llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
196
7.26k
    : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
197
  // Use #line markers to pretend that we have a single-line source file
198
  // containing only the user expression. This will hide our wrapper code
199
  // from the user when we render diagnostics with Clang.
200
7.26k
  m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
201
7.26k
  m_end_marker = g_expression_suffix;
202
7.26k
}
203
204
namespace {
205
/// Allows checking if a token is contained in a given expression.
206
class TokenVerifier {
207
  /// The tokens we found in the expression.
208
  llvm::StringSet<> m_tokens;
209
210
public:
211
  TokenVerifier(std::string body);
212
  /// Returns true iff the given expression body contained a token with the
213
  /// given content.
214
80.9k
  bool hasToken(llvm::StringRef token) const {
215
80.9k
    return m_tokens.contains(token);
216
80.9k
  }
217
};
218
219
// If we're evaluating from inside a lambda that captures a 'this' pointer,
220
// add a "using" declaration to 'stream' for each capture used in the
221
// expression (tokenized by 'verifier').
222
//
223
// If no 'this' capture exists, generate no using declarations. Instead
224
// capture lookups will get resolved by the same mechanism as class member
225
// variable lookup. That's because Clang generates an unnamed structure
226
// representing the lambda closure whose members are the captured variables.
227
void AddLambdaCaptureDecls(StreamString &stream, StackFrame *frame,
228
154
                           TokenVerifier const &verifier) {
229
154
  assert(frame);
230
231
154
  if (auto thisValSP = ClangExpressionUtil::GetLambdaValueObject(frame)) {
232
63
    uint32_t numChildren = thisValSP->GetNumChildren();
233
253
    for (uint32_t i = 0; i < numChildren; 
++i190
) {
234
190
      auto childVal = thisValSP->GetChildAtIndex(i, true);
235
190
      ConstString childName(childVal ? childVal->GetName() : 
ConstString("")0
);
236
237
190
      if (!childName.IsEmpty() && verifier.hasToken(childName.GetStringRef()) &&
238
190
          
childName != "this"37
) {
239
21
        stream.Printf("using $__lldb_local_vars::%s;\n",
240
21
                      childName.GetCString());
241
21
      }
242
190
    }
243
63
  }
244
154
}
245
246
} // namespace
247
248
6.91k
TokenVerifier::TokenVerifier(std::string body) {
249
6.91k
  using namespace clang;
250
251
  // We only care about tokens and not their original source locations. If we
252
  // move the whole expression to only be in one line we can simplify the
253
  // following code that extracts the token contents.
254
6.91k
  std::replace(body.begin(), body.end(), '\n', ' ');
255
6.91k
  std::replace(body.begin(), body.end(), '\r', ' ');
256
257
6.91k
  FileSystemOptions file_opts;
258
6.91k
  FileManager file_mgr(file_opts,
259
6.91k
                       FileSystem::Instance().GetVirtualFileSystem());
260
261
  // Let's build the actual source code Clang needs and setup some utility
262
  // objects.
263
6.91k
  llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
264
6.91k
  llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(
265
6.91k
      new DiagnosticOptions());
266
6.91k
  DiagnosticsEngine diags(diag_ids, diags_opts);
267
6.91k
  clang::SourceManager SM(diags, file_mgr);
268
6.91k
  auto buf = llvm::MemoryBuffer::getMemBuffer(body);
269
270
6.91k
  FileID FID = SM.createFileID(buf->getMemBufferRef());
271
272
  // Let's just enable the latest ObjC and C++ which should get most tokens
273
  // right.
274
6.91k
  LangOptions Opts;
275
6.91k
  Opts.ObjC = true;
276
6.91k
  Opts.DollarIdents = true;
277
6.91k
  Opts.CPlusPlus20 = true;
278
6.91k
  Opts.LineComment = true;
279
280
6.91k
  Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
281
282
6.91k
  Token token;
283
6.91k
  bool exit = false;
284
51.3k
  while (!exit) {
285
    // Returns true if this is the last token we get from the lexer.
286
44.4k
    exit = lex.LexFromRawLexer(token);
287
288
    // Extract the column number which we need to extract the token content.
289
    // Our expression is just one line, so we don't need to handle any line
290
    // numbers here.
291
44.4k
    bool invalid = false;
292
44.4k
    unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
293
44.4k
    if (invalid)
294
0
      continue;
295
    // Column numbers start at 1, but indexes in our string start at 0.
296
44.4k
    --start;
297
298
    // Annotations don't have a length, so let's skip them.
299
44.4k
    if (token.isAnnotation())
300
0
      continue;
301
302
    // Extract the token string from our source code and store it.
303
44.4k
    std::string token_str = body.substr(start, token.getLength());
304
44.4k
    if (token_str.empty())
305
74
      continue;
306
44.3k
    m_tokens.insert(token_str);
307
44.3k
  }
308
6.91k
}
309
310
void ClangExpressionSourceCode::AddLocalVariableDecls(StreamString &stream,
311
                                                      const std::string &expr,
312
6.91k
                                                      StackFrame *frame) const {
313
6.91k
  assert(frame);
314
6.91k
  TokenVerifier tokens(expr);
315
316
6.91k
  lldb::VariableListSP var_list_sp = frame->GetInScopeVariableList(false, true);
317
318
90.6k
  for (size_t i = 0; i < var_list_sp->GetSize(); 
i++83.7k
) {
319
83.7k
    lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
320
321
83.7k
    ConstString var_name = var_sp->GetName();
322
323
83.7k
    if (var_name == "this" && 
m_wrap_kind == WrapKind::CppMemberFunction278
) {
324
154
      AddLambdaCaptureDecls(stream, frame, tokens);
325
326
154
      continue;
327
154
    }
328
329
    // We can check for .block_descriptor w/o checking for langauge since this
330
    // is not a valid identifier in either C or C++.
331
83.5k
    if (!var_name || 
var_name == ".block_descriptor"82.1k
)
332
2.45k
      continue;
333
334
81.1k
    if (!expr.empty() && 
!tokens.hasToken(var_name.GetStringRef())80.8k
)
335
74.4k
      continue;
336
337
6.70k
    const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
338
6.70k
                         
m_wrap_kind == WrapKind::ObjCStaticMethod6.66k
;
339
6.70k
    if ((var_name == "self" || 
var_name == "_cmd"6.66k
) &&
is_objc42
)
340
42
      continue;
341
342
6.66k
    stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
343
6.66k
  }
344
6.91k
}
345
346
bool ClangExpressionSourceCode::GetText(
347
    std::string &text, ExecutionContext &exe_ctx, bool add_locals,
348
7.26k
    bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {
349
7.26k
  const char *target_specific_defines = "typedef signed char BOOL;\n";
350
7.26k
  std::string module_macros;
351
7.26k
  llvm::raw_string_ostream module_macros_stream(module_macros);
352
353
7.26k
  Target *target = exe_ctx.GetTargetPtr();
354
7.26k
  if (target) {
355
7.26k
    if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
356
7.26k
        
target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_327.24k
) {
357
19
      target_specific_defines = "typedef bool BOOL;\n";
358
19
    }
359
7.26k
    if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
360
7.24k
      if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
361
7.24k
        if (platform_sp->GetPluginName() == "ios-simulator") {
362
0
          target_specific_defines = "typedef bool BOOL;\n";
363
0
        }
364
7.24k
      }
365
7.24k
    }
366
367
7.26k
    auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
368
7.26k
        target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
369
7.26k
    std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
370
7.26k
        persistent_vars->GetClangModulesDeclVendor();
371
7.26k
    if (decl_vendor) {
372
7.24k
      const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
373
7.24k
          persistent_vars->GetHandLoadedClangModules();
374
7.24k
      ClangModulesDeclVendor::ModuleVector modules_for_macros;
375
376
21.6k
      for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
377
21.6k
        modules_for_macros.push_back(module);
378
21.6k
      }
379
380
7.24k
      if (target->GetEnableAutoImportClangModules()) {
381
7.24k
        if (StackFrame *frame = exe_ctx.GetFramePtr()) {
382
6.95k
          if (Block *block = frame->GetFrameBlock()) {
383
6.92k
            SymbolContext sc;
384
385
6.92k
            block->CalculateSymbolContext(&sc);
386
387
6.92k
            if (sc.comp_unit) {
388
6.92k
              StreamString error_stream;
389
390
6.92k
              decl_vendor->AddModulesForCompileUnit(
391
6.92k
                  *sc.comp_unit, modules_for_macros, error_stream);
392
6.92k
            }
393
6.92k
          }
394
6.95k
        }
395
7.24k
      }
396
397
7.24k
      decl_vendor->ForEachMacro(
398
7.24k
          modules_for_macros,
399
7.24k
          [&module_macros_stream](llvm::StringRef token,
400
505k
                                  llvm::StringRef expansion) -> bool {
401
            // Check if the macro hasn't already been defined in the
402
            // g_expression_prefix (which defines a few builtin macros).
403
505k
            module_macros_stream << "#ifndef " << token << "\n";
404
505k
            module_macros_stream << expansion << "\n";
405
505k
            module_macros_stream << "#endif\n";
406
505k
            return false;
407
505k
          });
408
7.24k
    }
409
7.26k
  }
410
411
7.26k
  StreamString debug_macros_stream;
412
7.26k
  StreamString lldb_local_var_decls;
413
7.26k
  if (StackFrame *frame = exe_ctx.GetFramePtr()) {
414
6.97k
    const SymbolContext &sc = frame->GetSymbolContext(
415
6.97k
        lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
416
417
6.97k
    if (sc.comp_unit && 
sc.line_entry.IsValid()6.92k
) {
418
6.92k
      DebugMacros *dm = sc.comp_unit->GetDebugMacros();
419
6.92k
      if (dm) {
420
0
        AddMacroState state(sc.line_entry.file, sc.line_entry.line);
421
0
        AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
422
0
      }
423
6.92k
    }
424
425
6.97k
    if (add_locals)
426
6.93k
      if (target->GetInjectLocalVariables(&exe_ctx)) {
427
6.91k
        AddLocalVariableDecls(lldb_local_var_decls,
428
6.91k
                              force_add_all_locals ? 
""67
:
m_body6.85k
, frame);
429
6.91k
      }
430
6.97k
  }
431
432
7.26k
  if (m_wrap) {
433
    // Generate a list of @import statements that will import the specified
434
    // module into our expression.
435
7.26k
    std::string module_imports;
436
7.26k
    for (const std::string &module : modules) {
437
408
      module_imports.append("@import ");
438
408
      module_imports.append(module);
439
408
      module_imports.append(";\n");
440
408
    }
441
442
7.26k
    StreamString wrap_stream;
443
444
7.26k
    wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
445
7.26k
                       module_macros.c_str(), debug_macros_stream.GetData(),
446
7.26k
                       target_specific_defines, m_prefix.c_str());
447
448
    // First construct a tagged form of the user expression so we can find it
449
    // later:
450
7.26k
    std::string tagged_body;
451
7.26k
    tagged_body.append(m_start_marker);
452
7.26k
    tagged_body.append(m_body);
453
7.26k
    tagged_body.append(m_end_marker);
454
455
7.26k
    switch (m_wrap_kind) {
456
7.01k
    case WrapKind::Function:
457
7.01k
      wrap_stream.Printf("%s"
458
7.01k
                         "void                           \n"
459
7.01k
                         "%s(void *$__lldb_arg)          \n"
460
7.01k
                         "{                              \n"
461
7.01k
                         "    %s;                        \n"
462
7.01k
                         "%s"
463
7.01k
                         "}                              \n",
464
7.01k
                         module_imports.c_str(), m_name.c_str(),
465
7.01k
                         lldb_local_var_decls.GetData(), tagged_body.c_str());
466
7.01k
      break;
467
176
    case WrapKind::CppMemberFunction:
468
176
      wrap_stream.Printf("%s"
469
176
                         "void                                   \n"
470
176
                         "$__lldb_class::%s(void *$__lldb_arg)   \n"
471
176
                         "{                                      \n"
472
176
                         "    %s;                                \n"
473
176
                         "%s"
474
176
                         "}                                      \n",
475
176
                         module_imports.c_str(), m_name.c_str(),
476
176
                         lldb_local_var_decls.GetData(), tagged_body.c_str());
477
176
      break;
478
68
    case WrapKind::ObjCInstanceMethod:
479
68
      wrap_stream.Printf(
480
68
          "%s"
481
68
          "@interface $__lldb_objc_class ($__lldb_category)       \n"
482
68
          "-(void)%s:(void *)$__lldb_arg;                         \n"
483
68
          "@end                                                   \n"
484
68
          "@implementation $__lldb_objc_class ($__lldb_category)  \n"
485
68
          "-(void)%s:(void *)$__lldb_arg                          \n"
486
68
          "{                                                      \n"
487
68
          "    %s;                                                \n"
488
68
          "%s"
489
68
          "}                                                      \n"
490
68
          "@end                                                   \n",
491
68
          module_imports.c_str(), m_name.c_str(), m_name.c_str(),
492
68
          lldb_local_var_decls.GetData(), tagged_body.c_str());
493
68
      break;
494
495
11
    case WrapKind::ObjCStaticMethod:
496
11
      wrap_stream.Printf(
497
11
          "%s"
498
11
          "@interface $__lldb_objc_class ($__lldb_category)        \n"
499
11
          "+(void)%s:(void *)$__lldb_arg;                          \n"
500
11
          "@end                                                    \n"
501
11
          "@implementation $__lldb_objc_class ($__lldb_category)   \n"
502
11
          "+(void)%s:(void *)$__lldb_arg                           \n"
503
11
          "{                                                       \n"
504
11
          "    %s;                                                 \n"
505
11
          "%s"
506
11
          "}                                                       \n"
507
11
          "@end                                                    \n",
508
11
          module_imports.c_str(), m_name.c_str(), m_name.c_str(),
509
11
          lldb_local_var_decls.GetData(), tagged_body.c_str());
510
11
      break;
511
7.26k
    }
512
513
7.26k
    text = std::string(wrap_stream.GetString());
514
7.26k
  } else {
515
0
    text.append(m_body);
516
0
  }
517
518
7.26k
  return true;
519
7.26k
}
520
521
bool ClangExpressionSourceCode::GetOriginalBodyBounds(
522
7.28k
    std::string transformed_text, size_t &start_loc, size_t &end_loc) {
523
7.28k
  start_loc = transformed_text.find(m_start_marker);
524
7.28k
  if (start_loc == std::string::npos)
525
0
    return false;
526
7.28k
  start_loc += m_start_marker.size();
527
7.28k
  end_loc = transformed_text.find(m_end_marker);
528
7.28k
  return end_loc != std::string::npos;
529
7.28k
}