blob: b5cae2df2f6688da9c7f905498a0215dfc2bb49b [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.
Chris Lattner22eb9722006-06-18 05:43:12 +000021// -d[MDNI] - Dump various things.
22// -fworking-directory - #line's with preprocessor's working dir.
23// -fpreprocessed
24// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
25// -W*
26// -w
27//
28// Messages to emit:
29// "Multiple include guards may be useful for:\n"
30//
31// TODO: Implement the include guard optimization.
32//
33//===----------------------------------------------------------------------===//
34
35#include "clang/Lex/Preprocessor.h"
36#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000037#include "clang/Lex/Pragma.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000038#include "clang/Lex/ScratchBuffer.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 Lattner0b8cfc22006-06-28 06:49:17 +000053 ScratchBuf = new ScratchBuffer(SourceMgr);
54
Chris Lattner22eb9722006-06-18 05:43:12 +000055 // Clear stats.
56 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
57 NumIf = NumElse = NumEndif = 0;
58 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
Chris Lattner3665f162006-07-04 07:26:10 +000059 MaxIncludeStackDepth = 0; NumMultiIncludeFileOptzn = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000060 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000061
Chris Lattner22eb9722006-06-18 05:43:12 +000062 // Macro expansion is enabled.
63 DisableMacroExpansion = false;
64 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000065
66 // There is no file-change handler yet.
67 FileChangeHandler = 0;
Chris Lattner01d66cc2006-07-03 22:16:27 +000068 IdentHandler = 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 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";
Chris Lattner3665f162006-07-04 07:26:10 +0000188 std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
189 << " the multi-include optimization.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000190 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
191 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
192 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
193 std::cerr << " " << NumElse << " #else/#elif.\n";
194 std::cerr << " " << NumEndif << " #endif.\n";
195 std::cerr << " " << NumPragma << " #pragma.\n";
196 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
197
198 std::cerr << NumMacroExpanded << " macros expanded, "
199 << NumFastMacroExpanded << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000200}
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.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000285 // FIXME: Portability. This should be a sys::Path interface, this doesn't
286 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner22eb9722006-06-18 05:43:12 +0000287 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 Lattnerf88c53a2006-07-03 05:26:05 +0000298 unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
299 const FileEntry *CurFE = SourceMgr.getFileEntryForFileID(TheFileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000301 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000302 // FIXME: Portability. 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.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000328 // FIXME: Portability. Adding file to dir should be in sys::Path.
329 std::string SearchDir = SearchDirs[i].getDir()->getName()+"/"+Filename;
330 if (const FileEntry *FE = FileMgr.getFile(SearchDir)) {
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
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000343/// isInPrimaryFile - Return true if we're in the top-level file, not in a
344/// #include.
345bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000346 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner13044d92006-07-03 05:16:44 +0000347 return CurLexer->isMainFile();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000348
Chris Lattner13044d92006-07-03 05:16:44 +0000349 // If there are any stacked lexers, we're in a #include.
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000350 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000351 if (IncludeMacroStack[i].TheLexer &&
352 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
353 return IncludeMacroStack[i].TheLexer->isMainFile();
354 return false;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000355}
356
357/// getCurrentLexer - Return the current file lexer being lexed from. Note
358/// that this ignores any potentially active macro expansions and _Pragma
359/// expansions going on at the time.
360Lexer *Preprocessor::getCurrentFileLexer() const {
361 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
362
363 // Look for a stacked lexer.
364 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000365 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000366 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
367 return L;
368 }
369 return 0;
370}
371
372
Chris Lattner22eb9722006-06-18 05:43:12 +0000373/// EnterSourceFile - Add a source file to the top of the include stack and
374/// start lexing tokens from it instead of the current buffer. Return true
375/// on failure.
376void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner13044d92006-07-03 05:16:44 +0000377 const DirectoryLookup *CurDir,
378 bool isMainFile) {
Chris Lattner69772b02006-07-02 20:34:39 +0000379 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 ++NumEnteredSourceFiles;
381
Chris Lattner69772b02006-07-02 20:34:39 +0000382 if (MaxIncludeStackDepth < IncludeMacroStack.size())
383 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000384
Chris Lattner22eb9722006-06-18 05:43:12 +0000385 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
Chris Lattner69772b02006-07-02 20:34:39 +0000386 Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
Chris Lattner13044d92006-07-03 05:16:44 +0000387 if (isMainFile) TheLexer->setIsMainFile();
Chris Lattner69772b02006-07-02 20:34:39 +0000388 EnterSourceFileWithLexer(TheLexer, CurDir);
389}
Chris Lattner22eb9722006-06-18 05:43:12 +0000390
Chris Lattner69772b02006-07-02 20:34:39 +0000391/// EnterSourceFile - Add a source file to the top of the include stack and
392/// start lexing tokens from it instead of the current buffer.
393void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
394 const DirectoryLookup *CurDir) {
395
396 // Add the current lexer to the include stack.
397 if (CurLexer || CurMacroExpander)
398 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
399 CurMacroExpander));
400
401 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000402 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000403 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000404
405 // Notify the client, if desired, that we are in a new source file.
Chris Lattner98a53122006-07-02 23:00:20 +0000406 if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000407 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
408
409 // Get the file entry for the current file.
410 if (const FileEntry *FE =
411 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
412 FileType = getFileInfo(FE).DirInfo;
413
Chris Lattner1840e492006-07-02 22:30:01 +0000414 FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
Chris Lattner55a60952006-06-25 04:20:34 +0000415 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000416 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000417}
418
Chris Lattner69772b02006-07-02 20:34:39 +0000419
420
Chris Lattner22eb9722006-06-18 05:43:12 +0000421/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000422/// tokens from it instead of the current buffer.
423void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000424 IdentifierInfo *Identifier = Tok.getIdentifierInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000425 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner69772b02006-07-02 20:34:39 +0000426 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
427 CurMacroExpander));
428 CurLexer = 0;
429 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000430
431 // TODO: Figure out arguments.
432
433 // Mark the macro as currently disabled, so that it is not recursively
434 // expanded.
435 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000436 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000437}
438
Chris Lattner22eb9722006-06-18 05:43:12 +0000439//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000440// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000441//===----------------------------------------------------------------------===//
442
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000443/// RegisterBuiltinMacro - Register the specified identifier in the identifier
444/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000445IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000446 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000447 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000448
449 // Mark it as being a macro that is builtin.
450 MacroInfo *MI = new MacroInfo(SourceLocation());
451 MI->setIsBuiltinMacro();
452 Id->setMacroInfo(MI);
453 return Id;
454}
455
456
Chris Lattner677757a2006-06-28 05:26:32 +0000457/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
458/// identifier table.
459void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000460 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000461 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000462 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
463 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000464 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000465
466 // GCC Extensions.
467 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
468 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000469 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000470}
471
Chris Lattner677757a2006-06-28 05:26:32 +0000472
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000473/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
474/// expanded as a macro, handle it and return the next token as 'Identifier'.
475void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
476 MacroInfo *MI) {
477 ++NumMacroExpanded;
Chris Lattner13044d92006-07-03 05:16:44 +0000478
479 // Notice that this macro has been used.
480 MI->setIsUsed(true);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000481
482 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
483 if (MI->isBuiltinMacro())
Chris Lattner69772b02006-07-02 20:34:39 +0000484 return ExpandBuiltinMacro(Identifier);
485
486 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000487 // FIXME: Fn-Like Macros: Read/Validate the argument list here!
Chris Lattner69772b02006-07-02 20:34:39 +0000488
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000489
490 // If this macro expands to no tokens, don't bother to push it onto the
491 // expansion stack, only to take it right back off.
492 if (MI->getNumTokens() == 0) {
493 // Ignore this macro use, just return the next token in the current
494 // buffer.
495 bool HadLeadingSpace = Identifier.hasLeadingSpace();
496 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
497
498 Lex(Identifier);
499
500 // If the identifier isn't on some OTHER line, inherit the leading
501 // whitespace/first-on-a-line property of this token. This handles
502 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
503 // empty.
504 if (!Identifier.isAtStartOfLine()) {
505 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
506 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
507 }
508 ++NumFastMacroExpanded;
509 return;
510
511 } else if (MI->getNumTokens() == 1 &&
512 // Don't handle identifiers if they need recursive expansion.
513 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
514 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000515 // FIXME: Fn-Like Macros: Function-style macros only if no arguments?
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000516
517 // Otherwise, if this macro expands into a single trivially-expanded
518 // token: expand it now. This handles common cases like
519 // "#define VAL 42".
520
521 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
522 // identifier to the expanded token.
523 bool isAtStartOfLine = Identifier.isAtStartOfLine();
524 bool hasLeadingSpace = Identifier.hasLeadingSpace();
525
526 // Remember where the token is instantiated.
527 SourceLocation InstantiateLoc = Identifier.getLocation();
528
529 // Replace the result token.
530 Identifier = MI->getReplacementToken(0);
531
532 // Restore the StartOfLine/LeadingSpace markers.
533 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
534 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
535
536 // Update the tokens location to include both its logical and physical
537 // locations.
538 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000539 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000540 Identifier.SetLocation(Loc);
541
542 // Since this is not an identifier token, it can't be macro expanded, so
543 // we're done.
544 ++NumFastMacroExpanded;
545 return;
546 }
547
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000548 // Start expanding the macro (FIXME: Fn-Like Macros: pass arguments).
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000549 EnterMacro(Identifier);
550
551 // Now that the macro is at the top of the include stack, ask the
552 // preprocessor to read the next token from it.
553 return Lex(Identifier);
554}
555
Chris Lattnerc673f902006-06-30 06:10:41 +0000556/// ComputeDATE_TIME - Compute the current time, enter it into the specified
557/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
558/// the identifier tokens inserted.
559static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
560 ScratchBuffer *ScratchBuf) {
561 time_t TT = time(0);
562 struct tm *TM = localtime(&TT);
563
564 static const char * const Months[] = {
565 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
566 };
567
568 char TmpBuffer[100];
569 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
570 TM->tm_year+1900);
571 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
572
573 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
574 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
575}
576
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000577/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
578/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000579void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000580 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000581 IdentifierInfo *II = Tok.getIdentifierInfo();
582 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000583
584 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
585 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000586 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +0000587 return Handle_Pragma(Tok);
588
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000589 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000590
591 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000592 Tok.SetIdentifierInfo(0);
593 Tok.ClearFlag(LexerToken::NeedsCleaning);
594
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000595 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000596 // __LINE__ expands to a simple numeric value.
597 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
598 unsigned Length = strlen(TmpBuffer);
599 Tok.SetKind(tok::numeric_constant);
600 Tok.SetLength(Length);
601 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000602 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000603 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000604 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000605 Diag(Tok, diag::ext_pp_base_file);
606 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
607 while (NextLoc.getFileID() != 0) {
608 Loc = NextLoc;
609 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
610 }
611 }
612
Chris Lattner0766e592006-07-03 01:07:01 +0000613 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
614 std::string FN = SourceMgr.getSourceName(Loc);
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000615 FN = Lexer::Stringify(FN);
Chris Lattner630b33c2006-07-01 22:46:53 +0000616 Tok.SetKind(tok::string_literal);
617 Tok.SetLength(FN.size());
618 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000619 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000620 if (!DATELoc.isValid())
621 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
622 Tok.SetKind(tok::string_literal);
623 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
624 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000625 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000626 if (!TIMELoc.isValid())
627 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
628 Tok.SetKind(tok::string_literal);
629 Tok.SetLength(strlen("\"hh:mm:ss\""));
630 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000631 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000632 Diag(Tok, diag::ext_pp_include_level);
633
634 // Compute the include depth of this token.
635 unsigned Depth = 0;
636 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
637 for (; Loc.getFileID() != 0; ++Depth)
638 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
639
640 // __INCLUDE_LEVEL__ expands to a simple numeric value.
641 sprintf(TmpBuffer, "%u", Depth);
642 unsigned Length = strlen(TmpBuffer);
643 Tok.SetKind(tok::numeric_constant);
644 Tok.SetLength(Length);
645 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000646 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +0000647 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
648 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
649 Diag(Tok, diag::ext_pp_timestamp);
650
651 // Get the file that we are lexing out of. If we're currently lexing from
652 // a macro, dig into the include stack.
653 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000654 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000655
656 if (TheLexer)
657 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
658
659 // If this file is older than the file it depends on, emit a diagnostic.
660 const char *Result;
661 if (CurFile) {
662 time_t TT = CurFile->getModificationTime();
663 struct tm *TM = localtime(&TT);
664 Result = asctime(TM);
665 } else {
666 Result = "??? ??? ?? ??:??:?? ????\n";
667 }
668 TmpBuffer[0] = '"';
669 strcpy(TmpBuffer+1, Result);
670 unsigned Len = strlen(TmpBuffer);
671 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
672 Tok.SetKind(tok::string_literal);
673 Tok.SetLength(Len);
674 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000675 } else {
676 assert(0 && "Unknown identifier!");
677 }
678}
Chris Lattner677757a2006-06-28 05:26:32 +0000679
Chris Lattner13044d92006-07-03 05:16:44 +0000680namespace {
681struct UnusedIdentifierReporter : public IdentifierVisitor {
682 Preprocessor &PP;
683 UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
684
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000685 void VisitIdentifier(IdentifierInfo &II) const {
686 if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
687 PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner13044d92006-07-03 05:16:44 +0000688 }
689};
690}
691
Chris Lattner677757a2006-06-28 05:26:32 +0000692//===----------------------------------------------------------------------===//
693// Lexer Event Handling.
694//===----------------------------------------------------------------------===//
695
696/// HandleIdentifier - This callback is invoked when the lexer reads an
697/// identifier. This callback looks up the identifier in the map and/or
698/// potentially macro expands it or turns it into a named token (like 'for').
699void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
700 if (Identifier.getIdentifierInfo() == 0) {
701 // If we are skipping tokens (because we are in a #if 0 block), there will
702 // be no identifier info, just return the token.
703 assert(isSkipping() && "Token isn't an identifier?");
704 return;
705 }
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000706 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +0000707
708 // If this identifier was poisoned, and if it was not produced from a macro
709 // expansion, emit an error.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000710 if (II.isPoisoned() && CurLexer)
Chris Lattner677757a2006-06-28 05:26:32 +0000711 Diag(Identifier, diag::err_pp_used_poisoned_id);
712
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000713 if (MacroInfo *MI = II.getMacroInfo())
Chris Lattner677757a2006-06-28 05:26:32 +0000714 if (MI->isEnabled() && !DisableMacroExpansion)
715 return HandleMacroExpandedIdentifier(Identifier, MI);
716
717 // Change the kind of this identifier to the appropriate token kind, e.g.
718 // turning "for" into a keyword.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000719 Identifier.SetKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +0000720
721 // If this is an extension token, diagnose its use.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000722 if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +0000723}
724
Chris Lattner22eb9722006-06-18 05:43:12 +0000725/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
726/// the current file. This either returns the EOF token or pops a level off
727/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000728void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 assert(!CurMacroExpander &&
730 "Ending a file when currently in a macro!");
731
732 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
733 // this is an error condition. Just return the EOF token up to
734 // SkipExcludedConditionalBlock. The Lexer will have already have issued
735 // errors for the unterminated #if's on the conditional stack.
736 if (isSkipping()) {
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 Lattnercb283342006-06-18 06:48:37 +0000741 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000742 }
743
Chris Lattner371ac8a2006-07-04 07:11:10 +0000744 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +0000745 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000746 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +0000747 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +0000748 // Okay, this has a controlling macro, remember in PerFileInfo.
749 if (const FileEntry *FE =
750 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
751 getFileInfo(FE).ControllingMacro = ControllingMacro;
Chris Lattner371ac8a2006-07-04 07:11:10 +0000752 }
753 }
754
Chris Lattner22eb9722006-06-18 05:43:12 +0000755 // If this is a #include'd file, pop it off the include stack and continue
756 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000757 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000758 // We're done with the #included file.
759 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000760 CurLexer = IncludeMacroStack.back().TheLexer;
761 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
762 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
763 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000764
765 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000766 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000767 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
768
769 // Get the file entry for the current file.
770 if (const FileEntry *FE =
771 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
772 FileType = getFileInfo(FE).DirInfo;
773
Chris Lattner0c885f52006-06-21 06:50:18 +0000774 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000775 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000776 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000777
Chris Lattner22eb9722006-06-18 05:43:12 +0000778 return Lex(Result);
779 }
780
Chris Lattnerd01e2912006-06-18 16:22:51 +0000781 Result.StartToken();
782 CurLexer->BufferPtr = CurLexer->BufferEnd;
783 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000784 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000785
786 // We're done with the #included file.
787 delete CurLexer;
788 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000789
790 // This is the end of the top-level file.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000791 Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000792}
793
794/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000795/// the current macro line.
796void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000797 assert(CurMacroExpander && !CurLexer &&
798 "Ending a macro when currently in a #include file!");
799
800 // Mark macro not ignored now that it is no longer being expanded.
801 CurMacroExpander->getMacro().EnableMacro();
802 delete CurMacroExpander;
803
Chris Lattner69772b02006-07-02 20:34:39 +0000804 // Handle this like a #include file being popped off the stack.
805 CurMacroExpander = 0;
806 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000807}
808
809
810//===----------------------------------------------------------------------===//
811// Utility Methods for Preprocessor Directive Handling.
812//===----------------------------------------------------------------------===//
813
814/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
815/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000816void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000817 LexerToken Tmp;
818 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000819 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000821}
822
823/// ReadMacroName - Lex and validate a macro name, which occurs after a
824/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattner44f8a662006-07-03 01:27:27 +0000825/// of the macro line if the macro name is invalid. isDefineUndef is true if
826/// this is due to a a #define or #undef directive, false if it is something
827/// else (e.g. #ifdef).
828void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, bool isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000829 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000830 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000831
832 // Missing macro name?
833 if (MacroNameTok.getKind() == tok::eom)
834 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
835
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000836 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
837 if (II == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000838 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000839 // Fall through on error.
840 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000841 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +0000842
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000843 } else if (isDefineUndef && II->getName()[0] == 'd' && // defined
844 !strcmp(II->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +0000845 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +0000846 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000847 } else if (isDefineUndef && II->getMacroInfo() &&
848 II->getMacroInfo()->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +0000849 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
850 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +0000851 } else {
852 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000853 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 }
855
Chris Lattner22eb9722006-06-18 05:43:12 +0000856 // Invalid macro name, read and discard the rest of the line. Then set the
857 // token kind to tok::eom.
858 MacroNameTok.SetKind(tok::eom);
859 return DiscardUntilEndOfDirective();
860}
861
862/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
863/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000864void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000865 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000866 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 // There should be no tokens after the directive, but we allow them as an
868 // extension.
869 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000870 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
871 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000872 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000873}
874
875
876
877/// SkipExcludedConditionalBlock - We just read a #if or related directive and
878/// decided that the subsequent tokens are in the #if'd out portion of the
879/// file. Lex the rest of the file, until we see an #endif. If
880/// FoundNonSkipPortion is true, then we have already emitted code for part of
881/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
882/// is true, then #else directives are ok, if not, then we have already seen one
883/// so a #else directive is a duplicate. When this returns, the caller can lex
884/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000885void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000886 bool FoundNonSkipPortion,
887 bool FoundElse) {
888 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +0000889 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +0000890 "Lexing a macro, not a file?");
891
892 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
893 FoundNonSkipPortion, FoundElse);
894
895 // Know that we are going to be skipping tokens. Set this flag to indicate
896 // this, which has a couple of effects:
897 // 1. If EOF of the current lexer is found, the include stack isn't popped.
898 // 2. Identifier information is not looked up for identifier tokens. As an
899 // effect of this, implicit macro expansion is naturally disabled.
900 // 3. "#" tokens at the start of a line are treated as normal tokens, not
901 // implicitly transformed by the lexer.
902 // 4. All notes, warnings, and extension messages are disabled.
903 //
904 SkippingContents = true;
905 LexerToken Tok;
906 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000907 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000908
909 // If this is the end of the buffer, we have an error. The lexer will have
910 // already handled this error condition, so just return and let the caller
911 // lex after this #include.
912 if (Tok.getKind() == tok::eof) break;
913
914 // If this token is not a preprocessor directive, just skip it.
915 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
916 continue;
917
918 // We just parsed a # character at the start of a line, so we're in
919 // directive mode. Tell the lexer this so any newlines we see will be
920 // converted into an EOM token (this terminates the macro).
921 CurLexer->ParsingPreprocessorDirective = true;
922
923 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000924 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000925
926 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
927 // something bogus), skip it.
928 if (Tok.getKind() != tok::identifier) {
929 CurLexer->ParsingPreprocessorDirective = false;
930 continue;
931 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000932
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 // If the first letter isn't i or e, it isn't intesting to us. We know that
934 // this is safe in the face of spelling differences, because there is no way
935 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000936 // allows us to avoid looking up the identifier info for #define/#undef and
937 // other common directives.
938 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
939 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000940 if (FirstChar >= 'a' && FirstChar <= 'z' &&
941 FirstChar != 'i' && FirstChar != 'e') {
942 CurLexer->ParsingPreprocessorDirective = false;
943 continue;
944 }
945
Chris Lattnere60165f2006-06-22 06:36:29 +0000946 // Get the identifier name without trigraphs or embedded newlines. Note
947 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
948 // when skipping.
949 // TODO: could do this with zero copies in the no-clean case by using
950 // strncmp below.
951 char Directive[20];
952 unsigned IdLen;
953 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
954 IdLen = Tok.getLength();
955 memcpy(Directive, RawCharData, IdLen);
956 Directive[IdLen] = 0;
957 } else {
958 std::string DirectiveStr = getSpelling(Tok);
959 IdLen = DirectiveStr.size();
960 if (IdLen >= 20) {
961 CurLexer->ParsingPreprocessorDirective = false;
962 continue;
963 }
964 memcpy(Directive, &DirectiveStr[0], IdLen);
965 Directive[IdLen] = 0;
966 }
967
Chris Lattner22eb9722006-06-18 05:43:12 +0000968 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000969 if ((IdLen == 2) || // "if"
970 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
971 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000972 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
973 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000974 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000975 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000976 /*foundnonskip*/false,
977 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000978 }
979 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000980 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000981 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 PPConditionalInfo CondInfo;
983 CondInfo.WasSkipping = true; // Silence bogus warning.
984 bool InCond = CurLexer->popConditionalLevel(CondInfo);
985 assert(!InCond && "Can't be skipping if not in a conditional!");
986
987 // If we popped the outermost skipping block, we're done skipping!
988 if (!CondInfo.WasSkipping)
989 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000990 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 // #else directive in a skipping conditional. If not in some other
992 // skipping conditional, and if #else hasn't already been seen, enter it
993 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000994 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
996
997 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000998 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000999
1000 // Note that we've seen a #else in this conditional.
1001 CondInfo.FoundElse = true;
1002
1003 // If the conditional is at the top level, and the #if block wasn't
1004 // entered, enter the #else block now.
1005 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1006 CondInfo.FoundNonSkip = true;
1007 break;
1008 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001009 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001010 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1011
1012 bool ShouldEnter;
1013 // If this is in a skipping block or if we're already handled this #if
1014 // block, don't bother parsing the condition.
1015 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001016 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 ShouldEnter = false;
1018 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001019 // Restore the value of SkippingContents so that identifiers are
1020 // looked up, etc, inside the #elif expression.
1021 assert(SkippingContents && "We have to be skipping here!");
1022 SkippingContents = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001023 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001024 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001025 SkippingContents = true;
1026 }
1027
1028 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001029 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001030
1031 // If this condition is true, enter it!
1032 if (ShouldEnter) {
1033 CondInfo.FoundNonSkip = true;
1034 break;
1035 }
1036 }
1037 }
1038
1039 CurLexer->ParsingPreprocessorDirective = false;
1040 }
1041
1042 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1043 // of the file, just stop skipping and return to lexing whatever came after
1044 // the #if block.
1045 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001046}
1047
1048//===----------------------------------------------------------------------===//
1049// Preprocessor Directive Handling.
1050//===----------------------------------------------------------------------===//
1051
1052/// HandleDirective - This callback is invoked when the lexer sees a # token
1053/// at the start of a line. This consumes the directive, modifies the
1054/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1055/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001056void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001057 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001058
1059 // We just parsed a # character at the start of a line, so we're in directive
1060 // mode. Tell the lexer this so any newlines we see will be converted into an
1061 // EOM token (this terminates the macro).
1062 CurLexer->ParsingPreprocessorDirective = true;
1063
1064 ++NumDirectives;
1065
Chris Lattner371ac8a2006-07-04 07:11:10 +00001066 // We are about to read a token. For the multiple-include optimization FA to
1067 // work, we have to remember if we had read any tokens *before* this
1068 // pp-directive.
1069 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1070
Chris Lattner22eb9722006-06-18 05:43:12 +00001071 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001072 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001073
1074 switch (Result.getKind()) {
1075 default: break;
1076 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001077 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001078
1079#if 0
1080 case tok::numeric_constant:
1081 // FIXME: implement # 7 line numbers!
1082 break;
1083#endif
1084 case tok::kw_else:
1085 return HandleElseDirective(Result);
1086 case tok::kw_if:
Chris Lattnera8654ca2006-07-04 17:42:08 +00001087 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001089 // Get the identifier name without trigraphs or embedded newlines.
1090 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001091 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001092 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001093 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001094 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnera8654ca2006-07-04 17:42:08 +00001095 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001096 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001097 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001098 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1099 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 break;
1101 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001102 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001103 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001104 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001105 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
Chris Lattner40931922006-06-22 06:14:04 +00001106 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001107 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001108 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001110 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001111 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001112 break;
1113 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001114 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001115 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001116 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001117 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
Chris Lattner40931922006-06-22 06:14:04 +00001118 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001119 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001120 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001121 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001122 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1123 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001124 break;
1125 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001126 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1127 return HandleIncludeDirective(Result); // Handle #include.
1128 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001129 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001130 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001131 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001132 break;
1133 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001134 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001135 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001136 }
1137 break;
1138 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001139 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1140 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001141 break;
1142 }
1143 break;
1144 }
1145
1146 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001147 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001148
1149 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001150 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001151
1152 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001153}
1154
Chris Lattner01d66cc2006-07-03 22:16:27 +00001155void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001156 bool isWarning) {
1157 // Read the rest of the line raw. We do this because we don't want macros
1158 // to be expanded and we don't require that the tokens be valid preprocessing
1159 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1160 // collapse multiple consequtive white space between tokens, but this isn't
1161 // specified by the standard.
1162 std::string Message = CurLexer->ReadToEndOfLine();
1163
1164 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001165 return Diag(Tok, DiagID, Message);
1166}
1167
1168/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1169///
1170void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001171 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001172 Diag(Tok, diag::ext_pp_ident_directive);
1173
Chris Lattner371ac8a2006-07-04 07:11:10 +00001174 // Read the string argument.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001175 LexerToken StrTok;
1176 Lex(StrTok);
1177
1178 // If the token kind isn't a string, it's a malformed directive.
1179 if (StrTok.getKind() != tok::string_literal)
1180 return Diag(StrTok, diag::err_pp_malformed_ident);
1181
1182 // Verify that there is nothing after the string, other than EOM.
1183 CheckEndOfDirective("#ident");
1184
1185 if (IdentHandler)
1186 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001187}
1188
Chris Lattnerb8761832006-06-24 21:31:03 +00001189//===----------------------------------------------------------------------===//
1190// Preprocessor Include Directive Handling.
1191//===----------------------------------------------------------------------===//
1192
Chris Lattner22eb9722006-06-18 05:43:12 +00001193/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1194/// file to be included from the lexer, then include it! This is a common
1195/// routine with functionality shared between #include, #include_next and
1196/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001197void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 const DirectoryLookup *LookupFrom,
1199 bool isImport) {
1200 ++NumIncluded;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001201
Chris Lattner22eb9722006-06-18 05:43:12 +00001202 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001203 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001204
1205 // If the token kind is EOM, the error has already been diagnosed.
1206 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001207 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001208
1209 // Verify that there is nothing after the filename, other than EOM. Use the
1210 // preprocessor to lex this in case lexing the filename entered a macro.
1211 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001212
1213 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001214 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1216
Chris Lattner269c2322006-06-25 06:23:00 +00001217 // Find out whether the filename is <x> or "x".
1218 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001219
1220 // Remove the quotes.
1221 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1222
Chris Lattner22eb9722006-06-18 05:43:12 +00001223 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001224 const DirectoryLookup *CurDir;
1225 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 if (File == 0)
1227 return Diag(FilenameTok, diag::err_pp_file_not_found);
1228
1229 // Get information about this file.
1230 PerFileInfo &FileInfo = getFileInfo(File);
1231
1232 // If this is a #import directive, check that we have not already imported
1233 // this header.
1234 if (isImport) {
1235 // If this has already been imported, don't import it again.
1236 FileInfo.isImport = true;
1237
1238 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001239 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001240 } else {
1241 // Otherwise, if this is a #include of a file that was previously #import'd
1242 // or if this is the second #include of a #pragma once file, ignore it.
1243 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001244 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 }
Chris Lattner3665f162006-07-04 07:26:10 +00001246
1247 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1248 // if the macro that guards it is defined, we know the #include has no effect.
1249 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
1250 ++NumMultiIncludeFileOptzn;
1251 return;
1252 }
1253
Chris Lattner22eb9722006-06-18 05:43:12 +00001254
1255 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001256 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001257 if (FileID == 0)
1258 return Diag(FilenameTok, diag::err_pp_file_not_found);
1259
1260 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001261 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001262
1263 // Increment the number of times this file has been included.
1264 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001265}
1266
1267/// HandleIncludeNextDirective - Implements #include_next.
1268///
Chris Lattnercb283342006-06-18 06:48:37 +00001269void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1270 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001271
1272 // #include_next is like #include, except that we start searching after
1273 // the current found directory. If we can't do this, issue a
1274 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001275 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001276 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001277 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001278 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001279 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001280 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001281 } else {
1282 // Start looking up in the next directory.
1283 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001284 }
1285
1286 return HandleIncludeDirective(IncludeNextTok, Lookup);
1287}
1288
1289/// HandleImportDirective - Implements #import.
1290///
Chris Lattnercb283342006-06-18 06:48:37 +00001291void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1292 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001293
1294 return HandleIncludeDirective(ImportTok, 0, true);
1295}
1296
Chris Lattnerb8761832006-06-24 21:31:03 +00001297//===----------------------------------------------------------------------===//
1298// Preprocessor Macro Directive Handling.
1299//===----------------------------------------------------------------------===//
1300
Chris Lattner22eb9722006-06-18 05:43:12 +00001301/// HandleDefineDirective - Implements #define. This consumes the entire macro
1302/// line then lets the caller lex the next real token.
1303///
Chris Lattnercb283342006-06-18 06:48:37 +00001304void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001306
Chris Lattner22eb9722006-06-18 05:43:12 +00001307 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001308 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001309
1310 // Error reading macro name? If so, diagnostic already issued.
1311 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001312 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001313
Chris Lattner50b497e2006-06-18 16:32:35 +00001314 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001315
1316 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001317 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001318
1319 if (Tok.getKind() == tok::eom) {
1320 // If there is no body to this macro, we have no special handling here.
1321 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1322 // This is a function-like macro definition.
1323 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001324 return DiscardUntilEndOfDirective();
1325
1326 } else if (!Tok.hasLeadingSpace()) {
1327 // C99 requires whitespace between the macro definition and the body. Emit
1328 // a diagnostic for something like "#define X+".
1329 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001330 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001331 } else {
1332 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1333 // one in some cases!
1334 }
1335 } else {
1336 // This is a normal token with leading space. Clear the leading space
1337 // marker on the first token to get proper expansion.
1338 Tok.ClearFlag(LexerToken::LeadingSpace);
1339 }
1340
1341 // Read the rest of the macro body.
1342 while (Tok.getKind() != tok::eom) {
1343 MI->AddTokenToBody(Tok);
1344
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001345 // FIXME: Read macro body. See create_iso_definition.
Chris Lattner22eb9722006-06-18 05:43:12 +00001346
1347 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001348 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 }
1350
Chris Lattner13044d92006-07-03 05:16:44 +00001351 // If this is the primary source file, remember that this macro hasn't been
1352 // used yet.
1353 if (isInPrimaryFile())
1354 MI->setIsUsed(false);
1355
Chris Lattner22eb9722006-06-18 05:43:12 +00001356 // Finally, if this identifier already had a macro defined for it, verify that
1357 // the macro bodies are identical and free the old definition.
1358 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001359 if (!OtherMI->isUsed())
1360 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1361
Chris Lattner22eb9722006-06-18 05:43:12 +00001362 // FIXME: Verify the definition is the same.
1363 // Macros must be identical. This means all tokes and whitespace separation
1364 // must be the same.
1365 delete OtherMI;
1366 }
1367
1368 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369}
1370
1371
1372/// HandleUndefDirective - Implements #undef.
1373///
Chris Lattnercb283342006-06-18 06:48:37 +00001374void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001375 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001376
Chris Lattner22eb9722006-06-18 05:43:12 +00001377 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001378 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001379
1380 // Error reading macro name? If so, diagnostic already issued.
1381 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001382 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001383
1384 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001385 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001386
1387 // Okay, we finally have a valid identifier to undef.
1388 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1389
1390 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001391 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001392
Chris Lattner13044d92006-07-03 05:16:44 +00001393 if (!MI->isUsed())
1394 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001395
1396 // Free macro definition.
1397 delete MI;
1398 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001399}
1400
1401
Chris Lattnerb8761832006-06-24 21:31:03 +00001402//===----------------------------------------------------------------------===//
1403// Preprocessor Conditional Directive Handling.
1404//===----------------------------------------------------------------------===//
1405
Chris Lattner22eb9722006-06-18 05:43:12 +00001406/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00001407/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1408/// if any tokens have been returned or pp-directives activated before this
1409/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00001410///
Chris Lattner371ac8a2006-07-04 07:11:10 +00001411void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1412 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001413 ++NumIf;
1414 LexerToken DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001415
Chris Lattner22eb9722006-06-18 05:43:12 +00001416 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001417 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001418
1419 // Error reading macro name? If so, diagnostic already issued.
1420 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001421 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001422
1423 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001424 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1425
1426 // If the start of a top-level #ifdef, inform MIOpt.
1427 if (!ReadAnyTokensBeforeDirective &&
1428 CurLexer->getConditionalStackDepth() == 0) {
1429 assert(isIfndef && "#ifdef shouldn't reach here");
1430 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1431 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001432
Chris Lattnera78a97e2006-07-03 05:42:18 +00001433 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1434
1435 // If there is a macro, mark it used.
1436 if (MI) MI->setIsUsed(true);
1437
Chris Lattner22eb9722006-06-18 05:43:12 +00001438 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001439 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001440 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001441 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001442 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 } else {
1444 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001445 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001446 /*Foundnonskip*/false,
1447 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001448 }
1449}
1450
1451/// HandleIfDirective - Implements the #if directive.
1452///
Chris Lattnera8654ca2006-07-04 17:42:08 +00001453void Preprocessor::HandleIfDirective(LexerToken &IfToken,
1454 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001455 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001456
Chris Lattner371ac8a2006-07-04 07:11:10 +00001457 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001458 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001459 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001460
1461 // Should we include the stuff contained by this directive?
1462 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00001463 // If this condition is equivalent to #ifndef X, and if this is the first
1464 // directive seen, handle it for the multiple-include optimization.
1465 if (!ReadAnyTokensBeforeDirective &&
1466 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
1467 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1468
Chris Lattner22eb9722006-06-18 05:43:12 +00001469 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001470 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001471 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001472 } else {
1473 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001474 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001475 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001476 }
1477}
1478
1479/// HandleEndifDirective - Implements the #endif directive.
1480///
Chris Lattnercb283342006-06-18 06:48:37 +00001481void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001482 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001483
Chris Lattner22eb9722006-06-18 05:43:12 +00001484 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001485 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001486
1487 PPConditionalInfo CondInfo;
1488 if (CurLexer->popConditionalLevel(CondInfo)) {
1489 // No conditionals on the stack: this is an #endif without an #if.
1490 return Diag(EndifToken, diag::err_pp_endif_without_if);
1491 }
1492
Chris Lattner371ac8a2006-07-04 07:11:10 +00001493 // If this the end of a top-level #endif, inform MIOpt.
1494 if (CurLexer->getConditionalStackDepth() == 0)
1495 CurLexer->MIOpt.ExitTopLevelConditional();
1496
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 assert(!CondInfo.WasSkipping && !isSkipping() &&
1498 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001499}
1500
1501
Chris Lattnercb283342006-06-18 06:48:37 +00001502void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001503 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001504
Chris Lattner22eb9722006-06-18 05:43:12 +00001505 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001506 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001507
1508 PPConditionalInfo CI;
1509 if (CurLexer->popConditionalLevel(CI))
1510 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001511
1512 // If this is a top-level #else, inform the MIOpt.
1513 if (CurLexer->getConditionalStackDepth() == 0)
1514 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00001515
1516 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001517 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001518
1519 // Finally, skip the rest of the contents of this block and return the first
1520 // token after it.
1521 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1522 /*FoundElse*/true);
1523}
1524
Chris Lattnercb283342006-06-18 06:48:37 +00001525void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001526 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001527
Chris Lattner22eb9722006-06-18 05:43:12 +00001528 // #elif directive in a non-skipping conditional... start skipping.
1529 // We don't care what the condition is, because we will always skip it (since
1530 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001531 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001532
1533 PPConditionalInfo CI;
1534 if (CurLexer->popConditionalLevel(CI))
1535 return Diag(ElifToken, diag::pp_err_elif_without_if);
1536
Chris Lattner371ac8a2006-07-04 07:11:10 +00001537 // If this is a top-level #elif, inform the MIOpt.
1538 if (CurLexer->getConditionalStackDepth() == 0)
1539 CurLexer->MIOpt.FoundTopLevelElse();
1540
Chris Lattner22eb9722006-06-18 05:43:12 +00001541 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001542 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001543
1544 // Finally, skip the rest of the contents of this block and return the first
1545 // token after it.
1546 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1547 /*FoundElse*/CI.FoundElse);
1548}
Chris Lattnerb8761832006-06-24 21:31:03 +00001549