blob: d18a2f981350213d9633ad0e52724d69765e73d3 [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>
Reid Spencer5f016e22007-07-11 17:01:13 +000040using 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.
Chris Lattnerc35717a2007-07-13 17:10:38 +0000910 llvm::SmallVector<char, 64> IdentifierBuffer;
911 IdentifierBuffer.resize(Identifier.getLength());
912 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 unsigned Size = getSpelling(Identifier, TmpBuf);
914 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
915 }
916 Identifier.setIdentifierInfo(II);
917 return II;
918}
919
920
921/// HandleIdentifier - This callback is invoked when the lexer reads an
922/// identifier. This callback looks up the identifier in the map and/or
923/// potentially macro expands it or turns it into a named token (like 'for').
924void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
925 assert(Identifier.getIdentifierInfo() &&
926 "Can't handle identifiers without identifier info!");
927
928 IdentifierInfo &II = *Identifier.getIdentifierInfo();
929
930 // If this identifier was poisoned, and if it was not produced from a macro
931 // expansion, emit an error.
932 if (II.isPoisoned() && CurLexer) {
933 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
934 Diag(Identifier, diag::err_pp_used_poisoned_id);
935 else
936 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
937 }
938
939 // If this is a macro to be expanded, do it.
940 if (MacroInfo *MI = II.getMacroInfo()) {
941 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
942 if (MI->isEnabled()) {
943 if (!HandleMacroExpandedIdentifier(Identifier, MI))
944 return;
945 } else {
946 // C99 6.10.3.4p2 says that a disabled macro may never again be
947 // expanded, even if it's in a context where it could be expanded in the
948 // future.
949 Identifier.setFlag(LexerToken::DisableExpand);
950 }
951 }
952 } else if (II.isOtherTargetMacro() && !DisableMacroExpansion) {
953 // If this identifier is a macro on some other target, emit a diagnostic.
954 // This diagnosic is only emitted when macro expansion is enabled, because
955 // the macro would not have been expanded for the other target either.
956 II.setIsOtherTargetMacro(false); // Don't warn on second use.
957 getTargetInfo().DiagnoseNonPortability(Identifier.getLocation(),
958 diag::port_target_macro_use);
959
960 }
961
962 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
963 // then we act as if it is the actual operator and not the textual
964 // representation of it.
965 if (II.isCPlusPlusOperatorKeyword())
966 Identifier.setIdentifierInfo(0);
967
968 // Change the kind of this identifier to the appropriate token kind, e.g.
969 // turning "for" into a keyword.
970 Identifier.setKind(II.getTokenID());
971
972 // If this is an extension token, diagnose its use.
973 // FIXME: tried (unsuccesfully) to shut this up when compiling with gnu99
974 // For now, I'm just commenting it out (while I work on attributes).
975 if (II.isExtensionToken() && Features.C99)
976 Diag(Identifier, diag::ext_token_used);
977}
978
979/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
980/// the current file. This either returns the EOF token or pops a level off
981/// the include stack and keeps going.
982bool Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
983 assert(!CurMacroExpander &&
984 "Ending a file when currently in a macro!");
985
986 // See if this file had a controlling macro.
987 if (CurLexer) { // Not ending a macro, ignore it.
988 if (const IdentifierInfo *ControllingMacro =
989 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
990 // Okay, this has a controlling macro, remember in PerFileInfo.
991 if (const FileEntry *FE =
992 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
993 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
994 }
995 }
996
997 // If this is a #include'd file, pop it off the include stack and continue
998 // lexing the #includer file.
999 if (!IncludeMacroStack.empty()) {
1000 // We're done with the #included file.
1001 RemoveTopOfLexerStack();
1002
1003 // Notify the client, if desired, that we are in a new source file.
1004 if (Callbacks && !isEndOfMacro && CurLexer) {
1005 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
1006
1007 // Get the file entry for the current file.
1008 if (const FileEntry *FE =
1009 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
1010 FileType = HeaderInfo.getFileDirFlavor(FE);
1011
1012 Callbacks->FileChanged(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1013 PPCallbacks::ExitFile, FileType);
1014 }
1015
1016 // Client should lex another token.
1017 return false;
1018 }
1019
1020 Result.startToken();
1021 CurLexer->BufferPtr = CurLexer->BufferEnd;
1022 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
1023 Result.setKind(tok::eof);
1024
1025 // We're done with the #included file.
1026 delete CurLexer;
1027 CurLexer = 0;
1028
1029 // This is the end of the top-level file. If the diag::pp_macro_not_used
1030 // diagnostic is enabled, walk all of the identifiers, looking for macros that
1031 // have not been used.
1032 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored){
1033 for (IdentifierTable::iterator I = Identifiers.begin(),
1034 E = Identifiers.end(); I != E; ++I) {
1035 const IdentifierInfo &II = I->getValue();
1036 if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
1037 Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
1038 }
1039 }
1040
1041 return true;
1042}
1043
1044/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
1045/// the current macro expansion or token stream expansion.
1046bool Preprocessor::HandleEndOfMacro(LexerToken &Result) {
1047 assert(CurMacroExpander && !CurLexer &&
1048 "Ending a macro when currently in a #include file!");
1049
1050 delete CurMacroExpander;
1051
1052 // Handle this like a #include file being popped off the stack.
1053 CurMacroExpander = 0;
1054 return HandleEndOfFile(Result, true);
1055}
1056
1057
1058//===----------------------------------------------------------------------===//
1059// Utility Methods for Preprocessor Directive Handling.
1060//===----------------------------------------------------------------------===//
1061
1062/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1063/// current line until the tok::eom token is found.
1064void Preprocessor::DiscardUntilEndOfDirective() {
1065 LexerToken Tmp;
1066 do {
1067 LexUnexpandedToken(Tmp);
1068 } while (Tmp.getKind() != tok::eom);
1069}
1070
1071/// isCXXNamedOperator - Returns "true" if the token is a named operator in C++.
1072static bool isCXXNamedOperator(const std::string &Spelling) {
1073 return Spelling == "and" || Spelling == "bitand" || Spelling == "bitor" ||
1074 Spelling == "compl" || Spelling == "not" || Spelling == "not_eq" ||
1075 Spelling == "or" || Spelling == "xor";
1076}
1077
1078/// ReadMacroName - Lex and validate a macro name, which occurs after a
1079/// #define or #undef. This sets the token kind to eom and discards the rest
1080/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1081/// this is due to a a #define, 2 if #undef directive, 0 if it is something
1082/// else (e.g. #ifdef).
1083void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
1084 // Read the token, don't allow macro expansion on it.
1085 LexUnexpandedToken(MacroNameTok);
1086
1087 // Missing macro name?
1088 if (MacroNameTok.getKind() == tok::eom)
1089 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1090
1091 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1092 if (II == 0) {
1093 std::string Spelling = getSpelling(MacroNameTok);
1094 if (isCXXNamedOperator(Spelling))
1095 // C++ 2.5p2: Alternative tokens behave the same as its primary token
1096 // except for their spellings.
1097 Diag(MacroNameTok, diag::err_pp_operator_used_as_macro_name, Spelling);
1098 else
1099 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
1100 // Fall through on error.
1101 } else if (isDefineUndef && II->getPPKeywordID() == tok::pp_defined) {
1102 // Error if defining "defined": C99 6.10.8.4.
1103 Diag(MacroNameTok, diag::err_defined_macro_name);
1104 } else if (isDefineUndef && II->getMacroInfo() &&
1105 II->getMacroInfo()->isBuiltinMacro()) {
1106 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
1107 if (isDefineUndef == 1)
1108 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1109 else
1110 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
1111 } else {
1112 // Okay, we got a good identifier node. Return it.
1113 return;
1114 }
1115
1116 // Invalid macro name, read and discard the rest of the line. Then set the
1117 // token kind to tok::eom.
1118 MacroNameTok.setKind(tok::eom);
1119 return DiscardUntilEndOfDirective();
1120}
1121
1122/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1123/// not, emit a diagnostic and consume up until the eom.
1124void Preprocessor::CheckEndOfDirective(const char *DirType) {
1125 LexerToken Tmp;
1126 Lex(Tmp);
1127 // There should be no tokens after the directive, but we allow them as an
1128 // extension.
1129 while (Tmp.getKind() == tok::comment) // Skip comments in -C mode.
1130 Lex(Tmp);
1131
1132 if (Tmp.getKind() != tok::eom) {
1133 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1134 DiscardUntilEndOfDirective();
1135 }
1136}
1137
1138
1139
1140/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1141/// decided that the subsequent tokens are in the #if'd out portion of the
1142/// file. Lex the rest of the file, until we see an #endif. If
1143/// FoundNonSkipPortion is true, then we have already emitted code for part of
1144/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1145/// is true, then #else directives are ok, if not, then we have already seen one
1146/// so a #else directive is a duplicate. When this returns, the caller can lex
1147/// the first valid token.
1148void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
1149 bool FoundNonSkipPortion,
1150 bool FoundElse) {
1151 ++NumSkipped;
1152 assert(CurMacroExpander == 0 && CurLexer &&
1153 "Lexing a macro, not a file?");
1154
1155 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1156 FoundNonSkipPortion, FoundElse);
1157
1158 // Enter raw mode to disable identifier lookup (and thus macro expansion),
1159 // disabling warnings, etc.
1160 CurLexer->LexingRawMode = true;
1161 LexerToken Tok;
1162 while (1) {
1163 CurLexer->Lex(Tok);
1164
1165 // If this is the end of the buffer, we have an error.
1166 if (Tok.getKind() == tok::eof) {
1167 // Emit errors for each unterminated conditional on the stack, including
1168 // the current one.
1169 while (!CurLexer->ConditionalStack.empty()) {
1170 Diag(CurLexer->ConditionalStack.back().IfLoc,
1171 diag::err_pp_unterminated_conditional);
1172 CurLexer->ConditionalStack.pop_back();
1173 }
1174
1175 // Just return and let the caller lex after this #include.
1176 break;
1177 }
1178
1179 // If this token is not a preprocessor directive, just skip it.
1180 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
1181 continue;
1182
1183 // We just parsed a # character at the start of a line, so we're in
1184 // directive mode. Tell the lexer this so any newlines we see will be
1185 // converted into an EOM token (this terminates the macro).
1186 CurLexer->ParsingPreprocessorDirective = true;
1187 CurLexer->KeepCommentMode = false;
1188
1189
1190 // Read the next token, the directive flavor.
1191 LexUnexpandedToken(Tok);
1192
1193 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1194 // something bogus), skip it.
1195 if (Tok.getKind() != tok::identifier) {
1196 CurLexer->ParsingPreprocessorDirective = false;
1197 // Restore comment saving mode.
1198 CurLexer->KeepCommentMode = KeepComments;
1199 continue;
1200 }
1201
1202 // If the first letter isn't i or e, it isn't intesting to us. We know that
1203 // this is safe in the face of spelling differences, because there is no way
1204 // to spell an i/e in a strange way that is another letter. Skipping this
1205 // allows us to avoid looking up the identifier info for #define/#undef and
1206 // other common directives.
1207 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1208 char FirstChar = RawCharData[0];
1209 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1210 FirstChar != 'i' && FirstChar != 'e') {
1211 CurLexer->ParsingPreprocessorDirective = false;
1212 // Restore comment saving mode.
1213 CurLexer->KeepCommentMode = KeepComments;
1214 continue;
1215 }
1216
1217 // Get the identifier name without trigraphs or embedded newlines. Note
1218 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1219 // when skipping.
1220 // TODO: could do this with zero copies in the no-clean case by using
1221 // strncmp below.
1222 char Directive[20];
1223 unsigned IdLen;
1224 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1225 IdLen = Tok.getLength();
1226 memcpy(Directive, RawCharData, IdLen);
1227 Directive[IdLen] = 0;
1228 } else {
1229 std::string DirectiveStr = getSpelling(Tok);
1230 IdLen = DirectiveStr.size();
1231 if (IdLen >= 20) {
1232 CurLexer->ParsingPreprocessorDirective = false;
1233 // Restore comment saving mode.
1234 CurLexer->KeepCommentMode = KeepComments;
1235 continue;
1236 }
1237 memcpy(Directive, &DirectiveStr[0], IdLen);
1238 Directive[IdLen] = 0;
1239 }
1240
1241 if (FirstChar == 'i' && Directive[1] == 'f') {
1242 if ((IdLen == 2) || // "if"
1243 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1244 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
1245 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1246 // bother parsing the condition.
1247 DiscardUntilEndOfDirective();
1248 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
1249 /*foundnonskip*/false,
1250 /*fnddelse*/false);
1251 }
1252 } else if (FirstChar == 'e') {
1253 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
1254 CheckEndOfDirective("#endif");
1255 PPConditionalInfo CondInfo;
1256 CondInfo.WasSkipping = true; // Silence bogus warning.
1257 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1258 InCond = InCond; // Silence warning in no-asserts mode.
1259 assert(!InCond && "Can't be skipping if not in a conditional!");
1260
1261 // If we popped the outermost skipping block, we're done skipping!
1262 if (!CondInfo.WasSkipping)
1263 break;
1264 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
1265 // #else directive in a skipping conditional. If not in some other
1266 // skipping conditional, and if #else hasn't already been seen, enter it
1267 // as a non-skipping conditional.
1268 CheckEndOfDirective("#else");
1269 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1270
1271 // If this is a #else with a #else before it, report the error.
1272 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
1273
1274 // Note that we've seen a #else in this conditional.
1275 CondInfo.FoundElse = true;
1276
1277 // If the conditional is at the top level, and the #if block wasn't
1278 // entered, enter the #else block now.
1279 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1280 CondInfo.FoundNonSkip = true;
1281 break;
1282 }
1283 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
1284 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1285
1286 bool ShouldEnter;
1287 // If this is in a skipping block or if we're already handled this #if
1288 // block, don't bother parsing the condition.
1289 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
1290 DiscardUntilEndOfDirective();
1291 ShouldEnter = false;
1292 } else {
1293 // Restore the value of LexingRawMode so that identifiers are
1294 // looked up, etc, inside the #elif expression.
1295 assert(CurLexer->LexingRawMode && "We have to be skipping here!");
1296 CurLexer->LexingRawMode = false;
1297 IdentifierInfo *IfNDefMacro = 0;
1298 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
1299 CurLexer->LexingRawMode = true;
1300 }
1301
1302 // If this is a #elif with a #else before it, report the error.
1303 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
1304
1305 // If this condition is true, enter it!
1306 if (ShouldEnter) {
1307 CondInfo.FoundNonSkip = true;
1308 break;
1309 }
1310 }
1311 }
1312
1313 CurLexer->ParsingPreprocessorDirective = false;
1314 // Restore comment saving mode.
1315 CurLexer->KeepCommentMode = KeepComments;
1316 }
1317
1318 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1319 // of the file, just stop skipping and return to lexing whatever came after
1320 // the #if block.
1321 CurLexer->LexingRawMode = false;
1322}
1323
1324//===----------------------------------------------------------------------===//
1325// Preprocessor Directive Handling.
1326//===----------------------------------------------------------------------===//
1327
1328/// HandleDirective - This callback is invoked when the lexer sees a # token
1329/// at the start of a line. This consumes the directive, modifies the
1330/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1331/// read is the correct one.
1332void Preprocessor::HandleDirective(LexerToken &Result) {
1333 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1334
1335 // We just parsed a # character at the start of a line, so we're in directive
1336 // mode. Tell the lexer this so any newlines we see will be converted into an
1337 // EOM token (which terminates the directive).
1338 CurLexer->ParsingPreprocessorDirective = true;
1339
1340 ++NumDirectives;
1341
1342 // We are about to read a token. For the multiple-include optimization FA to
1343 // work, we have to remember if we had read any tokens *before* this
1344 // pp-directive.
1345 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1346
1347 // Read the next token, the directive flavor. This isn't expanded due to
1348 // C99 6.10.3p8.
1349 LexUnexpandedToken(Result);
1350
1351 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1352 // #define A(x) #x
1353 // A(abc
1354 // #warning blah
1355 // def)
1356 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1357 if (InMacroArgs)
1358 Diag(Result, diag::ext_embedded_directive);
1359
1360TryAgain:
1361 switch (Result.getKind()) {
1362 case tok::eom:
1363 return; // null directive.
1364 case tok::comment:
1365 // Handle stuff like "# /*foo*/ define X" in -E -C mode.
1366 LexUnexpandedToken(Result);
1367 goto TryAgain;
1368
1369 case tok::numeric_constant:
1370 // FIXME: implement # 7 line numbers!
1371 DiscardUntilEndOfDirective();
1372 return;
1373 default:
1374 IdentifierInfo *II = Result.getIdentifierInfo();
1375 if (II == 0) break; // Not an identifier.
1376
1377 // Ask what the preprocessor keyword ID is.
1378 switch (II->getPPKeywordID()) {
1379 default: break;
1380 // C99 6.10.1 - Conditional Inclusion.
1381 case tok::pp_if:
1382 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
1383 case tok::pp_ifdef:
1384 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
1385 case tok::pp_ifndef:
1386 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
1387 case tok::pp_elif:
1388 return HandleElifDirective(Result);
1389 case tok::pp_else:
1390 return HandleElseDirective(Result);
1391 case tok::pp_endif:
1392 return HandleEndifDirective(Result);
1393
1394 // C99 6.10.2 - Source File Inclusion.
1395 case tok::pp_include:
1396 return HandleIncludeDirective(Result); // Handle #include.
1397
1398 // C99 6.10.3 - Macro Replacement.
1399 case tok::pp_define:
1400 return HandleDefineDirective(Result, false);
1401 case tok::pp_undef:
1402 return HandleUndefDirective(Result);
1403
1404 // C99 6.10.4 - Line Control.
1405 case tok::pp_line:
1406 // FIXME: implement #line
1407 DiscardUntilEndOfDirective();
1408 return;
1409
1410 // C99 6.10.5 - Error Directive.
1411 case tok::pp_error:
1412 return HandleUserDiagnosticDirective(Result, false);
1413
1414 // C99 6.10.6 - Pragma Directive.
1415 case tok::pp_pragma:
1416 return HandlePragmaDirective();
1417
1418 // GNU Extensions.
1419 case tok::pp_import:
1420 return HandleImportDirective(Result);
1421 case tok::pp_include_next:
1422 return HandleIncludeNextDirective(Result);
1423
1424 case tok::pp_warning:
1425 Diag(Result, diag::ext_pp_warning_directive);
1426 return HandleUserDiagnosticDirective(Result, true);
1427 case tok::pp_ident:
1428 return HandleIdentSCCSDirective(Result);
1429 case tok::pp_sccs:
1430 return HandleIdentSCCSDirective(Result);
1431 case tok::pp_assert:
1432 //isExtension = true; // FIXME: implement #assert
1433 break;
1434 case tok::pp_unassert:
1435 //isExtension = true; // FIXME: implement #unassert
1436 break;
1437
1438 // clang extensions.
1439 case tok::pp_define_target:
1440 return HandleDefineDirective(Result, true);
1441 case tok::pp_define_other_target:
1442 return HandleDefineOtherTargetDirective(Result);
1443 }
1444 break;
1445 }
1446
1447 // If we reached here, the preprocessing token is not valid!
1448 Diag(Result, diag::err_pp_invalid_directive);
1449
1450 // Read the rest of the PP line.
1451 DiscardUntilEndOfDirective();
1452
1453 // Okay, we're done parsing the directive.
1454}
1455
1456void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
1457 bool isWarning) {
1458 // Read the rest of the line raw. We do this because we don't want macros
1459 // to be expanded and we don't require that the tokens be valid preprocessing
1460 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1461 // collapse multiple consequtive white space between tokens, but this isn't
1462 // specified by the standard.
1463 std::string Message = CurLexer->ReadToEndOfLine();
1464
1465 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1466 return Diag(Tok, DiagID, Message);
1467}
1468
1469/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1470///
1471void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
1472 // Yes, this directive is an extension.
1473 Diag(Tok, diag::ext_pp_ident_directive);
1474
1475 // Read the string argument.
1476 LexerToken StrTok;
1477 Lex(StrTok);
1478
1479 // If the token kind isn't a string, it's a malformed directive.
1480 if (StrTok.getKind() != tok::string_literal &&
1481 StrTok.getKind() != tok::wide_string_literal)
1482 return Diag(StrTok, diag::err_pp_malformed_ident);
1483
1484 // Verify that there is nothing after the string, other than EOM.
1485 CheckEndOfDirective("#ident");
1486
1487 if (Callbacks)
1488 Callbacks->Ident(Tok.getLocation(), getSpelling(StrTok));
1489}
1490
1491//===----------------------------------------------------------------------===//
1492// Preprocessor Include Directive Handling.
1493//===----------------------------------------------------------------------===//
1494
1495/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1496/// checked and spelled filename, e.g. as an operand of #include. This returns
1497/// true if the input filename was in <>'s or false if it were in ""'s. The
1498/// caller is expected to provide a buffer that is large enough to hold the
1499/// spelling of the filename, but is also expected to handle the case when
1500/// this method decides to use a different buffer.
1501bool Preprocessor::GetIncludeFilenameSpelling(const LexerToken &FilenameTok,
1502 const char *&BufStart,
1503 const char *&BufEnd) {
1504 // Get the text form of the filename.
1505 unsigned Len = getSpelling(FilenameTok, BufStart);
1506 BufEnd = BufStart+Len;
1507 assert(BufStart != BufEnd && "Can't have tokens with empty spellings!");
1508
1509 // Make sure the filename is <x> or "x".
1510 bool isAngled;
1511 if (BufStart[0] == '<') {
1512 if (BufEnd[-1] != '>') {
1513 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1514 BufStart = 0;
1515 return true;
1516 }
1517 isAngled = true;
1518 } else if (BufStart[0] == '"') {
1519 if (BufEnd[-1] != '"') {
1520 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1521 BufStart = 0;
1522 return true;
1523 }
1524 isAngled = false;
1525 } else {
1526 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1527 BufStart = 0;
1528 return true;
1529 }
1530
1531 // Diagnose #include "" as invalid.
1532 if (BufEnd-BufStart <= 2) {
1533 Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename);
1534 BufStart = 0;
1535 return "";
1536 }
1537
1538 // Skip the brackets.
1539 ++BufStart;
1540 --BufEnd;
1541 return isAngled;
1542}
1543
1544/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1545/// file to be included from the lexer, then include it! This is a common
1546/// routine with functionality shared between #include, #include_next and
1547/// #import.
1548void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
1549 const DirectoryLookup *LookupFrom,
1550 bool isImport) {
1551
1552 LexerToken FilenameTok;
1553 CurLexer->LexIncludeFilename(FilenameTok);
1554
1555 // If the token kind is EOM, the error has already been diagnosed.
1556 if (FilenameTok.getKind() == tok::eom)
1557 return;
1558
1559 // Reserve a buffer to get the spelling.
1560 llvm::SmallVector<char, 128> FilenameBuffer;
1561 FilenameBuffer.resize(FilenameTok.getLength());
1562
1563 const char *FilenameStart = &FilenameBuffer[0], *FilenameEnd;
1564 bool isAngled = GetIncludeFilenameSpelling(FilenameTok,
1565 FilenameStart, FilenameEnd);
1566 // If GetIncludeFilenameSpelling set the start ptr to null, there was an
1567 // error.
1568 if (FilenameStart == 0)
1569 return;
1570
1571 // Verify that there is nothing after the filename, other than EOM. Use the
1572 // preprocessor to lex this in case lexing the filename entered a macro.
1573 CheckEndOfDirective("#include");
1574
1575 // Check that we don't have infinite #include recursion.
1576 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
1577 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1578
1579 // Search include directories.
1580 const DirectoryLookup *CurDir;
1581 const FileEntry *File = LookupFile(FilenameStart, FilenameEnd,
1582 isAngled, LookupFrom, CurDir);
1583 if (File == 0)
1584 return Diag(FilenameTok, diag::err_pp_file_not_found,
1585 std::string(FilenameStart, FilenameEnd));
1586
1587 // Ask HeaderInfo if we should enter this #include file.
1588 if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) {
1589 // If it returns true, #including this file will have no effect.
1590 return;
1591 }
1592
1593 // Look up the file, create a File ID for it.
1594 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
1595 if (FileID == 0)
1596 return Diag(FilenameTok, diag::err_pp_file_not_found,
1597 std::string(FilenameStart, FilenameEnd));
1598
1599 // Finally, if all is good, enter the new file!
1600 EnterSourceFile(FileID, CurDir);
1601}
1602
1603/// HandleIncludeNextDirective - Implements #include_next.
1604///
1605void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1606 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
1607
1608 // #include_next is like #include, except that we start searching after
1609 // the current found directory. If we can't do this, issue a
1610 // diagnostic.
1611 const DirectoryLookup *Lookup = CurDirLookup;
1612 if (isInPrimaryFile()) {
1613 Lookup = 0;
1614 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1615 } else if (Lookup == 0) {
1616 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1617 } else {
1618 // Start looking up in the next directory.
1619 ++Lookup;
1620 }
1621
1622 return HandleIncludeDirective(IncludeNextTok, Lookup);
1623}
1624
1625/// HandleImportDirective - Implements #import.
1626///
1627void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1628 Diag(ImportTok, diag::ext_pp_import_directive);
1629
1630 return HandleIncludeDirective(ImportTok, 0, true);
1631}
1632
1633//===----------------------------------------------------------------------===//
1634// Preprocessor Macro Directive Handling.
1635//===----------------------------------------------------------------------===//
1636
1637/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1638/// definition has just been read. Lex the rest of the arguments and the
1639/// closing ), updating MI with what we learn. Return true if an error occurs
1640/// parsing the arg list.
1641bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1642 LexerToken Tok;
1643 while (1) {
1644 LexUnexpandedToken(Tok);
1645 switch (Tok.getKind()) {
1646 case tok::r_paren:
1647 // Found the end of the argument list.
1648 if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
1649 // Otherwise we have #define FOO(A,)
1650 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1651 return true;
1652 case tok::ellipsis: // #define X(... -> C99 varargs
1653 // Warn if use of C99 feature in non-C99 mode.
1654 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1655
1656 // Lex the token after the identifier.
1657 LexUnexpandedToken(Tok);
1658 if (Tok.getKind() != tok::r_paren) {
1659 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1660 return true;
1661 }
1662 // Add the __VA_ARGS__ identifier as an argument.
1663 MI->addArgument(Ident__VA_ARGS__);
1664 MI->setIsC99Varargs();
1665 return false;
1666 case tok::eom: // #define X(
1667 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1668 return true;
1669 default:
1670 // Handle keywords and identifiers here to accept things like
1671 // #define Foo(for) for.
1672 IdentifierInfo *II = Tok.getIdentifierInfo();
1673 if (II == 0) {
1674 // #define X(1
1675 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1676 return true;
1677 }
1678
1679 // If this is already used as an argument, it is used multiple times (e.g.
1680 // #define X(A,A.
1681 if (MI->getArgumentNum(II) != -1) { // C99 6.10.3p6
1682 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
1683 return true;
1684 }
1685
1686 // Add the argument to the macro info.
1687 MI->addArgument(II);
1688
1689 // Lex the token after the identifier.
1690 LexUnexpandedToken(Tok);
1691
1692 switch (Tok.getKind()) {
1693 default: // #define X(A B
1694 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1695 return true;
1696 case tok::r_paren: // #define X(A)
1697 return false;
1698 case tok::comma: // #define X(A,
1699 break;
1700 case tok::ellipsis: // #define X(A... -> GCC extension
1701 // Diagnose extension.
1702 Diag(Tok, diag::ext_named_variadic_macro);
1703
1704 // Lex the token after the identifier.
1705 LexUnexpandedToken(Tok);
1706 if (Tok.getKind() != tok::r_paren) {
1707 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1708 return true;
1709 }
1710
1711 MI->setIsGNUVarargs();
1712 return false;
1713 }
1714 }
1715 }
1716}
1717
1718/// HandleDefineDirective - Implements #define. This consumes the entire macro
1719/// line then lets the caller lex the next real token. If 'isTargetSpecific' is
1720/// true, then this is a "#define_target", otherwise this is a "#define".
1721///
1722void Preprocessor::HandleDefineDirective(LexerToken &DefineTok,
1723 bool isTargetSpecific) {
1724 ++NumDefined;
1725
1726 LexerToken MacroNameTok;
1727 ReadMacroName(MacroNameTok, 1);
1728
1729 // Error reading macro name? If so, diagnostic already issued.
1730 if (MacroNameTok.getKind() == tok::eom)
1731 return;
1732
1733 // If we are supposed to keep comments in #defines, reenable comment saving
1734 // mode.
1735 CurLexer->KeepCommentMode = KeepMacroComments;
1736
1737 // Create the new macro.
1738 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
1739 if (isTargetSpecific) MI->setIsTargetSpecific();
1740
1741 // If the identifier is an 'other target' macro, clear this bit.
1742 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
1743
1744
1745 LexerToken Tok;
1746 LexUnexpandedToken(Tok);
1747
1748 // If this is a function-like macro definition, parse the argument list,
1749 // marking each of the identifiers as being used as macro arguments. Also,
1750 // check other constraints on the first token of the macro body.
1751 if (Tok.getKind() == tok::eom) {
1752 // If there is no body to this macro, we have no special handling here.
1753 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1754 // This is a function-like macro definition. Read the argument list.
1755 MI->setIsFunctionLike();
1756 if (ReadMacroDefinitionArgList(MI)) {
1757 // Forget about MI.
1758 delete MI;
1759 // Throw away the rest of the line.
1760 if (CurLexer->ParsingPreprocessorDirective)
1761 DiscardUntilEndOfDirective();
1762 return;
1763 }
1764
1765 // Read the first token after the arg list for down below.
1766 LexUnexpandedToken(Tok);
1767 } else if (!Tok.hasLeadingSpace()) {
1768 // C99 requires whitespace between the macro definition and the body. Emit
1769 // a diagnostic for something like "#define X+".
1770 if (Features.C99) {
1771 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
1772 } else {
1773 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1774 // one in some cases!
1775 }
1776 } else {
1777 // This is a normal token with leading space. Clear the leading space
1778 // marker on the first token to get proper expansion.
1779 Tok.clearFlag(LexerToken::LeadingSpace);
1780 }
1781
1782 // If this is a definition of a variadic C99 function-like macro, not using
1783 // the GNU named varargs extension, enabled __VA_ARGS__.
1784
1785 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
1786 // This gets unpoisoned where it is allowed.
1787 assert(Ident__VA_ARGS__->isPoisoned() && "__VA_ARGS__ should be poisoned!");
1788 if (MI->isC99Varargs())
1789 Ident__VA_ARGS__->setIsPoisoned(false);
1790
1791 // Read the rest of the macro body.
1792 while (Tok.getKind() != tok::eom) {
1793 MI->AddTokenToBody(Tok);
1794
1795 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
1796 // parameters in function-like macro expansions.
1797 if (Tok.getKind() != tok::hash || MI->isObjectLike()) {
1798 // Get the next token of the macro.
1799 LexUnexpandedToken(Tok);
1800 continue;
1801 }
1802
1803 // Get the next token of the macro.
1804 LexUnexpandedToken(Tok);
1805
1806 // Not a macro arg identifier?
1807 if (!Tok.getIdentifierInfo() ||
1808 MI->getArgumentNum(Tok.getIdentifierInfo()) == -1) {
1809 Diag(Tok, diag::err_pp_stringize_not_parameter);
1810 delete MI;
1811
1812 // Disable __VA_ARGS__ again.
1813 Ident__VA_ARGS__->setIsPoisoned(true);
1814 return;
1815 }
1816
1817 // Things look ok, add the param name token to the macro.
1818 MI->AddTokenToBody(Tok);
1819
1820 // Get the next token of the macro.
1821 LexUnexpandedToken(Tok);
1822 }
1823
1824 // Disable __VA_ARGS__ again.
1825 Ident__VA_ARGS__->setIsPoisoned(true);
1826
1827 // Check that there is no paste (##) operator at the begining or end of the
1828 // replacement list.
1829 unsigned NumTokens = MI->getNumTokens();
1830 if (NumTokens != 0) {
1831 if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
1832 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
1833 delete MI;
1834 return;
1835 }
1836 if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
1837 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
1838 delete MI;
1839 return;
1840 }
1841 }
1842
1843 // If this is the primary source file, remember that this macro hasn't been
1844 // used yet.
1845 if (isInPrimaryFile())
1846 MI->setIsUsed(false);
1847
1848 // Finally, if this identifier already had a macro defined for it, verify that
1849 // the macro bodies are identical and free the old definition.
1850 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
1851 if (!OtherMI->isUsed())
1852 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1853
1854 // Macros must be identical. This means all tokes and whitespace separation
1855 // must be the same. C99 6.10.3.2.
1856 if (!MI->isIdenticalTo(*OtherMI, *this)) {
1857 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
1858 MacroNameTok.getIdentifierInfo()->getName());
1859 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
1860 }
1861 delete OtherMI;
1862 }
1863
1864 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
1865}
1866
1867/// HandleDefineOtherTargetDirective - Implements #define_other_target.
1868void Preprocessor::HandleDefineOtherTargetDirective(LexerToken &Tok) {
1869 LexerToken MacroNameTok;
1870 ReadMacroName(MacroNameTok, 1);
1871
1872 // Error reading macro name? If so, diagnostic already issued.
1873 if (MacroNameTok.getKind() == tok::eom)
1874 return;
1875
1876 // Check to see if this is the last token on the #undef line.
1877 CheckEndOfDirective("#define_other_target");
1878
1879 // If there is already a macro defined by this name, turn it into a
1880 // target-specific define.
1881 if (MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
1882 MI->setIsTargetSpecific(true);
1883 return;
1884 }
1885
1886 // Mark the identifier as being a macro on some other target.
1887 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro();
1888}
1889
1890
1891/// HandleUndefDirective - Implements #undef.
1892///
1893void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
1894 ++NumUndefined;
1895
1896 LexerToken MacroNameTok;
1897 ReadMacroName(MacroNameTok, 2);
1898
1899 // Error reading macro name? If so, diagnostic already issued.
1900 if (MacroNameTok.getKind() == tok::eom)
1901 return;
1902
1903 // Check to see if this is the last token on the #undef line.
1904 CheckEndOfDirective("#undef");
1905
1906 // Okay, we finally have a valid identifier to undef.
1907 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1908
1909 // #undef untaints an identifier if it were marked by define_other_target.
1910 MacroNameTok.getIdentifierInfo()->setIsOtherTargetMacro(false);
1911
1912 // If the macro is not defined, this is a noop undef, just return.
1913 if (MI == 0) return;
1914
1915 if (!MI->isUsed())
1916 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
1917
1918 // Free macro definition.
1919 delete MI;
1920 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
1921}
1922
1923
1924//===----------------------------------------------------------------------===//
1925// Preprocessor Conditional Directive Handling.
1926//===----------------------------------------------------------------------===//
1927
1928/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1929/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1930/// if any tokens have been returned or pp-directives activated before this
1931/// #ifndef has been lexed.
1932///
1933void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1934 bool ReadAnyTokensBeforeDirective) {
1935 ++NumIf;
1936 LexerToken DirectiveTok = Result;
1937
1938 LexerToken MacroNameTok;
1939 ReadMacroName(MacroNameTok);
1940
1941 // Error reading macro name? If so, diagnostic already issued.
1942 if (MacroNameTok.getKind() == tok::eom)
1943 return;
1944
1945 // Check to see if this is the last token on the #if[n]def line.
1946 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1947
1948 // If the start of a top-level #ifdef, inform MIOpt.
1949 if (!ReadAnyTokensBeforeDirective &&
1950 CurLexer->getConditionalStackDepth() == 0) {
1951 assert(isIfndef && "#ifdef shouldn't reach here");
1952 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1953 }
1954
1955 IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
1956 MacroInfo *MI = MII->getMacroInfo();
1957
1958 // If there is a macro, process it.
1959 if (MI) {
1960 // Mark it used.
1961 MI->setIsUsed(true);
1962
1963 // If this is the first use of a target-specific macro, warn about it.
1964 if (MI->isTargetSpecific()) {
1965 MI->setIsTargetSpecific(false); // Don't warn on second use.
1966 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
1967 diag::port_target_macro_use);
1968 }
1969 } else {
1970 // Use of a target-specific macro for some other target? If so, warn.
1971 if (MII->isOtherTargetMacro()) {
1972 MII->setIsOtherTargetMacro(false); // Don't warn on second use.
1973 getTargetInfo().DiagnoseNonPortability(MacroNameTok.getLocation(),
1974 diag::port_target_macro_use);
1975 }
1976 }
1977
1978 // Should we include the stuff contained by this directive?
1979 if (!MI == isIfndef) {
1980 // Yes, remember that we are inside a conditional, then lex the next token.
1981 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
1982 /*foundnonskip*/true, /*foundelse*/false);
1983 } else {
1984 // No, skip the contents of this block and return the first token after it.
1985 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
1986 /*Foundnonskip*/false,
1987 /*FoundElse*/false);
1988 }
1989}
1990
1991/// HandleIfDirective - Implements the #if directive.
1992///
1993void Preprocessor::HandleIfDirective(LexerToken &IfToken,
1994 bool ReadAnyTokensBeforeDirective) {
1995 ++NumIf;
1996
1997 // Parse and evaluation the conditional expression.
1998 IdentifierInfo *IfNDefMacro = 0;
1999 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
2000
2001 // Should we include the stuff contained by this directive?
2002 if (ConditionalTrue) {
2003 // If this condition is equivalent to #ifndef X, and if this is the first
2004 // directive seen, handle it for the multiple-include optimization.
2005 if (!ReadAnyTokensBeforeDirective &&
2006 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
2007 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
2008
2009 // Yes, remember that we are inside a conditional, then lex the next token.
2010 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
2011 /*foundnonskip*/true, /*foundelse*/false);
2012 } else {
2013 // No, skip the contents of this block and return the first token after it.
2014 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
2015 /*FoundElse*/false);
2016 }
2017}
2018
2019/// HandleEndifDirective - Implements the #endif directive.
2020///
2021void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
2022 ++NumEndif;
2023
2024 // Check that this is the whole directive.
2025 CheckEndOfDirective("#endif");
2026
2027 PPConditionalInfo CondInfo;
2028 if (CurLexer->popConditionalLevel(CondInfo)) {
2029 // No conditionals on the stack: this is an #endif without an #if.
2030 return Diag(EndifToken, diag::err_pp_endif_without_if);
2031 }
2032
2033 // If this the end of a top-level #endif, inform MIOpt.
2034 if (CurLexer->getConditionalStackDepth() == 0)
2035 CurLexer->MIOpt.ExitTopLevelConditional();
2036
2037 assert(!CondInfo.WasSkipping && !CurLexer->LexingRawMode &&
2038 "This code should only be reachable in the non-skipping case!");
2039}
2040
2041
2042void Preprocessor::HandleElseDirective(LexerToken &Result) {
2043 ++NumElse;
2044
2045 // #else directive in a non-skipping conditional... start skipping.
2046 CheckEndOfDirective("#else");
2047
2048 PPConditionalInfo CI;
2049 if (CurLexer->popConditionalLevel(CI))
2050 return Diag(Result, diag::pp_err_else_without_if);
2051
2052 // If this is a top-level #else, inform the MIOpt.
2053 if (CurLexer->getConditionalStackDepth() == 0)
2054 CurLexer->MIOpt.FoundTopLevelElse();
2055
2056 // If this is a #else with a #else before it, report the error.
2057 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
2058
2059 // Finally, skip the rest of the contents of this block and return the first
2060 // token after it.
2061 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2062 /*FoundElse*/true);
2063}
2064
2065void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
2066 ++NumElse;
2067
2068 // #elif directive in a non-skipping conditional... start skipping.
2069 // We don't care what the condition is, because we will always skip it (since
2070 // the block immediately before it was included).
2071 DiscardUntilEndOfDirective();
2072
2073 PPConditionalInfo CI;
2074 if (CurLexer->popConditionalLevel(CI))
2075 return Diag(ElifToken, diag::pp_err_elif_without_if);
2076
2077 // If this is a top-level #elif, inform the MIOpt.
2078 if (CurLexer->getConditionalStackDepth() == 0)
2079 CurLexer->MIOpt.FoundTopLevelElse();
2080
2081 // If this is a #elif with a #else before it, report the error.
2082 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
2083
2084 // Finally, skip the rest of the contents of this block and return the first
2085 // token after it.
2086 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
2087 /*FoundElse*/CI.FoundElse);
2088}
2089