blob: b51222cefd3bdcb9c1dbe822f2f7463d3b9221d6 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15// -H - Print the name of each header file used.
16// -d[MDNI] - Dump various things.
17// -fworking-directory - #line's with preprocessor's working dir.
18// -fpreprocessed
19// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20// -W*
21// -w
22//
23// Messages to emit:
24// "Multiple include guards may be useful for:\n"
25//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Lex/HeaderSearch.h"
30#include "clang/Lex/MacroInfo.h"
31#include "clang/Lex/PPCallbacks.h"
32#include "clang/Lex/Pragma.h"
33#include "clang/Lex/ScratchBuffer.h"
34#include "clang/Basic/Diagnostic.h"
35#include "clang/Basic/FileManager.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "llvm/ADT/SmallVector.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000039#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000040#include <iostream>
Chris Lattner77034d32007-09-03 18:30:32 +000041#include <ctime>
Reid Spencer5f016e22007-07-11 17:01:13 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
45
46Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
47 TargetInfo &target, SourceManager &SM,
48 HeaderSearch &Headers)
49 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
50 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts),
51 CurLexer(0), CurDirLookup(0), CurMacroExpander(0), Callbacks(0) {
52 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattner9594acf2007-07-15 00:25:26 +000053
Reid Spencer5f016e22007-07-11 17:01:13 +000054 // Clear stats.
55 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
56 NumIf = NumElse = NumEndif = 0;
57 NumEnteredSourceFiles = 0;
58 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
59 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
60 MaxIncludeStackDepth = 0;
61 NumSkipped = 0;
62
63 // Default to discarding comments.
64 KeepComments = false;
65 KeepMacroComments = false;
66
67 // Macro expansion is enabled.
68 DisableMacroExpansion = false;
69 InMacroArgs = false;
Chris Lattner9594acf2007-07-15 00:25:26 +000070 NumCachedMacroExpanders = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000071
72 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
73 // This gets unpoisoned where it is allowed.
74 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
75
76 // Initialize the pragma handlers.
77 PragmaHandlers = new PragmaNamespace(0);
78 RegisterBuiltinPragmas();
79
80 // Initialize builtin macros like __LINE__ and friends.
81 RegisterBuiltinMacros();
82}
83
84Preprocessor::~Preprocessor() {
85 // Free any active lexers.
86 delete CurLexer;
87
88 while (!IncludeMacroStack.empty()) {
89 delete IncludeMacroStack.back().TheLexer;
90 delete IncludeMacroStack.back().TheMacroExpander;
91 IncludeMacroStack.pop_back();
92 }
93
Chris Lattner9594acf2007-07-15 00:25:26 +000094 // Free any cached macro expanders.
95 for (unsigned i = 0, e = NumCachedMacroExpanders; i != e; ++i)
96 delete MacroExpanderCache[i];
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098 // Release pragma information.
99 delete PragmaHandlers;
100
101 // Delete the scratch buffer info.
102 delete ScratchBuf;
103}
104
105PPCallbacks::~PPCallbacks() {
106}
107
108/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
Chris Lattnerd2177732007-07-20 16:59:19 +0000109/// the specified Token's location, translating the token's start
Reid Spencer5f016e22007-07-11 17:01:13 +0000110/// position in the current buffer into a SourcePosition object for rendering.
111void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID) {
112 Diags.Report(Loc, DiagID);
113}
114
115void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
116 const std::string &Msg) {
117 Diags.Report(Loc, DiagID, &Msg, 1);
118}
119
Chris Lattnerd2177732007-07-20 16:59:19 +0000120void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
122 << getSpelling(Tok) << "'";
123
124 if (!DumpFlags) return;
125 std::cerr << "\t";
126 if (Tok.isAtStartOfLine())
127 std::cerr << " [StartOfLine]";
128 if (Tok.hasLeadingSpace())
129 std::cerr << " [LeadingSpace]";
130 if (Tok.isExpandDisabled())
131 std::cerr << " [ExpandDisabled]";
132 if (Tok.needsCleaning()) {
133 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
134 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
135 << "']";
136 }
137}
138
139void Preprocessor::DumpMacro(const MacroInfo &MI) const {
140 std::cerr << "MACRO: ";
141 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
142 DumpToken(MI.getReplacementToken(i));
143 std::cerr << " ";
144 }
145 std::cerr << "\n";
146}
147
148void Preprocessor::PrintStats() {
149 std::cerr << "\n*** Preprocessor Stats:\n";
150 std::cerr << NumDirectives << " directives found:\n";
151 std::cerr << " " << NumDefined << " #define.\n";
152 std::cerr << " " << NumUndefined << " #undef.\n";
153 std::cerr << " #include/#include_next/#import:\n";
154 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
155 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
156 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
157 std::cerr << " " << NumElse << " #else/#elif.\n";
158 std::cerr << " " << NumEndif << " #endif.\n";
159 std::cerr << " " << NumPragma << " #pragma.\n";
160 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
161
162 std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
163 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
164 << NumFastMacroExpanded << " on the fast path.\n";
165 std::cerr << (NumFastTokenPaste+NumTokenPaste)
166 << " token paste (##) operations performed, "
167 << NumFastTokenPaste << " on the fast path.\n";
168}
169
170//===----------------------------------------------------------------------===//
171// Token Spelling
172//===----------------------------------------------------------------------===//
173
174
175/// getSpelling() - Return the 'spelling' of this token. The spelling of a
176/// token are the characters used to represent the token in the source file
177/// after trigraph expansion and escaped-newline folding. In particular, this
178/// wants to get the true, uncanonicalized, spelling of things like digraphs
179/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000180std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
182
183 // If this token contains nothing interesting, return it directly.
184 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
185 if (!Tok.needsCleaning())
186 return std::string(TokStart, TokStart+Tok.getLength());
187
188 std::string Result;
189 Result.reserve(Tok.getLength());
190
191 // Otherwise, hard case, relex the characters into the string.
192 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
193 Ptr != End; ) {
194 unsigned CharSize;
195 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
196 Ptr += CharSize;
197 }
198 assert(Result.size() != unsigned(Tok.getLength()) &&
199 "NeedsCleaning flag set on something that didn't need cleaning!");
200 return Result;
201}
202
203/// getSpelling - This method is used to get the spelling of a token into a
204/// preallocated buffer, instead of as an std::string. The caller is required
205/// to allocate enough space for the token, which is guaranteed to be at least
206/// Tok.getLength() bytes long. The actual length of the token is returned.
207///
208/// Note that this method may do two possible things: it may either fill in
209/// the buffer specified with characters, or it may *change the input pointer*
210/// to point to a constant buffer with the data already in it (avoiding a
211/// copy). The caller is not allowed to modify the returned buffer pointer
212/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000213unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 const char *&Buffer) const {
215 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
216
217 // If this token is an identifier, just return the string from the identifier
218 // table, which is very quick.
219 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
220 Buffer = II->getName();
Chris Lattner0f670322007-07-22 22:50:09 +0000221
222 // Return the length of the token. If the token needed cleaning, don't
223 // include the size of the newlines or trigraphs in it.
224 if (!Tok.needsCleaning())
225 return Tok.getLength();
226 else
227 return strlen(Buffer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229
230 // Otherwise, compute the start of the token in the input lexer buffer.
231 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
232
233 // If this token contains nothing interesting, return it directly.
234 if (!Tok.needsCleaning()) {
235 Buffer = TokStart;
236 return Tok.getLength();
237 }
238 // Otherwise, hard case, relex the characters into the string.
239 char *OutBuf = const_cast<char*>(Buffer);
240 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
241 Ptr != End; ) {
242 unsigned CharSize;
243 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
244 Ptr += CharSize;
245 }
246 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
247 "NeedsCleaning flag set on something that didn't need cleaning!");
248
249 return OutBuf-Buffer;
250}
251
252
253/// CreateString - Plop the specified string into a scratch buffer and return a
254/// location for it. If specified, the source location provides a source
255/// location for the token.
256SourceLocation Preprocessor::
257CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
258 if (SLoc.isValid())
259 return ScratchBuf->getToken(Buf, Len, SLoc);
260 return ScratchBuf->getToken(Buf, Len);
261}
262
263
Chris Lattner97ba77c2007-07-16 06:48:38 +0000264/// AdvanceToTokenCharacter - Given a location that specifies the start of a
265/// token, return a new location that specifies a character within the token.
266SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
267 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000268 // If they request the first char of the token, we're trivially done. If this
269 // is a macro expansion, it doesn't make sense to point to a character within
270 // the instantiation point (the name). We could point to the source
271 // character, but without also pointing to instantiation info, this is
272 // confusing.
273 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000274
275 // Figure out how many physical characters away the specified logical
276 // character is. This needs to take into consideration newlines and
277 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000278 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
279 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000280
281 // The usual case is that tokens don't contain anything interesting. Skip
282 // over the uninteresting characters. If a token only consists of simple
283 // chars, this method is extremely fast.
284 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000285 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000286
287 // If we have a character that may be a trigraph or escaped newline, create a
288 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000289 if (CharNo != 0) {
290 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000291 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000292 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000293 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000294 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000295 for (; CharNo; --CharNo)
296 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000297
298 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000299 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000300
301 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000302}
303
304
305
Reid Spencer5f016e22007-07-11 17:01:13 +0000306//===----------------------------------------------------------------------===//
307// Source File Location Methods.
308//===----------------------------------------------------------------------===//
309
310/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
311/// return null on failure. isAngled indicates whether the file reference is
312/// for system #include's or not (i.e. using <> instead of "").
313const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
314 const char *FilenameEnd,
315 bool isAngled,
316 const DirectoryLookup *FromDir,
317 const DirectoryLookup *&CurDir) {
318 // If the header lookup mechanism may be relative to the current file, pass in
319 // info about where the current file is.
320 const FileEntry *CurFileEnt = 0;
321 if (!FromDir) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000322 SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
323 CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 }
325
326 // Do a standard file entry lookup.
327 CurDir = CurDirLookup;
328 const FileEntry *FE =
329 HeaderInfo.LookupFile(FilenameStart, FilenameEnd,
330 isAngled, FromDir, CurDir, CurFileEnt);
331 if (FE) return FE;
332
333 // Otherwise, see if this is a subframework header. If so, this is relative
334 // to one of the headers on the #include stack. Walk the list of the current
335 // headers on the #include stack and pass them to HeaderInfo.
336 if (CurLexer && !CurLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000337 CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
339 CurFileEnt)))
340 return FE;
341 }
342
343 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
344 IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
345 if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000346 CurFileEnt = SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
348 CurFileEnt)))
349 return FE;
350 }
351 }
352
353 // Otherwise, we really couldn't find the file.
354 return 0;
355}
356
357/// isInPrimaryFile - Return true if we're in the top-level file, not in a
358/// #include.
359bool Preprocessor::isInPrimaryFile() const {
360 if (CurLexer && !CurLexer->Is_PragmaLexer)
361 return CurLexer->isMainFile();
362
363 // If there are any stacked lexers, we're in a #include.
364 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
365 if (IncludeMacroStack[i].TheLexer &&
366 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
367 return IncludeMacroStack[i].TheLexer->isMainFile();
368 return false;
369}
370
371/// getCurrentLexer - Return the current file lexer being lexed from. Note
372/// that this ignores any potentially active macro expansions and _Pragma
373/// expansions going on at the time.
374Lexer *Preprocessor::getCurrentFileLexer() const {
375 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
376
377 // Look for a stacked lexer.
378 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
379 Lexer *L = IncludeMacroStack[i-1].TheLexer;
380 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
381 return L;
382 }
383 return 0;
384}
385
386
387/// EnterSourceFile - Add a source file to the top of the include stack and
388/// start lexing tokens from it instead of the current buffer. Return true
389/// on failure.
390void Preprocessor::EnterSourceFile(unsigned FileID,
391 const DirectoryLookup *CurDir,
392 bool isMainFile) {
393 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
394 ++NumEnteredSourceFiles;
395
396 if (MaxIncludeStackDepth < IncludeMacroStack.size())
397 MaxIncludeStackDepth = IncludeMacroStack.size();
398
Chris Lattner25bdb512007-07-20 16:52:03 +0000399 Lexer *TheLexer = new Lexer(SourceLocation::getFileLoc(FileID, 0), *this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 if (isMainFile) TheLexer->setIsMainFile();
401 EnterSourceFileWithLexer(TheLexer, CurDir);
402}
403
404/// EnterSourceFile - Add a source file to the top of the include stack and
405/// start lexing tokens from it instead of the current buffer.
406void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
407 const DirectoryLookup *CurDir) {
408
409 // Add the current lexer to the include stack.
410 if (CurLexer || CurMacroExpander)
411 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
412 CurMacroExpander));
413
414 CurLexer = TheLexer;
415 CurDirLookup = CurDir;
416 CurMacroExpander = 0;
417
418 // Notify the client, if desired, that we are in a new source file.
419 if (Callbacks && !CurLexer->Is_PragmaLexer) {
420 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
421
422 // Get the file entry for the current file.
423 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +0000424 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 FileType = HeaderInfo.getFileDirFlavor(FE);
426
Chris Lattner9dc1f532007-07-20 16:37:10 +0000427 Callbacks->FileChanged(CurLexer->getFileLoc(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 PPCallbacks::EnterFile, FileType);
429 }
430}
431
432
433
434/// EnterMacro - Add a Macro to the top of the include stack and start lexing
435/// tokens from it instead of the current buffer.
Chris Lattnerd2177732007-07-20 16:59:19 +0000436void Preprocessor::EnterMacro(Token &Tok, MacroArgs *Args) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
438 CurMacroExpander));
439 CurLexer = 0;
440 CurDirLookup = 0;
441
Chris Lattner9594acf2007-07-15 00:25:26 +0000442 if (NumCachedMacroExpanders == 0) {
443 CurMacroExpander = new MacroExpander(Tok, Args, *this);
444 } else {
445 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
446 CurMacroExpander->Init(Tok, Args);
447 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000448}
449
450/// EnterTokenStream - Add a "macro" context to the top of the include stack,
451/// which will cause the lexer to start returning the specified tokens. Note
452/// that these tokens will be re-macro-expanded when/if expansion is enabled.
453/// This method assumes that the specified stream of tokens has a permanent
454/// owner somewhere, so they do not need to be copied.
Chris Lattnerd2177732007-07-20 16:59:19 +0000455void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // Save our current state.
457 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
458 CurMacroExpander));
459 CurLexer = 0;
460 CurDirLookup = 0;
461
462 // Create a macro expander to expand from the specified token stream.
Chris Lattner9594acf2007-07-15 00:25:26 +0000463 if (NumCachedMacroExpanders == 0) {
464 CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
465 } else {
466 CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
467 CurMacroExpander->Init(Toks, NumToks);
468 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000469}
470
471/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
472/// lexer stack. This should only be used in situations where the current
473/// state of the top-of-stack lexer is known.
474void Preprocessor::RemoveTopOfLexerStack() {
475 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Chris Lattner9594acf2007-07-15 00:25:26 +0000476
477 if (CurMacroExpander) {
478 // Delete or cache the now-dead macro expander.
479 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
480 delete CurMacroExpander;
481 else
482 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
483 } else {
484 delete CurLexer;
485 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 CurLexer = IncludeMacroStack.back().TheLexer;
487 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
488 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
489 IncludeMacroStack.pop_back();
490}
491
492//===----------------------------------------------------------------------===//
493// Macro Expansion Handling.
494//===----------------------------------------------------------------------===//
495
496/// RegisterBuiltinMacro - Register the specified identifier in the identifier
497/// table and mark it as a builtin macro to be expanded.
498IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
499 // Get the identifier.
500 IdentifierInfo *Id = getIdentifierInfo(Name);
501
502 // Mark it as being a macro that is builtin.
503 MacroInfo *MI = new MacroInfo(SourceLocation());
504 MI->setIsBuiltinMacro();
505 Id->setMacroInfo(MI);
506 return Id;
507}
508
509
510/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
511/// identifier table.
512void Preprocessor::RegisterBuiltinMacros() {
513 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
514 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
515 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
516 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
517 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
518
519 // GCC Extensions.
520 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
521 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
522 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
523}
524
525/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
526/// in its expansion, currently expands to that token literally.
527static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
528 const IdentifierInfo *MacroIdent) {
529 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
530
531 // If the token isn't an identifier, it's always literally expanded.
532 if (II == 0) return true;
533
534 // If the identifier is a macro, and if that macro is enabled, it may be
535 // expanded so it's not a trivial expansion.
536 if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() &&
537 // Fast expanding "#define X X" is ok, because X would be disabled.
538 II != MacroIdent)
539 return false;
540
541 // If this is an object-like macro invocation, it is safe to trivially expand
542 // it.
543 if (MI->isObjectLike()) return true;
544
545 // If this is a function-like macro invocation, it's safe to trivially expand
546 // as long as the identifier is not a macro argument.
547 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
548 I != E; ++I)
549 if (*I == II)
550 return false; // Identifier is a macro argument.
551
552 return true;
553}
554
555
556/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
557/// lexed is a '('. If so, consume the token and return true, if not, this
558/// method should have no observable side-effect on the lexed tokens.
559bool Preprocessor::isNextPPTokenLParen() {
560 // Do some quick tests for rejection cases.
561 unsigned Val;
562 if (CurLexer)
563 Val = CurLexer->isNextPPTokenLParen();
564 else
565 Val = CurMacroExpander->isNextTokenLParen();
566
567 if (Val == 2) {
Chris Lattner0ea793e2007-07-19 00:07:36 +0000568 // We have run off the end. If it's a source file we don't
569 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
570 // macro stack.
571 if (CurLexer)
572 return false;
573 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
575 if (Entry.TheLexer)
576 Val = Entry.TheLexer->isNextPPTokenLParen();
577 else
578 Val = Entry.TheMacroExpander->isNextTokenLParen();
Chris Lattner0ea793e2007-07-19 00:07:36 +0000579
580 if (Val != 2)
581 break;
582
583 // Ran off the end of a source file?
584 if (Entry.TheLexer)
585 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 }
587 }
588
589 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
590 // have found something that isn't a '(' or we found the end of the
591 // translation unit. In either case, return false.
592 if (Val != 1)
593 return false;
594
Chris Lattnerd2177732007-07-20 16:59:19 +0000595 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 LexUnexpandedToken(Tok);
597 assert(Tok.getKind() == tok::l_paren && "Error computing l-paren-ness?");
598 return true;
599}
600
601/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
602/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000603bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 MacroInfo *MI) {
605
606 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
607 if (MI->isBuiltinMacro()) {
608 ExpandBuiltinMacro(Identifier);
609 return false;
610 }
611
612 // If this is the first use of a target-specific macro, warn about it.
613 if (MI->isTargetSpecific()) {
614 MI->setIsTargetSpecific(false); // Don't warn on second use.
615 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
616 diag::port_target_macro_use);
617 }
618
619 /// Args - If this is a function-like macro expansion, this contains,
620 /// for each macro argument, the list of tokens that were provided to the
621 /// invocation.
622 MacroArgs *Args = 0;
623
624 // If this is a function-like macro, read the arguments.
625 if (MI->isFunctionLike()) {
626 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000627 // name isn't a '(', this macro should not be expanded. Otherwise, consume
628 // it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 if (!isNextPPTokenLParen())
630 return true;
631
632 // Remember that we are now parsing the arguments to a macro invocation.
633 // Preprocessor directives used inside macro arguments are not portable, and
634 // this enables the warning.
635 InMacroArgs = true;
636 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
637
638 // Finished parsing args.
639 InMacroArgs = false;
640
641 // If there was an error parsing the arguments, bail out.
642 if (Args == 0) return false;
643
644 ++NumFnMacroExpanded;
645 } else {
646 ++NumMacroExpanded;
647 }
648
649 // Notice that this macro has been used.
650 MI->setIsUsed(true);
651
652 // If we started lexing a macro, enter the macro expansion body.
653
654 // If this macro expands to no tokens, don't bother to push it onto the
655 // expansion stack, only to take it right back off.
656 if (MI->getNumTokens() == 0) {
657 // No need for arg info.
658 if (Args) Args->destroy();
659
660 // Ignore this macro use, just return the next token in the current
661 // buffer.
662 bool HadLeadingSpace = Identifier.hasLeadingSpace();
663 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
664
665 Lex(Identifier);
666
667 // If the identifier isn't on some OTHER line, inherit the leading
668 // whitespace/first-on-a-line property of this token. This handles
669 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
670 // empty.
671 if (!Identifier.isAtStartOfLine()) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000672 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
673 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 }
675 ++NumFastMacroExpanded;
676 return false;
677
678 } else if (MI->getNumTokens() == 1 &&
679 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){
680 // Otherwise, if this macro expands into a single trivially-expanded
681 // token: expand it now. This handles common cases like
682 // "#define VAL 42".
683
684 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
685 // identifier to the expanded token.
686 bool isAtStartOfLine = Identifier.isAtStartOfLine();
687 bool hasLeadingSpace = Identifier.hasLeadingSpace();
688
689 // Remember where the token is instantiated.
690 SourceLocation InstantiateLoc = Identifier.getLocation();
691
692 // Replace the result token.
693 Identifier = MI->getReplacementToken(0);
694
695 // Restore the StartOfLine/LeadingSpace markers.
Chris Lattnerd2177732007-07-20 16:59:19 +0000696 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
697 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000698
699 // Update the tokens location to include both its logical and physical
700 // locations.
701 SourceLocation Loc =
702 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
703 Identifier.setLocation(Loc);
704
705 // If this is #define X X, we must mark the result as unexpandible.
706 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
707 if (NewII->getMacroInfo() == MI)
Chris Lattnerd2177732007-07-20 16:59:19 +0000708 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
710 // Since this is not an identifier token, it can't be macro expanded, so
711 // we're done.
712 ++NumFastMacroExpanded;
713 return false;
714 }
715
716 // Start expanding the macro.
717 EnterMacro(Identifier, Args);
718
719 // Now that the macro is at the top of the include stack, ask the
720 // preprocessor to read the next token from it.
721 Lex(Identifier);
722 return false;
723}
724
725/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
726/// invoked to read all of the actual arguments specified for the macro
727/// invocation. This returns null on error.
Chris Lattnerd2177732007-07-20 16:59:19 +0000728MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 MacroInfo *MI) {
730 // The number of fixed arguments to parse.
731 unsigned NumFixedArgsLeft = MI->getNumArgs();
732 bool isVariadic = MI->isVariadic();
733
734 // Outer loop, while there are more arguments, keep reading them.
Chris Lattnerd2177732007-07-20 16:59:19 +0000735 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 Tok.setKind(tok::comma);
737 --NumFixedArgsLeft; // Start reading the first arg.
738
739 // ArgTokens - Build up a list of tokens that make up each argument. Each
740 // argument is separated by an EOF token. Use a SmallVector so we can avoid
741 // heap allocations in the common case.
Chris Lattnerd2177732007-07-20 16:59:19 +0000742 llvm::SmallVector<Token, 64> ArgTokens;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
744 unsigned NumActuals = 0;
745 while (Tok.getKind() == tok::comma) {
Chris Lattner2b64fdc2007-07-19 16:11:58 +0000746 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
747 // that we already consumed the first one.
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 unsigned NumParens = 0;
749
750 while (1) {
751 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
752 // an argument value in a macro could expand to ',' or '(' or ')'.
753 LexUnexpandedToken(Tok);
754
755 if (Tok.getKind() == tok::eof) {
756 Diag(MacroName, diag::err_unterm_macro_invoc);
757 // Do not lose the EOF. Return it to the client.
758 MacroName = Tok;
759 return 0;
760 } else if (Tok.getKind() == tok::r_paren) {
761 // If we found the ) token, the macro arg list is done.
762 if (NumParens-- == 0)
763 break;
764 } else if (Tok.getKind() == tok::l_paren) {
765 ++NumParens;
766 } else if (Tok.getKind() == tok::comma && NumParens == 0) {
767 // Comma ends this argument if there are more fixed arguments expected.
768 if (NumFixedArgsLeft)
769 break;
770
771 // If this is not a variadic macro, too many args were specified.
772 if (!isVariadic) {
773 // Emit the diagnostic at the macro name in case there is a missing ).
774 // Emitting it at the , could be far away from the macro name.
775 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
776 return 0;
777 }
778 // Otherwise, continue to add the tokens to this variable argument.
779 } else if (Tok.getKind() == tok::comment && !KeepMacroComments) {
780 // If this is a comment token in the argument list and we're just in
781 // -C mode (not -CC mode), discard the comment.
782 continue;
783 }
784
785 ArgTokens.push_back(Tok);
786 }
787
788 // Empty arguments are standard in C99 and supported as an extension in
789 // other modes.
790 if (ArgTokens.empty() && !Features.C99)
791 Diag(Tok, diag::ext_empty_fnmacro_arg);
792
793 // Add a marker EOF token to the end of the token list for this argument.
Chris Lattnerd2177732007-07-20 16:59:19 +0000794 Token EOFTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 EOFTok.startToken();
796 EOFTok.setKind(tok::eof);
797 EOFTok.setLocation(Tok.getLocation());
798 EOFTok.setLength(0);
799 ArgTokens.push_back(EOFTok);
800 ++NumActuals;
801 --NumFixedArgsLeft;
802 };
803
804 // Okay, we either found the r_paren. Check to see if we parsed too few
805 // arguments.
806 unsigned MinArgsExpected = MI->getNumArgs();
807
808 // See MacroArgs instance var for description of this.
809 bool isVarargsElided = false;
810
811 if (NumActuals < MinArgsExpected) {
812 // There are several cases where too few arguments is ok, handle them now.
813 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
814 // Varargs where the named vararg parameter is missing: ok as extension.
815 // #define A(x, ...)
816 // A("blah")
817 Diag(Tok, diag::ext_missing_varargs_arg);
818
819 // Remember this occurred if this is a C99 macro invocation with at least
820 // one actual argument.
821 isVarargsElided = MI->isC99Varargs() && MI->getNumArgs() > 1;
822 } else if (MI->getNumArgs() == 1) {
823 // #define A(x)
824 // A()
825 // is ok because it is an empty argument.
826
827 // Empty arguments are standard in C99 and supported as an extension in
828 // other modes.
829 if (ArgTokens.empty() && !Features.C99)
830 Diag(Tok, diag::ext_empty_fnmacro_arg);
831 } else {
832 // Otherwise, emit the error.
833 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
834 return 0;
835 }
836
837 // Add a marker EOF token to the end of the token list for this argument.
838 SourceLocation EndLoc = Tok.getLocation();
839 Tok.startToken();
840 Tok.setKind(tok::eof);
841 Tok.setLocation(EndLoc);
842 Tok.setLength(0);
843 ArgTokens.push_back(Tok);
844 }
845
846 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
847}
848
849/// ComputeDATE_TIME - Compute the current time, enter it into the specified
850/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
851/// the identifier tokens inserted.
852static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
853 Preprocessor &PP) {
854 time_t TT = time(0);
855 struct tm *TM = localtime(&TT);
856
857 static const char * const Months[] = {
858 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
859 };
860
861 char TmpBuffer[100];
862 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
863 TM->tm_year+1900);
864 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
865
866 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
867 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
868}
869
870/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
871/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000872void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 // Figure out which token this is.
874 IdentifierInfo *II = Tok.getIdentifierInfo();
875 assert(II && "Can't be a macro without id info!");
876
877 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
878 // lex the token after it.
879 if (II == Ident_Pragma)
880 return Handle_Pragma(Tok);
881
882 ++NumBuiltinMacroExpanded;
883
884 char TmpBuffer[100];
885
886 // Set up the return result.
887 Tok.setIdentifierInfo(0);
Chris Lattnerd2177732007-07-20 16:59:19 +0000888 Tok.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 if (II == Ident__LINE__) {
891 // __LINE__ expands to a simple numeric value.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000892 sprintf(TmpBuffer, "%u", SourceMgr.getLogicalLineNumber(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 unsigned Length = strlen(TmpBuffer);
894 Tok.setKind(tok::numeric_constant);
895 Tok.setLength(Length);
896 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
897 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
898 SourceLocation Loc = Tok.getLocation();
899 if (II == Ident__BASE_FILE__) {
900 Diag(Tok, diag::ext_pp_base_file);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000901 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
902 while (NextLoc.isValid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 Loc = NextLoc;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000904 NextLoc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 }
906 }
907
908 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
Chris Lattner9dc1f532007-07-20 16:37:10 +0000909 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 FN = '"' + Lexer::Stringify(FN) + '"';
911 Tok.setKind(tok::string_literal);
912 Tok.setLength(FN.size());
913 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
914 } else if (II == Ident__DATE__) {
915 if (!DATELoc.isValid())
916 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
917 Tok.setKind(tok::string_literal);
918 Tok.setLength(strlen("\"Mmm dd yyyy\""));
919 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
920 } else if (II == Ident__TIME__) {
921 if (!TIMELoc.isValid())
922 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
923 Tok.setKind(tok::string_literal);
924 Tok.setLength(strlen("\"hh:mm:ss\""));
925 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
926 } else if (II == Ident__INCLUDE_LEVEL__) {
927 Diag(Tok, diag::ext_pp_include_level);
928
929 // Compute the include depth of this token.
930 unsigned Depth = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000931 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
932 for (; Loc.isValid(); ++Depth)
933 Loc = SourceMgr.getIncludeLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000934
935 // __INCLUDE_LEVEL__ expands to a simple numeric value.
936 sprintf(TmpBuffer, "%u", Depth);
937 unsigned Length = strlen(TmpBuffer);
938 Tok.setKind(tok::numeric_constant);
939 Tok.setLength(Length);
940 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
941 } else if (II == Ident__TIMESTAMP__) {
942 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
943 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
944 Diag(Tok, diag::ext_pp_timestamp);
945
946 // Get the file that we are lexing out of. If we're currently lexing from
947 // a macro, dig into the include stack.
948 const FileEntry *CurFile = 0;
949 Lexer *TheLexer = getCurrentFileLexer();
950
951 if (TheLexer)
Chris Lattner9dc1f532007-07-20 16:37:10 +0000952 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000953
954 // If this file is older than the file it depends on, emit a diagnostic.
955 const char *Result;
956 if (CurFile) {
957 time_t TT = CurFile->getModificationTime();
958 struct tm *TM = localtime(&TT);
959 Result = asctime(TM);
960 } else {
961 Result = "??? ??? ?? ??:??:?? ????\n";
962 }
963 TmpBuffer[0] = '"';
964 strcpy(TmpBuffer+1, Result);
965 unsigned Len = strlen(TmpBuffer);
966 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
967 Tok.setKind(tok::string_literal);
968 Tok.setLength(Len);
969 Tok.setLocation(CreateString(TmpBuffer, Len, Tok.getLocation()));
970 } else {
971 assert(0 && "Unknown identifier!");
972 }
973}
974
975//===----------------------------------------------------------------------===//
976// Lexer Event Handling.
977//===----------------------------------------------------------------------===//
978
979/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
980/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000981IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 const char *BufPtr) {
983 assert(Identifier.getKind() == tok::identifier && "Not an identifier!");
984 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
985
986 // Look up this token, see if it is a macro, or if it is a language keyword.
987 IdentifierInfo *II;
988 if (BufPtr && !Identifier.needsCleaning()) {
989 // No cleaning needed, just use the characters from the lexed buffer.
990 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
991 } else {
992 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +0000993 llvm::SmallVector<char, 64> IdentifierBuffer;
994 IdentifierBuffer.resize(Identifier.getLength());
995 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 unsigned Size = getSpelling(Identifier, TmpBuf);
997 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
998 }
999 Identifier.setIdentifierInfo(II);
1000 return II;
1001}
1002
1003
1004/// HandleIdentifier - This callback is invoked when the lexer reads an
1005/// identifier. This callback looks up the identifier in the map and/or
1006/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +00001007void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 assert(Identifier.getIdentifierInfo() &&
1009 "Can't handle identifiers without identifier info!");
1010
1011 IdentifierInfo &II = *Identifier.getIdentifierInfo();
1012
1013 // If this identifier was poisoned, and if it was not produced from a macro
1014 // expansion, emit an error.
1015 if (II.isPoisoned() && CurLexer) {
1016 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
1017 Diag(Identifier, diag::err_pp_used_poisoned_id);
1018 else
1019 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
1020 }
1021
1022 // If this is a macro to be expanded, do it.
1023 if (MacroInfo *MI = II.getMacroInfo()) {
1024 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
1025 if (MI->isEnabled()) {
1026 if (!HandleMacroExpandedIdentifier(Identifier, MI))
1027 return;
1028 } else {
1029 // C99 6.10.3.4p2 says that a disabled macro may never again be
1030 // expanded, even if it's in a context where it could be expanded in the
1031 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +00001032 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 }
1034 }
1035 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
1036 // If this identifier is a macro on some other target, emit a diagnostic.
1037 // This diagnosic is only emitted when macro expansion is enabled, because
1038 // the macro would not have been expanded for the other target either.
1039 II.setIsOtherTargetMacro(false); // Don't warn on second use.
1040 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
1041 diag::port_target_macro_use);
1042
1043 }
1044
1045 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
1046 // then we act as if it is the actual operator and not the textual
1047 // representation of it.
1048 if (II.isCPlusPlusOperatorKeyword())
1049 Identifier.setIdentifierInfo(0);
1050
1051 // Change the kind of this identifier to the appropriate token kind, e.g.
1052 // turning "for" into a keyword.
1053 Identifier.setKind(II.getTokenID());
1054
1055 // If this is an extension token, diagnose its use.
1056 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
1057 // For now, I'm just commenting it out (while I work on attributes).
1058 if (II.isExtensionToken() && Features.C99)
1059 Diag(Identifier, diag::ext_token_used);
1060}
1061
1062/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
1063/// the current file. This either returns the EOF token or pops a level off
1064/// the include stack and keeps going.
Chris Lattnerd2177732007-07-20 16:59:19 +00001065bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 assert(!CurMacroExpander &&
1067 "Ending a file when currently in a macro!");
1068
1069 // See if this file had a controlling macro.
1070 if (CurLexer) { // Not ending a macro, ignore it.
1071 if (const IdentifierInfo *ControllingMacro =
1072 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
1073 // Okay, this has a controlling macro, remember in PerFileInfo.
1074 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001075 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
1077 }
1078 }
1079
1080 // If this is a #include'd file, pop it off the include stack and continue
1081 // lexing the #includer file.
1082 if (!IncludeMacroStack.empty()) {
1083 // We're done with the #included file.
1084 RemoveTopOfLexerStack();
1085
1086 // Notify the client, if desired, that we are in a new source file.
1087 if (Callbacks && !isEndOfMacro && CurLexer) {
1088 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1089
1090 // Get the file entry for the current file.
1091 if (const FileEntry *FE =
Chris Lattner9dc1f532007-07-20 16:37:10 +00001092 SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 FileType = HeaderInfo.getFileDirFlavor(FE);
1094
1095 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1096 PPCallbacks::ExitFile, FileType);
1097 }
1098
1099 // Client should lex another token.
1100 return false;
1101 }
1102
1103 Result.startToken();
1104 CurLexer->BufferPtr = CurLexer->BufferEnd;
1105 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
1106 Result.setKind(tok::eof);
1107
1108 // We're done with the #included file.
1109 delete CurLexer;
1110 CurLexer = 0;
1111
1112 // This is the end of the top-level file. If the diag::pp_macro_not_used
1113 // diagnostic is enabled, walk all of the identifiers, looking for macros that
1114 // have not been used.
1115 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
1116 for (IdentifierTable::iterator I = Identifiers.begin(),
1117 E = Identifiers.end(); I != E; ++I) {
1118 const IdentifierInfo &II = I->getValue();
1119 if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
1120 Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
1121 }
1122 }
1123
1124 return true;
1125}
1126
1127/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1128/// the current macro expansion or token stream expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00001129bool Preprocessor::HandleEndOfMacro(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 assert(CurMacroExpander && !CurLexer &&
1131 "Ending a macro when currently in a #include file!");
1132
Chris Lattner9594acf2007-07-15 00:25:26 +00001133 // Delete or cache the now-dead macro expander.
1134 if (NumCachedMacroExpanders == MacroExpanderCacheSize)
1135 delete CurMacroExpander;
1136 else
1137 MacroExpanderCache[NumCachedMacroExpanders++] = CurMacroExpander;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
1139 // Handle this like a #include file being popped off the stack.
1140 CurMacroExpander = 0;
1141 return HandleEndOfFile(Result, true);
1142}
1143
1144
1145//===----------------------------------------------------------------------===//
1146// Utility Methods for Preprocessor Directive Handling.
1147//===----------------------------------------------------------------------===//
1148
1149/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1150/// current line until the tok::eom token is found.
1151void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattnerd2177732007-07-20 16:59:19 +00001152 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 do {
1154 LexUnexpandedToken(Tmp);
1155 } while (Tmp.getKind() != tok::eom);
1156}
1157
1158/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1159static bool isCXXNamedOperator(const std::string &Spelling) {
1160 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1161 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1162 Spelling == "or" || Spelling == "xor";
1163}
1164
1165/// ReadMacroName - Lex and validate a macro name, which occurs after a
1166/// #define or #undef. This sets the token kind to eom and discards the rest
1167/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1168/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1169/// else (e.g. #ifdef).
Chris Lattnerd2177732007-07-20 16:59:19 +00001170void Preprocessor::ReadMacroName(Token &MacroNameTok, char isDefineUndef) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 // Read the token, don't allow macro expansion on it.
1172 LexUnexpandedToken(MacroNameTok);
1173
1174 // Missing macro name?
1175 if (MacroNameTok.getKind() == tok::eom)
1176 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1177
1178 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1179 if (II == 0) {
1180 std::string Spelling = getSpelling(MacroNameTok);
1181 if (isCXXNamedOperator(Spelling))
1182 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1183 // except for their spellings.
1184 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1185 else
1186 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1187 // Fall through on error.
1188 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1189 // Error if defining "defined": C99 6.10.8.4.
1190 Diag(MacroNameTok, diag::err_defined_macro_name);
1191 } else if (isDefineUndef && II->getMacroInfo() &&
1192 II->getMacroInfo()->isBuiltinMacro()) {
1193 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1194 if (isDefineUndef == 1)
1195 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1196 else
1197 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1198 } else {
1199 // Okay, we got a good identifier node. Return it.
1200 return;
1201 }
1202
1203 // Invalid macro name, read and discard the rest of the line. Then set the
1204 // token kind to tok::eom.
1205 MacroNameTok.setKind(tok::eom);
1206 return DiscardUntilEndOfDirective();
1207}
1208
1209/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1210/// not, emit a diagnostic and consume up until the eom.
1211void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001212 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 Lex(Tmp);
1214 // There should be no tokens after the directive, but we allow them as an
1215 // extension.
1216 while (Tmp.getKind() == tok::comment) // Skip comments in -C mode.
1217 Lex(Tmp);
1218
1219 if (Tmp.getKind() != tok::eom) {
1220 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1221 DiscardUntilEndOfDirective();
1222 }
1223}
1224
1225
1226
1227/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1228/// decided that the subsequent tokens are in the #if'd out portion of the
1229/// file. Lex the rest of the file, until we see an #endif. If
1230/// FoundNonSkipPortion is true, then we have already emitted code for part of
1231/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1232/// is true, then #else directives are ok, if not, then we have already seen one
1233/// so a #else directive is a duplicate. When this returns, the caller can lex
1234/// the first valid token.
1235void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1236 bool FoundNonSkipPortion,
1237 bool FoundElse) {
1238 ++NumSkipped;
1239 assert(CurMacroExpander == 0 && CurLexer &&
1240 "Lexing a macro, not a file?");
1241
1242 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1243 FoundNonSkipPortion, FoundElse);
1244
1245 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1246 // disabling warnings, etc.
1247 CurLexer->LexingRawMode = true;
Chris Lattnerd2177732007-07-20 16:59:19 +00001248 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 while (1) {
1250 CurLexer->Lex(Tok);
1251
1252 // If this is the end of the buffer, we have an error.
1253 if (Tok.getKind() == tok::eof) {
1254 // Emit errors for each unterminated conditional on the stack, including
1255 // the current one.
1256 while (!CurLexer->ConditionalStack.empty()) {
1257 Diag(CurLexer->ConditionalStack.back().IfLoc,
1258 diag::err_pp_unterminated_conditional);
1259 CurLexer->ConditionalStack.pop_back();
1260 }
1261
1262 // Just return and let the caller lex after this #include.
1263 break;
1264 }
1265
1266 // If this token is not a preprocessor directive, just skip it.
1267 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
1268 continue;
1269
1270 // We just parsed a # character at the start of a line, so we're in
1271 // directive mode. Tell the lexer this so any newlines we see will be
1272 // converted into an EOM token (this terminates the macro).
1273 CurLexer->ParsingPreprocessorDirective = true;
1274 CurLexer->KeepCommentMode = false;
1275
1276
1277 // Read the next token, the directive flavor.
1278 LexUnexpandedToken(Tok);
1279
1280 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1281 // something bogus), skip it.
1282 if (Tok.getKind() != tok::identifier) {
1283 CurLexer->ParsingPreprocessorDirective = false;
1284 // Restore comment saving mode.
1285 CurLexer->KeepCommentMode = KeepComments;
1286 continue;
1287 }
1288
1289 // If the first letter isn't i or e, it isn't intesting to us. We know that
1290 // this is safe in the face of spelling differences, because there is no way
1291 // to spell an i/e in a strange way that is another letter. Skipping this
1292 // allows us to avoid looking up the identifier info for #define/#undef and
1293 // other common directives.
1294 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1295 char FirstChar = RawCharData[0];
1296 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1297 FirstChar != 'i' && FirstChar != 'e') {
1298 CurLexer->ParsingPreprocessorDirective = false;
1299 // Restore comment saving mode.
1300 CurLexer->KeepCommentMode = KeepComments;
1301 continue;
1302 }
1303
1304 // Get the identifier name without trigraphs or embedded newlines. Note
1305 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1306 // when skipping.
1307 // TODO: could do this with zero copies in the no-clean case by using
1308 // strncmp below.
1309 char Directive[20];
1310 unsigned IdLen;
1311 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1312 IdLen = Tok.getLength();
1313 memcpy(Directive, RawCharData, IdLen);
1314 Directive[IdLen] = 0;
1315 } else {
1316 std::string DirectiveStr = getSpelling(Tok);
1317 IdLen = DirectiveStr.size();
1318 if (IdLen >= 20) {
1319 CurLexer->ParsingPreprocessorDirective = false;
1320 // Restore comment saving mode.
1321 CurLexer->KeepCommentMode = KeepComments;
1322 continue;
1323 }
1324 memcpy(Directive, &DirectiveStr[0], IdLen);
1325 Directive[IdLen] = 0;
1326 }
1327
1328 if (FirstChar == 'i' && Directive[1] == 'f') {
1329 if ((IdLen == 2) || // "if"
1330 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1331 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1332 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1333 // bother parsing the condition.
1334 DiscardUntilEndOfDirective();
1335 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1336 /*foundnonskip*/false,
1337 /*fnddelse*/false);
1338 }
1339 } else if (FirstChar == 'e') {
1340 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1341 CheckEndOfDirective("#endif");
1342 PPConditionalInfo CondInfo;
1343 CondInfo.WasSkipping = true; // Silence bogus warning.
1344 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1345 InCond = InCond; // Silence warning in no-asserts mode.
1346 assert(!InCond && "Can't be skipping if not in a conditional!");
1347
1348 // If we popped the outermost skipping block, we're done skipping!
1349 if (!CondInfo.WasSkipping)
1350 break;
1351 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1352 // #else directive in a skipping conditional. If not in some other
1353 // skipping conditional, and if #else hasn't already been seen, enter it
1354 // as a non-skipping conditional.
1355 CheckEndOfDirective("#else");
1356 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1357
1358 // If this is a #else with a #else before it, report the error.
1359 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1360
1361 // Note that we've seen a #else in this conditional.
1362 CondInfo.FoundElse = true;
1363
1364 // If the conditional is at the top level, and the #if block wasn't
1365 // entered, enter the #else block now.
1366 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1367 CondInfo.FoundNonSkip = true;
1368 break;
1369 }
1370 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1371 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1372
1373 bool ShouldEnter;
1374 // If this is in a skipping block or if we're already handled this #if
1375 // block, don't bother parsing the condition.
1376 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1377 DiscardUntilEndOfDirective();
1378 ShouldEnter = false;
1379 } else {
1380 // Restore the value of LexingRawMode so that identifiers are
1381 // looked up, etc, inside the #elif expression.
1382 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1383 CurLexer->LexingRawMode = false;
1384 IdentifierInfo *IfNDefMacro = 0;
1385 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1386 CurLexer->LexingRawMode = true;
1387 }
1388
1389 // If this is a #elif with a #else before it, report the error.
1390 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1391
1392 // If this condition is true, enter it!
1393 if (ShouldEnter) {
1394 CondInfo.FoundNonSkip = true;
1395 break;
1396 }
1397 }
1398 }
1399
1400 CurLexer->ParsingPreprocessorDirective = false;
1401 // Restore comment saving mode.
1402 CurLexer->KeepCommentMode = KeepComments;
1403 }
1404
1405 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1406 // of the file, just stop skipping and return to lexing whatever came after
1407 // the #if block.
1408 CurLexer->LexingRawMode = false;
1409}
1410
1411//===----------------------------------------------------------------------===//
1412// Preprocessor Directive Handling.
1413//===----------------------------------------------------------------------===//
1414
1415/// HandleDirective - This callback is invoked when the lexer sees a # token
1416/// at the start of a line. This consumes the directive, modifies the
1417/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1418/// read is the correct one.
Chris Lattnerd2177732007-07-20 16:59:19 +00001419void Preprocessor::HandleDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1421
1422 // We just parsed a # character at the start of a line, so we're in directive
1423 // mode. Tell the lexer this so any newlines we see will be converted into an
1424 // EOM token (which terminates the directive).
1425 CurLexer->ParsingPreprocessorDirective = true;
1426
1427 ++NumDirectives;
1428
1429 // We are about to read a token. For the multiple-include optimization FA to
1430 // work, we have to remember if we had read any tokens *before* this
1431 // pp-directive.
1432 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1433
1434 // Read the next token, the directive flavor. This isn't expanded due to
1435 // C99 6.10.3p8.
1436 LexUnexpandedToken(Result);
1437
1438 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1439 // #define A(x) #x
1440 // A(abc
1441 // #warning blah
1442 // def)
1443 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1444 if (InMacroArgs)
1445 Diag(Result, diag::ext_embedded_directive);
1446
1447TryAgain:
1448 switch (Result.getKind()) {
1449 case tok::eom:
1450 return; // null directive.
1451 case tok::comment:
1452 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1453 LexUnexpandedToken(Result);
1454 goto TryAgain;
1455
1456 case tok::numeric_constant:
1457 // FIXME: implement # 7 line numbers!
1458 DiscardUntilEndOfDirective();
1459 return;
1460 default:
1461 IdentifierInfo *II = Result.getIdentifierInfo();
1462 if (II == 0) break; // Not an identifier.
1463
1464 // Ask what the preprocessor keyword ID is.
1465 switch (II->getPPKeywordID()) {
1466 default: break;
1467 // C99 6.10.1 - Conditional Inclusion.
1468 case tok::pp_if:
1469 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1470 case tok::pp_ifdef:
1471 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1472 case tok::pp_ifndef:
1473 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1474 case tok::pp_elif:
1475 return HandleElifDirective(Result);
1476 case tok::pp_else:
1477 return HandleElseDirective(Result);
1478 case tok::pp_endif:
1479 return HandleEndifDirective(Result);
1480
1481 // C99 6.10.2 - Source File Inclusion.
1482 case tok::pp_include:
1483 return HandleIncludeDirective(Result); // Handle #include.
1484
1485 // C99 6.10.3 - Macro Replacement.
1486 case tok::pp_define:
1487 return HandleDefineDirective(Result, false);
1488 case tok::pp_undef:
1489 return HandleUndefDirective(Result);
1490
1491 // C99 6.10.4 - Line Control.
1492 case tok::pp_line:
1493 // FIXME: implement #line
1494 DiscardUntilEndOfDirective();
1495 return;
1496
1497 // C99 6.10.5 - Error Directive.
1498 case tok::pp_error:
1499 return HandleUserDiagnosticDirective(Result, false);
1500
1501 // C99 6.10.6 - Pragma Directive.
1502 case tok::pp_pragma:
1503 return HandlePragmaDirective();
1504
1505 // GNU Extensions.
1506 case tok::pp_import:
1507 return HandleImportDirective(Result);
1508 case tok::pp_include_next:
1509 return HandleIncludeNextDirective(Result);
1510
1511 case tok::pp_warning:
1512 Diag(Result, diag::ext_pp_warning_directive);
1513 return HandleUserDiagnosticDirective(Result, true);
1514 case tok::pp_ident:
1515 return HandleIdentSCCSDirective(Result);
1516 case tok::pp_sccs:
1517 return HandleIdentSCCSDirective(Result);
1518 case tok::pp_assert:
1519 //isExtension = true; // FIXME: implement #assert
1520 break;
1521 case tok::pp_unassert:
1522 //isExtension = true; // FIXME: implement #unassert
1523 break;
1524
1525 // clang extensions.
1526 case tok::pp_define_target:
1527 return HandleDefineDirective(Result, true);
1528 case tok::pp_define_other_target:
1529 return HandleDefineOtherTargetDirective(Result);
1530 }
1531 break;
1532 }
1533
1534 // If we reached here, the preprocessing token is not valid!
1535 Diag(Result, diag::err_pp_invalid_directive);
1536
1537 // Read the rest of the PP line.
1538 DiscardUntilEndOfDirective();
1539
1540 // Okay, we're done parsing the directive.
1541}
1542
Chris Lattnerd2177732007-07-20 16:59:19 +00001543void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 bool isWarning) {
1545 // Read the rest of the line raw. We do this because we don't want macros
1546 // to be expanded and we don't require that the tokens be valid preprocessing
1547 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1548 // collapse multiple consequtive white space between tokens, but this isn't
1549 // specified by the standard.
1550 std::string Message = CurLexer->ReadToEndOfLine();
1551
1552 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1553 return Diag(Tok, DiagID, Message);
1554}
1555
1556/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1557///
Chris Lattnerd2177732007-07-20 16:59:19 +00001558void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // Yes, this directive is an extension.
1560 Diag(Tok, diag::ext_pp_ident_directive);
1561
1562 // Read the string argument.
Chris Lattnerd2177732007-07-20 16:59:19 +00001563 Token StrTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 Lex(StrTok);
1565
1566 // If the token kind isn't a string, it's a malformed directive.
1567 if (StrTok.getKind() != tok::string_literal &&
1568 StrTok.getKind() != tok::wide_string_literal)
1569 return Diag(StrTok, diag::err_pp_malformed_ident);
1570
1571 // Verify that there is nothing after the string, other than EOM.
1572 CheckEndOfDirective("#ident");
1573
1574 if (Callbacks)
1575 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1576}
1577
1578//===----------------------------------------------------------------------===//
1579// Preprocessor Include Directive Handling.
1580//===----------------------------------------------------------------------===//
1581
1582/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1583/// checked and spelled filename, e.g. as an operand of #include. This returns
1584/// true if the input filename was in <>'s or false if it were in ""'s. The
1585/// caller is expected to provide a buffer that is large enough to hold the
1586/// spelling of the filename, but is also expected to handle the case when
1587/// this method decides to use a different buffer.
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001588bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 const char *&BufStart,
1590 const char *&BufEnd) {
1591 // Get the text form of the filename.
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1593
1594 // Make sure the filename is <x> or "x".
1595 bool isAngled;
1596 if (BufStart[0] == '<') {
1597 if (BufEnd[-1] != '>') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001598 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 BufStart = 0;
1600 return true;
1601 }
1602 isAngled = true;
1603 } else if (BufStart[0] == '"') {
1604 if (BufEnd[-1] != '"') {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001605 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 BufStart = 0;
1607 return true;
1608 }
1609 isAngled = false;
1610 } else {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001611 Diag(Loc, diag::err_pp_expects_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 BufStart = 0;
1613 return true;
1614 }
1615
1616 // Diagnose #include "" as invalid.
1617 if (BufEnd-BufStart <= 2) {
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001618 Diag(Loc, diag::err_pp_empty_filename);
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 BufStart = 0;
1620 return "";
1621 }
1622
1623 // Skip the brackets.
1624 ++BufStart;
1625 --BufEnd;
1626 return isAngled;
1627}
1628
Chris Lattner706ab502007-07-23 04:56:47 +00001629/// ConcatenateIncludeName - Handle cases where the #include name is expanded
1630/// from a macro as multiple tokens, which need to be glued together. This
1631/// occurs for code like:
1632/// #define FOO <a/b.h>
1633/// #include FOO
1634/// because in this case, "<a/b.h>" is returned as 7 tokens, not one.
1635///
1636/// This code concatenates and consumes tokens up to the '>' token. It returns
1637/// false if the > was found, otherwise it returns true if it finds and consumes
1638/// the EOM marker.
1639static bool ConcatenateIncludeName(llvm::SmallVector<char, 128> &FilenameBuffer,
1640 Preprocessor &PP) {
1641 Token CurTok;
1642
1643 PP.Lex(CurTok);
1644 while (CurTok.getKind() != tok::eom) {
1645 // Append the spelling of this token to the buffer. If there was a space
1646 // before it, add it now.
1647 if (CurTok.hasLeadingSpace())
1648 FilenameBuffer.push_back(' ');
1649
1650 // Get the spelling of the token, directly into FilenameBuffer if possible.
1651 unsigned PreAppendSize = FilenameBuffer.size();
1652 FilenameBuffer.resize(PreAppendSize+CurTok.getLength());
1653
1654 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1655 unsigned ActualLen = PP.getSpelling(CurTok, BufPtr);
1656
1657 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1658 if (BufPtr != &FilenameBuffer[PreAppendSize])
1659 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1660
1661 // Resize FilenameBuffer to the correct size.
1662 if (CurTok.getLength() != ActualLen)
1663 FilenameBuffer.resize(PreAppendSize+ActualLen);
1664
1665 // If we found the '>' marker, return success.
1666 if (CurTok.getKind() == tok::greater)
1667 return false;
1668
1669 PP.Lex(CurTok);
1670 }
1671
1672 // If we hit the eom marker, emit an error and return true so that the caller
1673 // knows the EOM has been read.
1674 PP.Diag(CurTok.getLocation(), diag::err_pp_expects_filename);
1675 return true;
1676}
1677
Reid Spencer5f016e22007-07-11 17:01:13 +00001678/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1679/// file to be included from the lexer, then include it! This is a common
1680/// routine with functionality shared between #include, #include_next and
1681/// #import.
Chris Lattnerd2177732007-07-20 16:59:19 +00001682void Preprocessor::HandleIncludeDirective(Token &IncludeTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 const DirectoryLookup *LookupFrom,
1684 bool isImport) {
1685
Chris Lattnerd2177732007-07-20 16:59:19 +00001686 Token FilenameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 CurLexer->LexIncludeFilename(FilenameTok);
1688
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 // Reserve a buffer to get the spelling.
1690 llvm::SmallVector<char, 128> FilenameBuffer;
Chris Lattner706ab502007-07-23 04:56:47 +00001691 const char *FilenameStart, *FilenameEnd;
1692
1693 switch (FilenameTok.getKind()) {
1694 case tok::eom:
1695 // If the token kind is EOM, the error has already been diagnosed.
1696 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001697
Chris Lattner706ab502007-07-23 04:56:47 +00001698 case tok::angle_string_literal:
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001699 case tok::string_literal: {
Chris Lattner706ab502007-07-23 04:56:47 +00001700 FilenameBuffer.resize(FilenameTok.getLength());
1701 FilenameStart = &FilenameBuffer[0];
1702 unsigned Len = getSpelling(FilenameTok, FilenameStart);
1703 FilenameEnd = FilenameStart+Len;
1704 break;
Chris Lattnerf11ccfc2007-07-23 22:23:52 +00001705 }
Chris Lattner706ab502007-07-23 04:56:47 +00001706
1707 case tok::less:
1708 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1709 // case, glue the tokens together into FilenameBuffer and interpret those.
1710 FilenameBuffer.push_back('<');
1711 if (ConcatenateIncludeName(FilenameBuffer, *this))
1712 return; // Found <eom> but no ">"? Diagnostic already emitted.
1713 FilenameStart = &FilenameBuffer[0];
1714 FilenameEnd = &FilenameBuffer[FilenameBuffer.size()];
1715 break;
1716 default:
1717 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1718 DiscardUntilEndOfDirective();
1719 return;
1720 }
1721
Chris Lattnerf1c99ac2007-07-23 04:15:27 +00001722 bool isAngled = GetIncludeFilenameSpelling(FilenameTok.getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +00001723 FilenameStart, FilenameEnd);
1724 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1725 // error.
Chris Lattner706ab502007-07-23 04:56:47 +00001726 if (FilenameStart == 0) {
1727 DiscardUntilEndOfDirective();
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 return;
Chris Lattner706ab502007-07-23 04:56:47 +00001729 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001730
1731 // Verify that there is nothing after the filename, other than EOM. Use the
1732 // preprocessor to lex this in case lexing the filename entered a macro.
1733 CheckEndOfDirective("#include");
1734
1735 // Check that we don't have infinite #include recursion.
1736 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
1737 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1738
1739 // Search include directories.
1740 const DirectoryLookup *CurDir;
1741 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
1742 isAngled, LookupFrom, CurDir);
1743 if (File == 0)
1744 return Diag(FilenameTok, diag::err_pp_file_not_found,
1745 std::string(FilenameStart, FilenameEnd));
1746
1747 // Ask HeaderInfo if we should enter this #include file.
1748 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1749 // If it returns true, #including this file will have no effect.
1750 return;
1751 }
1752
1753 // Look up the file, create a File ID for it.
1754 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
1755 if (FileID == 0)
1756 return Diag(FilenameTok, diag::err_pp_file_not_found,
1757 std::string(FilenameStart, FilenameEnd));
1758
1759 // Finally, if all is good, enter the new file!
1760 EnterSourceFile(FileID, CurDir);
1761}
1762
1763/// HandleIncludeNextDirective - Implements #include_next.
1764///
Chris Lattnerd2177732007-07-20 16:59:19 +00001765void Preprocessor::HandleIncludeNextDirective(Token &IncludeNextTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1767
1768 // #include_next is like #include, except that we start searching after
1769 // the current found directory. If we can't do this, issue a
1770 // diagnostic.
1771 const DirectoryLookup *Lookup = CurDirLookup;
1772 if (isInPrimaryFile()) {
1773 Lookup = 0;
1774 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1775 } else if (Lookup == 0) {
1776 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1777 } else {
1778 // Start looking up in the next directory.
1779 ++Lookup;
1780 }
1781
1782 return HandleIncludeDirective(IncludeNextTok, Lookup);
1783}
1784
1785/// HandleImportDirective - Implements #import.
1786///
Chris Lattnerd2177732007-07-20 16:59:19 +00001787void Preprocessor::HandleImportDirective(Token &ImportTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 Diag(ImportTok, diag::ext_pp_import_directive);
1789
1790 return HandleIncludeDirective(ImportTok, 0, true);
1791}
1792
1793//===----------------------------------------------------------------------===//
1794// Preprocessor Macro Directive Handling.
1795//===----------------------------------------------------------------------===//
1796
1797/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1798/// definition has just been read. Lex the rest of the arguments and the
1799/// closing ), updating MI with what we learn. Return true if an error occurs
1800/// parsing the arg list.
1801bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
Chris Lattner25c96482007-07-14 22:46:43 +00001802 llvm::SmallVector<IdentifierInfo*, 32> Arguments;
1803
Chris Lattnerd2177732007-07-20 16:59:19 +00001804 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 while (1) {
1806 LexUnexpandedToken(Tok);
1807 switch (Tok.getKind()) {
1808 case tok::r_paren:
1809 // Found the end of the argument list.
Chris Lattner25c96482007-07-14 22:46:43 +00001810 if (Arguments.empty()) { // #define FOO()
1811 MI->setArgumentList(Arguments.begin(), Arguments.end());
1812 return false;
1813 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 // Otherwise we have #define FOO(A,)
1815 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1816 return true;
1817 case tok::ellipsis: // #define X(... -> C99 varargs
1818 // Warn if use of C99 feature in non-C99 mode.
1819 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1820
1821 // Lex the token after the identifier.
1822 LexUnexpandedToken(Tok);
1823 if (Tok.getKind() != tok::r_paren) {
1824 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1825 return true;
1826 }
1827 // Add the __VA_ARGS__ identifier as an argument.
Chris Lattner25c96482007-07-14 22:46:43 +00001828 Arguments.push_back(Ident__VA_ARGS__);
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 MI->setIsC99Varargs();
Chris Lattner25c96482007-07-14 22:46:43 +00001830 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00001831 return false;
1832 case tok::eom: // #define X(
1833 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1834 return true;
1835 default:
1836 // Handle keywords and identifiers here to accept things like
1837 // #define Foo(for) for.
1838 IdentifierInfo *II = Tok.getIdentifierInfo();
1839 if (II == 0) {
1840 // #define X(1
1841 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1842 return true;
1843 }
1844
1845 // If this is already used as an argument, it is used multiple times (e.g.
1846 // #define X(A,A.
Chris Lattner25c96482007-07-14 22:46:43 +00001847 if (std::find(Arguments.begin(), Arguments.end(), II) !=
1848 Arguments.end()) { // C99 6.10.3p6
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
1850 return true;
1851 }
1852
1853 // Add the argument to the macro info.
Chris Lattner25c96482007-07-14 22:46:43 +00001854 Arguments.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001855
1856 // Lex the token after the identifier.
1857 LexUnexpandedToken(Tok);
1858
1859 switch (Tok.getKind()) {
1860 default: // #define X(A B
1861 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1862 return true;
1863 case tok::r_paren: // #define X(A)
Chris Lattner25c96482007-07-14 22:46:43 +00001864 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00001865 return false;
1866 case tok::comma: // #define X(A,
1867 break;
1868 case tok::ellipsis: // #define X(A... -> GCC extension
1869 // Diagnose extension.
1870 Diag(Tok, diag::ext_named_variadic_macro);
1871
1872 // Lex the token after the identifier.
1873 LexUnexpandedToken(Tok);
1874 if (Tok.getKind() != tok::r_paren) {
1875 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1876 return true;
1877 }
1878
1879 MI->setIsGNUVarargs();
Chris Lattner25c96482007-07-14 22:46:43 +00001880 MI->setArgumentList(Arguments.begin(), Arguments.end());
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 return false;
1882 }
1883 }
1884 }
1885}
1886
1887/// HandleDefineDirective - Implements #define. This consumes the entire macro
1888/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
1889/// true, then this is a "#define_target", otherwise this is a "#define".
1890///
Chris Lattnerd2177732007-07-20 16:59:19 +00001891void Preprocessor::HandleDefineDirective(Token &DefineTok,
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 bool isTargetSpecific) {
1893 ++NumDefined;
1894
Chris Lattnerd2177732007-07-20 16:59:19 +00001895 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 ReadMacroName(MacroNameTok, 1);
1897
1898 // Error reading macro name? If so, diagnostic already issued.
1899 if (MacroNameTok.getKind() == tok::eom)
1900 return;
Chris Lattnerc215bd62007-07-14 22:11:41 +00001901
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 // If we are supposed to keep comments in #defines, reenable comment saving
1903 // mode.
1904 CurLexer->KeepCommentMode = KeepMacroComments;
1905
1906 // Create the new macro.
1907 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
1908 if (isTargetSpecific) MI->setIsTargetSpecific();
1909
1910 // If the identifier is an 'other target' macro, clear this bit.
1911 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
1912
1913
Chris Lattnerd2177732007-07-20 16:59:19 +00001914 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001915 LexUnexpandedToken(Tok);
1916
1917 // If this is a function-like macro definition, parse the argument list,
1918 // marking each of the identifiers as being used as macro arguments. Also,
1919 // check other constraints on the first token of the macro body.
1920 if (Tok.getKind() == tok::eom) {
1921 // If there is no body to this macro, we have no special handling here.
1922 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1923 // This is a function-like macro definition. Read the argument list.
1924 MI->setIsFunctionLike();
1925 if (ReadMacroDefinitionArgList(MI)) {
1926 // Forget about MI.
1927 delete MI;
1928 // Throw away the rest of the line.
1929 if (CurLexer->ParsingPreprocessorDirective)
1930 DiscardUntilEndOfDirective();
1931 return;
1932 }
1933
1934 // Read the first token after the arg list for down below.
1935 LexUnexpandedToken(Tok);
1936 } else if (!Tok.hasLeadingSpace()) {
1937 // C99 requires whitespace between the macro definition and the body. Emit
1938 // a diagnostic for something like "#define X+".
1939 if (Features.C99) {
1940 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1941 } else {
1942 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1943 // one in some cases!
1944 }
1945 } else {
1946 // This is a normal token with leading space. Clear the leading space
1947 // marker on the first token to get proper expansion.
Chris Lattnerd2177732007-07-20 16:59:19 +00001948 Tok.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001949 }
1950
1951 // If this is a definition of a variadic C99 function-like macro, not using
1952 // the GNU named varargs extension, enabled __VA_ARGS__.
1953
1954 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1955 // This gets unpoisoned where it is allowed.
1956 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1957 if (MI->isC99Varargs())
1958 Ident__VA_ARGS__->setIsPoisoned(false);
1959
1960 // Read the rest of the macro body.
Chris Lattnerb5e240f2007-07-14 21:54:03 +00001961 if (MI->isObjectLike()) {
1962 // Object-like macros are very simple, just read their body.
1963 while (Tok.getKind() != tok::eom) {
1964 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 // Get the next token of the macro.
1966 LexUnexpandedToken(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 }
1968
Chris Lattnerb5e240f2007-07-14 21:54:03 +00001969 } else {
1970 // Otherwise, read the body of a function-like macro. This has to validate
1971 // the # (stringize) operator.
1972 while (Tok.getKind() != tok::eom) {
1973 MI->AddTokenToBody(Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +00001974
Chris Lattnerb5e240f2007-07-14 21:54:03 +00001975 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
1976 // parameters in function-like macro expansions.
1977 if (Tok.getKind() != tok::hash) {
1978 // Get the next token of the macro.
1979 LexUnexpandedToken(Tok);
1980 continue;
1981 }
1982
1983 // Get the next token of the macro.
1984 LexUnexpandedToken(Tok);
1985
1986 // Not a macro arg identifier?
1987 if (!Tok.getIdentifierInfo() ||
1988 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1989 Diag(Tok, diag::err_pp_stringize_not_parameter);
1990 delete MI;
1991
1992 // Disable __VA_ARGS__ again.
1993 Ident__VA_ARGS__->setIsPoisoned(true);
1994 return;
1995 }
1996
1997 // Things look ok, add the param name token to the macro.
1998 MI->AddTokenToBody(Tok);
1999
2000 // Get the next token of the macro.
2001 LexUnexpandedToken(Tok);
2002 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 }
2004
Chris Lattnerc215bd62007-07-14 22:11:41 +00002005
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 // Disable __VA_ARGS__ again.
2007 Ident__VA_ARGS__->setIsPoisoned(true);
2008
2009 // Check that there is no paste (##) operator at the begining or end of the
2010 // replacement list.
2011 unsigned NumTokens = MI->getNumTokens();
2012 if (NumTokens != 0) {
2013 if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
2014 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
2015 delete MI;
2016 return;
2017 }
2018 if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
2019 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
2020 delete MI;
2021 return;
2022 }
2023 }
2024
2025 // If this is the primary source file, remember that this macro hasn't been
2026 // used yet.
2027 if (isInPrimaryFile())
2028 MI->setIsUsed(false);
2029
2030 // Finally, if this identifier already had a macro defined for it, verify that
2031 // the macro bodies are identical and free the old definition.
2032 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
2033 if (!OtherMI->isUsed())
2034 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
2035
2036 // Macros must be identical. This means all tokes and whitespace separation
2037 // must be the same. C99 6.10.3.2.
2038 if (!MI->isIdenticalTo(*OtherMI, *this)) {
2039 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
2040 MacroNameTok.getIdentifierInfo()->getName());
2041 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
2042 }
2043 delete OtherMI;
2044 }
2045
2046 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
2047}
2048
2049/// HandleDefineOtherTargetDirective - Implements #define_other_target.
Chris Lattnerd2177732007-07-20 16:59:19 +00002050void Preprocessor::HandleDefineOtherTargetDirective(Token &Tok) {
2051 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 ReadMacroName(MacroNameTok, 1);
2053
2054 // Error reading macro name? If so, diagnostic already issued.
2055 if (MacroNameTok.getKind() == tok::eom)
2056 return;
2057
2058 // Check to see if this is the last token on the #undef line.
2059 CheckEndOfDirective("#define_other_target");
2060
2061 // If there is already a macro defined by this name, turn it into a
2062 // target-specific define.
2063 if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
2064 MI->setIsTargetSpecific(true);
2065 return;
2066 }
2067
2068 // Mark the identifier as being a macro on some other target.
2069 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
2070}
2071
2072
2073/// HandleUndefDirective - Implements #undef.
2074///
Chris Lattnerd2177732007-07-20 16:59:19 +00002075void Preprocessor::HandleUndefDirective(Token &UndefTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002076 ++NumUndefined;
2077
Chris Lattnerd2177732007-07-20 16:59:19 +00002078 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002079 ReadMacroName(MacroNameTok, 2);
2080
2081 // Error reading macro name? If so, diagnostic already issued.
2082 if (MacroNameTok.getKind() == tok::eom)
2083 return;
2084
2085 // Check to see if this is the last token on the #undef line.
2086 CheckEndOfDirective("#undef");
2087
2088 // Okay, we finally have a valid identifier to undef.
2089 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
2090
2091 // #undef untaints an identifier if it were marked by define_other_target.
2092 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
2093
2094 // If the macro is not defined, this is a noop undef, just return.
2095 if (MI == 0) return;
2096
2097 if (!MI->isUsed())
2098 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
2099
2100 // Free macro definition.
2101 delete MI;
2102 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
2103}
2104
2105
2106//===----------------------------------------------------------------------===//
2107// Preprocessor Conditional Directive Handling.
2108//===----------------------------------------------------------------------===//
2109
2110/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
2111/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
2112/// if any tokens have been returned or pp-directives activated before this
2113/// #ifndef has been lexed.
2114///
Chris Lattnerd2177732007-07-20 16:59:19 +00002115void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 bool ReadAnyTokensBeforeDirective) {
2117 ++NumIf;
Chris Lattnerd2177732007-07-20 16:59:19 +00002118 Token DirectiveTok = Result;
Reid Spencer5f016e22007-07-11 17:01:13 +00002119
Chris Lattnerd2177732007-07-20 16:59:19 +00002120 Token MacroNameTok;
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 ReadMacroName(MacroNameTok);
2122
2123 // Error reading macro name? If so, diagnostic already issued.
2124 if (MacroNameTok.getKind() == tok::eom)
2125 return;
2126
2127 // Check to see if this is the last token on the #if[n]def line.
2128 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
2129
2130 // If the start of a top-level #ifdef, inform MIOpt.
2131 if (!ReadAnyTokensBeforeDirective &&
2132 CurLexer->getConditionalStackDepth() == 0) {
2133 assert(isIfndef && "#ifdef shouldn't reach here");
2134 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
2135 }
2136
2137 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
2138 MacroInfo *MI = MII->getMacroInfo();
2139
2140 // If there is a macro, process it.
2141 if (MI) {
2142 // Mark it used.
2143 MI->setIsUsed(true);
2144
2145 // If this is the first use of a target-specific macro, warn about it.
2146 if (MI->isTargetSpecific()) {
2147 MI->setIsTargetSpecific(false); // Don't warn on second use.
2148 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2149 diag::port_target_macro_use);
2150 }
2151 } else {
2152 // Use of a target-specific macro for some other target? If so, warn.
2153 if (MII->isOtherTargetMacro()) {
2154 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
2155 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
2156 diag::port_target_macro_use);
2157 }
2158 }
2159
2160 // Should we include the stuff contained by this directive?
2161 if (!MI == isIfndef) {
2162 // Yes, remember that we are inside a conditional, then lex the next token.
2163 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
2164 /*foundnonskip*/true, /*foundelse*/false);
2165 } else {
2166 // No, skip the contents of this block and return the first token after it.
2167 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
2168 /*Foundnonskip*/false,
2169 /*FoundElse*/false);
2170 }
2171}
2172
2173/// HandleIfDirective - Implements the #if directive.
2174///
Chris Lattnerd2177732007-07-20 16:59:19 +00002175void Preprocessor::HandleIfDirective(Token &IfToken,
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 bool ReadAnyTokensBeforeDirective) {
2177 ++NumIf;
2178
2179 // Parse and evaluation the conditional expression.
2180 IdentifierInfo *IfNDefMacro = 0;
2181 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2182
2183 // Should we include the stuff contained by this directive?
2184 if (ConditionalTrue) {
2185 // If this condition is equivalent to #ifndef X, and if this is the first
2186 // directive seen, handle it for the multiple-include optimization.
2187 if (!ReadAnyTokensBeforeDirective &&
2188 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2189 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2190
2191 // Yes, remember that we are inside a conditional, then lex the next token.
2192 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2193 /*foundnonskip*/true, /*foundelse*/false);
2194 } else {
2195 // No, skip the contents of this block and return the first token after it.
2196 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2197 /*FoundElse*/false);
2198 }
2199}
2200
2201/// HandleEndifDirective - Implements the #endif directive.
2202///
Chris Lattnerd2177732007-07-20 16:59:19 +00002203void Preprocessor::HandleEndifDirective(Token &EndifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002204 ++NumEndif;
2205
2206 // Check that this is the whole directive.
2207 CheckEndOfDirective("#endif");
2208
2209 PPConditionalInfo CondInfo;
2210 if (CurLexer->popConditionalLevel(CondInfo)) {
2211 // No conditionals on the stack: this is an #endif without an #if.
2212 return Diag(EndifToken, diag::err_pp_endif_without_if);
2213 }
2214
2215 // If this the end of a top-level #endif, inform MIOpt.
2216 if (CurLexer->getConditionalStackDepth() == 0)
2217 CurLexer->MIOpt.ExitTopLevelConditional();
2218
2219 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2220 "This code should only be reachable in the non-skipping case!");
2221}
2222
2223
Chris Lattnerd2177732007-07-20 16:59:19 +00002224void Preprocessor::HandleElseDirective(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002225 ++NumElse;
2226
2227 // #else directive in a non-skipping conditional... start skipping.
2228 CheckEndOfDirective("#else");
2229
2230 PPConditionalInfo CI;
2231 if (CurLexer->popConditionalLevel(CI))
2232 return Diag(Result, diag::pp_err_else_without_if);
2233
2234 // If this is a top-level #else, inform the MIOpt.
2235 if (CurLexer->getConditionalStackDepth() == 0)
2236 CurLexer->MIOpt.FoundTopLevelElse();
2237
2238 // If this is a #else with a #else before it, report the error.
2239 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2240
2241 // Finally, skip the rest of the contents of this block and return the first
2242 // token after it.
2243 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2244 /*FoundElse*/true);
2245}
2246
Chris Lattnerd2177732007-07-20 16:59:19 +00002247void Preprocessor::HandleElifDirective(Token &ElifToken) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002248 ++NumElse;
2249
2250 // #elif directive in a non-skipping conditional... start skipping.
2251 // We don't care what the condition is, because we will always skip it (since
2252 // the block immediately before it was included).
2253 DiscardUntilEndOfDirective();
2254
2255 PPConditionalInfo CI;
2256 if (CurLexer->popConditionalLevel(CI))
2257 return Diag(ElifToken, diag::pp_err_elif_without_if);
2258
2259 // If this is a top-level #elif, inform the MIOpt.
2260 if (CurLexer->getConditionalStackDepth() == 0)
2261 CurLexer->MIOpt.FoundTopLevelElse();
2262
2263 // If this is a #elif with a #else before it, report the error.
2264 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2265
2266 // Finally, skip the rest of the contents of this block and return the first
2267 // token after it.
2268 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2269 /*FoundElse*/CI.FoundElse);
2270}
2271