code-moniker-workspace 0.6.1

Workspace model, ports, snapshots, linkage, and change analysis for code-moniker.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use std::collections::{BTreeSet, HashMap, HashSet};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;

use code_moniker_core::lang::c::Presets;

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct HeaderUsage {
	c: bool,
	cpp: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum TranslationUnitLanguage {
	C,
	Cpp,
}

/// Build facts that affect the meaning of C-family source files. A `.h` suffix
/// is not a language declaration: until C++ extraction exists, headers reached
/// exclusively from C++ translation units must not be parsed as C.
#[derive(Clone, Debug, Default)]
pub struct CBuildContext {
	root: PathBuf,
	header_usage: HashMap<PathBuf, HeaderUsage>,
	include_paths: Vec<PathBuf>,
	workspace_files: Arc<BTreeSet<String>>,
	external_include_package: Option<String>,
	has_c_translation_unit: bool,
	has_cpp_translation_unit: bool,
}

impl CBuildContext {
	pub fn load(root: &Path) -> Self {
		let root = absolute_normalized(root);
		let entries = workspace_entries(&root);
		let mut context = Self::from_entries(root, &entries);
		context.record_translation_units(entries);
		context
	}

	fn from_entries(root: PathBuf, entries: &[PathBuf]) -> Self {
		let workspace_files = entries
			.iter()
			.filter_map(|path| project_relative_path(&root, path))
			.collect::<BTreeSet<_>>();
		let makefile = load_makefile_hints(&root);
		let mut context = Self {
			include_paths: makefile.include_paths,
			root: root.clone(),
			header_usage: HashMap::new(),
			workspace_files: Arc::new(workspace_files),
			external_include_package: makefile.external_include_package,
			has_c_translation_unit: false,
			has_cpp_translation_unit: false,
		};
		if !context.include_paths.contains(&root) {
			context.include_paths.push(root);
		}
		context
	}

	fn record_translation_units(&mut self, entries: Vec<PathBuf>) {
		let mut visited_c = HashSet::new();
		let mut visited_cpp = HashSet::new();
		for path in entries {
			let Some(language) = translation_unit_language(&path) else {
				continue;
			};
			match language {
				TranslationUnitLanguage::C => self.has_c_translation_unit = true,
				TranslationUnitLanguage::Cpp => self.has_cpp_translation_unit = true,
			}
			let visited = match language {
				TranslationUnitLanguage::C => &mut visited_c,
				TranslationUnitLanguage::Cpp => &mut visited_cpp,
			};
			self.record_includes(&path, language, visited);
		}
	}

	pub fn extraction_presets(&self) -> Presets {
		Presets {
			include_paths: self
				.include_paths
				.iter()
				.filter_map(|path| project_relative_path(&self.root, path))
				.collect(),
			workspace_files: Arc::clone(&self.workspace_files),
			external_include_package: self.external_include_package.clone(),
		}
	}

	pub fn should_index_as_c(&self, path: &Path) -> bool {
		if !has_extension(path, "h") {
			return true;
		}
		if self.has_cpp_translation_unit && !self.has_c_translation_unit {
			return false;
		}
		let path = absolute_normalized(path);
		!self
			.header_usage
			.get(&path)
			.is_some_and(|usage| usage.cpp && !usage.c)
	}

	fn record_includes(
		&mut self,
		path: &Path,
		language: TranslationUnitLanguage,
		visited: &mut HashSet<PathBuf>,
	) {
		let path = absolute_normalized(path);
		if !visited.insert(path.clone()) {
			return;
		}
		let Ok(source) = std::fs::read(&path) else {
			return;
		};
		let source = String::from_utf8_lossy(&source);
		for include in includes(&source) {
			let Some(header) = self.resolve_include(&path, include) else {
				continue;
			};
			let usage = self.header_usage.entry(header.clone()).or_default();
			match language {
				TranslationUnitLanguage::C => usage.c = true,
				TranslationUnitLanguage::Cpp => usage.cpp = true,
			}
			self.record_includes(&header, language, visited);
		}
	}

	fn resolve_include(&self, source: &Path, include: IncludeDirective<'_>) -> Option<PathBuf> {
		if include.quoted {
			let relative = source.parent().unwrap_or(&self.root).join(include.path);
			if relative.is_file() {
				return Some(absolute_normalized(&relative));
			}
		}
		self.include_paths
			.iter()
			.map(|base| base.join(include.path))
			.find(|candidate| candidate.is_file())
			.map(|candidate| absolute_normalized(&candidate))
	}
}

fn workspace_entries(root: &Path) -> Vec<PathBuf> {
	ignore::WalkBuilder::new(root)
		.build()
		.filter_map(Result::ok)
		.filter(|entry| entry.file_type().is_some_and(|kind| kind.is_file()))
		.map(|entry| entry.into_path())
		.collect()
}

fn translation_unit_language(path: &Path) -> Option<TranslationUnitLanguage> {
	let extension = path.extension()?.to_str()?.to_ascii_lowercase();
	match extension.as_str() {
		"c" => Some(TranslationUnitLanguage::C),
		"cc" | "cpp" | "cxx" | "c++" => Some(TranslationUnitLanguage::Cpp),
		_ => None,
	}
}

fn has_extension(path: &Path, expected: &str) -> bool {
	path.extension()
		.and_then(|extension| extension.to_str())
		.is_some_and(|extension| extension.eq_ignore_ascii_case(expected))
}

#[derive(Clone, Copy)]
struct IncludeDirective<'a> {
	path: &'a str,
	quoted: bool,
}

fn includes(source: &str) -> impl Iterator<Item = IncludeDirective<'_>> {
	source.lines().filter_map(|line| {
		let directive = line.trim_start().strip_prefix('#')?.trim_start();
		let rest = directive.strip_prefix("include")?;
		if rest
			.chars()
			.next()
			.is_some_and(|character| character.is_alphanumeric() || character == '_')
		{
			return None;
		}
		let rest = rest.trim_start();
		if let Some(quoted) = rest.strip_prefix('"') {
			let end = quoted.find('"')?;
			return Some(IncludeDirective {
				path: &quoted[..end],
				quoted: true,
			});
		}
		let system = rest.strip_prefix('<')?;
		let end = system.find('>')?;
		Some(IncludeDirective {
			path: &system[..end],
			quoted: false,
		})
	})
}

#[derive(Default)]
struct MakefileHints {
	include_paths: Vec<PathBuf>,
	external_include_package: Option<String>,
}

fn load_makefile_hints(root: &Path) -> MakefileHints {
	let Ok(bytes) = std::fs::read(root.join("Makefile")) else {
		return MakefileHints::default();
	};
	let text = String::from_utf8_lossy(&bytes).replace("\\\n", " ");
	let mut paths = Vec::new();
	for line in text.lines() {
		let Some((_, value)) = line.split_once('=') else {
			continue;
		};
		let mut tokens = value.split_whitespace().peekable();
		while let Some(token) = tokens.next() {
			let raw = if token == "-I" {
				tokens.next()
			} else {
				token.strip_prefix("-I")
			};
			let Some(raw) = raw else {
				continue;
			};
			let raw = raw.trim_matches(['\'', '"']);
			if raw.is_empty() || raw.contains('$') || raw.contains('`') || raw.starts_with('-') {
				continue;
			}
			let path = absolute_normalized(&root.join(raw));
			if path.is_dir() && !paths.contains(&path) {
				paths.push(path);
			}
		}
	}
	let external_include_package = (text.contains("PGXS")
		&& (text.contains("PG_CONFIG") || text.contains("pg_config")))
	.then(|| "postgresql".to_string());
	MakefileHints {
		include_paths: paths,
		external_include_package,
	}
}

fn absolute_normalized(path: &Path) -> PathBuf {
	let absolute = if path.is_absolute() {
		path.to_path_buf()
	} else {
		std::env::current_dir()
			.unwrap_or_else(|_| PathBuf::from("."))
			.join(path)
	};
	let mut normalized = PathBuf::new();
	for component in absolute.components() {
		match component {
			Component::CurDir => {}
			Component::ParentDir => {
				normalized.pop();
			}
			other => normalized.push(other.as_os_str()),
		}
	}
	normalized
}

fn project_relative_path(root: &Path, path: &Path) -> Option<String> {
	let relative = absolute_normalized(path)
		.strip_prefix(root)
		.ok()?
		.to_path_buf();
	Some(
		relative
			.components()
			.filter_map(|component| component.as_os_str().to_str())
			.collect::<Vec<_>>()
			.join("/"),
	)
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::fs;

	fn write(root: &Path, relative: &str, body: &str) {
		let path = root.join(relative);
		if let Some(parent) = path.parent() {
			fs::create_dir_all(parent).unwrap();
		}
		fs::write(path, body).unwrap();
	}

	#[test]
	fn cpp_only_header_is_not_indexed_as_c() {
		let temp = tempfile::tempdir().unwrap();
		write(
			temp.path(),
			"generated/model.pb.cc",
			"#include \"model.pb.h\"\n",
		);
		write(
			temp.path(),
			"generated/model.pb.h",
			"namespace generated {}\n",
		);

		let context = CBuildContext::load(temp.path());

		assert!(!context.should_index_as_c(&temp.path().join("generated/model.pb.h")));
	}

	#[test]
	fn header_shared_with_c_translation_unit_remains_c_indexable() {
		let temp = tempfile::tempdir().unwrap();
		write(temp.path(), "main.c", "#include \"shared.h\"\n");
		write(temp.path(), "main.cpp", "#include \"shared.h\"\n");
		write(temp.path(), "shared.h", "int shared(void);\n");

		let context = CBuildContext::load(temp.path());

		assert!(context.should_index_as_c(&temp.path().join("shared.h")));
	}

	#[test]
	fn cpp_only_project_does_not_treat_orphan_headers_as_c() {
		let temp = tempfile::tempdir().unwrap();
		write(temp.path(), "main.cpp", "int main() { return 0; }\n");
		write(temp.path(), "orphan.h", "class Orphan {};\n");

		let context = CBuildContext::load(temp.path());

		assert!(!context.should_index_as_c(&temp.path().join("orphan.h")));
	}

	#[test]
	fn transitive_cpp_headers_are_not_indexed_as_c() {
		let temp = tempfile::tempdir().unwrap();
		write(temp.path(), "main.cpp", "#include \"first.hpp\"\n");
		write(temp.path(), "first.hpp", "#include \"second.h\"\n");
		write(temp.path(), "second.h", "class Second {};\n");

		let context = CBuildContext::load(temp.path());

		assert!(!context.should_index_as_c(&temp.path().join("second.h")));
	}

	#[test]
	fn makefile_include_path_resolves_cpp_header_provenance() {
		let temp = tempfile::tempdir().unwrap();
		write(temp.path(), "Makefile", "CXXFLAGS += -I./include\n");
		write(
			temp.path(),
			"src/model.cpp",
			"#include \"generated/model.h\"\n",
		);
		write(
			temp.path(),
			"include/generated/model.h",
			"class Model {};\n",
		);

		let context = CBuildContext::load(temp.path());

		assert!(!context.should_index_as_c(&temp.path().join("include/generated/model.h")));
	}

	#[test]
	fn angle_include_through_makefile_path_marks_header_as_c() {
		let temp = tempfile::tempdir().unwrap();
		write(temp.path(), "Makefile", "CPPFLAGS += -I./include\n");
		write(temp.path(), "main.cpp", "#include \"shared.h\"\n");
		write(temp.path(), "main.c", "#include <shared.h>\n");
		write(temp.path(), "include/shared.h", "int shared(void);\n");

		let context = CBuildContext::load(temp.path());

		assert!(context.should_index_as_c(&temp.path().join("include/shared.h")));
	}

	#[test]
	fn pgxs_makefile_declares_postgresql_header_provenance() {
		let temp = tempfile::tempdir().unwrap();
		write(
			temp.path(),
			"Makefile",
			"USE_PGXS = 1\nPG_CONFIG = pg_config\nPGXS := $(shell $(PG_CONFIG) --pgxs)\ninclude $(PGXS)\n",
		);

		let context = CBuildContext::load(temp.path());

		assert_eq!(
			context
				.extraction_presets()
				.external_include_package
				.as_deref(),
			Some("postgresql")
		);
	}
}