[cfe-commits] r158418 - in /cfe/trunk: docs/ include/clang/Driver/ include/clang/Frontend/ include/clang/Lex/ lib/Frontend/ lib/Lex/ test/Frontend/ test/Frontend/Inputs/SystemHeaderPrefix/ test/Frontend/Inputs/SystemHeaderPrefix/libs/ test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/ test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/ test/Frontend/Inputs/SystemHeaderPrefix/src/

Richard Smith richard-llvm at metafoo.co.uk
Wed Jun 13 13:27:03 PDT 2012


Author: rsmith
Date: Wed Jun 13 15:27:03 2012
New Revision: 158418

URL: http://llvm.org/viewvc/llvm-project?rev=158418&view=rev
Log:
Add -isystem-prefix and -ino-system-prefix arguments, which can be used to
override whether headers are system headers by checking for prefixes of the
header name specified in the #include directive.

This allows warnings to be disabled for third-party code which is found in
specific subdirectories of include paths.

Added:
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/all.h
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/warn.h
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/all.h
    cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/warn.h
    cfe/trunk/test/Frontend/system-header-prefix.c
Modified:
    cfe/trunk/docs/UsersManual.html
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/include/clang/Frontend/HeaderSearchOptions.h
    cfe/trunk/include/clang/Lex/HeaderSearch.h
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/lib/Frontend/InitHeaderSearch.cpp
    cfe/trunk/lib/Lex/HeaderSearch.cpp

Modified: cfe/trunk/docs/UsersManual.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/UsersManual.html?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/docs/UsersManual.html (original)
+++ cfe/trunk/docs/UsersManual.html Wed Jun 13 15:27:03 2012
@@ -43,6 +43,7 @@
    <li><a href="#diagnostics_categories">Diagnostic Categories</a></li>
    <li><a href="#diagnostics_commandline">Controlling Diagnostics via Command Line Flags</a></li>
    <li><a href="#diagnostics_pragmas">Controlling Diagnostics via Pragmas</a></li>
+   <li><a href="#diagnostics_systemheader">Controlling Diagnostics in System Headers</a></li>
    <li><a href="#diagnostics_enable_everything">Enabling All Warnings</a></li>
    <li><a href="#analyzer_diagnositics">Controlling Static Analyzer Diagnostics</a></li>
    </ul>
@@ -654,6 +655,45 @@
 compatible #pragmas there is no guarantee that they will have identical behaviour
 on both compilers. </p>
 
+<h4 id="diagnostics_systemheader">Controlling Diagnostics in System Headers</h4>
+
+<p>Warnings are suppressed when they occur in system headers. By default, an
+included file is treated as a system header if it is found in an include path
+specified by <tt>-isystem</tt>, but this can be overridden in several ways.</p>
+
+<p>The <tt>system_header</tt> pragma can be used to mark the current file as
+being a system header. No warnings will be produced from the location of the
+pragma onwards within the same file.</p>
+
+<pre>
+char a = 'xy'; // warning
+
+#pragma clang system_header
+
+char b = 'ab'; // no warning
+</pre>
+
+<p>The <tt>-isystem-prefix</tt> and <tt>-ino-system-prefix</tt> command-line
+arguments can be used to override whether subsets of an include path are treated
+as system headers. When the name in a <tt>#include</tt> directive is found
+within a header search path and starts with a system prefix, the header is
+treated as a system header. The last prefix on the command-line which matches
+the specified header name takes precedence. For instance:</p>
+
+<pre>
+clang -Ifoo -isystem bar -isystem-prefix x/ -ino-system-prefix x/y/
+</pre>
+
+<p>Here, <tt>#include "x/a.h"</tt> is treated as including a system header, even
+if the header is found in <tt>foo</tt>, and <tt>#include "x/y/b.h"</tt> is
+treated as not including a system header, even if the header is found in
+<tt>bar</tt>.
+</p>
+
+<p>A <tt>#include</tt> directive which finds a file relative to the current
+directory is treated as including a system header if the including file is
+treated as a system header.</p>
+
 <h4 id="diagnostics_enable_everything">Enabling All Warnings</h4>
 
 <p>In addition to the traditional <tt>-W</tt> flags, one can enable <b>all</b>

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Wed Jun 13 15:27:03 2012
@@ -471,6 +471,14 @@
            "implicit extern \"C\" semantics; these are assumed to not be "
            "user-provided and are used to model system and standard headers' "
            "paths.">;
+def isystem_prefix : JoinedOrSeparate<"-isystem-prefix">,
+  MetaVarName<"<prefix>">,
+  HelpText<"Treat all #include paths starting with <prefix> as including a "
+           "system header.">;
+def ino_system_prefix : JoinedOrSeparate<"-ino-system-prefix">,
+  MetaVarName<"<prefix>">,
+  HelpText<"Treat all #include paths starting with <prefix> as not including a "
+           "system header.">;
 
 //===----------------------------------------------------------------------===//
 // Preprocessor Options

Modified: cfe/trunk/include/clang/Frontend/HeaderSearchOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/HeaderSearchOptions.h?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/HeaderSearchOptions.h (original)
+++ cfe/trunk/include/clang/Frontend/HeaderSearchOptions.h Wed Jun 13 15:27:03 2012
@@ -69,6 +69,18 @@
         IsInternal(isInternal), ImplicitExternC(implicitExternC) {}
   };
 
+  struct SystemHeaderPrefix {
+    /// A prefix to be matched against paths in #include directives.
+    std::string Prefix;
+
+    /// True if paths beginning with this prefix should be treated as system
+    /// headers.
+    bool IsSystemHeader;
+
+    SystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader)
+      : Prefix(Prefix), IsSystemHeader(IsSystemHeader) {}
+  };
+
   /// If non-empty, the directory to use as a "virtual system root" for include
   /// paths.
   std::string Sysroot;
@@ -76,6 +88,9 @@
   /// User specified include entries.
   std::vector<Entry> UserEntries;
 
+  /// User-specified system header prefixes.
+  std::vector<SystemHeaderPrefix> SystemHeaderPrefixes;
+
   /// The directory which holds the compiler resource files (builtin includes,
   /// etc.).
   std::string ResourceDir;
@@ -117,6 +132,13 @@
     UserEntries.push_back(Entry(Path, Group, IsUserSupplied, IsFramework,
                                 IgnoreSysRoot, IsInternal, ImplicitExternC));
   }
+
+  /// AddSystemHeaderPrefix - Override whether #include directives naming a
+  /// path starting with \arg Prefix should be considered as naming a system
+  /// header.
+  void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
+    SystemHeaderPrefixes.push_back(SystemHeaderPrefix(Prefix, IsSystemHeader));
+  }
 };
 
 } // end namespace clang

Modified: cfe/trunk/include/clang/Lex/HeaderSearch.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/HeaderSearch.h?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/HeaderSearch.h (original)
+++ cfe/trunk/include/clang/Lex/HeaderSearch.h Wed Jun 13 15:27:03 2012
@@ -16,6 +16,7 @@
 
 #include "clang/Lex/DirectoryLookup.h"
 #include "clang/Lex/ModuleMap.h"
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Allocator.h"
@@ -144,6 +145,12 @@
   unsigned SystemDirIdx;
   bool NoCurDirSearch;
 
+  /// #include prefixes for which the 'system header' property is overridden.
+  /// For a #include "x" or #include <x> directive, the last string in this
+  /// list which is a prefix of 'x' determines whether the file is treated as
+  /// a system header.
+  std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
+
   /// \brief The path to the module cache.
   std::string ModuleCachePath;
   
@@ -235,6 +242,11 @@
     SystemDirIdx++;
   }
 
+  /// SetSystemHeaderPrefixes - Set the list of system header prefixes.
+  void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) {
+    SystemHeaderPrefixes.assign(P.begin(), P.end());
+  }
+
   /// HasIncludeAliasMap - Checks whether the map exists or not
   bool HasIncludeAliasMap() const {
     return IncludeAliases;

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Wed Jun 13 15:27:03 2012
@@ -624,6 +624,16 @@
     Res.push_back(E.Path);
   }
 
+  /// User-specified system header prefixes.
+  for (unsigned i = 0, e = Opts.SystemHeaderPrefixes.size(); i != e; ++i) {
+    if (Opts.SystemHeaderPrefixes[i].IsSystemHeader)
+      Res.push_back("-isystem-prefix");
+    else
+      Res.push_back("-ino-system-prefix");
+
+    Res.push_back(Opts.SystemHeaderPrefixes[i].Prefix);
+  }
+
   if (!Opts.ResourceDir.empty())
     Res.push_back("-resource-dir", Opts.ResourceDir);
   if (!Opts.ModuleCachePath.empty())
@@ -1688,6 +1698,14 @@
     Opts.AddPath((*I)->getValue(Args), frontend::System,
                  false, false, /*IgnoreSysRoot=*/true, /*IsInternal=*/true,
                  (*I)->getOption().matches(OPT_internal_externc_isystem));
+
+  // Add the path prefixes which are implicitly treated as being system headers.
+  for (arg_iterator I = Args.filtered_begin(OPT_isystem_prefix,
+                                            OPT_ino_system_prefix),
+                    E = Args.filtered_end();
+       I != E; ++I)
+    Opts.AddSystemHeaderPrefix((*I)->getValue(Args),
+                               (*I)->getOption().matches(OPT_isystem_prefix));
 }
 
 void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,

Modified: cfe/trunk/lib/Frontend/InitHeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitHeaderSearch.cpp?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/InitHeaderSearch.cpp (original)
+++ cfe/trunk/lib/Frontend/InitHeaderSearch.cpp Wed Jun 13 15:27:03 2012
@@ -40,6 +40,7 @@
   std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
   typedef std::vector<std::pair<IncludeDirGroup,
                       DirectoryLookup> >::const_iterator path_iterator;
+  std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
   HeaderSearch &Headers;
   bool Verbose;
   std::string IncludeSysroot;
@@ -57,6 +58,12 @@
                bool isCXXAware, bool isUserSupplied,
                bool isFramework, bool IgnoreSysRoot = false);
 
+  /// AddSystemHeaderPrefix - Add the specified prefix to the system header
+  /// prefix list.
+  void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
+    SystemHeaderPrefixes.push_back(std::make_pair(Prefix, IsSystemHeader));
+  }
+
   /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
   ///  libstdc++.
   void AddGnuCPlusPlusIncludePaths(StringRef Base,
@@ -623,6 +630,8 @@
   bool DontSearchCurDir = false;  // TODO: set to true if -I- is set?
   Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
 
+  Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
+
   // If verbose, print the list of directories that will be searched.
   if (Verbose) {
     llvm::errs() << "#include \"...\" search starts here:\n";
@@ -660,6 +669,10 @@
 
   Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
 
+  for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
+    Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
+                               HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
+
   if (HSOpts.UseBuiltinIncludes) {
     // Set up the builtin include directory in the module map.
     llvm::sys::Path P(HSOpts.ResourceDir);

Modified: cfe/trunk/lib/Lex/HeaderSearch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/HeaderSearch.cpp?rev=158418&r1=158417&r2=158418&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/HeaderSearch.cpp (original)
+++ cfe/trunk/lib/Lex/HeaderSearch.cpp Wed Jun 13 15:27:03 2012
@@ -510,6 +510,16 @@
     if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
       HFI.DirInfo = SrcMgr::C_System;
 
+    // If the filename matches a known system header prefix, override
+    // whether the file is a system header.
+    for (unsigned i = SystemHeaderPrefixes.size(); i; --i) {
+      if (Filename.startswith(SystemHeaderPrefixes[i-1].first)) {
+        HFI.DirInfo = SystemHeaderPrefixes[i-1].second ? SrcMgr::C_System
+                                                       : SrcMgr::C_User;
+        break;
+      }
+    }
+
     // If this file is found in a header map and uses the framework style of
     // includes, then this header is part of a framework we're building.
     if (CurDir->isIndexHeaderMap()) {

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/all.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1 @@
+#include "warn.h"

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/boost/warn.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1,2 @@
+#if BOOST
+#endif

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/all.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/all.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/all.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/all.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1 @@
+#include "warn.h"

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/warn.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/warn.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/warn.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/libs/mylib/warn.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1,2 @@
+#if MYLIB
+#endif

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/all.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/all.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/all.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/all.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1,6 @@
+#include "libs/boost/all.h"
+#include "libs/mylib/all.h"
+
+#include "libs/boost/warn.h"
+#include "libs/mylib/warn.h"
+#include "src/warn.h"

Added: cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/warn.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/warn.h?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/warn.h (added)
+++ cfe/trunk/test/Frontend/Inputs/SystemHeaderPrefix/src/warn.h Wed Jun 13 15:27:03 2012
@@ -0,0 +1,2 @@
+#if SRC
+#endif

Added: cfe/trunk/test/Frontend/system-header-prefix.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Frontend/system-header-prefix.c?rev=158418&view=auto
==============================================================================
--- cfe/trunk/test/Frontend/system-header-prefix.c (added)
+++ cfe/trunk/test/Frontend/system-header-prefix.c Wed Jun 13 15:27:03 2012
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -isystem-prefix libs/ -ino-system-prefix libs/mylib/ -I%S/Inputs/SystemHeaderPrefix -Wundef -E %s 2>&1 | FileCheck %s
+
+#include "src/all.h"
+
+// CHECK-NOT: BOOST
+// CHECK: libs/mylib/warn.h:1:5: warning: 'MYLIB' is not defined, evaluates to 0
+// CHECK-NOT: BOOST
+// CHECK: libs/mylib/warn.h:1:5: warning: 'MYLIB' is not defined, evaluates to 0
+// CHECK-NOT: BOOST
+// CHECK: src/warn.h:1:5: warning: 'SRC' is not defined, evaluates to 0
+// CHECK-NOT: BOOST





More information about the cfe-commits mailing list