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