Report full function extents from the stock observer

The observer emits an optional extent inside each proven definition
span from the body's last statement finish (the C++ frontend does not
set function_end_locus). The occurrence decoder binds it to the same
snapshot through the negotiated encoding and rejects malformed
extents; call hierarchy attributes nested calls to the enclosing
extent. A real stdio session reported both inner calls as outgoing
from outer, and the observer and occurrence corpora still pass.
This commit is contained in:
faumaray committed 2026-12-05 17:28:59 +03:00
1 parent a098e45dc9
commit ec86a79a1a
11 files changed
+239 -11

No files matched your search

+82 -1
View File
@@ -15,6 +15,7 @@
#include "diagnostic.h"
#include "function.h"
#include "input.h"
#include "tree-iterator.h"
#include <cerrno>
#include <climits>
@@ -63,6 +64,8 @@ struct body_entry {
char path[max_path + 1]{};
bool definition{};
bool conflict{};
bool has_extent{};
unsigned end_line{}, end_byte{};
};
struct use_entry {
@@ -371,6 +374,64 @@ bool token_span(location_t token, unsigned length, const char *expected, span &o
return true;
}
// One physical position in an admitted primary file, in the same byte space as
// token_span (the first-line BOM is restored). Ordinary maps only.
bool point_position(location_t location, const char *&path_out, unsigned &line, unsigned &column) {
const location_t pure = get_pure_location(location);
if (RESERVED_LOCATION_P(pure) || from_macro_expansion_at(pure)) {
return false;
}
const line_map *raw_map = linemap_lookup(line_table, pure);
if (!raw_map || !MAP_ORDINARY_P(raw_map)) {
return false;
}
const line_map_ordinary *map = linemap_check_ordinary(raw_map);
if (map->sysp || !map->to_file) {
return false;
}
file_entry *file = file_for(map->to_file);
if (!file) {
return false;
}
line = SOURCE_LINE(map, pure);
column = SOURCE_COLUMN(map, pure);
if (!line || !column || column == UINT_MAX) {
return false;
}
const bool bom = line == 1 && file->size >= 3 && file->data[0] == 0xEF &&
file->data[1] == 0xBB && file->data[2] == 0xBF;
if (bom) {
column += 3;
}
path_out = file->path;
return true;
}
// End of the last statement in a saved function body. The C++ frontend does not
// set function_end_locus, so the body itself is the authority.
location_t last_body_location(tree node) {
if (!node) {
return UNKNOWN_LOCATION;
}
if (TREE_CODE(node) == BIND_EXPR) {
return last_body_location(BIND_EXPR_BODY(node));
}
if (TREE_CODE(node) == STATEMENT_LIST) {
for (tree_stmt_iterator i = tsi_last(node); !tsi_end_p(i); tsi_prev(&i)) {
if (location_t location = last_body_location(tsi_stmt(i));
location != UNKNOWN_LOCATION) {
return location;
}
}
return UNKNOWN_LOCATION;
}
return EXPR_LOCATION(node);
}
bool end_line_after(unsigned line, unsigned column, const body_entry *body) {
return line > body->where.line || (line == body->where.line && column >= body->where.start);
}
bool range_bytes(location_t location, file_entry *&file, std::size_t &begin, std::size_t &end) {
const location_t start = get_pure_location(get_start(location));
const location_t last = get_pure_location(get_finish(location));
@@ -618,6 +679,18 @@ void pre_genericize(void *data, void *) {
}
if (body_entry *body = find_body(DECL_UID(decl))) {
body->definition = true;
location_t end = last_body_location(DECL_SAVED_TREE(decl));
if (!RESERVED_LOCATION_P(end)) {
end = get_finish(end);
}
const char *path = nullptr;
unsigned line = 0, column = 0;
if (!RESERVED_LOCATION_P(end) && point_position(end, path, line, column) &&
std::strcmp(path, body->path) == 0 && (end_line_after(line, column, body))) {
body->has_extent = true;
body->end_line = line;
body->end_byte = column;
}
}
const location_t location = DECL_SOURCE_LOCATION(decl);
if (RESERVED_LOCATION_P(location)) {
@@ -692,8 +765,16 @@ void finish(void *, void *) {
if (known) {
std::fputs("{\"path\":", out);
json_string(out, body->path);
std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u}", body->where.line,
std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u", body->where.line,
body->where.start, body->where.end);
if (body->has_extent) {
std::fprintf(out,
",\"extent\":{\"startLine\":%u,\"startByte\":%u,"
"\"endLine\":%u,\"endByte\":%u}",
body->where.line, body->where.start, body->end_line,
body->end_byte);
}
std::fputc('}', out);
} else {
std::fputs("null", out);
}