Skip to content

[clang][scan] Report module dependencies in topological order #9197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clang/include/clang-c/Dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ CINDEX_LINKAGE size_t clang_experimental_DepGraph_getNumModules(CXDepGraph);
CINDEX_LINKAGE CXDepGraphModule
clang_experimental_DepGraph_getModule(CXDepGraph, size_t Index);

/**
* \returns the \c CXDepGraphModule object at the given \p Index in
* a topologically sorted list.
*
* The \c CXDepGraphModule object is only valid to use while \c CXDepGraph is
* valid. Must be disposed with \c clang_experimental_DepGraphModule_dispose.
*/
CINDEX_LINKAGE CXDepGraphModule
clang_experimental_DepGraph_getModuleTopological(CXDepGraph, size_t Index);

CINDEX_LINKAGE void clang_experimental_DepGraphModule_dispose(CXDepGraphModule);

/**
Expand Down
11 changes: 6 additions & 5 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,11 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
return {};

// If this module has been handled already, just return its ID.
auto ModI = MDC.ModularDeps.insert({M, nullptr});
if (!ModI.second)
return ModI.first->second->ID;
if (auto ModI = MDC.ModularDeps.find(M); ModI != MDC.ModularDeps.end())
return ModI->second->ID;

ModI.first->second = std::make_unique<ModuleDeps>();
ModuleDeps &MD = *ModI.first->second;
auto OwnedMD = std::make_unique<ModuleDeps>();
ModuleDeps &MD = *OwnedMD;

MD.ID.ModuleName = M->getFullModuleName();
MD.IsSystem = M->IsSystem;
Expand Down Expand Up @@ -756,6 +755,8 @@ ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {

MD.BuildInfo = std::move(CI);

MDC.ModularDeps.insert({M, std::move(OwnedMD)});

return MD.ID;
}

Expand Down
7 changes: 7 additions & 0 deletions clang/test/ClangScanDeps/modules-order-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
// RUN: diff %t/output1 %t/output2
// RUN: diff %t/output1 %t/output3

// And that module dependencies are in topological order.
// RUN: FileCheck --input-file %t/output1 %s
// CHECK: modules
// CHECK-DAG: name: FromMod1
// CHECK-DAG: name: FromMod2
// CHECK: name: FromMain1

//--- module.modulemap
module FromMain1 { header "FromMain1.h" }
module FromMain2 { header "FromMain2.h" }
Expand Down
3 changes: 2 additions & 1 deletion clang/tools/c-index-test/core_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,8 @@ static int scanDeps(ArrayRef<const char *> Args, std::string WorkingDirectory,
llvm::outs() << "modules:\n";
for (size_t I = 0, E = clang_experimental_DepGraph_getNumModules(Graph);
I < E; ++I) {
CXDepGraphModule Mod = clang_experimental_DepGraph_getModule(Graph, I);
CXDepGraphModule Mod =
clang_experimental_DepGraph_getModuleTopological(Graph, I);
const char *Name = clang_experimental_DepGraphModule_getName(Mod);
const char *ContextHash =
clang_experimental_DepGraphModule_getContextHash(Mod);
Expand Down
6 changes: 6 additions & 0 deletions clang/tools/libclang/CDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ size_t clang_experimental_DepGraph_getNumModules(CXDepGraph Graph) {

CXDepGraphModule clang_experimental_DepGraph_getModule(CXDepGraph Graph,
size_t Index) {
return clang_experimental_DepGraph_getModuleTopological(Graph, Index);
}

CXDepGraphModule
clang_experimental_DepGraph_getModuleTopological(CXDepGraph Graph,
size_t Index) {
TranslationUnitDeps &TUDeps = unwrap(Graph)->TUDeps;
return wrap(new DependencyGraphModule{&TUDeps.ModuleGraph[Index]});
}
Expand Down
1 change: 1 addition & 0 deletions clang/tools/libclang/libclang.map
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ LLVM_16 {
clang_experimental_DepGraph_dispose;
clang_experimental_DepGraph_getDiagnostics;
clang_experimental_DepGraph_getModule;
clang_experimental_DepGraph_getModuleTopological;
clang_experimental_DepGraph_getNumModules;
clang_experimental_DepGraph_getNumTUCommands;
clang_experimental_DepGraph_getTUCommand;
Expand Down