[clang] [clang] Implement -Wmissing-include-dirs (PR #94827)

Braden Helmer via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 10 12:04:25 PDT 2024


https://github.com/bradenhelmer updated https://github.com/llvm/llvm-project/pull/94827

>From 317e4c2fe7fb0ee76f7917b64ee447ba3ed838bc Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Fri, 7 Jun 2024 21:38:04 -0400
Subject: [PATCH 1/2] Implement -Wmissing-include-dirs

---
 clang/include/clang/Basic/DiagnosticDriverKinds.td | 3 +++
 clang/include/clang/Basic/DiagnosticGroups.td      | 2 +-
 clang/lib/Driver/Driver.cpp                        | 6 ++++++
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 773b234cd68fe..b7d50e22cc0d0 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -809,4 +809,7 @@ def warn_android_unversioned_fallback : Warning<
 
 def err_drv_triple_version_invalid : Error<
   "version '%0' in target triple '%1' is invalid">;
+
+def warn_missing_include_dirs : Warning <
+	"the included directory %0 is missing">, InGroup<MissingIncludeDirs>;
 }
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 7d5ba7869ec34..9b37d4bd3205b 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -506,7 +506,7 @@ def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
 def : DiagGroup<"missing-format-attribute">;
-def : DiagGroup<"missing-include-dirs">;
+def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
 def : DiagGroup<"nested-externs">;
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index f5ea73a04ae5c..5bc737a43338e 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1271,6 +1271,12 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     if (VFS->setCurrentWorkingDirectory(WD->getValue()))
       Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
 
+  // Check for missing include directories
+  for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {
+    if (!llvm::sys::fs::is_directory(IncludeDir))
+      Diag(diag::warn_missing_include_dirs) << IncludeDir;
+  }
+
   // FIXME: This stuff needs to go into the Compilation, not the driver.
   bool CCCPrintPhases;
 

>From 6bd7085136d100bf13a7df41075e356999e18b2d Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Mon, 10 Jun 2024 15:03:25 -0400
Subject: [PATCH 2/2] Add test and address PR comments

---
 clang/include/clang/Basic/DiagnosticDriverKinds.td |  4 ++--
 clang/lib/Driver/Driver.cpp                        | 10 ++++++----
 clang/test/Driver/warning-options.cpp              |  4 ++++
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index b7d50e22cc0d0..9490e4b1df711 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -810,6 +810,6 @@ def warn_android_unversioned_fallback : Warning<
 def err_drv_triple_version_invalid : Error<
   "version '%0' in target triple '%1' is invalid">;
 
-def warn_missing_include_dirs : Warning <
-	"the included directory %0 is missing">, InGroup<MissingIncludeDirs>;
+def warn_missing_include_dirs : Warning<
+  "the included directory %0 is missing">, InGroup<MissingIncludeDirs>, DefaultIgnore;
 }
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 5bc737a43338e..67bf0604acd6e 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1271,10 +1271,12 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     if (VFS->setCurrentWorkingDirectory(WD->getValue()))
       Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
 
-  // Check for missing include directories
-  for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {
-    if (!llvm::sys::fs::is_directory(IncludeDir))
-      Diag(diag::warn_missing_include_dirs) << IncludeDir;
+  // Check for missing include directories.
+  if (!Diags.isIgnored(diag::warn_missing_include_dirs, SourceLocation())) {
+    for (auto IncludeDir : Args.getAllArgValues(options::OPT_I_Group)) {
+      if (!VFS->exists(IncludeDir))
+        Diag(diag::warn_missing_include_dirs) << IncludeDir;
+    }
   }
 
   // FIXME: This stuff needs to go into the Compilation, not the driver.
diff --git a/clang/test/Driver/warning-options.cpp b/clang/test/Driver/warning-options.cpp
index d836ad143a1c5..8eceb8f5f5544 100644
--- a/clang/test/Driver/warning-options.cpp
+++ b/clang/test/Driver/warning-options.cpp
@@ -6,3 +6,7 @@
 // Check that -isysroot warns on nonexistent paths.
 // RUN: %clang -### -c -target i386-apple-darwin10 -isysroot %t/warning-options %s 2>&1 | FileCheck --check-prefix=CHECK-ISYSROOT %s
 // CHECK-ISYSROOT: warning: no such sysroot directory: '{{.*}}/warning-options'
+
+// Check for proper warning with -Wmissing-include-dirs
+// RUN: %clang -### -Wmissing-include-dirs -I %t/warning-options %s 2>&1 | FileCheck --check-prefix=CHECK-MISSING-INCLUDE-DIRS %s
+// CHECK-MISSING-INCLUDE-DIRS: warning: the included directory {{.*}}/warning-options is missing [-Wmissing-include-dirs]



More information about the cfe-commits mailing list