Skip to content

Commit 3b2f4d5

Browse files
committed
parser-json-shchk: separate module for ShellCheckTreeDecoder
1 parent 2a4f2c2 commit 3b2f4d5

File tree

4 files changed

+123
-62
lines changed

4 files changed

+123
-62
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ add_library(cs STATIC
7979
parser-json-cov.cc
8080
parser-json-gcc.cc
8181
parser-json-sarif.cc
82+
parser-json-shchk.cc
8283
parser-json-simple.cc
8384
parser-xml.cc
8485
shared-string.cc

src/parser-json-shchk.cc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (C) 2012-2022 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-json-shchk.hh"
21+
22+
#include "parser-gcc.hh" // for GccPostProcessor
23+
24+
struct ShellCheckTreeDecoder::Private {
25+
GccPostProcessor postProc;
26+
};
27+
28+
ShellCheckTreeDecoder::ShellCheckTreeDecoder():
29+
d(new Private)
30+
{
31+
}
32+
33+
ShellCheckTreeDecoder::~ShellCheckTreeDecoder() = default;
34+
35+
static bool scReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
36+
{
37+
using std::string;
38+
39+
// read level (error, warning, note)
40+
string &evtName = pEvt->event;
41+
evtName = valueOf<string>(evtNode, "level", "");
42+
if (evtName.empty())
43+
return false;
44+
45+
// read location
46+
pEvt->fileName = valueOf<string>(evtNode, "file", "<unknown>");
47+
pEvt->line = valueOf<int> (evtNode, "line", 0);
48+
pEvt->column = valueOf<int> (evtNode, "byte-column", 0);
49+
50+
// read message
51+
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");
52+
53+
// append [SC...] if available
54+
const string code = valueOf<string>(evtNode, "code", "");
55+
if (!code.empty())
56+
pEvt->msg += " [SC" + code + "]";
57+
58+
return true;
59+
}
60+
61+
bool ShellCheckTreeDecoder::readNode(Defect *def)
62+
{
63+
// move the iterator after we get the current position
64+
const pt::ptree *pNode = this->nextNode();
65+
if (!pNode)
66+
// failed initialization or EOF
67+
return false;
68+
69+
const pt::ptree &defNode = *pNode;
70+
71+
*def = Defect("SHELLCHECK_WARNING");
72+
73+
// read key event
74+
def->events.push_back(DefEvent());
75+
if (!scReadEvent(&def->events.back(), defNode))
76+
return false;
77+
78+
// TODO: go through fix/replacements nodes
79+
80+
// apply post-processing rules
81+
d->postProc.apply(def);
82+
83+
return true;
84+
}

src/parser-json-shchk.hh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2012-2022 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+
#ifndef H_GUARD_PARSER_JSON_SHCHK_H
21+
#define H_GUARD_PARSER_JSON_SHCHK_H
22+
23+
#include "abstract-tree.hh"
24+
25+
/// tree decoder of the JSON format produced by ShellCheck
26+
class ShellCheckTreeDecoder: public AbstractTreeDecoder {
27+
public:
28+
ShellCheckTreeDecoder();
29+
~ShellCheckTreeDecoder() override;
30+
bool readNode(Defect *def) override;
31+
32+
private:
33+
struct Private;
34+
std::unique_ptr<Private> d;
35+
};
36+
37+
#endif /* H_GUARD_PARSER_JSON_SHCHK_H */

src/parser-json.cc

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,14 @@
1919

2020
#include "parser-json.hh"
2121

22-
#include "abstract-tree.hh"
23-
#include "parser-gcc.hh" // for GccPostProcessor
2422
#include "parser-json-cov.hh"
2523
#include "parser-json-gcc.hh"
2624
#include "parser-json-sarif.hh"
25+
#include "parser-json-shchk.hh"
2726
#include "parser-json-simple.hh"
2827

2928
#include <boost/property_tree/json_parser.hpp>
3029

31-
/// tree decoder of the JSON format produced by ShellCheck
32-
class ShellCheckTreeDecoder: public AbstractTreeDecoder {
33-
public:
34-
bool readNode(Defect *def) override;
35-
36-
private:
37-
const GccPostProcessor postProc;
38-
};
39-
4030
struct JsonParser::Private {
4131
using TDecoderPtr = std::unique_ptr<AbstractTreeDecoder>;
4232

@@ -145,54 +135,3 @@ bool JsonParser::getNext(Defect *def)
145135
}
146136
}
147137
}
148-
149-
static bool scReadEvent(DefEvent *pEvt, const pt::ptree &evtNode)
150-
{
151-
using std::string;
152-
153-
// read level (error, warning, note)
154-
string &evtName = pEvt->event;
155-
evtName = valueOf<string>(evtNode, "level", "");
156-
if (evtName.empty())
157-
return false;
158-
159-
// read location
160-
pEvt->fileName = valueOf<string>(evtNode, "file", "<unknown>");
161-
pEvt->line = valueOf<int> (evtNode, "line", 0);
162-
pEvt->column = valueOf<int> (evtNode, "byte-column", 0);
163-
164-
// read message
165-
pEvt->msg = valueOf<string>(evtNode, "message", "<unknown>");
166-
167-
// append [SC...] if available
168-
const string code = valueOf<string>(evtNode, "code", "");
169-
if (!code.empty())
170-
pEvt->msg += " [SC" + code + "]";
171-
172-
return true;
173-
}
174-
175-
bool ShellCheckTreeDecoder::readNode(Defect *def)
176-
{
177-
// move the iterator after we get the current position
178-
const pt::ptree *pNode = this->nextNode();
179-
if (!pNode)
180-
// failed initialization or EOF
181-
return false;
182-
183-
const pt::ptree &defNode = *pNode;
184-
185-
*def = Defect("SHELLCHECK_WARNING");
186-
187-
// read key event
188-
def->events.push_back(DefEvent());
189-
if (!scReadEvent(&def->events.back(), defNode))
190-
return false;
191-
192-
// TODO: go through fix/replacements nodes
193-
194-
// apply post-processing rules
195-
this->postProc.apply(def);
196-
197-
return true;
198-
}

0 commit comments

Comments
 (0)