blob: 75b28c99dd0e096faf180ffd8b895a3139fd9e18 [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"
Chris Lattnerb8761832006-06-24 21:31:03 +000038#include "clang/Lex/Pragma.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000039#include "clang/Lex/ScratchBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000040#include "clang/Basic/Diagnostic.h"
41#include "clang/Basic/FileManager.h"
42#include "clang/Basic/SourceManager.h"
43#include <iostream>
44using namespace llvm;
45using namespace clang;
46
47//===----------------------------------------------------------------------===//
48
49Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
50 FileManager &FM, SourceManager &SM)
51 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
52 SystemDirIdx(0), NoCurDirSearch(false),
Chris Lattnerc8997182006-06-22 05:52:16 +000053 CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +000054 ScratchBuf = new ScratchBuffer(SourceMgr);
55
Chris Lattner22eb9722006-06-18 05:43:12 +000056 // Clear stats.
57 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
58 NumIf = NumElse = NumEndif = 0;
59 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
Chris Lattner69772b02006-07-02 20:34:39 +000060 MaxIncludeStackDepth = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000061 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000062
Chris Lattner22eb9722006-06-18 05:43:12 +000063 // Macro expansion is enabled.
64 DisableMacroExpansion = false;
65 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000066
67 // There is no file-change handler yet.
68 FileChangeHandler = 0;
Chris Lattnerb8761832006-06-24 21:31:03 +000069
70 // Initialize the pragma handlers.
71 PragmaHandlers = new PragmaNamespace(0);
72 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000073
74 // Initialize builtin macros like __LINE__ and friends.
75 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000076}
77
78Preprocessor::~Preprocessor() {
79 // Free any active lexers.
80 delete CurLexer;
81
Chris Lattner69772b02006-07-02 20:34:39 +000082 while (!IncludeMacroStack.empty()) {
83 delete IncludeMacroStack.back().TheLexer;
84 delete IncludeMacroStack.back().TheMacroExpander;
85 IncludeMacroStack.pop_back();
Chris Lattner22eb9722006-06-18 05:43:12 +000086 }
Chris Lattnerb8761832006-06-24 21:31:03 +000087
88 // Release pragma information.
89 delete PragmaHandlers;
Chris Lattner0b8cfc22006-06-28 06:49:17 +000090
91 // Delete the scratch buffer info.
92 delete ScratchBuf;
Chris Lattner22eb9722006-06-18 05:43:12 +000093}
94
95/// getFileInfo - Return the PerFileInfo structure for the specified
96/// FileEntry.
97Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
98 if (FE->getUID() >= FileInfo.size())
99 FileInfo.resize(FE->getUID()+1);
100 return FileInfo[FE->getUID()];
101}
102
103
104/// AddKeywords - Add all keywords to the symbol table.
105///
106void Preprocessor::AddKeywords() {
107 enum {
108 C90Shift = 0,
109 EXTC90 = 1 << C90Shift,
110 NOTC90 = 2 << C90Shift,
111 C99Shift = 2,
112 EXTC99 = 1 << C99Shift,
113 NOTC99 = 2 << C99Shift,
114 CPPShift = 4,
115 EXTCPP = 1 << CPPShift,
116 NOTCPP = 2 << CPPShift,
117 Mask = 3
118 };
119
120 // Add keywords and tokens for the current language.
121#define KEYWORD(NAME, FLAGS) \
122 AddKeyword(#NAME+1, tok::kw##NAME, \
123 (FLAGS >> C90Shift) & Mask, \
124 (FLAGS >> C99Shift) & Mask, \
125 (FLAGS >> CPPShift) & Mask);
126#define ALIAS(NAME, TOK) \
127 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
128#include "clang/Basic/TokenKinds.def"
129}
130
131/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
132/// the specified LexerToken's location, translating the token's start
133/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000134void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000135 const std::string &Msg) {
136 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
137 // warnings or extensions.
138 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000139 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000140
Chris Lattnercb283342006-06-18 06:48:37 +0000141 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000142}
Chris Lattnercb283342006-06-18 06:48:37 +0000143void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000144 const std::string &Msg) {
145 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
146 // warnings or extensions.
147 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000148 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000149
Chris Lattner50b497e2006-06-18 16:32:35 +0000150 Diag(Tok.getLocation(), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000151}
152
Chris Lattnerd01e2912006-06-18 16:22:51 +0000153
154void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
155 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
156 << getSpelling(Tok) << "'";
157
158 if (!DumpFlags) return;
159 std::cerr << "\t";
160 if (Tok.isAtStartOfLine())
161 std::cerr << " [StartOfLine]";
162 if (Tok.hasLeadingSpace())
163 std::cerr << " [LeadingSpace]";
164 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000165 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000166 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
167 << "']";
168 }
169}
170
171void Preprocessor::DumpMacro(const MacroInfo &MI) const {
172 std::cerr << "MACRO: ";
173 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
174 DumpToken(MI.getReplacementToken(i));
175 std::cerr << " ";
176 }
177 std::cerr << "\n";
178}
179
Chris Lattner22eb9722006-06-18 05:43:12 +0000180void Preprocessor::PrintStats() {
181 std::cerr << "\n*** Preprocessor Stats:\n";
182 std::cerr << FileInfo.size() << " files tracked.\n";
183 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
184 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
185 NumOnceOnlyFiles += FileInfo[i].isImport;
186 if (MaxNumIncludes < FileInfo[i].NumIncludes)
187 MaxNumIncludes = FileInfo[i].NumIncludes;
188 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
189 }
190 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
191 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
192 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
193
194 std::cerr << NumDirectives << " directives found:\n";
195 std::cerr << " " << NumDefined << " #define.\n";
196 std::cerr << " " << NumUndefined << " #undef.\n";
197 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
198 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
199 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
200 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
201 std::cerr << " " << NumElse << " #else/#elif.\n";
202 std::cerr << " " << NumEndif << " #endif.\n";
203 std::cerr << " " << NumPragma << " #pragma.\n";
204 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
205
206 std::cerr << NumMacroExpanded << " macros expanded, "
207 << NumFastMacroExpanded << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000208}
209
210//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000211// Token Spelling
212//===----------------------------------------------------------------------===//
213
214
215/// getSpelling() - Return the 'spelling' of this token. The spelling of a
216/// token are the characters used to represent the token in the source file
217/// after trigraph expansion and escaped-newline folding. In particular, this
218/// wants to get the true, uncanonicalized, spelling of things like digraphs
219/// UCNs, etc.
220std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
221 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
222
223 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000224 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000225 assert(TokStart && "Token has invalid location!");
226 if (!Tok.needsCleaning())
227 return std::string(TokStart, TokStart+Tok.getLength());
228
229 // Otherwise, hard case, relex the characters into the string.
230 std::string Result;
231 Result.reserve(Tok.getLength());
232
233 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
234 Ptr != End; ) {
235 unsigned CharSize;
236 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
237 Ptr += CharSize;
238 }
239 assert(Result.size() != unsigned(Tok.getLength()) &&
240 "NeedsCleaning flag set on something that didn't need cleaning!");
241 return Result;
242}
243
244/// getSpelling - This method is used to get the spelling of a token into a
245/// preallocated buffer, instead of as an std::string. The caller is required
246/// to allocate enough space for the token, which is guaranteed to be at least
247/// Tok.getLength() bytes long. The actual length of the token is returned.
248unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
249 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
250
Chris Lattner50b497e2006-06-18 16:32:35 +0000251 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000252 assert(TokStart && "Token has invalid location!");
253
254 // If this token contains nothing interesting, return it directly.
255 if (!Tok.needsCleaning()) {
256 unsigned Size = Tok.getLength();
257 memcpy(Buffer, TokStart, Size);
258 return Size;
259 }
260 // Otherwise, hard case, relex the characters into the string.
261 std::string Result;
262 Result.reserve(Tok.getLength());
263
264 char *OutBuf = Buffer;
265 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
266 Ptr != End; ) {
267 unsigned CharSize;
268 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
269 Ptr += CharSize;
270 }
271 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
272 "NeedsCleaning flag set on something that didn't need cleaning!");
273
274 return OutBuf-Buffer;
275}
276
277//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000278// Source File Location Methods.
279//===----------------------------------------------------------------------===//
280
281
282/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
283/// return null on failure. isAngled indicates whether the file reference is
284/// for system #include's or not (i.e. using <> instead of "").
285const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000286 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000287 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000288 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000289 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000290 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000291
292 // If 'Filename' is absolute, check to see if it exists and no searching.
293 // FIXME: this should be a sys::Path interface, this doesn't handle things
294 // like C:\foo.txt right, nor win32 \\network\device\blah.
295 if (Filename[0] == '/') {
296 // If this was an #include_next "/absolute/file", fail.
297 if (FromDir) return 0;
298
299 // Otherwise, just return the file.
300 return FileMgr.getFile(Filename);
301 }
302
303 // Step #0, unless disabled, check to see if the file is in the #includer's
304 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000305 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000306 const FileEntry *CurFE =
307 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
308 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000309 // Concatenate the requested file onto the directory.
310 // FIXME: should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000311 if (const FileEntry *FE =
312 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000313 if (CurDirLookup)
314 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000315 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000316 CurDir = 0;
317
318 // This file is a system header or C++ unfriendly if the old file is.
319 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000320 return FE;
321 }
322 }
323 }
324
325 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000326 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000327
328 // If this is a #include_next request, start searching after the directory the
329 // file was found in.
330 if (FromDir)
331 i = FromDir-&SearchDirs[0];
332
333 // Check each directory in sequence to see if it contains this file.
334 for (; i != SearchDirs.size(); ++i) {
335 // Concatenate the requested file onto the directory.
336 // FIXME: should be in sys::Path.
337 if (const FileEntry *FE =
338 FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000339 CurDir = &SearchDirs[i];
340
341 // This file is a system header or C++ unfriendly if the dir is.
342 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000343 return FE;
344 }
345 }
346
347 // Otherwise, didn't find it.
348 return 0;
349}
350
351/// EnterSourceFile - Add a source file to the top of the include stack and
352/// start lexing tokens from it instead of the current buffer. Return true
353/// on failure.
354void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerc8997182006-06-22 05:52:16 +0000355 const DirectoryLookup *CurDir) {
Chris Lattner69772b02006-07-02 20:34:39 +0000356 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000357 ++NumEnteredSourceFiles;
358
Chris Lattner69772b02006-07-02 20:34:39 +0000359 if (MaxIncludeStackDepth < IncludeMacroStack.size())
360 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000361
Chris Lattner22eb9722006-06-18 05:43:12 +0000362 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
Chris Lattner69772b02006-07-02 20:34:39 +0000363 Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
364 EnterSourceFileWithLexer(TheLexer, CurDir);
365}
Chris Lattner22eb9722006-06-18 05:43:12 +0000366
Chris Lattner69772b02006-07-02 20:34:39 +0000367/// EnterSourceFile - Add a source file to the top of the include stack and
368/// start lexing tokens from it instead of the current buffer.
369void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
370 const DirectoryLookup *CurDir) {
371
372 // Add the current lexer to the include stack.
373 if (CurLexer || CurMacroExpander)
374 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
375 CurMacroExpander));
376
377 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000378 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000379 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000380
381 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000382 if (FileChangeHandler) {
383 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
384
385 // Get the file entry for the current file.
386 if (const FileEntry *FE =
387 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
388 FileType = getFileInfo(FE).DirInfo;
389
Chris Lattner55a60952006-06-25 04:20:34 +0000390 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart),
391 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000392 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000393}
394
Chris Lattner69772b02006-07-02 20:34:39 +0000395
396
Chris Lattner22eb9722006-06-18 05:43:12 +0000397/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000398/// tokens from it instead of the current buffer.
399void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000400 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
401 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner69772b02006-07-02 20:34:39 +0000402 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
403 CurMacroExpander));
404 CurLexer = 0;
405 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000406
407 // TODO: Figure out arguments.
408
409 // Mark the macro as currently disabled, so that it is not recursively
410 // expanded.
411 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000412 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000413}
414
Chris Lattner22eb9722006-06-18 05:43:12 +0000415//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000416// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000417//===----------------------------------------------------------------------===//
418
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000419/// RegisterBuiltinMacro - Register the specified identifier in the identifier
420/// table and mark it as a builtin macro to be expanded.
421IdentifierTokenInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
422 // Get the identifier.
423 IdentifierTokenInfo *Id = getIdentifierInfo(Name);
424
425 // Mark it as being a macro that is builtin.
426 MacroInfo *MI = new MacroInfo(SourceLocation());
427 MI->setIsBuiltinMacro();
428 Id->setMacroInfo(MI);
429 return Id;
430}
431
432
Chris Lattner677757a2006-06-28 05:26:32 +0000433/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
434/// identifier table.
435void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000436 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000437 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000438 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
439 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000440 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000441
442 // GCC Extensions.
443 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
444 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000445 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000446
Chris Lattner69772b02006-07-02 20:34:39 +0000447 // FIXME: implement them all:
Chris Lattnerc1283b92006-07-01 23:16:30 +0000448//Pseudo #defines.
449 // __STDC__ 1 if !stdc_0_in_system_headers and "std"
450 // __STDC_VERSION__
451 // __STDC_HOSTED__
452 // __OBJC__
Chris Lattner22eb9722006-06-18 05:43:12 +0000453}
454
Chris Lattner677757a2006-06-28 05:26:32 +0000455
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000456/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
457/// expanded as a macro, handle it and return the next token as 'Identifier'.
458void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
459 MacroInfo *MI) {
460 ++NumMacroExpanded;
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000461
462 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
463 if (MI->isBuiltinMacro())
Chris Lattner69772b02006-07-02 20:34:39 +0000464 return ExpandBuiltinMacro(Identifier);
465
466 // If we started lexing a macro, enter the macro expansion body.
467 // FIXME: Read/Validate the argument list here!
468
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000469
470 // If this macro expands to no tokens, don't bother to push it onto the
471 // expansion stack, only to take it right back off.
472 if (MI->getNumTokens() == 0) {
473 // Ignore this macro use, just return the next token in the current
474 // buffer.
475 bool HadLeadingSpace = Identifier.hasLeadingSpace();
476 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
477
478 Lex(Identifier);
479
480 // If the identifier isn't on some OTHER line, inherit the leading
481 // whitespace/first-on-a-line property of this token. This handles
482 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
483 // empty.
484 if (!Identifier.isAtStartOfLine()) {
485 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
486 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
487 }
488 ++NumFastMacroExpanded;
489 return;
490
491 } else if (MI->getNumTokens() == 1 &&
492 // Don't handle identifiers if they need recursive expansion.
493 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
494 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
495 // FIXME: Function-style macros only if no arguments?
496
497 // Otherwise, if this macro expands into a single trivially-expanded
498 // token: expand it now. This handles common cases like
499 // "#define VAL 42".
500
501 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
502 // identifier to the expanded token.
503 bool isAtStartOfLine = Identifier.isAtStartOfLine();
504 bool hasLeadingSpace = Identifier.hasLeadingSpace();
505
506 // Remember where the token is instantiated.
507 SourceLocation InstantiateLoc = Identifier.getLocation();
508
509 // Replace the result token.
510 Identifier = MI->getReplacementToken(0);
511
512 // Restore the StartOfLine/LeadingSpace markers.
513 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
514 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
515
516 // Update the tokens location to include both its logical and physical
517 // locations.
518 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000519 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000520 Identifier.SetLocation(Loc);
521
522 // Since this is not an identifier token, it can't be macro expanded, so
523 // we're done.
524 ++NumFastMacroExpanded;
525 return;
526 }
527
528 // Start expanding the macro (FIXME, pass arguments).
529 EnterMacro(Identifier);
530
531 // Now that the macro is at the top of the include stack, ask the
532 // preprocessor to read the next token from it.
533 return Lex(Identifier);
534}
535
Chris Lattnerc673f902006-06-30 06:10:41 +0000536/// ComputeDATE_TIME - Compute the current time, enter it into the specified
537/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
538/// the identifier tokens inserted.
539static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
540 ScratchBuffer *ScratchBuf) {
541 time_t TT = time(0);
542 struct tm *TM = localtime(&TT);
543
544 static const char * const Months[] = {
545 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
546 };
547
548 char TmpBuffer[100];
549 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
550 TM->tm_year+1900);
551 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
552
553 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
554 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
555}
556
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000557/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
558/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000559void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000560 // Figure out which token this is.
561 IdentifierTokenInfo *ITI = Tok.getIdentifierInfo();
562 assert(ITI && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000563
564 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
565 // lex the token after it.
566 if (ITI == Ident_Pragma)
567 return Handle_Pragma(Tok);
568
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000569 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000570
571 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000572 Tok.SetIdentifierInfo(0);
573 Tok.ClearFlag(LexerToken::NeedsCleaning);
574
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000575 if (ITI == Ident__LINE__) {
576 // __LINE__ expands to a simple numeric value.
577 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
578 unsigned Length = strlen(TmpBuffer);
579 Tok.SetKind(tok::numeric_constant);
580 Tok.SetLength(Length);
581 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000582 } else if (ITI == Ident__FILE__ || ITI == Ident__BASE_FILE__) {
583 SourceLocation Loc = Tok.getLocation();
584 if (ITI == Ident__BASE_FILE__) {
585 Diag(Tok, diag::ext_pp_base_file);
586 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
587 while (NextLoc.getFileID() != 0) {
588 Loc = NextLoc;
589 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
590 }
591 }
592
593 // FIXME: Escape this filename correctly.
594 std::string FN = '"' + SourceMgr.getSourceName(Loc) + '"';
Chris Lattner630b33c2006-07-01 22:46:53 +0000595 Tok.SetKind(tok::string_literal);
596 Tok.SetLength(FN.size());
597 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000598 } else if (ITI == Ident__DATE__) {
599 if (!DATELoc.isValid())
600 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
601 Tok.SetKind(tok::string_literal);
602 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
603 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000604 } else if (ITI == Ident__TIME__) {
605 if (!TIMELoc.isValid())
606 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
607 Tok.SetKind(tok::string_literal);
608 Tok.SetLength(strlen("\"hh:mm:ss\""));
609 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000610 } else if (ITI == Ident__INCLUDE_LEVEL__) {
611 Diag(Tok, diag::ext_pp_include_level);
612
613 // Compute the include depth of this token.
614 unsigned Depth = 0;
615 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
616 for (; Loc.getFileID() != 0; ++Depth)
617 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
618
619 // __INCLUDE_LEVEL__ expands to a simple numeric value.
620 sprintf(TmpBuffer, "%u", Depth);
621 unsigned Length = strlen(TmpBuffer);
622 Tok.SetKind(tok::numeric_constant);
623 Tok.SetLength(Length);
624 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattner847e0e42006-07-01 23:49:16 +0000625 } else if (ITI == Ident__TIMESTAMP__) {
626 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
627 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
628 Diag(Tok, diag::ext_pp_timestamp);
629
630 // Get the file that we are lexing out of. If we're currently lexing from
631 // a macro, dig into the include stack.
632 const FileEntry *CurFile = 0;
Chris Lattner69772b02006-07-02 20:34:39 +0000633 Lexer *TheLexer = getCurrentLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000634
635 if (TheLexer)
636 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
637
638 // If this file is older than the file it depends on, emit a diagnostic.
639 const char *Result;
640 if (CurFile) {
641 time_t TT = CurFile->getModificationTime();
642 struct tm *TM = localtime(&TT);
643 Result = asctime(TM);
644 } else {
645 Result = "??? ??? ?? ??:??:?? ????\n";
646 }
647 TmpBuffer[0] = '"';
648 strcpy(TmpBuffer+1, Result);
649 unsigned Len = strlen(TmpBuffer);
650 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
651 Tok.SetKind(tok::string_literal);
652 Tok.SetLength(Len);
653 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000654 } else {
655 assert(0 && "Unknown identifier!");
656 }
657}
Chris Lattner677757a2006-06-28 05:26:32 +0000658
659//===----------------------------------------------------------------------===//
660// Lexer Event Handling.
661//===----------------------------------------------------------------------===//
662
663/// HandleIdentifier - This callback is invoked when the lexer reads an
664/// identifier. This callback looks up the identifier in the map and/or
665/// potentially macro expands it or turns it into a named token (like 'for').
666void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
667 if (Identifier.getIdentifierInfo() == 0) {
668 // If we are skipping tokens (because we are in a #if 0 block), there will
669 // be no identifier info, just return the token.
670 assert(isSkipping() && "Token isn't an identifier?");
671 return;
672 }
673 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
674
675 // If this identifier was poisoned, and if it was not produced from a macro
676 // expansion, emit an error.
677 if (ITI.isPoisoned() && CurLexer)
678 Diag(Identifier, diag::err_pp_used_poisoned_id);
679
680 if (MacroInfo *MI = ITI.getMacroInfo())
681 if (MI->isEnabled() && !DisableMacroExpansion)
682 return HandleMacroExpandedIdentifier(Identifier, MI);
683
684 // Change the kind of this identifier to the appropriate token kind, e.g.
685 // turning "for" into a keyword.
686 Identifier.SetKind(ITI.getTokenID());
687
688 // If this is an extension token, diagnose its use.
689 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
690}
691
Chris Lattner22eb9722006-06-18 05:43:12 +0000692/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
693/// the current file. This either returns the EOF token or pops a level off
694/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000695void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 assert(!CurMacroExpander &&
697 "Ending a file when currently in a macro!");
698
699 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
700 // this is an error condition. Just return the EOF token up to
701 // SkipExcludedConditionalBlock. The Lexer will have already have issued
702 // errors for the unterminated #if's on the conditional stack.
703 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000704 Result.StartToken();
705 CurLexer->BufferPtr = CurLexer->BufferEnd;
706 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000707 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000708 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000709 }
710
711 // If this is a #include'd file, pop it off the include stack and continue
712 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000713 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000714 // We're done with the #included file.
715 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000716 CurLexer = IncludeMacroStack.back().TheLexer;
717 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
718 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
719 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000720
721 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000722 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000723 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
724
725 // Get the file entry for the current file.
726 if (const FileEntry *FE =
727 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
728 FileType = getFileInfo(FE).DirInfo;
729
Chris Lattner0c885f52006-06-21 06:50:18 +0000730 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000731 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000732 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000733
Chris Lattner22eb9722006-06-18 05:43:12 +0000734 return Lex(Result);
735 }
736
Chris Lattnerd01e2912006-06-18 16:22:51 +0000737 Result.StartToken();
738 CurLexer->BufferPtr = CurLexer->BufferEnd;
739 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000740 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000741
742 // We're done with the #included file.
743 delete CurLexer;
744 CurLexer = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000745}
746
747/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000748/// the current macro line.
749void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000750 assert(CurMacroExpander && !CurLexer &&
751 "Ending a macro when currently in a #include file!");
752
753 // Mark macro not ignored now that it is no longer being expanded.
754 CurMacroExpander->getMacro().EnableMacro();
755 delete CurMacroExpander;
756
Chris Lattner69772b02006-07-02 20:34:39 +0000757 // Handle this like a #include file being popped off the stack.
758 CurMacroExpander = 0;
759 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000760}
761
762
763//===----------------------------------------------------------------------===//
764// Utility Methods for Preprocessor Directive Handling.
765//===----------------------------------------------------------------------===//
766
767/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
768/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000769void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000770 LexerToken Tmp;
771 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000772 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000774}
775
776/// ReadMacroName - Lex and validate a macro name, which occurs after a
777/// #define or #undef. This sets the token kind to eom and discards the rest
778/// of the macro line if the macro name is invalid.
Chris Lattnercb283342006-06-18 06:48:37 +0000779void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000780 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000781 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000782
783 // Missing macro name?
784 if (MacroNameTok.getKind() == tok::eom)
785 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
786
787 if (MacroNameTok.getIdentifierInfo() == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000788 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000789 // Fall through on error.
790 } else if (0) {
791 // FIXME: Error if defining a C++ named operator.
792
793 } else if (0) {
794 // FIXME: Error if defining "defined", "__DATE__", and other predef macros
795 // in C99 6.10.8.4.
796 } else {
797 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000798 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000799 }
800
801
802 // Invalid macro name, read and discard the rest of the line. Then set the
803 // token kind to tok::eom.
804 MacroNameTok.SetKind(tok::eom);
805 return DiscardUntilEndOfDirective();
806}
807
808/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
809/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000810void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000811 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000812 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 // There should be no tokens after the directive, but we allow them as an
814 // extension.
815 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000816 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
817 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000818 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000819}
820
821
822
823/// SkipExcludedConditionalBlock - We just read a #if or related directive and
824/// decided that the subsequent tokens are in the #if'd out portion of the
825/// file. Lex the rest of the file, until we see an #endif. If
826/// FoundNonSkipPortion is true, then we have already emitted code for part of
827/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
828/// is true, then #else directives are ok, if not, then we have already seen one
829/// so a #else directive is a duplicate. When this returns, the caller can lex
830/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000831void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000832 bool FoundNonSkipPortion,
833 bool FoundElse) {
834 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +0000835 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +0000836 "Lexing a macro, not a file?");
837
838 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
839 FoundNonSkipPortion, FoundElse);
840
841 // Know that we are going to be skipping tokens. Set this flag to indicate
842 // this, which has a couple of effects:
843 // 1. If EOF of the current lexer is found, the include stack isn't popped.
844 // 2. Identifier information is not looked up for identifier tokens. As an
845 // effect of this, implicit macro expansion is naturally disabled.
846 // 3. "#" tokens at the start of a line are treated as normal tokens, not
847 // implicitly transformed by the lexer.
848 // 4. All notes, warnings, and extension messages are disabled.
849 //
850 SkippingContents = true;
851 LexerToken Tok;
852 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000853 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000854
855 // If this is the end of the buffer, we have an error. The lexer will have
856 // already handled this error condition, so just return and let the caller
857 // lex after this #include.
858 if (Tok.getKind() == tok::eof) break;
859
860 // If this token is not a preprocessor directive, just skip it.
861 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
862 continue;
863
864 // We just parsed a # character at the start of a line, so we're in
865 // directive mode. Tell the lexer this so any newlines we see will be
866 // converted into an EOM token (this terminates the macro).
867 CurLexer->ParsingPreprocessorDirective = true;
868
869 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000870 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000871
872 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
873 // something bogus), skip it.
874 if (Tok.getKind() != tok::identifier) {
875 CurLexer->ParsingPreprocessorDirective = false;
876 continue;
877 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000878
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 // If the first letter isn't i or e, it isn't intesting to us. We know that
880 // this is safe in the face of spelling differences, because there is no way
881 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000882 // allows us to avoid looking up the identifier info for #define/#undef and
883 // other common directives.
884 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
885 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000886 if (FirstChar >= 'a' && FirstChar <= 'z' &&
887 FirstChar != 'i' && FirstChar != 'e') {
888 CurLexer->ParsingPreprocessorDirective = false;
889 continue;
890 }
891
Chris Lattnere60165f2006-06-22 06:36:29 +0000892 // Get the identifier name without trigraphs or embedded newlines. Note
893 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
894 // when skipping.
895 // TODO: could do this with zero copies in the no-clean case by using
896 // strncmp below.
897 char Directive[20];
898 unsigned IdLen;
899 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
900 IdLen = Tok.getLength();
901 memcpy(Directive, RawCharData, IdLen);
902 Directive[IdLen] = 0;
903 } else {
904 std::string DirectiveStr = getSpelling(Tok);
905 IdLen = DirectiveStr.size();
906 if (IdLen >= 20) {
907 CurLexer->ParsingPreprocessorDirective = false;
908 continue;
909 }
910 memcpy(Directive, &DirectiveStr[0], IdLen);
911 Directive[IdLen] = 0;
912 }
913
Chris Lattner22eb9722006-06-18 05:43:12 +0000914 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000915 if ((IdLen == 2) || // "if"
916 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
917 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000918 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
919 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000920 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000921 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000922 /*foundnonskip*/false,
923 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 }
925 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000926 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000927 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 PPConditionalInfo CondInfo;
929 CondInfo.WasSkipping = true; // Silence bogus warning.
930 bool InCond = CurLexer->popConditionalLevel(CondInfo);
931 assert(!InCond && "Can't be skipping if not in a conditional!");
932
933 // If we popped the outermost skipping block, we're done skipping!
934 if (!CondInfo.WasSkipping)
935 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000936 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000937 // #else directive in a skipping conditional. If not in some other
938 // skipping conditional, and if #else hasn't already been seen, enter it
939 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000940 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000941 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
942
943 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000944 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000945
946 // Note that we've seen a #else in this conditional.
947 CondInfo.FoundElse = true;
948
949 // If the conditional is at the top level, and the #if block wasn't
950 // entered, enter the #else block now.
951 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
952 CondInfo.FoundNonSkip = true;
953 break;
954 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000955 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +0000956 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
957
958 bool ShouldEnter;
959 // If this is in a skipping block or if we're already handled this #if
960 // block, don't bother parsing the condition.
961 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +0000962 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000963 ShouldEnter = false;
964 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +0000965 // Restore the value of SkippingContents so that identifiers are
966 // looked up, etc, inside the #elif expression.
967 assert(SkippingContents && "We have to be skipping here!");
968 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +0000969 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +0000970 SkippingContents = true;
971 }
972
973 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000974 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000975
976 // If this condition is true, enter it!
977 if (ShouldEnter) {
978 CondInfo.FoundNonSkip = true;
979 break;
980 }
981 }
982 }
983
984 CurLexer->ParsingPreprocessorDirective = false;
985 }
986
987 // Finally, if we are out of the conditional (saw an #endif or ran off the end
988 // of the file, just stop skipping and return to lexing whatever came after
989 // the #if block.
990 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000991}
992
993//===----------------------------------------------------------------------===//
994// Preprocessor Directive Handling.
995//===----------------------------------------------------------------------===//
996
997/// HandleDirective - This callback is invoked when the lexer sees a # token
998/// at the start of a line. This consumes the directive, modifies the
999/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1000/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001001void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001002 // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
1003
1004 // We just parsed a # character at the start of a line, so we're in directive
1005 // mode. Tell the lexer this so any newlines we see will be converted into an
1006 // EOM token (this terminates the macro).
1007 CurLexer->ParsingPreprocessorDirective = true;
1008
1009 ++NumDirectives;
1010
1011 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001012 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001013
1014 switch (Result.getKind()) {
1015 default: break;
1016 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001017 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001018
1019#if 0
1020 case tok::numeric_constant:
1021 // FIXME: implement # 7 line numbers!
1022 break;
1023#endif
1024 case tok::kw_else:
1025 return HandleElseDirective(Result);
1026 case tok::kw_if:
1027 return HandleIfDirective(Result);
1028 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001029 // Get the identifier name without trigraphs or embedded newlines.
1030 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001031 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001032 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001033 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001034 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnerb8761832006-06-24 21:31:03 +00001035 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001036 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001037 return HandleElifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001038 if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001039 isExtension = true; // FIXME: implement #sccs
Chris Lattner22eb9722006-06-18 05:43:12 +00001040 // SCCS is the same as #ident.
1041 }
1042 break;
1043 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001044 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001045 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001046 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001047 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001048 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001049 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001050 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001051 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001052 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattnerb8761832006-06-24 21:31:03 +00001053 isExtension = true; // FIXME: implement #ident
Chris Lattner22eb9722006-06-18 05:43:12 +00001054 break;
1055 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001056 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001057 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001058 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001059 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +00001060 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001061 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001062 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001063 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001064 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1065 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001066 break;
1067 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001068 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1069 return HandleIncludeDirective(Result); // Handle #include.
1070 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001071 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001072 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001073 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 break;
1075 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001076 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001077 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 }
1079 break;
1080 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001081 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1082 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001083 break;
1084 }
1085 break;
1086 }
1087
1088 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001089 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001090
1091 // Read the rest of the PP line.
1092 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001093 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001094 } while (Result.getKind() != tok::eom);
1095
1096 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001097}
1098
Chris Lattnercb283342006-06-18 06:48:37 +00001099void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 bool isWarning) {
1101 // Read the rest of the line raw. We do this because we don't want macros
1102 // to be expanded and we don't require that the tokens be valid preprocessing
1103 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1104 // collapse multiple consequtive white space between tokens, but this isn't
1105 // specified by the standard.
1106 std::string Message = CurLexer->ReadToEndOfLine();
1107
1108 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1109 return Diag(Result, DiagID, Message);
1110}
1111
Chris Lattnerb8761832006-06-24 21:31:03 +00001112//===----------------------------------------------------------------------===//
1113// Preprocessor Include Directive Handling.
1114//===----------------------------------------------------------------------===//
1115
Chris Lattner22eb9722006-06-18 05:43:12 +00001116/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1117/// file to be included from the lexer, then include it! This is a common
1118/// routine with functionality shared between #include, #include_next and
1119/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001120void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001121 const DirectoryLookup *LookupFrom,
1122 bool isImport) {
1123 ++NumIncluded;
1124 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001125 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001126
1127 // If the token kind is EOM, the error has already been diagnosed.
1128 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001129 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001130
1131 // Verify that there is nothing after the filename, other than EOM. Use the
1132 // preprocessor to lex this in case lexing the filename entered a macro.
1133 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001134
1135 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001136 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001137 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1138
Chris Lattner269c2322006-06-25 06:23:00 +00001139 // Find out whether the filename is <x> or "x".
1140 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001141
1142 // Remove the quotes.
1143 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1144
Chris Lattner22eb9722006-06-18 05:43:12 +00001145 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001146 const DirectoryLookup *CurDir;
1147 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 if (File == 0)
1149 return Diag(FilenameTok, diag::err_pp_file_not_found);
1150
1151 // Get information about this file.
1152 PerFileInfo &FileInfo = getFileInfo(File);
1153
1154 // If this is a #import directive, check that we have not already imported
1155 // this header.
1156 if (isImport) {
1157 // If this has already been imported, don't import it again.
1158 FileInfo.isImport = true;
1159
1160 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001161 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001162 } else {
1163 // Otherwise, if this is a #include of a file that was previously #import'd
1164 // or if this is the second #include of a #pragma once file, ignore it.
1165 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001166 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001167 }
1168
1169 // Look up the file, create a File ID for it.
1170 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +00001171 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001172 if (FileID == 0)
1173 return Diag(FilenameTok, diag::err_pp_file_not_found);
1174
1175 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001176 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001177
1178 // Increment the number of times this file has been included.
1179 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001180}
1181
1182/// HandleIncludeNextDirective - Implements #include_next.
1183///
Chris Lattnercb283342006-06-18 06:48:37 +00001184void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1185 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001186
1187 // #include_next is like #include, except that we start searching after
1188 // the current found directory. If we can't do this, issue a
1189 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001190 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001191 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001192 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001193 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001194 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001195 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001196 } else {
1197 // Start looking up in the next directory.
1198 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001199 }
1200
1201 return HandleIncludeDirective(IncludeNextTok, Lookup);
1202}
1203
1204/// HandleImportDirective - Implements #import.
1205///
Chris Lattnercb283342006-06-18 06:48:37 +00001206void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1207 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001208
1209 return HandleIncludeDirective(ImportTok, 0, true);
1210}
1211
Chris Lattnerb8761832006-06-24 21:31:03 +00001212//===----------------------------------------------------------------------===//
1213// Preprocessor Macro Directive Handling.
1214//===----------------------------------------------------------------------===//
1215
Chris Lattner22eb9722006-06-18 05:43:12 +00001216/// HandleDefineDirective - Implements #define. This consumes the entire macro
1217/// line then lets the caller lex the next real token.
1218///
Chris Lattnercb283342006-06-18 06:48:37 +00001219void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 ++NumDefined;
1221 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001222 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001223
1224 // Error reading macro name? If so, diagnostic already issued.
1225 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001226 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001227
Chris Lattner50b497e2006-06-18 16:32:35 +00001228 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001229
1230 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001231 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001232
1233 if (Tok.getKind() == tok::eom) {
1234 // If there is no body to this macro, we have no special handling here.
1235 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1236 // This is a function-like macro definition.
1237 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001238 return DiscardUntilEndOfDirective();
1239
1240 } else if (!Tok.hasLeadingSpace()) {
1241 // C99 requires whitespace between the macro definition and the body. Emit
1242 // a diagnostic for something like "#define X+".
1243 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001244 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 } else {
1246 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1247 // one in some cases!
1248 }
1249 } else {
1250 // This is a normal token with leading space. Clear the leading space
1251 // marker on the first token to get proper expansion.
1252 Tok.ClearFlag(LexerToken::LeadingSpace);
1253 }
1254
1255 // Read the rest of the macro body.
1256 while (Tok.getKind() != tok::eom) {
1257 MI->AddTokenToBody(Tok);
1258
1259 // FIXME: See create_iso_definition.
1260
1261 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001262 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 }
1264
1265 // Finally, if this identifier already had a macro defined for it, verify that
1266 // the macro bodies are identical and free the old definition.
1267 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner677757a2006-06-28 05:26:32 +00001268 if (OtherMI->isBuiltinMacro())
1269 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1270
1271
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 // FIXME: Verify the definition is the same.
1273 // Macros must be identical. This means all tokes and whitespace separation
1274 // must be the same.
1275 delete OtherMI;
1276 }
1277
1278 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001279}
1280
1281
1282/// HandleUndefDirective - Implements #undef.
1283///
Chris Lattnercb283342006-06-18 06:48:37 +00001284void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 ++NumUndefined;
1286 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001287 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001288
1289 // Error reading macro name? If so, diagnostic already issued.
1290 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001291 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001292
1293 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001294 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001295
1296 // Okay, we finally have a valid identifier to undef.
1297 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1298
1299 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001300 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001301
1302 if (MI->isBuiltinMacro())
1303 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001304
1305#if 0 // FIXME: implement warn_unused_macros.
1306 if (CPP_OPTION (pfile, warn_unused_macros))
1307 _cpp_warn_if_unused_macro (pfile, node, NULL);
1308#endif
1309
1310 // Free macro definition.
1311 delete MI;
1312 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001313}
1314
1315
Chris Lattnerb8761832006-06-24 21:31:03 +00001316//===----------------------------------------------------------------------===//
1317// Preprocessor Conditional Directive Handling.
1318//===----------------------------------------------------------------------===//
1319
Chris Lattner22eb9722006-06-18 05:43:12 +00001320/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1321/// true when this is a #ifndef directive.
1322///
Chris Lattnercb283342006-06-18 06:48:37 +00001323void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001324 ++NumIf;
1325 LexerToken DirectiveTok = Result;
1326
1327 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001328 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001329
1330 // Error reading macro name? If so, diagnostic already issued.
1331 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001332 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001333
1334 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001335 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001336
1337 // Should we include the stuff contained by this directive?
1338 if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
1339 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001340 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001341 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001342 } else {
1343 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001344 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001345 /*Foundnonskip*/false,
1346 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 }
1348}
1349
1350/// HandleIfDirective - Implements the #if directive.
1351///
Chris Lattnercb283342006-06-18 06:48:37 +00001352void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001353 ++NumIf;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001354 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001355
1356 // Should we include the stuff contained by this directive?
1357 if (ConditionalTrue) {
1358 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001359 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001360 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001361 } else {
1362 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001363 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001364 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 }
1366}
1367
1368/// HandleEndifDirective - Implements the #endif directive.
1369///
Chris Lattnercb283342006-06-18 06:48:37 +00001370void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001371 ++NumEndif;
1372 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001373 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001374
1375 PPConditionalInfo CondInfo;
1376 if (CurLexer->popConditionalLevel(CondInfo)) {
1377 // No conditionals on the stack: this is an #endif without an #if.
1378 return Diag(EndifToken, diag::err_pp_endif_without_if);
1379 }
1380
1381 assert(!CondInfo.WasSkipping && !isSkipping() &&
1382 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001383}
1384
1385
Chris Lattnercb283342006-06-18 06:48:37 +00001386void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001387 ++NumElse;
1388 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001389 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001390
1391 PPConditionalInfo CI;
1392 if (CurLexer->popConditionalLevel(CI))
1393 return Diag(Result, diag::pp_err_else_without_if);
1394
1395 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001396 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397
1398 // Finally, skip the rest of the contents of this block and return the first
1399 // token after it.
1400 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1401 /*FoundElse*/true);
1402}
1403
Chris Lattnercb283342006-06-18 06:48:37 +00001404void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001405 ++NumElse;
1406 // #elif directive in a non-skipping conditional... start skipping.
1407 // We don't care what the condition is, because we will always skip it (since
1408 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001409 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001410
1411 PPConditionalInfo CI;
1412 if (CurLexer->popConditionalLevel(CI))
1413 return Diag(ElifToken, diag::pp_err_elif_without_if);
1414
1415 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001416 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001417
1418 // Finally, skip the rest of the contents of this block and return the first
1419 // token after it.
1420 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1421 /*FoundElse*/CI.FoundElse);
1422}
Chris Lattnerb8761832006-06-24 21:31:03 +00001423
1424
1425//===----------------------------------------------------------------------===//
1426// Preprocessor Pragma Directive Handling.
1427//===----------------------------------------------------------------------===//
1428
Chris Lattner69772b02006-07-02 20:34:39 +00001429/// HandlePragmaDirective - The "#pragma" directive has been parsed. Lex the
1430/// rest of the pragma, passing it to the registered pragma handlers.
1431void Preprocessor::HandlePragmaDirective() {
Chris Lattnerb8761832006-06-24 21:31:03 +00001432 ++NumPragma;
1433
1434 // Invoke the first level of pragma handlers which reads the namespace id.
1435 LexerToken Tok;
1436 PragmaHandlers->HandlePragma(*this, Tok);
1437
1438 // If the pragma handler didn't read the rest of the line, consume it now.
Chris Lattner17862172006-06-24 22:12:56 +00001439 if (CurLexer->ParsingPreprocessorDirective)
1440 DiscardUntilEndOfDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001441}
1442
Chris Lattner69772b02006-07-02 20:34:39 +00001443/// Handle_Pragma - Read a _Pragma directive, slice it up, process it, then
1444/// return the first token after the directive. The _Pragma token has just
1445/// been read into 'Tok'.
1446void Preprocessor::Handle_Pragma(LexerToken &Tok) {
1447 // Remember the pragma token location.
1448 SourceLocation PragmaLoc = Tok.getLocation();
1449
1450 // Read the '('.
1451 Lex(Tok);
1452 if (Tok.getKind() != tok::l_paren)
1453 return Diag(PragmaLoc, diag::err__Pragma_malformed);
1454
1455 // Read the '"..."'.
1456 Lex(Tok);
1457 if (Tok.getKind() != tok::string_literal)
1458 return Diag(PragmaLoc, diag::err__Pragma_malformed);
1459
1460 // Remember the string.
1461 std::string StrVal = getSpelling(Tok);
1462 SourceLocation StrLoc = Tok.getLocation();
1463
1464 // Read the ')'.
1465 Lex(Tok);
1466 if (Tok.getKind() != tok::r_paren)
1467 return Diag(PragmaLoc, diag::err__Pragma_malformed);
1468
1469 // The _Pragma is lexically sound. Destringize according to C99 6.10.9.1.
1470 if (StrVal[0] == 'L') // Remove L prefix.
1471 StrVal.erase(StrVal.begin());
1472 assert(StrVal[0] == '"' && StrVal[StrVal.size()-1] == '"' &&
1473 "Invalid string token!");
1474
1475 // Remove the front quote, replacing it with a space, so that the pragma
1476 // contents appear to have a space before them.
1477 StrVal[0] = ' ';
1478
1479 // Replace the terminating quote with a \n\0.
1480 StrVal[StrVal.size()-1] = '\n';
1481 StrVal += '\0';
1482
1483 // Remove escaped quotes and escapes.
1484 for (unsigned i = 0, e = StrVal.size(); i != e-1; ++i) {
1485 if (StrVal[i] == '\\' &&
1486 (StrVal[i+1] == '\\' || StrVal[i+1] == '"')) {
1487 // \\ -> '\' and \" -> '"'.
1488 StrVal.erase(StrVal.begin()+i);
1489 --e;
1490 }
1491 }
1492
1493 // Plop the string (including the trailing null) into a buffer where we can
1494 // lex it.
1495 SourceLocation TokLoc = ScratchBuf->getToken(&StrVal[0], StrVal.size());
1496 const char *StrData = SourceMgr.getCharacterData(TokLoc);
1497
1498 // FIXME: Create appropriate mapping info for this FileID, so that we know the
1499 // tokens are coming out of the input string (StrLoc).
1500 unsigned FileID = TokLoc.getFileID();
1501 assert(FileID && "Could not create FileID for predefines?");
1502
1503 // Make and enter a lexer object so that we lex and expand the tokens just
1504 // like any others.
1505 Lexer *TL = new Lexer(SourceMgr.getBuffer(FileID), FileID, *this,
1506 StrData, StrData+StrVal.size()-1 /* no null */);
1507 EnterSourceFileWithLexer(TL, 0);
1508
1509 // Ensure that the lexer thinks it is inside a directive, so that end \n will
1510 // return an EOM token.
1511 TL->ParsingPreprocessorDirective = true;
1512
1513 // With everything set up, lex this as a #pragma directive.
1514 HandlePragmaDirective();
1515
1516 // Finally, return whatever came after the pragma directive.
1517 return Lex(Tok);
1518}
1519
1520
1521
Chris Lattnerb8761832006-06-24 21:31:03 +00001522/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
Chris Lattner17862172006-06-24 22:12:56 +00001523///
Chris Lattnerb8761832006-06-24 21:31:03 +00001524void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) {
Chris Lattner69772b02006-07-02 20:34:39 +00001525 if (isInPrimaryFile()) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001526 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
1527 return;
1528 }
Chris Lattner17862172006-06-24 22:12:56 +00001529
1530 // FIXME: implement the _Pragma thing.
1531 assert(CurLexer && "Cannot have a pragma in a macro expansion yet!");
1532
1533 // Mark the file as a once-only file now.
1534 const FileEntry *File =
1535 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
1536 getFileInfo(File).isImport = true;
Chris Lattnerb8761832006-06-24 21:31:03 +00001537}
1538
Chris Lattner17862172006-06-24 22:12:56 +00001539/// HandlePragmaPoison - Handle #pragma GCC poison. PoisonTok is the 'poison'.
1540///
1541void Preprocessor::HandlePragmaPoison(LexerToken &PoisonTok) {
1542 LexerToken Tok;
1543 assert(!SkippingContents && "Why are we handling pragmas while skipping?");
1544 while (1) {
1545 // Read the next token to poison. While doing this, pretend that we are
1546 // skipping while reading the identifier to poison.
1547 // This avoids errors on code like:
1548 // #pragma GCC poison X
1549 // #pragma GCC poison X
1550 SkippingContents = true;
1551 LexUnexpandedToken(Tok);
1552 SkippingContents = false;
1553
1554 // If we reached the end of line, we're done.
1555 if (Tok.getKind() == tok::eom) return;
1556
1557 // Can only poison identifiers.
1558 if (Tok.getKind() != tok::identifier) {
1559 Diag(Tok, diag::err_pp_invalid_poison);
1560 return;
1561 }
1562
1563 // Look up the identifier info for the token.
1564 std::string TokStr = getSpelling(Tok);
1565 IdentifierTokenInfo *II =
1566 getIdentifierInfo(&TokStr[0], &TokStr[0]+TokStr.size());
1567
1568 // Already poisoned.
1569 if (II->isPoisoned()) continue;
1570
1571 // If this is a macro identifier, emit a warning.
1572 if (II->getMacroInfo())
1573 Diag(Tok, diag::pp_poisoning_existing_macro);
1574
1575 // Finally, poison it!
1576 II->setIsPoisoned();
1577 }
1578}
Chris Lattnerb8761832006-06-24 21:31:03 +00001579
Chris Lattner269c2322006-06-25 06:23:00 +00001580/// HandlePragmaSystemHeader - Implement #pragma GCC system_header. We know
1581/// that the whole directive has been parsed.
Chris Lattner55a60952006-06-25 04:20:34 +00001582void Preprocessor::HandlePragmaSystemHeader(LexerToken &SysHeaderTok) {
Chris Lattner69772b02006-07-02 20:34:39 +00001583 if (isInPrimaryFile()) {
Chris Lattner55a60952006-06-25 04:20:34 +00001584 Diag(SysHeaderTok, diag::pp_pragma_sysheader_in_main_file);
1585 return;
1586 }
1587
1588 // Mark the file as a system header.
1589 const FileEntry *File =
1590 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
1591 getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir;
1592
1593
1594 // Notify the client, if desired, that we are in a new source file.
1595 if (FileChangeHandler)
1596 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
1597 SystemHeaderPragma, DirectoryLookup::SystemHeaderDir);
1598}
1599
Chris Lattner269c2322006-06-25 06:23:00 +00001600/// HandlePragmaDependency - Handle #pragma GCC dependency "foo" blah.
1601///
1602void Preprocessor::HandlePragmaDependency(LexerToken &DependencyTok) {
1603 LexerToken FilenameTok;
1604 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
1605
1606 // If the token kind is EOM, the error has already been diagnosed.
1607 if (FilenameTok.getKind() == tok::eom)
1608 return;
1609
1610 // Find out whether the filename is <x> or "x".
1611 bool isAngled = Filename[0] == '<';
1612
1613 // Remove the quotes.
1614 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1615
1616 // Search include directories.
1617 const DirectoryLookup *CurDir;
1618 const FileEntry *File = LookupFile(Filename, isAngled, 0, CurDir);
1619 if (File == 0)
1620 return Diag(FilenameTok, diag::err_pp_file_not_found);
1621
Chris Lattner69772b02006-07-02 20:34:39 +00001622 Lexer *TheLexer = getCurrentLexer();
Chris Lattner269c2322006-06-25 06:23:00 +00001623 const FileEntry *CurFile =
1624 SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
1625
1626 // If this file is older than the file it depends on, emit a diagnostic.
1627 if (CurFile && CurFile->getModificationTime() < File->getModificationTime()) {
1628 // Lex tokens at the end of the message and include them in the message.
1629 std::string Message;
1630 Lex(DependencyTok);
1631 while (DependencyTok.getKind() != tok::eom) {
1632 Message += getSpelling(DependencyTok) + " ";
1633 Lex(DependencyTok);
1634 }
1635
1636 Message.erase(Message.end()-1);
1637 Diag(FilenameTok, diag::pp_out_of_date_dependency, Message);
1638 }
1639}
1640
1641
Chris Lattnerb8761832006-06-24 21:31:03 +00001642/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
1643/// If 'Namespace' is non-null, then it is a token required to exist on the
1644/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1645void Preprocessor::AddPragmaHandler(const char *Namespace,
1646 PragmaHandler *Handler) {
1647 PragmaNamespace *InsertNS = PragmaHandlers;
1648
1649 // If this is specified to be in a namespace, step down into it.
1650 if (Namespace) {
1651 IdentifierTokenInfo *NSID = getIdentifierInfo(Namespace);
1652
1653 // If there is already a pragma handler with the name of this namespace,
1654 // we either have an error (directive with the same name as a namespace) or
1655 // we already have the namespace to insert into.
1656 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
1657 InsertNS = Existing->getIfNamespace();
1658 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
1659 " handler with the same name!");
1660 } else {
1661 // Otherwise, this namespace doesn't exist yet, create and insert the
1662 // handler for it.
1663 InsertNS = new PragmaNamespace(NSID);
1664 PragmaHandlers->AddPragma(InsertNS);
1665 }
1666 }
1667
1668 // Check to make sure we don't already have a pragma for this identifier.
1669 assert(!InsertNS->FindHandler(Handler->getName()) &&
1670 "Pragma handler already exists for this identifier!");
1671 InsertNS->AddPragma(Handler);
1672}
1673
Chris Lattner17862172006-06-24 22:12:56 +00001674namespace {
Chris Lattner55a60952006-06-25 04:20:34 +00001675struct PragmaOnceHandler : public PragmaHandler {
Chris Lattnerb8761832006-06-24 21:31:03 +00001676 PragmaOnceHandler(const IdentifierTokenInfo *OnceID) : PragmaHandler(OnceID){}
1677 virtual void HandlePragma(Preprocessor &PP, LexerToken &OnceTok) {
1678 PP.CheckEndOfDirective("#pragma once");
1679 PP.HandlePragmaOnce(OnceTok);
1680 }
1681};
1682
Chris Lattner55a60952006-06-25 04:20:34 +00001683struct PragmaPoisonHandler : public PragmaHandler {
Chris Lattner17862172006-06-24 22:12:56 +00001684 PragmaPoisonHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
1685 virtual void HandlePragma(Preprocessor &PP, LexerToken &PoisonTok) {
1686 PP.HandlePragmaPoison(PoisonTok);
1687 }
1688};
Chris Lattner55a60952006-06-25 04:20:34 +00001689
1690struct PragmaSystemHeaderHandler : public PragmaHandler {
1691 PragmaSystemHeaderHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID){}
1692 virtual void HandlePragma(Preprocessor &PP, LexerToken &SHToken) {
1693 PP.HandlePragmaSystemHeader(SHToken);
1694 PP.CheckEndOfDirective("#pragma");
1695 }
1696};
Chris Lattner269c2322006-06-25 06:23:00 +00001697struct PragmaDependencyHandler : public PragmaHandler {
1698 PragmaDependencyHandler(const IdentifierTokenInfo *ID) : PragmaHandler(ID) {}
1699 virtual void HandlePragma(Preprocessor &PP, LexerToken &DepToken) {
1700 PP.HandlePragmaDependency(DepToken);
1701 }
1702};
Chris Lattner17862172006-06-24 22:12:56 +00001703}
1704
Chris Lattnerb8761832006-06-24 21:31:03 +00001705
1706/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1707/// #pragma GCC poison/system_header/dependency and #pragma once.
1708void Preprocessor::RegisterBuiltinPragmas() {
1709 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
Chris Lattner17862172006-06-24 22:12:56 +00001710 AddPragmaHandler("GCC", new PragmaPoisonHandler(getIdentifierInfo("poison")));
Chris Lattner55a60952006-06-25 04:20:34 +00001711 AddPragmaHandler("GCC", new PragmaSystemHeaderHandler(
1712 getIdentifierInfo("system_header")));
Chris Lattner269c2322006-06-25 06:23:00 +00001713 AddPragmaHandler("GCC", new PragmaDependencyHandler(
1714 getIdentifierInfo("dependency")));
Chris Lattnerb8761832006-06-24 21:31:03 +00001715}