[clang] ff9576f - [Frontend] Fix -MP when main file is <stdin>

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 28 20:35:34 PDT 2022


Author: Fangrui Song
Date: 2022-10-28T20:35:29-07:00
New Revision: ff9576f74514b836e1ba0268409a2ecb919d7118

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

LOG: [Frontend] Fix -MP when main file is <stdin>

rC220726 had a bug: `echo "<cstdlib>" | clang -M -MP -x c++ - 2>/dev/null`
(used by glibc/configure.ac find_cxx_header) omitted a `cstdlib:` line. Instead
of filtering out `<stdin>` in `Dependencies`, retain it (so that the number of
entries does not change whether or not main file is `<stdin>`) and filter the
`PhonyTarget` output.

Added: 
    clang/test/Frontend/dependency-gen-phony.c

Modified: 
    clang/lib/Frontend/DependencyFile.cpp

Removed: 
    clang/test/Frontend/dependency-gen-extradeps-phony.c


################################################################################
diff  --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp
index 06ddb0f0db201..930d5b488b8f4 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -160,10 +160,7 @@ bool DependencyCollector::addDependency(StringRef Filename) {
 }
 
 static bool isSpecialFilename(StringRef Filename) {
-  return llvm::StringSwitch<bool>(Filename)
-      .Case("<built-in>", true)
-      .Case("<stdin>", true)
-      .Default(false);
+  return Filename == "<built-in>";
 }
 
 bool DependencyCollector::sawDependency(StringRef Filename, bool FromModule,
@@ -356,6 +353,8 @@ void DependencyFileGenerator::outputDependencyFile(llvm::raw_ostream &OS) {
   // duplicates.
   ArrayRef<std::string> Files = getDependencies();
   for (StringRef File : Files) {
+    if (File == "<stdin>")
+      continue;
     // Start a new line if this would exceed the column limit. Make
     // sure to leave space for a trailing " \" in case we need to
     // break the line on the next iteration.

diff  --git a/clang/test/Frontend/dependency-gen-extradeps-phony.c b/clang/test/Frontend/dependency-gen-extradeps-phony.c
deleted file mode 100644
index 2d67a959ed85e..0000000000000
--- a/clang/test/Frontend/dependency-gen-extradeps-phony.c
+++ /dev/null
@@ -1,10 +0,0 @@
-// RUN: %clang -MM -MP -Xclang -fdepfile-entry=1.extra -Xclang -fdepfile-entry=2.extra -Xclang -fdepfile-entry=2.extra %s | \
-// RUN: FileCheck %s --implicit-check-not=.c:
-//
-// Verify that phony targets are only created for the extra dependency files,
-// and not the input file.
-
-// CHECK: dependency-gen-extradeps-phony.o: 1.extra 2.extra \
-// CHECK-NEXT: dependency-gen-extradeps-phony.c
-// CHECK: 1.extra:
-// CHECK: 2.extra:

diff  --git a/clang/test/Frontend/dependency-gen-phony.c b/clang/test/Frontend/dependency-gen-phony.c
new file mode 100644
index 0000000000000..5bfb02ba819a5
--- /dev/null
+++ b/clang/test/Frontend/dependency-gen-phony.c
@@ -0,0 +1,23 @@
+// RUN: cd %S
+// RUN: %clang -MM -MP -I Inputs -Xclang -fdepfile-entry=1.extra -Xclang -fdepfile-entry=2.extra -Xclang -fdepfile-entry=2.extra dependency-gen-phony.c | \
+// RUN:   FileCheck %s --match-full-lines --strict-whitespace --implicit-check-not=.c:
+// RUN: %clang -MM -MP -I Inputs -xc - < dependency-gen-phony.c | FileCheck %s --check-prefix=STDIO --implicit-check-not=.c:
+
+/// Verify that phony targets are only created for the extra dependency files,
+/// and not the input file.
+//       CHECK:dependency-gen-phony.o: 1.extra 2.extra dependency-gen-phony.c \
+//  CHECK-NEXT:  Inputs{{/|\\\\}}empty.h
+// CHECK-EMPTY:
+//  CHECK-NEXT:1.extra:
+// CHECK-EMPTY:
+//  CHECK-NEXT:2.extra:
+// CHECK-EMPTY:
+//  CHECK-NEXT:{{.*}}empty.h:
+//   CHECK-NOT:{{.}}
+
+// STDIO:      -.o: Inputs{{/|\\\\}}empty.h
+// STDIO-EMPTY:
+// STDIO-NEXT: {{.*}}empty.h:
+// STDIO-NOT:  {{.}}
+
+#include "empty.h"


        


More information about the cfe-commits mailing list