[cfe-commits] r145683 - in /cfe/trunk: include/clang/Serialization/ASTReader.h include/clang/Serialization/ASTWriter.h lib/Serialization/ASTReader.cpp lib/Serialization/ASTWriter.cpp test/Modules/Inputs/submodules/hash_map.h test/Modules/Inputs/submodules/type_traits.h test/Modules/Inputs/submodules/vector.h test/Modules/submodules-preprocess.cpp

Douglas Gregor dgregor at apple.com
Fri Dec 2 07:45:11 PST 2011


Author: dgregor
Date: Fri Dec  2 09:45:10 2011
New Revision: 145683

URL: http://llvm.org/viewvc/llvm-project?rev=145683&view=rev
Log:
Implement name hiding for macro definitions within modules, such that
only the macro definitions from visible (sub)modules will actually be
visible. This provides the same behavior for macros that r145640
provided for declarations.

Modified:
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/include/clang/Serialization/ASTWriter.h
    cfe/trunk/lib/Serialization/ASTReader.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp
    cfe/trunk/test/Modules/Inputs/submodules/hash_map.h
    cfe/trunk/test/Modules/Inputs/submodules/type_traits.h
    cfe/trunk/test/Modules/Inputs/submodules/vector.h
    cfe/trunk/test/Modules/submodules-preprocess.cpp

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Fri Dec  2 09:45:10 2011
@@ -384,7 +384,8 @@
   GlobalSubmoduleMapType GlobalSubmoduleMap;
 
   /// \brief A set of hidden declarations.
-  typedef llvm::SmallVector<Decl *, 2> HiddenNames;
+  typedef llvm::SmallVector<llvm::PointerUnion<Decl *, IdentifierInfo *>, 2>
+    HiddenNames;
   
   typedef llvm::DenseMap<Module *, HiddenNames> HiddenNamesMapType;
 
@@ -1343,8 +1344,17 @@
 
   /// \brief Note that the identifier is a macro whose record will be loaded
   /// from the given AST file at the given (file-local) offset.
-  void SetIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
-                            uint64_t Offset);
+  ///
+  /// \param II The name of the macro.
+  ///
+  /// \param F The module file from which the macro definition was deserialized.
+  ///
+  /// \param Offset The offset into the module file at which the macro 
+  /// definition is located.
+  ///
+  /// \param Visible Whether the macro should be made visible.
+  void setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
+                            uint64_t Offset, bool Visible);
 
   /// \brief Read the set of macros defined by this external macro source.
   virtual void ReadDefinedMacros();

Modified: cfe/trunk/include/clang/Serialization/ASTWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTWriter.h?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTWriter.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTWriter.h Fri Dec  2 09:45:10 2011
@@ -388,11 +388,7 @@
   void WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot);
   void WritePreprocessorDetail(PreprocessingRecord &PPRec);
   void WriteSubmodules(Module *WritingModule);
-                    
-  /// \brief Infer the submodule ID that contains an entity at the given
-  /// source location.
-  serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
-                    
+                                        
   void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag);
   void WriteCXXBaseSpecifiersOffsets();
   void WriteType(QualType T);
@@ -607,6 +603,10 @@
     return DeclsToRewrite.count(D);
   }
 
+  /// \brief Infer the submodule ID that contains an entity at the given
+  /// source location.
+  serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
+
   /// \brief Note that the identifier II occurs at the given offset
   /// within the identifier table.
   void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);

Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Fri Dec  2 09:45:10 2011
@@ -562,8 +562,28 @@
   if (hasMacroDefinition) {
     // FIXME: Check for conflicts?
     uint32_t Offset = ReadUnalignedLE32(d);
-    Reader.SetIdentifierIsMacro(II, F, Offset);
-    DataLen -= 4;
+    unsigned LocalSubmoduleID = ReadUnalignedLE32(d);
+    
+    // Determine whether this macro definition should be visible now, or
+    // whether it is in a hidden submodule.
+    bool Visible = true;
+    if (SubmoduleID GlobalSubmoduleID
+          = Reader.getGlobalSubmoduleID(F, LocalSubmoduleID)) {
+      if (Module *Owner = Reader.getSubmodule(GlobalSubmoduleID)) {
+        if (Owner->NameVisibility == Module::Hidden) {
+          // The owning module is not visible, and this macro definition should
+          // not be, either.
+          Visible = false;
+          
+          // Note that this macro definition was hidden because its owning 
+          // module is not yet visible.
+          Reader.HiddenNamesMap[Owner].push_back(II);
+        }
+      } 
+    }
+    
+    Reader.setIdentifierIsMacro(II, F, Offset, Visible);
+    DataLen -= 8;
   }
 
   Reader.SetIdentifierInfo(ID, II);
@@ -1454,10 +1474,12 @@
   return HFI;
 }
 
-void ASTReader::SetIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
-                                     uint64_t LocalOffset) {
-  // Note that this identifier has a macro definition.
-  II->setHasMacroDefinition(true);
+void ASTReader::setIdentifierIsMacro(IdentifierInfo *II, ModuleFile &F,
+                                     uint64_t LocalOffset, bool Visible) {
+  if (Visible) {
+    // Note that this identifier has a macro definition.
+    II->setHasMacroDefinition(true);
+  }
   
   // Adjust the offset to a global offset.
   UnreadMacroRecordOffsets[II] = F.GlobalBitOffset + LocalOffset;
@@ -2440,8 +2462,12 @@
 }
 
 void ASTReader::makeNamesVisible(const HiddenNames &Names) {
-  for (unsigned I = 0, N = Names.size(); I != N; ++I)
-    Names[I]->ModulePrivate = false;    
+  for (unsigned I = 0, N = Names.size(); I != N; ++I) {
+    if (Decl *D = Names[I].dyn_cast<Decl *>())
+      D->ModulePrivate = false;
+    else
+      Names[I].get<IdentifierInfo *>()->setHasMacroDefinition(true);
+  }
 }
 
 void ASTReader::makeModuleVisible(Module *Mod, 

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Fri Dec  2 09:45:10 2011
@@ -2423,7 +2423,7 @@
     if (isInterestingIdentifier(II, Macro)) {
       DataLen += 2; // 2 bytes for builtin ID, flags
       if (hasMacroDefinition(II, Macro))
-        DataLen += 4;
+        DataLen += 8;
       
       for (IdentifierResolver::iterator D = IdResolver.begin(II),
                                      DEnd = IdResolver.end();
@@ -2465,9 +2465,12 @@
     Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
     clang::io::Emit16(Out, Bits);
 
-    if (HasMacroDefinition)
+    if (HasMacroDefinition) {
       clang::io::Emit32(Out, Writer.getMacroOffset(II));
-
+      clang::io::Emit32(Out, 
+        Writer.inferSubmoduleIDFromLocation(Macro->getDefinitionLoc()));
+    }
+    
     // Emit the declaration IDs in reverse order, because the
     // IdentifierResolver provides the declarations as they would be
     // visible (e.g., the function "stat" would come before the struct

Modified: cfe/trunk/test/Modules/Inputs/submodules/hash_map.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/submodules/hash_map.h?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/submodules/hash_map.h (original)
+++ cfe/trunk/test/Modules/Inputs/submodules/hash_map.h Fri Dec  2 09:45:10 2011
@@ -1 +1,4 @@
 template<typename Key, typename Data> class hash_map { };
+
+#define HAVE_HASH_MAP
+

Modified: cfe/trunk/test/Modules/Inputs/submodules/type_traits.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/submodules/type_traits.h?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/submodules/type_traits.h (original)
+++ cfe/trunk/test/Modules/Inputs/submodules/type_traits.h Fri Dec  2 09:45:10 2011
@@ -7,3 +7,6 @@
 struct remove_reference<T&> {
   typedef T type;
 };
+
+#define HAVE_TYPE_TRAITS
+

Modified: cfe/trunk/test/Modules/Inputs/submodules/vector.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/submodules/vector.h?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/submodules/vector.h (original)
+++ cfe/trunk/test/Modules/Inputs/submodules/vector.h Fri Dec  2 09:45:10 2011
@@ -1 +1,3 @@
 template<typename T> class vector { };
+
+#define HAVE_VECTOR

Modified: cfe/trunk/test/Modules/submodules-preprocess.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/submodules-preprocess.cpp?rev=145683&r1=145682&r2=145683&view=diff
==============================================================================
--- cfe/trunk/test/Modules/submodules-preprocess.cpp (original)
+++ cfe/trunk/test/Modules/submodules-preprocess.cpp Fri Dec  2 09:45:10 2011
@@ -3,12 +3,58 @@
 
 __import_module__ std.vector;
 
-vector<int> vi;
-remove_reference<int&>::type *int_ptr = 0;
+#ifndef HAVE_VECTOR
+#  error HAVE_VECTOR macro is not available (but should be)
+#endif
+
+#ifdef HAVE_TYPE_TRAITS
+#  error HAVE_TYPE_TRAITS_MAP macro is available (but shouldn't be)
+#endif
+
+#ifdef HAVE_HASH_MAP
+#  error HAVE_HASH_MAP macro is available (but shouldn't be)
+#endif
 
 __import_module__ std.typetraits; // expected-error{{no submodule named 'typetraits' in module 'std'; did you mean 'type_traits'?}}
 
-vector<float> vf;
-remove_reference<int&>::type *int_ptr2 = 0;
+#ifndef HAVE_VECTOR
+#  error HAVE_VECTOR macro is not available (but should be)
+#endif
+
+#ifndef HAVE_TYPE_TRAITS
+#  error HAVE_TYPE_TRAITS_MAP macro is not available (but should be)
+#endif
+
+#ifdef HAVE_HASH_MAP
+#  error HAVE_HASH_MAP macro is available (but shouldn't be)
+#endif
 
 __import_module__ std.vector.compare; // expected-error{{no submodule named 'compare' in module 'std.vector'}}
+
+__import_module__ std; // import everything in 'std'
+
+#ifndef HAVE_VECTOR
+#  error HAVE_VECTOR macro is not available (but should be)
+#endif
+
+#ifndef HAVE_TYPE_TRAITS
+#  error HAVE_TYPE_TRAITS_MAP macro is not available (but should be)
+#endif
+
+#ifdef HAVE_HASH_MAP
+#  error HAVE_HASH_MAP macro is available (but shouldn't be)
+#endif
+
+__import_module__ std.hash_map;
+
+#ifndef HAVE_VECTOR
+#  error HAVE_VECTOR macro is not available (but should be)
+#endif
+
+#ifndef HAVE_TYPE_TRAITS
+#  error HAVE_TYPE_TRAITS_MAP macro is not available (but should be)
+#endif
+
+#ifndef HAVE_HASH_MAP
+#  error HAVE_HASH_MAP macro is not available (but should be)
+#endif





More information about the cfe-commits mailing list