Make ASTContext explicitly keep track of the declaration for the C
FILE type, rather than using name lookup to find FILE within the
translation unit. Within precompiled headers, FILE is treated as yet
another "special type" (like __builtin_va_list).

This change should provide a performance improvement (not verified),
since the lookup into the translation unit declaration 
forces the (otherwise unneeded) construction of a large hash table.
More importantly, with precompiled headers, the construction
of that table requires deserializing most of the top-level
declarations from the precompiled header, which are then unused.

Fixes PR 4509.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74911 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 08607d5..c59a5d7 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -36,7 +36,8 @@
                        Builtin::Context &builtins,
                        bool FreeMem, unsigned size_reserve) : 
   GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), 
-  ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), 
+  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0),
+  SourceMgr(SM), LangOpts(LOpts), 
   LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), 
   Idents(idents), Selectors(sels),
   BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {  
@@ -3844,16 +3845,12 @@
     break;
   }
   case 'P': {
-    IdentifierInfo *II = &Context.Idents.get("FILE");
-    DeclContext::lookup_result Lookup 
-      = Context.getTranslationUnitDecl()->lookup(II);
-    if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) {
-      Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first));
-      break;
-    }
-    else {
+    Type = Context.getFILEType();
+    if (Type.isNull()) {
       Error = ASTContext::GE_Missing_FILE;
       return QualType();
+    } else {
+      break;
     }
   }
   }
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index bc0e720..47a7e62 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -1536,6 +1536,17 @@
   if (unsigned FastEnum 
         = SpecialTypes[pch::SPECIAL_TYPE_OBJC_FAST_ENUMERATION_STATE])
     Context->setObjCFastEnumerationStateType(GetType(FastEnum));
+  if (unsigned File = SpecialTypes[pch::SPECIAL_TYPE_FILE]) {
+    QualType FileType = GetType(File);
+    assert(!FileType.isNull() && "FILE type is NULL");
+    if (const TypedefType *Typedef = FileType->getAsTypedefType())
+      Context->setFILEDecl(Typedef->getDecl());
+    else {
+      const TagType *Tag = FileType->getAsTagType();
+      assert(Tag && "Invalid FILE type in PCH file");
+      Context->setFILEDecl(Tag->getDecl());
+    }
+  }
 }
 
 /// \brief Retrieve the name of the original source file name
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index 566df35..0362e8a 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -1892,6 +1892,7 @@
   AddTypeRef(Context.getObjCClassType(), Record);
   AddTypeRef(Context.getRawCFConstantStringType(), Record);
   AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
+  AddTypeRef(Context.getFILEType(), Record);
   Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
 
   // Write the record containing external, unnamed definitions.
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index f148e8d..a1ab68a 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -1691,6 +1691,14 @@
       }
     }
   }
+
+  // If this is the C FILE type, notify the AST context.
+  if (IdentifierInfo *II = NewTD->getIdentifier())
+    if (!NewTD->isInvalidDecl() &&
+        NewTD->getDeclContext()->getLookupContext()->isTranslationUnit() &&        
+        II->isStr("FILE"))
+      Context.setFILEDecl(NewTD);
+  
   return NewTD;
 }
 
@@ -3768,6 +3776,13 @@
     CurContext->addDecl(New);
   }
 
+  // If this is the C FILE type, notify the AST context.
+  if (IdentifierInfo *II = New->getIdentifier())
+    if (!New->isInvalidDecl() &&
+        New->getDeclContext()->getLookupContext()->isTranslationUnit() &&        
+        II->isStr("FILE"))
+      Context.setFILEDecl(New);
+  
   OwnedDecl = true;
   return DeclPtrTy::make(New);
 }