1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#include "string_function.h" |
8 |
|
|
#include "parseFunc.h" |
9 |
|
|
|
10 |
|
|
using namespace std; |
11 |
|
|
|
12 |
|
|
///Parse a function |
13 |
|
|
/** @param[out] vecFunc : vector of functions to be completed |
14 |
|
|
* @param[out] parser : PFileParser |
15 |
|
|
* @param removeIA32intrinsic : true to remove IA32 intrinsics (seems to be the buildin ones used by the compiler for ARM) |
16 |
|
|
* @return true is a function has been parsed, false otherwise |
17 |
|
|
*/ |
18 |
|
231196 |
bool parseFunction(PVecFunc & vecFunc, PFileParser & parser, bool removeIA32intrinsic){ |
19 |
|
|
//Get all the functions that start like : |
20 |
|
|
// extern __inline void __attribute__((__gnu_inline__, __always_inline__, __artificial__)) |
21 |
✓✓✓✓
|
231196 |
if(!parser.isMatch("extern")){return false;} |
22 |
✓✓✗✓
|
2267 |
if(!parser.isMatch("__inline")){return false;} |
23 |
✓✓ |
6801 |
std::string returnType(parser.getStrComposedOf("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")); |
24 |
✓✓✓✓
|
2267 |
if(parser.isMatch("__attribute__")){ |
25 |
✓✓✗✓
|
2204 |
if(!parser.isMatch("(")){ |
26 |
|
|
return false; |
27 |
|
|
} |
28 |
✓✓✓ |
2204 |
parser.getUntilKeyWithoutPaternRecurse(")", "("); |
29 |
|
|
}else{ |
30 |
|
63 |
return false; |
31 |
|
|
} |
32 |
✓ |
2204 |
parser.skipWhiteSpace(); |
33 |
✓✓ |
6612 |
std::string functionName(parser.getStrComposedOf("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")); |
34 |
|
|
// std::cerr << "parseFunction : find function '" << functionName << "' at " << parser.getLocation(); |
35 |
✓ |
4408 |
PFunc func; |
36 |
✓ |
2204 |
func.setName(functionName); |
37 |
✓ |
2204 |
func.setReturnType(returnType); |
38 |
|
|
|
39 |
✓✓✓✗
|
2204 |
if(parser.isMatch("(")){ |
40 |
✓✓ |
6612 |
std::string vecArgStr(parser.getUntilKeyWithoutPatern(")")); |
41 |
✓✓✗ |
2204 |
if(vecArgStr != ""){ |
42 |
✓ |
4408 |
std::list<std::string> listArg(cutStringList(vecArgStr, ',')); |
43 |
✓✓ |
8466 |
for(std::list<std::string>::iterator it(listArg.begin()); it != listArg.end(); ++it){ |
44 |
✓ |
12524 |
std::list<std::string> listStr(cutStringOnSpacesList(*it)); |
45 |
✓ |
12524 |
std::string argName(listStr.back()); |
46 |
✓ |
6262 |
bool isStar(findInString(argName, '*')); |
47 |
✓✓ |
6262 |
if(isStar){ |
48 |
✓ |
210 |
argName = eraseCharInStr(argName, '*'); |
49 |
|
|
} |
50 |
|
6262 |
listStr.pop_back(); |
51 |
|
6262 |
bool isNotFirst(false); |
52 |
✓ |
12524 |
std::string typeName(""); |
53 |
✓✓ |
13294 |
for(std::list<std::string>::iterator itArg(listStr.begin()); itArg != listStr.end(); ++itArg){ |
54 |
✓✓ |
7032 |
if(isNotFirst){ |
55 |
✓ |
792 |
typeName += " "; |
56 |
|
|
} |
57 |
✓ |
7032 |
typeName += *itArg; |
58 |
|
7032 |
isNotFirst = true; |
59 |
|
|
} |
60 |
✓✓ |
6262 |
if(isStar){ |
61 |
✓ |
210 |
typeName += " *"; |
62 |
|
|
} |
63 |
✓ |
12524 |
PArg arg; |
64 |
✓ |
6262 |
arg.setName(argName); |
65 |
✓ |
6262 |
arg.setType(typeName); |
66 |
✓✓ |
6262 |
func.getVecArg().push_back(arg); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} |
70 |
✗✓ |
2204 |
if(removeIA32intrinsic){ |
71 |
|
|
bool isIA32Function(false); |
72 |
|
|
if(parser.isMatch("{")){ |
73 |
|
|
std::string body = parser.getUntilKeyWithoutPatern("}"); |
74 |
|
|
isIA32Function = strstr(body.c_str(), "ia32") != NULL; |
75 |
|
|
}else if(parser.isMatch(";")){} |
76 |
|
|
else{ |
77 |
|
|
std::cerr << std::endl; |
78 |
|
|
return false; |
79 |
|
|
} |
80 |
|
|
if(!isIA32Function){ |
81 |
|
|
vecFunc.push_back(func); |
82 |
|
|
} |
83 |
|
|
}else{ |
84 |
✓ |
2204 |
vecFunc.push_back(func); |
85 |
|
|
} |
86 |
|
2204 |
return true; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
///Parse a macro in the header file |
90 |
|
|
/** @param[out] vecFunc : vector of all the found function (and macro) in the current parsed file |
91 |
|
|
* @param[out] parser : PFileParser |
92 |
|
|
*/ |
93 |
|
232242 |
bool parserMacro(PVecFunc & vecFunc, PFileParser & parser){ |
94 |
✓✓✓✓
|
232242 |
if(!parser.isMatch("#")){return false;} |
95 |
|
|
|
96 |
✓✓ |
3138 |
std::string macro(parser.getUntilKey("\n")); |
97 |
✓✗ |
1046 |
if(macro.size() > 1lu){ |
98 |
✓✓✓ |
2617 |
while(macro[macro.size() - 2lu] == '\\'){ |
99 |
✓✓✓ |
1571 |
macro += parser.getUntilKey("\n"); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
✓ |
1046 |
PFunc func; |
104 |
✓✓✓ |
1046 |
func.setMacro("#" + macro + "\n"); |
105 |
|
|
|
106 |
✓ |
1046 |
vecFunc.push_back(func); |
107 |
|
1046 |
return true; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
///Parse a full file of functions |
112 |
|
|
/** @param[out] vecFunc : vector of functions to be completed |
113 |
|
|
* @param[out] parser : PFileParser |
114 |
|
|
* @param removeIA32intrinsic : true to remove IA32 intrinsics (seems to be the buildin ones used by the compiler for ARM) |
115 |
|
|
*/ |
116 |
|
232247 |
void parseFunctionFile(PVecFunc & vecFunc, PFileParser & parser, bool removeIA32intrinsic){ |
117 |
✓✓ |
232247 |
while(!parser.isEndOfFile()){ |
118 |
✓✓ |
232242 |
if(parserMacro(vecFunc, parser)){} |
119 |
✓✓ |
231196 |
else if(parseFunction(vecFunc, parser, removeIA32intrinsic)){} |
120 |
|
|
else{ |
121 |
|
228992 |
parser.getNextChar(); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
5 |
} |
125 |
|
|
|
126 |
|
|
///Parse the functions in the input file |
127 |
|
|
/** @param[out] vecFunc : vector of loaded functions |
128 |
|
|
* @param fileName : name of the input file |
129 |
|
|
* @param removeIA32intrinsic : true to remove IA32 intrinsics (seems to be the buildin ones used by the compiler for ARM) |
130 |
|
|
* @return true on success, false otherwise |
131 |
|
|
*/ |
132 |
|
6 |
bool parseFunc(PVecFunc & vecFunc, const std::string & fileName, bool removeIA32intrinsic){ |
133 |
✗✓ |
6 |
if(fileName == ""){return false;} |
134 |
✓ |
12 |
PFileParser parser; |
135 |
✓✓✓ |
6 |
if(!parser.open(fileName)){ |
136 |
✓✓✓✓
|
1 |
cerr << "parseFunc : can't parse file '"<<fileName<<"'" << endl; |
137 |
|
1 |
return false; |
138 |
|
|
} |
139 |
✓ |
5 |
parseFunctionFile(vecFunc, parser, removeIA32intrinsic); |
140 |
✓✓✓✓ ✓✓ |
5 |
std::cout << "parseFunc : found " << vecFunc.size() << " functions in file '"<<fileName<<"'" << std::endl; |
141 |
|
5 |
return true; |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
///Parse the list of function with list of input file |
145 |
|
|
/** @param[out] vecHeader : vector of header to be completed |
146 |
|
|
* @param vecInputFile : vector of the input files |
147 |
|
|
* @param removeIA32intrinsic : true to remove IA32 intrinsics (seems to be the buildin ones used by the compiler for ARM) |
148 |
|
|
* @return true on success, false otherwise |
149 |
|
|
*/ |
150 |
|
2 |
bool parserFuncListFile(PVecHeader & vecHeader, const std::vector<std::string> & vecInputFile, bool removeIA32intrinsic){ |
151 |
✗✓ |
2 |
if(vecInputFile.size() == 0lu){return false;} |
152 |
|
2 |
bool b(true); |
153 |
✓✓ |
8 |
for(std::vector<std::string>::const_iterator it(vecInputFile.begin()); it != vecInputFile.end(); ++it){ |
154 |
✓ |
12 |
PHeader header; |
155 |
✓✓✓ |
6 |
header.setName("plib_" + getFileName(*it)); |
156 |
✓✓✓✓
|
6 |
if(parseFunc(header.getVecFunc(), *it, removeIA32intrinsic)){ |
157 |
✓ |
5 |
vecHeader.push_back(header); |
158 |
|
5 |
b &= true; |
159 |
|
|
}else{ |
160 |
|
1 |
b &= false; |
161 |
|
|
} |
162 |
|
|
} |
163 |
|
2 |
return b; |
164 |
|
|
} |
165 |
|
|
|