-
Notifications
You must be signed in to change notification settings - Fork 17
csgrep: --add-input-lines option for --mode=json #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ struct DefEvent { | |
| std::string fileName; | ||
| int line = 0; | ||
| int column = 0; | ||
| int inputLine = 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also (optionally) record |
||
| std::string event; | ||
| std::string msg; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,8 +114,9 @@ std::ostream& operator<<(std::ostream &str, EToken code) | |
|
|
||
| class ErrFileLexer { | ||
| public: | ||
| ErrFileLexer(std::istream &input): | ||
| ErrFileLexer(std::istream &input, bool addInputLines = false): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be easier if the |
||
| lineReader_(input), | ||
| addInputLines_(addInputLines), | ||
| hasError_(false) | ||
| { | ||
| } | ||
|
|
@@ -140,6 +141,7 @@ class ErrFileLexer { | |
|
|
||
| private: | ||
| LineReader lineReader_; | ||
| bool addInputLines_; | ||
| bool hasError_; | ||
| Defect def_; | ||
| DefEvent evt_; | ||
|
|
@@ -181,6 +183,8 @@ EToken ErrFileLexer::readNext() | |
| evt_ = DefEvent(); | ||
| evt_.event = sm[/* # */ 1]; | ||
| evt_.msg = sm[/* msg */ 2]; | ||
| if (addInputLines_) | ||
| evt_.inputLine = lineReader_.lineNo(); | ||
| return T_COMMENT; | ||
| } | ||
|
|
||
|
|
@@ -202,6 +206,9 @@ EToken ErrFileLexer::readNext() | |
| evt_.event = sm[/* event */ 4]; | ||
| evt_.msg = sm[/* msg */ 5]; | ||
|
|
||
| if (addInputLines_) | ||
| evt_.inputLine = lineReader_.lineNo(); | ||
|
|
||
| return T_EVENT; | ||
| } | ||
|
|
||
|
|
@@ -523,7 +530,7 @@ struct CovParser::Private { | |
| ImpliedAttrDigger digger; | ||
|
|
||
| Private(InStream &input_): | ||
| lexer(input_.str()), | ||
| lexer(input_.str(), input_.addInputLines()), | ||
| fileName(input_.fileName()), | ||
| silent(input_.silent()), | ||
| hasError(false), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,9 +68,10 @@ class AbstractTokenFilter: public ITokenizer { | |
|
|
||
| class Tokenizer: public ITokenizer { | ||
| public: | ||
| Tokenizer(std::istream &input): | ||
| Tokenizer(std::istream &input, bool addInputLines = false): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the |
||
| input_(input), | ||
| lineNo_(0) | ||
| lineNo_(0), | ||
| addInputLines_(addInputLines) | ||
| { | ||
| } | ||
|
|
||
|
|
@@ -83,6 +84,7 @@ class Tokenizer: public ITokenizer { | |
| private: | ||
| std::istream &input_; | ||
| int lineNo_; | ||
| bool addInputLines_; | ||
|
|
||
| const RE reSideBar_ = | ||
| RE("^ *((([0-9]+)? \\| )|(\\+\\+\\+ \\|\\+)).*$"); | ||
|
|
@@ -131,6 +133,9 @@ EToken Tokenizer::readNext(DefEvent *pEvt) | |
| *pEvt = DefEvent(); | ||
| pEvt->msg = line; | ||
|
|
||
| if (addInputLines_) | ||
| pEvt->inputLine = lineNo_; | ||
|
|
||
| // check for line markers produced by gcc-9.2.1 (a.k.a. sidebar) | ||
| if (boost::regex_match(pEvt->msg, reSideBar_)) | ||
| // xxx.c:2:1: note: include '<stdlib.h>' or provide a declaration... | ||
|
|
@@ -387,7 +392,7 @@ EToken MultilineConcatenator::readNext(DefEvent *pEvt) | |
| class BasicGccParser { | ||
| public: | ||
| BasicGccParser(InStream &input): | ||
| rawTokenizer_(input.str()), | ||
| rawTokenizer_(input.str(), input.addInputLines()), | ||
| noiseFilter_(&rawTokenizer_), | ||
| markerConverter_(&noiseFilter_), | ||
| tokenizer_(&markerConverter_), | ||
|
|
@@ -535,6 +540,7 @@ bool BasicGccParser::getNext(Defect *pDef) | |
| DefEvent evt; | ||
|
|
||
| const EToken tok = tokenizer_.readNext(&evt); | ||
|
|
||
| switch (tok) { | ||
| case T_NULL: | ||
| if (!hasKeyEvent_ && !defCurrent_.events.empty()) | ||
|
|
@@ -828,7 +834,7 @@ bool GccParser::getNext(Defect *pDef) | |
| while (d->core.getNext(&d->lastDef) && d->tryMerge(pDef)) | ||
| ; | ||
|
|
||
| // initialize verbosityLevel | ||
| // initialize verbosityLevel | ||
| // FIXME: similar code to KeyEventDigger::initVerbosity() | ||
| TEvtList &evtList = pDef->events; | ||
| const unsigned evtCount = evtList.size(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,11 +68,14 @@ SimpleTreeDecoder::Private::Private(InStream &input): | |
| }; | ||
|
|
||
| // known per-event subnodes | ||
| // 'input_line' is whitelisted, but currently there is no use-case for it | ||
| // so it is not re-read | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why cannot we preserve non-zero |
||
| this->nodeStore[NK_EVENT] = { | ||
| "column", | ||
| "event", | ||
| "file_name", | ||
| "h_size", | ||
| "input_line", | ||
| "line", | ||
| "message", | ||
| "v_size", | ||
|
|
@@ -189,4 +192,3 @@ bool SimpleTreeDecoder::readNode(Defect *def) | |
|
|
||
| return true; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,8 @@ static array simpleEncodeEvents(const TEvtList &events) | |
| evtNode["h_size"] = evt.hSize; | ||
| if (0 < evt.vSize) | ||
| evtNode["v_size"] = evt.vSize; | ||
| if (0 < evt.inputLine) | ||
| evtNode["input_line"] = evt.inputLine; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to record |
||
|
|
||
| // describe the event | ||
| evtNode["event"] = evt.event; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| --mode=json --add-input-lines |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Error: SHELLCHECK_WARNING: | ||
| /etc/rc.d/rc.sysinit:492:13: warning: Quote this to prevent word splitting. [SC2046] | ||
| # 490| if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then | ||
| # 491| if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then | ||
| # 492|-> restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1 | ||
| # 493| fi | ||
| # 494| fi |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { | ||
| "defects": [ | ||
| { | ||
| "checker": "SHELLCHECK_WARNING", | ||
| "language": "shell", | ||
| "tool": "shellcheck", | ||
| "hash_v1": "b6311c1fdc52c47d4279cd6650af36e6f8299960", | ||
| "key_event_idx": 0, | ||
| "events": [ | ||
| { | ||
| "file_name": "/etc/rc.d/rc.sysinit", | ||
| "line": 492, | ||
| "column": 13, | ||
| "input_line": 2, | ||
| "event": "warning", | ||
| "message": "Quote this to prevent word splitting. [SC2046]", | ||
| "verbosity_level": 0 | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 3, | ||
| "event": "#", | ||
| "message": " 490| if [ -n \"$SELINUX_STATE\" -a \"$READONLY\" != \"yes\" ]; then", | ||
| "verbosity_level": 1 | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 4, | ||
| "event": "#", | ||
| "message": " 491| if [ -f /.autorelabel ] || strstr \"$cmdline\" autorelabel ; then", | ||
| "verbosity_level": 1 | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 5, | ||
| "event": "#", | ||
| "message": " 492|-> \trestorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\\// { print $2 }' /etc/fstab) >/dev/null 2>&1", | ||
| "verbosity_level": 1 | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 6, | ||
| "event": "#", | ||
| "message": " 493| fi", | ||
| "verbosity_level": 1 | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 7, | ||
| "event": "#", | ||
| "message": " 494| fi", | ||
| "verbosity_level": 1 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| { | ||
| "defects": [ | ||
| { | ||
| "checker": "SHELLCHECK_WARNING", | ||
| "language": "shell", | ||
| "key_event_idx": 0, | ||
| "events": [ | ||
| { | ||
| "file_name": "/etc/rc.d/rc.sysinit", | ||
| "line": 492, | ||
| "column": 13, | ||
| "input_line": 2, | ||
| "event": "warning", | ||
| "message": "Quote this to prevent word splitting. [SC2046]", | ||
| "verbosity_level": "0" | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 3, | ||
| "event": "#", | ||
| "message": " 490| if [ -n \"$SELINUX_STATE\" -a \"$READONLY\" != \"yes\" ]; then", | ||
| "verbosity_level": "1" | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 4, | ||
| "event": "#", | ||
| "message": " 491| if [ -f /.autorelabel ] || strstr \"$cmdline\" autorelabel ; then", | ||
| "verbosity_level": "1" | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 5, | ||
| "event": "#", | ||
| "message": " 492|-> \trestorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\\// { print $2 }' /etc/fstab) >/dev/null 2>&1", | ||
| "verbosity_level": "1" | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 6, | ||
| "event": "#", | ||
| "message": " 493| fi", | ||
| "verbosity_level": "1" | ||
| }, | ||
| { | ||
| "file_name": "", | ||
| "line": 0, | ||
| "input_line": 7, | ||
| "event": "#", | ||
| "message": " 494| fi", | ||
| "verbosity_level": "1" | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Error: SHELLCHECK_WARNING: | ||
| /etc/rc.d/rc.sysinit:492:13: warning: Quote this to prevent word splitting. [SC2046] | ||
| # 490| if [ -n "$SELINUX_STATE" -a "$READONLY" != "yes" ]; then | ||
| # 491| if [ -f /.autorelabel ] || strstr "$cmdline" autorelabel ; then | ||
| # 492|-> restorecon $(awk '!/^#/ && $4 !~ /noauto/ && $2 ~ /^\// { print $2 }' /etc/fstab) >/dev/null 2>&1 | ||
| # 493| fi | ||
| # 494| fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
--add-input-linesoption name does not sound intuitive for what it does. Without the context I would understand it as if some input text was being added rather than line numbers. As mentioned above, we should record the file names, too. What about naming it--record-input-locationsinstead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I also rename the JSON fields to
input_line_locationandinput_file_location?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use
input_fileandinput_line. I see location as an abstraction over file, line, column. Something like: https://github.com/gcc-mirror/gcc/blob/5cd3889135d77bf951e4ffe169868b453c36257d/libcpp/include/line-map.h#L1293