Files
Language-Server/compiler-patches/gcc-16.2.0/0002-ide-occurrences.patch
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

522 lines
17 KiB
Diff

--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -2501,6 +2501,9 @@
C++ ObjC++ Var(flag_visibility_ms_compat)
Changes visibility to match Microsoft Visual Studio by default.
+fvls-ide
+C++ Var(flag_vls_ide) Undocumented
+
fvls-observe
C++ Var(flag_vls_observe) Undocumented
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7059,6 +7059,8 @@
/* Whether we parsed an ellipsis (`...') just before the declarator,
to indicate this is a parameter pack. */
BOOL_BITFIELD parameter_pack_p : 1;
+ /* The private observer saw a plain identifier token at this cdk_id. */
+ BOOL_BITFIELD vls_name_token_p : 1;
/* If this declarator is parenthesized, this the open-paren. It is
UNKNOWN_LOCATION when not parenthesized. */
location_t parenthesized;
--- a/gcc/cp/lex.cc
+++ b/gcc/cp/lex.cc
@@ -353,7 +353,8 @@
EXPR_STMT, OMP_DEPOBJ
};
- if ((flag_vls_observe || flag_vls_step) && !vls_eval::start ())
+ if ((flag_vls_observe || flag_vls_step || flag_vls_ide)
+ && !vls_eval::start ())
return false;
memset (&statement_code_p, 0, sizeof (statement_code_p));
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -24,6 +24,7 @@
#include "system.h"
#include "coretypes.h"
#include "cp-tree.h"
+#include "vls-eval.h"
#include "c-family/c-common.h"
#include "timevar.h"
#include "stringpool.h"
@@ -51,6 +52,35 @@
#include "builtins.h"
#include "contracts.h"
#include "analyzer/analyzer-language.h"
+
+/* A commit of the topmost context does not commit its parents. A token
+ in a discarded outer parse must never enter the IDE candidate table. */
+static bool
+vls_committed (const cp_parser *parser)
+{
+ for (const cp_parser_context *c = parser->context; c && c->next; c = c->next)
+ if (c->status != CP_PARSER_STATUS_KIND_COMMITTED)
+ {
+ vls_eval::ide::tentative ();
+ return false;
+ }
+ return !processing_template_decl;
+}
+
+/* Use the actual lexical declarator from this parse, even if GCC merged
+ DECL into an older declaration with a different source location. */
+static void
+vls_witness_declarator (const cp_parser *parser, tree decl,
+ const cp_declarator *id, bool definition)
+{
+ if (!decl || (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != FIELD_DECL))
+ return;
+ while (id && id->kind != cdk_id)
+ id = id->declarator;
+ if (id && id->vls_name_token_p && id->u.id.unqualified_name == DECL_NAME (decl)
+ && vls_committed (parser))
+ vls_eval::ide::witness (decl, id->id_loc, definition);
+}
/* The lexer. */
@@ -1901,6 +1931,7 @@
declarator->std_attributes = NULL_TREE;
declarator->declarator = NULL;
declarator->parameter_pack_p = false;
+ declarator->vls_name_token_p = false;
declarator->id_loc = UNKNOWN_LOCATION;
declarator->init_loc = UNKNOWN_LOCATION;
@@ -8954,6 +8985,25 @@
tree stripped_expression
= tree_strip_any_location_wrapper (postfix_expression);
+ /* Keep the terminal identifier while the callee still has its
+ lexical range. The completed call has its caret at '('. */
+ tree vls_name = NULL_TREE;
+ location_t vls_token = UNKNOWN_LOCATION;
+ if (flag_vls_ide
+ && (TREE_CODE (stripped_expression) == FUNCTION_DECL
+ || TREE_CODE (stripped_expression) == OVERLOAD
+ || identifier_p (stripped_expression)))
+ {
+ cp_token *last = cp_lexer_safe_previous_token (parser->lexer);
+ if (last && last->type == CPP_NAME && !last->tree_check_p
+ && get_pure_location (last->location)
+ == get_pure_location (postfix_expression.get_location ())
+ && get_finish (last->location) == postfix_expression.get_finish ())
+ {
+ vls_name = last->u.value;
+ vls_token = last->location;
+ }
+ }
is_builtin_constant_p
= DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
if (is_builtin_constant_p)
@@ -9130,6 +9180,11 @@
disallow_virtual,
koenig_p,
complain);
+ if (vls_name && TREE_CODE (postfix_expression) == CALL_EXPR
+ && vls_committed (parser))
+ vls_eval::ide::occurrence
+ (cp_get_callee_fndecl_nofold (postfix_expression),
+ TREE_TYPE (postfix_expression), vls_name, vls_token);
}
if (close_paren_loc != UNKNOWN_LOCATION)
@@ -9528,6 +9583,8 @@
bool dependent_p;
bool pseudo_destructor_p;
tree scope = NULL_TREE;
+ tree vls_name = NULL_TREE;
+ location_t vls_token = UNKNOWN_LOCATION;
location_t start_loc = postfix_expression.get_start ();
/* If this is a `->' operator, dereference the pointer. */
@@ -9660,6 +9717,18 @@
&template_p,
/*declarator_p=*/false,
/*optional_p=*/false));
+
+ /* The first token may be a qualifier with the same spelling as the
+ field (b.x::x). Only the terminal name belongs to the field use. */
+ if (flag_vls_ide && !splice_p && !template_keyword_p)
+ {
+ cp_token *last = cp_lexer_safe_previous_token (parser->lexer);
+ if (last && last->type == CPP_NAME && !last->tree_check_p)
+ {
+ vls_name = last->u.value;
+ vls_token = last->location;
+ }
+ }
/* In general, build a SCOPE_REF if the member name is qualified.
However, if the name was not dependent and has already been
@@ -9746,6 +9815,11 @@
&& (cp_parser_non_integral_constant_expression
(parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
postfix_expression = error_mark_node;
+
+ if (vls_name && TREE_CODE (postfix_expression) == COMPONENT_REF
+ && vls_committed (parser))
+ vls_eval::ide::occurrence (TREE_OPERAND (postfix_expression, 1),
+ TREE_TYPE (postfix_expression), vls_name, vls_token);
return postfix_expression;
}
@@ -26283,6 +26357,8 @@
&& declarator->id_loc != UNKNOWN_LOCATION
&& DECL_SOURCE_LOCATION (decl) == input_location)
DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
+ if (flag_vls_ide && decl && TREE_CODE (decl) == FUNCTION_DECL)
+ vls_witness_declarator (parser, decl, declarator, false);
}
/* Perform deferred access control checks, now that we know in which
@@ -27156,6 +27232,11 @@
declarator = make_id_declarator (qualifying_scope,
unqualified_name,
sfk, token->location);
+ if (flag_vls_ide && token == declarator_id_start_token
+ && token->type == CPP_NAME && !token->tree_check_p
+ && !qualifying_scope && sfk == sfk_none
+ && token->u.value == unqualified_name)
+ declarator->vls_name_token_p = true;
declarator->std_attributes = attrs;
declarator->parameter_pack_p = pack_expansion_p;
@@ -31583,6 +31664,8 @@
decl = grokfield (declarator, &decl_specifiers,
initializer, /*init_const_expr_p=*/true,
asm_specification, attributes);
+ if (flag_vls_ide && decl && TREE_CODE (decl) == FIELD_DECL)
+ vls_witness_declarator (parser, decl, declarator, true);
if (parser->fully_implicit_function_template_p)
{
@@ -36071,6 +36154,8 @@
? TV_PARSE_INLINE : TV_PARSE_FUNC);
fn = cp_parser_function_definition_after_declarator (parser,
/*inline_p=*/false);
+ if (flag_vls_ide && !seen_error ())
+ vls_witness_declarator (parser, fn, declarator, true);
}
return fn;
--- a/gcc/cp/vls-eval.cc
+++ b/gcc/cp/vls-eval.cc
@@ -260,10 +260,19 @@
} // anonymous namespace
+// Shares the bounded v1 writer, but has a distinct handshake and vocabulary.
+#include "vls-ide.inc"
+
bool
start ()
{
const char *s = getenv ("VLS_GCC_NONCE");
+ if (flag_vls_ide && (!flag_vls_observe || flag_vls_step
+ || !ide::snapshot_setup ()))
+ {
+ error ("invalid private IDE observer setup");
+ return false;
+ }
if (!flag_vls_observe || !valid_nonce (s) || !valid_pipes ()
|| signal (SIGPIPE, SIG_IGN) == SIG_ERR)
{
@@ -274,6 +283,12 @@
observing = true;
stepping = flag_vls_step;
record r ("hello");
+ if (flag_vls_ide)
+ {
+ ide::hello (r);
+ publish (r);
+ return true;
+ }
r.append (",\"backend\":\"gcc-16.2.0/vls-eval-1\",\"pid\":%ld,"
"\"capabilities\":[\"integralReturn\",\"cooperativeStep\"],"
"\"kinds\":[\"hello\",\"enter\",\"body\",\"value\",\"cached\","
@@ -288,6 +303,8 @@
{
if (observing)
{
+ if (flag_vls_ide)
+ ide::finish ();
record r ("frontendEnd");
publish (r);
observing = false;
@@ -297,7 +314,7 @@
void
call::enter (tree decl)
{
- if (!observing || DECL_ARTIFICIAL (decl) || !DECL_NAME (decl))
+ if (!observing || flag_vls_ide || DECL_ARTIFICIAL (decl) || !DECL_NAME (decl))
return;
location_t loc = DECL_SOURCE_LOCATION (decl);
if (RESERVED_LOCATION_P (loc) || in_system_header_at (loc))
--- a/gcc/cp/vls-eval.h
+++ b/gcc/cp/vls-eval.h
@@ -6,6 +6,12 @@
bool start ();
void finish ();
+
+namespace ide {
+void tentative ();
+void witness (tree, location_t, bool);
+void occurrence (tree, tree, tree, location_t);
+}
// A dynamic attempt in this invocation. Only numeric IDs and static outcome
// tags survive a hook. GCC owns all trees, including the result declaration.
--- /dev/null
+++ b/gcc/cp/vls-ide.inc
@@ -0,0 +1,244 @@
+/* Private, read-only GCC 16.2 primary-file occurrence probe. Included in
+ vls-eval.cc to share its bounded record writer and invocation nonce. */
+namespace ide {
+namespace {
+
+constexpr unsigned capacity = 256;
+constexpr unsigned max_name = 64;
+char snapshot[65];
+bool at_capacity, withheld_tentative, withheld_origin, withheld_semantics;
+bool withheld_conflict;
+
+/* One-based physical byte columns; end is exclusive. No line map, tree,
+ parser, or spelling pointer escapes a parser hook. */
+struct span
+{
+ unsigned line, start, end;
+ bool same (const span &s) const
+ { return line == s.line && start == s.start && end == s.end; }
+ void write (record &r) const
+ {
+ r.append ("{\"line\":%u,\"startByte\":%u,\"endByte\":%u}", line, start, end);
+ }
+};
+
+struct witness_entry
+{
+ unsigned uid;
+ span where;
+ bool definition;
+ bool conflict;
+};
+struct use_entry
+{
+ unsigned uid;
+ span where;
+ char name[max_name + 1];
+ bool field;
+ bool is_double;
+ bool conflict;
+};
+witness_entry targets[capacity];
+use_entry uses[capacity];
+unsigned target_count, use_count;
+
+bool
+plain_name (tree decl)
+{
+ if (!decl || (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != FIELD_DECL)
+ || DECL_ARTIFICIAL (decl) || !DECL_NAME (decl))
+ return false;
+ if (TREE_CODE (decl) == FUNCTION_DECL
+ && (!TREE_TYPE (decl) || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
+ || (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl)))
+ || (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))))
+ return false;
+ tree id = DECL_NAME (decl);
+ unsigned length = IDENTIFIER_LENGTH (id);
+ if (!length || length > max_name)
+ return false;
+ const char *name = IDENTIFIER_POINTER (id);
+ for (unsigned i = 0; i != length; ++i)
+ if (!(name[i] >= 'a' && name[i] <= 'z') && !(name[i] >= 'A' && name[i] <= 'Z')
+ && !(i && name[i] >= '0' && name[i] <= '9') && name[i] != '_')
+ return false;
+ return true;
+}
+
+bool
+primary_token (location_t token, unsigned length, span &out)
+{
+ location_t start = get_pure_location (get_start (token));
+ location_t last = get_pure_location (get_finish (token));
+ if (RESERVED_LOCATION_P (start) || RESERVED_LOCATION_P (last)
+ || get_pure_location (token) != start
+ || from_macro_expansion_at (start) || from_macro_expansion_at (last))
+ return false;
+ const line_map *m = linemap_lookup (line_table, start);
+ if (!m || !MAP_ORDINARY_P (m) || linemap_lookup (line_table, last) != m)
+ return false;
+ const line_map_ordinary *map = linemap_check_ordinary (m);
+ if (!MAIN_FILE_P (map) || map->sysp || !map->to_file || !main_input_filename
+ || strcmp (map->to_file, main_input_filename) != 0)
+ return false;
+ unsigned line = SOURCE_LINE (map, start);
+ unsigned column = SOURCE_COLUMN (map, start);
+ unsigned final = SOURCE_COLUMN (map, last);
+ if (!line || !column || SOURCE_LINE (map, last) != line || final < column
+ || final == UINT_MAX || final - column + 1 != length)
+ return false;
+ out = {line, column, final + 1};
+ return true;
+}
+
+witness_entry *
+find_target (unsigned uid)
+{
+ for (unsigned i = 0; i < target_count; ++i)
+ if (targets[i].uid == uid)
+ return &targets[i];
+ return nullptr;
+}
+
+} // anonymous namespace
+
+bool
+snapshot_setup ()
+{
+ const char *s = getenv ("VLS_GCC_SNAPSHOT");
+ if (!s || strnlen (s, sizeof snapshot) != sizeof snapshot - 1)
+ return false;
+ for (unsigned i = 0; i < sizeof snapshot - 1; ++i)
+ if (!(s[i] >= '0' && s[i] <= '9') && !(s[i] >= 'a' && s[i] <= 'f'))
+ return false;
+ memcpy (snapshot, s, sizeof snapshot);
+ return true;
+}
+
+void
+hello (record &r)
+{
+ r.append (",\"backend\":\"gcc-16.2.0/vls-ide-1\",\"pid\":%ld,"
+ "\"capabilities\":[\"primaryFileOccurrences\"],"
+ "\"kinds\":[\"hello\",\"occurrence\",\"ideCoverage\","
+ "\"terminal\",\"frontendEnd\"],\"snapshot\":\"%s\"",
+ (long) getpid (), snapshot);
+}
+
+void
+tentative ()
+{
+ withheld_tentative = true;
+}
+
+void
+witness (tree decl, location_t token, bool definition)
+{
+ if (at_capacity || !plain_name (decl))
+ return;
+ span where;
+ if (!primary_token (token, IDENTIFIER_LENGTH (DECL_NAME (decl)), where))
+ {
+ withheld_origin = true;
+ return;
+ }
+ unsigned uid = DECL_UID (decl);
+ if (witness_entry *old = find_target (uid))
+ {
+ if (definition && !old->definition)
+ *old = {uid, where, true, false};
+ else if (definition && !old->where.same (where))
+ old->conflict = withheld_conflict = true;
+ return;
+ }
+ if (target_count == capacity)
+ at_capacity = true;
+ else
+ targets[target_count++] = {uid, where, definition, false};
+}
+
+void
+occurrence (tree decl, tree type, tree token_name, location_t token)
+{
+ if (at_capacity)
+ return;
+ if (!plain_name (decl) || DECL_NAME (decl) != token_name
+ || (type != integer_type_node && type != double_type_node))
+ {
+ withheld_semantics = true;
+ return;
+ }
+ span where;
+ unsigned length = IDENTIFIER_LENGTH (token_name);
+ if (!primary_token (token, length, where))
+ {
+ withheld_origin = true;
+ return;
+ }
+ unsigned uid = DECL_UID (decl);
+ bool field = TREE_CODE (decl) == FIELD_DECL;
+ bool is_double = type == double_type_node;
+ for (unsigned i = 0; i < use_count; ++i)
+ if (uses[i].where.same (where))
+ {
+ if (uses[i].uid != uid || uses[i].field != field || uses[i].is_double != is_double)
+ uses[i].conflict = withheld_conflict = true;
+ return;
+ }
+ if (use_count == capacity)
+ {
+ at_capacity = true;
+ return;
+ }
+ use_entry &u = uses[use_count++];
+ u = {uid, where, {}, field, is_double, false};
+ memcpy (u.name, IDENTIFIER_POINTER (token_name), length);
+}
+
+void
+finish ()
+{
+ /* A same-name #line can give two distinct tokens identical logical
+ filename/line/column, even if their bytes are identical. Suppress the
+ entire set before publishing its first member. Includes can also set
+ this flag. The IDE harness supplies one include-free primary snapshot. */
+ const char *reason = nullptr;
+ if (line_table->seen_line_directive)
+ reason = "lineDirective";
+ else if (seen_error ())
+ reason = "compileErrors";
+ else if (at_capacity)
+ reason = "capacity";
+ if (!reason)
+ for (unsigned i = 0; i < use_count; ++i)
+ {
+ const use_entry &u = uses[i];
+ if (u.conflict)
+ continue;
+ const witness_entry *w = find_target (u.uid);
+ const bool known = w && !w->conflict;
+ record r ("occurrence");
+ r.append (",\"sourceKind\":\"primaryFileToken\",\"role\":\"%s\","
+ "\"declUid\":%u,\"name\":", u.field ? "field" : "call", u.uid);
+ r.text (u.name);
+ r.append (",\"resultType\":\"%s\",\"use\":", u.is_double ? "double" : "int");
+ u.where.write (r);
+ const char *kind = known ? (w->definition ? "definition" : "declaration") : "unavailable";
+ r.append (",\"targetKind\":\"%s\",\"target\":", kind);
+ if (known)
+ w->where.write (r);
+ else
+ r.append ("null");
+ publish (r);
+ }
+ record coverage ("ideCoverage");
+ coverage.append (",\"source\":");
+ coverage.text (main_input_filename);
+ coverage.append (",\"state\":\"%s\",\"reason\":", reason ? "unavailable" : "partial");
+ coverage.text (reason);
+ coverage.append (",\"tentative\":%s,\"unmapped\":%s,\"unsupported\":%s,\"conflict\":%s",
+ withheld_tentative ? "true" : "false", withheld_origin ? "true" : "false",
+ withheld_semantics ? "true" : "false", withheld_conflict ? "true" : "false");
+ publish (coverage);
+}
+} // namespace ide