ClangPersistentVariables.cpp revision 321369
1//===-- ClangPersistentVariables.cpp ----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ClangPersistentVariables.h"
11
12#include "lldb/Core/Value.h"
13#include "lldb/Utility/DataExtractor.h"
14#include "lldb/Utility/Log.h"
15#include "lldb/Utility/StreamString.h"
16
17#include "clang/AST/Decl.h"
18
19#include "llvm/ADT/StringMap.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24ClangPersistentVariables::ClangPersistentVariables()
25    : lldb_private::PersistentExpressionState(LLVMCastKind::eKindClang),
26      m_next_persistent_variable_id(0) {}
27
28ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
29    const lldb::ValueObjectSP &valobj_sp) {
30  return AddNewlyConstructedVariable(new ClangExpressionVariable(valobj_sp));
31}
32
33ExpressionVariableSP ClangPersistentVariables::CreatePersistentVariable(
34    ExecutionContextScope *exe_scope, const ConstString &name,
35    const CompilerType &compiler_type, lldb::ByteOrder byte_order,
36    uint32_t addr_byte_size) {
37  return AddNewlyConstructedVariable(new ClangExpressionVariable(
38      exe_scope, name, compiler_type, byte_order, addr_byte_size));
39}
40
41void ClangPersistentVariables::RemovePersistentVariable(
42    lldb::ExpressionVariableSP variable) {
43  RemoveVariable(variable);
44
45  const char *name = variable->GetName().AsCString();
46
47  if (*name != '$')
48    return;
49  name++;
50
51  if (strtoul(name, NULL, 0) == m_next_persistent_variable_id - 1)
52    m_next_persistent_variable_id--;
53}
54
55ConstString ClangPersistentVariables::GetNextPersistentVariableName() {
56  char name_cstr[256];
57  ::snprintf(name_cstr, sizeof(name_cstr), "$%u",
58             m_next_persistent_variable_id++);
59  ConstString name(name_cstr);
60  return name;
61}
62
63void ClangPersistentVariables::RegisterPersistentDecl(const ConstString &name,
64                                                      clang::NamedDecl *decl) {
65  m_persistent_decls.insert(
66      std::pair<const char *, clang::NamedDecl *>(name.GetCString(), decl));
67
68  if (clang::EnumDecl *enum_decl = llvm::dyn_cast<clang::EnumDecl>(decl)) {
69    for (clang::EnumConstantDecl *enumerator_decl : enum_decl->enumerators()) {
70      m_persistent_decls.insert(std::pair<const char *, clang::NamedDecl *>(
71          ConstString(enumerator_decl->getNameAsString()).GetCString(),
72          enumerator_decl));
73    }
74  }
75}
76
77clang::NamedDecl *
78ClangPersistentVariables::GetPersistentDecl(const ConstString &name) {
79  PersistentDeclMap::const_iterator i =
80      m_persistent_decls.find(name.GetCString());
81
82  if (i == m_persistent_decls.end())
83    return NULL;
84  else
85    return i->second;
86}
87