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" |
| 16 | #include "clang/Lex/HeaderSearch.h" |
| 17 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 18 | #include "clang/Lex/LexDiagnostic.h" |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 15ba2af | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Ted Kremenek | 4b39108 | 2008-11-18 01:33:13 +0000 | [diff] [blame] | 23 | PPCallbacks::~PPCallbacks() {} |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 24 | |
| 25 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 26 | // Miscellaneous Methods. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 29 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
Chris Lattner | 7d39d74 | 2008-03-09 04:49:35 +0000 | [diff] [blame] | 30 | /// #include. This looks through macro expansions and active _Pragma lexers. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 31 | bool Preprocessor::isInPrimaryFile() const { |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 32 | if (IsFileLexer()) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 33 | return IncludeMacroStack.empty(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 34 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 35 | // If there are any stacked lexers, we're in a #include. |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 36 | assert(IsFileLexer(IncludeMacroStack[0]) && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 37 | "Top level include stack isn't our primary lexer?"); |
| 38 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 39 | if (IsFileLexer(IncludeMacroStack[i])) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 40 | return false; |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 45 | /// that this ignores any potentially active macro expansions and _Pragma |
| 46 | /// expansions going on at the time. |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 47 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 48 | if (IsFileLexer()) |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 49 | return CurPPLexer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 50 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 51 | // Look for a stacked lexer. |
| 52 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 53 | const IncludeStackInfo& ISI = IncludeMacroStack[i-1]; |
Ted Kremenek | 81d24e1 | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 54 | if (IsFileLexer(ISI)) |
Ted Kremenek | 68e48e4 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 55 | return ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 60 | |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | // Methods for Entering and Callbacks for leaving various contexts |
| 63 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 64 | |
| 65 | /// EnterSourceFile - Add a source file to the top of the include stack and |
| 66 | /// start lexing tokens from it instead of the current buffer. Return true |
| 67 | /// on failure. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 68 | void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 69 | assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!"); |
| 70 | ++NumEnteredSourceFiles; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 71 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 72 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 73 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 74 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 75 | if (PTH) { |
Chris Lattner | f056d92 | 2009-01-17 08:06:50 +0000 | [diff] [blame] | 76 | if (PTHLexer *PL = PTH->CreateLexer(FID)) |
Chris Lattner | 88d3ac1 | 2009-01-17 08:03:42 +0000 | [diff] [blame] | 77 | return EnterSourceFileWithPTH(PL, CurDir); |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 88d3ac1 | 2009-01-17 08:03:42 +0000 | [diff] [blame] | 79 | EnterSourceFileWithLexer(new Lexer(FID, *this), CurDir); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 80 | } |
Chris Lattner | 7218183 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 81 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 82 | /// EnterSourceFileWithLexer - Add a source file to the top of the include stack |
| 83 | /// and start lexing tokens from it instead of the current buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 84 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 85 | const DirectoryLookup *CurDir) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 86 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 87 | // Add the current lexer to the include stack. |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 88 | if (CurPPLexer || CurTokenLexer) |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 89 | PushIncludeMacroStack(); |
| 90 | |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 91 | CurLexer.reset(TheLexer); |
Ted Kremenek | 9c1b750 | 2008-11-18 00:12:49 +0000 | [diff] [blame] | 92 | CurPPLexer = TheLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 93 | CurDirLookup = CurDir; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 94 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 95 | // Notify the client, if desired, that we are in a new source file. |
| 96 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 97 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 98 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 99 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 100 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 101 | PPCallbacks::EnterFile, FileType); |
| 102 | } |
| 103 | } |
| 104 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 105 | /// EnterSourceFileWithPTH - Add a source file to the top of the include stack |
| 106 | /// and start getting tokens from it using the PTH cache. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 107 | void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 108 | const DirectoryLookup *CurDir) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 109 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 110 | if (CurPPLexer || CurTokenLexer) |
| 111 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 113 | CurDirLookup = CurDir; |
| 114 | CurPTHLexer.reset(PL); |
| 115 | CurPPLexer = CurPTHLexer.get(); |
| 116 | |
| 117 | // Notify the client, if desired, that we are in a new source file. |
| 118 | if (Callbacks) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 119 | FileID FID = CurPPLexer->getFileID(); |
Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 120 | SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID); |
| 121 | SrcMgr::CharacteristicKind FileType = |
| 122 | SourceMgr.getFileCharacteristic(EnterLoc); |
| 123 | Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType); |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 126 | |
| 127 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 128 | /// tokens from it instead of the current buffer. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 129 | void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, |
| 130 | MacroArgs *Args) { |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 131 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 132 | CurDirLookup = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 133 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 134 | if (NumCachedTokenLexers == 0) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 135 | CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 136 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 137 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 138 | CurTokenLexer->Init(Tok, ILEnd, Args); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 143 | /// which will cause the lexer to start returning the specified tokens. |
| 144 | /// |
| 145 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 146 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 147 | /// be re-macro-expanded when/if expansion is enabled. |
| 148 | /// |
| 149 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 150 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 151 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 152 | /// must be freed. |
| 153 | /// |
| 154 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 155 | bool DisableMacroExpansion, |
| 156 | bool OwnsTokens) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 157 | // Save our current state. |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 158 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 159 | CurDirLookup = 0; |
| 160 | |
| 161 | // Create a macro expander to expand from the specified token stream. |
| 162 | if (NumCachedTokenLexers == 0) { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 163 | CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion, |
| 164 | OwnsTokens, *this)); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 165 | } else { |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 166 | CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]); |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 167 | CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 172 | /// the current file. This either returns the EOF token or pops a level off |
| 173 | /// the include stack and keeps going. |
| 174 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 175 | assert(!CurTokenLexer && |
| 176 | "Ending a file when currently in a macro!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 177 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 178 | // See if this file had a controlling macro. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 179 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 180 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 181 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 182 | // Okay, this has a controlling macro, remember in HeaderFileInfo. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 183 | if (const FileEntry *FE = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 184 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 185 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 186 | } |
| 187 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 188 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 189 | // If this is a #include'd file, pop it off the include stack and continue |
| 190 | // lexing the #includer file. |
| 191 | if (!IncludeMacroStack.empty()) { |
| 192 | // We're done with the #included file. |
| 193 | RemoveTopOfLexerStack(); |
| 194 | |
| 195 | // 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] | 196 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 197 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 198 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); |
| 199 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 200 | PPCallbacks::ExitFile, FileType); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Client should lex another token. |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // If the file ends with a newline, form the EOF token on the newline itself, |
| 208 | // rather than "on the line following it", which doesn't exist. This makes |
| 209 | // diagnostics relating to the end of file include the last file that the user |
| 210 | // actually typed, which is goodness. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 211 | if (CurLexer) { |
| 212 | const char *EndPos = CurLexer->BufferEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 213 | if (EndPos != CurLexer->BufferStart && |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 214 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 215 | --EndPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 216 | |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 217 | // Handle \n\r and \r\n: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 218 | if (EndPos != CurLexer->BufferStart && |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 219 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 220 | EndPos[-1] != EndPos[0]) |
| 221 | --EndPos; |
| 222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 223 | |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 224 | Result.startToken(); |
| 225 | CurLexer->BufferPtr = EndPos; |
| 226 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 227 | |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 228 | // We're done with the #included file. |
| 229 | CurLexer.reset(); |
Chris Lattner | 6711608 | 2009-02-13 23:06:48 +0000 | [diff] [blame] | 230 | } else { |
| 231 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 232 | CurPTHLexer->getEOF(Result); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 233 | CurPTHLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 234 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 235 | |
Ted Kremenek | 41938c8 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 236 | CurPPLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 237 | |
| 238 | // This is the end of the top-level file. If the diag::pp_macro_not_used |
| 239 | // diagnostic is enabled, look for macros that have not been used. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 240 | if (getDiagnostics().getDiagnosticLevel(diag::pp_macro_not_used) != |
Chris Lattner | 7f549df | 2009-03-13 21:17:23 +0000 | [diff] [blame] | 241 | Diagnostic::Ignored) { |
Chris Lattner | 09b5c1d | 2009-02-06 06:26:42 +0000 | [diff] [blame] | 242 | for (macro_iterator I = macro_begin(), E = macro_end(); I != E; ++I) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 243 | if (!I->second->isUsed()) |
| 244 | Diag(I->second->getDefinitionLoc(), diag::pp_macro_not_used); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 250 | /// hits the end of its token stream. |
| 251 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 252 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 253 | "Ending a macro when currently in a #include file!"); |
| 254 | |
| 255 | // Delete or cache the now-dead macro expander. |
| 256 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 257 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 258 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 259 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 260 | |
| 261 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 262 | return HandleEndOfFile(Result, true); |
| 263 | } |
| 264 | |
| 265 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 266 | /// lexer stack. This should only be used in situations where the current |
| 267 | /// state of the top-of-stack lexer is unknown. |
| 268 | void Preprocessor::RemoveTopOfLexerStack() { |
| 269 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 270 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 271 | if (CurTokenLexer) { |
| 272 | // Delete or cache the now-dead macro expander. |
| 273 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 274 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 275 | else |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 276 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 277 | } |
| 278 | |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 279 | PopIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 283 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 284 | /// state, returning the token on the next source line. |
| 285 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 286 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 287 | "Pasted comment can only be formed from macro"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 288 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 289 | // We handle this by scanning for the closest real lexer, switching it to |
| 290 | // raw mode and preprocessor mode. This will cause it to return \n as an |
| 291 | // explicit EOM token. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 292 | PreprocessorLexer *FoundLexer = 0; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 293 | bool LexerWasInPPMode = false; |
| 294 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 295 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 296 | if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 297 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 298 | // Once we find a real lexer, mark it as raw mode (disabling macro |
| 299 | // expansions) and preprocessor mode (return EOM). We know that the lexer |
| 300 | // was *not* in raw mode before, because the macro that the comment came |
| 301 | // from was expanded. However, it could have already been in preprocessor |
| 302 | // mode (#if COMMENT) in which case we have to return it to that mode and |
| 303 | // return EOM. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 304 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 305 | FoundLexer->LexingRawMode = true; |
| 306 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 307 | FoundLexer->ParsingPreprocessorDirective = true; |
| 308 | break; |
| 309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 310 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 311 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 312 | // lexer. In either case, finish off the macro the comment came from, getting |
| 313 | // the next token. |
| 314 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 315 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 316 | // Discarding comments as long as we don't have EOF or EOM. This 'comments |
| 317 | // out' the rest of the line, including any tokens that came from other macros |
| 318 | // that were active, as in: |
| 319 | // #define submacro a COMMENT b |
| 320 | // submacro c |
| 321 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
| 322 | while (Tok.isNot(tok::eom) && Tok.isNot(tok::eof)) |
| 323 | Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 324 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 325 | // If we got an eom token, then we successfully found the end of the line. |
| 326 | if (Tok.is(tok::eom)) { |
| 327 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 328 | // Restore the lexer back to normal mode instead of raw mode. |
| 329 | FoundLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 330 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 331 | // If the lexer was already in preprocessor mode, just return the EOM token |
| 332 | // to finish the preprocessor line. |
| 333 | if (LexerWasInPPMode) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 334 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 335 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 336 | FoundLexer->ParsingPreprocessorDirective = false; |
| 337 | return Lex(Tok); |
| 338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame^] | 339 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 340 | // If we got an EOF token, then we reached the end of the token stream but |
| 341 | // didn't find an explicit \n. This can only happen if there was no lexer |
| 342 | // active (an active lexer would return EOM at EOF if there was no \n in |
| 343 | // preprocessor directive mode), so just return EOF as our token. |
| 344 | assert(!FoundLexer && "Lexer should return EOM before EOF in PP mode"); |
| 345 | } |