Author: Bob Wilson
Date: 2025-04-23T16:09:38-07:00
New Revision: 4f36ada1e205df08ad4377df88729f8defb36558
URL: 
https://github.com/llvm/llvm-project/commit/4f36ada1e205df08ad4377df88729f8defb36558
DIFF: 
https://github.com/llvm/llvm-project/commit/4f36ada1e205df08ad4377df88729f8defb36558.diff

LOG: [Clang] Fix crash when -header-include-filtering is not specified (#136232)

If you specify -header-include-format=json, the only filtering option
currently supported is -header-include-filtering=only-direct-system. If
you specify some other filtering option, Clang gives an error message.
But, if you do not specify the filtering option at all, Clang crashes
when producing the error message, since it tries to get the value of the
unused option.

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticDriverKinds.td
    clang/lib/Frontend/CompilerInvocation.cpp
    clang/test/Preprocessor/print-header-json.c
    clang/tools/driver/driver.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index c69ad3adc5b3b..b15cba698030c 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -396,8 +396,14 @@ def err_drv_print_header_env_var : Error<
   "environment variable CC_PRINT_HEADERS_%select{FORMAT|FILTERING}0 has 
invalid value %1">;
 def err_drv_print_header_env_var_combination : Error<
   "unsupported combination: CC_PRINT_HEADERS_FORMAT=%0 and 
CC_PRINT_HEADERS_FILTERING=%1">;
-def err_drv_print_header_env_var_combination_cc1 : Error<
+def err_drv_print_header_env_var_invalid_format : Error<
+  "environment variable CC_PRINT_HEADERS_FORMAT=%0 requires a compatible value 
for CC_PRINT_HEADERS_FILTERING">;
+def err_drv_print_header_cc1_invalid_combination : Error<
   "unsupported combination: -header-include-format=%0 and 
-header-include-filtering=%1">;
+def err_drv_print_header_cc1_invalid_filtering : Error<
+  "-header-include-filtering=%0 requires a compatible value for 
-header-include-format">;
+def err_drv_print_header_cc1_invalid_format : Error<
+  "-header-include-format=%0 requires a compatible value for 
-header-include-filtering">;
 
 def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup<Deprecated>;
 def warn_drv_optimization_value : Warning<"optimization level '%0' is not 
supported; using '%1%2' instead">,

diff  --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 9e9eed4efc577..1df503859204d 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2435,13 +2435,25 @@ static bool 
ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
 
   // Check for invalid combinations of header-include-format
   // and header-include-filtering.
-  if ((Opts.HeaderIncludeFormat == HIFMT_Textual &&
-       Opts.HeaderIncludeFiltering != HIFIL_None) ||
-      (Opts.HeaderIncludeFormat == HIFMT_JSON &&
-       Opts.HeaderIncludeFiltering != HIFIL_Only_Direct_System))
-    Diags.Report(diag::err_drv_print_header_env_var_combination_cc1)
-        << Args.getLastArg(OPT_header_include_format_EQ)->getValue()
-        << Args.getLastArg(OPT_header_include_filtering_EQ)->getValue();
+  if (Opts.HeaderIncludeFormat == HIFMT_Textual &&
+      Opts.HeaderIncludeFiltering != HIFIL_None) {
+    if (Args.hasArg(OPT_header_include_format_EQ))
+      Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
+          << headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
+          << headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
+    else
+      Diags.Report(diag::err_drv_print_header_cc1_invalid_filtering)
+          << headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
+  } else if (Opts.HeaderIncludeFormat == HIFMT_JSON &&
+             Opts.HeaderIncludeFiltering == HIFIL_None) {
+    if (Args.hasArg(OPT_header_include_filtering_EQ))
+      Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
+          << headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
+          << headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
+    else
+      Diags.Report(diag::err_drv_print_header_cc1_invalid_format)
+          << headerIncludeFormatKindToString(Opts.HeaderIncludeFormat);
+  }
 
   return Diags.getNumErrors() == NumErrorsBefore;
 }

diff  --git a/clang/test/Preprocessor/print-header-json.c 
b/clang/test/Preprocessor/print-header-json.c
index d0d5e6b6f7d9e..1ba63ddc7a249 100644
--- a/clang/test/Preprocessor/print-header-json.c
+++ b/clang/test/Preprocessor/print-header-json.c
@@ -1,11 +1,16 @@
 // RUN: %clang_cc1 -E -header-include-format=json 
-header-include-filtering=only-direct-system -header-include-file %t.txt -I 
%S/Inputs/print-header-json -isystem %S/Inputs/print-header-json/system %s
 // RUN: cat %t.txt | FileCheck %s --check-prefix=SUPPORTED
+
 // RUN: not %clang_cc1 -E -header-include-format=textual 
-header-include-filtering=only-direct-system -header-include-file %t.txt -I 
%S/Inputs/print-header-json -isystem %S/Inputs/print-header-json/system %s -o 
/dev/null 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED0
 // RUN: not %clang_cc1 -E -header-include-format=json 
-header-include-filtering=none -header-include-file %t.txt -I 
%S/Inputs/print-header-json -isystem %S/Inputs/print-header-json/system %s -o 
/dev/null 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED1
-// RUN: rm %t.txt
-// RUN: env CC_PRINT_HEADERS_FORMAT=json 
CC_PRINT_HEADERS_FILTERING=only-direct-system CC_PRINT_HEADERS_FILE=%t.txt 
%clang -fsyntax-only -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null
 // RUN: env CC_PRINT_HEADERS_FORMAT=textual 
CC_PRINT_HEADERS_FILTERING=only-direct-system CC_PRINT_HEADERS_FILE=%t.txt not 
%clang -fsyntax-only -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null 2>&1 | FileCheck %s 
--check-prefix=UNSUPPORTED2
 // RUN: env CC_PRINT_HEADERS_FORMAT=json CC_PRINT_HEADERS_FILTERING=none 
CC_PRINT_HEADERS_FILE=%t.txt not %clang -fsyntax-only -I 
%S/Inputs/print-header-json -isystem %S/Inputs/print-header-json/system %s -o 
/dev/null 2>&1 | FileCheck %s --check-prefix=UNSUPPORTED3
+// RUN: env CC_PRINT_HEADERS_FORMAT=json CC_PRINT_HEADERS_FILE=%t.txt not 
%clang -fsyntax-only -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null 2>&1 | FileCheck %s 
--check-prefix=UNSUPPORTED4
+// RUN: not %clang_cc1 -E -header-include-filtering=only-direct-system 
-header-include-file %t.txt -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null 2>&1 | FileCheck %s 
--check-prefix=UNSUPPORTED5
+// RUN: not %clang_cc1 -E -header-include-format=json -header-include-file 
%t.txt -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null 2>&1 | FileCheck %s 
--check-prefix=UNSUPPORTED6
+
+// RUN: rm %t.txt
+// RUN: env CC_PRINT_HEADERS_FORMAT=json 
CC_PRINT_HEADERS_FILTERING=only-direct-system CC_PRINT_HEADERS_FILE=%t.txt 
%clang -fsyntax-only -I %S/Inputs/print-header-json -isystem 
%S/Inputs/print-header-json/system %s -o /dev/null
 // RUN: cat %t.txt | FileCheck %s --check-prefix=SUPPORTED
 
 #include "system0.h"
@@ -18,3 +23,6 @@
 // UNSUPPORTED1: error: unsupported combination: -header-include-format=json 
and -header-include-filtering=none
 // UNSUPPORTED2: error: unsupported combination: 
CC_PRINT_HEADERS_FORMAT=textual and 
CC_PRINT_HEADERS_FILTERING=only-direct-system
 // UNSUPPORTED3: error: unsupported combination: CC_PRINT_HEADERS_FORMAT=json 
and CC_PRINT_HEADERS_FILTERING=none
+// UNSUPPORTED4: error: environment variable CC_PRINT_HEADERS_FORMAT=json 
requires a compatible value for CC_PRINT_HEADERS_FILTERING
+// UNSUPPORTED5: error: -header-include-filtering=only-direct-system requires 
a compatible value for -header-include-format
+// UNSUPPORTED6: error: -header-include-format=json requires a compatible 
value for -header-include-filtering

diff  --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp
index 00c00cea16f47..db72b4a4526fe 100644
--- a/clang/tools/driver/driver.cpp
+++ b/clang/tools/driver/driver.cpp
@@ -156,6 +156,11 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver 
&TheDriver) {
       }
 
       const char *FilteringStr = ::getenv("CC_PRINT_HEADERS_FILTERING");
+      if (!FilteringStr) {
+        
TheDriver.Diag(clang::diag::err_drv_print_header_env_var_invalid_format)
+            << EnvVar;
+        return false;
+      }
       HeaderIncludeFilteringKind Filtering;
       if (!stringToHeaderIncludeFiltering(FilteringStr, Filtering)) {
         TheDriver.Diag(clang::diag::err_drv_print_header_env_var)


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to