From 4f36ada1e205df08ad4377df88729f8defb36558 Mon Sep 17 00:00:00 2001 From: Bob Wilson Date: Wed, 23 Apr 2025 16:09:38 -0700 Subject: [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. --- clang/lib/Frontend/CompilerInvocation.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 9e9eed4..1df5038 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; } -- cgit v1.1