CppParser
Loading...
Searching...
No Matches
cppprog.h
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#pragma once
25
26#include "cppast.h"
27#include "cppparser.h"
28#include "cpptypetree.h"
29
30#include <functional>
31#include <map>
32#include <set>
33#include <vector>
34
36
37inline bool selectAllFiles(const std::string& file)
38{
39 return true;
40}
41
42inline bool selectHeadersOnly(const std::string& file)
43{
44 const auto dotPos = file.rfind('.');
45 const auto dotNotFound = (dotPos == file.npos);
46 const auto dotIsTheLastChar = (dotPos == (file.length() - 1));
47 if (dotNotFound || dotIsTheLastChar)
48 return false;
49 return file[dotPos + 1] == 'h';
50}
51
52using CppCompoundArray = std::vector<CppCompoundPtr>;
53using CppProgFileSelecter = std::function<bool(const std::string&)>;
54
59{
60public:
61 CppProgram(const std::string& folder,
62 CppParser parser = CppParser(),
63 const CppProgFileSelecter& fileSelector = selectHeadersOnly);
64 CppProgram(const std::vector<std::string>& files, CppParser parser = CppParser());
65
66public:
79 const CppTypeTreeNode* nameLookup(const std::string& name, const CppTypeTreeNode* beginFrom = nullptr) const;
91 const CppTypeTreeNode* searchTypeNode(const std::string& name, const CppTypeTreeNode* parentNode = nullptr) const;
96 const CppTypeTreeNode* typeTreeNodeFromCppObj(CppObj* cppObj) const;
101
102public:
107 void addCppAst(CppCompoundPtr cppAst);
108 void addCompound(CppCompound* compound, CppCompound* parent);
109 void addCompound(CppCompound* compound, CppTypeTreeNode* parentTypeNode);
110
111private:
112 void loadType(CppCompound* cppCompound, CppTypeTreeNode* typeNode);
113
114private:
115 using CppObjToTypeNodeMap = std::map<CppObj*, CppTypeTreeNode*>;
116
120};
121
123{
124 return fileAsts_;
125}
126
128{
129 CppObjToTypeNodeMap::const_iterator itr = cppObjToTypeNode_.find(cppObj);
130 return itr == cppObjToTypeNode_.end() ? nullptr : itr->second;
131}
Parses C++ source and generates an AST.
Definition: cppparser.h:40
Represents an entire C++ program.
Definition: cppprog.h:59
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
CppCompoundArray & getFileAsts()
Definition: cppprog.h:122
const CppTypeTreeNode * typeTreeNodeFromCppObj(CppObj *cppObj) const
Definition: cppprog.h:127
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::map< CppObj *, CppTypeTreeNode * > CppObjToTypeNodeMap
Definition: cppprog.h:115
std::unique_ptr< CppCompound > CppCompoundPtr
Definition: cppast.h:416
std::function< bool(const std::string &)> CppProgFileSelecter
Definition: cppprog.h:53
bool selectAllFiles(const std::string &file)
Definition: cppprog.h:37
std::vector< CppCompoundPtr > CppCompoundArray
Definition: cppprog.h:52
bool selectHeadersOnly(const std::string &file)
Definition: cppprog.h:42
std::string & name(CppVar *var)
All classes, structs, unions, and namespaces can be classified as a Compound object.
Definition: cppast.h:927
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