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