Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangPersistentVariables.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ClangPersistentVariables.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 "ClangPersistentVariables.h"
10
#include "ClangASTImporter.h"
11
#include "ClangModulesDeclVendor.h"
12
13
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
14
#include "lldb/Core/Value.h"
15
#include "lldb/Target/Target.h"
16
#include "lldb/Utility/DataExtractor.h"
17
#include "lldb/Utility/Log.h"
18
#include "lldb/Utility/StreamString.h"
19
20
#include "clang/AST/Decl.h"
21
22
#include "llvm/ADT/StringMap.h"
23
24
using namespace lldb;
25
using namespace lldb_private;
26
27
ClangPersistentVariables::ClangPersistentVariables(
28
    std::shared_ptr<Target> target_sp)
29
    : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
30
2.46k
      m_target_sp(target_sp) {}
31
32
ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
33
147
    const lldb::ValueObjectSP &valobj_sp) {
34
147
  return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
35
147
}
36
37
ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
38
    ExecutionContextScope *exe_scope, ConstString name,
39
    const CompilerType &compiler_type, lldb::ByteOrder byte_order,
40
8.76k
    uint32_t addr_byte_size) {
41
8.76k
  return AddNewlyConstructedVariable(new ClangExpressionVariable(
42
8.76k
      exe_scope, name, compiler_type, byte_order, addr_byte_size));
43
8.76k
}
44
45
void ClangPersistentVariables::RemovePersistentVariable(
46
77
    lldb::ExpressionVariableSP variable) {
47
77
  RemoveVariable(variable);
48
49
  // Check if the removed variable was the last one that was created. If yes,
50
  // reuse the variable id for the next variable.
51
52
  // Nothing to do if we have not assigned a variable id so far.
53
77
  if (m_next_persistent_variable_id == 0)
54
0
    return;
55
56
77
  llvm::StringRef name = variable->GetName().GetStringRef();
57
  // Remove the prefix from the variable that only the indes is left.
58
77
  if (!name.consume_front(GetPersistentVariablePrefix(false)))
59
0
    return;
60
61
  // Check if the variable contained a variable id.
62
77
  uint32_t variable_id;
63
77
  if (name.getAsInteger(10, variable_id))
64
0
    return;
65
  // If it's the most recent variable id that was assigned, make sure that this
66
  // variable id will be used for the next persistent variable.
67
77
  if (variable_id == m_next_persistent_variable_id - 1)
68
77
    m_next_persistent_variable_id--;
69
77
}
70
71
llvm::Optional<CompilerType>
72
ClangPersistentVariables::GetCompilerTypeFromPersistentDecl(
73
21
    ConstString type_name) {
74
21
  PersistentDecl p = m_persistent_decls.lookup(type_name.GetCString());
75
76
21
  if (p.m_decl == nullptr)
77
9
    return llvm::None;
78
79
12
  if (clang::TypeDecl *tdecl = llvm::dyn_cast<clang::TypeDecl>(p.m_decl)) {
80
12
    opaque_compiler_type_t t = static_cast<opaque_compiler_type_t>(
81
12
        const_cast<clang::Type *>(tdecl->getTypeForDecl()));
82
12
    return CompilerType(p.m_context, t);
83
12
  }
84
0
  return llvm::None;
85
12
}
86
87
void ClangPersistentVariables::RegisterPersistentDecl(ConstString name,
88
                                                      clang::NamedDecl *decl,
89
103
                                                      TypeSystemClang *ctx) {
90
103
  PersistentDecl p = {decl, ctx};
91
103
  m_persistent_decls.insert(std::make_pair(name.GetCString(), p));
92
93
103
  if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
94
6
    for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
95
6
      p = {enumerator_decl, ctx};
96
6
      m_persistent_decls.insert(std::make_pair(
97
6
          ConstString(enumerator_decl->getNameAsString()).GetCString(), p));
98
6
    }
99
2
  }
100
103
}
101
102
clang::NamedDecl *
103
42.0k
ClangPersistentVariables::GetPersistentDecl(ConstString name) {
104
42.0k
  return m_persistent_decls.lookup(name.GetCString()).m_decl;
105
42.0k
}
106
107
std::shared_ptr<ClangASTImporter>
108
15.4k
ClangPersistentVariables::GetClangASTImporter() {
109
15.4k
  if (!m_ast_importer_sp) {
110
2.46k
    m_ast_importer_sp = std::make_shared<ClangASTImporter>();
111
2.46k
  }
112
15.4k
  return m_ast_importer_sp;
113
15.4k
}
114
115
std::shared_ptr<ClangModulesDeclVendor>
116
54.2k
ClangPersistentVariables::GetClangModulesDeclVendor() {
117
54.2k
  if (!m_modules_decl_vendor_sp) {
118
1.42k
    m_modules_decl_vendor_sp.reset(
119
1.42k
        ClangModulesDeclVendor::Create(*m_target_sp.get()));
120
1.42k
  }
121
54.2k
  return m_modules_decl_vendor_sp;
122
54.2k
}
123
124
ConstString
125
8.87k
ClangPersistentVariables::GetNextPersistentVariableName(bool is_error) {
126
8.87k
  llvm::SmallString<64> name;
127
8.87k
  {
128
8.87k
    llvm::raw_svector_ostream os(name);
129
8.87k
    os << GetPersistentVariablePrefix(is_error)
130
8.87k
       << m_next_persistent_variable_id++;
131
8.87k
  }
132
8.87k
  return ConstString(name);
133
8.87k
}