Skip to content

Commit 5122daf

Browse files
committed
csgrep: --add-input-lines option for --mode=json
If --add-input-lines option is used, DefEvents output as json will have "input_line" referencing the line number of the input file. Signed-off-by: Jan Matufka <jmatufka@redhat.com>
1 parent 9ea9212 commit 5122daf

17 files changed

Lines changed: 184 additions & 15 deletions

src/csgrep.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ int main(int argc, char *argv[])
619619
("file-glob", "expand glob patterns in the names of input files")
620620
("ignore-case,i", "ignore case when matching regular expressions")
621621
("ignore-parser-warnings", "if enabled, parser warnings about the input files do not affect exit code")
622+
("add-input-lines", "if enabled, events in mode=json will also contain input_line numbers from the original input file/stream")
622623
("invert-match,v", "select defects that do not match the selected criteria")
623624
("invert-regex,n", "invert regular expressions in all predicates")
624625
("filter-file,f", po::value<TStringList>(), "read custom filtering rules from a file in JSON format");
@@ -639,7 +640,7 @@ int main(int argc, char *argv[])
639640
p.add("input-file", -1);
640641

641642
po::store(po::parse_command_line(argc, argv, desc), vm);
642-
po::notify(vm);
643+
po::notify(vm);
643644

644645
po::options_description opts;
645646
opts.add(desc).add(hidden);
@@ -735,6 +736,9 @@ int main(int argc, char *argv[])
735736
if (vm.count("ignore-parser-warnings"))
736737
eng->setIgnoreParserWarnings(true);
737738

739+
if (vm.count("add-input-lines"))
740+
eng->setAddInputLines(true);
741+
738742
bool hasError = false;
739743

740744
// if no input file is given, read from stdin

src/lib/defect.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct DefEvent {
4949
std::string fileName;
5050
int line = 0;
5151
int column = 0;
52+
int inputLine = 0;
5253
std::string event;
5354
std::string msg;
5455

src/lib/instream.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
#include "instream.hh"
2121

22-
InStream::InStream(const std::string &fileName, const bool silent):
22+
InStream::InStream(const std::string &fileName, const bool silent, const bool addInputLines):
2323
fileName_(fileName),
2424
silent_(silent),
25+
addInputLines_(addInputLines),
2526
str_((fileName_ == "-")
2627
? std::cin
2728
: fileStr_)

src/lib/instream.hh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,23 @@ struct InFileException {
3838

3939
class InStream {
4040
public:
41-
InStream(const std::string &fileName, bool silent = false);
41+
InStream(const std::string &fileName, bool silent = false,
42+
bool addInputLines = false);
4243
InStream(std::istringstream &str, bool silent = false);
4344
~InStream() = default;
4445

45-
const std::string& fileName() const { return fileName_; }
46-
std::istream& str() const { return str_; }
47-
bool silent() const { return silent_; }
48-
bool anyError() const { return anyError_; }
46+
const std::string& fileName() const { return fileName_; }
47+
std::istream& str() const { return str_; }
48+
bool silent() const { return silent_; }
49+
bool addInputLines() const { return addInputLines_; }
50+
bool anyError() const { return anyError_; }
4951

5052
void handleError(const std::string &msg = "", unsigned long line = 0UL);
5153

5254
private:
5355
const std::string fileName_;
5456
const bool silent_;
57+
const bool addInputLines_ = false;
5558
bool anyError_ = false;
5659
std::ifstream fileStr_;
5760
std::istream &str_;

src/lib/parser-cov.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ std::ostream& operator<<(std::ostream &str, EToken code)
114114

115115
class ErrFileLexer {
116116
public:
117-
ErrFileLexer(std::istream &input):
117+
ErrFileLexer(std::istream &input, bool addInputLines = false):
118118
lineReader_(input),
119+
addInputLines_(addInputLines),
119120
hasError_(false)
120121
{
121122
}
@@ -140,6 +141,7 @@ class ErrFileLexer {
140141

141142
private:
142143
LineReader lineReader_;
144+
bool addInputLines_;
143145
bool hasError_;
144146
Defect def_;
145147
DefEvent evt_;
@@ -181,6 +183,8 @@ EToken ErrFileLexer::readNext()
181183
evt_ = DefEvent();
182184
evt_.event = sm[/* # */ 1];
183185
evt_.msg = sm[/* msg */ 2];
186+
if (addInputLines_)
187+
evt_.inputLine = lineReader_.lineNo();
184188
return T_COMMENT;
185189
}
186190

@@ -202,6 +206,9 @@ EToken ErrFileLexer::readNext()
202206
evt_.event = sm[/* event */ 4];
203207
evt_.msg = sm[/* msg */ 5];
204208

209+
if (addInputLines_)
210+
evt_.inputLine = lineReader_.lineNo();
211+
205212
return T_EVENT;
206213
}
207214

@@ -523,7 +530,7 @@ struct CovParser::Private {
523530
ImpliedAttrDigger digger;
524531

525532
Private(InStream &input_):
526-
lexer(input_.str()),
533+
lexer(input_.str(), input_.addInputLines()),
527534
fileName(input_.fileName()),
528535
silent(input_.silent()),
529536
hasError(false),

src/lib/parser-gcc.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ class AbstractTokenFilter: public ITokenizer {
6868

6969
class Tokenizer: public ITokenizer {
7070
public:
71-
Tokenizer(std::istream &input):
71+
Tokenizer(std::istream &input, bool addInputLines = false):
7272
input_(input),
73-
lineNo_(0)
73+
lineNo_(0),
74+
addInputLines_(addInputLines)
7475
{
7576
}
7677

@@ -83,6 +84,7 @@ class Tokenizer: public ITokenizer {
8384
private:
8485
std::istream &input_;
8586
int lineNo_;
87+
bool addInputLines_;
8688

8789
const RE reSideBar_ =
8890
RE("^ *((([0-9]+)? \\| )|(\\+\\+\\+ \\|\\+)).*$");
@@ -131,6 +133,9 @@ EToken Tokenizer::readNext(DefEvent *pEvt)
131133
*pEvt = DefEvent();
132134
pEvt->msg = line;
133135

136+
if (addInputLines_)
137+
pEvt->inputLine = lineNo_;
138+
134139
// check for line markers produced by gcc-9.2.1 (a.k.a. sidebar)
135140
if (boost::regex_match(pEvt->msg, reSideBar_))
136141
// xxx.c:2:1: note: include '<stdlib.h>' or provide a declaration...
@@ -387,7 +392,7 @@ EToken MultilineConcatenator::readNext(DefEvent *pEvt)
387392
class BasicGccParser {
388393
public:
389394
BasicGccParser(InStream &input):
390-
rawTokenizer_(input.str()),
395+
rawTokenizer_(input.str(), input.addInputLines()),
391396
noiseFilter_(&rawTokenizer_),
392397
markerConverter_(&noiseFilter_),
393398
tokenizer_(&markerConverter_),
@@ -535,6 +540,7 @@ bool BasicGccParser::getNext(Defect *pDef)
535540
DefEvent evt;
536541

537542
const EToken tok = tokenizer_.readNext(&evt);
543+
538544
switch (tok) {
539545
case T_NULL:
540546
if (!hasKeyEvent_ && !defCurrent_.events.empty())
@@ -828,7 +834,7 @@ bool GccParser::getNext(Defect *pDef)
828834
while (d->core.getNext(&d->lastDef) && d->tryMerge(pDef))
829835
;
830836

831-
// initialize verbosityLevel
837+
// initialize verbosityLevel
832838
// FIXME: similar code to KeyEventDigger::initVerbosity()
833839
TEvtList &evtList = pDef->events;
834840
const unsigned evtCount = evtList.size();

src/lib/parser-json-simple.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ SimpleTreeDecoder::Private::Private(InStream &input):
6868
};
6969

7070
// known per-event subnodes
71+
// 'input_line' is whitelisted, but currently there is no use-case for it
72+
// so it is not re-read
7173
this->nodeStore[NK_EVENT] = {
7274
"column",
7375
"event",
7476
"file_name",
7577
"h_size",
78+
"input_line",
7679
"line",
7780
"message",
7881
"v_size",
@@ -189,4 +192,3 @@ bool SimpleTreeDecoder::readNode(Defect *def)
189192

190193
return true;
191194
}
192-

src/lib/writer-json-simple.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static array simpleEncodeEvents(const TEvtList &events)
4949
evtNode["h_size"] = evt.hSize;
5050
if (0 < evt.vSize)
5151
evtNode["v_size"] = evt.vSize;
52+
if (0 < evt.inputLine)
53+
evtNode["input_line"] = evt.inputLine;
5254

5355
// describe the event
5456
evtNode["event"] = evt.event;

src/lib/writer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool AbstractWriter::handleFile(InStream &input)
5656
bool AbstractWriter::handleFile(const std::string &fileName, bool silent)
5757
{
5858
try {
59-
InStream str(fileName, silent);
59+
InStream str(fileName, silent, addInputLines_);
6060
return this->handleFile(str);
6161
}
6262
catch (const InFileException &e) {

src/lib/writer.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ class AbstractWriter {
6262
ignoreParserWarnings_ = val;
6363
}
6464

65+
void setAddInputLines(const bool val) {
66+
addInputLines_ = val;
67+
}
6568

6669
private:
6770
EFileFormat inputFormat_ = FF_INVALID;
6871
const TScanProps emptyProps_{};
6972
bool ignoreParserWarnings_ = false;
73+
bool addInputLines_ = false;
7074
};
7175

7276
using TWriterPtr = std::unique_ptr<AbstractWriter>;

0 commit comments

Comments
 (0)