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" |
Rafael Espindola | 8229d22 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.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 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 61 | return nullptr; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 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. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 71 | bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 72 | SourceLocation Loc) { |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 73 | assert(!CurTokenLexer && "Cannot #include a file inside a macro!"); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 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); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 82 | return false; |
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)) << ""; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 94 | return true; |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 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); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 105 | return false; |
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; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 120 | CurSubmodule = nullptr; |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 121 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 122 | CurLexerKind = CLK_Lexer; |
| 123 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 124 | // Notify the client, if desired, that we are in a new source file. |
| 125 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 126 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 127 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 129 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 130 | PPCallbacks::EnterFile, FileType); |
| 131 | } |
| 132 | } |
| 133 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 134 | /// EnterSourceFileWithPTH - Add a source file to the top of the include stack |
| 135 | /// and start getting tokens from it using the PTH cache. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 137 | const DirectoryLookup *CurDir) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 139 | if (CurPPLexer || CurTokenLexer) |
| 140 | PushIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 142 | CurDirLookup = CurDir; |
| 143 | CurPTHLexer.reset(PL); |
| 144 | CurPPLexer = CurPTHLexer.get(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 145 | CurSubmodule = nullptr; |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 146 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 147 | CurLexerKind = CLK_PTHLexer; |
| 148 | |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 149 | // Notify the client, if desired, that we are in a new source file. |
| 150 | if (Callbacks) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 151 | FileID FID = CurPPLexer->getFileID(); |
Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 152 | SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID); |
| 153 | SrcMgr::CharacteristicKind FileType = |
| 154 | SourceMgr.getFileCharacteristic(EnterLoc); |
| 155 | Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType); |
Ted Kremenek | 6137dc9 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 158 | |
| 159 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 160 | /// tokens from it instead of the current buffer. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 161 | void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, |
Richard Smith | c30981a | 2012-08-30 13:38:46 +0000 | [diff] [blame] | 162 | MacroInfo *Macro, MacroArgs *Args) { |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 163 | TokenLexer *TokLexer; |
| 164 | if (NumCachedTokenLexers == 0) { |
| 165 | TokLexer = new TokenLexer(Tok, ILEnd, Macro, Args, *this); |
| 166 | } else { |
| 167 | TokLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 168 | TokLexer->Init(Tok, ILEnd, Macro, Args); |
| 169 | } |
| 170 | |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 171 | PushIncludeMacroStack(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 172 | CurDirLookup = nullptr; |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 173 | CurTokenLexer.reset(TokLexer); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 174 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 175 | CurLexerKind = CLK_TokenLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 6b88450 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 179 | /// which will cause the lexer to start returning the specified tokens. |
| 180 | /// |
| 181 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 182 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 183 | /// be re-macro-expanded when/if expansion is enabled. |
| 184 | /// |
| 185 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 186 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 187 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 188 | /// must be freed. |
| 189 | /// |
| 190 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 191 | bool DisableMacroExpansion, |
| 192 | bool OwnsTokens) { |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 193 | // Create a macro expander to expand from the specified token stream. |
| 194 | TokenLexer *TokLexer; |
| 195 | if (NumCachedTokenLexers == 0) { |
| 196 | TokLexer = new TokenLexer(Toks, NumToks, DisableMacroExpansion, |
| 197 | OwnsTokens, *this); |
| 198 | } else { |
| 199 | TokLexer = TokenLexerCache[--NumCachedTokenLexers]; |
| 200 | TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
| 201 | } |
| 202 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 203 | // Save our current state. |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 204 | PushIncludeMacroStack(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 205 | CurDirLookup = nullptr; |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 206 | CurTokenLexer.reset(TokLexer); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 207 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 208 | CurLexerKind = CLK_TokenLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 211 | /// \brief Compute the relative path that names the given file relative to |
| 212 | /// the given directory. |
| 213 | static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, |
| 214 | const FileEntry *File, |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 215 | SmallString<128> &Result) { |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 216 | Result.clear(); |
| 217 | |
| 218 | StringRef FilePath = File->getDir()->getName(); |
| 219 | StringRef Path = FilePath; |
| 220 | while (!Path.empty()) { |
| 221 | if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) { |
| 222 | if (CurDir == Dir) { |
| 223 | Result = FilePath.substr(Path.size()); |
| 224 | llvm::sys::path::append(Result, |
| 225 | llvm::sys::path::filename(File->getName())); |
| 226 | return; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | Path = llvm::sys::path::parent_path(Path); |
| 231 | } |
| 232 | |
| 233 | Result = File->getName(); |
| 234 | } |
| 235 | |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 236 | void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { |
| 237 | if (CurTokenLexer) { |
| 238 | CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 239 | return; |
| 240 | } |
| 241 | if (CurLexer) { |
| 242 | CurLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 243 | return; |
| 244 | } |
| 245 | // FIXME: Handle other kinds of lexers? It generally shouldn't matter, |
| 246 | // but it might if they're empty? |
| 247 | } |
| 248 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 249 | /// \brief Determine the location to use as the end of the buffer for a lexer. |
| 250 | /// |
| 251 | /// If the file ends with a newline, form the EOF token on the newline itself, |
| 252 | /// rather than "on the line following it", which doesn't exist. This makes |
| 253 | /// diagnostics relating to the end of file include the last file that the user |
| 254 | /// actually typed, which is goodness. |
| 255 | const char *Preprocessor::getCurLexerEndPos() { |
| 256 | const char *EndPos = CurLexer->BufferEnd; |
| 257 | if (EndPos != CurLexer->BufferStart && |
| 258 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 259 | --EndPos; |
| 260 | |
| 261 | // Handle \n\r and \r\n: |
| 262 | if (EndPos != CurLexer->BufferStart && |
| 263 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 264 | EndPos[-1] != EndPos[0]) |
| 265 | --EndPos; |
| 266 | } |
| 267 | |
| 268 | return EndPos; |
| 269 | } |
| 270 | |
| 271 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 272 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 273 | /// the current file. This either returns the EOF token or pops a level off |
| 274 | /// the include stack and keeps going. |
| 275 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 276 | assert(!CurTokenLexer && |
| 277 | "Ending a file when currently in a macro!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 279 | // See if this file had a controlling macro. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 280 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 282 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 283 | // Okay, this has a controlling macro, remember in HeaderFileInfo. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | if (const FileEntry *FE = |
Richard Trieu | 671538e | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 285 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 286 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 287 | if (MacroInfo *MI = |
| 288 | getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) { |
| 289 | MI->UsedForHeaderGuard = true; |
| 290 | } |
Richard Trieu | 671538e | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 291 | if (const IdentifierInfo *DefinedMacro = |
| 292 | CurPPLexer->MIOpt.GetDefinedMacro()) { |
| 293 | if (!ControllingMacro->hasMacroDefinition() && |
| 294 | DefinedMacro != ControllingMacro && |
| 295 | HeaderInfo.FirstTimeLexingFile(FE)) { |
Ismail Pazarbasi | 223e7a8 | 2013-10-12 23:17:37 +0000 | [diff] [blame] | 296 | |
| 297 | // If the edit distance between the two macros is more than 50%, |
| 298 | // DefinedMacro may not be header guard, or can be header guard of |
| 299 | // another header file. Therefore, it maybe defining something |
| 300 | // completely different. This can be observed in the wild when |
| 301 | // handling feature macros or header guards in different files. |
| 302 | |
| 303 | const StringRef ControllingMacroName = ControllingMacro->getName(); |
| 304 | const StringRef DefinedMacroName = DefinedMacro->getName(); |
| 305 | const size_t MaxHalfLength = std::max(ControllingMacroName.size(), |
| 306 | DefinedMacroName.size()) / 2; |
| 307 | const unsigned ED = ControllingMacroName.edit_distance( |
| 308 | DefinedMacroName, true, MaxHalfLength); |
| 309 | if (ED <= MaxHalfLength) { |
| 310 | // Emit a warning for a bad header guard. |
| 311 | Diag(CurPPLexer->MIOpt.GetMacroLocation(), |
| 312 | diag::warn_header_guard) |
| 313 | << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro; |
| 314 | Diag(CurPPLexer->MIOpt.GetDefinedLocation(), |
| 315 | diag::note_header_guard) |
| 316 | << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro |
| 317 | << ControllingMacro |
| 318 | << FixItHint::CreateReplacement( |
| 319 | CurPPLexer->MIOpt.GetDefinedLocation(), |
| 320 | ControllingMacro->getName()); |
| 321 | } |
Richard Trieu | 671538e | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | } |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | |
John McCall | 3eff321 | 2011-10-18 00:44:04 +0000 | [diff] [blame] | 328 | // Complain about reaching a true EOF within arc_cf_code_audited. |
| 329 | // We don't want to complain about reaching the end of a macro |
| 330 | // instantiation or a _Pragma. |
| 331 | if (PragmaARCCFCodeAuditedLoc.isValid() && |
John McCall | d80d90d | 2011-10-18 01:36:41 +0000 | [diff] [blame] | 332 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
John McCall | 8dfac0b | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 333 | Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited); |
| 334 | |
| 335 | // Recover by leaving immediately. |
| 336 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 337 | } |
| 338 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 339 | // If this is a #include'd file, pop it off the include stack and continue |
| 340 | // lexing the #includer file. |
| 341 | if (!IncludeMacroStack.empty()) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 342 | |
| 343 | // If we lexed the code-completion file, act as if we reached EOF. |
| 344 | if (isCodeCompletionEnabled() && CurPPLexer && |
| 345 | SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) == |
| 346 | CodeCompletionFileLoc) { |
| 347 | if (CurLexer) { |
| 348 | Result.startToken(); |
| 349 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 350 | CurLexer.reset(); |
| 351 | } else { |
| 352 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
| 353 | CurPTHLexer->getEOF(Result); |
| 354 | CurPTHLexer.reset(); |
| 355 | } |
| 356 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 357 | CurPPLexer = nullptr; |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
Argyrios Kyrtzidis | d9d2b67 | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 361 | if (!isEndOfMacro && CurPPLexer && |
| 362 | SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) { |
| 363 | // Notify SourceManager to record the number of FileIDs that were created |
| 364 | // during lexing of the #include'd file. |
| 365 | unsigned NumFIDs = |
| 366 | SourceMgr.local_sloc_entry_size() - |
| 367 | CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/; |
| 368 | SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); |
| 369 | } |
| 370 | |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 371 | FileID ExitedFID; |
| 372 | if (Callbacks && !isEndOfMacro && CurPPLexer) |
| 373 | ExitedFID = CurPPLexer->getFileID(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 374 | |
| 375 | bool LeavingSubmodule = CurSubmodule && CurLexer; |
| 376 | if (LeavingSubmodule) { |
| 377 | // Notify the parser that we've left the module. |
| 378 | const char *EndPos = getCurLexerEndPos(); |
| 379 | Result.startToken(); |
| 380 | CurLexer->BufferPtr = EndPos; |
| 381 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 382 | Result.setAnnotationEndLoc(Result.getLocation()); |
| 383 | Result.setAnnotationValue(CurSubmodule); |
| 384 | } |
| 385 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 386 | // We're done with the #included file. |
| 387 | RemoveTopOfLexerStack(); |
| 388 | |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 389 | // Propagate info about start-of-line/leading white-space/etc. |
| 390 | PropagateLineStartLeadingSpaceInfo(Result); |
| 391 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 392 | // 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] | 393 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 394 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 8c61b53 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 395 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); |
| 396 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
Argyrios Kyrtzidis | c892c5f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 397 | PPCallbacks::ExitFile, FileType, ExitedFID); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 400 | // Client should lex another token unless we generated an EOM. |
| 401 | return LeavingSubmodule; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 404 | // If this is the end of the main file, form an EOF token. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 405 | if (CurLexer) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 406 | const char *EndPos = getCurLexerEndPos(); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 407 | Result.startToken(); |
| 408 | CurLexer->BufferPtr = EndPos; |
| 409 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Argyrios Kyrtzidis | bb06b50 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 411 | if (isCodeCompletionEnabled()) { |
| 412 | // Inserting the code-completion point increases the source buffer by 1, |
| 413 | // but the main FileID was created before inserting the point. |
| 414 | // Compensate by reducing the EOF location by 1, otherwise the location |
| 415 | // will point to the next FileID. |
| 416 | // FIXME: This is hacky, the code-completion point should probably be |
| 417 | // inserted before the main FileID is created. |
| 418 | if (CurLexer->getFileLoc() == CodeCompletionFileLoc) |
| 419 | Result.setLocation(Result.getLocation().getLocWithOffset(-1)); |
| 420 | } |
| 421 | |
Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 422 | if (!isIncrementalProcessingEnabled()) |
| 423 | // We're done with lexing. |
| 424 | CurLexer.reset(); |
Chris Lattner | 6711608 | 2009-02-13 23:06:48 +0000 | [diff] [blame] | 425 | } else { |
| 426 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
Ted Kremenek | 59d08cb | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 427 | CurPTHLexer->getEOF(Result); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 428 | CurPTHLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 429 | } |
Axel Naumann | e55329d | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 430 | |
| 431 | if (!isIncrementalProcessingEnabled()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 432 | CurPPLexer = nullptr; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 433 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 434 | if (TUKind == TU_Complete) { |
| 435 | // This is the end of the top-level file. 'WarnUnusedMacroLocs' has |
| 436 | // collected all macro locations that we need to warn because they are not |
| 437 | // used. |
| 438 | for (WarnUnusedMacroLocsTy::iterator |
| 439 | I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); |
| 440 | I!=E; ++I) |
| 441 | Diag(*I, diag::pp_macro_not_used); |
| 442 | } |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 444 | // If we are building a module that has an umbrella header, make sure that |
| 445 | // each of the headers within the directory covered by the umbrella header |
| 446 | // was actually included by the umbrella header. |
| 447 | if (Module *Mod = getCurrentModule()) { |
| 448 | if (Mod->getUmbrellaHeader()) { |
| 449 | SourceLocation StartLoc |
| 450 | = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 451 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 452 | if (!getDiagnostics().isIgnored(diag::warn_uncovered_module_header, |
| 453 | StartLoc)) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 454 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 455 | const DirectoryEntry *Dir = Mod->getUmbrellaDir(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 456 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
| 457 | std::error_code EC; |
| 458 | for (vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End; |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 459 | Entry != End && !EC; Entry.increment(EC)) { |
| 460 | using llvm::StringSwitch; |
| 461 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 462 | // Check whether this entry has an extension typically associated with |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 463 | // headers. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 464 | if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName())) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 465 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 466 | .Default(false)) |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 467 | continue; |
| 468 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 469 | if (const FileEntry *Header = |
| 470 | getFileManager().getFile(Entry->getName())) |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 471 | if (!getSourceManager().hasFileInfo(Header)) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 472 | if (!ModMap.isHeaderInUnavailableModule(Header)) { |
| 473 | // Find the relative path that would access this header. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 474 | SmallString<128> RelativePath; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 475 | computeRelativePath(FileMgr, Dir, Header, RelativePath); |
| 476 | Diag(StartLoc, diag::warn_uncovered_module_header) |
Douglas Gregor | c9c3907 | 2013-01-04 18:58:28 +0000 | [diff] [blame] | 477 | << Mod->getFullModuleName() << RelativePath; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 478 | } |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
Douglas Gregor | 58ea48d | 2013-05-20 13:49:41 +0000 | [diff] [blame] | 483 | |
| 484 | // Check whether there are any headers that were included, but not |
| 485 | // mentioned at all in the module map. Such headers |
| 486 | SourceLocation StartLoc |
| 487 | = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 488 | if (!getDiagnostics().isIgnored(diag::warn_forgotten_module_header, |
| 489 | StartLoc)) { |
Douglas Gregor | 58ea48d | 2013-05-20 13:49:41 +0000 | [diff] [blame] | 490 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 491 | for (unsigned I = 0, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) { |
| 492 | // We only care about file entries. |
| 493 | const SrcMgr::SLocEntry &Entry = SourceMgr.getLocalSLocEntry(I); |
| 494 | if (!Entry.isFile()) |
| 495 | continue; |
| 496 | |
| 497 | // Dig out the actual file. |
| 498 | const FileEntry *File = Entry.getFile().getContentCache()->OrigEntry; |
| 499 | if (!File) |
| 500 | continue; |
| 501 | |
| 502 | // If it's not part of a module and not unknown, complain. |
| 503 | if (!ModMap.findModuleForHeader(File) && |
| 504 | !ModMap.isHeaderInUnavailableModule(File)) { |
| 505 | Diag(StartLoc, diag::warn_forgotten_module_header) |
| 506 | << File->getName() << Mod->getFullModuleName(); |
| 507 | } |
| 508 | } |
| 509 | } |
Douglas Gregor | 585ec93 | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 510 | } |
Douglas Gregor | 58ea48d | 2013-05-20 13:49:41 +0000 | [diff] [blame] | 511 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 512 | return true; |
| 513 | } |
| 514 | |
| 515 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 516 | /// hits the end of its token stream. |
| 517 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 518 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 519 | "Ending a macro when currently in a #include file!"); |
| 520 | |
Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 521 | if (!MacroExpandingLexersStack.empty() && |
| 522 | MacroExpandingLexersStack.back().first == CurTokenLexer.get()) |
| 523 | removeCachedMacroExpandedTokensOfLastLexer(); |
| 524 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 525 | // Delete or cache the now-dead macro expander. |
| 526 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 527 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 528 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 529 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.release(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 530 | |
| 531 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 532 | return HandleEndOfFile(Result, true); |
| 533 | } |
| 534 | |
| 535 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 536 | /// lexer stack. This should only be used in situations where the current |
| 537 | /// state of the top-of-stack lexer is unknown. |
| 538 | void Preprocessor::RemoveTopOfLexerStack() { |
| 539 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 541 | if (CurTokenLexer) { |
| 542 | // Delete or cache the now-dead macro expander. |
| 543 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | caaa7df | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 544 | CurTokenLexer.reset(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 545 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 546 | TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Ted Kremenek | ed04c4c | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 549 | PopIncludeMacroStack(); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 553 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 554 | /// state, returning the token on the next source line. |
| 555 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 556 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 557 | "Pasted comment can only be formed from macro"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 559 | // We handle this by scanning for the closest real lexer, switching it to |
| 560 | // 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] | 561 | // explicit EOD token. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 562 | PreprocessorLexer *FoundLexer = nullptr; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 563 | bool LexerWasInPPMode = false; |
| 564 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 565 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 566 | if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 568 | // 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] | 569 | // expansions) and preprocessor mode (return EOD). We know that the lexer |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 570 | // was *not* in raw mode before, because the macro that the comment came |
| 571 | // from was expanded. However, it could have already been in preprocessor |
| 572 | // 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] | 573 | // return EOD. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 574 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 575 | FoundLexer->LexingRawMode = true; |
| 576 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 577 | FoundLexer->ParsingPreprocessorDirective = true; |
| 578 | break; |
| 579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 581 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 582 | // lexer. In either case, finish off the macro the comment came from, getting |
| 583 | // the next token. |
| 584 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 585 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 586 | // 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] | 587 | // out' the rest of the line, including any tokens that came from other macros |
| 588 | // that were active, as in: |
| 589 | // #define submacro a COMMENT b |
| 590 | // submacro c |
| 591 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 592 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 593 | Lex(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 595 | // If we got an eod token, then we successfully found the end of the line. |
| 596 | if (Tok.is(tok::eod)) { |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 597 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 598 | // Restore the lexer back to normal mode instead of raw mode. |
| 599 | FoundLexer->LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 601 | // 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] | 602 | // to finish the preprocessor line. |
| 603 | if (LexerWasInPPMode) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 605 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 606 | FoundLexer->ParsingPreprocessorDirective = false; |
| 607 | return Lex(Tok); |
| 608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 610 | // If we got an EOF token, then we reached the end of the token stream but |
| 611 | // 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] | 612 | // 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] | 613 | // preprocessor directive mode), so just return EOF as our token. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 614 | assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); |
Chris Lattner | 8c32b1a | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 615 | } |