| 1 |
/* |
| 2 |
|
| 3 |
SPDX-FileCopyrightText: 2015 The Qt Company Ltd. |
| 4 |
SPDX-FileCopyrightText: 2015 Olivier Goffart <[email protected]> |
| 5 |
Contact: http://www.qt.io/licensing/ |
| 6 |
|
| 7 |
This file contains code copied from Qt 5.6 (https://github.com/qt/qtbase/blob/5.6/src/corelib/kernel/qmetaobject.cpp). |
| 8 |
|
| 9 |
SPDX-License-Identifier: LGPL-2.1-only WITH Qt-LGPL-exception-1.1 OR LGPL-3.0-only WITH Qt-LGPL-exception-1.1 OR LicenseRef-Qt-Commercial |
| 10 |
|
| 11 |
*/ |
| 12 |
|
| 13 |
#ifndef CLAZY_NORMALIZED_SIGNATURE_UTILS_H |
| 14 |
#define CLAZY_NORMALIZED_SIGNATURE_UTILS_H |
| 15 |
|
| 16 |
#include <string> |
| 17 |
|
| 18 |
namespace clazy |
| 19 |
{ |
| 20 |
inline bool is_space(char s) |
| 21 |
{ |
| 22 |
return (s == ' ' || s == '\t'); |
| 23 |
} |
| 24 |
|
| 25 |
inline bool is_ident_start(char s) |
| 26 |
{ |
| 27 |
return ((s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_' || s == '$'); |
| 28 |
} |
| 29 |
|
| 30 |
inline bool is_ident_char(char s) |
| 31 |
{ |
| 32 |
return ((s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || (s >= '0' && s <= '9') || s == '_' || s == '$'); |
| 33 |
} |
| 34 |
|
| 35 |
static void qRemoveWhitespace(const char *s, char *d) |
| 36 |
{ |
| 37 |
char last = 0; |
| 38 |
while (*s && is_space(*s)) { |
| 39 |
s++; |
| 40 |
} |
| 41 |
while (*s) { |
| 42 |
while (*s && !is_space(*s)) { |
| 43 |
last = *d++ = *s++; |
| 44 |
} |
| 45 |
while (*s && is_space(*s)) { |
| 46 |
s++; |
| 47 |
} |
| 48 |
if (*s && ((is_ident_char(*s) && is_ident_char(last)) || ((*s == ':') && (last == '<')))) { |
| 49 |
last = *d++ = ' '; |
| 50 |
} |
| 51 |
} |
| 52 |
*d = '\0'; |
| 53 |
} |
| 54 |
|
| 55 |
// This function is shared with moc.cpp. This file should be included where needed. |
| 56 |
static std::string normalizeTypeInternal(const char *t, const char *e, bool fixScope = false, bool adjustConst = true) |
| 57 |
{ |
| 58 |
int len = e - t; |
| 59 |
/* |
| 60 |
Convert 'char const *' into 'const char *'. Start at index 1, |
| 61 |
not 0, because 'const char *' is already OK. |
| 62 |
*/ |
| 63 |
std::string constbuf; |
| 64 |
for (int i = 1; i < len; i++) { |
| 65 |
if (t[i] == 'c' && strncmp(t + i + 1, "onst", 4) == 0 && (i + 5 >= len || !is_ident_char(t[i + 5])) && !is_ident_char(t[i - 1])) { |
| 66 |
constbuf = std::string(t, len); |
| 67 |
if (is_space(t[i - 1])) { |
| 68 |
constbuf.erase(i - 1, 6); |
| 69 |
} else { |
| 70 |
constbuf.erase(i, 5); |
| 71 |
} |
| 72 |
constbuf = "const " + constbuf; |
| 73 |
t = constbuf.data(); |
| 74 |
e = constbuf.data() + constbuf.length(); |
| 75 |
break; |
| 76 |
} |
| 77 |
/* |
| 78 |
We mustn't convert 'char * const *' into 'const char **' |
| 79 |
and we must beware of 'Bar<const Bla>'. |
| 80 |
*/ |
| 81 |
if (t[i] == '&' || t[i] == '*' || t[i] == '<') { |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
if (adjustConst && e > t + 6 && strncmp("const ", t, 6) == 0) { |
| 86 |
if (*(e - 1) == '&') { // treat const reference as value |
| 87 |
t += 6; |
| 88 |
--e; |
| 89 |
} else if (is_ident_char(*(e - 1)) || *(e - 1) == '>') { // treat const value as value |
| 90 |
t += 6; |
| 91 |
} |
| 92 |
} |
| 93 |
std::string result; |
| 94 |
|
| 95 |
#if 1 |
| 96 |
// consume initial 'const ' |
| 97 |
if (strncmp("const ", t, 6) == 0) { |
| 98 |
t += 6; |
| 99 |
result += "const "; |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
// some type substitutions for 'unsigned x' |
| 104 |
if (strncmp("unsigned", t, 8) == 0) { |
| 105 |
// make sure "unsigned" is an isolated word before making substitutions |
| 106 |
if (!t[8] || !is_ident_char(t[8])) { |
| 107 |
if (strncmp(" int", t + 8, 4) == 0) { |
| 108 |
t += 8 + 4; |
| 109 |
result += "uint"; |
| 110 |
} else if (strncmp(" long", t + 8, 5) == 0) { |
| 111 |
if ((strlen(t + 8 + 5) < 4 || strncmp(t + 8 + 5, " int", 4) != 0) // preserve '[unsigned] long int' |
| 112 |
&& (strlen(t + 8 + 5) < 5 || strncmp(t + 8 + 5, " long", 5) != 0) // preserve '[unsigned] long long' |
| 113 |
) { |
| 114 |
t += 8 + 5; |
| 115 |
result += "ulong"; |
| 116 |
} |
| 117 |
} else if (strncmp(" short", t + 8, 6) != 0 // preserve unsigned short |
| 118 |
&& strncmp(" char", t + 8, 5) != 0) { // preserve unsigned char |
| 119 |
// treat rest (unsigned) as uint |
| 120 |
t += 8; |
| 121 |
result += "uint"; |
| 122 |
} |
| 123 |
} |
| 124 |
} else { |
| 125 |
// discard 'struct', 'class', and 'enum'; they are optional |
| 126 |
// and we don't want them in the normalized signature |
| 127 |
struct { |
| 128 |
const char *keyword; |
| 129 |
int len; |
| 130 |
} optional[] = {{"struct ", 7}, {"class ", 6}, {"enum ", 5}, {nullptr, 0}}; |
| 131 |
int i = 0; |
| 132 |
do { |
| 133 |
if (strncmp(optional[i].keyword, t, optional[i].len) == 0) { |
| 134 |
t += optional[i].len; |
| 135 |
break; |
| 136 |
} |
| 137 |
} while (optional[++i].keyword != nullptr); |
| 138 |
} |
| 139 |
|
| 140 |
bool star = false; |
| 141 |
while (t != e) { |
| 142 |
char c = *t++; |
| 143 |
if (fixScope && c == ':' && *t == ':') { |
| 144 |
++t; |
| 145 |
c = *t++; |
| 146 |
int i = result.size() - 1; |
| 147 |
while (i >= 0 && is_ident_char(result.at(i))) { |
| 148 |
--i; |
| 149 |
} |
| 150 |
result.resize(i + 1); |
| 151 |
} |
| 152 |
star = star || c == '*'; |
| 153 |
result += c; |
| 154 |
if (c == '<') { |
| 155 |
// template recursion |
| 156 |
const char *tt = t; |
| 157 |
int templdepth = 1; |
| 158 |
int scopeDepth = 0; |
| 159 |
while (t != e) { |
| 160 |
c = *t++; |
| 161 |
if (c == '{' || c == '(' || c == '[') { |
| 162 |
++scopeDepth; |
| 163 |
} |
| 164 |
if (c == '}' || c == ')' || c == ']') { |
| 165 |
--scopeDepth; |
| 166 |
} |
| 167 |
if (scopeDepth == 0) { |
| 168 |
if (c == '<') { |
| 169 |
++templdepth; |
| 170 |
} |
| 171 |
if (c == '>') { |
| 172 |
--templdepth; |
| 173 |
} |
| 174 |
if (templdepth == 0 || (templdepth == 1 && c == ',')) { |
| 175 |
result += normalizeTypeInternal(tt, t - 1, fixScope, false); |
| 176 |
result += c; |
| 177 |
if (templdepth == 0) { |
| 178 |
if (*t == '>') { |
| 179 |
result += ' '; // avoid >> |
| 180 |
} |
| 181 |
break; |
| 182 |
} |
| 183 |
tt = t; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// cv qualifers can appear after the type as well |
| 190 |
if (!is_ident_char(c) && t != e && (e - t >= 5 && strncmp("const", t, 5) == 0) && (e - t == 5 || !is_ident_char(t[5]))) { |
| 191 |
t += 5; |
| 192 |
while (t != e && is_space(*t)) { |
| 193 |
++t; |
| 194 |
} |
| 195 |
if (adjustConst && t != e && *t == '&') { |
| 196 |
// treat const ref as value |
| 197 |
++t; |
| 198 |
} else if (adjustConst && !star) { |
| 199 |
// treat const as value |
| 200 |
} else if (!star) { |
| 201 |
// move const to the front (but not if const comes after a *) |
| 202 |
result = "const " + result; |
| 203 |
} else { |
| 204 |
// keep const after a * |
| 205 |
result += "const"; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
return result; |
| 211 |
} |
| 212 |
|
| 213 |
inline char *qNormalizeType(char *d, int &templdepth, std::string &result) |
| 214 |
{ |
| 215 |
const char *t = d; |
| 216 |
while (*d && (templdepth || (*d != ',' && *d != ')'))) { |
| 217 |
if (*d == '<') { |
| 218 |
++templdepth; |
| 219 |
} |
| 220 |
if (*d == '>') { |
| 221 |
--templdepth; |
| 222 |
} |
| 223 |
++d; |
| 224 |
} |
| 225 |
// "void" should only be removed if this is part of a signature that has |
| 226 |
// an explicit void argument; e.g., "void foo(void)" --> "void foo()" |
| 227 |
if (strncmp("void)", t, d - t + 1) != 0) { |
| 228 |
result += normalizeTypeInternal(t, d); |
| 229 |
} |
| 230 |
|
| 231 |
return d; |
| 232 |
} |
| 233 |
|
| 234 |
inline std::string normalizedType(const char *type) |
| 235 |
{ |
| 236 |
std::string result; |
| 237 |
|
| 238 |
if (!type || !*type) { |
| 239 |
return result; |
| 240 |
} |
| 241 |
|
| 242 |
char *stackbuf = new char[strlen(type) + 1]; |
| 243 |
qRemoveWhitespace(type, stackbuf); |
| 244 |
int templdepth = 0; |
| 245 |
qNormalizeType(stackbuf, templdepth, result); |
| 246 |
delete[] stackbuf; |
| 247 |
|
| 248 |
return result; |
| 249 |
} |
| 250 |
|
| 251 |
inline std::string normalizedSignature(const char *method) |
| 252 |
{ |
| 253 |
std::string result; |
| 254 |
if (!method || !*method) { |
| 255 |
return result; |
| 256 |
} |
| 257 |
int len = int(strlen(method)); |
| 258 |
// char *stackbuf = new char[len + 1]; |
| 259 |
char *stackbuf = new char[len + 1]; |
| 260 |
char *d = stackbuf; |
| 261 |
qRemoveWhitespace(method, d); |
| 262 |
|
| 263 |
result.reserve(len); |
| 264 |
|
| 265 |
int argdepth = 0; |
| 266 |
int templdepth = 0; |
| 267 |
while (*d) { |
| 268 |
if (argdepth == 1) { |
| 269 |
d = qNormalizeType(d, templdepth, result); |
| 270 |
if (!*d) { // most likely an invalid signature. |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
if (*d == '(') { |
| 275 |
++argdepth; |
| 276 |
} |
| 277 |
if (*d == ')') { |
| 278 |
--argdepth; |
| 279 |
} |
| 280 |
result += *d++; |
| 281 |
} |
| 282 |
|
| 283 |
delete[] stackbuf; |
| 284 |
return result; |
| 285 |
} |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
#endif |