[clang] 8711120 - [clang][deps] Migrate ModuleDepCollector to LexedFileChanged NFCI

Ben Langmuir via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 9 11:43:22 PST 2023


Author: Ben Langmuir
Date: 2023-02-09T11:42:03-08:00
New Revision: 8711120e8bcf891f3c316e20869493e93472f200

URL: https://github.com/llvm/llvm-project/commit/8711120e8bcf891f3c316e20869493e93472f200
DIFF: https://github.com/llvm/llvm-project/commit/8711120e8bcf891f3c316e20869493e93472f200.diff

LOG: [clang][deps] Migrate ModuleDepCollector to LexedFileChanged NFCI

LexedFileChanged has the semantics we want of ignoring #line/etc. It's
also consistent with other dep collectors like DependencyFileGenerator.

Differential Revision: https://reviews.llvm.org/D143613

Added: 
    clang/test/ClangScanDeps/line-directive.c

Modified: 
    clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
    clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
index 6e4cec25961de..3e1b71e6b8a48 100644
--- a/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
+++ b/clang/include/clang/Tooling/DependencyScanning/ModuleDepCollector.h
@@ -126,9 +126,9 @@ class ModuleDepCollectorPP final : public PPCallbacks {
 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
index 4ff9e606677a8..94ece9eb4c685 100644
--- a/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
+++ b/clang/lib/Tooling/DependencyScanning/ModuleDepCollector.cpp
@@ -301,11 +301,12 @@ void ModuleDepCollector::associateWithContextHash(const CompilerInvocation &CI,
   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 @@ void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
   // 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<StringRef> Filename =
-          SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc))))
+  if (std::optional<StringRef> 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
index 0000000000000..4bf532167a935
--- /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"


        


More information about the cfe-commits mailing list