Skip to content

Commit aa927d5

Browse files
committed
Refactoring: Add new file lib/templatesimplifier.cpp
The plan is to move template simplification into this new class to take some lines from 10 000 line tokenizer.
1 parent f4703e0 commit aa927d5

7 files changed

Lines changed: 156 additions & 68 deletions

File tree

Makefile

Lines changed: 33 additions & 29 deletions
Large diffs are not rendered by default.

cppcheck.cbp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@
159159
<Unit filename="lib/suppressions.h" />
160160
<Unit filename="lib/symboldatabase.cpp" />
161161
<Unit filename="lib/symboldatabase.h" />
162+
<Unit filename="lib/templatesimplifier.cpp" />
163+
<Unit filename="lib/templatesimplifier.h" />
162164
<Unit filename="lib/timer.cpp" />
163165
<Unit filename="lib/timer.h" />
164166
<Unit filename="lib/token.cpp" />

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ HEADERS += $${BASEPATH}check.h \
3030
$${BASEPATH}settings.h \
3131
$${BASEPATH}suppressions.h \
3232
$${BASEPATH}symboldatabase.h \
33+
$${BASEPATH}templatesimplifier.h \
3334
$${BASEPATH}timer.h \
3435
$${BASEPATH}token.h \
3536
$${BASEPATH}tokenize.h
@@ -61,6 +62,7 @@ SOURCES += $${BASEPATH}check64bit.cpp \
6162
$${BASEPATH}settings.cpp \
6263
$${BASEPATH}suppressions.cpp \
6364
$${BASEPATH}symboldatabase.cpp \
65+
$${BASEPATH}templatesimplifier.cpp \
6466
$${BASEPATH}timer.cpp \
6567
$${BASEPATH}token.cpp \
6668
$${BASEPATH}tokenize.cpp

lib/templatesimplifier.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "templatesimplifier.h"
20+
#include "token.h"
21+
22+
//---------------------------------------------------------------------------
23+
#ifdef _MSC_VER
24+
#pragma warning(disable: 4503)
25+
#endif
26+
27+
28+
29+
//---------------------------------------------------------------------------
30+
31+
TemplateSimplifier::TemplateSimplifier()
32+
{
33+
}
34+
35+
TemplateSimplifier::~TemplateSimplifier()
36+
{
37+
}
38+
39+
void TemplateSimplifier::cleanupAfterSimplify(Token *tokens)
40+
{
41+
bool goback = false;
42+
for (Token *tok = tokens; tok; tok = tok->next()) {
43+
if (goback) {
44+
tok = tok->previous();
45+
goback = false;
46+
}
47+
if (tok->str() == "(")
48+
tok = tok->link();
49+
50+
else if (Token::Match(tok, "%type% <") &&
51+
(!tok->previous() || tok->previous()->str() == ";")) {
52+
const Token *tok2 = tok->tokAt(2);
53+
std::string type;
54+
while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) {
55+
type += tok2->str() + ",";
56+
tok2 = tok2->tokAt(2);
57+
}
58+
if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) {
59+
type += tok2->str();
60+
tok->str(tok->str() + "<" + type + ">");
61+
Token::eraseTokens(tok, tok2->tokAt(2));
62+
if (tok == tokens)
63+
goback = true;
64+
}
65+
}
66+
}
67+
}

lib/templatesimplifier.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
20+
//---------------------------------------------------------------------------
21+
#ifndef templatesimplifierH
22+
#define templatesimplifierH
23+
//---------------------------------------------------------------------------
24+
25+
26+
27+
class Token;
28+
29+
30+
/// @addtogroup Core
31+
/// @{
32+
33+
/** @brief Simplify templates from the preprocessed and partially simplified code. */
34+
class TemplateSimplifier {
35+
public:
36+
TemplateSimplifier();
37+
virtual ~TemplateSimplifier();
38+
39+
/**
40+
* Used after simplifyTemplates to perform a little cleanup.
41+
* Sometimes the simplifyTemplates isn't fully successful and then
42+
* there are function calls etc with "wrong" syntax.
43+
*/
44+
static void cleanupAfterSimplify(Token *tokens);
45+
};
46+
47+
/// @}
48+
49+
//---------------------------------------------------------------------------
50+
#endif

lib/tokenize.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "check.h"
3131
#include "path.h"
3232
#include "symboldatabase.h"
33+
#include "templatesimplifier.h"
3334

3435
#include <string>
3536
#include <cstring>
@@ -2324,7 +2325,7 @@ bool Tokenizer::tokenize(std::istream &code,
23242325
// then unsimplified function calls etc remain. These have the
23252326
// "wrong" syntax. So this function will just fix so that the
23262327
// syntax is corrected.
2327-
simplifyTemplates2();
2328+
TemplateSimplifier::cleanupAfterSimplify(_tokens);
23282329

23292330
// Simplify the operator "?:"
23302331
simplifyConditionOperator();
@@ -3519,37 +3520,6 @@ void Tokenizer::simplifyTemplates()
35193520
}
35203521
//---------------------------------------------------------------------------
35213522

3522-
void Tokenizer::simplifyTemplates2()
3523-
{
3524-
bool goback = false;
3525-
for (Token *tok = _tokens; tok; tok = tok->next()) {
3526-
if (goback) {
3527-
tok = tok->previous();
3528-
goback = false;
3529-
}
3530-
if (tok->str() == "(")
3531-
tok = tok->link();
3532-
3533-
else if (Token::Match(tok, "%type% <") &&
3534-
(!tok->previous() || tok->previous()->str() == ";")) {
3535-
const Token *tok2 = tok->tokAt(2);
3536-
std::string type;
3537-
while (Token::Match(tok2, "%type% ,") || Token::Match(tok2, "%num% ,")) {
3538-
type += tok2->str() + ",";
3539-
tok2 = tok2->tokAt(2);
3540-
}
3541-
if (Token::Match(tok2, "%type% > (") || Token::Match(tok2, "%num% > (")) {
3542-
type += tok2->str();
3543-
tok->str(tok->str() + "<" + type + ">");
3544-
Token::eraseTokens(tok, tok2->tokAt(2));
3545-
if (tok == _tokens)
3546-
goback = true;
3547-
}
3548-
}
3549-
}
3550-
}
3551-
//---------------------------------------------------------------------------
3552-
35533523
std::string Tokenizer::getNameForFunctionParams(const Token *start)
35543524
{
35553525
if (start->next() == start->link())

lib/tokenize.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -543,13 +543,6 @@ class Tokenizer {
543543
*/
544544
int simplifyTemplatesGetTemplateNamePosition(const Token *tok);
545545

546-
/**
547-
* Used after simplifyTemplates to perform a little cleanup.
548-
* Sometimes the simplifyTemplates isn't fully successful and then
549-
* there are function calls etc with "wrong" syntax.
550-
*/
551-
void simplifyTemplates2();
552-
553546
/**
554547
* Simplify e.g. 'atol("0")' into '0'
555548
*/

0 commit comments

Comments
 (0)