1 //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the Preprocessor interface.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 // Options to support:
15 //   -H       - Print the name of each header file used.
16 //   -d[DNI] - Dump various things.
17 //   -fworking-directory - #line's with preprocessor's working dir.
18 //   -fpreprocessed
19 //   -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20 //   -W*
21 //   -w
22 //
23 // Messages to emit:
24 //   "Multiple include guards may be useful for:\n"
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "clang/Lex/Preprocessor.h"
29 #include "MacroArgs.h"
30 #include "clang/Lex/ExternalPreprocessorSource.h"
31 #include "clang/Lex/HeaderSearch.h"
32 #include "clang/Lex/MacroInfo.h"
33 #include "clang/Lex/Pragma.h"
34 #include "clang/Lex/PreprocessingRecord.h"
35 #include "clang/Lex/ScratchBuffer.h"
36 #include "clang/Lex/LexDiagnostic.h"
37 #include "clang/Lex/CodeCompletionHandler.h"
38 #include "clang/Lex/ModuleLoader.h"
39 #include "clang/Basic/SourceManager.h"
40 #include "clang/Basic/FileManager.h"
41 #include "clang/Basic/TargetInfo.h"
42 #include "llvm/ADT/APFloat.h"
43 #include "llvm/ADT/SmallVector.h"
44 #include "llvm/Support/MemoryBuffer.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Support/Capacity.h"
47 using namespace clang;
48 
49 //===----------------------------------------------------------------------===//
50 ExternalPreprocessorSource::~ExternalPreprocessorSource() { }
51 
52 Preprocessor::Preprocessor(DiagnosticsEngine &diags, LangOptions &opts,
53                            const TargetInfo *target, SourceManager &SM,
54                            HeaderSearch &Headers, ModuleLoader &TheModuleLoader,
55                            IdentifierInfoLookup* IILookup,
56                            bool OwnsHeaders,
57                            bool DelayInitialization)
58   : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
59     SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),
60     ExternalSource(0),
61     Identifiers(opts, IILookup), CodeComplete(0),
62     CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),
63     SkipMainFilePreamble(0, true), CurPPLexer(0),
64     CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), MacroArgCache(0),
65     Record(0), MIChainHead(0), MICache(0)
66 {
67   OwnsHeaderSearch = OwnsHeaders;
68 
69   if (!DelayInitialization) {
70     assert(Target && "Must provide target information for PP initialization");
71     Initialize(*Target);
72   }
73 }
74 
75 Preprocessor::~Preprocessor() {
76   assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
77 
78   while (!IncludeMacroStack.empty()) {
79     delete IncludeMacroStack.back().TheLexer;
80     delete IncludeMacroStack.back().TheTokenLexer;
81     IncludeMacroStack.pop_back();
82   }
83 
84   // Free any macro definitions.
85   for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next)
86     I->MI.Destroy();
87 
88   // Free any cached macro expanders.
89   for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
90     delete TokenLexerCache[i];
91 
92   // Free any cached MacroArgs.
93   for (MacroArgs *ArgList = MacroArgCache; ArgList; )
94     ArgList = ArgList->deallocate();
95 
96   // Release pragma information.
97   delete PragmaHandlers;
98 
99   // Delete the scratch buffer info.
100   delete ScratchBuf;
101 
102   // Delete the header search info, if we own it.
103   if (OwnsHeaderSearch)
104     delete &HeaderInfo;
105 
106   delete Callbacks;
107 }
108 
109 void Preprocessor::Initialize(const TargetInfo &Target) {
110   assert((!this->Target || this->Target == &Target) &&
111          "Invalid override of target information");
112   this->Target = &Target;
113 
114   // Initialize information about built-ins.
115   BuiltinInfo.InitializeTarget(Target);
116 
117   ScratchBuf = new ScratchBuffer(SourceMgr);
118   CounterValue = 0; // __COUNTER__ starts at 0.
119 
120   // Clear stats.
121   NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
122   NumIf = NumElse = NumEndif = 0;
123   NumEnteredSourceFiles = 0;
124   NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
125   NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
126   MaxIncludeStackDepth = 0;
127   NumSkipped = 0;
128 
129   // Default to discarding comments.
130   KeepComments = false;
131   KeepMacroComments = false;
132   SuppressIncludeNotFoundError = false;
133   AutoModuleImport = false;
134 
135   // Macro expansion is enabled.
136   DisableMacroExpansion = false;
137   InMacroArgs = false;
138   NumCachedTokenLexers = 0;
139 
140   CachedLexPos = 0;
141 
142   // We haven't read anything from the external source.
143   ReadMacrosFromExternalSource = false;
144 
145   // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
146   // This gets unpoisoned where it is allowed.
147   (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
148   SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
149 
150   // Initialize the pragma handlers.
151   PragmaHandlers = new PragmaNamespace(StringRef());
152   RegisterBuiltinPragmas();
153 
154   // Initialize builtin macros like __LINE__ and friends.
155   RegisterBuiltinMacros();
156 
157   if(Features.Borland) {
158     Ident__exception_info        = getIdentifierInfo("_exception_info");
159     Ident___exception_info       = getIdentifierInfo("__exception_info");
160     Ident_GetExceptionInfo       = getIdentifierInfo("GetExceptionInformation");
161     Ident__exception_code        = getIdentifierInfo("_exception_code");
162     Ident___exception_code       = getIdentifierInfo("__exception_code");
163     Ident_GetExceptionCode       = getIdentifierInfo("GetExceptionCode");
164     Ident__abnormal_termination  = getIdentifierInfo("_abnormal_termination");
165     Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
166     Ident_AbnormalTermination    = getIdentifierInfo("AbnormalTermination");
167   } else {
168     Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0;
169     Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0;
170     Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0;
171   }
172 }
173 
174 void Preprocessor::setPTHManager(PTHManager* pm) {
175   PTH.reset(pm);
176   FileMgr.addStatCache(PTH->createStatCache());
177 }
178 
179 void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
180   llvm::errs() << tok::getTokenName(Tok.getKind()) << " '"
181                << getSpelling(Tok) << "'";
182 
183   if (!DumpFlags) return;
184 
185   llvm::errs() << "\t";
186   if (Tok.isAtStartOfLine())
187     llvm::errs() << " [StartOfLine]";
188   if (Tok.hasLeadingSpace())
189     llvm::errs() << " [LeadingSpace]";
190   if (Tok.isExpandDisabled())
191     llvm::errs() << " [ExpandDisabled]";
192   if (Tok.needsCleaning()) {
193     const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
194     llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
195                  << "']";
196   }
197 
198   llvm::errs() << "\tLoc=<";
199   DumpLocation(Tok.getLocation());
200   llvm::errs() << ">";
201 }
202 
203 void Preprocessor::DumpLocation(SourceLocation Loc) const {
204   Loc.dump(SourceMgr);
205 }
206 
207 void Preprocessor::DumpMacro(const MacroInfo &MI) const {
208   llvm::errs() << "MACRO: ";
209   for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
210     DumpToken(MI.getReplacementToken(i));
211     llvm::errs() << "  ";
212   }
213   llvm::errs() << "\n";
214 }
215 
216 void Preprocessor::PrintStats() {
217   llvm::errs() << "\n*** Preprocessor Stats:\n";
218   llvm::errs() << NumDirectives << " directives found:\n";
219   llvm::errs() << "  " << NumDefined << " #define.\n";
220   llvm::errs() << "  " << NumUndefined << " #undef.\n";
221   llvm::errs() << "  #include/#include_next/#import:\n";
222   llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
223   llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
224   llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
225   llvm::errs() << "  " << NumElse << " #else/#elif.\n";
226   llvm::errs() << "  " << NumEndif << " #endif.\n";
227   llvm::errs() << "  " << NumPragma << " #pragma.\n";
228   llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
229 
230   llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
231              << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
232              << NumFastMacroExpanded << " on the fast path.\n";
233   llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
234              << " token paste (##) operations performed, "
235              << NumFastTokenPaste << " on the fast path.\n";
236 }
237 
238 Preprocessor::macro_iterator
239 Preprocessor::macro_begin(bool IncludeExternalMacros) const {
240   if (IncludeExternalMacros && ExternalSource &&
241       !ReadMacrosFromExternalSource) {
242     ReadMacrosFromExternalSource = true;
243     ExternalSource->ReadDefinedMacros();
244   }
245 
246   return Macros.begin();
247 }
248 
249 size_t Preprocessor::getTotalMemory() const {
250   return BP.getTotalMemory()
251     + llvm::capacity_in_bytes(MacroExpandedTokens)
252     + Predefines.capacity() /* Predefines buffer. */
253     + llvm::capacity_in_bytes(Macros)
254     + llvm::capacity_in_bytes(PragmaPushMacroInfo)
255     + llvm::capacity_in_bytes(PoisonReasons)
256     + llvm::capacity_in_bytes(CommentHandlers);
257 }
258 
259 Preprocessor::macro_iterator
260 Preprocessor::macro_end(bool IncludeExternalMacros) const {
261   if (IncludeExternalMacros && ExternalSource &&
262       !ReadMacrosFromExternalSource) {
263     ReadMacrosFromExternalSource = true;
264     ExternalSource->ReadDefinedMacros();
265   }
266 
267   return Macros.end();
268 }
269 
270 bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File,
271                                           unsigned CompleteLine,
272                                           unsigned CompleteColumn) {
273   assert(File);
274   assert(CompleteLine && CompleteColumn && "Starts from 1:1");
275   assert(!CodeCompletionFile && "Already set");
276 
277   using llvm::MemoryBuffer;
278 
279   // Load the actual file's contents.
280   bool Invalid = false;
281   const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid);
282   if (Invalid)
283     return true;
284 
285   // Find the byte position of the truncation point.
286   const char *Position = Buffer->getBufferStart();
287   for (unsigned Line = 1; Line < CompleteLine; ++Line) {
288     for (; *Position; ++Position) {
289       if (*Position != '\r' && *Position != '\n')
290         continue;
291 
292       // Eat \r\n or \n\r as a single line.
293       if ((Position[1] == '\r' || Position[1] == '\n') &&
294           Position[0] != Position[1])
295         ++Position;
296       ++Position;
297       break;
298     }
299   }
300 
301   Position += CompleteColumn - 1;
302 
303   // Insert '\0' at the code-completion point.
304   if (Position < Buffer->getBufferEnd()) {
305     CodeCompletionFile = File;
306     CodeCompletionOffset = Position - Buffer->getBufferStart();
307 
308     MemoryBuffer *NewBuffer =
309         MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1,
310                                             Buffer->getBufferIdentifier());
311     char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart());
312     char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
313     *NewPos = '\0';
314     std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
315     SourceMgr.overrideFileContents(File, NewBuffer);
316   }
317 
318   return false;
319 }
320 
321 void Preprocessor::CodeCompleteNaturalLanguage() {
322   if (CodeComplete)
323     CodeComplete->CodeCompleteNaturalLanguage();
324   setCodeCompletionReached();
325 }
326 
327 /// getSpelling - This method is used to get the spelling of a token into a
328 /// SmallVector. Note that the returned StringRef may not point to the
329 /// supplied buffer if a copy can be avoided.
330 StringRef Preprocessor::getSpelling(const Token &Tok,
331                                           SmallVectorImpl<char> &Buffer,
332                                           bool *Invalid) const {
333   // NOTE: this has to be checked *before* testing for an IdentifierInfo.
334   if (Tok.isNot(tok::raw_identifier)) {
335     // Try the fast path.
336     if (const IdentifierInfo *II = Tok.getIdentifierInfo())
337       return II->getName();
338   }
339 
340   // Resize the buffer if we need to copy into it.
341   if (Tok.needsCleaning())
342     Buffer.resize(Tok.getLength());
343 
344   const char *Ptr = Buffer.data();
345   unsigned Len = getSpelling(Tok, Ptr, Invalid);
346   return StringRef(Ptr, Len);
347 }
348 
349 /// CreateString - Plop the specified string into a scratch buffer and return a
350 /// location for it.  If specified, the source location provides a source
351 /// location for the token.
352 void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
353                                 SourceLocation ExpansionLocStart,
354                                 SourceLocation ExpansionLocEnd) {
355   Tok.setLength(Len);
356 
357   const char *DestPtr;
358   SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
359 
360   if (ExpansionLocStart.isValid())
361     Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
362                                        ExpansionLocEnd, Len);
363   Tok.setLocation(Loc);
364 
365   // If this is a raw identifier or a literal token, set the pointer data.
366   if (Tok.is(tok::raw_identifier))
367     Tok.setRawIdentifierData(DestPtr);
368   else if (Tok.isLiteral())
369     Tok.setLiteralData(DestPtr);
370 }
371 
372 Module *Preprocessor::getCurrentModule() {
373   if (getLangOptions().CurrentModule.empty())
374     return 0;
375 
376   return getHeaderSearchInfo().getModule(getLangOptions().CurrentModule);
377 }
378 
379 //===----------------------------------------------------------------------===//
380 // Preprocessor Initialization Methods
381 //===----------------------------------------------------------------------===//
382 
383 
384 /// EnterMainSourceFile - Enter the specified FileID as the main source file,
385 /// which implicitly adds the builtin defines etc.
386 void Preprocessor::EnterMainSourceFile() {
387   // We do not allow the preprocessor to reenter the main file.  Doing so will
388   // cause FileID's to accumulate information from both runs (e.g. #line
389   // information) and predefined macros aren't guaranteed to be set properly.
390   assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
391   FileID MainFileID = SourceMgr.getMainFileID();
392 
393   // Enter the main file source buffer.
394   EnterSourceFile(MainFileID, 0, SourceLocation());
395 
396   // If we've been asked to skip bytes in the main file (e.g., as part of a
397   // precompiled preamble), do so now.
398   if (SkipMainFilePreamble.first > 0)
399     CurLexer->SkipBytes(SkipMainFilePreamble.first,
400                         SkipMainFilePreamble.second);
401 
402   // Tell the header info that the main file was entered.  If the file is later
403   // #imported, it won't be re-entered.
404   if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
405     HeaderInfo.IncrementIncludeCount(FE);
406 
407   // Preprocess Predefines to populate the initial preprocessor state.
408   llvm::MemoryBuffer *SB =
409     llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
410   assert(SB && "Cannot create predefined source buffer");
411   FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
412   assert(!FID.isInvalid() && "Could not create FileID for predefines?");
413 
414   // Start parsing the predefines.
415   EnterSourceFile(FID, 0, SourceLocation());
416 }
417 
418 void Preprocessor::EndSourceFile() {
419   // Notify the client that we reached the end of the source file.
420   if (Callbacks)
421     Callbacks->EndOfMainFile();
422 }
423 
424 //===----------------------------------------------------------------------===//
425 // Lexer Event Handling.
426 //===----------------------------------------------------------------------===//
427 
428 /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
429 /// identifier information for the token and install it into the token,
430 /// updating the token kind accordingly.
431 IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
432   assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!");
433 
434   // Look up this token, see if it is a macro, or if it is a language keyword.
435   IdentifierInfo *II;
436   if (!Identifier.needsCleaning()) {
437     // No cleaning needed, just use the characters from the lexed buffer.
438     II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(),
439                                            Identifier.getLength()));
440   } else {
441     // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
442     llvm::SmallString<64> IdentifierBuffer;
443     StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
444     II = getIdentifierInfo(CleanedStr);
445   }
446 
447   // Update the token info (identifier info and appropriate token kind).
448   Identifier.setIdentifierInfo(II);
449   Identifier.setKind(II->getTokenID());
450 
451   return II;
452 }
453 
454 void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
455   PoisonReasons[II] = DiagID;
456 }
457 
458 void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
459   assert(Ident__exception_code && Ident__exception_info);
460   assert(Ident___exception_code && Ident___exception_info);
461   Ident__exception_code->setIsPoisoned(Poison);
462   Ident___exception_code->setIsPoisoned(Poison);
463   Ident_GetExceptionCode->setIsPoisoned(Poison);
464   Ident__exception_info->setIsPoisoned(Poison);
465   Ident___exception_info->setIsPoisoned(Poison);
466   Ident_GetExceptionInfo->setIsPoisoned(Poison);
467   Ident__abnormal_termination->setIsPoisoned(Poison);
468   Ident___abnormal_termination->setIsPoisoned(Poison);
469   Ident_AbnormalTermination->setIsPoisoned(Poison);
470 }
471 
472 void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
473   assert(Identifier.getIdentifierInfo() &&
474          "Can't handle identifiers without identifier info!");
475   llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
476     PoisonReasons.find(Identifier.getIdentifierInfo());
477   if(it == PoisonReasons.end())
478     Diag(Identifier, diag::err_pp_used_poisoned_id);
479   else
480     Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
481 }
482 
483 /// HandleIdentifier - This callback is invoked when the lexer reads an
484 /// identifier.  This callback looks up the identifier in the map and/or
485 /// potentially macro expands it or turns it into a named token (like 'for').
486 ///
487 /// Note that callers of this method are guarded by checking the
488 /// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
489 /// IdentifierInfo methods that compute these properties will need to change to
490 /// match.
491 void Preprocessor::HandleIdentifier(Token &Identifier) {
492   assert(Identifier.getIdentifierInfo() &&
493          "Can't handle identifiers without identifier info!");
494 
495   IdentifierInfo &II = *Identifier.getIdentifierInfo();
496 
497   // If the information about this identifier is out of date, update it from
498   // the external source.
499   if (II.isOutOfDate()) {
500     ExternalSource->updateOutOfDateIdentifier(II);
501     Identifier.setKind(II.getTokenID());
502   }
503 
504   // If this identifier was poisoned, and if it was not produced from a macro
505   // expansion, emit an error.
506   if (II.isPoisoned() && CurPPLexer) {
507     HandlePoisonedIdentifier(Identifier);
508   }
509 
510   // If this is a macro to be expanded, do it.
511   if (MacroInfo *MI = getMacroInfo(&II)) {
512     if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
513       if (MI->isEnabled()) {
514         if (!HandleMacroExpandedIdentifier(Identifier, MI))
515           return;
516       } else {
517         // C99 6.10.3.4p2 says that a disabled macro may never again be
518         // expanded, even if it's in a context where it could be expanded in the
519         // future.
520         Identifier.setFlag(Token::DisableExpand);
521       }
522     }
523   }
524 
525   // If this identifier is a keyword in C++11, produce a warning. Don't warn if
526   // we're not considering macro expansion, since this identifier might be the
527   // name of a macro.
528   // FIXME: This warning is disabled in cases where it shouldn't be, like
529   //   "#define constexpr constexpr", "int constexpr;"
530   if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) {
531     Diag(Identifier, diag::warn_cxx11_keyword) << II.getName();
532     // Don't diagnose this keyword again in this translation unit.
533     II.setIsCXX11CompatKeyword(false);
534   }
535 
536   // C++ 2.11p2: If this is an alternative representation of a C++ operator,
537   // then we act as if it is the actual operator and not the textual
538   // representation of it.
539   if (II.isCPlusPlusOperatorKeyword())
540     Identifier.setIdentifierInfo(0);
541 
542   // If this is an extension token, diagnose its use.
543   // We avoid diagnosing tokens that originate from macro definitions.
544   // FIXME: This warning is disabled in cases where it shouldn't be,
545   // like "#define TY typeof", "TY(1) x".
546   if (II.isExtensionToken() && !DisableMacroExpansion)
547     Diag(Identifier, diag::ext_token_used);
548 
549   // If this is the '__import_module__' keyword, note that the next token
550   // indicates a module name.
551   if (II.getTokenID() == tok::kw___import_module__ &&
552       !InMacroArgs && !DisableMacroExpansion) {
553     ModuleImportLoc = Identifier.getLocation();
554     ModuleImportPath.clear();
555     ModuleImportExpectsIdentifier = true;
556     CurLexerKind = CLK_LexAfterModuleImport;
557   }
558 }
559 
560 /// \brief Lex a token following the __import_module__ keyword.
561 void Preprocessor::LexAfterModuleImport(Token &Result) {
562   // Figure out what kind of lexer we actually have.
563   if (CurLexer)
564     CurLexerKind = CLK_Lexer;
565   else if (CurPTHLexer)
566     CurLexerKind = CLK_PTHLexer;
567   else if (CurTokenLexer)
568     CurLexerKind = CLK_TokenLexer;
569   else
570     CurLexerKind = CLK_CachingLexer;
571 
572   // Lex the next token.
573   Lex(Result);
574 
575   // The token sequence
576   //
577   //   __import_module__ identifier (. identifier)*
578   //
579   // indicates a module import directive. We already saw the __import_module__
580   // keyword, so now we're looking for the identifiers.
581   if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
582     // We expected to see an identifier here, and we did; continue handling
583     // identifiers.
584     ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(),
585                                               Result.getLocation()));
586     ModuleImportExpectsIdentifier = false;
587     CurLexerKind = CLK_LexAfterModuleImport;
588     return;
589   }
590 
591   // If we're expecting a '.' or a ';', and we got a '.', then wait until we
592   // see the next identifier.
593   if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
594     ModuleImportExpectsIdentifier = true;
595     CurLexerKind = CLK_LexAfterModuleImport;
596     return;
597   }
598 
599   // If we have a non-empty module path, load the named module.
600   if (!ModuleImportPath.empty())
601     (void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath,
602                                      Module::MacrosVisible,
603                                      /*IsIncludeDirective=*/false);
604 }
605 
606 void Preprocessor::AddCommentHandler(CommentHandler *Handler) {
607   assert(Handler && "NULL comment handler");
608   assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) ==
609          CommentHandlers.end() && "Comment handler already registered");
610   CommentHandlers.push_back(Handler);
611 }
612 
613 void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) {
614   std::vector<CommentHandler *>::iterator Pos
615   = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler);
616   assert(Pos != CommentHandlers.end() && "Comment handler not registered");
617   CommentHandlers.erase(Pos);
618 }
619 
620 bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
621   bool AnyPendingTokens = false;
622   for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
623        HEnd = CommentHandlers.end();
624        H != HEnd; ++H) {
625     if ((*H)->HandleComment(*this, Comment))
626       AnyPendingTokens = true;
627   }
628   if (!AnyPendingTokens || getCommentRetentionState())
629     return false;
630   Lex(result);
631   return true;
632 }
633 
634 ModuleLoader::~ModuleLoader() { }
635 
636 CommentHandler::~CommentHandler() { }
637 
638 CodeCompletionHandler::~CodeCompletionHandler() { }
639 
640 void Preprocessor::createPreprocessingRecord(
641                                       bool IncludeNestedMacroExpansions) {
642   if (Record)
643     return;
644 
645   Record = new PreprocessingRecord(getSourceManager(),
646                                    IncludeNestedMacroExpansions);
647   addPPCallbacks(Record);
648 }
649