Skip to content

Commit a837f61

Browse files
danmardmcppcheckclaude
authored
Fix #735 (Refactoring: split TokenList::readfile() into smaller functions) (#736)
Co-authored-by: Daniel Marjamäki <daniel.marjamaki@cppcheck.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cab2758 commit a837f61

2 files changed

Lines changed: 152 additions & 85 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 131 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,133 @@ void simplecpp::TokenList::lineDirective(unsigned int fileIndex_, unsigned int l
726726

727727
static const std::string COMMENT_END("*/");
728728

729+
/**
730+
* Read a name or a number.
731+
* In a number, a ' digit separator is removed, so 1'000 gives "1000".
732+
* The stream is left after the last character of the token.
733+
* @param stream the stream to read from
734+
* @param ch the first character, already read from the stream
735+
* @return the name or number
736+
*/
737+
static simplecpp::TokenString readNameOrNumber(simplecpp::TokenList::Stream &stream, unsigned char ch)
738+
{
739+
simplecpp::TokenString currentToken;
740+
const bool num = !!std::isdigit(ch);
741+
while (stream.good() && isNameChar(ch)) {
742+
currentToken += ch;
743+
ch = stream.readChar();
744+
if (num && ch=='\'' && isNameChar(stream.peekChar()))
745+
ch = stream.readChar();
746+
}
747+
stream.ungetChar();
748+
return currentToken;
749+
}
750+
751+
/**
752+
* Read a // comment, up to but not including the newline.
753+
* A backslash followed by a newline continues the comment on the next line.
754+
* The backslash, and any spaces or tabs after it, are then replaced by a
755+
* newline in the comment text.
756+
* @param stream the stream to read from
757+
* @param ch the first '/', already read from the stream
758+
* @param location the location of the comment, used for warnings
759+
* @param multiline incremented for each continued line
760+
* @param outputList if not null, a portability warning is added when there is
761+
* whitespace between the backslash and the newline
762+
* @return the comment text, including the leading //
763+
*/
764+
static simplecpp::TokenString readLineComment(simplecpp::TokenList::Stream &stream, unsigned char ch, const simplecpp::Location &location, unsigned int &multiline, simplecpp::OutputList *outputList)
765+
{
766+
simplecpp::TokenString currentToken;
767+
while (stream.good() && ch != '\n') {
768+
currentToken += ch;
769+
ch = stream.readChar();
770+
if (ch == '\\') {
771+
simplecpp::TokenString tmp;
772+
char tmp_ch = ch;
773+
while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
774+
tmp += tmp_ch;
775+
tmp_ch = stream.readChar();
776+
}
777+
if (!stream.good()) {
778+
break;
779+
}
780+
781+
if (tmp_ch != '\n') {
782+
currentToken += tmp;
783+
} else {
784+
const simplecpp::TokenString check_portability = currentToken + tmp;
785+
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
786+
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
787+
portabilityBackslash(outputList, location);
788+
++multiline;
789+
tmp_ch = stream.readChar();
790+
currentToken += '\n';
791+
}
792+
ch = tmp_ch;
793+
}
794+
}
795+
if (ch == '\n') {
796+
stream.ungetChar();
797+
}
798+
return currentToken;
799+
}
800+
801+
simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline)
802+
{
803+
TokenString currentToken = "/*";
804+
(void)stream.readChar();
805+
unsigned char ch = stream.readChar();
806+
while (stream.good()) {
807+
currentToken += ch;
808+
if (currentToken.size() >= 4U && endsWith(currentToken, COMMENT_END))
809+
break;
810+
ch = stream.readChar();
811+
}
812+
// multiline..
813+
814+
std::string::size_type pos = 0;
815+
while ((pos = currentToken.find("\\\n",pos)) != std::string::npos) {
816+
currentToken.erase(pos,2);
817+
++multiline;
818+
}
819+
if (multiline || isLastLinePreprocessor()) {
820+
pos = 0;
821+
while ((pos = currentToken.find('\n',pos)) != std::string::npos) {
822+
currentToken.erase(pos,1);
823+
++multiline;
824+
}
825+
}
826+
return currentToken;
827+
}
828+
829+
void simplecpp::TokenList::readErrorText(Stream &stream, unsigned char ch, Location &location)
830+
{
831+
TokenString currentToken;
832+
char prev = ' ';
833+
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
834+
currentToken += ch;
835+
prev = ch;
836+
ch = stream.readChar();
837+
}
838+
stream.ungetChar();
839+
std::string::size_type pos = 0;
840+
unsigned int spliced = 0;
841+
while ((pos = currentToken.find('\\', pos)) != std::string::npos) {
842+
if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') {
843+
currentToken.erase(pos, 2);
844+
++spliced;
845+
} else {
846+
++pos;
847+
}
848+
}
849+
if (!currentToken.empty()) {
850+
push_back(new Token(currentToken, location));
851+
location.adjust(currentToken);
852+
}
853+
location.line += spliced;
854+
}
855+
729856
void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, const DUI &dui, OutputList *outputList)
730857
{
731858
unsigned int multiline = 0U;
@@ -880,105 +1007,24 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
8801007
if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') {
8811008
const Token* const ppTok = cback()->previous;
8821009
if (ppTok->next && (ppTok->next->str() == "error" || ppTok->next->str() == "warning")) {
883-
char prev = ' ';
884-
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
885-
currentToken += ch;
886-
prev = ch;
887-
ch = stream.readChar();
888-
}
889-
stream.ungetChar();
890-
std::string::size_type pos = 0;
891-
unsigned int spliced = 0;
892-
while ((pos = currentToken.find('\\', pos)) != std::string::npos) {
893-
if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') {
894-
currentToken.erase(pos, 2);
895-
++spliced;
896-
} else {
897-
++pos;
898-
}
899-
}
900-
if (!currentToken.empty()) {
901-
push_back(new Token(currentToken, location));
902-
location.adjust(currentToken);
903-
}
904-
location.line += spliced;
1010+
readErrorText(stream, ch, location);
9051011
continue;
9061012
}
9071013
}
9081014

9091015
// number or name
9101016
if (isNameChar(ch)) {
911-
const bool num = !!std::isdigit(ch);
912-
while (stream.good() && isNameChar(ch)) {
913-
currentToken += ch;
914-
ch = stream.readChar();
915-
if (num && ch=='\'' && isNameChar(stream.peekChar()))
916-
ch = stream.readChar();
917-
}
918-
919-
stream.ungetChar();
1017+
currentToken = readNameOrNumber(stream, ch);
9201018
}
9211019

9221020
// comment
9231021
else if (ch == '/' && stream.peekChar() == '/') {
924-
while (stream.good() && ch != '\n') {
925-
currentToken += ch;
926-
ch = stream.readChar();
927-
if (ch == '\\') {
928-
TokenString tmp;
929-
char tmp_ch = ch;
930-
while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
931-
tmp += tmp_ch;
932-
tmp_ch = stream.readChar();
933-
}
934-
if (!stream.good()) {
935-
break;
936-
}
937-
938-
if (tmp_ch != '\n') {
939-
currentToken += tmp;
940-
} else {
941-
const TokenString check_portability = currentToken + tmp;
942-
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
943-
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
944-
portabilityBackslash(outputList, location);
945-
++multiline;
946-
tmp_ch = stream.readChar();
947-
currentToken += '\n';
948-
}
949-
ch = tmp_ch;
950-
}
951-
}
952-
if (ch == '\n') {
953-
stream.ungetChar();
954-
}
1022+
currentToken = readLineComment(stream, ch, location, multiline, outputList);
9551023
}
9561024

9571025
// comment
9581026
else if (ch == '/' && stream.peekChar() == '*') {
959-
currentToken = "/*";
960-
(void)stream.readChar();
961-
ch = stream.readChar();
962-
while (stream.good()) {
963-
currentToken += ch;
964-
if (currentToken.size() >= 4U && endsWith(currentToken, COMMENT_END))
965-
break;
966-
ch = stream.readChar();
967-
}
968-
// multiline..
969-
970-
std::string::size_type pos = 0;
971-
while ((pos = currentToken.find("\\\n",pos)) != std::string::npos) {
972-
currentToken.erase(pos,2);
973-
++multiline;
974-
}
975-
if (multiline || isLastLinePreprocessor()) {
976-
pos = 0;
977-
while ((pos = currentToken.find('\n',pos)) != std::string::npos) {
978-
currentToken.erase(pos,1);
979-
++multiline;
980-
}
981-
}
1027+
currentToken = readBlockComment(stream, multiline);
9821028
}
9831029

9841030
// string / char literal

‎simplecpp.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,27 @@ namespace simplecpp {
429429
const Token* lastLineTok(int maxsize=1000) const;
430430
const Token* isLastLinePreprocessor(int maxsize=1000) const;
431431

432+
/**
433+
* Read a block comment, up to and including the closing star-slash.
434+
* Each backslash-newline is removed from the text. If the comment is
435+
* on a continued line or in a preprocessor directive, its newlines are
436+
* removed too, so that the directive keeps going after the comment.
437+
* @param stream the stream to read from. The first '/' is already
438+
* read, and the next character is '*'.
439+
* @param multiline incremented for each newline that is removed
440+
* @return the comment text
441+
*/
442+
TokenString readBlockComment(Stream &stream, unsigned int &multiline);
443+
/**
444+
* Read the text of a #error or #warning directive, up to but not
445+
* including the newline, and add it as one token. A backslash-newline
446+
* continues the text on the next line and is removed from it.
447+
* @param stream the stream to read from
448+
* @param ch the first character of the text, already read from the stream
449+
* @param location the location of the text. Updated to point after it.
450+
*/
451+
void readErrorText(Stream &stream, unsigned char ch, Location &location);
452+
432453
unsigned int fileIndex(const std::string &filename);
433454

434455
Token *frontToken;

0 commit comments

Comments
 (0)