[cfe-commits] r145478 - in /cfe/trunk: include/clang/Frontend/CompilerInstance.h include/clang/Lex/Preprocessor.h lib/Frontend/CompilerInstance.cpp lib/Lex/Preprocessor.cpp

Douglas Gregor dgregor at apple.com
Tue Nov 29 20:26:53 PST 2011


Author: dgregor
Date: Tue Nov 29 22:26:53 2011
New Revision: 145478

URL: http://llvm.org/viewvc/llvm-project?rev=145478&view=rev
Log:
Teach the preprocessor how to handle module import declarations that
involve submodules (e.g., importing std.vector), rather than always
importing the top-level module.


Modified:
    cfe/trunk/include/clang/Frontend/CompilerInstance.h
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/Lex/Preprocessor.cpp

Modified: cfe/trunk/include/clang/Frontend/CompilerInstance.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/CompilerInstance.h?rev=145478&r1=145477&r2=145478&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/CompilerInstance.h (original)
+++ cfe/trunk/include/clang/Frontend/CompilerInstance.h Tue Nov 29 22:26:53 2011
@@ -109,6 +109,14 @@
   /// along with the module map
   llvm::DenseMap<const IdentifierInfo *, KnownModule> KnownModules;
   
+  /// \brief The location of the module-import keyword for the last module
+  /// import. 
+  SourceLocation LastModuleImportLoc;
+  
+  /// \brief The result of the last module import.
+  ///
+  KnownModule LastModuleImportResult;
+  
   /// \brief Holds information about the output file.
   ///
   /// If TempFilename is not empty we must rename it to Filename at the end.

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=145478&r1=145477&r2=145478&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Nov 29 22:26:53 2011
@@ -167,6 +167,14 @@
   /// lexed, if any.
   SourceLocation ModuleImportLoc;
 
+  /// \brief The module import path that we're currently processing.
+  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> 
+    ModuleImportPath;
+  
+  /// \brief Whether the module import expectes an identifier next. Otherwise,
+  /// it expects a '.' or ';'.
+  bool ModuleImportExpectsIdentifier;
+  
   /// \brief The source location of the currently-active
   /// #pragma clang arc_cf_code_audited begin.
   SourceLocation PragmaARCCFCodeAuditedLoc;

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=145478&r1=145477&r2=145478&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Tue Nov 29 22:26:53 2011
@@ -1070,6 +1070,12 @@
 
 ModuleKey CompilerInstance::loadModule(SourceLocation ImportLoc, 
                                        ModuleIdPath Path) {
+  // If we've already handled this import, just return the cached result.
+  // This one-element cache is important to eliminate redundant diagnostics
+  // when both the preprocessor and parser see the same import declaration.
+  if (!ImportLoc.isInvalid() && LastModuleImportLoc == ImportLoc)
+    return LastModuleImportResult.getOpaqueValue();
+  
   // Determine what file we're searching from.
   SourceManager &SourceMgr = getSourceManager();
   SourceLocation ExpandedImportLoc = SourceMgr.getExpansionLoc(ImportLoc);
@@ -1241,6 +1247,8 @@
   // FIXME: The module file's FileEntry makes a poor key indeed! Once we 
   // eliminate the need for FileEntry here, the module itself will become the
   // key (which does make sense).
+  LastModuleImportLoc = ImportLoc;
+  LastModuleImportResult = Known;
   return Known.getOpaqueValue();
 }
 

Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=145478&r1=145477&r2=145478&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Tue Nov 29 22:26:53 2011
@@ -546,6 +546,8 @@
   if (II.getTokenID() == tok::kw___import_module__ &&
       !InMacroArgs && !DisableMacroExpansion) {
     ModuleImportLoc = Identifier.getLocation();
+    ModuleImportPath.clear();
+    ModuleImportExpectsIdentifier = true;
     CurLexerKind = CLK_LexAfterModuleImport;
   }
 }
@@ -567,19 +569,31 @@
 
   // The token sequence 
   //
-  //   __import_module__ identifier
+  //   __import_module__ identifier (. identifier)*
   //
   // indicates a module import directive. We already saw the __import_module__
-  // keyword, so now we're looking for the identifier.
-  if (Result.getKind() != tok::identifier)
+  // keyword, so now we're looking for the identifiers.
+  if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
+    // We expected to see an identifier here, and we did; continue handling
+    // identifiers.
+    ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
+                                              Result.getLocation()));
+    ModuleImportExpectsIdentifier = false;
+    CurLexerKind = CLK_LexAfterModuleImport;
     return;
+  }
   
-  // Load the module.
-  llvm::SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
-  Path.push_back(std::make_pair(Result.getIdentifierInfo(), 
-                                Result.getLocation()));
-  
-  (void)TheModuleLoader.loadModule(ModuleImportLoc, Path);
+  // If we're expecting a '.' or a ';', and we got a '.', then wait until we
+  // see the next identifier.
+  if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
+    ModuleImportExpectsIdentifier = true;
+    CurLexerKind = CLK_LexAfterModuleImport;
+    return;
+  }
+
+  // If we have a non-empty module path, load the named module.
+  if (!ModuleImportPath.empty())
+    (void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath);
 }
 
 void Preprocessor::AddCommentHandler(CommentHandler *Handler) {





More information about the cfe-commits mailing list