r371878 - Reland r371785: Add -Wpoison-system-directories warning
Manoj Gupta via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 13 11:00:51 PDT 2019
Author: manojgupta
Date: Fri Sep 13 11:00:51 2019
New Revision: 371878
URL: http://llvm.org/viewvc/llvm-project?rev=371878&view=rev
Log:
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)
Added:
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/.keep
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c++/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c++/.keep
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/.keep
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/.keep
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/
cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/.keep
cfe/trunk/test/Frontend/warning-poison-system-directories.c
Modified:
cfe/trunk/include/clang/Basic/DiagnosticCommonKinds.td
cfe/trunk/lib/Frontend/InitHeaderSearch.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticCommonKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticCommonKinds.td?rev=371878&r1=371877&r2=371878&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticCommonKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticCommonKinds.td Fri Sep 13 11:00:51 2019
@@ -315,4 +315,9 @@ def err_unknown_analyzer_checker_or_pack
"no analyzer checkers or packages are associated with '%0'">;
def note_suggest_disabling_all_checkers : Note<
"use -analyzer-disable-all-checks to disable all static analyzer checkers">;
+
+// Poison system directories.
+def warn_poison_system_directories : Warning <
+ "include location '%0' is unsafe for cross-compilation">,
+ InGroup<DiagGroup<"poison-system-directories">>, DefaultIgnore;
}
Modified: cfe/trunk/lib/Frontend/InitHeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitHeaderSearch.cpp?rev=371878&r1=371877&r2=371878&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/InitHeaderSearch.cpp (original)
+++ cfe/trunk/lib/Frontend/InitHeaderSearch.cpp Fri Sep 13 11:00:51 2019
@@ -137,6 +137,13 @@ bool InitHeaderSearch::AddUnmappedPath(c
SmallString<256> MappedPathStorage;
StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
+ // If use system headers while cross-compiling, emit the warning.
+ if (HasSysroot && (MappedPathStr.startswith("/usr/include") ||
+ MappedPathStr.startswith("/usr/local/include"))) {
+ Headers.getDiags().Report(diag::warn_poison_system_directories)
+ << MappedPathStr;
+ }
+
// Compute the DirectoryLookup type.
SrcMgr::CharacteristicKind Type;
if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
Added: cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/.keep
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/.keep?rev=371878&view=auto
==============================================================================
(empty)
Added: cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c++/.keep
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c%2B%2B/.keep?rev=371878&view=auto
==============================================================================
(empty)
Added: cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/.keep
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/.keep?rev=371878&view=auto
==============================================================================
(empty)
Added: cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/.keep
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/.keep?rev=371878&view=auto
==============================================================================
(empty)
Added: cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/.keep
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/.keep?rev=371878&view=auto
==============================================================================
(empty)
Added: cfe/trunk/test/Frontend/warning-poison-system-directories.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/warning-poison-system-directories.c?rev=371878&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/warning-poison-system-directories.c (added)
+++ cfe/trunk/test/Frontend/warning-poison-system-directories.c Fri Sep 13 11:00:51 2019
@@ -0,0 +1,29 @@
+// REQUIRES: x86-registered-target
+
+// System directory and sysroot option causes warning.
+// 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
+// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
+// 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
+// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
+// 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
+// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
+// 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
+// RUN: FileCheck -check-prefix=WARN < %t.1.stderr %s
+
+// Missing target but included sysroot still causes the warning.
+// RUN: %clang -Wpoison-system-directories -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s 2> %t.2.stderr
+// RUN: FileCheck -check-prefix=WARN < %t.2.stderr %s
+
+// With -Werror the warning causes the failure.
+// 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
+// RUN: FileCheck -check-prefix=ERROR < %t.3.stderr %s
+
+// Cros target without sysroot causes no warning.
+// RUN: %clang -Wpoison-system-directories -Werror -target x86_64 -I/usr/include -c -o - %s
+
+// By default the warning is off.
+// RUN: %clang -Werror -target x86_64 -I/usr/include --sysroot %S/Inputs/sysroot_x86_64_cross_linux_tree -c -o - %s
+
+// WARN: warning: include location {{[^ ]+}} is unsafe for cross-compilation [-Wpoison-system-directories]
+
+// ERROR: error: include location {{[^ ]+}} is unsafe for cross-compilation [-Werror,-Wpoison-system-directories]
More information about the cfe-commits
mailing list