Skip to content

Commit 4abf288

Browse files
committed
parser-common: introduce the parse_int() helper
... to deduplicate its code in parsers
1 parent 400a7a0 commit 4abf288

File tree

5 files changed

+42
-34
lines changed

5 files changed

+42
-34
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ add_library(cs STATIC
3838
instream.cc
3939
json-parser.cc
4040
json-writer.cc
41+
parser-common.cc
4142
shared-string.cc
4243
version.cc
4344
)

csparser.cc

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <sstream>
3030

3131
#include <boost/algorithm/string.hpp>
32-
#include <boost/lexical_cast.hpp>
3332
#include <boost/regex.hpp>
3433

3534
class LineReader {
@@ -185,20 +184,10 @@ EToken ErrFileLexer::readNext() {
185184
evt_.msg = sm[/* msg */ 5];
186185

187186
// parse line number
188-
try {
189-
evt_.line = boost::lexical_cast<int>(sm[/* line */ 2]);
190-
}
191-
catch (boost::bad_lexical_cast &) {
192-
evt_.line = 0;
193-
}
187+
evt_.line = parse_int(sm[/* line */ 2]);
194188

195189
// parse column number
196-
try {
197-
evt_.column = boost::lexical_cast<int>(sm[/* col */ 3]);
198-
}
199-
catch (boost::bad_lexical_cast &) {
200-
evt_.column = 0;
201-
}
190+
evt_.column = parse_int(sm[/* col */ 3]);
202191

203192
return T_EVENT;
204193
}

gcc-parser.cc

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <algorithm>
2525

2626
#include <boost/foreach.hpp>
27-
#include <boost/lexical_cast.hpp>
2827
#include <boost/regex.hpp>
2928

3029
enum EToken {
@@ -156,20 +155,10 @@ EToken Tokenizer::readNext(DefEvent *pEvt) {
156155
pEvt->fileName = sm[/* file */ 1];
157156

158157
// parse line number
159-
try {
160-
pEvt->line = boost::lexical_cast<int>(sm[/* line */ 2]);
161-
}
162-
catch (boost::bad_lexical_cast &) {
163-
pEvt->line = 0;
164-
}
158+
pEvt->line = parse_int(sm[/* line */ 2]);
165159

166160
// parse column number
167-
try {
168-
pEvt->column = boost::lexical_cast<int>(sm[/* col */ 3]);
169-
}
170-
catch (boost::bad_lexical_cast &) {
171-
pEvt->column = 0;
172-
}
161+
pEvt->column = parse_int(sm[/* col */ 3]);
173162

174163
return tok;
175164
}
@@ -574,14 +563,8 @@ void PostProcessor::transGccAnal(Defect *pDef) {
574563

575564
// pick CWE number if available
576565
const std::string rawMsg = sm[/* msg */ 1];
577-
if (boost::regex_match(rawMsg, sm, reGccAnalCwe_)) {
578-
try {
579-
pDef->cwe = boost::lexical_cast<int>(sm[/* cwe */ 1]);
580-
}
581-
catch (boost::bad_lexical_cast &) {
582-
pDef->cwe = 0;
583-
}
584-
}
566+
if (boost::regex_match(rawMsg, sm, reGccAnalCwe_))
567+
pDef->cwe = parse_int(sm[/* cwe */ 1]);
585568
}
586569

587570
void PostProcessor::apply(Defect *pDef) {

parser-common.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2020 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "parser-common.hh"
21+
22+
#include <boost/lexical_cast.hpp>
23+
24+
int parse_int(const std::string &str, const int fallback) {
25+
try {
26+
return boost::lexical_cast<int>(str);
27+
}
28+
catch (boost::bad_lexical_cast &) {
29+
return fallback;
30+
}
31+
}

parser-common.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
#ifndef H_GUARD_PARSER_COMMON_H
2121
#define H_GUARD_PARSER_COMMON_H
2222

23+
#include <string>
24+
2325
#define RE_EVENT_GCC "(?:(?:(?:fatal|internal) )?[a-z][\\[\\]A-Za-z0-9_-]+)"
2426
#define RE_EVENT_PROSPECTOR "(?:[A-Z]+[0-9]+\\[[a-z0-9-]+\\])"
2527
#define RE_EVENT RE_EVENT_GCC "|" RE_EVENT_PROSPECTOR
2628

29+
int parse_int(const std::string &, int fallback = 0);
30+
2731
#endif /* H_GUARD_PARSER_COMMON_H */

0 commit comments

Comments
 (0)