blob: 7ea3bbae4902355b73668608ac8d6fa8aac5ba05 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +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// TODO: GCC Diagnostics emitted by the lexer:
15//
16// ERROR : __VA_ARGS__ can only appear in the expansion of a C99 variadic macro
17//
18// Options to support:
19// -H - Print the name of each header file used.
20// -C -CC - Do not discard comments for cpp.
21// -P - Do not emit #line directives.
22// -d[MDNI] - Dump various things.
23// -fworking-directory - #line's with preprocessor's working dir.
24// -fpreprocessed
25// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
26// -W*
27// -w
28//
29// Messages to emit:
30// "Multiple include guards may be useful for:\n"
31//
32// TODO: Implement the include guard optimization.
33//
34//===----------------------------------------------------------------------===//
35
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Basic/Diagnostic.h"
39#include "clang/Basic/FileManager.h"
40#include "clang/Basic/SourceManager.h"
41#include <iostream>
42using namespace llvm;
43using namespace clang;
44
45//===----------------------------------------------------------------------===//
46
47Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
48 FileManager &FM, SourceManager &SM)
49 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
50 SystemDirIdx(0), NoCurDirSearch(false),
51 CurLexer(0), CurNextDirLookup(0), CurMacroExpander(0) {
52 // Clear stats.
53 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
54 NumIf = NumElse = NumEndif = 0;
55 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
56 MaxIncludeStackDepth = MaxMacroStackDepth = 0;
57 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000058
Chris Lattner22eb9722006-06-18 05:43:12 +000059 // Macro expansion is enabled.
60 DisableMacroExpansion = false;
61 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000062
63 // There is no file-change handler yet.
64 FileChangeHandler = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000065}
66
67Preprocessor::~Preprocessor() {
68 // Free any active lexers.
69 delete CurLexer;
70
71 while (!IncludeStack.empty()) {
72 delete IncludeStack.back().TheLexer;
73 IncludeStack.pop_back();
74 }
75}
76
77/// getFileInfo - Return the PerFileInfo structure for the specified
78/// FileEntry.
79Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
80 if (FE->getUID() >= FileInfo.size())
81 FileInfo.resize(FE->getUID()+1);
82 return FileInfo[FE->getUID()];
83}
84
85
86/// AddKeywords - Add all keywords to the symbol table.
87///
88void Preprocessor::AddKeywords() {
89 enum {
90 C90Shift = 0,
91 EXTC90 = 1 << C90Shift,
92 NOTC90 = 2 << C90Shift,
93 C99Shift = 2,
94 EXTC99 = 1 << C99Shift,
95 NOTC99 = 2 << C99Shift,
96 CPPShift = 4,
97 EXTCPP = 1 << CPPShift,
98 NOTCPP = 2 << CPPShift,
99 Mask = 3
100 };
101
102 // Add keywords and tokens for the current language.
103#define KEYWORD(NAME, FLAGS) \
104 AddKeyword(#NAME+1, tok::kw##NAME, \
105 (FLAGS >> C90Shift) & Mask, \
106 (FLAGS >> C99Shift) & Mask, \
107 (FLAGS >> CPPShift) & Mask);
108#define ALIAS(NAME, TOK) \
109 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
110#include "clang/Basic/TokenKinds.def"
111}
112
113/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
114/// the specified LexerToken's location, translating the token's start
115/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000116void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000117 const std::string &Msg) {
118 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
119 // warnings or extensions.
120 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000121 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000122
Chris Lattnercb283342006-06-18 06:48:37 +0000123 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000124}
Chris Lattnercb283342006-06-18 06:48:37 +0000125void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000126 const std::string &Msg) {
127 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
128 // warnings or extensions.
129 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000130 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000131
Chris Lattner50b497e2006-06-18 16:32:35 +0000132 Diag(Tok.getLocation(), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000133}
134
Chris Lattnerd01e2912006-06-18 16:22:51 +0000135
136void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
137 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
138 << getSpelling(Tok) << "'";
139
140 if (!DumpFlags) return;
141 std::cerr << "\t";
142 if (Tok.isAtStartOfLine())
143 std::cerr << " [StartOfLine]";
144 if (Tok.hasLeadingSpace())
145 std::cerr << " [LeadingSpace]";
146 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000147 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
149 << "']";
150 }
151}
152
153void Preprocessor::DumpMacro(const MacroInfo &MI) const {
154 std::cerr << "MACRO: ";
155 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
156 DumpToken(MI.getReplacementToken(i));
157 std::cerr << " ";
158 }
159 std::cerr << "\n";
160}
161
Chris Lattner22eb9722006-06-18 05:43:12 +0000162void Preprocessor::PrintStats() {
163 std::cerr << "\n*** Preprocessor Stats:\n";
164 std::cerr << FileInfo.size() << " files tracked.\n";
165 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
166 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
167 NumOnceOnlyFiles += FileInfo[i].isImport;
168 if (MaxNumIncludes < FileInfo[i].NumIncludes)
169 MaxNumIncludes = FileInfo[i].NumIncludes;
170 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
171 }
172 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
173 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
174 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
175
176 std::cerr << NumDirectives << " directives found:\n";
177 std::cerr << " " << NumDefined << " #define.\n";
178 std::cerr << " " << NumUndefined << " #undef.\n";
179 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
180 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
181 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
182 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
183 std::cerr << " " << NumElse << " #else/#elif.\n";
184 std::cerr << " " << NumEndif << " #endif.\n";
185 std::cerr << " " << NumPragma << " #pragma.\n";
186 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
187
188 std::cerr << NumMacroExpanded << " macros expanded, "
189 << NumFastMacroExpanded << " on the fast path.\n";
190 if (MaxMacroStackDepth > 1)
191 std::cerr << " " << MaxMacroStackDepth << " max macroexpand stack depth\n";
192}
193
194//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000195// Token Spelling
196//===----------------------------------------------------------------------===//
197
198
199/// getSpelling() - Return the 'spelling' of this token. The spelling of a
200/// token are the characters used to represent the token in the source file
201/// after trigraph expansion and escaped-newline folding. In particular, this
202/// wants to get the true, uncanonicalized, spelling of things like digraphs
203/// UCNs, etc.
204std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
205 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
206
207 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000208 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000209 assert(TokStart && "Token has invalid location!");
210 if (!Tok.needsCleaning())
211 return std::string(TokStart, TokStart+Tok.getLength());
212
213 // Otherwise, hard case, relex the characters into the string.
214 std::string Result;
215 Result.reserve(Tok.getLength());
216
217 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
218 Ptr != End; ) {
219 unsigned CharSize;
220 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
221 Ptr += CharSize;
222 }
223 assert(Result.size() != unsigned(Tok.getLength()) &&
224 "NeedsCleaning flag set on something that didn't need cleaning!");
225 return Result;
226}
227
228/// getSpelling - This method is used to get the spelling of a token into a
229/// preallocated buffer, instead of as an std::string. The caller is required
230/// to allocate enough space for the token, which is guaranteed to be at least
231/// Tok.getLength() bytes long. The actual length of the token is returned.
232unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
233 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
234
Chris Lattner50b497e2006-06-18 16:32:35 +0000235 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000236 assert(TokStart && "Token has invalid location!");
237
238 // If this token contains nothing interesting, return it directly.
239 if (!Tok.needsCleaning()) {
240 unsigned Size = Tok.getLength();
241 memcpy(Buffer, TokStart, Size);
242 return Size;
243 }
244 // Otherwise, hard case, relex the characters into the string.
245 std::string Result;
246 Result.reserve(Tok.getLength());
247
248 char *OutBuf = Buffer;
249 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
250 Ptr != End; ) {
251 unsigned CharSize;
252 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
253 Ptr += CharSize;
254 }
255 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
256 "NeedsCleaning flag set on something that didn't need cleaning!");
257
258 return OutBuf-Buffer;
259}
260
261//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000262// Source File Location Methods.
263//===----------------------------------------------------------------------===//
264
265
266/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
267/// return null on failure. isAngled indicates whether the file reference is
268/// for system #include's or not (i.e. using <> instead of "").
269const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
270 bool isSystem,
271 const DirectoryLookup *FromDir,
272 const DirectoryLookup *&NextDir) {
273 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
274 NextDir = 0;
275
276 // If 'Filename' is absolute, check to see if it exists and no searching.
277 // FIXME: this should be a sys::Path interface, this doesn't handle things
278 // like C:\foo.txt right, nor win32 \\network\device\blah.
279 if (Filename[0] == '/') {
280 // If this was an #include_next "/absolute/file", fail.
281 if (FromDir) return 0;
282
283 // Otherwise, just return the file.
284 return FileMgr.getFile(Filename);
285 }
286
287 // Step #0, unless disabled, check to see if the file is in the #includer's
288 // directory. This search is not done for <> headers.
289 if (!isSystem && !FromDir && !NoCurDirSearch) {
290 const FileEntry *CurFE =
291 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
292 if (CurFE) {
293 if (const FileEntry *FE =
294 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
295 if (CurNextDirLookup)
296 NextDir = CurNextDirLookup;
297 else
298 NextDir = &SearchDirs[0];
299 return FE;
300 }
301 }
302 }
303
304 // If this is a system #include, ignore the user #include locs.
305 unsigned i = isSystem ? SystemDirIdx : 0;
306
307 // If this is a #include_next request, start searching after the directory the
308 // file was found in.
309 if (FromDir)
310 i = FromDir-&SearchDirs[0];
311
312 // Check each directory in sequence to see if it contains this file.
313 for (; i != SearchDirs.size(); ++i) {
314 // Concatenate the requested file onto the directory.
315 // FIXME: should be in sys::Path.
316 if (const FileEntry *FE =
317 FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
318 NextDir = &SearchDirs[i+1];
319 return FE;
320 }
321 }
322
323 // Otherwise, didn't find it.
324 return 0;
325}
326
327/// EnterSourceFile - Add a source file to the top of the include stack and
328/// start lexing tokens from it instead of the current buffer. Return true
329/// on failure.
330void Preprocessor::EnterSourceFile(unsigned FileID,
331 const DirectoryLookup *NextDir) {
332 ++NumEnteredSourceFiles;
333
334 // Add the current lexer to the include stack.
335 if (CurLexer) {
336 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurNextDirLookup));
337 } else {
338 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
339 }
340
341 if (MaxIncludeStackDepth < IncludeStack.size())
342 MaxIncludeStackDepth = IncludeStack.size();
343
344 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
345
346 CurLexer = new Lexer(Buffer, FileID, *this);
347 CurNextDirLookup = NextDir;
Chris Lattner0c885f52006-06-21 06:50:18 +0000348
349 // Notify the client, if desired, that we are in a new source file.
350 if (FileChangeHandler)
351 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart), true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000352}
353
354/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000355/// tokens from it instead of the current buffer.
356void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000357 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
358 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000359 if (CurLexer) {
360 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurNextDirLookup));
361 CurLexer = 0;
362 CurNextDirLookup = 0;
363 } else if (CurMacroExpander) {
364 MacroStack.push_back(CurMacroExpander);
365 }
366
367 if (MaxMacroStackDepth < MacroStack.size())
368 MaxMacroStackDepth = MacroStack.size();
369
370 // TODO: Figure out arguments.
371
372 // Mark the macro as currently disabled, so that it is not recursively
373 // expanded.
374 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000375 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000376}
377
378
379//===----------------------------------------------------------------------===//
380// Lexer Event Handling.
381//===----------------------------------------------------------------------===//
382
383/// HandleIdentifier - This callback is invoked when the lexer reads an
384/// identifier. This callback looks up the identifier in the map and/or
385/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnercb283342006-06-18 06:48:37 +0000386void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000387 if (Identifier.getIdentifierInfo() == 0) {
388 // If we are skipping tokens (because we are in a #if 0 block), there will
389 // be no identifier info, just return the token.
390 assert(isSkipping() && "Token isn't an identifier?");
Chris Lattnercb283342006-06-18 06:48:37 +0000391 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000392 }
393 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
394
395 // FIXME: Check for poisoning in ITI?
396
397 if (MacroInfo *MI = ITI.getMacroInfo()) {
398 if (MI->isEnabled() && !DisableMacroExpansion) {
399 ++NumMacroExpanded;
400 // If we started lexing a macro, enter the macro expansion body.
401 // FIXME: Read/Validate the argument list here!
402
403 // If this macro expands to no tokens, don't bother to push it onto the
404 // expansion stack, only to take it right back off.
405 if (MI->getNumTokens() == 0) {
406 // Ignore this macro use, just return the next token in the current
407 // buffer.
408 bool HadLeadingSpace = Identifier.hasLeadingSpace();
409 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
410
Chris Lattnercb283342006-06-18 06:48:37 +0000411 Lex(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000412
413 // If the identifier isn't on some OTHER line, inherit the leading
414 // whitespace/first-on-a-line property of this token. This handles
415 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
416 // empty.
417 if (!Identifier.isAtStartOfLine()) {
418 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
419 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
420 }
421 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000422 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000423
424 } else if (MI->getNumTokens() == 1 &&
425 // Don't handle identifiers, which might need recursive
426 // expansion.
427 MI->getReplacementToken(0).getIdentifierInfo() == 0) {
428 // FIXME: Function-style macros only if no arguments?
429
430 // Otherwise, if this macro expands into a single trivially-expanded
431 // token: expand it now. This handles common cases like
432 // "#define VAL 42".
433
434 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
435 // identifier to the expanded token.
436 bool isAtStartOfLine = Identifier.isAtStartOfLine();
437 bool hasLeadingSpace = Identifier.hasLeadingSpace();
438
439 // Replace the result token.
440 Identifier = MI->getReplacementToken(0);
441
442 // Restore the StartOfLine/LeadingSpace markers.
443 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
444 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
445
446 // FIXME: Get correct macro expansion stack location info!
447
448 // Since this is not an identifier token, it can't be macro expanded, so
449 // we're done.
450 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000451 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000452 }
453
454 // Start expanding the macro (FIXME, pass arguments).
Chris Lattnercb283342006-06-18 06:48:37 +0000455 EnterMacro(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000456
457 // Now that the macro is at the top of the include stack, ask the
458 // preprocessor to read the next token from it.
459 return Lex(Identifier);
460 }
461 }
462
463 // Change the kind of this identifier to the appropriate token kind, e.g.
464 // turning "for" into a keyword.
465 Identifier.SetKind(ITI.getTokenID());
466
467 // If this is an extension token, diagnose its use.
Chris Lattnercb283342006-06-18 06:48:37 +0000468 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner22eb9722006-06-18 05:43:12 +0000469}
470
471/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
472/// the current file. This either returns the EOF token or pops a level off
473/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000474void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000475 assert(!CurMacroExpander &&
476 "Ending a file when currently in a macro!");
477
478 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
479 // this is an error condition. Just return the EOF token up to
480 // SkipExcludedConditionalBlock. The Lexer will have already have issued
481 // errors for the unterminated #if's on the conditional stack.
482 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000483 Result.StartToken();
484 CurLexer->BufferPtr = CurLexer->BufferEnd;
485 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000486 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000487 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000488 }
489
490 // If this is a #include'd file, pop it off the include stack and continue
491 // lexing the #includer file.
492 if (!IncludeStack.empty()) {
493 // We're done with the #included file.
494 delete CurLexer;
495 CurLexer = IncludeStack.back().TheLexer;
496 CurNextDirLookup = IncludeStack.back().TheDirLookup;
497 IncludeStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000498
499 // Notify the client, if desired, that we are in a new source file.
500 if (FileChangeHandler && !isEndOfMacro)
501 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
502 false);
503
Chris Lattner22eb9722006-06-18 05:43:12 +0000504 return Lex(Result);
505 }
506
Chris Lattnerd01e2912006-06-18 16:22:51 +0000507 Result.StartToken();
508 CurLexer->BufferPtr = CurLexer->BufferEnd;
509 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000510 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000511
512 // We're done with the #included file.
513 delete CurLexer;
514 CurLexer = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000515}
516
517/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000518/// the current macro line.
519void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000520 assert(CurMacroExpander && !CurLexer &&
521 "Ending a macro when currently in a #include file!");
522
523 // Mark macro not ignored now that it is no longer being expanded.
524 CurMacroExpander->getMacro().EnableMacro();
525 delete CurMacroExpander;
526
527 if (!MacroStack.empty()) {
528 // In a nested macro invocation, continue lexing from the macro.
529 CurMacroExpander = MacroStack.back();
530 MacroStack.pop_back();
531 return Lex(Result);
532 } else {
533 CurMacroExpander = 0;
534 // Handle this like a #include file being popped off the stack.
Chris Lattner0c885f52006-06-21 06:50:18 +0000535 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000536 }
537}
538
539
540//===----------------------------------------------------------------------===//
541// Utility Methods for Preprocessor Directive Handling.
542//===----------------------------------------------------------------------===//
543
544/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
545/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000546void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000547 LexerToken Tmp;
548 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000549 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000550 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000551}
552
553/// ReadMacroName - Lex and validate a macro name, which occurs after a
554/// #define or #undef. This sets the token kind to eom and discards the rest
555/// of the macro line if the macro name is invalid.
Chris Lattnercb283342006-06-18 06:48:37 +0000556void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000557 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000558 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000559
560 // Missing macro name?
561 if (MacroNameTok.getKind() == tok::eom)
562 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
563
564 if (MacroNameTok.getIdentifierInfo() == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000565 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000566 // Fall through on error.
567 } else if (0) {
568 // FIXME: Error if defining a C++ named operator.
569
570 } else if (0) {
571 // FIXME: Error if defining "defined", "__DATE__", and other predef macros
572 // in C99 6.10.8.4.
573 } else {
574 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000575 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000576 }
577
578
579 // Invalid macro name, read and discard the rest of the line. Then set the
580 // token kind to tok::eom.
581 MacroNameTok.SetKind(tok::eom);
582 return DiscardUntilEndOfDirective();
583}
584
585/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
586/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000587void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000588 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000589 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000590 // There should be no tokens after the directive, but we allow them as an
591 // extension.
592 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000593 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
594 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000595 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000596}
597
598
599
600/// SkipExcludedConditionalBlock - We just read a #if or related directive and
601/// decided that the subsequent tokens are in the #if'd out portion of the
602/// file. Lex the rest of the file, until we see an #endif. If
603/// FoundNonSkipPortion is true, then we have already emitted code for part of
604/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
605/// is true, then #else directives are ok, if not, then we have already seen one
606/// so a #else directive is a duplicate. When this returns, the caller can lex
607/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000608void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000609 bool FoundNonSkipPortion,
610 bool FoundElse) {
611 ++NumSkipped;
612 assert(MacroStack.empty() && CurMacroExpander == 0 && CurLexer &&
613 "Lexing a macro, not a file?");
614
615 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
616 FoundNonSkipPortion, FoundElse);
617
618 // Know that we are going to be skipping tokens. Set this flag to indicate
619 // this, which has a couple of effects:
620 // 1. If EOF of the current lexer is found, the include stack isn't popped.
621 // 2. Identifier information is not looked up for identifier tokens. As an
622 // effect of this, implicit macro expansion is naturally disabled.
623 // 3. "#" tokens at the start of a line are treated as normal tokens, not
624 // implicitly transformed by the lexer.
625 // 4. All notes, warnings, and extension messages are disabled.
626 //
627 SkippingContents = true;
628 LexerToken Tok;
629 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000630 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000631
632 // If this is the end of the buffer, we have an error. The lexer will have
633 // already handled this error condition, so just return and let the caller
634 // lex after this #include.
635 if (Tok.getKind() == tok::eof) break;
636
637 // If this token is not a preprocessor directive, just skip it.
638 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
639 continue;
640
641 // We just parsed a # character at the start of a line, so we're in
642 // directive mode. Tell the lexer this so any newlines we see will be
643 // converted into an EOM token (this terminates the macro).
644 CurLexer->ParsingPreprocessorDirective = true;
645
646 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000647 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000648
649 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
650 // something bogus), skip it.
651 if (Tok.getKind() != tok::identifier) {
652 CurLexer->ParsingPreprocessorDirective = false;
653 continue;
654 }
655
656 // If the first letter isn't i or e, it isn't intesting to us. We know that
657 // this is safe in the face of spelling differences, because there is no way
658 // to spell an i/e in a strange way that is another letter. Skipping this
659 // allows us to avoid computing the spelling for #define/#undef and other
660 // common directives.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000661 // FIXME: This should use a bit in the identifier information!
Chris Lattner50b497e2006-06-18 16:32:35 +0000662 char FirstChar = SourceMgr.getCharacterData(Tok.getLocation())[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000663 if (FirstChar >= 'a' && FirstChar <= 'z' &&
664 FirstChar != 'i' && FirstChar != 'e') {
665 CurLexer->ParsingPreprocessorDirective = false;
666 continue;
667 }
668
669 // Strip out trigraphs and embedded newlines.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000670 std::string Directive = getSpelling(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000671 FirstChar = Directive[0];
672 if (FirstChar == 'i' && Directive[1] == 'f') {
673 if (Directive == "if" || Directive == "ifdef" || Directive == "ifndef") {
674 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
675 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000676 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000677 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000678 /*foundnonskip*/false,
679 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000680 }
681 } else if (FirstChar == 'e') {
682 if (Directive == "endif") {
Chris Lattnercb283342006-06-18 06:48:37 +0000683 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000684 PPConditionalInfo CondInfo;
685 CondInfo.WasSkipping = true; // Silence bogus warning.
686 bool InCond = CurLexer->popConditionalLevel(CondInfo);
687 assert(!InCond && "Can't be skipping if not in a conditional!");
688
689 // If we popped the outermost skipping block, we're done skipping!
690 if (!CondInfo.WasSkipping)
691 break;
692 } else if (Directive == "else") {
693 // #else directive in a skipping conditional. If not in some other
694 // skipping conditional, and if #else hasn't already been seen, enter it
695 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000696 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000697 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
698
699 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000700 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000701
702 // Note that we've seen a #else in this conditional.
703 CondInfo.FoundElse = true;
704
705 // If the conditional is at the top level, and the #if block wasn't
706 // entered, enter the #else block now.
707 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
708 CondInfo.FoundNonSkip = true;
709 break;
710 }
711 } else if (Directive == "elif") {
712 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
713
714 bool ShouldEnter;
715 // If this is in a skipping block or if we're already handled this #if
716 // block, don't bother parsing the condition.
717 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +0000718 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000719 ShouldEnter = false;
720 } else {
721 // Evaluate the #elif condition!
722 const char *Start = CurLexer->BufferPtr;
723
724 // Restore the value of SkippingContents so that identifiers are
725 // looked up, etc, inside the #elif expression.
726 assert(SkippingContents && "We have to be skipping here!");
727 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +0000728 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 SkippingContents = true;
730 }
731
732 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000733 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000734
735 // If this condition is true, enter it!
736 if (ShouldEnter) {
737 CondInfo.FoundNonSkip = true;
738 break;
739 }
740 }
741 }
742
743 CurLexer->ParsingPreprocessorDirective = false;
744 }
745
746 // Finally, if we are out of the conditional (saw an #endif or ran off the end
747 // of the file, just stop skipping and return to lexing whatever came after
748 // the #if block.
749 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000750}
751
752//===----------------------------------------------------------------------===//
753// Preprocessor Directive Handling.
754//===----------------------------------------------------------------------===//
755
756/// HandleDirective - This callback is invoked when the lexer sees a # token
757/// at the start of a line. This consumes the directive, modifies the
758/// lexer/preprocessor state, and advances the lexer(s) so that the next token
759/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +0000760void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000761 // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
762
763 // We just parsed a # character at the start of a line, so we're in directive
764 // mode. Tell the lexer this so any newlines we see will be converted into an
765 // EOM token (this terminates the macro).
766 CurLexer->ParsingPreprocessorDirective = true;
767
768 ++NumDirectives;
769
770 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000771 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000772
773 switch (Result.getKind()) {
774 default: break;
775 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +0000776 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000777
778#if 0
779 case tok::numeric_constant:
780 // FIXME: implement # 7 line numbers!
781 break;
782#endif
783 case tok::kw_else:
784 return HandleElseDirective(Result);
785 case tok::kw_if:
786 return HandleIfDirective(Result);
787 case tok::identifier:
788 // Strip out trigraphs and embedded newlines.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000789 std::string Directive = getSpelling(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000790 bool isExtension = false;
791 switch (Directive.size()) {
792 case 4:
793 if (Directive == "line")
794 ;
795 if (Directive == "elif")
796 return HandleElifDirective(Result);
797 if (Directive == "sccs") {
798 isExtension = true;
799 // SCCS is the same as #ident.
800 }
801 break;
802 case 5:
803 if (Directive == "endif")
804 return HandleEndifDirective(Result);
805 if (Directive == "ifdef")
806 return HandleIfdefDirective(Result, false);
807 if (Directive == "undef")
808 return HandleUndefDirective(Result);
809 if (Directive == "error")
810 return HandleUserDiagnosticDirective(Result, false);
811 if (Directive == "ident")
812 isExtension = true;
813 break;
814 case 6:
815 if (Directive == "define")
816 return HandleDefineDirective(Result);
817 if (Directive == "ifndef")
818 return HandleIfdefDirective(Result, true);
819 if (Directive == "import")
820 return HandleImportDirective(Result);
821 if (Directive == "pragma") {
822 // FIXME: implement #pragma
823 ++NumPragma;
824#if 1
825 // Read the rest of the PP line.
826 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000827 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000828 } while (Result.getKind() != tok::eom);
829
Chris Lattnercb283342006-06-18 06:48:37 +0000830 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000831#endif
832 } else if (Directive == "assert") {
833 isExtension = true;
834 }
835 break;
836 case 7:
837 if (Directive == "include") // Handle #include.
838 return HandleIncludeDirective(Result);
Chris Lattnercb283342006-06-18 06:48:37 +0000839 if (Directive == "warning") {
840 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +0000841 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +0000842 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000843 break;
844 case 8:
845 if (Directive == "unassert") {
846 isExtension = true;
847 }
848 break;
849 case 12:
850 if (Directive == "include_next") // Handle #include_next.
851 return HandleIncludeNextDirective(Result);
852 break;
853 }
854 break;
855 }
856
857 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +0000858 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000859
860 // Read the rest of the PP line.
861 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000862 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000863 } while (Result.getKind() != tok::eom);
864
865 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000866}
867
Chris Lattnercb283342006-06-18 06:48:37 +0000868void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +0000869 bool isWarning) {
870 // Read the rest of the line raw. We do this because we don't want macros
871 // to be expanded and we don't require that the tokens be valid preprocessing
872 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
873 // collapse multiple consequtive white space between tokens, but this isn't
874 // specified by the standard.
875 std::string Message = CurLexer->ReadToEndOfLine();
876
877 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
878 return Diag(Result, DiagID, Message);
879}
880
881/// HandleIncludeDirective - The "#include" tokens have just been read, read the
882/// file to be included from the lexer, then include it! This is a common
883/// routine with functionality shared between #include, #include_next and
884/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +0000885void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +0000886 const DirectoryLookup *LookupFrom,
887 bool isImport) {
888 ++NumIncluded;
889 LexerToken FilenameTok;
Chris Lattnercb283342006-06-18 06:48:37 +0000890 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000891
892 // If the token kind is EOM, the error has already been diagnosed.
893 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +0000894 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000895
896 // Check that we don't have infinite #include recursion.
897 if (IncludeStack.size() == MaxAllowedIncludeStackDepth-1)
898 return Diag(FilenameTok, diag::err_pp_include_too_deep);
899
900 // Get the text form of the filename.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000901 std::string Filename = getSpelling(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000902 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
903
904 // Make sure the filename is <x> or "x".
905 bool isAngled;
906 if (Filename[0] == '<') {
907 isAngled = true;
908 if (Filename[Filename.size()-1] != '>')
909 return Diag(FilenameTok, diag::err_pp_expects_filename);
910 } else if (Filename[0] == '"') {
911 isAngled = false;
912 if (Filename[Filename.size()-1] != '"')
913 return Diag(FilenameTok, diag::err_pp_expects_filename);
914 } else {
915 return Diag(FilenameTok, diag::err_pp_expects_filename);
916 }
917
918 // Remove the quotes.
919 Filename = std::string(Filename.begin()+1, Filename.end()-1);
920
921 // Diagnose #include "" as invalid.
922 if (Filename.empty())
923 return Diag(FilenameTok, diag::err_pp_empty_filename);
924
925 // Search include directories.
926 const DirectoryLookup *NextDir;
927 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, NextDir);
928 if (File == 0)
929 return Diag(FilenameTok, diag::err_pp_file_not_found);
930
931 // Get information about this file.
932 PerFileInfo &FileInfo = getFileInfo(File);
933
934 // If this is a #import directive, check that we have not already imported
935 // this header.
936 if (isImport) {
937 // If this has already been imported, don't import it again.
938 FileInfo.isImport = true;
939
940 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +0000941 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000942 } else {
943 // Otherwise, if this is a #include of a file that was previously #import'd
944 // or if this is the second #include of a #pragma once file, ignore it.
945 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +0000946 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000947 }
948
949 // Look up the file, create a File ID for it.
950 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +0000951 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +0000952 if (FileID == 0)
953 return Diag(FilenameTok, diag::err_pp_file_not_found);
954
955 // Finally, if all is good, enter the new file!
956 EnterSourceFile(FileID, NextDir);
957
958 // Increment the number of times this file has been included.
959 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +0000960}
961
962/// HandleIncludeNextDirective - Implements #include_next.
963///
Chris Lattnercb283342006-06-18 06:48:37 +0000964void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
965 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000966
967 // #include_next is like #include, except that we start searching after
968 // the current found directory. If we can't do this, issue a
969 // diagnostic.
970 const DirectoryLookup *Lookup = CurNextDirLookup;
971 if (IncludeStack.empty()) {
972 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +0000973 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +0000974 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000975 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattner22eb9722006-06-18 05:43:12 +0000976 }
977
978 return HandleIncludeDirective(IncludeNextTok, Lookup);
979}
980
981/// HandleImportDirective - Implements #import.
982///
Chris Lattnercb283342006-06-18 06:48:37 +0000983void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
984 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000985
986 return HandleIncludeDirective(ImportTok, 0, true);
987}
988
989/// HandleDefineDirective - Implements #define. This consumes the entire macro
990/// line then lets the caller lex the next real token.
991///
Chris Lattnercb283342006-06-18 06:48:37 +0000992void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000993 ++NumDefined;
994 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +0000995 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000996
997 // Error reading macro name? If so, diagnostic already issued.
998 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +0000999 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001000
Chris Lattner50b497e2006-06-18 16:32:35 +00001001 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001002
1003 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001004 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001005
1006 if (Tok.getKind() == tok::eom) {
1007 // If there is no body to this macro, we have no special handling here.
1008 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1009 // This is a function-like macro definition.
1010 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001011 return DiscardUntilEndOfDirective();
1012
1013 } else if (!Tok.hasLeadingSpace()) {
1014 // C99 requires whitespace between the macro definition and the body. Emit
1015 // a diagnostic for something like "#define X+".
1016 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001017 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001018 } else {
1019 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1020 // one in some cases!
1021 }
1022 } else {
1023 // This is a normal token with leading space. Clear the leading space
1024 // marker on the first token to get proper expansion.
1025 Tok.ClearFlag(LexerToken::LeadingSpace);
1026 }
1027
1028 // Read the rest of the macro body.
1029 while (Tok.getKind() != tok::eom) {
1030 MI->AddTokenToBody(Tok);
1031
1032 // FIXME: See create_iso_definition.
1033
1034 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001035 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001036 }
1037
1038 // Finally, if this identifier already had a macro defined for it, verify that
1039 // the macro bodies are identical and free the old definition.
1040 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
1041 // FIXME: Verify the definition is the same.
1042 // Macros must be identical. This means all tokes and whitespace separation
1043 // must be the same.
1044 delete OtherMI;
1045 }
1046
1047 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001048}
1049
1050
1051/// HandleUndefDirective - Implements #undef.
1052///
Chris Lattnercb283342006-06-18 06:48:37 +00001053void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001054 ++NumUndefined;
1055 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001056 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001057
1058 // Error reading macro name? If so, diagnostic already issued.
1059 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001060 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001061
1062 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001063 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001064
1065 // Okay, we finally have a valid identifier to undef.
1066 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1067
1068 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001069 if (MI == 0) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001070
1071#if 0 // FIXME: implement warn_unused_macros.
1072 if (CPP_OPTION (pfile, warn_unused_macros))
1073 _cpp_warn_if_unused_macro (pfile, node, NULL);
1074#endif
1075
1076 // Free macro definition.
1077 delete MI;
1078 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001079}
1080
1081
1082/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1083/// true when this is a #ifndef directive.
1084///
Chris Lattnercb283342006-06-18 06:48:37 +00001085void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 ++NumIf;
1087 LexerToken DirectiveTok = Result;
1088
1089 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001090 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001091
1092 // Error reading macro name? If so, diagnostic already issued.
1093 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001094 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001095
1096 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001097 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001098
1099 // Should we include the stuff contained by this directive?
1100 if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
1101 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001102 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001103 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 } else {
1105 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001106 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001107 /*Foundnonskip*/false,
1108 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 }
1110}
1111
1112/// HandleIfDirective - Implements the #if directive.
1113///
Chris Lattnercb283342006-06-18 06:48:37 +00001114void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001115 ++NumIf;
1116 const char *Start = CurLexer->BufferPtr;
1117
Chris Lattner7966aaf2006-06-18 06:50:36 +00001118 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001119
1120 // Should we include the stuff contained by this directive?
1121 if (ConditionalTrue) {
1122 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001123 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001124 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001125 } else {
1126 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001127 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001128 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001129 }
1130}
1131
1132/// HandleEndifDirective - Implements the #endif directive.
1133///
Chris Lattnercb283342006-06-18 06:48:37 +00001134void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 ++NumEndif;
1136 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001137 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001138
1139 PPConditionalInfo CondInfo;
1140 if (CurLexer->popConditionalLevel(CondInfo)) {
1141 // No conditionals on the stack: this is an #endif without an #if.
1142 return Diag(EndifToken, diag::err_pp_endif_without_if);
1143 }
1144
1145 assert(!CondInfo.WasSkipping && !isSkipping() &&
1146 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001147}
1148
1149
Chris Lattnercb283342006-06-18 06:48:37 +00001150void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001151 ++NumElse;
1152 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001153 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001154
1155 PPConditionalInfo CI;
1156 if (CurLexer->popConditionalLevel(CI))
1157 return Diag(Result, diag::pp_err_else_without_if);
1158
1159 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001160 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001161
1162 // Finally, skip the rest of the contents of this block and return the first
1163 // token after it.
1164 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1165 /*FoundElse*/true);
1166}
1167
Chris Lattnercb283342006-06-18 06:48:37 +00001168void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001169 ++NumElse;
1170 // #elif directive in a non-skipping conditional... start skipping.
1171 // We don't care what the condition is, because we will always skip it (since
1172 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001173 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001174
1175 PPConditionalInfo CI;
1176 if (CurLexer->popConditionalLevel(CI))
1177 return Diag(ElifToken, diag::pp_err_elif_without_if);
1178
1179 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001180 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001181
1182 // Finally, skip the rest of the contents of this block and return the first
1183 // token after it.
1184 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1185 /*FoundElse*/CI.FoundElse);
1186}