blob: e08601e70cdf0114d88db5e628c966ce6e79aa5f [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//
Chris Lattner236ed522006-06-26 01:36:29 +000034// Predefined Macros: _Pragma, __TIMESTAMP__, __TIME__, ...
35//
Chris Lattner22eb9722006-06-18 05:43:12 +000036//===----------------------------------------------------------------------===//
37
38#include "clang/Lex/Preprocessor.h"
39#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000040#include "clang/Lex/Pragma.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000041#include "clang/Basic/Diagnostic.h"
42#include "clang/Basic/FileManager.h"
43#include "clang/Basic/SourceManager.h"
44#include <iostream>
45using namespace llvm;
46using namespace clang;
47
48//===----------------------------------------------------------------------===//
49
50Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
51 FileManager &FM, SourceManager &SM)
52 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
53 SystemDirIdx(0), NoCurDirSearch(false),
Chris Lattnerc8997182006-06-22 05:52:16 +000054 CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
Chris Lattner22eb9722006-06-18 05:43:12 +000055 // Clear stats.
56 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
57 NumIf = NumElse = NumEndif = 0;
58 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
59 MaxIncludeStackDepth = MaxMacroStackDepth = 0;
60 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000061
Chris Lattner22eb9722006-06-18 05:43:12 +000062 // Macro expansion is enabled.
63 DisableMacroExpansion = false;
64 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000065
66 // There is no file-change handler yet.
67 FileChangeHandler = 0;
Chris Lattnerb8761832006-06-24 21:31:03 +000068
69 // Initialize the pragma handlers.
70 PragmaHandlers = new PragmaNamespace(0);
71 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000072
73 // Initialize builtin macros like __LINE__ and friends.
74 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000075}
76
77Preprocessor::~Preprocessor() {
78 // Free any active lexers.
79 delete CurLexer;
80
81 while (!IncludeStack.empty()) {
82 delete IncludeStack.back().TheLexer;
83 IncludeStack.pop_back();
84 }
Chris Lattnerb8761832006-06-24 21:31:03 +000085
86 // Release pragma information.
87 delete PragmaHandlers;
Chris Lattner22eb9722006-06-18 05:43:12 +000088}
89
90/// getFileInfo - Return the PerFileInfo structure for the specified
91/// FileEntry.
92Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
93 if (FE->getUID() >= FileInfo.size())
94 FileInfo.resize(FE->getUID()+1);
95 return FileInfo[FE->getUID()];
96}
97
98
99/// AddKeywords - Add all keywords to the symbol table.
100///
101void Preprocessor::AddKeywords() {
102 enum {
103 C90Shift = 0,
104 EXTC90 = 1 << C90Shift,
105 NOTC90 = 2 << C90Shift,
106 C99Shift = 2,
107 EXTC99 = 1 << C99Shift,
108 NOTC99 = 2 << C99Shift,
109 CPPShift = 4,
110 EXTCPP = 1 << CPPShift,
111 NOTCPP = 2 << CPPShift,
112 Mask = 3
113 };
114
115 // Add keywords and tokens for the current language.
116#define KEYWORD(NAME, FLAGS) \
117 AddKeyword(#NAME+1, tok::kw##NAME, \
118 (FLAGS >> C90Shift) & Mask, \
119 (FLAGS >> C99Shift) & Mask, \
120 (FLAGS >> CPPShift) & Mask);
121#define ALIAS(NAME, TOK) \
122 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
123#include "clang/Basic/TokenKinds.def"
124}
125
126/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
127/// the specified LexerToken's location, translating the token's start
128/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000129void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000130 const std::string &Msg) {
131 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
132 // warnings or extensions.
133 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000134 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000135
Chris Lattnercb283342006-06-18 06:48:37 +0000136 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000137}
Chris Lattnercb283342006-06-18 06:48:37 +0000138void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000139 const std::string &Msg) {
140 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
141 // warnings or extensions.
142 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000143 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000144
Chris Lattner50b497e2006-06-18 16:32:35 +0000145 Diag(Tok.getLocation(), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000146}
147
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148
149void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
150 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
151 << getSpelling(Tok) << "'";
152
153 if (!DumpFlags) return;
154 std::cerr << "\t";
155 if (Tok.isAtStartOfLine())
156 std::cerr << " [StartOfLine]";
157 if (Tok.hasLeadingSpace())
158 std::cerr << " [LeadingSpace]";
159 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000160 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000161 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
162 << "']";
163 }
164}
165
166void Preprocessor::DumpMacro(const MacroInfo &MI) const {
167 std::cerr << "MACRO: ";
168 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
169 DumpToken(MI.getReplacementToken(i));
170 std::cerr << " ";
171 }
172 std::cerr << "\n";
173}
174
Chris Lattner22eb9722006-06-18 05:43:12 +0000175void Preprocessor::PrintStats() {
176 std::cerr << "\n*** Preprocessor Stats:\n";
177 std::cerr << FileInfo.size() << " files tracked.\n";
178 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
179 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
180 NumOnceOnlyFiles += FileInfo[i].isImport;
181 if (MaxNumIncludes < FileInfo[i].NumIncludes)
182 MaxNumIncludes = FileInfo[i].NumIncludes;
183 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
184 }
185 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
186 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
187 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
188
189 std::cerr << NumDirectives << " directives found:\n";
190 std::cerr << " " << NumDefined << " #define.\n";
191 std::cerr << " " << NumUndefined << " #undef.\n";
192 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
193 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
194 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
195 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
196 std::cerr << " " << NumElse << " #else/#elif.\n";
197 std::cerr << " " << NumEndif << " #endif.\n";
198 std::cerr << " " << NumPragma << " #pragma.\n";
199 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
200
201 std::cerr << NumMacroExpanded << " macros expanded, "
202 << NumFastMacroExpanded << " on the fast path.\n";
203 if (MaxMacroStackDepth > 1)
204 std::cerr << " " << MaxMacroStackDepth << " max macroexpand stack depth\n";
205}
206
207//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000208// Token Spelling
209//===----------------------------------------------------------------------===//
210
211
212/// getSpelling() - Return the 'spelling' of this token. The spelling of a
213/// token are the characters used to represent the token in the source file
214/// after trigraph expansion and escaped-newline folding. In particular, this
215/// wants to get the true, uncanonicalized, spelling of things like digraphs
216/// UCNs, etc.
217std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
218 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
219
220 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000221 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000222 assert(TokStart && "Token has invalid location!");
223 if (!Tok.needsCleaning())
224 return std::string(TokStart, TokStart+Tok.getLength());
225
226 // Otherwise, hard case, relex the characters into the string.
227 std::string Result;
228 Result.reserve(Tok.getLength());
229
230 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
231 Ptr != End; ) {
232 unsigned CharSize;
233 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
234 Ptr += CharSize;
235 }
236 assert(Result.size() != unsigned(Tok.getLength()) &&
237 "NeedsCleaning flag set on something that didn't need cleaning!");
238 return Result;
239}
240
241/// getSpelling - This method is used to get the spelling of a token into a
242/// preallocated buffer, instead of as an std::string. The caller is required
243/// to allocate enough space for the token, which is guaranteed to be at least
244/// Tok.getLength() bytes long. The actual length of the token is returned.
245unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
246 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
247
Chris Lattner50b497e2006-06-18 16:32:35 +0000248 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000249 assert(TokStart && "Token has invalid location!");
250
251 // If this token contains nothing interesting, return it directly.
252 if (!Tok.needsCleaning()) {
253 unsigned Size = Tok.getLength();
254 memcpy(Buffer, TokStart, Size);
255 return Size;
256 }
257 // Otherwise, hard case, relex the characters into the string.
258 std::string Result;
259 Result.reserve(Tok.getLength());
260
261 char *OutBuf = Buffer;
262 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
263 Ptr != End; ) {
264 unsigned CharSize;
265 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
266 Ptr += CharSize;
267 }
268 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
269 "NeedsCleaning flag set on something that didn't need cleaning!");
270
271 return OutBuf-Buffer;
272}
273
274//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000275// Source File Location Methods.
276//===----------------------------------------------------------------------===//
277
278
279/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
280/// return null on failure. isAngled indicates whether the file reference is
281/// for system #include's or not (i.e. using <> instead of "").
282const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000283 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000284 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000285 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000286 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000287 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000288
289 // If 'Filename' is absolute, check to see if it exists and no searching.
290 // FIXME: this should be a sys::Path interface, this doesn't handle things
291 // like C:\foo.txt right, nor win32 \\network\device\blah.
292 if (Filename[0] == '/') {
293 // If this was an #include_next "/absolute/file", fail.
294 if (FromDir) return 0;
295
296 // Otherwise, just return the file.
297 return FileMgr.getFile(Filename);
298 }
299
300 // Step #0, unless disabled, check to see if the file is in the #includer's
301 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000302 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000303 const FileEntry *CurFE =
304 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
305 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000306 // Concatenate the requested file onto the directory.
307 // FIXME: should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000308 if (const FileEntry *FE =
309 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000310 if (CurDirLookup)
311 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000312 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000313 CurDir = 0;
314
315 // This file is a system header or C++ unfriendly if the old file is.
316 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000317 return FE;
318 }
319 }
320 }
321
322 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000323 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000324
325 // If this is a #include_next request, start searching after the directory the
326 // file was found in.
327 if (FromDir)
328 i = FromDir-&SearchDirs[0];
329
330 // Check each directory in sequence to see if it contains this file.
331 for (; i != SearchDirs.size(); ++i) {
332 // Concatenate the requested file onto the directory.
333 // FIXME: should be in sys::Path.
334 if (const FileEntry *FE =
335 FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000336 CurDir = &SearchDirs[i];
337
338 // This file is a system header or C++ unfriendly if the dir is.
339 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000340 return FE;
341 }
342 }
343
344 // Otherwise, didn't find it.
345 return 0;
346}
347
348/// EnterSourceFile - Add a source file to the top of the include stack and
349/// start lexing tokens from it instead of the current buffer. Return true
350/// on failure.
351void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerc8997182006-06-22 05:52:16 +0000352 const DirectoryLookup *CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000353 ++NumEnteredSourceFiles;
354
355 // Add the current lexer to the include stack.
356 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000357 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
Chris Lattner22eb9722006-06-18 05:43:12 +0000358 } else {
359 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
360 }
361
362 if (MaxIncludeStackDepth < IncludeStack.size())
363 MaxIncludeStackDepth = IncludeStack.size();
364
365 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
366
Chris Lattnerc8997182006-06-22 05:52:16 +0000367 CurLexer = new Lexer(Buffer, FileID, *this);
368 CurDirLookup = CurDir;
Chris Lattner0c885f52006-06-21 06:50:18 +0000369
370 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000371 if (FileChangeHandler) {
372 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
373
374 // Get the file entry for the current file.
375 if (const FileEntry *FE =
376 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
377 FileType = getFileInfo(FE).DirInfo;
378
Chris Lattner55a60952006-06-25 04:20:34 +0000379 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart),
380 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000381 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000382}
383
384/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000385/// tokens from it instead of the current buffer.
386void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000387 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
388 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000389 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000390 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
391 CurLexer = 0;
392 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000393 } else if (CurMacroExpander) {
394 MacroStack.push_back(CurMacroExpander);
395 }
396
397 if (MaxMacroStackDepth < MacroStack.size())
398 MaxMacroStackDepth = MacroStack.size();
399
400 // TODO: Figure out arguments.
401
402 // Mark the macro as currently disabled, so that it is not recursively
403 // expanded.
404 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000405 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000406}
407
Chris Lattner22eb9722006-06-18 05:43:12 +0000408//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000409// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000410//===----------------------------------------------------------------------===//
411
Chris Lattner677757a2006-06-28 05:26:32 +0000412/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
413/// identifier table.
414void Preprocessor::RegisterBuiltinMacros() {
415 // Do this for each thing.
416 MacroInfo *MI = new MacroInfo(SourceLocation());
417 MI->setIsBuiltinMacro();
418 getIdentifierInfo("__LINE__")->setMacroInfo(MI);
Chris Lattner17862172006-06-24 22:12:56 +0000419
Chris Lattner677757a2006-06-28 05:26:32 +0000420 // FIXME: Warn on #undef / #define of a builtin macro.
421 // FIXME: make HandleMacroExpandedIdentifier handle this case.
422 // FIXME: implement them all, including _Pragma.
423 //MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +0000424}
425
Chris Lattner677757a2006-06-28 05:26:32 +0000426
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000427/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
428/// expanded as a macro, handle it and return the next token as 'Identifier'.
429void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
430 MacroInfo *MI) {
431 ++NumMacroExpanded;
432 // If we started lexing a macro, enter the macro expansion body.
433 // FIXME: Read/Validate the argument list here!
434
435 // If this macro expands to no tokens, don't bother to push it onto the
436 // expansion stack, only to take it right back off.
437 if (MI->getNumTokens() == 0) {
438 // Ignore this macro use, just return the next token in the current
439 // buffer.
440 bool HadLeadingSpace = Identifier.hasLeadingSpace();
441 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
442
443 Lex(Identifier);
444
445 // If the identifier isn't on some OTHER line, inherit the leading
446 // whitespace/first-on-a-line property of this token. This handles
447 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
448 // empty.
449 if (!Identifier.isAtStartOfLine()) {
450 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
451 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
452 }
453 ++NumFastMacroExpanded;
454 return;
455
456 } else if (MI->getNumTokens() == 1 &&
457 // Don't handle identifiers if they need recursive expansion.
458 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
459 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
460 // FIXME: Function-style macros only if no arguments?
461
462 // Otherwise, if this macro expands into a single trivially-expanded
463 // token: expand it now. This handles common cases like
464 // "#define VAL 42".
465
466 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
467 // identifier to the expanded token.
468 bool isAtStartOfLine = Identifier.isAtStartOfLine();
469 bool hasLeadingSpace = Identifier.hasLeadingSpace();
470
471 // Remember where the token is instantiated.
472 SourceLocation InstantiateLoc = Identifier.getLocation();
473
474 // Replace the result token.
475 Identifier = MI->getReplacementToken(0);
476
477 // Restore the StartOfLine/LeadingSpace markers.
478 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
479 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
480
481 // Update the tokens location to include both its logical and physical
482 // locations.
483 SourceLocation Loc =
484 MacroExpander::getInstantiationLoc(*this, Identifier.getLocation(),
485 InstantiateLoc);
486 Identifier.SetLocation(Loc);
487
488 // Since this is not an identifier token, it can't be macro expanded, so
489 // we're done.
490 ++NumFastMacroExpanded;
491 return;
492 }
493
494 // Start expanding the macro (FIXME, pass arguments).
495 EnterMacro(Identifier);
496
497 // Now that the macro is at the top of the include stack, ask the
498 // preprocessor to read the next token from it.
499 return Lex(Identifier);
500}
501
Chris Lattner677757a2006-06-28 05:26:32 +0000502
503//===----------------------------------------------------------------------===//
504// Lexer Event Handling.
505//===----------------------------------------------------------------------===//
506
507/// HandleIdentifier - This callback is invoked when the lexer reads an
508/// identifier. This callback looks up the identifier in the map and/or
509/// potentially macro expands it or turns it into a named token (like 'for').
510void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
511 if (Identifier.getIdentifierInfo() == 0) {
512 // If we are skipping tokens (because we are in a #if 0 block), there will
513 // be no identifier info, just return the token.
514 assert(isSkipping() && "Token isn't an identifier?");
515 return;
516 }
517 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
518
519 // If this identifier was poisoned, and if it was not produced from a macro
520 // expansion, emit an error.
521 if (ITI.isPoisoned() && CurLexer)
522 Diag(Identifier, diag::err_pp_used_poisoned_id);
523
524 if (MacroInfo *MI = ITI.getMacroInfo())
525 if (MI->isEnabled() && !DisableMacroExpansion)
526 return HandleMacroExpandedIdentifier(Identifier, MI);
527
528 // Change the kind of this identifier to the appropriate token kind, e.g.
529 // turning "for" into a keyword.
530 Identifier.SetKind(ITI.getTokenID());
531
532 // If this is an extension token, diagnose its use.
533 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
534}
535
Chris Lattner22eb9722006-06-18 05:43:12 +0000536/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
537/// the current file. This either returns the EOF token or pops a level off
538/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000539void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000540 assert(!CurMacroExpander &&
541 "Ending a file when currently in a macro!");
542
543 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
544 // this is an error condition. Just return the EOF token up to
545 // SkipExcludedConditionalBlock. The Lexer will have already have issued
546 // errors for the unterminated #if's on the conditional stack.
547 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000548 Result.StartToken();
549 CurLexer->BufferPtr = CurLexer->BufferEnd;
550 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000551 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000552 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000553 }
554
555 // If this is a #include'd file, pop it off the include stack and continue
556 // lexing the #includer file.
557 if (!IncludeStack.empty()) {
558 // We're done with the #included file.
559 delete CurLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000560 CurLexer = IncludeStack.back().TheLexer;
561 CurDirLookup = IncludeStack.back().TheDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000562 IncludeStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000563
564 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000565 if (FileChangeHandler && !isEndOfMacro) {
566 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
567
568 // Get the file entry for the current file.
569 if (const FileEntry *FE =
570 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
571 FileType = getFileInfo(FE).DirInfo;
572
Chris Lattner0c885f52006-06-21 06:50:18 +0000573 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000574 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000575 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000576
Chris Lattner22eb9722006-06-18 05:43:12 +0000577 return Lex(Result);
578 }
579
Chris Lattnerd01e2912006-06-18 16:22:51 +0000580 Result.StartToken();
581 CurLexer->BufferPtr = CurLexer->BufferEnd;
582 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000583 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000584
585 // We're done with the #included file.
586 delete CurLexer;
587 CurLexer = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000588}
589
590/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000591/// the current macro line.
592void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000593 assert(CurMacroExpander && !CurLexer &&
594 "Ending a macro when currently in a #include file!");
595
596 // Mark macro not ignored now that it is no longer being expanded.
597 CurMacroExpander->getMacro().EnableMacro();
598 delete CurMacroExpander;
599
600 if (!MacroStack.empty()) {
601 // In a nested macro invocation, continue lexing from the macro.
602 CurMacroExpander = MacroStack.back();
603 MacroStack.pop_back();
604 return Lex(Result);
605 } else {
606 CurMacroExpander = 0;
607 // Handle this like a #include file being popped off the stack.
Chris Lattner0c885f52006-06-21 06:50:18 +0000608 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000609 }
610}
611
612
613//===----------------------------------------------------------------------===//
614// Utility Methods for Preprocessor Directive Handling.
615//===----------------------------------------------------------------------===//
616
617/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
618/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000619void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000620 LexerToken Tmp;
621 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000622 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000623 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000624}
625
626/// ReadMacroName - Lex and validate a macro name, which occurs after a
627/// #define or #undef. This sets the token kind to eom and discards the rest
628/// of the macro line if the macro name is invalid.
Chris Lattnercb283342006-06-18 06:48:37 +0000629void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000630 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000631 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000632
633 // Missing macro name?
634 if (MacroNameTok.getKind() == tok::eom)
635 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
636
637 if (MacroNameTok.getIdentifierInfo() == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000638 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000639 // Fall through on error.
640 } else if (0) {
641 // FIXME: Error if defining a C++ named operator.
642
643 } else if (0) {
644 // FIXME: Error if defining "defined", "__DATE__", and other predef macros
645 // in C99 6.10.8.4.
646 } else {
647 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000648 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000649 }
650
651
652 // Invalid macro name, read and discard the rest of the line. Then set the
653 // token kind to tok::eom.
654 MacroNameTok.SetKind(tok::eom);
655 return DiscardUntilEndOfDirective();
656}
657
658/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
659/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000660void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000661 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000662 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000663 // There should be no tokens after the directive, but we allow them as an
664 // extension.
665 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000666 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
667 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000668 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000669}
670
671
672
673/// SkipExcludedConditionalBlock - We just read a #if or related directive and
674/// decided that the subsequent tokens are in the #if'd out portion of the
675/// file. Lex the rest of the file, until we see an #endif. If
676/// FoundNonSkipPortion is true, then we have already emitted code for part of
677/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
678/// is true, then #else directives are ok, if not, then we have already seen one
679/// so a #else directive is a duplicate. When this returns, the caller can lex
680/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000681void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000682 bool FoundNonSkipPortion,
683 bool FoundElse) {
684 ++NumSkipped;
685 assert(MacroStack.empty() && CurMacroExpander == 0 && CurLexer &&
686 "Lexing a macro, not a file?");
687
688 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
689 FoundNonSkipPortion, FoundElse);
690
691 // Know that we are going to be skipping tokens. Set this flag to indicate
692 // this, which has a couple of effects:
693 // 1. If EOF of the current lexer is found, the include stack isn't popped.
694 // 2. Identifier information is not looked up for identifier tokens. As an
695 // effect of this, implicit macro expansion is naturally disabled.
696 // 3. "#" tokens at the start of a line are treated as normal tokens, not
697 // implicitly transformed by the lexer.
698 // 4. All notes, warnings, and extension messages are disabled.
699 //
700 SkippingContents = true;
701 LexerToken Tok;
702 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000703 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000704
705 // If this is the end of the buffer, we have an error. The lexer will have
706 // already handled this error condition, so just return and let the caller
707 // lex after this #include.
708 if (Tok.getKind() == tok::eof) break;
709
710 // If this token is not a preprocessor directive, just skip it.
711 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
712 continue;
713
714 // We just parsed a # character at the start of a line, so we're in
715 // directive mode. Tell the lexer this so any newlines we see will be
716 // converted into an EOM token (this terminates the macro).
717 CurLexer->ParsingPreprocessorDirective = true;
718
719 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000720 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000721
722 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
723 // something bogus), skip it.
724 if (Tok.getKind() != tok::identifier) {
725 CurLexer->ParsingPreprocessorDirective = false;
726 continue;
727 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000728
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 // If the first letter isn't i or e, it isn't intesting to us. We know that
730 // this is safe in the face of spelling differences, because there is no way
731 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000732 // allows us to avoid looking up the identifier info for #define/#undef and
733 // other common directives.
734 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
735 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000736 if (FirstChar >= 'a' && FirstChar <= 'z' &&
737 FirstChar != 'i' && FirstChar != 'e') {
738 CurLexer->ParsingPreprocessorDirective = false;
739 continue;
740 }
741
Chris Lattnere60165f2006-06-22 06:36:29 +0000742 // Get the identifier name without trigraphs or embedded newlines. Note
743 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
744 // when skipping.
745 // TODO: could do this with zero copies in the no-clean case by using
746 // strncmp below.
747 char Directive[20];
748 unsigned IdLen;
749 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
750 IdLen = Tok.getLength();
751 memcpy(Directive, RawCharData, IdLen);
752 Directive[IdLen] = 0;
753 } else {
754 std::string DirectiveStr = getSpelling(Tok);
755 IdLen = DirectiveStr.size();
756 if (IdLen >= 20) {
757 CurLexer->ParsingPreprocessorDirective = false;
758 continue;
759 }
760 memcpy(Directive, &DirectiveStr[0], IdLen);
761 Directive[IdLen] = 0;
762 }
763
Chris Lattner22eb9722006-06-18 05:43:12 +0000764 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000765 if ((IdLen == 2) || // "if"
766 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
767 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000768 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
769 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000770 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000771 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000772 /*foundnonskip*/false,
773 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000774 }
775 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000776 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000777 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000778 PPConditionalInfo CondInfo;
779 CondInfo.WasSkipping = true; // Silence bogus warning.
780 bool InCond = CurLexer->popConditionalLevel(CondInfo);
781 assert(!InCond && "Can't be skipping if not in a conditional!");
782
783 // If we popped the outermost skipping block, we're done skipping!
784 if (!CondInfo.WasSkipping)
785 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000786 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000787 // #else directive in a skipping conditional. If not in some other
788 // skipping conditional, and if #else hasn't already been seen, enter it
789 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000790 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000791 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
792
793 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000794 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000795
796 // Note that we've seen a #else in this conditional.
797 CondInfo.FoundElse = true;
798
799 // If the conditional is at the top level, and the #if block wasn't
800 // entered, enter the #else block now.
801 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
802 CondInfo.FoundNonSkip = true;
803 break;
804 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000805 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +0000806 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
807
808 bool ShouldEnter;
809 // If this is in a skipping block or if we're already handled this #if
810 // block, don't bother parsing the condition.
811 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +0000812 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 ShouldEnter = false;
814 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 // Restore the value of SkippingContents so that identifiers are
816 // looked up, etc, inside the #elif expression.
817 assert(SkippingContents && "We have to be skipping here!");
818 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +0000819 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 SkippingContents = true;
821 }
822
823 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000824 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000825
826 // If this condition is true, enter it!
827 if (ShouldEnter) {
828 CondInfo.FoundNonSkip = true;
829 break;
830 }
831 }
832 }
833
834 CurLexer->ParsingPreprocessorDirective = false;
835 }
836
837 // Finally, if we are out of the conditional (saw an #endif or ran off the end
838 // of the file, just stop skipping and return to lexing whatever came after
839 // the #if block.
840 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000841}
842
843//===----------------------------------------------------------------------===//
844// Preprocessor Directive Handling.
845//===----------------------------------------------------------------------===//
846
847/// HandleDirective - This callback is invoked when the lexer sees a # token
848/// at the start of a line. This consumes the directive, modifies the
849/// lexer/preprocessor state, and advances the lexer(s) so that the next token
850/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +0000851void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000852 // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
853
854 // We just parsed a # character at the start of a line, so we're in directive
855 // mode. Tell the lexer this so any newlines we see will be converted into an
856 // EOM token (this terminates the macro).
857 CurLexer->ParsingPreprocessorDirective = true;
858
859 ++NumDirectives;
860
861 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000862 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000863
864 switch (Result.getKind()) {
865 default: break;
866 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +0000867 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000868
869#if 0
870 case tok::numeric_constant:
871 // FIXME: implement # 7 line numbers!
872 break;
873#endif
874 case tok::kw_else:
875 return HandleElseDirective(Result);
876 case tok::kw_if:
877 return HandleIfDirective(Result);
878 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +0000879 // Get the identifier name without trigraphs or embedded newlines.
880 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +0000881 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +0000882 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000883 case 4:
Chris Lattner40931922006-06-22 06:14:04 +0000884 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnerb8761832006-06-24 21:31:03 +0000885 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +0000886 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 return HandleElifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000888 if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
Chris Lattnerb8761832006-06-24 21:31:03 +0000889 isExtension = true; // FIXME: implement #sccs
Chris Lattner22eb9722006-06-18 05:43:12 +0000890 // SCCS is the same as #ident.
891 }
892 break;
893 case 5:
Chris Lattner40931922006-06-22 06:14:04 +0000894 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000895 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000896 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000897 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000898 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000899 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000900 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000901 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000902 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattnerb8761832006-06-24 21:31:03 +0000903 isExtension = true; // FIXME: implement #ident
Chris Lattner22eb9722006-06-18 05:43:12 +0000904 break;
905 case 6:
Chris Lattner40931922006-06-22 06:14:04 +0000906 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000907 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000908 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000909 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +0000910 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000911 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +0000912 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
913 return HandlePragmaDirective(Result);
914 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
915 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +0000916 break;
917 case 7:
Chris Lattner40931922006-06-22 06:14:04 +0000918 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
919 return HandleIncludeDirective(Result); // Handle #include.
920 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +0000921 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +0000922 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +0000923 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 break;
925 case 8:
Chris Lattner40931922006-06-22 06:14:04 +0000926 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +0000927 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 }
929 break;
930 case 12:
Chris Lattner40931922006-06-22 06:14:04 +0000931 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
932 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 break;
934 }
935 break;
936 }
937
938 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +0000939 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000940
941 // Read the rest of the PP line.
942 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000943 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000944 } while (Result.getKind() != tok::eom);
945
946 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000947}
948
Chris Lattnercb283342006-06-18 06:48:37 +0000949void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +0000950 bool isWarning) {
951 // Read the rest of the line raw. We do this because we don't want macros
952 // to be expanded and we don't require that the tokens be valid preprocessing
953 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
954 // collapse multiple consequtive white space between tokens, but this isn't
955 // specified by the standard.
956 std::string Message = CurLexer->ReadToEndOfLine();
957
958 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
959 return Diag(Result, DiagID, Message);
960}
961
Chris Lattnerb8761832006-06-24 21:31:03 +0000962//===----------------------------------------------------------------------===//
963// Preprocessor Include Directive Handling.
964//===----------------------------------------------------------------------===//
965
Chris Lattner22eb9722006-06-18 05:43:12 +0000966/// HandleIncludeDirective - The "#include" tokens have just been read, read the
967/// file to be included from the lexer, then include it! This is a common
968/// routine with functionality shared between #include, #include_next and
969/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +0000970void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +0000971 const DirectoryLookup *LookupFrom,
972 bool isImport) {
973 ++NumIncluded;
974 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +0000975 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000976
977 // If the token kind is EOM, the error has already been diagnosed.
978 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +0000979 return;
Chris Lattner269c2322006-06-25 06:23:00 +0000980
981 // Verify that there is nothing after the filename, other than EOM. Use the
982 // preprocessor to lex this in case lexing the filename entered a macro.
983 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +0000984
985 // Check that we don't have infinite #include recursion.
986 if (IncludeStack.size() == MaxAllowedIncludeStackDepth-1)
987 return Diag(FilenameTok, diag::err_pp_include_too_deep);
988
Chris Lattner269c2322006-06-25 06:23:00 +0000989 // Find out whether the filename is <x> or "x".
990 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +0000991
992 // Remove the quotes.
993 Filename = std::string(Filename.begin()+1, Filename.end()-1);
994
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +0000996 const DirectoryLookup *CurDir;
997 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +0000998 if (File == 0)
999 return Diag(FilenameTok, diag::err_pp_file_not_found);
1000
1001 // Get information about this file.
1002 PerFileInfo &FileInfo = getFileInfo(File);
1003
1004 // If this is a #import directive, check that we have not already imported
1005 // this header.
1006 if (isImport) {
1007 // If this has already been imported, don't import it again.
1008 FileInfo.isImport = true;
1009
1010 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001011 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001012 } else {
1013 // Otherwise, if this is a #include of a file that was previously #import'd
1014 // or if this is the second #include of a #pragma once file, ignore it.
1015 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001016 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 }
1018
1019 // Look up the file, create a File ID for it.
1020 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +00001021 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001022 if (FileID == 0)
1023 return Diag(FilenameTok, diag::err_pp_file_not_found);
1024
1025 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001026 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001027
1028 // Increment the number of times this file has been included.
1029 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001030}
1031
1032/// HandleIncludeNextDirective - Implements #include_next.
1033///
Chris Lattnercb283342006-06-18 06:48:37 +00001034void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1035 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001036
1037 // #include_next is like #include, except that we start searching after
1038 // the current found directory. If we can't do this, issue a
1039 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001040 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001041 if (IncludeStack.empty()) {
1042 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001043 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001044 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001045 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001046 } else {
1047 // Start looking up in the next directory.
1048 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001049 }
1050
1051 return HandleIncludeDirective(IncludeNextTok, Lookup);
1052}
1053
1054/// HandleImportDirective - Implements #import.
1055///
Chris Lattnercb283342006-06-18 06:48:37 +00001056void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1057 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001058
1059 return HandleIncludeDirective(ImportTok, 0, true);
1060}
1061
Chris Lattnerb8761832006-06-24 21:31:03 +00001062//===----------------------------------------------------------------------===//
1063// Preprocessor Macro Directive Handling.
1064//===----------------------------------------------------------------------===//
1065
Chris Lattner22eb9722006-06-18 05:43:12 +00001066/// HandleDefineDirective - Implements #define. This consumes the entire macro
1067/// line then lets the caller lex the next real token.
1068///
Chris Lattnercb283342006-06-18 06:48:37 +00001069void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001070 ++NumDefined;
1071 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001072 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001073
1074 // Error reading macro name? If so, diagnostic already issued.
1075 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001076 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001077
Chris Lattner50b497e2006-06-18 16:32:35 +00001078 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001079
1080 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001081 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001082
1083 if (Tok.getKind() == tok::eom) {
1084 // If there is no body to this macro, we have no special handling here.
1085 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1086 // This is a function-like macro definition.
1087 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 return DiscardUntilEndOfDirective();
1089
1090 } else if (!Tok.hasLeadingSpace()) {
1091 // C99 requires whitespace between the macro definition and the body. Emit
1092 // a diagnostic for something like "#define X+".
1093 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001094 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001095 } else {
1096 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1097 // one in some cases!
1098 }
1099 } else {
1100 // This is a normal token with leading space. Clear the leading space
1101 // marker on the first token to get proper expansion.
1102 Tok.ClearFlag(LexerToken::LeadingSpace);
1103 }
1104
1105 // Read the rest of the macro body.
1106 while (Tok.getKind() != tok::eom) {
1107 MI->AddTokenToBody(Tok);
1108
1109 // FIXME: See create_iso_definition.
1110
1111 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001112 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001113 }
1114
1115 // Finally, if this identifier already had a macro defined for it, verify that
1116 // the macro bodies are identical and free the old definition.
1117 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner677757a2006-06-28 05:26:32 +00001118 if (OtherMI->isBuiltinMacro())
1119 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1120
1121
Chris Lattner22eb9722006-06-18 05:43:12 +00001122 // FIXME: Verify the definition is the same.
1123 // Macros must be identical. This means all tokes and whitespace separation
1124 // must be the same.
1125 delete OtherMI;
1126 }
1127
1128 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001129}
1130
1131
1132/// HandleUndefDirective - Implements #undef.
1133///
Chris Lattnercb283342006-06-18 06:48:37 +00001134void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 ++NumUndefined;
1136 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001137 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001138
1139 // Error reading macro name? If so, diagnostic already issued.
1140 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001141 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001142
1143 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001144 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001145
1146 // Okay, we finally have a valid identifier to undef.
1147 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1148
1149 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001150 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001151
1152 if (MI->isBuiltinMacro())
1153 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001154
1155#if 0 // FIXME: implement warn_unused_macros.
1156 if (CPP_OPTION (pfile, warn_unused_macros))
1157 _cpp_warn_if_unused_macro (pfile, node, NULL);
1158#endif
1159
1160 // Free macro definition.
1161 delete MI;
1162 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001163}
1164
1165
Chris Lattnerb8761832006-06-24 21:31:03 +00001166//===----------------------------------------------------------------------===//
1167// Preprocessor Conditional Directive Handling.
1168//===----------------------------------------------------------------------===//
1169
Chris Lattner22eb9722006-06-18 05:43:12 +00001170/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1171/// true when this is a #ifndef directive.
1172///
Chris Lattnercb283342006-06-18 06:48:37 +00001173void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001174 ++NumIf;
1175 LexerToken DirectiveTok = Result;
1176
1177 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001178 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001179
1180 // Error reading macro name? If so, diagnostic already issued.
1181 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001182 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001183
1184 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001185 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001186
1187 // Should we include the stuff contained by this directive?
1188 if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
1189 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001190 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001191 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001192 } else {
1193 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001194 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001195 /*Foundnonskip*/false,
1196 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001197 }
1198}
1199
1200/// HandleIfDirective - Implements the #if directive.
1201///
Chris Lattnercb283342006-06-18 06:48:37 +00001202void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001203 ++NumIf;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001204 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001205
1206 // Should we include the stuff contained by this directive?
1207 if (ConditionalTrue) {
1208 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001209 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001211 } else {
1212 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001213 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001214 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 }
1216}
1217
1218/// HandleEndifDirective - Implements the #endif directive.
1219///
Chris Lattnercb283342006-06-18 06:48:37 +00001220void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001221 ++NumEndif;
1222 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001223 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001224
1225 PPConditionalInfo CondInfo;
1226 if (CurLexer->popConditionalLevel(CondInfo)) {
1227 // No conditionals on the stack: this is an #endif without an #if.
1228 return Diag(EndifToken, diag::err_pp_endif_without_if);
1229 }
1230
1231 assert(!CondInfo.WasSkipping && !isSkipping() &&
1232 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001233}
1234
1235
Chris Lattnercb283342006-06-18 06:48:37 +00001236void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001237 ++NumElse;
1238 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001239 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001240
1241 PPConditionalInfo CI;
1242 if (CurLexer->popConditionalLevel(CI))
1243 return Diag(Result, diag::pp_err_else_without_if);
1244
1245 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001246 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001247
1248 // Finally, skip the rest of the contents of this block and return the first
1249 // token after it.
1250 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1251 /*FoundElse*/true);
1252}
1253
Chris Lattnercb283342006-06-18 06:48:37 +00001254void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001255 ++NumElse;
1256 // #elif directive in a non-skipping conditional... start skipping.
1257 // We don't care what the condition is, because we will always skip it (since
1258 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001259 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001260
1261 PPConditionalInfo CI;
1262 if (CurLexer->popConditionalLevel(CI))
1263 return Diag(ElifToken, diag::pp_err_elif_without_if);
1264
1265 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001266 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001267
1268 // Finally, skip the rest of the contents of this block and return the first
1269 // token after it.
1270 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1271 /*FoundElse*/CI.FoundElse);
1272}
Chris Lattnerb8761832006-06-24 21:31:03 +00001273
1274
1275//===----------------------------------------------------------------------===//
1276// Preprocessor Pragma Directive Handling.
1277//===----------------------------------------------------------------------===//
1278
1279/// HandlePragmaDirective - The "#pragma" directive has been parsed with
1280/// PragmaTok containing the "pragma" identifier. Lex the rest of the pragma,
1281/// passing it to the registered pragma handlers.
1282void Preprocessor::HandlePragmaDirective(LexerToken &PragmaTok) {
1283 ++NumPragma;
1284
1285 // Invoke the first level of pragma handlers which reads the namespace id.
1286 LexerToken Tok;
1287 PragmaHandlers->HandlePragma(*this, Tok);
1288
1289 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattner17862172006-06-24 22:12:56 +00001290 if (CurLexer->ParsingPreprocessorDirective)
1291 DiscardUntilEndOfDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001292}
1293
1294/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
Chris Lattner17862172006-06-24 22:12:56 +00001295///
Chris Lattnerb8761832006-06-24 21:31:03 +00001296void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) {
1297 if (IncludeStack.empty()) {
1298 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
1299 return;
1300 }
Chris Lattner17862172006-06-24 22:12:56 +00001301
1302 // FIXME: implement the _Pragma thing.
1303 assert(CurLexer && "Cannot have a pragma in a macro expansion yet!");
1304
1305 // Mark the file as a once-only file now.
1306 const FileEntry *File =
1307 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
1308 getFileInfo(File).isImport = true;
Chris Lattnerb8761832006-06-24 21:31:03 +00001309}
1310
Chris Lattner17862172006-06-24 22:12:56 +00001311/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
1312///
1313void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) {
1314 LexerToken Tok;
1315 assert(!SkippingContents && "Why are we handling pragmas while skipping?");
1316 while (1) {
1317 // Read the next token to poison. While doing this, pretend that we are
1318 // skipping while reading the identifier to poison.
1319 // This avoids errors on code like:
1320 // #pragma GCC poison X
1321 // #pragma GCC poison X
1322 SkippingContents = true;
1323 LexUnexpandedToken(Tok);
1324 SkippingContents = false;
1325
1326 // If we reached the end of line, we're done.
1327 if (Tok.getKind() == tok::eom) return;
1328
1329 // Can only poison identifiers.
1330 if (Tok.getKind() != tok::identifier) {
1331 Diag(Tok, diag::err_pp_invalid_poison);
1332 return;
1333 }
1334
1335 // Look up the identifier info for the token.
1336 std::string TokStr = getSpelling(Tok);
1337 IdentifierTokenInfo *II =
1338 getIdentifierInfo(&TokStr[0], &TokStr[0]+TokStr.size());
1339
1340 // Already poisoned.
1341 if (II->isPoisoned()) continue;
1342
1343 // If this is a macro identifier, emit a warning.
1344 if (II->getMacroInfo())
1345 Diag(Tok, diag::pp_poisoning_existing_macro);
1346
1347 // Finally, poison it!
1348 II->setIsPoisoned();
1349 }
1350}
Chris Lattnerb8761832006-06-24 21:31:03 +00001351
Chris Lattner269c2322006-06-25 06:23:00 +00001352/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
1353/// that the whole directive has been parsed.
Chris Lattner55a60952006-06-25 04:20:34 +00001354void Preprocessor::HandlePragmaSystemHeader(LexerToken &SysHeaderTok) {
1355 if (IncludeStack.empty()) {
1356 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
1357 return;
1358 }
1359
1360 // Mark the file as a system header.
1361 const FileEntry *File =
1362 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
1363 getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
1364
1365
1366 // Notify the client, if desired, that we are in a new source file.
1367 if (FileChangeHandler)
1368 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1369 SystemHeaderPragma, DirectoryLookup::SystemHeaderDir);
1370}
1371
Chris Lattner269c2322006-06-25 06:23:00 +00001372/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
1373///
1374void Preprocessor::HandlePragmaDependency(LexerToken &DependencyTok) {
1375 LexerToken FilenameTok;
1376 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
1377
1378 // If the token kind is EOM, the error has already been diagnosed.
1379 if (FilenameTok.getKind() == tok::eom)
1380 return;
1381
1382 // Find out whether the filename is <x> or "x".
1383 bool isAngled = Filename[0] == '<';
1384
1385 // Remove the quotes.
1386 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1387
1388 // Search include directories.
1389 const DirectoryLookup *CurDir;
1390 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
1391 if (File == 0)
1392 return Diag(FilenameTok, diag::err_pp_file_not_found);
1393
1394 Lexer *TheLexer = CurLexer;
1395 if (TheLexer == 0) {
1396 assert(!IncludeStack.empty() && "No current lexer?");
1397 TheLexer = IncludeStack.back().TheLexer;
1398 }
1399 const FileEntry *CurFile =
1400 SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
1401
1402 // If this file is older than the file it depends on, emit a diagnostic.
1403 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
1404 // Lex tokens at the end of the message and include them in the message.
1405 std::string Message;
1406 Lex(DependencyTok);
1407 while (DependencyTok.getKind() != tok::eom) {
1408 Message += getSpelling(DependencyTok) + " ";
1409 Lex(DependencyTok);
1410 }
1411
1412 Message.erase(Message.end()-1);
1413 Diag(FilenameTok, diag::pp_out_of_date_dependency, Message);
1414 }
1415}
1416
1417
Chris Lattnerb8761832006-06-24 21:31:03 +00001418/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
1419/// If 'Namespace' is non-null, then it is a token required to exist on the
1420/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1421void Preprocessor::AddPragmaHandler(const char *Namespace,
1422 PragmaHandler *Handler) {
1423 PragmaNamespace *InsertNS = PragmaHandlers;
1424
1425 // If this is specified to be in a namespace, step down into it.
1426 if (Namespace) {
1427 IdentifierTokenInfo *NSID = getIdentifierInfo(Namespace);
1428
1429 // If there is already a pragma handler with the name of this namespace,
1430 // we either have an error (directive with the same name as a namespace) or
1431 // we already have the namespace to insert into.
1432 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
1433 InsertNS = Existing->getIfNamespace();
1434 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
1435 " handler with the same name!");
1436 } else {
1437 // Otherwise, this namespace doesn't exist yet, create and insert the
1438 // handler for it.
1439 InsertNS = new PragmaNamespace(NSID);
1440 PragmaHandlers->AddPragma(InsertNS);
1441 }
1442 }
1443
1444 // Check to make sure we don't already have a pragma for this identifier.
1445 assert(!InsertNS->FindHandler(Handler->getName()) &&
1446 "Pragma handler already exists for this identifier!");
1447 InsertNS->AddPragma(Handler);
1448}
1449
Chris Lattner17862172006-06-24 22:12:56 +00001450namespace {
Chris Lattner55a60952006-06-25 04:20:34 +00001451struct PragmaOnceHandler : public PragmaHandler {
Chris Lattnerb8761832006-06-24 21:31:03 +00001452 PragmaOnceHandler(const IdentifierTokenInfo *OnceID) : PragmaHandler(OnceID){}
1453 virtual void HandlePragma(Preprocessor &PP, LexerToken &OnceTok) {
1454 PP.CheckEndOfDirective("#pragma once");
1455 PP.HandlePragmaOnce(OnceTok);
1456 }
1457};
1458
Chris Lattner55a60952006-06-25 04:20:34 +00001459struct PragmaPoisonHandler : public PragmaHandler {
Chris Lattner17862172006-06-24 22:12:56 +00001460 PragmaPoisonHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
1461 virtual void HandlePragma(Preprocessor &PP, LexerToken &PoisonTok) {
1462 PP.HandlePragmaPoison(PoisonTok);
1463 }
1464};
Chris Lattner55a60952006-06-25 04:20:34 +00001465
1466struct PragmaSystemHeaderHandler : public PragmaHandler {
1467 PragmaSystemHeaderHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID){}
1468 virtual void HandlePragma(Preprocessor &PP, LexerToken &SHToken) {
1469 PP.HandlePragmaSystemHeader(SHToken);
1470 PP.CheckEndOfDirective("#pragma");
1471 }
1472};
Chris Lattner269c2322006-06-25 06:23:00 +00001473struct PragmaDependencyHandler : public PragmaHandler {
1474 PragmaDependencyHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
1475 virtual void HandlePragma(Preprocessor &PP, LexerToken &DepToken) {
1476 PP.HandlePragmaDependency(DepToken);
1477 }
1478};
Chris Lattner17862172006-06-24 22:12:56 +00001479}
1480
Chris Lattnerb8761832006-06-24 21:31:03 +00001481
1482/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1483/// #pragma GCC poison/system_header/dependency and #pragma once.
1484void Preprocessor::RegisterBuiltinPragmas() {
1485 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner17862172006-06-24 22:12:56 +00001486 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
Chris Lattner55a60952006-06-25 04:20:34 +00001487 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
1488 getIdentifierInfo("system_header")));
Chris Lattner269c2322006-06-25 06:23:00 +00001489 AddPragmaHandler("GCC", new PragmaDependencyHandler(
1490 getIdentifierInfo("dependency")));
Chris Lattnerb8761832006-06-24 21:31:03 +00001491}