Skip to content

[clang-scan-deps] [P1689] Keep consistent behavior for make dependencies with clang #69551

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 2 commits into from
Oct 31, 2023
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
12 changes: 12 additions & 0 deletions clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,12 +666,24 @@ static StringRef makeAbsoluteAndPreferred(CompilerInstance &CI, StringRef Path,
}

void ModuleDepCollector::addFileDep(StringRef Path) {
if (IsStdModuleP1689Format) {
// Within P1689 format, we don't want all the paths to be absolute path
// since it may violate the tranditional make style dependencies info.
FileDeps.push_back(std::string(Path));
return;
}

llvm::SmallString<256> Storage;
Path = makeAbsoluteAndPreferred(ScanInstance, Path, Storage);
FileDeps.push_back(std::string(Path));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a use-after-free of Storage since Path will point to that buffer if the string is modified. Same for the other case below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks for your catch!

}

void ModuleDepCollector::addFileDep(ModuleDeps &MD, StringRef Path) {
if (IsStdModuleP1689Format) {
MD.FileDeps.insert(Path);
return;
}

llvm::SmallString<256> Storage;
Path = makeAbsoluteAndPreferred(ScanInstance, Path, Storage);
MD.FileDeps.insert(Path);
Expand Down
10 changes: 10 additions & 0 deletions clang/test/ClangScanDeps/P1689.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
// RUN: clang-scan-deps -format=p1689 \
// RUN: -- %clang++ -std=c++20 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -c %t/impl_part.cppm -o %t/impl_part.o \
// RUN: | FileCheck %t/impl_part.cppm -DPREFIX=%/t
//
// Check the path in the make style dependencies are generated in relative path form
// RUN: cd %t
// RUN: clang-scan-deps -format=p1689 \
// RUN: -- %clang++ -std=c++20 -c -fprebuilt-module-path=%t impl_part.cppm -o impl_part.o \
// RUN: -MT impl_part.o.ddi -MD -MF impl_part.dep
// RUN: cat impl_part.dep | FileCheck impl_part.cppm -DPREFIX=%/t --check-prefix=CHECK-MAKE-RELATIVE


//--- P1689.json.in
[
Expand Down Expand Up @@ -168,6 +176,8 @@ void World() {
// CHECK-MAKE: [[PREFIX]]/impl_part.cppm
// CHECK-MAKE: [[PREFIX]]/header.mock

// CHECK-MAKE-RELATIVE: impl_part.o.ddi: impl_part.cppm header.mock

//--- interface_part.cppm
export module M:interface_part;
export void World();
Expand Down