blob: 5a164d9c71f5834a042c15210c9930b28c3b2711 [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 Lattner22eb9722006-06-18 05:43:12 +000039#include "clang/Basic/Diagnostic.h"
40#include "clang/Basic/FileManager.h"
41#include "clang/Basic/SourceManager.h"
42#include <iostream>
43using namespace llvm;
44using namespace clang;
45
46//===----------------------------------------------------------------------===//
47
48Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
49 FileManager &FM, SourceManager &SM)
50 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
51 SystemDirIdx(0), NoCurDirSearch(false),
Chris Lattnerc8997182006-06-22 05:52:16 +000052 CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
Chris Lattner22eb9722006-06-18 05:43:12 +000053 // Clear stats.
54 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
55 NumIf = NumElse = NumEndif = 0;
56 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
57 MaxIncludeStackDepth = MaxMacroStackDepth = 0;
58 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000059
Chris Lattner22eb9722006-06-18 05:43:12 +000060 // Macro expansion is enabled.
61 DisableMacroExpansion = false;
62 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000063
64 // There is no file-change handler yet.
65 FileChangeHandler = 0;
Chris Lattnerb8761832006-06-24 21:31:03 +000066
67 // Initialize the pragma handlers.
68 PragmaHandlers = new PragmaNamespace(0);
69 RegisterBuiltinPragmas();
Chris Lattner22eb9722006-06-18 05:43:12 +000070}
71
72Preprocessor::~Preprocessor() {
73 // Free any active lexers.
74 delete CurLexer;
75
76 while (!IncludeStack.empty()) {
77 delete IncludeStack.back().TheLexer;
78 IncludeStack.pop_back();
79 }
Chris Lattnerb8761832006-06-24 21:31:03 +000080
81 // Release pragma information.
82 delete PragmaHandlers;
Chris Lattner22eb9722006-06-18 05:43:12 +000083}
84
85/// getFileInfo - Return the PerFileInfo structure for the specified
86/// FileEntry.
87Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
88 if (FE->getUID() >= FileInfo.size())
89 FileInfo.resize(FE->getUID()+1);
90 return FileInfo[FE->getUID()];
91}
92
93
94/// AddKeywords - Add all keywords to the symbol table.
95///
96void Preprocessor::AddKeywords() {
97 enum {
98 C90Shift = 0,
99 EXTC90 = 1 << C90Shift,
100 NOTC90 = 2 << C90Shift,
101 C99Shift = 2,
102 EXTC99 = 1 << C99Shift,
103 NOTC99 = 2 << C99Shift,
104 CPPShift = 4,
105 EXTCPP = 1 << CPPShift,
106 NOTCPP = 2 << CPPShift,
107 Mask = 3
108 };
109
110 // Add keywords and tokens for the current language.
111#define KEYWORD(NAME, FLAGS) \
112 AddKeyword(#NAME+1, tok::kw##NAME, \
113 (FLAGS >> C90Shift) & Mask, \
114 (FLAGS >> C99Shift) & Mask, \
115 (FLAGS >> CPPShift) & Mask);
116#define ALIAS(NAME, TOK) \
117 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
118#include "clang/Basic/TokenKinds.def"
119}
120
121/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
122/// the specified LexerToken's location, translating the token's start
123/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000124void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000125 const std::string &Msg) {
126 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
127 // warnings or extensions.
128 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000129 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000130
Chris Lattnercb283342006-06-18 06:48:37 +0000131 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000132}
Chris Lattnercb283342006-06-18 06:48:37 +0000133void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000134 const std::string &Msg) {
135 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
136 // warnings or extensions.
137 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000138 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000139
Chris Lattner50b497e2006-06-18 16:32:35 +0000140 Diag(Tok.getLocation(), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000141}
142
Chris Lattnerd01e2912006-06-18 16:22:51 +0000143
144void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
145 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
146 << getSpelling(Tok) << "'";
147
148 if (!DumpFlags) return;
149 std::cerr << "\t";
150 if (Tok.isAtStartOfLine())
151 std::cerr << " [StartOfLine]";
152 if (Tok.hasLeadingSpace())
153 std::cerr << " [LeadingSpace]";
154 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000155 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000156 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
157 << "']";
158 }
159}
160
161void Preprocessor::DumpMacro(const MacroInfo &MI) const {
162 std::cerr << "MACRO: ";
163 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
164 DumpToken(MI.getReplacementToken(i));
165 std::cerr << " ";
166 }
167 std::cerr << "\n";
168}
169
Chris Lattner22eb9722006-06-18 05:43:12 +0000170void Preprocessor::PrintStats() {
171 std::cerr << "\n*** Preprocessor Stats:\n";
172 std::cerr << FileInfo.size() << " files tracked.\n";
173 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
174 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
175 NumOnceOnlyFiles += FileInfo[i].isImport;
176 if (MaxNumIncludes < FileInfo[i].NumIncludes)
177 MaxNumIncludes = FileInfo[i].NumIncludes;
178 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
179 }
180 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
181 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
182 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
183
184 std::cerr << NumDirectives << " directives found:\n";
185 std::cerr << " " << NumDefined << " #define.\n";
186 std::cerr << " " << NumUndefined << " #undef.\n";
187 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
188 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
189 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
190 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
191 std::cerr << " " << NumElse << " #else/#elif.\n";
192 std::cerr << " " << NumEndif << " #endif.\n";
193 std::cerr << " " << NumPragma << " #pragma.\n";
194 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
195
196 std::cerr << NumMacroExpanded << " macros expanded, "
197 << NumFastMacroExpanded << " on the fast path.\n";
198 if (MaxMacroStackDepth > 1)
199 std::cerr << " " << MaxMacroStackDepth << " max macroexpand stack depth\n";
200}
201
202//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000203// Token Spelling
204//===----------------------------------------------------------------------===//
205
206
207/// getSpelling() - Return the 'spelling' of this token. The spelling of a
208/// token are the characters used to represent the token in the source file
209/// after trigraph expansion and escaped-newline folding. In particular, this
210/// wants to get the true, uncanonicalized, spelling of things like digraphs
211/// UCNs, etc.
212std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
213 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
214
215 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000216 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000217 assert(TokStart && "Token has invalid location!");
218 if (!Tok.needsCleaning())
219 return std::string(TokStart, TokStart+Tok.getLength());
220
221 // Otherwise, hard case, relex the characters into the string.
222 std::string Result;
223 Result.reserve(Tok.getLength());
224
225 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
226 Ptr != End; ) {
227 unsigned CharSize;
228 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
229 Ptr += CharSize;
230 }
231 assert(Result.size() != unsigned(Tok.getLength()) &&
232 "NeedsCleaning flag set on something that didn't need cleaning!");
233 return Result;
234}
235
236/// getSpelling - This method is used to get the spelling of a token into a
237/// preallocated buffer, instead of as an std::string. The caller is required
238/// to allocate enough space for the token, which is guaranteed to be at least
239/// Tok.getLength() bytes long. The actual length of the token is returned.
240unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
241 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
242
Chris Lattner50b497e2006-06-18 16:32:35 +0000243 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000244 assert(TokStart && "Token has invalid location!");
245
246 // If this token contains nothing interesting, return it directly.
247 if (!Tok.needsCleaning()) {
248 unsigned Size = Tok.getLength();
249 memcpy(Buffer, TokStart, Size);
250 return Size;
251 }
252 // Otherwise, hard case, relex the characters into the string.
253 std::string Result;
254 Result.reserve(Tok.getLength());
255
256 char *OutBuf = Buffer;
257 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
258 Ptr != End; ) {
259 unsigned CharSize;
260 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
261 Ptr += CharSize;
262 }
263 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
264 "NeedsCleaning flag set on something that didn't need cleaning!");
265
266 return OutBuf-Buffer;
267}
268
269//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000270// Source File Location Methods.
271//===----------------------------------------------------------------------===//
272
273
274/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
275/// return null on failure. isAngled indicates whether the file reference is
276/// for system #include's or not (i.e. using <> instead of "").
277const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000278 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000279 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000280 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000281 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000282 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000283
284 // If 'Filename' is absolute, check to see if it exists and no searching.
285 // FIXME: this should be a sys::Path interface, this doesn't handle things
286 // like C:\foo.txt right, nor win32 \\network\device\blah.
287 if (Filename[0] == '/') {
288 // If this was an #include_next "/absolute/file", fail.
289 if (FromDir) return 0;
290
291 // Otherwise, just return the file.
292 return FileMgr.getFile(Filename);
293 }
294
295 // Step #0, unless disabled, check to see if the file is in the #includer's
296 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000297 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000298 const FileEntry *CurFE =
299 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
300 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000301 // Concatenate the requested file onto the directory.
302 // FIXME: should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000303 if (const FileEntry *FE =
304 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000305 if (CurDirLookup)
306 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000307 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000308 CurDir = 0;
309
310 // This file is a system header or C++ unfriendly if the old file is.
311 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000312 return FE;
313 }
314 }
315 }
316
317 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000318 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000319
320 // If this is a #include_next request, start searching after the directory the
321 // file was found in.
322 if (FromDir)
323 i = FromDir-&SearchDirs[0];
324
325 // Check each directory in sequence to see if it contains this file.
326 for (; i != SearchDirs.size(); ++i) {
327 // Concatenate the requested file onto the directory.
328 // FIXME: should be in sys::Path.
329 if (const FileEntry *FE =
330 FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000331 CurDir = &SearchDirs[i];
332
333 // This file is a system header or C++ unfriendly if the dir is.
334 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000335 return FE;
336 }
337 }
338
339 // Otherwise, didn't find it.
340 return 0;
341}
342
343/// EnterSourceFile - Add a source file to the top of the include stack and
344/// start lexing tokens from it instead of the current buffer. Return true
345/// on failure.
346void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerc8997182006-06-22 05:52:16 +0000347 const DirectoryLookup *CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000348 ++NumEnteredSourceFiles;
349
350 // Add the current lexer to the include stack.
351 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000352 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
Chris Lattner22eb9722006-06-18 05:43:12 +0000353 } else {
354 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
355 }
356
357 if (MaxIncludeStackDepth < IncludeStack.size())
358 MaxIncludeStackDepth = IncludeStack.size();
359
360 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
361
Chris Lattnerc8997182006-06-22 05:52:16 +0000362 CurLexer = new Lexer(Buffer, FileID, *this);
363 CurDirLookup = CurDir;
Chris Lattner0c885f52006-06-21 06:50:18 +0000364
365 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000366 if (FileChangeHandler) {
367 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
368
369 // Get the file entry for the current file.
370 if (const FileEntry *FE =
371 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
372 FileType = getFileInfo(FE).DirInfo;
373
374 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart), true,
375 FileType);
376 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000377}
378
379/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000380/// tokens from it instead of the current buffer.
381void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000382 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
383 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000384 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000385 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
386 CurLexer = 0;
387 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000388 } else if (CurMacroExpander) {
389 MacroStack.push_back(CurMacroExpander);
390 }
391
392 if (MaxMacroStackDepth < MacroStack.size())
393 MaxMacroStackDepth = MacroStack.size();
394
395 // TODO: Figure out arguments.
396
397 // Mark the macro as currently disabled, so that it is not recursively
398 // expanded.
399 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000400 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000401}
402
403
404//===----------------------------------------------------------------------===//
405// Lexer Event Handling.
406//===----------------------------------------------------------------------===//
407
408/// HandleIdentifier - This callback is invoked when the lexer reads an
409/// identifier. This callback looks up the identifier in the map and/or
410/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnercb283342006-06-18 06:48:37 +0000411void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000412 if (Identifier.getIdentifierInfo() == 0) {
413 // If we are skipping tokens (because we are in a #if 0 block), there will
414 // be no identifier info, just return the token.
415 assert(isSkipping() && "Token isn't an identifier?");
Chris Lattnercb283342006-06-18 06:48:37 +0000416 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000417 }
418 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
419
420 // FIXME: Check for poisoning in ITI?
421
422 if (MacroInfo *MI = ITI.getMacroInfo()) {
423 if (MI->isEnabled() && !DisableMacroExpansion) {
424 ++NumMacroExpanded;
425 // If we started lexing a macro, enter the macro expansion body.
426 // FIXME: Read/Validate the argument list here!
427
428 // If this macro expands to no tokens, don't bother to push it onto the
429 // expansion stack, only to take it right back off.
430 if (MI->getNumTokens() == 0) {
431 // Ignore this macro use, just return the next token in the current
432 // buffer.
433 bool HadLeadingSpace = Identifier.hasLeadingSpace();
434 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
435
Chris Lattnercb283342006-06-18 06:48:37 +0000436 Lex(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000437
438 // If the identifier isn't on some OTHER line, inherit the leading
439 // whitespace/first-on-a-line property of this token. This handles
440 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
441 // empty.
442 if (!Identifier.isAtStartOfLine()) {
443 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
444 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
445 }
446 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000447 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000448
449 } else if (MI->getNumTokens() == 1 &&
450 // Don't handle identifiers, which might need recursive
451 // expansion.
452 MI->getReplacementToken(0).getIdentifierInfo() == 0) {
453 // FIXME: Function-style macros only if no arguments?
454
455 // Otherwise, if this macro expands into a single trivially-expanded
456 // token: expand it now. This handles common cases like
457 // "#define VAL 42".
458
459 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
460 // identifier to the expanded token.
461 bool isAtStartOfLine = Identifier.isAtStartOfLine();
462 bool hasLeadingSpace = Identifier.hasLeadingSpace();
463
464 // Replace the result token.
465 Identifier = MI->getReplacementToken(0);
466
467 // Restore the StartOfLine/LeadingSpace markers.
468 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
469 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
470
471 // FIXME: Get correct macro expansion stack location info!
472
473 // Since this is not an identifier token, it can't be macro expanded, so
474 // we're done.
475 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000476 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000477 }
478
479 // Start expanding the macro (FIXME, pass arguments).
Chris Lattnercb283342006-06-18 06:48:37 +0000480 EnterMacro(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000481
482 // Now that the macro is at the top of the include stack, ask the
483 // preprocessor to read the next token from it.
484 return Lex(Identifier);
485 }
486 }
487
488 // Change the kind of this identifier to the appropriate token kind, e.g.
489 // turning "for" into a keyword.
490 Identifier.SetKind(ITI.getTokenID());
491
492 // If this is an extension token, diagnose its use.
Chris Lattnercb283342006-06-18 06:48:37 +0000493 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner22eb9722006-06-18 05:43:12 +0000494}
495
496/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
497/// the current file. This either returns the EOF token or pops a level off
498/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000499void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000500 assert(!CurMacroExpander &&
501 "Ending a file when currently in a macro!");
502
503 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
504 // this is an error condition. Just return the EOF token up to
505 // SkipExcludedConditionalBlock. The Lexer will have already have issued
506 // errors for the unterminated #if's on the conditional stack.
507 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000508 Result.StartToken();
509 CurLexer->BufferPtr = CurLexer->BufferEnd;
510 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000511 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000512 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000513 }
514
515 // If this is a #include'd file, pop it off the include stack and continue
516 // lexing the #includer file.
517 if (!IncludeStack.empty()) {
518 // We're done with the #included file.
519 delete CurLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000520 CurLexer = IncludeStack.back().TheLexer;
521 CurDirLookup = IncludeStack.back().TheDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000522 IncludeStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000523
524 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000525 if (FileChangeHandler && !isEndOfMacro) {
526 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
527
528 // Get the file entry for the current file.
529 if (const FileEntry *FE =
530 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
531 FileType = getFileInfo(FE).DirInfo;
532
Chris Lattner0c885f52006-06-21 06:50:18 +0000533 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattnerc8997182006-06-22 05:52:16 +0000534 false, FileType);
535 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000536
Chris Lattner22eb9722006-06-18 05:43:12 +0000537 return Lex(Result);
538 }
539
Chris Lattnerd01e2912006-06-18 16:22:51 +0000540 Result.StartToken();
541 CurLexer->BufferPtr = CurLexer->BufferEnd;
542 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000543 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000544
545 // We're done with the #included file.
546 delete CurLexer;
547 CurLexer = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000548}
549
550/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000551/// the current macro line.
552void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000553 assert(CurMacroExpander && !CurLexer &&
554 "Ending a macro when currently in a #include file!");
555
556 // Mark macro not ignored now that it is no longer being expanded.
557 CurMacroExpander->getMacro().EnableMacro();
558 delete CurMacroExpander;
559
560 if (!MacroStack.empty()) {
561 // In a nested macro invocation, continue lexing from the macro.
562 CurMacroExpander = MacroStack.back();
563 MacroStack.pop_back();
564 return Lex(Result);
565 } else {
566 CurMacroExpander = 0;
567 // Handle this like a #include file being popped off the stack.
Chris Lattner0c885f52006-06-21 06:50:18 +0000568 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000569 }
570}
571
572
573//===----------------------------------------------------------------------===//
574// Utility Methods for Preprocessor Directive Handling.
575//===----------------------------------------------------------------------===//
576
577/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
578/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000579void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000580 LexerToken Tmp;
581 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000582 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000583 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000584}
585
586/// ReadMacroName - Lex and validate a macro name, which occurs after a
587/// #define or #undef. This sets the token kind to eom and discards the rest
588/// of the macro line if the macro name is invalid.
Chris Lattnercb283342006-06-18 06:48:37 +0000589void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000590 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000591 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000592
593 // Missing macro name?
594 if (MacroNameTok.getKind() == tok::eom)
595 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
596
597 if (MacroNameTok.getIdentifierInfo() == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000598 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000599 // Fall through on error.
600 } else if (0) {
601 // FIXME: Error if defining a C++ named operator.
602
603 } else if (0) {
604 // FIXME: Error if defining "defined", "__DATE__", and other predef macros
605 // in C99 6.10.8.4.
606 } else {
607 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000608 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000609 }
610
611
612 // Invalid macro name, read and discard the rest of the line. Then set the
613 // token kind to tok::eom.
614 MacroNameTok.SetKind(tok::eom);
615 return DiscardUntilEndOfDirective();
616}
617
618/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
619/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000620void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000621 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000622 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000623 // There should be no tokens after the directive, but we allow them as an
624 // extension.
625 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000626 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
627 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000628 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000629}
630
631
632
633/// SkipExcludedConditionalBlock - We just read a #if or related directive and
634/// decided that the subsequent tokens are in the #if'd out portion of the
635/// file. Lex the rest of the file, until we see an #endif. If
636/// FoundNonSkipPortion is true, then we have already emitted code for part of
637/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
638/// is true, then #else directives are ok, if not, then we have already seen one
639/// so a #else directive is a duplicate. When this returns, the caller can lex
640/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000641void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000642 bool FoundNonSkipPortion,
643 bool FoundElse) {
644 ++NumSkipped;
645 assert(MacroStack.empty() && CurMacroExpander == 0 && CurLexer &&
646 "Lexing a macro, not a file?");
647
648 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
649 FoundNonSkipPortion, FoundElse);
650
651 // Know that we are going to be skipping tokens. Set this flag to indicate
652 // this, which has a couple of effects:
653 // 1. If EOF of the current lexer is found, the include stack isn't popped.
654 // 2. Identifier information is not looked up for identifier tokens. As an
655 // effect of this, implicit macro expansion is naturally disabled.
656 // 3. "#" tokens at the start of a line are treated as normal tokens, not
657 // implicitly transformed by the lexer.
658 // 4. All notes, warnings, and extension messages are disabled.
659 //
660 SkippingContents = true;
661 LexerToken Tok;
662 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000663 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000664
665 // If this is the end of the buffer, we have an error. The lexer will have
666 // already handled this error condition, so just return and let the caller
667 // lex after this #include.
668 if (Tok.getKind() == tok::eof) break;
669
670 // If this token is not a preprocessor directive, just skip it.
671 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
672 continue;
673
674 // We just parsed a # character at the start of a line, so we're in
675 // directive mode. Tell the lexer this so any newlines we see will be
676 // converted into an EOM token (this terminates the macro).
677 CurLexer->ParsingPreprocessorDirective = true;
678
679 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000680 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000681
682 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
683 // something bogus), skip it.
684 if (Tok.getKind() != tok::identifier) {
685 CurLexer->ParsingPreprocessorDirective = false;
686 continue;
687 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000688
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 // If the first letter isn't i or e, it isn't intesting to us. We know that
690 // this is safe in the face of spelling differences, because there is no way
691 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000692 // allows us to avoid looking up the identifier info for #define/#undef and
693 // other common directives.
694 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
695 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 if (FirstChar >= 'a' && FirstChar <= 'z' &&
697 FirstChar != 'i' && FirstChar != 'e') {
698 CurLexer->ParsingPreprocessorDirective = false;
699 continue;
700 }
701
Chris Lattnere60165f2006-06-22 06:36:29 +0000702 // Get the identifier name without trigraphs or embedded newlines. Note
703 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
704 // when skipping.
705 // TODO: could do this with zero copies in the no-clean case by using
706 // strncmp below.
707 char Directive[20];
708 unsigned IdLen;
709 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
710 IdLen = Tok.getLength();
711 memcpy(Directive, RawCharData, IdLen);
712 Directive[IdLen] = 0;
713 } else {
714 std::string DirectiveStr = getSpelling(Tok);
715 IdLen = DirectiveStr.size();
716 if (IdLen >= 20) {
717 CurLexer->ParsingPreprocessorDirective = false;
718 continue;
719 }
720 memcpy(Directive, &DirectiveStr[0], IdLen);
721 Directive[IdLen] = 0;
722 }
723
Chris Lattner22eb9722006-06-18 05:43:12 +0000724 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000725 if ((IdLen == 2) || // "if"
726 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
727 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000728 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
729 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000730 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000731 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000732 /*foundnonskip*/false,
733 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000734 }
735 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000736 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000737 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000738 PPConditionalInfo CondInfo;
739 CondInfo.WasSkipping = true; // Silence bogus warning.
740 bool InCond = CurLexer->popConditionalLevel(CondInfo);
741 assert(!InCond && "Can't be skipping if not in a conditional!");
742
743 // If we popped the outermost skipping block, we're done skipping!
744 if (!CondInfo.WasSkipping)
745 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000746 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000747 // #else directive in a skipping conditional. If not in some other
748 // skipping conditional, and if #else hasn't already been seen, enter it
749 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000750 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000751 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
752
753 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000754 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000755
756 // Note that we've seen a #else in this conditional.
757 CondInfo.FoundElse = true;
758
759 // If the conditional is at the top level, and the #if block wasn't
760 // entered, enter the #else block now.
761 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
762 CondInfo.FoundNonSkip = true;
763 break;
764 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000765 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +0000766 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
767
768 bool ShouldEnter;
769 // If this is in a skipping block or if we're already handled this #if
770 // block, don't bother parsing the condition.
771 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +0000772 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 ShouldEnter = false;
774 } else {
775 // Evaluate the #elif condition!
776 const char *Start = CurLexer->BufferPtr;
777
778 // Restore the value of SkippingContents so that identifiers are
779 // looked up, etc, inside the #elif expression.
780 assert(SkippingContents && "We have to be skipping here!");
781 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +0000782 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +0000783 SkippingContents = true;
784 }
785
786 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000787 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000788
789 // If this condition is true, enter it!
790 if (ShouldEnter) {
791 CondInfo.FoundNonSkip = true;
792 break;
793 }
794 }
795 }
796
797 CurLexer->ParsingPreprocessorDirective = false;
798 }
799
800 // Finally, if we are out of the conditional (saw an #endif or ran off the end
801 // of the file, just stop skipping and return to lexing whatever came after
802 // the #if block.
803 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000804}
805
806//===----------------------------------------------------------------------===//
807// Preprocessor Directive Handling.
808//===----------------------------------------------------------------------===//
809
810/// HandleDirective - This callback is invoked when the lexer sees a # token
811/// at the start of a line. This consumes the directive, modifies the
812/// lexer/preprocessor state, and advances the lexer(s) so that the next token
813/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +0000814void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
816
817 // We just parsed a # character at the start of a line, so we're in directive
818 // mode. Tell the lexer this so any newlines we see will be converted into an
819 // EOM token (this terminates the macro).
820 CurLexer->ParsingPreprocessorDirective = true;
821
822 ++NumDirectives;
823
824 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000825 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826
827 switch (Result.getKind()) {
828 default: break;
829 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +0000830 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000831
832#if 0
833 case tok::numeric_constant:
834 // FIXME: implement # 7 line numbers!
835 break;
836#endif
837 case tok::kw_else:
838 return HandleElseDirective(Result);
839 case tok::kw_if:
840 return HandleIfDirective(Result);
841 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +0000842 // Get the identifier name without trigraphs or embedded newlines.
843 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +0000844 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +0000845 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000846 case 4:
Chris Lattner40931922006-06-22 06:14:04 +0000847 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnerb8761832006-06-24 21:31:03 +0000848 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +0000849 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000850 return HandleElifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000851 if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
Chris Lattnerb8761832006-06-24 21:31:03 +0000852 isExtension = true; // FIXME: implement #sccs
Chris Lattner22eb9722006-06-18 05:43:12 +0000853 // SCCS is the same as #ident.
854 }
855 break;
856 case 5:
Chris Lattner40931922006-06-22 06:14:04 +0000857 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000858 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000859 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000861 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000862 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000863 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000864 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000865 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattnerb8761832006-06-24 21:31:03 +0000866 isExtension = true; // FIXME: implement #ident
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 break;
868 case 6:
Chris Lattner40931922006-06-22 06:14:04 +0000869 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000870 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000871 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000872 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +0000873 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000874 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +0000875 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
876 return HandlePragmaDirective(Result);
877 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
878 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 break;
880 case 7:
Chris Lattner40931922006-06-22 06:14:04 +0000881 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
882 return HandleIncludeDirective(Result); // Handle #include.
883 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +0000884 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +0000885 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +0000886 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 break;
888 case 8:
Chris Lattner40931922006-06-22 06:14:04 +0000889 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +0000890 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +0000891 }
892 break;
893 case 12:
Chris Lattner40931922006-06-22 06:14:04 +0000894 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
895 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +0000896 break;
897 }
898 break;
899 }
900
901 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +0000902 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000903
904 // Read the rest of the PP line.
905 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000906 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000907 } while (Result.getKind() != tok::eom);
908
909 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000910}
911
Chris Lattnercb283342006-06-18 06:48:37 +0000912void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +0000913 bool isWarning) {
914 // Read the rest of the line raw. We do this because we don't want macros
915 // to be expanded and we don't require that the tokens be valid preprocessing
916 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
917 // collapse multiple consequtive white space between tokens, but this isn't
918 // specified by the standard.
919 std::string Message = CurLexer->ReadToEndOfLine();
920
921 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
922 return Diag(Result, DiagID, Message);
923}
924
Chris Lattnerb8761832006-06-24 21:31:03 +0000925//===----------------------------------------------------------------------===//
926// Preprocessor Include Directive Handling.
927//===----------------------------------------------------------------------===//
928
Chris Lattner22eb9722006-06-18 05:43:12 +0000929/// HandleIncludeDirective - The "#include" tokens have just been read, read the
930/// file to be included from the lexer, then include it! This is a common
931/// routine with functionality shared between #include, #include_next and
932/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +0000933void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +0000934 const DirectoryLookup *LookupFrom,
935 bool isImport) {
936 ++NumIncluded;
937 LexerToken FilenameTok;
Chris Lattnercb283342006-06-18 06:48:37 +0000938 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000939
940 // If the token kind is EOM, the error has already been diagnosed.
941 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +0000942 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000943
944 // Check that we don't have infinite #include recursion.
945 if (IncludeStack.size() == MaxAllowedIncludeStackDepth-1)
946 return Diag(FilenameTok, diag::err_pp_include_too_deep);
947
948 // Get the text form of the filename.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000949 std::string Filename = getSpelling(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000950 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
951
952 // Make sure the filename is <x> or "x".
953 bool isAngled;
954 if (Filename[0] == '<') {
955 isAngled = true;
956 if (Filename[Filename.size()-1] != '>')
957 return Diag(FilenameTok, diag::err_pp_expects_filename);
958 } else if (Filename[0] == '"') {
959 isAngled = false;
960 if (Filename[Filename.size()-1] != '"')
961 return Diag(FilenameTok, diag::err_pp_expects_filename);
962 } else {
963 return Diag(FilenameTok, diag::err_pp_expects_filename);
964 }
965
966 // Remove the quotes.
967 Filename = std::string(Filename.begin()+1, Filename.end()-1);
968
969 // Diagnose #include "" as invalid.
970 if (Filename.empty())
971 return Diag(FilenameTok, diag::err_pp_empty_filename);
972
973 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +0000974 const DirectoryLookup *CurDir;
975 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +0000976 if (File == 0)
977 return Diag(FilenameTok, diag::err_pp_file_not_found);
978
979 // Get information about this file.
980 PerFileInfo &FileInfo = getFileInfo(File);
981
982 // If this is a #import directive, check that we have not already imported
983 // this header.
984 if (isImport) {
985 // If this has already been imported, don't import it again.
986 FileInfo.isImport = true;
987
988 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +0000989 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 } else {
991 // Otherwise, if this is a #include of a file that was previously #import'd
992 // or if this is the second #include of a #pragma once file, ignore it.
993 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +0000994 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 }
996
997 // Look up the file, create a File ID for it.
998 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +0000999 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001000 if (FileID == 0)
1001 return Diag(FilenameTok, diag::err_pp_file_not_found);
1002
1003 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001004 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001005
1006 // Increment the number of times this file has been included.
1007 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001008}
1009
1010/// HandleIncludeNextDirective - Implements #include_next.
1011///
Chris Lattnercb283342006-06-18 06:48:37 +00001012void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1013 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001014
1015 // #include_next is like #include, except that we start searching after
1016 // the current found directory. If we can't do this, issue a
1017 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001018 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001019 if (IncludeStack.empty()) {
1020 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001021 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001022 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001023 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001024 } else {
1025 // Start looking up in the next directory.
1026 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001027 }
1028
1029 return HandleIncludeDirective(IncludeNextTok, Lookup);
1030}
1031
1032/// HandleImportDirective - Implements #import.
1033///
Chris Lattnercb283342006-06-18 06:48:37 +00001034void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1035 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001036
1037 return HandleIncludeDirective(ImportTok, 0, true);
1038}
1039
Chris Lattnerb8761832006-06-24 21:31:03 +00001040//===----------------------------------------------------------------------===//
1041// Preprocessor Macro Directive Handling.
1042//===----------------------------------------------------------------------===//
1043
Chris Lattner22eb9722006-06-18 05:43:12 +00001044/// HandleDefineDirective - Implements #define. This consumes the entire macro
1045/// line then lets the caller lex the next real token.
1046///
Chris Lattnercb283342006-06-18 06:48:37 +00001047void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001048 ++NumDefined;
1049 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001050 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001051
1052 // Error reading macro name? If so, diagnostic already issued.
1053 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001054 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001055
Chris Lattner50b497e2006-06-18 16:32:35 +00001056 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001057
1058 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001059 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001060
1061 if (Tok.getKind() == tok::eom) {
1062 // If there is no body to this macro, we have no special handling here.
1063 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1064 // This is a function-like macro definition.
1065 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001066 return DiscardUntilEndOfDirective();
1067
1068 } else if (!Tok.hasLeadingSpace()) {
1069 // C99 requires whitespace between the macro definition and the body. Emit
1070 // a diagnostic for something like "#define X+".
1071 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001072 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001073 } else {
1074 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1075 // one in some cases!
1076 }
1077 } else {
1078 // This is a normal token with leading space. Clear the leading space
1079 // marker on the first token to get proper expansion.
1080 Tok.ClearFlag(LexerToken::LeadingSpace);
1081 }
1082
1083 // Read the rest of the macro body.
1084 while (Tok.getKind() != tok::eom) {
1085 MI->AddTokenToBody(Tok);
1086
1087 // FIXME: See create_iso_definition.
1088
1089 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001090 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001091 }
1092
1093 // Finally, if this identifier already had a macro defined for it, verify that
1094 // the macro bodies are identical and free the old definition.
1095 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
1096 // FIXME: Verify the definition is the same.
1097 // Macros must be identical. This means all tokes and whitespace separation
1098 // must be the same.
1099 delete OtherMI;
1100 }
1101
1102 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001103}
1104
1105
1106/// HandleUndefDirective - Implements #undef.
1107///
Chris Lattnercb283342006-06-18 06:48:37 +00001108void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 ++NumUndefined;
1110 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001111 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001112
1113 // Error reading macro name? If so, diagnostic already issued.
1114 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001115 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001116
1117 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001118 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001119
1120 // Okay, we finally have a valid identifier to undef.
1121 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1122
1123 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001124 if (MI == 0) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001125
1126#if 0 // FIXME: implement warn_unused_macros.
1127 if (CPP_OPTION (pfile, warn_unused_macros))
1128 _cpp_warn_if_unused_macro (pfile, node, NULL);
1129#endif
1130
1131 // Free macro definition.
1132 delete MI;
1133 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001134}
1135
1136
Chris Lattnerb8761832006-06-24 21:31:03 +00001137//===----------------------------------------------------------------------===//
1138// Preprocessor Conditional Directive Handling.
1139//===----------------------------------------------------------------------===//
1140
Chris Lattner22eb9722006-06-18 05:43:12 +00001141/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1142/// true when this is a #ifndef directive.
1143///
Chris Lattnercb283342006-06-18 06:48:37 +00001144void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001145 ++NumIf;
1146 LexerToken DirectiveTok = Result;
1147
1148 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001149 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001150
1151 // Error reading macro name? If so, diagnostic already issued.
1152 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001153 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001154
1155 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001156 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001157
1158 // Should we include the stuff contained by this directive?
1159 if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
1160 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001161 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001162 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001163 } else {
1164 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001165 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001166 /*Foundnonskip*/false,
1167 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001168 }
1169}
1170
1171/// HandleIfDirective - Implements the #if directive.
1172///
Chris Lattnercb283342006-06-18 06:48:37 +00001173void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001174 ++NumIf;
1175 const char *Start = CurLexer->BufferPtr;
1176
Chris Lattner7966aaf2006-06-18 06:50:36 +00001177 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001178
1179 // Should we include the stuff contained by this directive?
1180 if (ConditionalTrue) {
1181 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001182 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001183 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001184 } else {
1185 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001186 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001187 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001188 }
1189}
1190
1191/// HandleEndifDirective - Implements the #endif directive.
1192///
Chris Lattnercb283342006-06-18 06:48:37 +00001193void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001194 ++NumEndif;
1195 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001196 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001197
1198 PPConditionalInfo CondInfo;
1199 if (CurLexer->popConditionalLevel(CondInfo)) {
1200 // No conditionals on the stack: this is an #endif without an #if.
1201 return Diag(EndifToken, diag::err_pp_endif_without_if);
1202 }
1203
1204 assert(!CondInfo.WasSkipping && !isSkipping() &&
1205 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001206}
1207
1208
Chris Lattnercb283342006-06-18 06:48:37 +00001209void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 ++NumElse;
1211 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001212 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001213
1214 PPConditionalInfo CI;
1215 if (CurLexer->popConditionalLevel(CI))
1216 return Diag(Result, diag::pp_err_else_without_if);
1217
1218 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001219 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001220
1221 // Finally, skip the rest of the contents of this block and return the first
1222 // token after it.
1223 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1224 /*FoundElse*/true);
1225}
1226
Chris Lattnercb283342006-06-18 06:48:37 +00001227void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001228 ++NumElse;
1229 // #elif directive in a non-skipping conditional... start skipping.
1230 // We don't care what the condition is, because we will always skip it (since
1231 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001232 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001233
1234 PPConditionalInfo CI;
1235 if (CurLexer->popConditionalLevel(CI))
1236 return Diag(ElifToken, diag::pp_err_elif_without_if);
1237
1238 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001239 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001240
1241 // Finally, skip the rest of the contents of this block and return the first
1242 // token after it.
1243 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1244 /*FoundElse*/CI.FoundElse);
1245}
Chris Lattnerb8761832006-06-24 21:31:03 +00001246
1247
1248//===----------------------------------------------------------------------===//
1249// Preprocessor Pragma Directive Handling.
1250//===----------------------------------------------------------------------===//
1251
1252/// HandlePragmaDirective - The "#pragma" directive has been parsed with
1253/// PragmaTok containing the "pragma" identifier. Lex the rest of the pragma,
1254/// passing it to the registered pragma handlers.
1255void Preprocessor::HandlePragmaDirective(LexerToken &PragmaTok) {
1256 ++NumPragma;
1257
1258 // Invoke the first level of pragma handlers which reads the namespace id.
1259 LexerToken Tok;
1260 PragmaHandlers->HandlePragma(*this, Tok);
1261
1262 // If the pragma handler didn't read the rest of the line, consume it now.
1263 if (CurLexer->ParsingPreprocessorDirective) {
1264 do {
1265 LexUnexpandedToken(Tok);
1266 } while (Tok.getKind() != tok::eom);
1267 }
1268}
1269
1270/// HandlePragmaOnce - Handle #pragma once. OnceTok is the 'once'.
1271void Preprocessor::HandlePragmaOnce(LexerToken &OnceTok) {
1272 if (IncludeStack.empty()) {
1273 Diag(OnceTok, diag::pp_pragma_once_in_main_file);
1274 return;
1275 }
1276}
1277
1278
1279/// AddPragmaHandler - Add the specified pragma handler to the preprocessor.
1280/// If 'Namespace' is non-null, then it is a token required to exist on the
1281/// pragma line before the pragma string starts, e.g. "STDC" or "GCC".
1282void Preprocessor::AddPragmaHandler(const char *Namespace,
1283 PragmaHandler *Handler) {
1284 PragmaNamespace *InsertNS = PragmaHandlers;
1285
1286 // If this is specified to be in a namespace, step down into it.
1287 if (Namespace) {
1288 IdentifierTokenInfo *NSID = getIdentifierInfo(Namespace);
1289
1290 // If there is already a pragma handler with the name of this namespace,
1291 // we either have an error (directive with the same name as a namespace) or
1292 // we already have the namespace to insert into.
1293 if (PragmaHandler *Existing = PragmaHandlers->FindHandler(NSID)) {
1294 InsertNS = Existing->getIfNamespace();
1295 assert(InsertNS != 0 && "Cannot have a pragma namespace and pragma"
1296 " handler with the same name!");
1297 } else {
1298 // Otherwise, this namespace doesn't exist yet, create and insert the
1299 // handler for it.
1300 InsertNS = new PragmaNamespace(NSID);
1301 PragmaHandlers->AddPragma(InsertNS);
1302 }
1303 }
1304
1305 // Check to make sure we don't already have a pragma for this identifier.
1306 assert(!InsertNS->FindHandler(Handler->getName()) &&
1307 "Pragma handler already exists for this identifier!");
1308 InsertNS->AddPragma(Handler);
1309}
1310
1311class PragmaOnceHandler : public PragmaHandler {
1312public:
1313 PragmaOnceHandler(const IdentifierTokenInfo *OnceID) : PragmaHandler(OnceID){}
1314 virtual void HandlePragma(Preprocessor &PP, LexerToken &OnceTok) {
1315 PP.CheckEndOfDirective("#pragma once");
1316 PP.HandlePragmaOnce(OnceTok);
1317 }
1318};
1319
1320
1321/// RegisterBuiltinPragmas - Install the standard preprocessor pragmas:
1322/// #pragma GCC poison/system_header/dependency and #pragma once.
1323void Preprocessor::RegisterBuiltinPragmas() {
1324 AddPragmaHandler(0, new PragmaOnceHandler(getIdentifierInfo("once")));
1325
1326}