aboutsummaryrefslogtreecommitdiff
path: root/lib/Lex/Preprocessor.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2011-09-07 23:11:54 +0000
committerDouglas Gregor <dgregor@apple.com>2011-09-07 23:11:54 +0000
commitb8db7cd9ac05c522855631670ec2e97255384f5a (patch)
tree219ea0bfbdfdca93fcff2d45221abc0ffb660eac /lib/Lex/Preprocessor.cpp
parent0e2ca014bdf92b405f7c02f2d37532f4c9b9b663 (diff)
Optimize the preprocessor's handling of the __import_module__
keyword. We now handle this keyword in HandleIdentifier, making a note for ourselves when we've seen the __import_module__ keyword so that the next lexed token can trigger a module import (if needed). This greatly simplifies Preprocessor::Lex(), and completely erases the 5.5% -Eonly slowdown Argiris noted when I originally implemented __import_module__. Big thanks to Argiris for noting that horrible regression! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139265 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Preprocessor.cpp')
-rw-r--r--lib/Lex/Preprocessor.cpp45
1 files changed, 30 insertions, 15 deletions
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index d33d9c513d..db3479e8f3 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -144,8 +144,6 @@ void Preprocessor::Initialize(const TargetInfo &Target) {
// We haven't read anything from the external source.
ReadMacrosFromExternalSource = false;
- LexDepth = 0;
-
// "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
// This gets unpoisoned where it is allowed.
(Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
@@ -524,27 +522,44 @@ void Preprocessor::HandleIdentifier(Token &Identifier) {
// like "#define TY typeof", "TY(1) x".
if (II.isExtensionToken() && !DisableMacroExpansion)
Diag(Identifier, diag::ext_token_used);
+
+ // If this is the '__import_module__' keyword, note that the next token
+ // indicates a module name.
+ if (II.getTokenID() == tok::kw___import_module__ &&
+ !InMacroArgs && !DisableMacroExpansion) {
+ ModuleImportLoc = Identifier.getLocation();
+ CurLexerKind = CLK_LexAfterModuleImport;
+ }
}
-void Preprocessor::HandleModuleImport(Token &Import) {
+/// \brief Lex a token following the __import_module__ keyword.
+void Preprocessor::LexAfterModuleImport(Token &Result) {
+ // Figure out what kind of lexer we actually have.
+ if (CurLexer)
+ CurLexerKind = CLK_Lexer;
+ else if (CurPTHLexer)
+ CurLexerKind = CLK_PTHLexer;
+ else if (CurTokenLexer)
+ CurLexerKind = CLK_TokenLexer;
+ else
+ CurLexerKind = CLK_CachingLexer;
+
+ // Lex the next token.
+ Lex(Result);
+
// The token sequence
//
// __import_module__ identifier
//
- // indicates a module import directive. We load the module and then
- // leave the token sequence for the parser.
- Token ModuleNameTok = LookAhead(0);
- if (ModuleNameTok.getKind() != tok::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)
return;
- (void)TheModuleLoader.loadModule(Import.getLocation(),
- *ModuleNameTok.getIdentifierInfo(),
- ModuleNameTok.getLocation());
-
- // FIXME: Transmogrify __import_module__ into some kind of AST-only
- // __import_module__ that is not recognized by the preprocessor but is
- // recognized by the parser. It would also be useful to stash the ModuleKey
- // somewhere, so we don't try to load the module twice.
+ // Load the module.
+ (void)TheModuleLoader.loadModule(ModuleImportLoc,
+ *Result.getIdentifierInfo(),
+ Result.getLocation());
}
void Preprocessor::AddCommentHandler(CommentHandler *Handler) {