blob: e824320cf732a79f167ec36f6e70d192903e01da [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"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Lex/LexDiagnostic.h"
Douglas Gregoraa93a872011-10-17 15:32:29 +000019#include "clang/Basic/FileManager.h"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000020#include "clang/Basic/SourceManager.h"
Douglas Gregor585ec932011-12-23 00:23:59 +000021#include "llvm/Support/FileSystem.h"
Ted Kremenek15ba2af2008-11-20 07:56:33 +000022#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor585ec932011-12-23 00:23:59 +000023#include "llvm/Support/PathV2.h"
24#include "llvm/ADT/StringSwitch.h"
Chris Lattner8c32b1a2008-03-09 04:10:46 +000025using namespace clang;
26
Ted Kremenek4b391082008-11-18 01:33:13 +000027PPCallbacks::~PPCallbacks() {}
Chris Lattner8c32b1a2008-03-09 04:10:46 +000028
29//===----------------------------------------------------------------------===//
Chris Lattner6b884502008-03-10 06:06:04 +000030// Miscellaneous Methods.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner8c32b1a2008-03-09 04:10:46 +000033/// isInPrimaryFile - Return true if we're in the top-level file, not in a
James Dennett1a5f9002012-06-22 05:36:05 +000034/// \#include. This looks through macro expansions and active _Pragma lexers.
Chris Lattner8c32b1a2008-03-09 04:10:46 +000035bool Preprocessor::isInPrimaryFile() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000036 if (IsFileLexer())
Chris Lattner8c32b1a2008-03-09 04:10:46 +000037 return IncludeMacroStack.empty();
Mike Stump1eb44332009-09-09 15:08:12 +000038
Chris Lattner8c32b1a2008-03-09 04:10:46 +000039 // If there are any stacked lexers, we're in a #include.
Ted Kremenek81d24e12008-11-20 16:19:53 +000040 assert(IsFileLexer(IncludeMacroStack[0]) &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +000041 "Top level include stack isn't our primary lexer?");
42 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Ted Kremenek81d24e12008-11-20 16:19:53 +000043 if (IsFileLexer(IncludeMacroStack[i]))
Chris Lattner8c32b1a2008-03-09 04:10:46 +000044 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 Kremenek68e48e42008-11-20 01:49:44 +000051PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
Ted Kremenek81d24e12008-11-20 16:19:53 +000052 if (IsFileLexer())
Ted Kremenek68e48e42008-11-20 01:49:44 +000053 return CurPPLexer;
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattner8c32b1a2008-03-09 04:10:46 +000055 // Look for a stacked lexer.
56 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Ted Kremenek68e48e42008-11-20 01:49:44 +000057 const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
Ted Kremenek81d24e12008-11-20 16:19:53 +000058 if (IsFileLexer(ISI))
Ted Kremenek68e48e42008-11-20 01:49:44 +000059 return ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +000060 }
61 return 0;
62}
63
Chris Lattner6b884502008-03-10 06:06:04 +000064
65//===----------------------------------------------------------------------===//
66// Methods for Entering and Callbacks for leaving various contexts
67//===----------------------------------------------------------------------===//
Chris Lattner8c32b1a2008-03-09 04:10:46 +000068
69/// EnterSourceFile - Add a source file to the top of the include stack and
Nuno Lopese7f2cbd2009-11-29 17:07:16 +000070/// start lexing tokens from it instead of the current buffer.
Chris Lattnere127a0d2010-04-20 20:35:58 +000071void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
72 SourceLocation Loc) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +000073 assert(CurTokenLexer == 0 && "Cannot #include a file inside a macro!");
74 ++NumEnteredSourceFiles;
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner8c32b1a2008-03-09 04:10:46 +000076 if (MaxIncludeStackDepth < IncludeMacroStack.size())
77 MaxIncludeStackDepth = IncludeMacroStack.size();
78
Ted Kremenek6137dc92008-12-02 19:46:31 +000079 if (PTH) {
Chris Lattner6e290142009-11-30 04:18:44 +000080 if (PTHLexer *PL = PTH->CreateLexer(FID)) {
81 EnterSourceFileWithPTH(PL, CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +000082 return;
Chris Lattner6e290142009-11-30 04:18:44 +000083 }
Ted Kremenek6137dc92008-12-02 19:46:31 +000084 }
Chris Lattner6e290142009-11-30 04:18:44 +000085
86 // Get the MemoryBuffer for this FID, if it fails, we fail.
Douglas Gregoraae58b02010-03-16 20:01:30 +000087 bool Invalid = false;
Chris Lattnere127a0d2010-04-20 20:35:58 +000088 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 Kyrtzidis7d100872011-09-04 03:32:15 +000096
97 if (isCodeCompletionEnabled() &&
98 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
99 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
100 CodeCompletionLoc =
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +0000101 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000102 }
103
Chris Lattner6e290142009-11-30 04:18:44 +0000104 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000105 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000106}
Chris Lattner72181832008-09-26 20:12:23 +0000107
Ted Kremenek6137dc92008-12-02 19:46:31 +0000108/// 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 Stump1eb44332009-09-09 15:08:12 +0000110void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000111 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000113 // Add the current lexer to the include stack.
Ted Kremenek41938c82008-11-19 21:57:25 +0000114 if (CurPPLexer || CurTokenLexer)
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000115 PushIncludeMacroStack();
116
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000117 CurLexer.reset(TheLexer);
Ted Kremenek9c1b7502008-11-18 00:12:49 +0000118 CurPPLexer = TheLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000119 CurDirLookup = CurDir;
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000120 if (CurLexerKind != CLK_LexAfterModuleImport)
121 CurLexerKind = CLK_Lexer;
122
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000123 // Notify the client, if desired, that we are in a new source file.
124 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000125 SrcMgr::CharacteristicKind FileType =
Chris Lattner0b9e7362008-09-26 21:18:42 +0000126 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000128 Callbacks->FileChanged(CurLexer->getFileLoc(),
129 PPCallbacks::EnterFile, FileType);
130 }
131}
132
Ted Kremenek6137dc92008-12-02 19:46:31 +0000133/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
134/// and start getting tokens from it using the PTH cache.
Mike Stump1eb44332009-09-09 15:08:12 +0000135void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Ted Kremenek6137dc92008-12-02 19:46:31 +0000136 const DirectoryLookup *CurDir) {
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek6137dc92008-12-02 19:46:31 +0000138 if (CurPPLexer || CurTokenLexer)
139 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000140
Ted Kremenek6137dc92008-12-02 19:46:31 +0000141 CurDirLookup = CurDir;
142 CurPTHLexer.reset(PL);
143 CurPPLexer = CurPTHLexer.get();
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000144 if (CurLexerKind != CLK_LexAfterModuleImport)
145 CurLexerKind = CLK_PTHLexer;
146
Ted Kremenek6137dc92008-12-02 19:46:31 +0000147 // Notify the client, if desired, that we are in a new source file.
148 if (Callbacks) {
Chris Lattner2b2453a2009-01-17 06:22:33 +0000149 FileID FID = CurPPLexer->getFileID();
Chris Lattner8c61b532009-01-19 08:01:53 +0000150 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
151 SrcMgr::CharacteristicKind FileType =
152 SourceMgr.getFileCharacteristic(EnterLoc);
153 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenek6137dc92008-12-02 19:46:31 +0000154 }
155}
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000156
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 Lattnere7fb4842009-02-15 20:52:18 +0000159void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
160 MacroArgs *Args) {
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000161 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000162 CurDirLookup = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000164 if (NumCachedTokenLexers == 0) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000165 CurTokenLexer.reset(new TokenLexer(Tok, ILEnd, Args, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000166 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000167 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000168 CurTokenLexer->Init(Tok, ILEnd, Args);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000169 }
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000170 if (CurLexerKind != CLK_LexAfterModuleImport)
171 CurLexerKind = CLK_TokenLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000172}
173
174/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner6b884502008-03-10 06:06:04 +0000175/// which will cause the lexer to start returning the specified tokens.
176///
177/// If DisableMacroExpansion is true, tokens lexed from the token stream will
178/// not be subject to further macro expansion. Otherwise, these tokens will
179/// be re-macro-expanded when/if expansion is enabled.
180///
181/// If OwnsTokens is false, this method assumes that the specified stream of
182/// tokens has a permanent owner somewhere, so they do not need to be copied.
183/// If it is true, it assumes the array of tokens is allocated with new[] and
184/// must be freed.
185///
186void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
187 bool DisableMacroExpansion,
188 bool OwnsTokens) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000189 // Save our current state.
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000190 PushIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000191 CurDirLookup = 0;
192
193 // Create a macro expander to expand from the specified token stream.
194 if (NumCachedTokenLexers == 0) {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000195 CurTokenLexer.reset(new TokenLexer(Toks, NumToks, DisableMacroExpansion,
196 OwnsTokens, *this));
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000197 } else {
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000198 CurTokenLexer.reset(TokenLexerCache[--NumCachedTokenLexers]);
Chris Lattner6b884502008-03-10 06:06:04 +0000199 CurTokenLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000200 }
Douglas Gregorb8db7cd2011-09-07 23:11:54 +0000201 if (CurLexerKind != CLK_LexAfterModuleImport)
202 CurLexerKind = CLK_TokenLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000203}
204
Douglas Gregor585ec932011-12-23 00:23:59 +0000205/// \brief Compute the relative path that names the given file relative to
206/// the given directory.
207static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir,
208 const FileEntry *File,
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000209 SmallString<128> &Result) {
Douglas Gregor585ec932011-12-23 00:23:59 +0000210 Result.clear();
211
212 StringRef FilePath = File->getDir()->getName();
213 StringRef Path = FilePath;
214 while (!Path.empty()) {
215 if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) {
216 if (CurDir == Dir) {
217 Result = FilePath.substr(Path.size());
218 llvm::sys::path::append(Result,
219 llvm::sys::path::filename(File->getName()));
220 return;
221 }
222 }
223
224 Path = llvm::sys::path::parent_path(Path);
225 }
226
227 Result = File->getName();
228}
229
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000230/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
231/// the current file. This either returns the EOF token or pops a level off
232/// the include stack and keeps going.
233bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
234 assert(!CurTokenLexer &&
235 "Ending a file when currently in a macro!");
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000237 // See if this file had a controlling macro.
Ted Kremenek1a531572008-11-19 22:43:49 +0000238 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump1eb44332009-09-09 15:08:12 +0000239 if (const IdentifierInfo *ControllingMacro =
Ted Kremenek1a531572008-11-19 22:43:49 +0000240 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff83d63c72009-04-24 20:03:17 +0000241 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump1eb44332009-09-09 15:08:12 +0000242 if (const FileEntry *FE =
Douglas Gregord83d2e72011-12-12 18:47:39 +0000243 SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000244 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
245 }
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
John McCall3eff3212011-10-18 00:44:04 +0000248 // Complain about reaching a true EOF within arc_cf_code_audited.
249 // We don't want to complain about reaching the end of a macro
250 // instantiation or a _Pragma.
251 if (PragmaARCCFCodeAuditedLoc.isValid() &&
John McCalld80d90d2011-10-18 01:36:41 +0000252 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
John McCall8dfac0b2011-09-30 05:12:12 +0000253 Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
254
255 // Recover by leaving immediately.
256 PragmaARCCFCodeAuditedLoc = SourceLocation();
257 }
258
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000259 // If this is a #include'd file, pop it off the include stack and continue
260 // lexing the #includer file.
261 if (!IncludeMacroStack.empty()) {
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000262
263 // If we lexed the code-completion file, act as if we reached EOF.
264 if (isCodeCompletionEnabled() && CurPPLexer &&
265 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
266 CodeCompletionFileLoc) {
267 if (CurLexer) {
268 Result.startToken();
269 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
270 CurLexer.reset();
271 } else {
272 assert(CurPTHLexer && "Got EOF but no current lexer set!");
273 CurPTHLexer->getEOF(Result);
274 CurPTHLexer.reset();
275 }
276
277 CurPPLexer = 0;
278 return true;
279 }
280
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000281 if (!isEndOfMacro && CurPPLexer &&
282 SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
283 // Notify SourceManager to record the number of FileIDs that were created
284 // during lexing of the #include'd file.
285 unsigned NumFIDs =
286 SourceMgr.local_sloc_entry_size() -
287 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
288 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
289 }
290
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000291 FileID ExitedFID;
292 if (Callbacks && !isEndOfMacro && CurPPLexer)
293 ExitedFID = CurPPLexer->getFileID();
294
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000295 // We're done with the #included file.
296 RemoveTopOfLexerStack();
297
298 // Notify the client, if desired, that we are in a new source file.
Ted Kremenek1a531572008-11-19 22:43:49 +0000299 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner9d728512008-10-27 01:19:25 +0000300 SrcMgr::CharacteristicKind FileType =
Chris Lattner8c61b532009-01-19 08:01:53 +0000301 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
302 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Argyrios Kyrtzidisc892c5f2011-10-11 17:29:44 +0000303 PPCallbacks::ExitFile, FileType, ExitedFID);
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000304 }
305
306 // Client should lex another token.
307 return false;
308 }
309
310 // If the file ends with a newline, form the EOF token on the newline itself,
311 // rather than "on the line following it", which doesn't exist. This makes
312 // diagnostics relating to the end of file include the last file that the user
313 // actually typed, which is goodness.
Ted Kremenek1a531572008-11-19 22:43:49 +0000314 if (CurLexer) {
315 const char *EndPos = CurLexer->BufferEnd;
Mike Stump1eb44332009-09-09 15:08:12 +0000316 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000317 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000318 --EndPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek1a531572008-11-19 22:43:49 +0000320 // Handle \n\r and \r\n:
Mike Stump1eb44332009-09-09 15:08:12 +0000321 if (EndPos != CurLexer->BufferStart &&
Ted Kremenek1a531572008-11-19 22:43:49 +0000322 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
323 EndPos[-1] != EndPos[0])
324 --EndPos;
325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenek1a531572008-11-19 22:43:49 +0000327 Result.startToken();
328 CurLexer->BufferPtr = EndPos;
329 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Axel Naumanne55329d2012-03-16 10:40:17 +0000331 if (!isIncrementalProcessingEnabled())
332 // We're done with lexing.
333 CurLexer.reset();
Chris Lattner67116082009-02-13 23:06:48 +0000334 } else {
335 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek59d08cb2008-12-23 19:24:24 +0000336 CurPTHLexer->getEOF(Result);
Ted Kremenek1a531572008-11-19 22:43:49 +0000337 CurPTHLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000338 }
Axel Naumanne55329d2012-03-16 10:40:17 +0000339
340 if (!isIncrementalProcessingEnabled())
341 CurPPLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000342
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +0000343 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has collected
344 // all macro locations that we need to warn because they are not used.
345 for (WarnUnusedMacroLocsTy::iterator
346 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); I!=E; ++I)
347 Diag(*I, diag::pp_macro_not_used);
Daniel Dunbardbd82092010-03-23 05:09:10 +0000348
Douglas Gregor585ec932011-12-23 00:23:59 +0000349 // If we are building a module that has an umbrella header, make sure that
350 // each of the headers within the directory covered by the umbrella header
351 // was actually included by the umbrella header.
352 if (Module *Mod = getCurrentModule()) {
353 if (Mod->getUmbrellaHeader()) {
354 SourceLocation StartLoc
355 = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
356
357 if (getDiagnostics().getDiagnosticLevel(
358 diag::warn_uncovered_module_header,
359 StartLoc) != DiagnosticsEngine::Ignored) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000360 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
Douglas Gregor224bfee2011-12-23 00:27:08 +0000361 typedef llvm::sys::fs::recursive_directory_iterator
Douglas Gregor585ec932011-12-23 00:23:59 +0000362 recursive_directory_iterator;
363 const DirectoryEntry *Dir = Mod->getUmbrellaDir();
364 llvm::error_code EC;
365 for (recursive_directory_iterator Entry(Dir->getName(), EC), End;
366 Entry != End && !EC; Entry.increment(EC)) {
367 using llvm::StringSwitch;
368
Douglas Gregor51f564f2011-12-31 04:05:44 +0000369 // Check whether this entry has an extension typically associated with
Douglas Gregor585ec932011-12-23 00:23:59 +0000370 // headers.
371 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
Douglas Gregor51f564f2011-12-31 04:05:44 +0000372 .Cases(".h", ".H", ".hh", ".hpp", true)
373 .Default(false))
Douglas Gregor585ec932011-12-23 00:23:59 +0000374 continue;
375
376 if (const FileEntry *Header = getFileManager().getFile(Entry->path()))
377 if (!getSourceManager().hasFileInfo(Header)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000378 if (!ModMap.isHeaderInUnavailableModule(Header)) {
379 // Find the relative path that would access this header.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000380 SmallString<128> RelativePath;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000381 computeRelativePath(FileMgr, Dir, Header, RelativePath);
382 Diag(StartLoc, diag::warn_uncovered_module_header)
383 << RelativePath;
384 }
Douglas Gregor585ec932011-12-23 00:23:59 +0000385 }
386 }
387 }
388 }
389 }
390
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000391 return true;
392}
393
394/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
395/// hits the end of its token stream.
396bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000397 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000398 "Ending a macro when currently in a #include file!");
399
Argyrios Kyrtzidis5b3284a2011-06-29 22:20:11 +0000400 if (!MacroExpandingLexersStack.empty() &&
401 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
402 removeCachedMacroExpandedTokensOfLastLexer();
403
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000404 // Delete or cache the now-dead macro expander.
405 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000406 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000407 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000408 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000409
410 // Handle this like a #include file being popped off the stack.
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000411 return HandleEndOfFile(Result, true);
412}
413
414/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
415/// lexer stack. This should only be used in situations where the current
416/// state of the top-of-stack lexer is unknown.
417void Preprocessor::RemoveTopOfLexerStack() {
418 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000420 if (CurTokenLexer) {
421 // Delete or cache the now-dead macro expander.
422 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000423 CurTokenLexer.reset();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000424 else
Ted Kremenekcaaa7df2008-11-13 17:11:24 +0000425 TokenLexerCache[NumCachedTokenLexers++] = CurTokenLexer.take();
Mike Stump1eb44332009-09-09 15:08:12 +0000426 }
427
Ted Kremeneked04c4c2008-11-13 16:51:03 +0000428 PopIncludeMacroStack();
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000429}
430
431/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
432/// comment (/##/) in microsoft mode, this method handles updating the current
433/// state, returning the token on the next source line.
434void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremenek1a531572008-11-19 22:43:49 +0000435 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000436 "Pasted comment can only be formed from macro");
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000438 // We handle this by scanning for the closest real lexer, switching it to
439 // raw mode and preprocessor mode. This will cause it to return \n as an
Peter Collingbourne84021552011-02-28 02:37:51 +0000440 // explicit EOD token.
Ted Kremenek1a531572008-11-19 22:43:49 +0000441 PreprocessorLexer *FoundLexer = 0;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000442 bool LexerWasInPPMode = false;
443 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
444 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Ted Kremenek1a531572008-11-19 22:43:49 +0000445 if (ISI.ThePPLexer == 0) continue; // Scan for a real lexer.
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000447 // Once we find a real lexer, mark it as raw mode (disabling macro
Peter Collingbourne84021552011-02-28 02:37:51 +0000448 // expansions) and preprocessor mode (return EOD). We know that the lexer
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000449 // was *not* in raw mode before, because the macro that the comment came
450 // from was expanded. However, it could have already been in preprocessor
451 // mode (#if COMMENT) in which case we have to return it to that mode and
Peter Collingbourne84021552011-02-28 02:37:51 +0000452 // return EOD.
Ted Kremenek1a531572008-11-19 22:43:49 +0000453 FoundLexer = ISI.ThePPLexer;
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000454 FoundLexer->LexingRawMode = true;
455 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
456 FoundLexer->ParsingPreprocessorDirective = true;
457 break;
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000460 // Okay, we either found and switched over the lexer, or we didn't find a
461 // lexer. In either case, finish off the macro the comment came from, getting
462 // the next token.
463 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Peter Collingbourne84021552011-02-28 02:37:51 +0000465 // Discarding comments as long as we don't have EOF or EOD. This 'comments
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000466 // out' the rest of the line, including any tokens that came from other macros
467 // that were active, as in:
468 // #define submacro a COMMENT b
469 // submacro c
470 // which should lex to 'a' only: 'b' and 'c' should be removed.
Peter Collingbourne84021552011-02-28 02:37:51 +0000471 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000472 Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Peter Collingbourne84021552011-02-28 02:37:51 +0000474 // If we got an eod token, then we successfully found the end of the line.
475 if (Tok.is(tok::eod)) {
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000476 assert(FoundLexer && "Can't get end of line without an active lexer");
477 // Restore the lexer back to normal mode instead of raw mode.
478 FoundLexer->LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Peter Collingbourne84021552011-02-28 02:37:51 +0000480 // If the lexer was already in preprocessor mode, just return the EOD token
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000481 // to finish the preprocessor line.
482 if (LexerWasInPPMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000484 // Otherwise, switch out of PP mode and return the next lexed token.
485 FoundLexer->ParsingPreprocessorDirective = false;
486 return Lex(Tok);
487 }
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000489 // If we got an EOF token, then we reached the end of the token stream but
490 // didn't find an explicit \n. This can only happen if there was no lexer
Peter Collingbourne84021552011-02-28 02:37:51 +0000491 // active (an active lexer would return EOD at EOF if there was no \n in
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000492 // preprocessor directive mode), so just return EOF as our token.
Peter Collingbourne84021552011-02-28 02:37:51 +0000493 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
Chris Lattner8c32b1a2008-03-09 04:10:46 +0000494}