CppParser
Loading...
Searching...
No Matches
cppprog.cpp
Go to the documentation of this file.
1/*
2 The MIT License (MIT)
3
4 Copyright (c) 2018 Satya Das
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy of
7 this software and associated documentation files (the "Software"), to deal in
8 the Software without restriction, including without limitation the rights to
9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 the Software, and to permit persons to whom the Software is furnished to do so,
11 subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24#include "cppprog.h"
25#include "cpputil.h"
26#include "utils.h"
27
32
33#include <iostream>
34
36
37CppProgram::CppProgram(const std::vector<std::string>& files, CppParser parser)
38{
40
41 for (const auto& f : files)
42 {
43 std::cout << "INFO\t Parsing '" << f << "'\n";
44 auto cppAst = parser.parseFile(f.c_str());
45 if (cppAst)
46 addCppAst(std::move(cppAst));
47 }
48}
49
50CppProgram::CppProgram(const std::string& folder, CppParser parser, const CppProgFileSelecter& fileSelector)
51 : CppProgram(collectFiles(folder, fileSelector), std::move(parser))
52{
53}
54
56{
57 if (!isCppFile(cppAst.get()))
58 return;
59 loadType(cppAst.get(), &cppTypeTreeRoot_);
60 fileAsts_.emplace_back(std::move(cppAst));
61}
62
63void CppProgram::addCompound(CppCompound* compound, CppTypeTreeNode* parentTypeNode)
64{
65 if (compound->name().empty())
66 return;
67 auto& childNode = parentTypeNode->children[compound->name()];
68 childNode.cppObjSet.insert(compound);
69 childNode.parent = parentTypeNode;
70 cppObjToTypeNode_[compound] = &childNode;
71 loadType(compound, &childNode);
72}
73
75{
76 auto itr = cppObjToTypeNode_.find(parent);
77 if (itr != cppObjToTypeNode_.end())
78 addCompound(compound, itr->second);
79}
80
82{
83 if (cppCompound == NULL)
84 return;
85 if (isCppFile(cppCompound)) // Type node for file object should be the root itself.
86 {
87 cppObjToTypeNode_[cppCompound] = typeNode;
88 typeNode->cppObjSet.insert(cppCompound);
89 }
90 forEachMember(cppCompound, [&](CppObj* mem) {
91 if (isCompound(mem))
92 {
93 addCompound((CppCompound*) mem, typeNode);
94 }
95 else if (isEnum(mem))
96 {
97 CppTypeTreeNode& childNode = typeNode->children[((CppEnum*) mem)->name_];
98 childNode.cppObjSet.insert(mem);
99 childNode.parent = typeNode;
100 cppObjToTypeNode_[mem] = &childNode;
101 }
102 else if (isTypedefName(mem))
103 {
104 auto* typedefName = static_cast<CppTypedefName*>(mem);
105 CppTypeTreeNode& childNode = typeNode->children[typedefName->var_->name()];
106 childNode.cppObjSet.insert(mem);
107 childNode.parent = typeNode;
108 cppObjToTypeNode_[mem] = &childNode;
109 }
110 else if (isUsingDecl(mem))
111 {
112 auto* usingDecl = static_cast<CppUsingDecl*>(mem);
113 CppTypeTreeNode& childNode = typeNode->children[usingDecl->name_];
114 childNode.cppObjSet.insert(mem);
115 childNode.parent = typeNode;
116 cppObjToTypeNode_[mem] = &childNode;
117 }
118 else if (isFunctionPtr(mem))
119 {
120 CppTypeTreeNode& childNode = typeNode->children[((CppFunctionPointer*) mem)->name_];
121 childNode.cppObjSet.insert(mem);
122 childNode.parent = typeNode;
123 cppObjToTypeNode_[mem] = &childNode;
124 }
125 else if (isFwdClsDecl(mem))
126 {
127 auto* fwdCls = static_cast<CppFwdClsDecl*>(mem);
128 if (!(fwdCls->attr() & kFriend))
129 {
130 CppTypeTreeNode& childNode = typeNode->children[((CppFwdClsDecl*) mem)->name_];
131 childNode.cppObjSet.insert(mem);
132 childNode.parent = typeNode;
133 cppObjToTypeNode_[mem] = &childNode;
134 }
135 }
136
137 return false;
138 });
139}
140
141const CppTypeTreeNode* CppProgram::nameLookup(const std::string& name, const CppTypeTreeNode* typeNode) const
142{
143 if (name.empty())
144 return &cppTypeTreeRoot_;
145 if (typeNode == nullptr)
146 typeNode = &cppTypeTreeRoot_;
147 size_t nameBegPos = 0;
148 size_t nameEndPos = name.find("::", nameBegPos);
149 if (nameEndPos == std::string::npos)
150 {
151 for (; typeNode != NULL; typeNode = typeNode->parent)
152 {
153 CppTypeTree::const_iterator itr = typeNode->children.find(name);
154 if (itr != typeNode->children.end())
155 return &itr->second;
156 }
157 return NULL;
158 }
159 else
160 {
161 auto nameToLookFor = name.substr(nameBegPos, nameEndPos - nameBegPos);
162 typeNode = nameLookup(nameToLookFor, typeNode);
163 if (!typeNode)
164 return nullptr;
165 do
166 {
167 nameBegPos = nameEndPos + 2;
168 nameEndPos = name.find("::", nameBegPos);
169 if (nameEndPos == std::string::npos)
170 nameEndPos = name.length();
171 nameToLookFor = name.substr(nameBegPos, nameEndPos - nameBegPos);
172 auto itr = typeNode->children.find(nameToLookFor);
173 if (itr == typeNode->children.end())
174 return nullptr;
175 typeNode = &itr->second;
176 } while (nameEndPos < name.length());
177 }
178 return typeNode;
179}
180
181const CppTypeTreeNode* CppProgram::searchTypeNode(const std::string& name, const CppTypeTreeNode* parentNode) const
182{
183 std::vector<const CppTypeTreeNode*> nextLevelNodes(1, parentNode ? parentNode : &cppTypeTreeRoot_);
184
185 do
186 {
187 std::vector<const CppTypeTreeNode*> currentLevelNodes;
188 currentLevelNodes.swap(nextLevelNodes);
189 assert(nextLevelNodes.empty());
190 for (const auto* node : currentLevelNodes)
191 {
192 for (const auto& child : node->children)
193 {
194 if (child.first == name)
195 return &(child.second);
196 nextLevelNodes.push_back(&(child.second));
197 }
198 }
199 } while (!nextLevelNodes.empty());
200
201 return nullptr;
202}
Parses C++ source and generates an AST.
Definition: cppparser.h:40
CppCompoundPtr parseFile(const std::string &filename)
Definition: cppparser.cpp:131
Represents an entire C++ program.
Definition: cppprog.h:59
CppProgram(const std::string &folder, CppParser parser=CppParser(), const CppProgFileSelecter &fileSelector=selectHeadersOnly)
Definition: cppprog.cpp:50
CppCompoundArray fileAsts_
Array of all top level ASTs corresponding to files.
Definition: cppprog.h:117
CppTypeTreeNode cppTypeTreeRoot_
Repository of all compound objects arranged as type-tree.
Definition: cppprog.h:118
void addCppAst(CppCompoundPtr cppAst)
Adds a new file AST to this program.
Definition: cppprog.cpp:55
void addCompound(CppCompound *compound, CppCompound *parent)
Definition: cppprog.cpp:74
void loadType(CppCompound *cppCompound, CppTypeTreeNode *typeNode)
Definition: cppprog.cpp:81
const CppTypeTreeNode * nameLookup(const std::string &name, const CppTypeTreeNode *beginFrom=nullptr) const
Finds the CppTypeTreeNode object corresponding to a given name.
Definition: cppprog.cpp:141
const CppTypeTreeNode * searchTypeNode(const std::string &name, const CppTypeTreeNode *parentNode=nullptr) const
Searches down (in breadth first manner) the CppTypeTreeNode object corresponding to a given name.
Definition: cppprog.cpp:181
CppObjToTypeNodeMap cppObjToTypeNode_
Definition: cppprog.h:119
std::unique_ptr< CppCompound > CppCompoundPtr
Definition: cppast.h:416
bool forEachMember(CppCompoundEPtr compound, std::function< bool(CppObj *)> visitor)
bool isCppFile(CppCompoundEPtr compound)
@ kFriend
Definition: cppconst.h:197
bool isFwdClsDecl(CppObj *cppObj)
bool isEnum(CppObj *cppObj)
bool isUsingDecl(CppObj *cppObj)
bool isCompound(CppObj *cppObj)
bool isTypedefName(CppObj *cppObj)
bool isFunctionPtr(CppObj *cppObj)
std::function< bool(const std::string &)> CppProgFileSelecter
Definition: cppprog.h:53
std::string & name(CppVar *var)
All classes, structs, unions, and namespaces can be classified as a Compound object.
Definition: cppast.h:927
std::string & name()
Definition: cppast.h:953
Function pointer type definition using typedef, e.g.
Definition: cppast.h:1390
An abstract class that is used as base class of all other classes.
Definition: cppast.h:134
A node in a CppTypeTree.
Definition: cpptypetree.h:60
CppObjSet cppObjSet
This needs to be a set because same namespace can be defined multiple times.
Definition: cpptypetree.h:66
CppTypeTree children
Definition: cpptypetree.h:67
CppTypeTreeNode * parent
Definition: cpptypetree.h:68
void collectFiles(std::vector< std::string > &files, const fs::path &path, const CppProgFileSelecter &fileSelector)
Definition: utils.cpp:101