CppParser
Loading...
Searching...
No Matches
cppparser.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 "cppparser.h"
25#include "cppast.h"
26#include "cppobjfactory.h"
27#include "parser.h"
28#include "string-utils.h"
29#include "utils.h"
30
31#include <algorithm>
32#include <map>
33#include <set>
34#include <string>
35#include <vector>
36#include <fstream>
37
38// Unfortunately parser is not reentrant and has no way as of now to inject parameters.
39// So, we need globals.
40
41// Globals to help parse when preprocessors are used
42std::set<std::string> gMacroNames;
43std::set<std::string> gKnownApiDecorNames;
44std::map<std::string, int> gDefinedNames;
45std::set<std::string> gUndefinedNames;
46std::set<std::string> gIgnorableMacroNames;
47std::map<std::string, int> gRenamedKeywords;
48
51
53
55 : objFactory_(std::move(objFactory))
56{
57 if (!objFactory_)
58 objFactory_.reset(new CppObjFactory);
59}
60
61void CppParser::addKnownMacro(std::string knownMacro)
62{
63 gMacroNames.insert(std::move(knownMacro));
64}
65
66void CppParser::addKnownMacros(const std::vector<std::string>& knownMacros)
67{
68 for (auto& macro : knownMacros)
69 gMacroNames.insert(macro);
70}
71
72void CppParser::addDefinedName(std::string definedName, int value)
73{
74 gDefinedNames[std::move(definedName)] = value;
75}
76
77void CppParser::addUndefinedName(std::string undefinedName)
78{
79 gUndefinedNames.insert(std::move(undefinedName));
80}
81
82void CppParser::addUndefinedNames(const std::vector<std::string>& undefinedNames)
83{
84 for (auto& macro : undefinedNames)
85 gUndefinedNames.insert(macro);
86}
87
88void CppParser::addIgnorableMacro(std::string ignorableMacro)
89{
90 gIgnorableMacroNames.insert(std::move(ignorableMacro));
91}
92
93void CppParser::addIgnorableMacros(const std::vector<std::string>& ignorableMacros)
94{
95 for (auto& macro : ignorableMacros)
96 gIgnorableMacroNames.insert(macro);
97}
98
99void CppParser::addKnownApiDecor(std::string knownApiDecor)
100{
101 gKnownApiDecorNames.insert(std::move(knownApiDecor));
102}
103
104void CppParser::addKnownApiDecors(const std::vector<std::string>& knownApiDecor)
105{
106 for (auto& apiDecor : knownApiDecor)
107 gKnownApiDecorNames.insert(apiDecor);
108}
109
110bool CppParser::addRenamedKeyword(const std::string& keyword, std::string renamedKeyword)
111{
112 extern int GetKeywordId(const std::string& keyword);
113 auto id = GetKeywordId(keyword);
114 if (id == -1)
115 return false;
116 gRenamedKeywords.emplace(std::make_pair(std::move(renamedKeyword), id));
117
118 return true;
119}
120
122{
124}
125
127{
129}
130
131CppCompoundPtr CppParser::parseFile(const std::string& filename)
132{
133 auto stm = readFile(filename);
134 auto cppCompound = parseStream(stm.data(), stm.size());
135 if (!cppCompound)
136 return cppCompound;
137 cppCompound->name(filename);
138 return cppCompound;
139}
140
141CppCompoundPtr CppParser::parseString(const std::string& filePathToSave, const std::string& content)
142{
143 std::ofstream out(filePathToSave);
144 out << content;
145 out.close();
146 return parseFile(filePathToSave);
147}
148
149CppCompoundPtr CppParser::parseString(const std::string& content)
150{
151 return parseString("/tmp/t.cpp", content);
152}
153
154CppCompoundPtr CppParser::parseStream(char* stm, size_t stmSize)
155{
156 if (stm == nullptr || stmSize == 0)
157 return nullptr;
158 gObjFactory = objFactory_.get();
159 return ::parseStream(stm, stmSize);
160}
161
163{
164 ::setErrorHandler(errorHandler);
165}
166
168{
170}
Factory class to create various CppObj instances.
Definition: cppobjfactory.h:37
void addKnownMacros(const std::vector< std::string > &knownMacros)
Definition: cppparser.cpp:66
void parseEnumBodyAsBlob()
Definition: cppparser.cpp:121
void addKnownApiDecor(std::string knownApiDecor)
Definition: cppparser.cpp:99
void addIgnorableMacros(const std::vector< std::string > &ignorableMacros)
Definition: cppparser.cpp:93
void parseFunctionBodyAsBlob(bool asBlob)
Definition: cppparser.cpp:126
void addUndefinedNames(const std::vector< std::string > &undefinedNames)
Definition: cppparser.cpp:82
CppParser(CppObjFactoryPtr objFactory=nullptr)
Definition: cppparser.cpp:54
CppObjFactoryPtr objFactory_
Definition: cppparser.h:82
CppCompoundPtr parseStream(char *stm, size_t stmSize)
Definition: cppparser.cpp:154
void addIgnorableMacro(std::string ignorableMacro)
Definition: cppparser.cpp:88
void resetErrorHandler()
Definition: cppparser.cpp:167
void addDefinedName(std::string definedName, int value=0)
Definition: cppparser.cpp:72
std::function< void(const char *errLineText, size_t lineNum, size_t errorStartPos, int lexerContext)> ErrorHandler
Definition: cppparser.h:43
void addKnownApiDecors(const std::vector< std::string > &knownApiDecor)
Definition: cppparser.cpp:104
CppCompoundPtr parseString(const std::string &filePathToSave, const std::string &content)
Definition: cppparser.cpp:141
void addKnownMacro(std::string knownMacro)
Definition: cppparser.cpp:61
void setErrorHandler(ErrorHandler errorHandler)
Definition: cppparser.cpp:162
CppCompoundPtr parseFile(const std::string &filename)
Definition: cppparser.cpp:131
void addUndefinedName(std::string undefinedName)
Definition: cppparser.cpp:77
bool addRenamedKeyword(const std::string &keyword, std::string renamedKeyword)
Definition: cppparser.cpp:110
std::unique_ptr< CppCompound > CppCompoundPtr
Definition: cppast.h:416
std::unique_ptr< CppObjFactory > CppObjFactoryPtr
Definition: cppobjfactory.h:59
std::set< std::string > gMacroNames
Definition: cppparser.cpp:42
std::set< std::string > gKnownApiDecorNames
Definition: cppparser.cpp:43
std::map< std::string, int > gDefinedNames
Definition: cppparser.cpp:44
bool gParseFunctionBodyAsBlob
Definition: cppparser.cpp:50
std::set< std::string > gIgnorableMacroNames
Definition: cppparser.cpp:46
CppObjFactory * gObjFactory
Definition: cppparser.cpp:52
std::map< std::string, int > gRenamedKeywords
Definition: cppparser.cpp:47
std::set< std::string > gUndefinedNames
Definition: cppparser.cpp:45
bool gParseEnumBodyAsBlob
Definition: cppparser.cpp:49
std::string readFile(const std::string &filename)
Definition: utils.cpp:79