Skip to content

Commit 549e1bf

Browse files
committed
parser-json-gcc: read endLine/endColumn if available
Otherwise fallback to `caret`, which was the only field used so far. Related: #136
1 parent 50cfeea commit 549e1bf

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/lib/parser-json-gcc.cc

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,56 @@ GccTreeDecoder::GccTreeDecoder():
3232

3333
GccTreeDecoder::~GccTreeDecoder() = default;
3434

35+
static bool gccReadLocRegion(
36+
DefEvent *pEvt,
37+
const pt::ptree &beg,
38+
const pt::ptree &end)
39+
{
40+
// read file name
41+
pEvt->fileName = valueOf<std::string>(beg, "file", "<unknown>");
42+
if (pEvt->fileName != valueOf<std::string>(end, "file", "<unknown>"))
43+
return false;
44+
45+
// read line
46+
if ((pEvt->line = valueOf<int>(beg, "line"))) {
47+
const int endLine = valueOf<int>(end, "line");
48+
pEvt->vSize = diffNums(pEvt->line, endLine);
49+
}
50+
51+
// read column
52+
if ((pEvt->column = valueOf<int>(beg, "byte-column"))) {
53+
const int endColumn = valueOf<int>(end, "byte-column");
54+
pEvt->hSize = diffNums(pEvt->column, endColumn);
55+
}
56+
57+
return true;
58+
}
59+
60+
static void gccReadLocation(DefEvent *pEvt, const pt::ptree *locs)
61+
{
62+
using std::string;
63+
64+
if (locs->empty())
65+
return;
66+
67+
const pt::ptree &firstLoc = locs->begin()->second;
68+
69+
// try to read a region between start..finish
70+
const pt::ptree *beg, *end;
71+
if (findChildOf(&beg, firstLoc, "start")
72+
&& findChildOf(&end, firstLoc, "finish")
73+
&& gccReadLocRegion(pEvt, *beg, *end))
74+
return;
75+
76+
// fallback to caret
77+
const pt::ptree *caret;
78+
if (findChildOf(&caret, firstLoc, "caret")) {
79+
pEvt->fileName = valueOf<string>(*caret, "file", "<unknown>");
80+
pEvt->line = valueOf<int> (*caret, "line");
81+
pEvt->column = valueOf<int> (*caret, "byte-column");
82+
}
83+
}
84+
3585
static bool gccReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
3686
{
3787
using std::string;
@@ -45,14 +95,8 @@ static bool gccReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
4595
// read location
4696
pEvt->fileName = "<unknown>";
4797
const pt::ptree *locs;
48-
if (findChildOf(&locs, evtNode, "locations") && !locs->empty()) {
49-
const pt::ptree *caret;
50-
if (findChildOf(&caret, locs->begin()->second, "caret")) {
51-
pEvt->fileName = valueOf<string>(*caret, "file", "<unknown>");
52-
pEvt->line = valueOf<int> (*caret, "line");
53-
pEvt->column = valueOf<int> (*caret, "byte-column");
54-
}
55-
}
98+
if (findChildOf(&locs, evtNode, "locations"))
99+
gccReadLocation(pEvt, locs);
56100

57101
// read message
58102
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");

tests/csgrep/0094-gcc-json-stdout.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
{
5050
"file_name": "xxx.c",
5151
"line": 11,
52-
"column": 15,
52+
"column": 11,
53+
"h_size": 7,
5354
"event": "warning[-Wunused-parameter]",
5455
"message": "unused parameter ‘argc’",
5556
"verbosity_level": 0
@@ -65,7 +66,8 @@
6566
{
6667
"file_name": "xxx.c",
6768
"line": 11,
68-
"column": 27,
69+
"column": 21,
70+
"h_size": 11,
6971
"event": "warning[-Wunused-parameter]",
7072
"message": "unused parameter ‘argv’",
7173
"verbosity_level": 0

0 commit comments

Comments
 (0)