Skip to content

Commit f419535

Browse files
author
Dwight Guth
authored
update llvm-kompile-compute-loc and llvm-kompile-compute-ordinal (#1094)
These binaries were not quite feature-complete with their original intended purpose, probably primarily because at the time at which they were reimplemented, the past version of them was somewhat broken. This PR adds the following features: 1. Outputting the k location is the default in llvm-kompile-compute-loc 2. If a k location does not exist, it falls back to the kore location rather than crashing. 3. The output is formatted with both a filename and a line number. 4. In llvm-kompile-compute-ordinal, you can specify a filename and it will search for the ordinal of a k rule on that line of that file in the input definition.
1 parent 0ec3daf commit f419535

File tree

6 files changed

+68
-33
lines changed

6 files changed

+68
-33
lines changed

include/kllvm/ast/util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ namespace kllvm {
1111
* the AST and other data structures.
1212
*/
1313

14-
[[nodiscard]] std::optional<int64_t>
14+
[[nodiscard]] std::optional<std::pair<std::string, uint64_t>>
1515
get_start_line_location(kore_axiom_declaration const &axiom);
1616

1717
[[nodiscard]] std::string trim(std::string s);
1818
} // namespace kllvm
1919

20-
#endif // UTIL_H
20+
#endif // UTIL_H

lib/ast/util.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,32 @@
55

66
using namespace kllvm;
77

8-
[[nodiscard]] std::optional<int64_t>
8+
[[nodiscard]] std::optional<std::pair<std::string, uint64_t>>
99
kllvm::get_start_line_location(kore_axiom_declaration const &axiom) {
10+
if (!axiom.attributes().contains(attribute_set::key::Source)
11+
|| !axiom.attributes().contains(attribute_set::key::Location)) {
12+
return std::nullopt;
13+
}
14+
auto source_att = axiom.attributes().get(attribute_set::key::Source);
15+
assert(source_att->get_arguments().size() == 1);
16+
17+
auto str_pattern_source = std::dynamic_pointer_cast<kore_string_pattern>(
18+
source_att->get_arguments()[0]);
19+
std::string source = str_pattern_source->get_contents();
20+
1021
auto location_att = axiom.attributes().get(attribute_set::key::Location);
1122
assert(location_att->get_arguments().size() == 1);
1223

13-
auto str_pattern = std::dynamic_pointer_cast<kore_string_pattern>(
24+
auto str_pattern_loc = std::dynamic_pointer_cast<kore_string_pattern>(
1425
location_att->get_arguments()[0]);
15-
std::string location = str_pattern->get_contents();
26+
std::string location = str_pattern_loc->get_contents();
1627

1728
size_t l_paren = location.find_first_of('(');
1829
size_t first_comma = location.find_first_of(',');
1930
size_t length = first_comma - l_paren - 1;
20-
return std::stoi(location.substr(l_paren + 1, length));
31+
return std::make_pair(
32+
source.substr(7, source.size() - 8),
33+
std::stoi(location.substr(l_paren + 1, length)));
2134
}
2235

2336
// trim the string from the start
@@ -26,4 +39,4 @@ kllvm::get_start_line_location(kore_axiom_declaration const &axiom) {
2639
return !std::isspace(c);
2740
}));
2841
return s;
29-
}
42+
}

test/defn/compute-ordinal-loc/definition.kore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %compute-ordinal $(cat %S/line_number_rule) --k-line > %t0
2-
// RUN: %compute-loc $(cat %t0) --k-line | diff - %S/line_number_rule
3-
// RUN: %compute-loc $(cat %t0) > %t1
4-
// RUN: %compute-ordinal $(cat %t1) | diff - %t0
2+
// RUN: %compute-loc $(cat %t0) | diff - %S/source_line_number_rule
3+
// RUN: %compute-loc $(cat %t0) --kore-line > %t1
4+
// RUN: %compute-ordinal $(cat %t1 | awk -F ':' '{print $2}') | diff - %t0
55

66
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/temp/inc.k)")]
77

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/temp/inc.k:5

tools/llvm-kompile-compute-loc/main.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,11 @@ cl::opt<std::string> kompiled_dir(
2020
cl::opt<int> ordinal(
2121
cl::Positional, cl::desc("<ordinal>"), cl::Required, cl::cat(loc_cat));
2222

23-
cl::opt<bool> is_k_line(
24-
"k-line",
25-
cl::desc("The tool will look for the line passed as an argument in the K "
26-
"definition"),
23+
cl::opt<bool> is_kore_line(
24+
"kore-line",
25+
cl::desc("The tool will output the line in the KORE definition"),
2726
cl::init(false), cl::cat(loc_cat));
2827

29-
std::optional<int64_t>
30-
get_k_location(std::string &definition, int const &ordinal) {
31-
// Parse the definition.kore to get the AST.
32-
kllvm::parser::kore_parser parser(definition);
33-
auto kore_ast = parser.definition();
34-
kore_ast->preprocess();
35-
36-
auto axiom = kore_ast->get_axiom_by_ordinal(ordinal);
37-
return get_start_line_location(axiom);
38-
}
39-
4028
std::optional<int64_t>
4129
get_kore_location(std::string &definition, int const &ordinal) {
4230

@@ -60,17 +48,41 @@ get_kore_location(std::string &definition, int const &ordinal) {
6048
return std::nullopt;
6149
}
6250

51+
std::optional<std::pair<std::string, int64_t>>
52+
get_k_or_kore_location(std::string &definition, int const &ordinal) {
53+
// Parse the definition.kore to get the AST.
54+
kllvm::parser::kore_parser parser(definition);
55+
auto kore_ast = parser.definition();
56+
kore_ast->preprocess();
57+
58+
auto axiom = kore_ast->get_axiom_by_ordinal(ordinal);
59+
auto k_loc = get_start_line_location(axiom);
60+
if (k_loc) {
61+
return k_loc;
62+
}
63+
auto kore_loc = get_kore_location(definition, ordinal);
64+
if (kore_loc) {
65+
return std::make_pair(definition, *kore_loc);
66+
}
67+
return std::nullopt;
68+
}
69+
6370
int main(int argc, char **argv) {
6471
cl::HideUnrelatedOptions({&loc_cat});
6572
cl::ParseCommandLineOptions(argc, argv);
6673

6774
auto definition = kompiled_dir + "/definition.kore";
6875

69-
auto location = is_k_line ? get_k_location(definition, ordinal)
70-
: get_kore_location(definition, ordinal);
76+
std::optional<std::pair<std::string, uint64_t>> location;
77+
if (is_kore_line) {
78+
auto line = get_kore_location(definition, ordinal);
79+
location = std::make_pair(definition, *line);
80+
} else {
81+
location = get_k_or_kore_location(definition, ordinal);
82+
}
7183

7284
if (location) {
73-
std::cout << *location << "\n";
85+
std::cout << location->first << ":" << location->second << "\n";
7486
return 0;
7587
}
7688

tools/llvm-kompile-compute-ordinal/main.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ cl::opt<std::string> kompiled_dir(
2121
cl::opt<int> line(
2222
cl::Positional, cl::desc("<line>"), cl::Required, cl::cat(ordinal_cat));
2323

24+
cl::opt<std::string> source(
25+
"source",
26+
cl::desc("The file to which the line number refers. Implies --k-line."),
27+
cl::cat(ordinal_cat));
28+
2429
cl::opt<bool> is_k_line(
2530
"k-line",
2631
cl::desc("The tool will look for the line passed as an argument in the K "
2732
"definition"),
2833
cl::init(false), cl::cat(ordinal_cat));
2934

30-
std::optional<int64_t>
31-
get_k_ordinal(std::string const &definition, int const &line) {
35+
std::optional<int64_t> get_k_ordinal(
36+
std::string const &definition, std::string const &source, int const &line) {
3237
// Parse the definition.kore to get the AST.
3338
kllvm::parser::kore_parser parser(definition);
3439
auto kore_ast = parser.definition();
@@ -37,8 +42,8 @@ get_k_ordinal(std::string const &definition, int const &line) {
3742
// Iterate through axioms.
3843
for (auto *axiom : kore_ast->get_axioms()) {
3944
if (axiom->attributes().contains(attribute_set::key::Location)) {
40-
auto start_line = get_start_line_location(*axiom);
41-
if (start_line == line) {
45+
auto loc = get_start_line_location(*axiom);
46+
if (loc->first.ends_with(source) && loc->second == line) {
4247
return axiom->get_ordinal();
4348
}
4449
}
@@ -74,9 +79,13 @@ int main(int argc, char **argv) {
7479
cl::HideUnrelatedOptions({&ordinal_cat});
7580
cl::ParseCommandLineOptions(argc, argv);
7681

82+
if (!source.empty()) {
83+
is_k_line = true;
84+
}
85+
7786
auto definition = kompiled_dir + "/definition.kore";
7887

79-
auto location = is_k_line ? get_k_ordinal(definition, line)
88+
auto location = is_k_line ? get_k_ordinal(definition, source, line)
8089
: get_kore_ordinal(definition, line);
8190

8291
if (location) {

0 commit comments

Comments
 (0)