@@ -95,6 +95,7 @@ static const simplecpp::TokenString HAS_INCLUDE("__has_include");
9595
9696template <class T > static std::string toString (T t)
9797{
98+ // NOLINTNEXTLINE(misc-const-correctness) - false positive
9899 std::ostringstream ostr;
99100 ostr << t;
100101 return ostr.str ();
@@ -284,7 +285,7 @@ void simplecpp::TokenList::clear()
284285{
285286 backToken = nullptr ;
286287 while (frontToken) {
287- Token *next = frontToken->next ;
288+ Token * const next = frontToken->next ;
288289 delete frontToken;
289290 frontToken = next;
290291 }
@@ -350,9 +351,9 @@ static unsigned char readChar(std::istream &istr, unsigned int bom)
350351 if (bom == 0 && static_cast <char >(istr.peek ()) == ' \n ' )
351352 (void )istr.get ();
352353 else if (bom == 0xfeff || bom == 0xfffe ) {
353- int c1 = istr.get ();
354- int c2 = istr.get ();
355- int ch16 = (bom == 0xfeff ) ? (c1<<8 | c2) : (c2<<8 | c1);
354+ const int c1 = istr.get ();
355+ const int c2 = istr.get ();
356+ const int ch16 = (bom == 0xfeff ) ? (c1<<8 | c2) : (c2<<8 | c1);
356357 if (ch16 != ' \n ' ) {
357358 istr.unget ();
358359 istr.unget ();
@@ -397,7 +398,7 @@ static unsigned short getAndSkipBOM(std::istream &istr)
397398
398399 // The UTF-16 BOM is 0xfffe or 0xfeff.
399400 if (ch1 >= 0xfe ) {
400- unsigned short bom = (static_cast <unsigned char >(istr.get ()) << 8 );
401+ const unsigned short bom = (static_cast <unsigned char >(istr.get ()) << 8 );
401402 if (istr.peek () >= 0xfe )
402403 return bom | static_cast <unsigned char >(istr.get ());
403404 istr.unget ();
@@ -428,7 +429,7 @@ static std::string escapeString(const std::string &str)
428429 std::ostringstream ostr;
429430 ostr << ' \" ' ;
430431 for (std::size_t i = 1U ; i < str.size () - 1 ; ++i) {
431- char c = str[i];
432+ const char c = str[i];
432433 if (c == ' \\ ' || c == ' \" ' || c == ' \' ' )
433434 ostr << ' \\ ' ;
434435 ostr << c;
@@ -859,7 +860,7 @@ void simplecpp::TokenList::combineOperators()
859860 start = start->previous ;
860861 }
861862 if (indentlevel == -1 && start) {
862- const Token *ftok = start;
863+ const Token * const ftok = start;
863864 bool isFuncDecl = ftok->name ;
864865 while (isFuncDecl) {
865866 if (!start->name && start->str () != " ::" && start->op != ' *' && start->op != ' &' )
@@ -957,10 +958,10 @@ void simplecpp::TokenList::constFoldMulDivRem(Token *tok)
957958 if (tok->op == ' *' )
958959 result = (stringToLL (tok->previous ->str ()) * stringToLL (tok->next ->str ()));
959960 else if (tok->op == ' /' || tok->op == ' %' ) {
960- long long rhs = stringToLL (tok->next ->str ());
961+ const long long rhs = stringToLL (tok->next ->str ());
961962 if (rhs == 0 )
962963 throw std::overflow_error (" division/modulo by zero" );
963- long long lhs = stringToLL (tok->previous ->str ());
964+ const long long lhs = stringToLL (tok->previous ->str ());
964965 if (rhs == -1 && lhs == std::numeric_limits<long long >::min ())
965966 throw std::overflow_error (" division overflow" );
966967 if (tok->op == ' /' )
@@ -1159,7 +1160,7 @@ void simplecpp::TokenList::removeComments()
11591160{
11601161 Token *tok = frontToken;
11611162 while (tok) {
1162- Token *tok1 = tok;
1163+ Token * const tok1 = tok;
11631164 tok = tok->next ;
11641165 if (tok1->comment )
11651166 deleteToken (tok1);
@@ -1395,7 +1396,7 @@ namespace simplecpp {
13951396 TokenList rawtokens2 (inputFiles);
13961397 const Location loc (macro2tok->location );
13971398 while (macro2tok) {
1398- Token *next = macro2tok->next ;
1399+ Token * const next = macro2tok->next ;
13991400 rawtokens2.push_back (new Token (macro2tok->str (), loc));
14001401 output2.deleteToken (macro2tok);
14011402 macro2tok = next;
@@ -1791,7 +1792,7 @@ namespace simplecpp {
17911792 TokenList temp2 (files);
17921793 temp2.push_back (new Token (temp.cback ()->str (), tok->location ));
17931794
1794- const Token *tok2 = appendTokens (&temp2, loc, tok->next , macros, expandedmacros, parametertokens);
1795+ const Token * const tok2 = appendTokens (&temp2, loc, tok->next , macros, expandedmacros, parametertokens);
17951796 if (!tok2)
17961797 return tok->next ;
17971798 output->takeTokens (temp);
@@ -1832,7 +1833,7 @@ namespace simplecpp {
18321833 }
18331834 TokenList tokens (files);
18341835 tokens.push_back (new Token (*tok));
1835- const Token *tok2 = appendTokens (&tokens, loc, tok->next , macros, expandedmacros, parametertokens);
1836+ const Token * const tok2 = appendTokens (&tokens, loc, tok->next , macros, expandedmacros, parametertokens);
18361837 if (!tok2) {
18371838 output->push_back (newMacroToken (tok->str (), loc, true , tok));
18381839 return tok->next ;
@@ -1843,9 +1844,9 @@ namespace simplecpp {
18431844 }
18441845
18451846 if (tok->str () == DEFINED) {
1846- const Token *tok2 = tok->next ;
1847- const Token *tok3 = tok2 ? tok2->next : nullptr ;
1848- const Token *tok4 = tok3 ? tok3->next : nullptr ;
1847+ const Token * const tok2 = tok->next ;
1848+ const Token * const tok3 = tok2 ? tok2->next : nullptr ;
1849+ const Token * const tok4 = tok3 ? tok3->next : nullptr ;
18491850 const Token *defToken = nullptr ;
18501851 const Token *lastToken = nullptr ;
18511852 if (sameline (tok, tok4) && tok2->op == ' (' && tok3->name && tok4->op == ' )' ) {
@@ -1955,12 +1956,12 @@ namespace simplecpp {
19551956 if (!sameline (tok, tok->next ) || !sameline (tok, tok->next ->next ))
19561957 throw invalidHashHash::unexpectedNewline (tok->location , name ());
19571958
1958- bool canBeConcatenatedWithEqual = A->isOneOf (" +-*/%&|^" ) || A->str () == " <<" || A->str () == " >>" ;
1959- bool canBeConcatenatedStringOrChar = isStringLiteral_ (A->str ()) || isCharLiteral_ (A->str ());
1959+ const bool canBeConcatenatedWithEqual = A->isOneOf (" +-*/%&|^" ) || A->str () == " <<" || A->str () == " >>" ;
1960+ const bool canBeConcatenatedStringOrChar = isStringLiteral_ (A->str ()) || isCharLiteral_ (A->str ());
19601961 if (!A->name && !A->number && A->op != ' ,' && !A->str ().empty () && !canBeConcatenatedWithEqual && !canBeConcatenatedStringOrChar)
19611962 throw invalidHashHash::unexpectedToken (tok->location , name (), A);
19621963
1963- Token *B = tok->next ->next ;
1964+ Token * const B = tok->next ->next ;
19641965 if (!B->name && !B->number && B->op && !B->isOneOf (" #=" ))
19651966 throw invalidHashHash::unexpectedToken (tok->location , name (), B);
19661967
@@ -2033,7 +2034,7 @@ namespace simplecpp {
20332034 if (tokensB.empty () && sameline (B,B->next ) && B->next ->op ==' (' ) {
20342035 const MacroMap::const_iterator it = macros.find (strAB);
20352036 if (it != macros.end () && expandedmacros.find (strAB) == expandedmacros.end () && it->second .functionLike ()) {
2036- const Token *tok2 = appendTokens (&tokens, loc, B->next , macros, expandedmacros, parametertokens);
2037+ const Token * const tok2 = appendTokens (&tokens, loc, B->next , macros, expandedmacros, parametertokens);
20372038 if (tok2)
20382039 nextTok = tok2->next ;
20392040 }
@@ -2094,7 +2095,7 @@ namespace simplecpp {
20942095
20952096 std::string::size_type pos = 0 ;
20962097 if (cygwinPath.size () >= 11 && startsWith (cygwinPath, " /cygdrive/" )) {
2097- unsigned char driveLetter = cygwinPath[10 ];
2098+ const unsigned char driveLetter = cygwinPath[10 ];
20982099 if (std::isalpha (driveLetter)) {
20992100 if (cygwinPath.size () == 11 ) {
21002101 windowsPath = toupper (driveLetter);
@@ -2440,10 +2441,10 @@ static unsigned long long stringToULLbounded(
24402441 std::size_t maxlen = std::string::npos
24412442)
24422443{
2443- std::string sub = s.substr (pos, maxlen);
2444- const char * start = sub.c_str ();
2444+ const std::string sub = s.substr (pos, maxlen);
2445+ const char * const start = sub.c_str ();
24452446 char * end;
2446- unsigned long long value = std::strtoull (start, &end, base);
2447+ const unsigned long long value = std::strtoull (start, &end, base);
24472448 pos += end - start;
24482449 if (end - start < minlen)
24492450 throw std::runtime_error (" expected digit" );
@@ -2516,7 +2517,7 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
25162517
25172518 if (str[pos] == ' \\ ' ) {
25182519 pos++;
2519- char escape = str[pos++];
2520+ const char escape = str[pos++];
25202521
25212522 if (pos >= str.size ())
25222523 throw std::runtime_error (" unexpected end of character literal" );
@@ -2583,7 +2584,7 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
25832584 case ' u' :
25842585 case ' U' : {
25852586 // universal character names have exactly 4 or 8 digits
2586- std::size_t ndigits = (escape == ' u' ? 4 : 8 );
2587+ const std::size_t ndigits = (escape == ' u' ? 4 : 8 );
25872588 value = stringToULLbounded (str, pos, 16 , ndigits, ndigits);
25882589
25892590 // UTF-8 encodes code points above 0x7f in multiple code units
@@ -2626,7 +2627,7 @@ long long simplecpp::characterLiteralToLL(const std::string& str)
26262627 if (pos + 1 >= str.size ())
26272628 throw std::runtime_error (" assumed UTF-8 encoded source, but character literal ends unexpectedly" );
26282629
2629- unsigned char c = str[pos++];
2630+ const unsigned char c = str[pos++];
26302631
26312632 if (((c >> 6 ) != 2 ) // ensure c has form 0xb10xxxxxx
26322633 || (!value && additional_bytes == 1 && c < 0xa0 ) // overlong 3-bytes encoding
@@ -2686,7 +2687,7 @@ static void simplifyNumbers(simplecpp::TokenList &expr)
26862687static void simplifyComments (simplecpp::TokenList &expr)
26872688{
26882689 for (simplecpp::Token *tok = expr.front (); tok;) {
2689- simplecpp::Token *d = tok;
2690+ simplecpp::Token * const d = tok;
26902691 tok = tok->next ;
26912692 if (d->comment )
26922693 expr.deleteToken (d);
@@ -2890,11 +2891,11 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
28902891
28912892 const std::string &sourcefile = rawtok->location .file ();
28922893
2893- const Token *htok = rawtok->nextSkipComments ();
2894+ const Token * const htok = rawtok->nextSkipComments ();
28942895 if (!sameline (rawtok, htok))
28952896 continue ;
28962897
2897- bool systemheader = (htok->str ()[0 ] == ' <' );
2898+ const bool systemheader = (htok->str ()[0 ] == ' <' );
28982899 const std::string header (realFilename (htok->str ().substr (1U , htok->str ().size () - 2U )));
28992900 if (hasFile (ret, sourcefile, header, dui, systemheader))
29002901 continue ;
@@ -2915,7 +2916,7 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
29152916
29162917static bool preprocessToken (simplecpp::TokenList &output, const simplecpp::Token **tok1, simplecpp::MacroMap ¯os, std::vector<std::string> &files, simplecpp::OutputList *outputList)
29172918{
2918- const simplecpp::Token *tok = *tok1;
2919+ const simplecpp::Token * const tok = *tok1;
29192920 const simplecpp::MacroMap::const_iterator it = macros.find (tok->str ());
29202921 if (it != macros.end ()) {
29212922 simplecpp::TokenList value (files);
@@ -3098,7 +3099,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
30983099 try {
30993100 const Macro ¯o = Macro (rawtok->previous , files);
31003101 if (dui.undefined .find (macro.name ()) == dui.undefined .end ()) {
3101- MacroMap::iterator it = macros.find (macro.name ());
3102+ const MacroMap::iterator it = macros.find (macro.name ());
31023103 if (it == macros.end ())
31033104 macros.insert (std::pair<TokenString, Macro>(macro.name (), macro));
31043105 else
@@ -3156,7 +3157,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
31563157 return ;
31573158 }
31583159
3159- const Token *inctok = inc2.cfront ();
3160+ const Token * const inctok = inc2.cfront ();
31603161
31613162 const bool systemheader = (inctok->op == ' <' );
31623163 const std::string header (realFilename (inctok->str ().substr (1U , inctok->str ().size () - 2U )));
@@ -3166,7 +3167,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
31663167 std::ifstream f;
31673168 header2 = openHeader (f, dui, rawtok->location .file (), header, systemheader);
31683169 if (f.is_open ()) {
3169- TokenList *tokens = new TokenList (f, files, header2, outputList);
3170+ TokenList * const tokens = new TokenList (f, files, header2, outputList);
31703171 filedata[header2] = tokens;
31713172 }
31723173 }
@@ -3188,7 +3189,7 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
31883189 }
31893190 } else if (pragmaOnce.find (header2) == pragmaOnce.end ()) {
31903191 includetokenstack.push (gotoNextLine (rawtok));
3191- const TokenList *includetokens = filedata.find (header2)->second ;
3192+ const TokenList * const includetokens = filedata.find (header2)->second ;
31923193 rawtok = includetokens ? includetokens->cfront () : nullptr ;
31933194 continue ;
31943195 }
0 commit comments