diff options
| author | Christian Kandeler <christian.kandeler@qt.io> | 2022-01-12 13:51:52 +0100 |
|---|---|---|
| committer | Christian Kandeler <christian.kandeler@qt.io> | 2022-01-12 13:40:24 +0000 |
| commit | 0326bbdc8186279974d4c52fb3edecf1303cdc77 (patch) | |
| tree | f9dfe1f2ce6c5250f1c70a78a3de19ce25b02bd5 /src/libs/cplusplus/pp-engine.cpp | |
| parent | 9056d708fa0886efaf10c7504d185afa0747a947 (diff) | |
| download | qt-creator-0326bbdc8186279974d4c52fb3edecf1303cdc77.tar.gz | |
CPlusPlus: Do not needlessly allocate space for a vector
It's not a rare ocurrence that the vector will stay empty, so let's not
reserve memory unconditionally.
Task-number: QTCREATORBUG-26841
Change-Id: I842620cfa1fd0571691829401e4ccc162ab61d7b
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/libs/cplusplus/pp-engine.cpp')
| -rw-r--r-- | src/libs/cplusplus/pp-engine.cpp | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index b3cfe6bd15..5e0a7f1b69 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -1147,7 +1147,12 @@ bool Preprocessor::handleFunctionLikeMacro(const Macro *macro, unsigned baseLine) { QVector<PPToken> expanded; - expanded.reserve(MAX_TOKEN_EXPANSION_COUNT); + + const auto addToken = [&expanded](PPToken &&tok) { + if (expanded.isEmpty()) + expanded.reserve(50); + expanded.push_back(std::move(tok)); + }; const size_t bodySize = body.size(); for (size_t i = 0; i < bodySize && expanded.size() < MAX_TOKEN_EXPANSION_COUNT; @@ -1189,10 +1194,10 @@ bool Preprocessor::handleFunctionLikeMacro(const Macro *macro, enclosedString.replace("\\", "\\\\"); enclosedString.replace("\"", "\\\""); - expanded.push_back(generateToken(T_STRING_LITERAL, - enclosedString.constData(), - enclosedString.size(), - lineno, true)); + addToken(generateToken(T_STRING_LITERAL, + enclosedString.constData(), + enclosedString.size(), + lineno, true)); } else { for (int k = 0; k < actualsSize; ++k) { // Mark the actual tokens (which are the replaced version of the @@ -1202,9 +1207,9 @@ bool Preprocessor::handleFunctionLikeMacro(const Macro *macro, actual.f.expanded = true; if (k == 0) actual.f.whitespace = bodyTk.whitespace(); - expanded += actual; if (k == actualsSize - 1) lineno = actual.lineno; + addToken(std::move(actual)); } } @@ -1219,12 +1224,12 @@ bool Preprocessor::handleFunctionLikeMacro(const Macro *macro, // No formal macro parameter for this identifier in the body. bodyTk.f.generated = true; bodyTk.lineno = baseLine; - expanded.push_back(std::move(bodyTk)); + addToken(std::move(bodyTk)); } } else if (bodyTk.isNot(T_POUND) && bodyTk.isNot(T_POUND_POUND)) { bodyTk.f.generated = true; bodyTk.lineno = baseLine; - expanded.push_back(std::move(bodyTk)); + addToken(std::move(bodyTk)); } if (i > 1 && body[int(i) - 1].is(T_POUND_POUND)) { |
