Skip to content

Commit af14c4b

Browse files
bob-wilsoncyndyishida
authored andcommitted
[Clang] Fix crash when -header-include-filtering is not specified (llvm#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.
1 parent d00f73e commit af14c4b

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,14 @@ def err_drv_print_header_env_var : Error<
399399
"environment variable CC_PRINT_HEADERS_%select{FORMAT|FILTERING}0 has invalid value %1">;
400400
def err_drv_print_header_env_var_combination : Error<
401401
"unsupported combination: CC_PRINT_HEADERS_FORMAT=%0 and CC_PRINT_HEADERS_FILTERING=%1">;
402-
def err_drv_print_header_env_var_combination_cc1 : Error<
402+
def err_drv_print_header_env_var_invalid_format : Error<
403+
"environment variable CC_PRINT_HEADERS_FORMAT=%0 requires a compatible value for CC_PRINT_HEADERS_FILTERING">;
404+
def err_drv_print_header_cc1_invalid_combination : Error<
403405
"unsupported combination: -header-include-format=%0 and -header-include-filtering=%1">;
406+
def err_drv_print_header_cc1_invalid_filtering : Error<
407+
"-header-include-filtering=%0 requires a compatible value for -header-include-format">;
408+
def err_drv_print_header_cc1_invalid_format : Error<
409+
"-header-include-format=%0 requires a compatible value for -header-include-filtering">;
404410

405411
def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup<Deprecated>;
406412
def warn_drv_optimization_value : Warning<"optimization level '%0' is not supported; using '%1%2' instead">,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2541,13 +2541,25 @@ static bool ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
25412541

25422542
// Check for invalid combinations of header-include-format
25432543
// and header-include-filtering.
2544-
if ((Opts.HeaderIncludeFormat == HIFMT_Textual &&
2545-
Opts.HeaderIncludeFiltering != HIFIL_None) ||
2546-
(Opts.HeaderIncludeFormat == HIFMT_JSON &&
2547-
Opts.HeaderIncludeFiltering != HIFIL_Only_Direct_System))
2548-
Diags.Report(diag::err_drv_print_header_env_var_combination_cc1)
2549-
<< Args.getLastArg(OPT_header_include_format_EQ)->getValue()
2550-
<< Args.getLastArg(OPT_header_include_filtering_EQ)->getValue();
2544+
if (Opts.HeaderIncludeFormat == HIFMT_Textual &&
2545+
Opts.HeaderIncludeFiltering != HIFIL_None) {
2546+
if (Args.hasArg(OPT_header_include_format_EQ))
2547+
Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
2548+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
2549+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2550+
else
2551+
Diags.Report(diag::err_drv_print_header_cc1_invalid_filtering)
2552+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2553+
} else if (Opts.HeaderIncludeFormat == HIFMT_JSON &&
2554+
Opts.HeaderIncludeFiltering == HIFIL_None) {
2555+
if (Args.hasArg(OPT_header_include_filtering_EQ))
2556+
Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
2557+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
2558+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2559+
else
2560+
Diags.Report(diag::err_drv_print_header_cc1_invalid_format)
2561+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat);
2562+
}
25512563

25522564
return Diags.getNumErrors() == NumErrorsBefore;
25532565
}

clang/test/Preprocessor/print-header-json.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// 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
22
// RUN: cat %t.txt | FileCheck %s --check-prefix=SUPPORTED
3+
34
// 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
45
// 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
5-
// RUN: rm %t.txt
6-
// 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
76
// 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
87
// 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
8+
// 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
9+
// 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
10+
// 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
11+
12+
// RUN: rm %t.txt
13+
// 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
914
// RUN: cat %t.txt | FileCheck %s --check-prefix=SUPPORTED
1015

1116
#include "system0.h"
@@ -18,3 +23,6 @@
1823
// UNSUPPORTED1: error: unsupported combination: -header-include-format=json and -header-include-filtering=none
1924
// UNSUPPORTED2: error: unsupported combination: CC_PRINT_HEADERS_FORMAT=textual and CC_PRINT_HEADERS_FILTERING=only-direct-system
2025
// UNSUPPORTED3: error: unsupported combination: CC_PRINT_HEADERS_FORMAT=json and CC_PRINT_HEADERS_FILTERING=none
26+
// UNSUPPORTED4: error: environment variable CC_PRINT_HEADERS_FORMAT=json requires a compatible value for CC_PRINT_HEADERS_FILTERING
27+
// UNSUPPORTED5: error: -header-include-filtering=only-direct-system requires a compatible value for -header-include-format
28+
// UNSUPPORTED6: error: -header-include-format=json requires a compatible value for -header-include-filtering

clang/tools/driver/driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
167167
}
168168

169169
const char *FilteringStr = ::getenv("CC_PRINT_HEADERS_FILTERING");
170+
if (!FilteringStr) {
171+
TheDriver.Diag(clang::diag::err_drv_print_header_env_var_invalid_format)
172+
<< EnvVar;
173+
return false;
174+
}
170175
HeaderIncludeFilteringKind Filtering;
171176
if (!stringToHeaderIncludeFiltering(FilteringStr, Filtering)) {
172177
TheDriver.Diag(clang::diag::err_drv_print_header_env_var)

0 commit comments

Comments
 (0)