| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 1 | //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===// | 
|  | 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 pieces of the Preprocessor interface that manage the | 
|  | 11 | // current lexer stack. | 
|  | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
|  | 15 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | aa93a87 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" | 
| Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" | 
|  | 19 | #include "clang/Lex/LexDiagnostic.h" | 
|  | 20 | #include "clang/Lex/MacroInfo.h" | 
|  | 21 | #include "llvm/ADT/StringSwitch.h" | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" | 
| Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/PathV2.h" | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 25 | using namespace clang; | 
|  | 26 |  | 
| Ted Kremenek | 4b39108 | 2008-11-18 01:33:13 +0000 | [diff] [blame] | 27 | PPCallbacks::~PPCallbacks() {} | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 30 | // Miscellaneous Methods. | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// | 
|  | 32 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 33 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a | 
| James Dennett | 1a5f900 | 2012-06-22 05:36:05 +0000 | [diff] [blame] | 34 | /// \#include.  This looks through macro expansions and active _Pragma lexers. | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 35 | bool Preprocessor::isInPrimaryFile() const { | 
| Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 36 | if (IsFileLexer()) | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 37 | return IncludeMacroStack.empty(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 39 | // If there are any stacked lexers, we're in a #include. | 
| Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 40 | assert(IsFileLexer(IncludeMacroStack[0]) && | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 41 | "Top level include stack isn't our primary lexer?"); | 
|  | 42 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) | 
| Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 43 | if (IsFileLexer(IncludeMacroStack[i])) | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 44 | return false; | 
|  | 45 | return true; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | /// getCurrentLexer - Return the current file lexer being lexed from.  Note | 
|  | 49 | /// that this ignores any potentially active macro expansions and _Pragma | 
|  | 50 | /// expansions going on at the time. | 
| Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 51 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { | 
| Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 52 | if (IsFileLexer()) | 
| Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 53 | return CurPPLexer; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 55 | // Look for a stacked lexer. | 
|  | 56 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { | 
| Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 57 | const IncludeStackInfo& ISI = IncludeMacroStack[i-1]; | 
| Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 58 | if (IsFileLexer(ISI)) | 
| Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 59 | return ISI.ThePPLexer; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 60 | } | 
|  | 61 | return 0; | 
|  | 62 | } | 
|  | 63 |  | 
| Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 64 |  | 
|  | 65 | //===----------------------------------------------------------------------===// | 
|  | 66 | // Methods for Entering and Callbacks for leaving various contexts | 
|  | 67 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 68 |  | 
|  | 69 | /// EnterSourceFile - Add a source file to the top of the include stack and | 
| Nuno Lopes | e7f2cbd | 2009-11-29 17:07:16 +0000 | [diff] [blame] | 70 | /// start lexing tokens from it instead of the current buffer. | 
| Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 71 | void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, | 
|  | 72 | SourceLocation Loc) { | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 73 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); | 
|  | 74 | ++NumEnteredSourceFiles; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 76 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) | 
|  | 77 | MaxIncludeStackDepth = IncludeMacroStack.size(); | 
|  | 78 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 79 | if (PTH) { | 
| Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 80 | if (PTHLexer *PL = PTH->CreateLexer(FID)) { | 
|  | 81 | EnterSourceFileWithPTH(PL, CurDir); | 
| Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 82 | return; | 
| Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 83 | } | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 84 | } | 
| Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 85 |  | 
|  | 86 | // Get the MemoryBuffer for this FID, if it fails, we fail. | 
| Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 87 | bool Invalid = false; | 
| Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 88 | const llvm::MemoryBuffer *InputFile = | 
|  | 89 | getSourceManager().getBuffer(FID, Loc, &Invalid); | 
|  | 90 | if (Invalid) { | 
|  | 91 | SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID); | 
|  | 92 | Diag(Loc, diag::err_pp_error_opening_file) | 
|  | 93 | << std::string(SourceMgr.getBufferName(FileStart)) << ""; | 
|  | 94 | return; | 
|  | 95 | } | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 96 |  | 
|  | 97 | if (isCodeCompletionEnabled() && | 
|  | 98 | SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) { | 
|  | 99 | CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID); | 
|  | 100 | CodeCompletionLoc = | 
| Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 101 | CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset); | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 104 | EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir); | 
| Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 105 | return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | } | 
| Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 107 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 108 | /// EnterSourceFileWithLexer - Add a source file to the top of the include stack | 
|  | 109 | ///  and start lexing tokens from it instead of the current buffer. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 111 | const DirectoryLookup *CurDir) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 113 | // Add the current lexer to the include stack. | 
| Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 114 | if (CurPPLexer || CurTokenLexer) | 
| Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 115 | PushIncludeMacroStack(); | 
|  | 116 |  | 
| Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 117 | CurLexer.reset(TheLexer); | 
| Ted Kremenek | 9c1b750 | 2008-11-18 00:12:49 +0000 | [diff] [blame] | 118 | CurPPLexer = TheLexer; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 119 | CurDirLookup = CurDir; | 
| Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 120 | if (CurLexerKind != CLK_LexAfterModuleImport) | 
|  | 121 | CurLexerKind = CLK_Lexer; | 
|  | 122 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 123 | // Notify the client, if desired, that we are in a new source file. | 
|  | 124 | if (Callbacks && !CurLexer->Is_PragmaLexer) { | 
| Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 125 | SrcMgr::CharacteristicKind FileType = | 
| Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 126 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 128 | Callbacks->FileChanged(CurLexer->getFileLoc(), | 
|  | 129 | PPCallbacks::EnterFile, FileType); | 
|  | 130 | } | 
|  | 131 | } | 
|  | 132 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 133 | /// EnterSourceFileWithPTH - Add a source file to the top of the include stack | 
|  | 134 | /// and start getting tokens from it using the PTH cache. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 136 | const DirectoryLookup *CurDir) { | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 138 | if (CurPPLexer || CurTokenLexer) | 
|  | 139 | PushIncludeMacroStack(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 140 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 141 | CurDirLookup = CurDir; | 
|  | 142 | CurPTHLexer.reset(PL); | 
|  | 143 | CurPPLexer = CurPTHLexer.get(); | 
| Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 144 | if (CurLexerKind != CLK_LexAfterModuleImport) | 
|  | 145 | CurLexerKind = CLK_PTHLexer; | 
|  | 146 |  | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 147 | // Notify the client, if desired, that we are in a new source file. | 
|  | 148 | if (Callbacks) { | 
| Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 149 | FileID FID = CurPPLexer->getFileID(); | 
| Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 150 | SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID); | 
|  | 151 | SrcMgr::CharacteristicKind FileType = | 
|  | 152 | SourceMgr.getFileCharacteristic(EnterLoc); | 
|  | 153 | Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType); | 
| Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 154 | } | 
|  | 155 | } | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 156 |  | 
|  | 157 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing | 
|  | 158 | /// tokens from it instead of the current buffer. | 
| Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 159 | void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, | 
| Richard Smith | c30981a | 2012-08-30 13:38:46 +0000 | [diff] [blame] | 160 | MacroInfo *Macro, MacroArgs *Args) { | 
| Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 161 | TokenLexer *TokLexer; | 
|  | 162 | if (NumCachedTokenLexers == 0) { | 
|  | 163 | TokLexer = new TokenLexer(Tok, ILEnd, Macro, Args, *this); | 
|  | 164 | } else { | 
|  | 165 | TokLexer = TokenLexerCache[--NumCachedTokenLexers]; | 
|  | 166 | TokLexer->Init(Tok, ILEnd, Macro, Args); | 
|  | 167 | } | 
|  | 168 |  | 
| Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 169 | PushIncludeMacroStack(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 170 | CurDirLookup = 0; | 
| Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 171 | CurTokenLexer.reset(TokLexer); | 
| Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 172 | if (CurLexerKind != CLK_LexAfterModuleImport) | 
|  | 173 | CurLexerKind = CLK_TokenLexer; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, | 
| Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 177 | /// which will cause the lexer to start returning the specified tokens. | 
|  | 178 | /// | 
|  | 179 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will | 
|  | 180 | /// not be subject to further macro expansion.  Otherwise, these tokens will | 
|  | 181 | /// be re-macro-expanded when/if expansion is enabled. | 
|  | 182 | /// | 
|  | 183 | /// If OwnsTokens is false, this method assumes that the specified stream of | 
|  | 184 | /// tokens has a permanent owner somewhere, so they do not need to be copied. | 
|  | 185 | /// If it is true, it assumes the array of tokens is allocated with new[] and | 
|  | 186 | /// must be freed. | 
|  | 187 | /// | 
|  | 188 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, | 
|  | 189 | bool DisableMacroExpansion, | 
|  | 190 | bool OwnsTokens) { | 
| Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 191 | // Create a macro expander to expand from the specified token stream. | 
|  | 192 | TokenLexer *TokLexer; | 
|  | 193 | if (NumCachedTokenLexers == 0) { | 
|  | 194 | TokLexer = new TokenLexer(Toks, NumToks, DisableMacroExpansion, | 
|  | 195 | OwnsTokens, *this); | 
|  | 196 | } else { | 
|  | 197 | TokLexer = TokenLexerCache[--NumCachedTokenLexers]; | 
|  | 198 | TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); | 
|  | 199 | } | 
|  | 200 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 201 | // Save our current state. | 
| Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 202 | PushIncludeMacroStack(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 203 | CurDirLookup = 0; | 
| Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 204 | CurTokenLexer.reset(TokLexer); | 
| Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 205 | if (CurLexerKind != CLK_LexAfterModuleImport) | 
|  | 206 | CurLexerKind = CLK_TokenLexer; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 209 | /// \brief Compute the relative path that names the given file relative to | 
|  | 210 | /// the given directory. | 
|  | 211 | static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, | 
|  | 212 | const FileEntry *File, | 
| Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 213 | SmallString<128> &Result) { | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 214 | Result.clear(); | 
|  | 215 |  | 
|  | 216 | StringRef FilePath = File->getDir()->getName(); | 
|  | 217 | StringRef Path = FilePath; | 
|  | 218 | while (!Path.empty()) { | 
|  | 219 | if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) { | 
|  | 220 | if (CurDir == Dir) { | 
|  | 221 | Result = FilePath.substr(Path.size()); | 
|  | 222 | llvm::sys::path::append(Result, | 
|  | 223 | llvm::sys::path::filename(File->getName())); | 
|  | 224 | return; | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | Path = llvm::sys::path::parent_path(Path); | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | Result = File->getName(); | 
|  | 232 | } | 
|  | 233 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 234 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of | 
|  | 235 | /// the current file.  This either returns the EOF token or pops a level off | 
|  | 236 | /// the include stack and keeps going. | 
|  | 237 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { | 
|  | 238 | assert(!CurTokenLexer && | 
|  | 239 | "Ending a file when currently in a macro!"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 241 | // See if this file had a controlling macro. | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 242 | if (CurPPLexer) {  // Not ending a macro, ignore it. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | if (const IdentifierInfo *ControllingMacro = | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 244 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { | 
| Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 245 | // Okay, this has a controlling macro, remember in HeaderFileInfo. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | if (const FileEntry *FE = | 
| Douglas Gregor | d83d2e7 | 2011-12-12 18:47:39 +0000 | [diff] [blame] | 247 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 248 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); | 
|  | 249 | } | 
|  | 250 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 |  | 
| John McCall | 3eff321 | 2011-10-18 00:44:04 +0000 | [diff] [blame] | 252 | // Complain about reaching a true EOF within arc_cf_code_audited. | 
|  | 253 | // We don't want to complain about reaching the end of a macro | 
|  | 254 | // instantiation or a _Pragma. | 
|  | 255 | if (PragmaARCCFCodeAuditedLoc.isValid() && | 
| John McCall | d80d90d | 2011-10-18 01:36:41 +0000 | [diff] [blame] | 256 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { | 
| John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 257 | Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited); | 
|  | 258 |  | 
|  | 259 | // Recover by leaving immediately. | 
|  | 260 | PragmaARCCFCodeAuditedLoc = SourceLocation(); | 
|  | 261 | } | 
|  | 262 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 263 | // If this is a #include'd file, pop it off the include stack and continue | 
|  | 264 | // lexing the #includer file. | 
|  | 265 | if (!IncludeMacroStack.empty()) { | 
| Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 266 |  | 
|  | 267 | // If we lexed the code-completion file, act as if we reached EOF. | 
|  | 268 | if (isCodeCompletionEnabled() && CurPPLexer && | 
|  | 269 | SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) == | 
|  | 270 | CodeCompletionFileLoc) { | 
|  | 271 | if (CurLexer) { | 
|  | 272 | Result.startToken(); | 
|  | 273 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); | 
|  | 274 | CurLexer.reset(); | 
|  | 275 | } else { | 
|  | 276 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); | 
|  | 277 | CurPTHLexer->getEOF(Result); | 
|  | 278 | CurPTHLexer.reset(); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | CurPPLexer = 0; | 
|  | 282 | return true; | 
|  | 283 | } | 
|  | 284 |  | 
| Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 285 | if (!isEndOfMacro && CurPPLexer && | 
|  | 286 | SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) { | 
|  | 287 | // Notify SourceManager to record the number of FileIDs that were created | 
|  | 288 | // during lexing of the #include'd file. | 
|  | 289 | unsigned NumFIDs = | 
|  | 290 | SourceMgr.local_sloc_entry_size() - | 
|  | 291 | CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/; | 
|  | 292 | SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); | 
|  | 293 | } | 
|  | 294 |  | 
| Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 295 | FileID ExitedFID; | 
|  | 296 | if (Callbacks && !isEndOfMacro && CurPPLexer) | 
|  | 297 | ExitedFID = CurPPLexer->getFileID(); | 
|  | 298 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 299 | // We're done with the #included file. | 
|  | 300 | RemoveTopOfLexerStack(); | 
|  | 301 |  | 
|  | 302 | // Notify the client, if desired, that we are in a new source file. | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 303 | if (Callbacks && !isEndOfMacro && CurPPLexer) { | 
| Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 304 | SrcMgr::CharacteristicKind FileType = | 
| Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 305 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); | 
|  | 306 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), | 
| Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 307 | PPCallbacks::ExitFile, FileType, ExitedFID); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 308 | } | 
|  | 309 |  | 
|  | 310 | // Client should lex another token. | 
|  | 311 | return false; | 
|  | 312 | } | 
|  | 313 |  | 
|  | 314 | // If the file ends with a newline, form the EOF token on the newline itself, | 
|  | 315 | // rather than "on the line following it", which doesn't exist.  This makes | 
|  | 316 | // diagnostics relating to the end of file include the last file that the user | 
|  | 317 | // actually typed, which is goodness. | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 318 | if (CurLexer) { | 
|  | 319 | const char *EndPos = CurLexer->BufferEnd; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | if (EndPos != CurLexer->BufferStart && | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 321 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 322 | --EndPos; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 |  | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 324 | // Handle \n\r and \r\n: | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | if (EndPos != CurLexer->BufferStart && | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 326 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && | 
|  | 327 | EndPos[-1] != EndPos[0]) | 
|  | 328 | --EndPos; | 
|  | 329 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 |  | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 331 | Result.startToken(); | 
|  | 332 | CurLexer->BufferPtr = EndPos; | 
|  | 333 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 |  | 
| Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 335 | if (isCodeCompletionEnabled()) { | 
|  | 336 | // Inserting the code-completion point increases the source buffer by 1, | 
|  | 337 | // but the main FileID was created before inserting the point. | 
|  | 338 | // Compensate by reducing the EOF location by 1, otherwise the location | 
|  | 339 | // will point to the next FileID. | 
|  | 340 | // FIXME: This is hacky, the code-completion point should probably be | 
|  | 341 | // inserted before the main FileID is created. | 
|  | 342 | if (CurLexer->getFileLoc() == CodeCompletionFileLoc) | 
|  | 343 | Result.setLocation(Result.getLocation().getLocWithOffset(-1)); | 
|  | 344 | } | 
|  | 345 |  | 
| Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 346 | if (!isIncrementalProcessingEnabled()) | 
|  | 347 | // We're done with lexing. | 
|  | 348 | CurLexer.reset(); | 
| Chris Lattner | 6711608 | 2009-02-13 23:06:48 +0000 | [diff] [blame] | 349 | } else { | 
|  | 350 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); | 
| Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 351 | CurPTHLexer->getEOF(Result); | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 352 | CurPTHLexer.reset(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 353 | } | 
| Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 354 |  | 
|  | 355 | if (!isIncrementalProcessingEnabled()) | 
|  | 356 | CurPPLexer = 0; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 357 |  | 
| Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 358 | // This is the end of the top-level file. 'WarnUnusedMacroLocs' has collected | 
|  | 359 | // all macro locations that we need to warn because they are not used. | 
|  | 360 | for (WarnUnusedMacroLocsTy::iterator | 
|  | 361 | I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); I!=E; ++I) | 
|  | 362 | Diag(*I, diag::pp_macro_not_used); | 
| Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 363 |  | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 364 | // If we are building a module that has an umbrella header, make sure that | 
|  | 365 | // each of the headers within the directory covered by the umbrella header | 
|  | 366 | // was actually included by the umbrella header. | 
|  | 367 | if (Module *Mod = getCurrentModule()) { | 
|  | 368 | if (Mod->getUmbrellaHeader()) { | 
|  | 369 | SourceLocation StartLoc | 
|  | 370 | = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); | 
|  | 371 |  | 
|  | 372 | if (getDiagnostics().getDiagnosticLevel( | 
|  | 373 | diag::warn_uncovered_module_header, | 
|  | 374 | StartLoc) != DiagnosticsEngine::Ignored) { | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 375 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); | 
| Douglas Gregor | 224bfee | 2011-12-23 00:27:08 +0000 | [diff] [blame] | 376 | typedef llvm::sys::fs::recursive_directory_iterator | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 377 | recursive_directory_iterator; | 
|  | 378 | const DirectoryEntry *Dir = Mod->getUmbrellaDir(); | 
|  | 379 | llvm::error_code EC; | 
|  | 380 | for (recursive_directory_iterator Entry(Dir->getName(), EC), End; | 
|  | 381 | Entry != End && !EC; Entry.increment(EC)) { | 
|  | 382 | using llvm::StringSwitch; | 
|  | 383 |  | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 384 | // Check whether this entry has an extension typically associated with | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 385 | // headers. | 
|  | 386 | if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path())) | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 387 | .Cases(".h", ".H", ".hh", ".hpp", true) | 
|  | 388 | .Default(false)) | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 389 | continue; | 
|  | 390 |  | 
|  | 391 | if (const FileEntry *Header = getFileManager().getFile(Entry->path())) | 
|  | 392 | if (!getSourceManager().hasFileInfo(Header)) { | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 393 | if (!ModMap.isHeaderInUnavailableModule(Header)) { | 
|  | 394 | // Find the relative path that would access this header. | 
| Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 395 | SmallString<128> RelativePath; | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 396 | computeRelativePath(FileMgr, Dir, Header, RelativePath); | 
|  | 397 | Diag(StartLoc, diag::warn_uncovered_module_header) | 
| Douglas Gregor | c9c3907 | 2013-01-04 18:58:28 +0000 | [diff] [blame] | 398 | << Mod->getFullModuleName() << RelativePath; | 
| Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 399 | } | 
| Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 400 | } | 
|  | 401 | } | 
|  | 402 | } | 
|  | 403 | } | 
|  | 404 | } | 
|  | 405 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 406 | return true; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer | 
|  | 410 | /// hits the end of its token stream. | 
|  | 411 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 412 | assert(CurTokenLexer && !CurPPLexer && | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 413 | "Ending a macro when currently in a #include file!"); | 
|  | 414 |  | 
| Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 415 | if (!MacroExpandingLexersStack.empty() && | 
|  | 416 | MacroExpandingLexersStack.back().first == CurTokenLexer.get()) | 
|  | 417 | removeCachedMacroExpandedTokensOfLastLexer(); | 
|  | 418 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 419 | // Delete or cache the now-dead macro expander. | 
|  | 420 | if (NumCachedTokenLexers == TokenLexerCacheSize) | 
| Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 421 | CurTokenLexer.reset(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 422 | else | 
| Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 423 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 424 |  | 
|  | 425 | // Handle this like a #include file being popped off the stack. | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 426 | return HandleEndOfFile(Result, true); | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the | 
|  | 430 | /// lexer stack.  This should only be used in situations where the current | 
|  | 431 | /// state of the top-of-stack lexer is unknown. | 
|  | 432 | void Preprocessor::RemoveTopOfLexerStack() { | 
|  | 433 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 435 | if (CurTokenLexer) { | 
|  | 436 | // Delete or cache the now-dead macro expander. | 
|  | 437 | if (NumCachedTokenLexers == TokenLexerCacheSize) | 
| Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 438 | CurTokenLexer.reset(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 439 | else | 
| Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 440 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | } | 
|  | 442 |  | 
| Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 443 | PopIncludeMacroStack(); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 444 | } | 
|  | 445 |  | 
|  | 446 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a | 
|  | 447 | /// comment (/##/) in microsoft mode, this method handles updating the current | 
|  | 448 | /// state, returning the token on the next source line. | 
|  | 449 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 450 | assert(CurTokenLexer && !CurPPLexer && | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 451 | "Pasted comment can only be formed from macro"); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 453 | // We handle this by scanning for the closest real lexer, switching it to | 
|  | 454 | // raw mode and preprocessor mode.  This will cause it to return \n as an | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 455 | // explicit EOD token. | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 456 | PreprocessorLexer *FoundLexer = 0; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 457 | bool LexerWasInPPMode = false; | 
|  | 458 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { | 
|  | 459 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 460 | if (ISI.ThePPLexer == 0) continue;  // Scan for a real lexer. | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 462 | // Once we find a real lexer, mark it as raw mode (disabling macro | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 463 | // expansions) and preprocessor mode (return EOD).  We know that the lexer | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 464 | // was *not* in raw mode before, because the macro that the comment came | 
|  | 465 | // from was expanded.  However, it could have already been in preprocessor | 
|  | 466 | // mode (#if COMMENT) in which case we have to return it to that mode and | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 467 | // return EOD. | 
| Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 468 | FoundLexer = ISI.ThePPLexer; | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 469 | FoundLexer->LexingRawMode = true; | 
|  | 470 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; | 
|  | 471 | FoundLexer->ParsingPreprocessorDirective = true; | 
|  | 472 | break; | 
|  | 473 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 475 | // Okay, we either found and switched over the lexer, or we didn't find a | 
|  | 476 | // lexer.  In either case, finish off the macro the comment came from, getting | 
|  | 477 | // the next token. | 
|  | 478 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 |  | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 480 | // Discarding comments as long as we don't have EOF or EOD.  This 'comments | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 481 | // out' the rest of the line, including any tokens that came from other macros | 
|  | 482 | // that were active, as in: | 
|  | 483 | //  #define submacro a COMMENT b | 
|  | 484 | //    submacro c | 
|  | 485 | // which should lex to 'a' only: 'b' and 'c' should be removed. | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 486 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 487 | Lex(Tok); | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 |  | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 489 | // If we got an eod token, then we successfully found the end of the line. | 
|  | 490 | if (Tok.is(tok::eod)) { | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 491 | assert(FoundLexer && "Can't get end of line without an active lexer"); | 
|  | 492 | // Restore the lexer back to normal mode instead of raw mode. | 
|  | 493 | FoundLexer->LexingRawMode = false; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 |  | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 495 | // If the lexer was already in preprocessor mode, just return the EOD token | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 496 | // to finish the preprocessor line. | 
|  | 497 | if (LexerWasInPPMode) return; | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 499 | // Otherwise, switch out of PP mode and return the next lexed token. | 
|  | 500 | FoundLexer->ParsingPreprocessorDirective = false; | 
|  | 501 | return Lex(Tok); | 
|  | 502 | } | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 |  | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 504 | // If we got an EOF token, then we reached the end of the token stream but | 
|  | 505 | // didn't find an explicit \n.  This can only happen if there was no lexer | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 506 | // active (an active lexer would return EOD at EOF if there was no \n in | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 507 | // preprocessor directive mode), so just return EOF as our token. | 
| Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 508 | assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); | 
| Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 509 | } |