Files
codebase-memory-mcp/internal/cbm/preprocessor.h
Martin Vogel beba7f8c0c C/C++: re-extract a header's includers in an incremental refresh
A C-family file's preprocessed pass reads the repository files its quoted
#include lines name, so its graph changes with their bytes even when its
own bytes do not. An incremental refresh after an edit to a header alone
re-extracted only the header, and its includers kept their old expansion.

- The #include lookup resolves only to files the index run covers (its
  discovery, through CBMIncludeScope and the pipeline context's
  indexed_files), so every file an expansion depends on is in the run's
  manifest.
- Each file's lsp_surface row records the files its lookup read
  (included_files; transitive, since the headers a header includes are on
  the includer's own list). It is not part of surface_sha.
- The closure planner adds every stored file whose record names a changed
  or deleted file to the delta as a changed file, which then goes through
  the same surface comparison. One pass over the stored rows.
- CBM_INDEX_FORMAT_VERSION 6 -> 7: an index written before records no
  included files, so it is rebuilt once.
- The graph and content digests cover the new column; a database without
  it keeps its previous digest stream.

Tests: pipeline_c_header_edit_refreshes_includers_incrementally (a header
edit reaches a direct and a transitive includer; the refresh equals a
fresh full index) and index_format_v6_included_files_rebuild.

Refs #2993

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-11-18 04:05:30 +01:00

86 lines
4.3 KiB
C

#ifndef CBM_PREPROCESSOR_H
#define CBM_PREPROCESSOR_H
#include <stddef.h>
#include <stdint.h>
/* #1989: build-system export macros (UE "<MOD>_API", CMake generate_export_header
* "<lib>_EXPORT", ...) are defined empty on the real compile command line but are
* invisible to tree-sitter, so `class MOD_API Foo` misparses with the macro token
* as the type name. The preprocessed second pass predefines the conventional
* export-macro-shaped identifiers found in a source file as empty, mirroring the
* real build line. The candidate list is bounded. */
#define CBM_EXPORT_MACRO_MAX 32
#define CBM_EXPORT_MACRO_NAME_MAX 96
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
char *source;
uint32_t *original_line_by_expanded_line; // 1-based; 0 means directive/unmapped.
uint8_t *belongs_to_main_file; // 1-based; true only for the original input file.
int expanded_line_count;
/* The repository files the #include lookup read (repository-relative,
* sorted, each once), whether or not the run that followed includes kept
* its expansion: the expansion depends on their bytes either way. */
char **included_files;
int included_count;
} CBMPreprocessedSource;
/* Supplies the files of the indexed repository to the #include lookup of the
* preprocessed pass. rel_path is relative to the repository root, '/'-separated
* and normalised. read returns a buffer of *out_len bytes, handed back to
* release once consumed, or NULL when the repository holds no such file. */
typedef struct {
char *(*read)(const void *userdata, const char *rel_path, size_t *out_len);
void (*release)(const void *userdata, char *data);
const void *userdata;
} CBMIncludeReader;
// Collect conventional export-macro-shaped identifiers (ALL_CAPS ending in
// _API/_EXPORT/_IMPORT/_DLLEXPORT/_DEPRECATED) from the source, up to max_out
// unique names. Returns the number of names stored in out.
int cbm_export_macro_candidates(const char *source, int source_len,
char (*out)[CBM_EXPORT_MACRO_NAME_MAX], int max_out);
// Preprocess C/C++ source: expand macros, evaluate #ifdef. #include is not
// followed (no include reader): every included header contributes nothing.
// Returns malloc-allocated expanded source, or NULL if no expansion needed/on failure.
// extra_defines: NULL-terminated array of "NAME=VALUE" strings (can be NULL).
// include_paths: kept for API compatibility; not consulted (see cbm_preprocess_with_map).
// The returned string must be freed with cbm_preprocess_free().
char *cbm_preprocess(const char *source, int source_len, const char *filename,
const char **extra_defines, const char **include_paths, int cpp_mode);
// Preprocess and return source plus expanded-line -> original-line ownership map.
// filename is the file's path relative to the indexed repository root. Quoted
// #include and __has_include names resolve through `reader` only, in the
// including file's directory within the repository root; a NULL reader
// resolves nothing. Angle-bracket names and computed #include lines resolve to
// nothing, and include_paths is not consulted, so the expansion is a function
// of the indexed tree alone. The expanded buffer holds the main file's own
// tokens only: what an included header defines (macros, #if state) shapes it,
// but the header's body is not part of it; the header is indexed as its own
// file. When the run that follows includes ends in #error (or another stop that
// discards the expansion), the file falls back to its expansion without includes.
// Returns NULL if no expansion is needed or preprocessing fails.
// Free with cbm_preprocessed_source_free().
CBMPreprocessedSource *cbm_preprocess_with_map(const char *source, int source_len,
const char *filename, const char **extra_defines,
const char **include_paths, int cpp_mode,
const CBMIncludeReader *reader);
// Free preprocessed source returned by cbm_preprocess.
void cbm_preprocess_free(char *expanded);
// Free preprocessed source and line maps returned by cbm_preprocess_with_map.
void cbm_preprocessed_source_free(CBMPreprocessedSource *pp);
#ifdef __cplusplus
}
#endif
#endif // CBM_PREPROCESSOR_H