diff --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h --- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h +++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h @@ -126,9 +126,9 @@ public: ModuleDepCollectorPP(ModuleDepCollector &MDC) : MDC(MDC) {} - void FileChanged(SourceLocation Loc, FileChangeReason Reason, - SrcMgr::CharacteristicKind FileType, - FileID PrevFID) override; + void LexedFileChanged(FileID FID, LexedFileChangeReason Reason, + SrcMgr::CharacteristicKind FileType, FileID PrevFID, + SourceLocation Loc) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, diff --git a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp --- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp +++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp @@ -301,11 +301,12 @@ assert(Inserted && "duplicate module mapping"); } -void ModuleDepCollectorPP::FileChanged(SourceLocation Loc, - FileChangeReason Reason, - SrcMgr::CharacteristicKind FileType, - FileID PrevFID) { - if (Reason != PPCallbacks::EnterFile) +void ModuleDepCollectorPP::LexedFileChanged(FileID FID, + LexedFileChangeReason Reason, + SrcMgr::CharacteristicKind FileType, + FileID PrevFID, + SourceLocation Loc) { + if (Reason != LexedFileChangeReason::EnterFile) return; // This has to be delayed as the context hash can change at the start of @@ -320,8 +321,7 @@ // Dependency generation really does want to go all the way to the // file entry for a source location to find out what is depended on. // We do not want #line markers to affect dependency generation! - if (std::optional Filename = - SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc)))) + if (std::optional Filename = SM.getNonBuiltinFilenameForID(FID)) MDC.addFileDep(llvm::sys::path::remove_leading_dotslash(*Filename)); } diff --git a/clang/test/ClangScanDeps/line-directive.c b/clang/test/ClangScanDeps/line-directive.c new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/line-directive.c @@ -0,0 +1,34 @@ +// Check that we get the right file dependencies and not the declared paths from +// line directives. + +// RUN: rm -rf %t +// RUN: split-file %s %t +// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json + +// RUN: clang-scan-deps -compilation-database %t/cdb.json \ +// RUN: -mode preprocess-dependency-directives -format experimental-full > %t/deps.json + +// RUN: cat %t/deps.json | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t + +// CHECK: "file-deps": [ +// CHECK-NEXT: "[[PREFIX]]/tu.c" +// CHECK-NEXT: "[[PREFIX]]/header.h" +// CHECK-NEXT: ] + +//--- cdb.json.template +[{ + "file": "DIR/tu.c", + "directory": "DIR", + "command": "clang -fsyntax-only DIR/tu.c" +}] + +//--- other.h + +//--- other.c + +//--- header.h +#line 100 "other.h" + +//--- tu.c +#include "header.h" +#line 100 "other.c"