Skip to content

Commit 0bdfa07

Browse files
committed
parseRange
1 parent 1bfa431 commit 0bdfa07

3 files changed

Lines changed: 54 additions & 14 deletions

File tree

lib/suppressions.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,19 +736,7 @@ std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::stri
736736
break;
737737

738738
// optional range
739-
pos = comment.find_first_not_of(" \t", pos);
740-
if (pos >= comment.size())
741-
break;
742-
int rangeValue = 0;
743-
if (comment[pos] == '+') {
744-
const std::string::size_type posRangeStart = pos + 1;
745-
pos = comment.find_first_of(" \t", posRangeStart);
746-
if (pos > comment.size())
747-
break;
748-
const std::string range = comment.substr(posRangeStart, pos-posRangeStart);
749-
// TODO check that range is an integer
750-
rangeValue = std::stoi(range);
751-
}
739+
const int rangeValue = parseRange(comment, pos);
752740

753741
// ids..
754742
const std::set<std::string> ids = parseIds(comment, pos);
@@ -802,6 +790,27 @@ std::list<SuppressionList::Suppression> polyspace::Parser::parse(const std::stri
802790
return ret;
803791
}
804792

793+
794+
int polyspace::Parser::parseRange(const std::string& comment, std::string::size_type& pos) {
795+
pos = comment.find_first_not_of(" \t", pos);
796+
if (pos >= comment.size())
797+
return 0;
798+
if (comment[pos] != '+')
799+
return 0;
800+
const std::string::size_type startpos = pos + 1;
801+
std::string::size_type endpos = comment.find_first_of(" \t", startpos);
802+
if (endpos > comment.size())
803+
return 0;
804+
const std::string range = comment.substr(startpos, endpos-startpos);
805+
try {
806+
int ret = std::stoi(range);
807+
pos = endpos;
808+
return ret;
809+
} catch (const std::invalid_argument &) {
810+
}
811+
return 0;
812+
}
813+
805814
std::vector<std::pair<std::string, std::string>> polyspace::Parser::parseFamilyRules(const std::string& comment, std::string::size_type& pos) {
806815
std::vector<std::pair<std::string, std::string>> fr;
807816
std::string family;

lib/suppressions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ namespace polyspace {
310310

311311
std::list<SuppressionList::Suppression> parse(const std::string &comment, int line, const std::string &filename);
312312

313+
static int parseRange(const std::string& comment, std::string::size_type& pos);
313314
static std::vector<std::pair<std::string, std::string>> parseFamilyRules(const std::string& comment, std::string::size_type& pos);
314315

315316
private:
316317
std::set<std::string> parseIds(const std::string& comment, std::string::size_type& pos) const;
317318

318319
static CommentKind parseKind(const std::string& token, std::string::size_type& pos);
319-
int parseRange();
320320

321321
std::map<std::string, std::string> mFamilyMap;
322322
};

test/testsuppressions.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class TestSuppressions : public TestFixture {
121121

122122
TEST_CASE(suppressionWildcard);
123123

124+
TEST_CASE(polyspaceParseRange);
124125
TEST_CASE(polyspaceParseIds);
125126

126127
TEST_CASE(polyspaceMisraC2012);
@@ -1928,6 +1929,36 @@ class TestSuppressions : public TestFixture {
19281929
return ret.empty() ? ret : ret.substr(1);
19291930
}
19301931

1932+
void polyspaceParseRange() const {
1933+
std::string::size_type pos;
1934+
1935+
// Happy case
1936+
pos = 0;
1937+
ASSERT_EQUALS(12, polyspace::Parser::parseRange(" +12 ",pos));
1938+
ASSERT_EQUALS(4U, pos);
1939+
1940+
// Invalid range => pos will point at the token
1941+
pos = 0;
1942+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" ",pos));
1943+
ASSERT_EQUALS(std::string::npos, pos);
1944+
1945+
pos = 0;
1946+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" test ",pos));
1947+
ASSERT_EQUALS(1U, pos);
1948+
1949+
pos = 0;
1950+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +",pos));
1951+
ASSERT_EQUALS(1U, pos);
1952+
1953+
pos = 0;
1954+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +12",pos));
1955+
ASSERT_EQUALS(1U, pos);
1956+
1957+
pos = 0;
1958+
ASSERT_EQUALS(0, polyspace::Parser::parseRange(" +A ",pos));
1959+
ASSERT_EQUALS(1U, pos);
1960+
}
1961+
19311962
void polyspaceParseIds() const {
19321963
ASSERT_EQUALS("test:12", polyspaceParseIdsResults("abc test:12",3));
19331964
ASSERT_EQUALS("test:12", polyspaceParseIdsResults("abc test:12 [12]",3));

0 commit comments

Comments
 (0)