Introduce module attributes into the module map grammar, along with a
single attribute ("system") that allows us to mark a module as being a
"system" module. Each of the headers that makes up a system module is
considered to be a system header, so that we (for example) suppress
warnings there.

If a module is being inferred for a framework, and that framework
directory is within a system frameworks directory, infer it as a
system framework.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149143 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Basic/Module.cpp b/lib/Basic/Module.cpp
index ff09441..015f421 100644
--- a/lib/Basic/Module.cpp
+++ b/lib/Basic/Module.cpp
@@ -24,9 +24,9 @@
                bool IsFramework, bool IsExplicit)
   : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 
     Umbrella(), IsAvailable(true), IsFromModuleFile(false), 
-    IsFramework(IsFramework), IsExplicit(IsExplicit), InferSubmodules(false), 
-    InferExplicitSubmodules(false), InferExportWildcard(false),
-    NameVisibility(Hidden) 
+    IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
+    InferSubmodules(false), InferExplicitSubmodules(false), 
+    InferExportWildcard(false), NameVisibility(Hidden) 
 { 
   if (Parent) {
     if (!Parent->isAvailable())
@@ -172,8 +172,15 @@
     OS << "framework ";
   if (IsExplicit)
     OS << "explicit ";
-  OS << "module " << Name << " {\n";
+  OS << "module " << Name;
 
+  if (IsSystem) {
+    OS.indent(Indent + 2);
+    OS << " [system]";
+  }
+
+  OS << " {\n";
+  
   if (!Requires.empty()) {
     OS.indent(Indent + 2);
     OS << "requires ";
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index c3f3e37..b8075ed 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -577,12 +577,15 @@
 
 // Initialization Utilities
 
-bool CompilerInstance::InitializeSourceManager(StringRef InputFile) {
-  return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(),
-                                 getSourceManager(), getFrontendOpts());
+bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
+                                               SrcMgr::CharacteristicKind Kind){
+  return InitializeSourceManager(InputFile, Kind, getDiagnostics(), 
+                                 getFileManager(), getSourceManager(), 
+                                 getFrontendOpts());
 }
 
 bool CompilerInstance::InitializeSourceManager(StringRef InputFile,
+                                               SrcMgr::CharacteristicKind Kind,
                                                DiagnosticsEngine &Diags,
                                                FileManager &FileMgr,
                                                SourceManager &SourceMgr,
@@ -594,7 +597,7 @@
       Diags.Report(diag::err_fe_error_reading) << InputFile;
       return false;
     }
-    SourceMgr.createMainFileID(File);
+    SourceMgr.createMainFileID(File, Kind);
   } else {
     llvm::OwningPtr<llvm::MemoryBuffer> SB;
     if (llvm::MemoryBuffer::getSTDIN(SB)) {
@@ -604,7 +607,7 @@
     }
     const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(),
                                                    SB->getBufferSize(), 0);
-    SourceMgr.createMainFileID(File);
+    SourceMgr.createMainFileID(File, Kind);
     SourceMgr.overrideFileContents(File, SB.take());
   }
 
diff --git a/lib/Frontend/FrontendAction.cpp b/lib/Frontend/FrontendAction.cpp
index 5a15847..11ac572 100644
--- a/lib/Frontend/FrontendAction.cpp
+++ b/lib/Frontend/FrontendAction.cpp
@@ -320,7 +320,10 @@
   // Initialize the main file entry. This needs to be delayed until after PCH
   // has loaded.
   if (!isCurrentFileAST()) {
-    if (!CI.InitializeSourceManager(getCurrentFile()))
+    if (!CI.InitializeSourceManager(getCurrentFile(),
+                                    getCurrentInput().IsSystem
+                                      ? SrcMgr::C_System
+                                      : SrcMgr::C_User))
       return;
   }
 
diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp
index 272474c..e99e47e 100644
--- a/lib/Frontend/FrontendActions.cpp
+++ b/lib/Frontend/FrontendActions.cpp
@@ -256,7 +256,8 @@
     // Simple case: we have an umbrella header and there are no additional
     // includes, we can just parse the umbrella header directly.
     setCurrentInput(FrontendInputFile(UmbrellaHeader->getName(),
-                                      getCurrentFileKind()));
+                                      getCurrentFileKind(),
+                                      Module->IsSystem));
     return true;
   }
   
@@ -313,7 +314,8 @@
   llvm::MemoryBuffer *HeaderContentsBuf
     = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents);
   CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf);  
-  setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind()));
+  setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind(),
+                                    Module->IsSystem));
   return true;
 }
 
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp
index 7af7c93..ff63bdb 100644
--- a/lib/Lex/HeaderSearch.cpp
+++ b/lib/Lex/HeaderSearch.cpp
@@ -134,7 +134,9 @@
         llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
         if (const DirectoryEntry *FrameworkDir 
               = FileMgr.getDirectory(FrameworkDirName)) {
-          Module = getFrameworkModule(ModuleName, FrameworkDir);
+          bool IsSystem
+            = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
+          Module = getFrameworkModule(ModuleName, FrameworkDir, IsSystem);
           if (Module)
             break;
         }
@@ -319,8 +321,10 @@
   Module *Module = 0;
   if (SuggestedModule) {
     if (const DirectoryEntry *FrameworkDir
-                                    = FileMgr.getDirectory(FrameworkName))
-      Module = HS.getFrameworkModule(ModuleName, FrameworkDir);
+                                        = FileMgr.getDirectory(FrameworkName)) {
+      bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
+      Module = HS.getFrameworkModule(ModuleName, FrameworkDir, IsSystem);
+    }
   }
   
   // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
@@ -858,7 +862,8 @@
 }
   
 Module *HeaderSearch::getFrameworkModule(StringRef Name, 
-                                         const DirectoryEntry *Dir) {
+                                         const DirectoryEntry *Dir,
+                                         bool IsSystem) {
   if (Module *Module = ModMap.findModule(Name))
     return Module;
   
@@ -907,7 +912,8 @@
   
   // Try to infer a module map from the top-level framework directory.
   Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(), 
-                                               TopFrameworkDir, 
+                                               TopFrameworkDir,
+                                               IsSystem,
                                                /*Parent=*/0);
   
   // Follow the submodule path to find the requested (sub)framework module
diff --git a/lib/Lex/ModuleMap.cpp b/lib/Lex/ModuleMap.cpp
index 08a1e23..708da94 100644
--- a/lib/Lex/ModuleMap.cpp
+++ b/lib/Lex/ModuleMap.cpp
@@ -284,6 +284,7 @@
 Module *
 ModuleMap::inferFrameworkModule(StringRef ModuleName, 
                                 const DirectoryEntry *FrameworkDir,
+                                bool IsSystem,
                                 Module *Parent) {
   // Check whether we've already found this module.
   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
@@ -305,6 +306,9 @@
   
   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
                               /*IsFramework=*/true, /*IsExplicit=*/false);
+  if (IsSystem)
+    Result->IsSystem = IsSystem;
+  
   if (!Parent)
     Modules[ModuleName] = Result;
   
@@ -338,7 +342,7 @@
           = FileMgr.getDirectory(Dir->path())) {
       // FIXME: Do we want to warn about subframeworks without umbrella headers?
       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
-                           Result);
+                           IsSystem, Result);
     }
   }
 
@@ -454,7 +458,9 @@
       Star,
       StringLiteral,
       LBrace,
-      RBrace
+      RBrace,
+      LSquare,
+      RSquare
     } Kind;
     
     unsigned Location;
@@ -579,6 +585,10 @@
     Tok.Kind = MMToken::LBrace;
     break;
 
+  case tok::l_square:
+    Tok.Kind = MMToken::LSquare;
+    break;
+      
   case tok::period:
     Tok.Kind = MMToken::Period;
     break;
@@ -587,6 +597,10 @@
     Tok.Kind = MMToken::RBrace;
     break;
       
+  case tok::r_square:
+    Tok.Kind = MMToken::RSquare;
+    break;
+      
   case tok::star:
     Tok.Kind = MMToken::Star;
     break;
@@ -625,27 +639,42 @@
 
 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
   unsigned braceDepth = 0;
+  unsigned squareDepth = 0;
   do {
     switch (Tok.Kind) {
     case MMToken::EndOfFile:
       return;
 
     case MMToken::LBrace:
-      if (Tok.is(K) && braceDepth == 0)
+      if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
         return;
         
       ++braceDepth;
       break;
-    
+
+    case MMToken::LSquare:
+      if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
+        return;
+      
+      ++squareDepth;
+      break;
+
     case MMToken::RBrace:
       if (braceDepth > 0)
         --braceDepth;
       else if (Tok.is(K))
         return;
       break;
-        
+
+    case MMToken::RSquare:
+      if (squareDepth > 0)
+        --squareDepth;
+      else if (Tok.is(K))
+        return;
+      break;
+
     default:
-      if (braceDepth == 0 && Tok.is(K))
+      if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
         return;
       break;
     }
@@ -681,10 +710,28 @@
   return false;
 }
 
+namespace {
+  /// \brief Enumerates the known attributes.
+  enum AttributeKind {
+    /// \brief An unknown attribute.
+    AT_unknown,
+    /// \brief The 'system' attribute.
+    AT_system
+  };
+}
+
 /// \brief Parse a module declaration.
 ///
 ///   module-declaration:
-///     'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* }
+///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 
+///       { module-member* }
+///
+///   attributes:
+///     attribute attributes
+///     attribute
+///
+///   attribute:
+///     [ identifier ]
 ///
 ///   module-member:
 ///     requires-declaration
@@ -777,6 +824,49 @@
   StringRef ModuleName = Id.back().first;
   SourceLocation ModuleNameLoc = Id.back().second;
   
+  // Parse the optional attribute list.
+  bool IsSystem = false;
+  while (Tok.is(MMToken::LSquare)) {
+    // Consume the '['.
+    SourceLocation LSquareLoc = consumeToken();
+    
+    // Check whether we have an attribute name here.
+    if (!Tok.is(MMToken::Identifier)) {
+      Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
+      skipUntil(MMToken::RSquare);
+      if (Tok.is(MMToken::RSquare))
+        consumeToken();
+      continue;
+    }
+    
+    // Decode the attribute name.
+    AttributeKind Attribute 
+      = llvm::StringSwitch<AttributeKind>(Tok.getString())
+        .Case("system", AT_system)
+        .Default(AT_unknown);
+    switch (Attribute) {
+    case AT_unknown:
+      Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
+        << Tok.getString();
+      break;
+        
+    case AT_system:
+      IsSystem = true;
+      break;
+    }
+    consumeToken();
+    
+    // Consume the ']'.
+    if (!Tok.is(MMToken::RSquare)) {
+      Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
+      Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
+      skipUntil(MMToken::RSquare);
+    }
+
+    if (Tok.is(MMToken::RSquare))
+      consumeToken();
+  }
+  
   // Parse the opening brace.
   if (!Tok.is(MMToken::LBrace)) {
     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
@@ -818,6 +908,8 @@
   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
                                         Explicit).first;
   ActiveModule->DefinitionLoc = ModuleNameLoc;
+  if (IsSystem)
+    ActiveModule->IsSystem = true;
   
   bool Done = false;
   do {
@@ -1251,8 +1343,10 @@
     case MMToken::HeaderKeyword:
     case MMToken::Identifier:
     case MMToken::LBrace:
+    case MMToken::LSquare:
     case MMToken::Period:
     case MMToken::RBrace:
+    case MMToken::RSquare:
     case MMToken::RequiresKeyword:
     case MMToken::Star:
     case MMToken::StringLiteral:
diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp
index 42a848d..7173402 100644
--- a/lib/Serialization/ASTReader.cpp
+++ b/lib/Serialization/ASTReader.cpp
@@ -3130,9 +3130,10 @@
       SubmoduleID Parent = getGlobalSubmoduleID(F, Record[1]);
       bool IsFramework = Record[2];
       bool IsExplicit = Record[3];
-      bool InferSubmodules = Record[4];
-      bool InferExplicitSubmodules = Record[5];
-      bool InferExportWildcard = Record[6];
+      bool IsSystem = Record[4];
+      bool InferSubmodules = Record[5];
+      bool InferExplicitSubmodules = Record[6];
+      bool InferExportWildcard = Record[7];
       
       Module *ParentModule = 0;
       if (Parent)
@@ -3151,6 +3152,7 @@
       }
       
       CurrentModule->IsFromModuleFile = true;
+      CurrentModule->IsSystem = IsSystem || CurrentModule->IsSystem;
       CurrentModule->InferSubmodules = InferSubmodules;
       CurrentModule->InferExplicitSubmodules = InferExplicitSubmodules;
       CurrentModule->InferExportWildcard = InferExportWildcard;
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp
index 838ec35..b715d6c 100644
--- a/lib/Serialization/ASTWriter.cpp
+++ b/lib/Serialization/ASTWriter.cpp
@@ -1897,8 +1897,9 @@
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem  
+  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
-  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
   Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
   unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
@@ -1949,6 +1950,7 @@
     }
     Record.push_back(Mod->IsFramework);
     Record.push_back(Mod->IsExplicit);
+    Record.push_back(Mod->IsSystem);
     Record.push_back(Mod->InferSubmodules);
     Record.push_back(Mod->InferExplicitSubmodules);
     Record.push_back(Mod->InferExportWildcard);