Skip to content

Commit 4f36ada

Browse files
authored
[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 f75295f commit 4f36ada

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
@@ -396,8 +396,14 @@ def err_drv_print_header_env_var : Error<
396396
"environment variable CC_PRINT_HEADERS_%select{FORMAT|FILTERING}0 has invalid value %1">;
397397
def err_drv_print_header_env_var_combination : Error<
398398
"unsupported combination: CC_PRINT_HEADERS_FORMAT=%0 and CC_PRINT_HEADERS_FILTERING=%1">;
399-
def err_drv_print_header_env_var_combination_cc1 : Error<
399+
def err_drv_print_header_env_var_invalid_format : Error<
400+
"environment variable CC_PRINT_HEADERS_FORMAT=%0 requires a compatible value for CC_PRINT_HEADERS_FILTERING">;
401+
def err_drv_print_header_cc1_invalid_combination : Error<
400402
"unsupported combination: -header-include-format=%0 and -header-include-filtering=%1">;
403+
def err_drv_print_header_cc1_invalid_filtering : Error<
404+
"-header-include-filtering=%0 requires a compatible value for -header-include-format">;
405+
def err_drv_print_header_cc1_invalid_format : Error<
406+
"-header-include-format=%0 requires a compatible value for -header-include-filtering">;
401407

402408
def warn_O4_is_O3 : Warning<"-O4 is equivalent to -O3">, InGroup<Deprecated>;
403409
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
@@ -2435,13 +2435,25 @@ static bool ParseDependencyOutputArgs(DependencyOutputOptions &Opts,
24352435

24362436
// Check for invalid combinations of header-include-format
24372437
// and header-include-filtering.
2438-
if ((Opts.HeaderIncludeFormat == HIFMT_Textual &&
2439-
Opts.HeaderIncludeFiltering != HIFIL_None) ||
2440-
(Opts.HeaderIncludeFormat == HIFMT_JSON &&
2441-
Opts.HeaderIncludeFiltering != HIFIL_Only_Direct_System))
2442-
Diags.Report(diag::err_drv_print_header_env_var_combination_cc1)
2443-
<< Args.getLastArg(OPT_header_include_format_EQ)->getValue()
2444-
<< Args.getLastArg(OPT_header_include_filtering_EQ)->getValue();
2438+
if (Opts.HeaderIncludeFormat == HIFMT_Textual &&
2439+
Opts.HeaderIncludeFiltering != HIFIL_None) {
2440+
if (Args.hasArg(OPT_header_include_format_EQ))
2441+
Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
2442+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
2443+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2444+
else
2445+
Diags.Report(diag::err_drv_print_header_cc1_invalid_filtering)
2446+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2447+
} else if (Opts.HeaderIncludeFormat == HIFMT_JSON &&
2448+
Opts.HeaderIncludeFiltering == HIFIL_None) {
2449+
if (Args.hasArg(OPT_header_include_filtering_EQ))
2450+
Diags.Report(diag::err_drv_print_header_cc1_invalid_combination)
2451+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat)
2452+
<< headerIncludeFilteringKindToString(Opts.HeaderIncludeFiltering);
2453+
else
2454+
Diags.Report(diag::err_drv_print_header_cc1_invalid_format)
2455+
<< headerIncludeFormatKindToString(Opts.HeaderIncludeFormat);
2456+
}
24452457

24462458
return Diags.getNumErrors() == NumErrorsBefore;
24472459
}

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
@@ -156,6 +156,11 @@ static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
156156
}
157157

158158
const char *FilteringStr = ::getenv("CC_PRINT_HEADERS_FILTERING");
159+
if (!FilteringStr) {
160+
TheDriver.Diag(clang::diag::err_drv_print_header_env_var_invalid_format)
161+
<< EnvVar;
162+
return false;
163+
}
159164
HeaderIncludeFilteringKind Filtering;
160165
if (!stringToHeaderIncludeFiltering(FilteringStr, Filtering)) {
161166
TheDriver.Diag(clang::diag::err_drv_print_header_env_var)

0 commit comments

Comments
 (0)