yunlian created this revision.
Herald added a subscriber: cfe-commits.

When using clang as a cross-compiler, we should not use system headers or 
libraries to do the compilation. This CL creates a new warning flag
-Wpoison-system-directories support to emit warnings if --sysroot is set and 
headers or libraries from common host system location are used.


Repository:
  rC Clang

https://reviews.llvm.org/D52524

Files:
  include/clang/Basic/DiagnosticCommonKinds.td
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Frontend/Utils.h
  lib/Frontend/CompilerInstance.cpp
  lib/Frontend/InitHeaderSearch.cpp


Index: lib/Frontend/InitHeaderSearch.cpp
===================================================================
--- lib/Frontend/InitHeaderSearch.cpp
+++ lib/Frontend/InitHeaderSearch.cpp
@@ -45,12 +45,13 @@
   HeaderSearch &Headers;
   bool Verbose;
   std::string IncludeSysroot;
+  DiagnosticsEngine &Diag;
   bool HasSysroot;
 
 public:
 
-  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
-    : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
+  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot, 
DiagnosticsEngine &Diag)
+    : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot), Diag(Diag),
       HasSysroot(!(sysroot.empty() || sysroot == "/")) {
   }
 
@@ -138,6 +139,17 @@
   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") {
+         Diag.Report(diag::warn_poison_system_directories) << 
MappedPathStr.str();
+       }
+  }
+
   // Compute the DirectoryLookup type.
   SrcMgr::CharacteristicKind Type;
   if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
@@ -663,8 +675,9 @@
 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
                                      const HeaderSearchOptions &HSOpts,
                                      const LangOptions &Lang,
+                                     DiagnosticsEngine &Diag,
                                      const llvm::Triple &Triple) {
-  InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
+  InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot, Diag);
 
   // Add the user defined entries.
   for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
Index: lib/Frontend/CompilerInstance.cpp
===================================================================
--- lib/Frontend/CompilerInstance.cpp
+++ lib/Frontend/CompilerInstance.cpp
@@ -416,7 +416,7 @@
     HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
 
   ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
-                           PP->getLangOpts(), *HeaderSearchTriple);
+                           PP->getLangOpts(), PP->getDiagnostics(), 
*HeaderSearchTriple);
 
   PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
 
Index: include/clang/Frontend/Utils.h
===================================================================
--- include/clang/Frontend/Utils.h
+++ include/clang/Frontend/Utils.h
@@ -63,6 +63,7 @@
 void ApplyHeaderSearchOptions(HeaderSearch &HS,
                               const HeaderSearchOptions &HSOpts,
                               const LangOptions &Lang,
+                              DiagnosticsEngine &Diag,
                               const llvm::Triple &triple);
 
 /// InitializePreprocessor - Initialize the preprocessor getting it and the
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -1031,3 +1031,7 @@
 // A warning group specifically for warnings related to function
 // multiversioning.
 def FunctionMultiVersioning : DiagGroup<"function-multiversion">;
+
+// A warning group for warnings about including system headers when
+// cross-compiling.
+def PoisonSystemDirectories : DiagGroup<"poison-system-directories">;
Index: include/clang/Basic/DiagnosticCommonKinds.td
===================================================================
--- include/clang/Basic/DiagnosticCommonKinds.td
+++ include/clang/Basic/DiagnosticCommonKinds.td
@@ -248,4 +248,7 @@
 // OpenMP
 def err_omp_more_one_clause : Error<
   "directive '#pragma omp %0' cannot contain more than one '%1' 
clause%select{| with '%3' name modifier| with 'source' dependence}2">;
+
+// Poison system directories.
+def warn_poison_system_directories : Warning <"include location '%0' is unsafe 
for cross-compilation">, InGroup<PoisonSystemDirectories>;
 }


Index: lib/Frontend/InitHeaderSearch.cpp
===================================================================
--- lib/Frontend/InitHeaderSearch.cpp
+++ lib/Frontend/InitHeaderSearch.cpp
@@ -45,12 +45,13 @@
   HeaderSearch &Headers;
   bool Verbose;
   std::string IncludeSysroot;
+  DiagnosticsEngine &Diag;
   bool HasSysroot;
 
 public:
 
-  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
-    : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
+  InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot, DiagnosticsEngine &Diag)
+    : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot), Diag(Diag),
       HasSysroot(!(sysroot.empty() || sysroot == "/")) {
   }
 
@@ -138,6 +139,17 @@
   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") {
+         Diag.Report(diag::warn_poison_system_directories) << MappedPathStr.str();
+       }
+  }
+
   // Compute the DirectoryLookup type.
   SrcMgr::CharacteristicKind Type;
   if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
@@ -663,8 +675,9 @@
 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
                                      const HeaderSearchOptions &HSOpts,
                                      const LangOptions &Lang,
+                                     DiagnosticsEngine &Diag,
                                      const llvm::Triple &Triple) {
-  InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
+  InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot, Diag);
 
   // Add the user defined entries.
   for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
Index: lib/Frontend/CompilerInstance.cpp
===================================================================
--- lib/Frontend/CompilerInstance.cpp
+++ lib/Frontend/CompilerInstance.cpp
@@ -416,7 +416,7 @@
     HeaderSearchTriple = &PP->getAuxTargetInfo()->getTriple();
 
   ApplyHeaderSearchOptions(PP->getHeaderSearchInfo(), getHeaderSearchOpts(),
-                           PP->getLangOpts(), *HeaderSearchTriple);
+                           PP->getLangOpts(), PP->getDiagnostics(), *HeaderSearchTriple);
 
   PP->setPreprocessedOutput(getPreprocessorOutputOpts().ShowCPP);
 
Index: include/clang/Frontend/Utils.h
===================================================================
--- include/clang/Frontend/Utils.h
+++ include/clang/Frontend/Utils.h
@@ -63,6 +63,7 @@
 void ApplyHeaderSearchOptions(HeaderSearch &HS,
                               const HeaderSearchOptions &HSOpts,
                               const LangOptions &Lang,
+                              DiagnosticsEngine &Diag,
                               const llvm::Triple &triple);
 
 /// InitializePreprocessor - Initialize the preprocessor getting it and the
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -1031,3 +1031,7 @@
 // A warning group specifically for warnings related to function
 // multiversioning.
 def FunctionMultiVersioning : DiagGroup<"function-multiversion">;
+
+// A warning group for warnings about including system headers when
+// cross-compiling.
+def PoisonSystemDirectories : DiagGroup<"poison-system-directories">;
Index: include/clang/Basic/DiagnosticCommonKinds.td
===================================================================
--- include/clang/Basic/DiagnosticCommonKinds.td
+++ include/clang/Basic/DiagnosticCommonKinds.td
@@ -248,4 +248,7 @@
 // OpenMP
 def err_omp_more_one_clause : Error<
   "directive '#pragma omp %0' cannot contain more than one '%1' clause%select{| with '%3' name modifier| with 'source' dependence}2">;
+
+// 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
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to