-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_program.cpp
More file actions
165 lines (150 loc) · 5.14 KB
/
Copy pathcpp_program.cpp
File metadata and controls
165 lines (150 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#include "cppparser/cpp_program.h"
#include "cppast/cppconst.h"
#include "utils.h"
#include <iostream>
namespace cppparser {
CppProgram::CppProgram(const std::vector<std::string>& files)
{
cppEntityToTypeNode_[nullptr] = &cppTypeTreeRoot_;
CppParser parser;
for (const auto& f : files)
{
std::cout << "INFO\t Parsing '" << f << "'\n";
auto cppAst = parser.parseFile(f.c_str());
if (cppAst)
addCppFile(std::move(cppAst));
}
}
void CppProgram::addCompound(const cppast::CppCompound& compound, CppTypeTreeNode& parentTypeNode)
{
if (compound.name().empty())
return;
auto& childNode = parentTypeNode.children[compound.name()];
childNode.cppEntitySet.insert(&compound);
childNode.parent = &parentTypeNode;
cppEntityToTypeNode_[&compound] = &childNode;
loadType(compound, childNode);
}
void CppProgram::addCompound(const cppast::CppCompound& compound, const cppast::CppCompound& parent)
{
auto itr = cppEntityToTypeNode_.find(&parent);
if (itr != cppEntityToTypeNode_.end())
addCompound(compound, *(itr->second));
}
void CppProgram::loadType(const cppast::CppCompound& cppCompound, CppTypeTreeNode& typeNode)
{
if (IsCppFile(cppCompound)) // Type node for file object should be the root itself.
{
cppEntityToTypeNode_[&cppCompound] = &typeNode;
typeNode.cppEntitySet.insert(&cppCompound);
}
cppCompound.visitAll([&](const cppast::CppEntity& mem) {
if (IsCompound(mem))
{
addCompound(*static_cast<const cppast::CppCompound*>(&mem), typeNode);
}
else if (IsEnum(mem))
{
CppTypeTreeNode& childNode = typeNode.children[((cppast::CppEnum&) mem).name()];
childNode.cppEntitySet.insert(&mem);
childNode.parent = &typeNode;
cppEntityToTypeNode_[&mem] = &childNode;
}
else if (IsTypedefName(mem))
{
const auto& typedefName = static_cast<const cppast::CppTypedefName&>(mem);
CppTypeTreeNode& childNode = typeNode.children[typedefName.var()->name()];
childNode.cppEntitySet.insert(&mem);
childNode.parent = &typeNode;
cppEntityToTypeNode_[&mem] = &childNode;
}
else if (IsUsingDecl(mem))
{
const auto& usingDecl = static_cast<const cppast::CppUsingDecl&>(mem);
CppTypeTreeNode& childNode = typeNode.children[usingDecl.name()];
childNode.cppEntitySet.insert(&mem);
childNode.parent = &typeNode;
cppEntityToTypeNode_[&mem] = &childNode;
}
else if (IsFunctionPtr(mem))
{
CppTypeTreeNode& childNode = typeNode.children[((const cppast::CppFunctionPointer&) mem).name()];
childNode.cppEntitySet.insert(&mem);
childNode.parent = &typeNode;
cppEntityToTypeNode_[&mem] = &childNode;
}
else if (IsFwdClsDecl(mem))
{
const auto& fwdCls = static_cast<const cppast::CppForwardClassDecl&>(mem);
if (!(fwdCls.attr() & cppast::CppIdentifierAttrib::FRIEND))
{
CppTypeTreeNode& childNode = typeNode.children[fwdCls.name()];
childNode.cppEntitySet.insert(&mem);
childNode.parent = &typeNode;
cppEntityToTypeNode_[&mem] = &childNode;
}
}
return false;
});
}
const CppTypeTreeNode* CppProgram::nameLookup(const std::string& name, const CppTypeTreeNode* beginFrom) const
{
if (name.empty())
return &cppTypeTreeRoot_;
auto* typeNode = beginFrom ? beginFrom : &cppTypeTreeRoot_;
size_t nameBegPos = 0;
size_t nameEndPos = name.find("::", nameBegPos);
if (nameEndPos == std::string::npos)
{
for (; typeNode != nullptr; typeNode = typeNode->parent)
{
CppTypeTree::const_iterator itr = typeNode->children.find(name);
if (itr != typeNode->children.end())
return &itr->second;
}
return nullptr;
}
else
{
auto nameToLookFor = name.substr(nameBegPos, nameEndPos - nameBegPos);
typeNode = nameLookup(nameToLookFor, typeNode);
if (!typeNode)
return nullptr;
do
{
nameBegPos = nameEndPos + 2;
nameEndPos = name.find("::", nameBegPos);
if (nameEndPos == std::string::npos)
nameEndPos = name.length();
nameToLookFor = name.substr(nameBegPos, nameEndPos - nameBegPos);
auto itr = typeNode->children.find(nameToLookFor);
if (itr == typeNode->children.end())
return nullptr;
typeNode = &itr->second;
} while (nameEndPos < name.length());
}
return typeNode;
}
const CppTypeTreeNode* CppProgram::searchTypeNode(const std::string& name, const CppTypeTreeNode* parentNode) const
{
std::vector<const CppTypeTreeNode*> nextLevelNodes(1, parentNode ? parentNode : &cppTypeTreeRoot_);
do
{
std::vector<const CppTypeTreeNode*> currentLevelNodes;
currentLevelNodes.swap(nextLevelNodes);
assert(nextLevelNodes.empty());
for (const auto* node : currentLevelNodes)
{
for (const auto& child : node->children)
{
if (child.first == name)
return &(child.second);
nextLevelNodes.push_back(&(child.second));
}
}
} while (!nextLevelNodes.empty());
return nullptr;
}
} // namespace cppparser