Skip to content

Commit 41d9d50

Browse files
committed
Reland r371785: Add -Wpoison-system-directories warning
When using clang as a cross-compiler, we should not use system headers to do the compilation. This CL adds support of a new warning flag -Wpoison-system-directories which emits warnings if --sysroot is set and headers from common host system location are used. By default the warning is disabled. The intention of the warning is to catch bad includes which are usually generated by third party build system not targeting cross-compilation. Such cases happen in Chrome OS when someone imports a new package or upgrade one to a newer version from upstream. This is reland of r371785 with a fix to test file. Patch by: denik (Denis Nikitin) llvm-svn: 371878
1 parent 4cb267f commit 41d9d50

File tree

8 files changed

+41
-0
lines changed

8 files changed

+41
-0
lines changed

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,9 @@ def err_unknown_analyzer_checker_or_package : Error<
315315
"no analyzer checkers or packages are associated with '%0'">;
316316
def note_suggest_disabling_all_checkers : Note<
317317
"use -analyzer-disable-all-checks to disable all static analyzer checkers">;
318+
319+
// Poison system directories.
320+
def warn_poison_system_directories : Warning <
321+
"include location '%0' is unsafe for cross-compilation">,
322+
InGroup<DiagGroup<"poison-system-directories">>, DefaultIgnore;
318323
}

clang/lib/Frontend/InitHeaderSearch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
137137
SmallString<256> MappedPathStorage;
138138
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
139139

140+
// If use system headers while cross-compiling, emit the warning.
141+
if (HasSysroot && (MappedPathStr.startswith("/usr/include") ||
142+
MappedPathStr.startswith("/usr/local/include"))) {
143+
Headers.getDiags().Report(diag::warn_poison_system_directories)
144+
<< MappedPathStr;
145+
}
146+
140147
// Compute the DirectoryLookup type.
141148
SrcMgr::CharacteristicKind Type;
142149
if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {

clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/.keep

Whitespace-only changes.

clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c++/.keep

Whitespace-only changes.

clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/.keep

Whitespace-only changes.

clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/.keep

Whitespace-only changes.

clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/.keep

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// REQUIRES: x86-registered-target
2+
3+
// System directory and sysroot option causes warning.
4+
// RUN: %clang -Wpoison-system-directories -target x86_64 -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.1.stderr
5+
// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
6+
// RUN: %clang -Wpoison-system-directories -target x86_64 -cxx-isystem/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.1.stderr
7+
// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
8+
// RUN: %clang -Wpoison-system-directories -target x86_64 -iquote/usr/local/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.1.stderr
9+
// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
10+
// RUN: %clang -Wpoison-system-directories -target x86_64 -isystem/usr/local/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.1.stderr
11+
// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
12+
13+
// Missing target but included sysroot still causes the warning.
14+
// RUN: %clang -Wpoison-system-directories -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.2.stderr
15+
// RUN: FileCheck -check-prefix=WARN < %t.2.stderr %s
16+
17+
// With -Werror the warning causes the failure.
18+
// RUN: not %clang -Werror=poison-system-directories -target x86_64 -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.3.stderr
19+
// RUN: FileCheck -check-prefix=ERROR < %t.3.stderr %s
20+
21+
// Cros target without sysroot causes no warning.
22+
// RUN: %clang -Wpoison-system-directories -Werror -target x86_64 -I/usr/include -c -o - %s
23+
24+
// By default the warning is off.
25+
// RUN: %clang -Werror -target x86_64 -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s
26+
27+
// WARN: warning: include location {{[^ ]+}} is unsafe for cross-compilation [-Wpoison-system-directories]
28+
29+
// ERROR: error: include location {{[^ ]+}} is unsafe for cross-compilation [-Werror,-Wpoison-system-directories]

0 commit comments

Comments
 (0)