Skip to content

Commit 2e7f7c3

Browse files
dmcppcheckclaude
andcommitted
Fix #735 (Refactoring: split TokenList::readfile() into smaller functions)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cab2758 commit 2e7f7c3

2 files changed

Lines changed: 113 additions & 85 deletions

File tree

‎simplecpp.cpp‎

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

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

729+
static simplecpp::TokenString readNameOrNumber(simplecpp::TokenList::Stream &stream, unsigned char ch)
730+
{
731+
simplecpp::TokenString currentToken;
732+
const bool num = !!std::isdigit(ch);
733+
while (stream.good() && isNameChar(ch)) {
734+
currentToken += ch;
735+
ch = stream.readChar();
736+
if (num && ch=='\'' && isNameChar(stream.peekChar()))
737+
ch = stream.readChar();
738+
}
739+
stream.ungetChar();
740+
return currentToken;
741+
}
742+
743+
static simplecpp::TokenString readLineComment(simplecpp::TokenList::Stream &stream, unsigned char ch, const simplecpp::Location &location, unsigned int &multiline, simplecpp::OutputList *outputList)
744+
{
745+
simplecpp::TokenString currentToken;
746+
while (stream.good() && ch != '\n') {
747+
currentToken += ch;
748+
ch = stream.readChar();
749+
if (ch == '\\') {
750+
simplecpp::TokenString tmp;
751+
char tmp_ch = ch;
752+
while ((stream.good()) && (tmp_ch == '\\' || tmp_ch == ' ' || tmp_ch == '\t')) {
753+
tmp += tmp_ch;
754+
tmp_ch = stream.readChar();
755+
}
756+
if (!stream.good()) {
757+
break;
758+
}
759+
760+
if (tmp_ch != '\n') {
761+
currentToken += tmp;
762+
} else {
763+
const simplecpp::TokenString check_portability = currentToken + tmp;
764+
const std::string::size_type pos = check_portability.find_last_not_of(" \t");
765+
if (pos < check_portability.size() - 1U && check_portability[pos] == '\\')
766+
portabilityBackslash(outputList, location);
767+
++multiline;
768+
tmp_ch = stream.readChar();
769+
currentToken += '\n';
770+
}
771+
ch = tmp_ch;
772+
}
773+
}
774+
if (ch == '\n') {
775+
stream.ungetChar();
776+
}
777+
return currentToken;
778+
}
779+
780+
simplecpp::TokenString simplecpp::TokenList::readBlockComment(Stream &stream, unsigned int &multiline)
781+
{
782+
TokenString currentToken = "/*";
783+
(void)stream.readChar();
784+
unsigned char ch = stream.readChar();
785+
while (stream.good()) {
786+
currentToken += ch;
787+
if (currentToken.size() >= 4U && endsWith(currentToken, COMMENT_END))
788+
break;
789+
ch = stream.readChar();
790+
}
791+
// multiline..
792+
793+
std::string::size_type pos = 0;
794+
while ((pos = currentToken.find("\\\n",pos)) != std::string::npos) {
795+
currentToken.erase(pos,2);
796+
++multiline;
797+
}
798+
if (multiline || isLastLinePreprocessor()) {
799+
pos = 0;
800+
while ((pos = currentToken.find('\n',pos)) != std::string::npos) {
801+
currentToken.erase(pos,1);
802+
++multiline;
803+
}
804+
}
805+
return currentToken;
806+
}
807+
808+
void simplecpp::TokenList::readErrorText(Stream &stream, unsigned char ch, Location &location)
809+
{
810+
TokenString currentToken;
811+
char prev = ' ';
812+
while (stream.good() && (prev == '\\' || (ch != '\r' && ch != '\n'))) {
813+
currentToken += ch;
814+
prev = ch;
815+
ch = stream.readChar();
816+
}
817+
stream.ungetChar();
818+
std::string::size_type pos = 0;
819+
unsigned int spliced = 0;
820+
while ((pos = currentToken.find('\\', pos)) != std::string::npos) {
821+
if (pos + 1 < currentToken.size() && currentToken[pos + 1] == '\n') {
822+
currentToken.erase(pos, 2);
823+
++spliced;
824+
} else {
825+
++pos;
826+
}
827+
}
828+
if (!currentToken.empty()) {
829+
push_back(new Token(currentToken, location));
830+
location.adjust(currentToken);
831+
}
832+
location.line += spliced;
833+
}
834+
729835
void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename, const DUI &dui, OutputList *outputList)
730836
{
731837
unsigned int multiline = 0U;
@@ -880,105 +986,24 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
880986
if (cback() && cback()->location.line == location.line && cback()->previous && cback()->previous->op == '#') {
881987
const Token* const ppTok = cback()->previous;
882988
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;
989+
readErrorText(stream, ch, location);
905990
continue;
906991
}
907992
}
908993

909994
// number or name
910995
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();
996+
currentToken = readNameOrNumber(stream, ch);
920997
}
921998

922999
// comment
9231000
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-
}
1001+
currentToken = readLineComment(stream, ch, location, multiline, outputList);
9551002
}
9561003

9571004
// comment
9581005
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-
}
1006+
currentToken = readBlockComment(stream, multiline);
9821007
}
9831008

9841009
// string / char literal

‎simplecpp.h‎

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

432+
TokenString readBlockComment(Stream &stream, unsigned int &multiline);
433+
void readErrorText(Stream &stream, unsigned char ch, Location &location);
434+
432435
unsigned int fileIndex(const std::string &filename);
433436

434437
Token *frontToken;

0 commit comments

Comments
 (0)