https://github.com/bob-wilson created 
https://github.com/llvm/llvm-project/pull/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.

>From 8577e445719fe62b4cee393e277525e24ea2d7d1 Mon Sep 17 00:00:00 2001
From: Bob Wilson <bob.wil...@apple.com>
Date: Thu, 17 Apr 2025 17:42:26 -0700
Subject: [PATCH] [Clang] Fix crash when -header-include-filtering is not
 specified

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.
---
 clang/lib/Frontend/CompilerInvocation.cpp   | 4 ++--
 clang/test/Preprocessor/print-header-json.c | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index cfc5c069b0849..f0b487299fc6e 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -2440,8 +2440,8 @@ static bool 
ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
       (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();
+        << headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
+        << headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
 
   return Diags.getNumErrors() == NumErrorsBefore;
 }
diff --git a/clang/test/Preprocessor/print-header-json.c 
b/clang/test/Preprocessor/print-header-json.c
index d0d5e6b6f7d9e..bb41337d4c7cf 100644
--- a/clang/test/Preprocessor/print-header-json.c
+++ b/clang/test/Preprocessor/print-header-json.c
@@ -2,6 +2,7 @@
 // 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: 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=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

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

Reply via email to