denik updated this revision to Diff 215271. denik added a comment. Fixed clang-format.
CHANGES SINCE LAST ACTION https://reviews.llvm.org/D52524/new/ https://reviews.llvm.org/D52524 Files: clang/include/clang/Basic/DiagnosticCommonKinds.td clang/include/clang/Basic/DiagnosticGroups.td clang/lib/Frontend/InitHeaderSearch.cpp clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/lib/.keep clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/include/c++/.keep clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/lib/gcc/.keep clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/include/.keep clang/test/Frontend/Inputs/sysroot_x86_64_cross_linux_tree/usr/local/lib/.keep clang/test/Frontend/warning-poison-system-directory.c Index: clang/test/Frontend/warning-poison-system-directory.c =================================================================== --- /dev/null +++ clang/test/Frontend/warning-poison-system-directory.c @@ -0,0 +1,18 @@ +// System directory and sysroot option causes warning. +// RUN: %clang -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 -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 -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 -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 -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 + +// Cros target without sysroot causes no warning. +// RUN: %clang -Werror -target x86_64 -I/usr/include -c -o - %s + +// WARN: warning: include location {{[^ ]+}} is unsafe for cross-compilation [-Wpoison-system-directories] Index: clang/lib/Frontend/InitHeaderSearch.cpp =================================================================== --- clang/lib/Frontend/InitHeaderSearch.cpp +++ clang/lib/Frontend/InitHeaderSearch.cpp @@ -137,6 +137,18 @@ SmallString<256> MappedPathStorage; StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); + // If use system headers/libraries while cross-compiling, + // emit the warning. + if (HasSysroot) { + if (MappedPathStr.startswith("/usr/include") || + MappedPathStr.startswith("/usr/local/include") || + MappedPathStr.startswith("/lib") || + MappedPathStr.startswith("/usr/local/lib")) { + Headers.getDiags().Report(diag::warn_poison_system_directories) + << MappedPathStr.str(); + } + } + // Compute the DirectoryLookup type. SrcMgr::CharacteristicKind Type; if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) { Index: clang/include/clang/Basic/DiagnosticGroups.td =================================================================== --- clang/include/clang/Basic/DiagnosticGroups.td +++ clang/include/clang/Basic/DiagnosticGroups.td @@ -1066,6 +1066,10 @@ // multiversioning. def FunctionMultiVersioning : DiagGroup<"function-multiversion">; +// A warning group for warnings about including system headers when +// cross-compiling. +def PoisonSystemDirectories : DiagGroup<"poison-system-directories">; + def NoDeref : DiagGroup<"noderef">; // A group for cross translation unit static analysis related warnings. Index: clang/include/clang/Basic/DiagnosticCommonKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticCommonKinds.td +++ clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -304,4 +304,7 @@ "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<PoisonSystemDirectories>; }
Index: clang/test/Frontend/warning-poison-system-directory.c =================================================================== --- /dev/null +++ clang/test/Frontend/warning-poison-system-directory.c @@ -0,0 +1,18 @@ +// System directory and sysroot option causes warning. +// RUN: %clang -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 -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 -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 -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 -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 + +// Cros target without sysroot causes no warning. +// RUN: %clang -Werror -target x86_64 -I/usr/include -c -o - %s + +// WARN: warning: include location {{[^ ]+}} is unsafe for cross-compilation [-Wpoison-system-directories] Index: clang/lib/Frontend/InitHeaderSearch.cpp =================================================================== --- clang/lib/Frontend/InitHeaderSearch.cpp +++ clang/lib/Frontend/InitHeaderSearch.cpp @@ -137,6 +137,18 @@ SmallString<256> MappedPathStorage; StringRef MappedPathStr = Path.toStringRef(MappedPathStorage); + // If use system headers/libraries while cross-compiling, + // emit the warning. + if (HasSysroot) { + if (MappedPathStr.startswith("/usr/include") || + MappedPathStr.startswith("/usr/local/include") || + MappedPathStr.startswith("/lib") || + MappedPathStr.startswith("/usr/local/lib")) { + Headers.getDiags().Report(diag::warn_poison_system_directories) + << MappedPathStr.str(); + } + } + // Compute the DirectoryLookup type. SrcMgr::CharacteristicKind Type; if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) { Index: clang/include/clang/Basic/DiagnosticGroups.td =================================================================== --- clang/include/clang/Basic/DiagnosticGroups.td +++ clang/include/clang/Basic/DiagnosticGroups.td @@ -1066,6 +1066,10 @@ // multiversioning. def FunctionMultiVersioning : DiagGroup<"function-multiversion">; +// A warning group for warnings about including system headers when +// cross-compiling. +def PoisonSystemDirectories : DiagGroup<"poison-system-directories">; + def NoDeref : DiagGroup<"noderef">; // A group for cross translation unit static analysis related warnings. Index: clang/include/clang/Basic/DiagnosticCommonKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticCommonKinds.td +++ clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -304,4 +304,7 @@ "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<PoisonSystemDirectories>; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits