Skip to content

Commit b6c6e63

Browse files
fix: resolve entry path in lookup function and add test for relative file resolution
1 parent 18aed86 commit b6c6e63

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

src/extract/compdb.cppm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ auto lookup(const CompilationDatabase& db, std::string_view file)
9696
auto target = fs::path(file).lexically_normal();
9797

9898
for(auto& entry : db.entries) {
99-
auto entry_path = fs::path(entry.file).lexically_normal();
99+
auto entry_path = (fs::path(entry.directory) / entry.file).lexically_normal();
100100
if(entry_path == target) {
101101
results.push_back(&entry);
102102
}

src/generate/generate.cppm

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,6 @@ auto build_module_page_graph(const config::TaskConfig& config,
414414
}
415415
}
416416

417-
if(auto colon_pos = mod_name.find(':'); colon_pos != std::string::npos) {
418-
auto main_name = mod_name.substr(0, colon_pos);
419-
if(auto main_it = mod_to_page.find(main_name); main_it != mod_to_page.end()) {
420-
add_page_dependency(graph, page_a, main_it->second);
421-
}
422-
}
423417
}
424418

425419
// Call graph edges between module pages

tests/unit/extract/compdb.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ TEST_SUITE(compdb) {
8787
auto no_results = lookup(db, "/project/src/nonexistent.cpp");
8888
EXPECT_EQ(no_results.size(), 0u);
8989
}
90+
91+
TEST_CASE(lookup_resolves_relative_file_against_entry_directory) {
92+
namespace fs = std::filesystem;
93+
94+
auto project_dir = (fs::temp_directory_path() / "clore_test_compdb_relative").lexically_normal();
95+
auto source_path = (project_dir / "src" / "main.cpp").lexically_normal();
96+
97+
CompilationDatabase db;
98+
db.entries.push_back(CompileEntry{
99+
.file = "src/main.cpp",
100+
.directory = project_dir.generic_string(),
101+
.arguments = {"clang++", "-c", "src/main.cpp"},
102+
});
103+
104+
auto results = lookup(db, source_path.generic_string());
105+
ASSERT_EQ(results.size(), 1u);
106+
EXPECT_EQ(results.front()->file, "src/main.cpp");
107+
EXPECT_EQ(results.front()->directory, project_dir.generic_string());
108+
}
90109
};

tests/unit/generate/generate.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,65 @@ TEST_SUITE(generate) {
364364
EXPECT_NE(partition_prompt_it->prompt.find("## Module: `demo.math:detail`"),
365365
std::string::npos);
366366
}
367+
368+
TEST_CASE(build_module_graph_does_not_create_partition_main_cycle) {
369+
ScopedTempDir temp("build_module_cycle");
370+
fs::create_directories(temp.path / "src");
371+
372+
auto config = make_config(temp.path);
373+
374+
extract::ProjectModel model;
375+
model.uses_modules = true;
376+
377+
auto main_file = (temp.path / "src" / "math.cppm").generic_string();
378+
auto partition_file = (temp.path / "src" / "math.detail.cppm").generic_string();
379+
380+
auto api_symbol = make_symbol(21, "add", "demo::math::add", "int add(int lhs, int rhs)", main_file);
381+
auto partition_symbol = make_symbol(22, "detail", "demo::math::detail", "int detail()", partition_file);
382+
383+
model.symbols.emplace(api_symbol.id, api_symbol);
384+
model.symbols.emplace(partition_symbol.id, partition_symbol);
385+
386+
model.modules.emplace(
387+
main_file,
388+
extract::ModuleUnit{
389+
.name = "demo.math",
390+
.is_interface = true,
391+
.source_file = main_file,
392+
.imports = {"demo.math:detail"},
393+
.symbols = {api_symbol.id},
394+
});
395+
model.modules.emplace(
396+
partition_file,
397+
extract::ModuleUnit{
398+
.name = "demo.math:detail",
399+
.is_interface = true,
400+
.source_file = partition_file,
401+
.imports = {},
402+
.symbols = {partition_symbol.id},
403+
});
404+
405+
auto graph = build_page_graph(config, model);
406+
407+
ASSERT_TRUE(graph.nodes.contains("demo.math/index.md"));
408+
ASSERT_TRUE(graph.nodes.contains("demo.math/detail.md"));
409+
410+
auto& main_node = graph.nodes.at("demo.math/index.md");
411+
auto& partition_node = graph.nodes.at("demo.math/detail.md");
412+
413+
EXPECT_EQ(std::count(main_node.depends_on.begin(), main_node.depends_on.end(),
414+
"demo.math/detail.md"),
415+
1);
416+
EXPECT_EQ(std::count(partition_node.depends_on.begin(), partition_node.depends_on.end(),
417+
"demo.math/index.md"),
418+
0);
419+
420+
auto pos_main = std::find(graph.generation_order.begin(), graph.generation_order.end(),
421+
"demo.math/index.md");
422+
auto pos_partition = std::find(graph.generation_order.begin(), graph.generation_order.end(),
423+
"demo.math/detail.md");
424+
ASSERT_TRUE(pos_main != graph.generation_order.end());
425+
ASSERT_TRUE(pos_partition != graph.generation_order.end());
426+
EXPECT_LT(pos_partition, pos_main);
427+
}
367428
};

0 commit comments

Comments
 (0)