blob: db418cbad521eca3a48a37d0f52b8e5620c14756 [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 Lattner69772b02006-07-02 20:34:39 +000059 MaxIncludeStackDepth = 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";
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";
Chris Lattner22eb9722006-06-18 05:43:12 +0000198}
199
200//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000201// Token Spelling
202//===----------------------------------------------------------------------===//
203
204
205/// getSpelling() - Return the 'spelling' of this token. The spelling of a
206/// token are the characters used to represent the token in the source file
207/// after trigraph expansion and escaped-newline folding. In particular, this
208/// wants to get the true, uncanonicalized, spelling of things like digraphs
209/// UCNs, etc.
210std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
211 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
212
213 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000214 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000215 assert(TokStart && "Token has invalid location!");
216 if (!Tok.needsCleaning())
217 return std::string(TokStart, TokStart+Tok.getLength());
218
219 // Otherwise, hard case, relex the characters into the string.
220 std::string Result;
221 Result.reserve(Tok.getLength());
222
223 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
224 Ptr != End; ) {
225 unsigned CharSize;
226 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
227 Ptr += CharSize;
228 }
229 assert(Result.size() != unsigned(Tok.getLength()) &&
230 "NeedsCleaning flag set on something that didn't need cleaning!");
231 return Result;
232}
233
234/// getSpelling - This method is used to get the spelling of a token into a
235/// preallocated buffer, instead of as an std::string. The caller is required
236/// to allocate enough space for the token, which is guaranteed to be at least
237/// Tok.getLength() bytes long. The actual length of the token is returned.
238unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
239 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
240
Chris Lattner50b497e2006-06-18 16:32:35 +0000241 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000242 assert(TokStart && "Token has invalid location!");
243
244 // If this token contains nothing interesting, return it directly.
245 if (!Tok.needsCleaning()) {
246 unsigned Size = Tok.getLength();
247 memcpy(Buffer, TokStart, Size);
248 return Size;
249 }
250 // Otherwise, hard case, relex the characters into the string.
251 std::string Result;
252 Result.reserve(Tok.getLength());
253
254 char *OutBuf = Buffer;
255 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
256 Ptr != End; ) {
257 unsigned CharSize;
258 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
259 Ptr += CharSize;
260 }
261 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
262 "NeedsCleaning flag set on something that didn't need cleaning!");
263
264 return OutBuf-Buffer;
265}
266
267//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000268// Source File Location Methods.
269//===----------------------------------------------------------------------===//
270
271
272/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
273/// return null on failure. isAngled indicates whether the file reference is
274/// for system #include's or not (i.e. using <> instead of "").
275const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000276 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000277 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000278 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000279 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000280 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000281
282 // If 'Filename' is absolute, check to see if it exists and no searching.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000283 // FIXME: Portability. This should be a sys::Path interface, this doesn't
284 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner22eb9722006-06-18 05:43:12 +0000285 if (Filename[0] == '/') {
286 // If this was an #include_next "/absolute/file", fail.
287 if (FromDir) return 0;
288
289 // Otherwise, just return the file.
290 return FileMgr.getFile(Filename);
291 }
292
293 // Step #0, unless disabled, check to see if the file is in the #includer's
294 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000295 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000296 unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
297 const FileEntry *CurFE = SourceMgr.getFileEntryForFileID(TheFileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000298 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000299 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000300 // FIXME: Portability. Should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000301 if (const FileEntry *FE =
302 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000303 if (CurDirLookup)
304 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000305 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000306 CurDir = 0;
307
308 // This file is a system header or C++ unfriendly if the old file is.
309 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000310 return FE;
311 }
312 }
313 }
314
315 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000316 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000317
318 // If this is a #include_next request, start searching after the directory the
319 // file was found in.
320 if (FromDir)
321 i = FromDir-&SearchDirs[0];
322
323 // Check each directory in sequence to see if it contains this file.
324 for (; i != SearchDirs.size(); ++i) {
325 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000326 // FIXME: Portability. Adding file to dir should be in sys::Path.
327 std::string SearchDir = SearchDirs[i].getDir()->getName()+"/"+Filename;
328 if (const FileEntry *FE = FileMgr.getFile(SearchDir)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000329 CurDir = &SearchDirs[i];
330
331 // This file is a system header or C++ unfriendly if the dir is.
332 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000333 return FE;
334 }
335 }
336
337 // Otherwise, didn't find it.
338 return 0;
339}
340
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000341/// isInPrimaryFile - Return true if we're in the top-level file, not in a
342/// #include.
343bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000344 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner13044d92006-07-03 05:16:44 +0000345 return CurLexer->isMainFile();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000346
Chris Lattner13044d92006-07-03 05:16:44 +0000347 // If there are any stacked lexers, we're in a #include.
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000348 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000349 if (IncludeMacroStack[i].TheLexer &&
350 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
351 return IncludeMacroStack[i].TheLexer->isMainFile();
352 return false;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000353}
354
355/// getCurrentLexer - Return the current file lexer being lexed from. Note
356/// that this ignores any potentially active macro expansions and _Pragma
357/// expansions going on at the time.
358Lexer *Preprocessor::getCurrentFileLexer() const {
359 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
360
361 // Look for a stacked lexer.
362 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000363 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000364 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
365 return L;
366 }
367 return 0;
368}
369
370
Chris Lattner22eb9722006-06-18 05:43:12 +0000371/// EnterSourceFile - Add a source file to the top of the include stack and
372/// start lexing tokens from it instead of the current buffer. Return true
373/// on failure.
374void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner13044d92006-07-03 05:16:44 +0000375 const DirectoryLookup *CurDir,
376 bool isMainFile) {
Chris Lattner69772b02006-07-02 20:34:39 +0000377 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000378 ++NumEnteredSourceFiles;
379
Chris Lattner69772b02006-07-02 20:34:39 +0000380 if (MaxIncludeStackDepth < IncludeMacroStack.size())
381 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000382
Chris Lattner22eb9722006-06-18 05:43:12 +0000383 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
Chris Lattner69772b02006-07-02 20:34:39 +0000384 Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
Chris Lattner13044d92006-07-03 05:16:44 +0000385 if (isMainFile) TheLexer->setIsMainFile();
Chris Lattner69772b02006-07-02 20:34:39 +0000386 EnterSourceFileWithLexer(TheLexer, CurDir);
387}
Chris Lattner22eb9722006-06-18 05:43:12 +0000388
Chris Lattner69772b02006-07-02 20:34:39 +0000389/// EnterSourceFile - Add a source file to the top of the include stack and
390/// start lexing tokens from it instead of the current buffer.
391void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
392 const DirectoryLookup *CurDir) {
393
394 // Add the current lexer to the include stack.
395 if (CurLexer || CurMacroExpander)
396 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
397 CurMacroExpander));
398
399 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000400 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000401 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000402
403 // Notify the client, if desired, that we are in a new source file.
Chris Lattner98a53122006-07-02 23:00:20 +0000404 if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000405 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
406
407 // Get the file entry for the current file.
408 if (const FileEntry *FE =
409 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
410 FileType = getFileInfo(FE).DirInfo;
411
Chris Lattner1840e492006-07-02 22:30:01 +0000412 FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
Chris Lattner55a60952006-06-25 04:20:34 +0000413 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000414 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000415}
416
Chris Lattner69772b02006-07-02 20:34:39 +0000417
418
Chris Lattner22eb9722006-06-18 05:43:12 +0000419/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000420/// tokens from it instead of the current buffer.
421void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000422 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
423 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner69772b02006-07-02 20:34:39 +0000424 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
425 CurMacroExpander));
426 CurLexer = 0;
427 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000428
429 // TODO: Figure out arguments.
430
431 // Mark the macro as currently disabled, so that it is not recursively
432 // expanded.
433 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000434 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000435}
436
Chris Lattner22eb9722006-06-18 05:43:12 +0000437//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000438// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000439//===----------------------------------------------------------------------===//
440
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000441/// RegisterBuiltinMacro - Register the specified identifier in the identifier
442/// table and mark it as a builtin macro to be expanded.
443IdentifierTokenInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
444 // Get the identifier.
445 IdentifierTokenInfo *Id = getIdentifierInfo(Name);
446
447 // Mark it as being a macro that is builtin.
448 MacroInfo *MI = new MacroInfo(SourceLocation());
449 MI->setIsBuiltinMacro();
450 Id->setMacroInfo(MI);
451 return Id;
452}
453
454
Chris Lattner677757a2006-06-28 05:26:32 +0000455/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
456/// identifier table.
457void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000458 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000459 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000460 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
461 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000462 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000463
464 // GCC Extensions.
465 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
466 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000467 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000468}
469
Chris Lattner677757a2006-06-28 05:26:32 +0000470
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000471/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
472/// expanded as a macro, handle it and return the next token as 'Identifier'.
473void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
474 MacroInfo *MI) {
475 ++NumMacroExpanded;
Chris Lattner13044d92006-07-03 05:16:44 +0000476
477 // Notice that this macro has been used.
478 MI->setIsUsed(true);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000479
480 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
481 if (MI->isBuiltinMacro())
Chris Lattner69772b02006-07-02 20:34:39 +0000482 return ExpandBuiltinMacro(Identifier);
483
484 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000485 // FIXME: Fn-Like Macros: Read/Validate the argument list here!
Chris Lattner69772b02006-07-02 20:34:39 +0000486
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000487
488 // If this macro expands to no tokens, don't bother to push it onto the
489 // expansion stack, only to take it right back off.
490 if (MI->getNumTokens() == 0) {
491 // Ignore this macro use, just return the next token in the current
492 // buffer.
493 bool HadLeadingSpace = Identifier.hasLeadingSpace();
494 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
495
496 Lex(Identifier);
497
498 // If the identifier isn't on some OTHER line, inherit the leading
499 // whitespace/first-on-a-line property of this token. This handles
500 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
501 // empty.
502 if (!Identifier.isAtStartOfLine()) {
503 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
504 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
505 }
506 ++NumFastMacroExpanded;
507 return;
508
509 } else if (MI->getNumTokens() == 1 &&
510 // Don't handle identifiers if they need recursive expansion.
511 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
512 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000513 // FIXME: Fn-Like Macros: Function-style macros only if no arguments?
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000514
515 // Otherwise, if this macro expands into a single trivially-expanded
516 // token: expand it now. This handles common cases like
517 // "#define VAL 42".
518
519 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
520 // identifier to the expanded token.
521 bool isAtStartOfLine = Identifier.isAtStartOfLine();
522 bool hasLeadingSpace = Identifier.hasLeadingSpace();
523
524 // Remember where the token is instantiated.
525 SourceLocation InstantiateLoc = Identifier.getLocation();
526
527 // Replace the result token.
528 Identifier = MI->getReplacementToken(0);
529
530 // Restore the StartOfLine/LeadingSpace markers.
531 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
532 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
533
534 // Update the tokens location to include both its logical and physical
535 // locations.
536 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000537 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000538 Identifier.SetLocation(Loc);
539
540 // Since this is not an identifier token, it can't be macro expanded, so
541 // we're done.
542 ++NumFastMacroExpanded;
543 return;
544 }
545
Chris Lattnerd7dfa572006-07-04 04:50:35 +0000546 // Start expanding the macro (FIXME: Fn-Like Macros: pass arguments).
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000547 EnterMacro(Identifier);
548
549 // Now that the macro is at the top of the include stack, ask the
550 // preprocessor to read the next token from it.
551 return Lex(Identifier);
552}
553
Chris Lattnerc673f902006-06-30 06:10:41 +0000554/// ComputeDATE_TIME - Compute the current time, enter it into the specified
555/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
556/// the identifier tokens inserted.
557static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
558 ScratchBuffer *ScratchBuf) {
559 time_t TT = time(0);
560 struct tm *TM = localtime(&TT);
561
562 static const char * const Months[] = {
563 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
564 };
565
566 char TmpBuffer[100];
567 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
568 TM->tm_year+1900);
569 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
570
571 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
572 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
573}
574
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000575/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
576/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000577void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000578 // Figure out which token this is.
579 IdentifierTokenInfo *ITI = Tok.getIdentifierInfo();
580 assert(ITI && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000581
582 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
583 // lex the token after it.
584 if (ITI == Ident_Pragma)
585 return Handle_Pragma(Tok);
586
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000587 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000588
589 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000590 Tok.SetIdentifierInfo(0);
591 Tok.ClearFlag(LexerToken::NeedsCleaning);
592
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000593 if (ITI == Ident__LINE__) {
594 // __LINE__ expands to a simple numeric value.
595 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
596 unsigned Length = strlen(TmpBuffer);
597 Tok.SetKind(tok::numeric_constant);
598 Tok.SetLength(Length);
599 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000600 } else if (ITI == Ident__FILE__ || ITI == Ident__BASE_FILE__) {
601 SourceLocation Loc = Tok.getLocation();
602 if (ITI == Ident__BASE_FILE__) {
603 Diag(Tok, diag::ext_pp_base_file);
604 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
605 while (NextLoc.getFileID() != 0) {
606 Loc = NextLoc;
607 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
608 }
609 }
610
Chris Lattner0766e592006-07-03 01:07:01 +0000611 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
612 std::string FN = SourceMgr.getSourceName(Loc);
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000613 FN = Lexer::Stringify(FN);
Chris Lattner630b33c2006-07-01 22:46:53 +0000614 Tok.SetKind(tok::string_literal);
615 Tok.SetLength(FN.size());
616 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000617 } else if (ITI == Ident__DATE__) {
618 if (!DATELoc.isValid())
619 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
620 Tok.SetKind(tok::string_literal);
621 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
622 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000623 } else if (ITI == Ident__TIME__) {
624 if (!TIMELoc.isValid())
625 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
626 Tok.SetKind(tok::string_literal);
627 Tok.SetLength(strlen("\"hh:mm:ss\""));
628 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000629 } else if (ITI == Ident__INCLUDE_LEVEL__) {
630 Diag(Tok, diag::ext_pp_include_level);
631
632 // Compute the include depth of this token.
633 unsigned Depth = 0;
634 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
635 for (; Loc.getFileID() != 0; ++Depth)
636 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
637
638 // __INCLUDE_LEVEL__ expands to a simple numeric value.
639 sprintf(TmpBuffer, "%u", Depth);
640 unsigned Length = strlen(TmpBuffer);
641 Tok.SetKind(tok::numeric_constant);
642 Tok.SetLength(Length);
643 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattner847e0e42006-07-01 23:49:16 +0000644 } else if (ITI == Ident__TIMESTAMP__) {
645 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
646 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
647 Diag(Tok, diag::ext_pp_timestamp);
648
649 // Get the file that we are lexing out of. If we're currently lexing from
650 // a macro, dig into the include stack.
651 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000652 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000653
654 if (TheLexer)
655 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
656
657 // If this file is older than the file it depends on, emit a diagnostic.
658 const char *Result;
659 if (CurFile) {
660 time_t TT = CurFile->getModificationTime();
661 struct tm *TM = localtime(&TT);
662 Result = asctime(TM);
663 } else {
664 Result = "??? ??? ?? ??:??:?? ????\n";
665 }
666 TmpBuffer[0] = '"';
667 strcpy(TmpBuffer+1, Result);
668 unsigned Len = strlen(TmpBuffer);
669 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
670 Tok.SetKind(tok::string_literal);
671 Tok.SetLength(Len);
672 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000673 } else {
674 assert(0 && "Unknown identifier!");
675 }
676}
Chris Lattner677757a2006-06-28 05:26:32 +0000677
Chris Lattner13044d92006-07-03 05:16:44 +0000678namespace {
679struct UnusedIdentifierReporter : public IdentifierVisitor {
680 Preprocessor &PP;
681 UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
682
683 void VisitIdentifier(IdentifierTokenInfo &ITI) const {
684 if (ITI.getMacroInfo() && !ITI.getMacroInfo()->isUsed())
685 PP.Diag(ITI.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
686 }
687};
688}
689
Chris Lattner677757a2006-06-28 05:26:32 +0000690//===----------------------------------------------------------------------===//
691// Lexer Event Handling.
692//===----------------------------------------------------------------------===//
693
694/// HandleIdentifier - This callback is invoked when the lexer reads an
695/// identifier. This callback looks up the identifier in the map and/or
696/// potentially macro expands it or turns it into a named token (like 'for').
697void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
698 if (Identifier.getIdentifierInfo() == 0) {
699 // If we are skipping tokens (because we are in a #if 0 block), there will
700 // be no identifier info, just return the token.
701 assert(isSkipping() && "Token isn't an identifier?");
702 return;
703 }
704 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
705
706 // If this identifier was poisoned, and if it was not produced from a macro
707 // expansion, emit an error.
708 if (ITI.isPoisoned() && CurLexer)
709 Diag(Identifier, diag::err_pp_used_poisoned_id);
710
711 if (MacroInfo *MI = ITI.getMacroInfo())
712 if (MI->isEnabled() && !DisableMacroExpansion)
713 return HandleMacroExpandedIdentifier(Identifier, MI);
714
715 // Change the kind of this identifier to the appropriate token kind, e.g.
716 // turning "for" into a keyword.
717 Identifier.SetKind(ITI.getTokenID());
718
719 // If this is an extension token, diagnose its use.
720 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
721}
722
Chris Lattner22eb9722006-06-18 05:43:12 +0000723/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
724/// the current file. This either returns the EOF token or pops a level off
725/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000726void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000727 assert(!CurMacroExpander &&
728 "Ending a file when currently in a macro!");
729
730 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
731 // this is an error condition. Just return the EOF token up to
732 // SkipExcludedConditionalBlock. The Lexer will have already have issued
733 // errors for the unterminated #if's on the conditional stack.
734 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000735 Result.StartToken();
736 CurLexer->BufferPtr = CurLexer->BufferEnd;
737 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000738 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000739 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000740 }
741
742 // If this is a #include'd file, pop it off the include stack and continue
743 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000744 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000745 // We're done with the #included file.
746 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000747 CurLexer = IncludeMacroStack.back().TheLexer;
748 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
749 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
750 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000751
752 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000753 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000754 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
755
756 // Get the file entry for the current file.
757 if (const FileEntry *FE =
758 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
759 FileType = getFileInfo(FE).DirInfo;
760
Chris Lattner0c885f52006-06-21 06:50:18 +0000761 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000762 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000763 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000764
Chris Lattner22eb9722006-06-18 05:43:12 +0000765 return Lex(Result);
766 }
767
Chris Lattnerd01e2912006-06-18 16:22:51 +0000768 Result.StartToken();
769 CurLexer->BufferPtr = CurLexer->BufferEnd;
770 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000771 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000772
773 // We're done with the #included file.
774 delete CurLexer;
775 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000776
777 // This is the end of the top-level file.
778 IdentifierInfo.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000779}
780
781/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000782/// the current macro line.
783void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000784 assert(CurMacroExpander && !CurLexer &&
785 "Ending a macro when currently in a #include file!");
786
787 // Mark macro not ignored now that it is no longer being expanded.
788 CurMacroExpander->getMacro().EnableMacro();
789 delete CurMacroExpander;
790
Chris Lattner69772b02006-07-02 20:34:39 +0000791 // Handle this like a #include file being popped off the stack.
792 CurMacroExpander = 0;
793 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000794}
795
796
797//===----------------------------------------------------------------------===//
798// Utility Methods for Preprocessor Directive Handling.
799//===----------------------------------------------------------------------===//
800
801/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
802/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000803void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000804 LexerToken Tmp;
805 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000806 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000807 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000808}
809
810/// ReadMacroName - Lex and validate a macro name, which occurs after a
811/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattner44f8a662006-07-03 01:27:27 +0000812/// of the macro line if the macro name is invalid. isDefineUndef is true if
813/// this is due to a a #define or #undef directive, false if it is something
814/// else (e.g. #ifdef).
815void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, bool isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000817 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000818
819 // Missing macro name?
820 if (MacroNameTok.getKind() == tok::eom)
821 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
822
Chris Lattneraaf09112006-07-03 01:17:59 +0000823 IdentifierTokenInfo *ITI = MacroNameTok.getIdentifierInfo();
824 if (ITI == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000825 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826 // Fall through on error.
827 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000828 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +0000829
Chris Lattner44f8a662006-07-03 01:27:27 +0000830 } else if (isDefineUndef && ITI->getName()[0] == 'd' && // defined
Chris Lattneraaf09112006-07-03 01:17:59 +0000831 !strcmp(ITI->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +0000832 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +0000833 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner44f8a662006-07-03 01:27:27 +0000834 } else if (isDefineUndef && ITI->getMacroInfo() &&
835 ITI->getMacroInfo()->isBuiltinMacro()) {
836 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
837 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +0000838 } else {
839 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000840 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000841 }
842
Chris Lattner22eb9722006-06-18 05:43:12 +0000843 // Invalid macro name, read and discard the rest of the line. Then set the
844 // token kind to tok::eom.
845 MacroNameTok.SetKind(tok::eom);
846 return DiscardUntilEndOfDirective();
847}
848
849/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
850/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000851void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000852 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000853 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 // There should be no tokens after the directive, but we allow them as an
855 // extension.
856 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000857 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
858 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000859 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000860}
861
862
863
864/// SkipExcludedConditionalBlock - We just read a #if or related directive and
865/// decided that the subsequent tokens are in the #if'd out portion of the
866/// file. Lex the rest of the file, until we see an #endif. If
867/// FoundNonSkipPortion is true, then we have already emitted code for part of
868/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
869/// is true, then #else directives are ok, if not, then we have already seen one
870/// so a #else directive is a duplicate. When this returns, the caller can lex
871/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000872void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000873 bool FoundNonSkipPortion,
874 bool FoundElse) {
875 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +0000876 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +0000877 "Lexing a macro, not a file?");
878
879 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
880 FoundNonSkipPortion, FoundElse);
881
882 // Know that we are going to be skipping tokens. Set this flag to indicate
883 // this, which has a couple of effects:
884 // 1. If EOF of the current lexer is found, the include stack isn't popped.
885 // 2. Identifier information is not looked up for identifier tokens. As an
886 // effect of this, implicit macro expansion is naturally disabled.
887 // 3. "#" tokens at the start of a line are treated as normal tokens, not
888 // implicitly transformed by the lexer.
889 // 4. All notes, warnings, and extension messages are disabled.
890 //
891 SkippingContents = true;
892 LexerToken Tok;
893 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000894 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000895
896 // If this is the end of the buffer, we have an error. The lexer will have
897 // already handled this error condition, so just return and let the caller
898 // lex after this #include.
899 if (Tok.getKind() == tok::eof) break;
900
901 // If this token is not a preprocessor directive, just skip it.
902 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
903 continue;
904
905 // We just parsed a # character at the start of a line, so we're in
906 // directive mode. Tell the lexer this so any newlines we see will be
907 // converted into an EOM token (this terminates the macro).
908 CurLexer->ParsingPreprocessorDirective = true;
909
910 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000911 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000912
913 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
914 // something bogus), skip it.
915 if (Tok.getKind() != tok::identifier) {
916 CurLexer->ParsingPreprocessorDirective = false;
917 continue;
918 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000919
Chris Lattner22eb9722006-06-18 05:43:12 +0000920 // If the first letter isn't i or e, it isn't intesting to us. We know that
921 // this is safe in the face of spelling differences, because there is no way
922 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000923 // allows us to avoid looking up the identifier info for #define/#undef and
924 // other common directives.
925 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
926 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000927 if (FirstChar >= 'a' && FirstChar <= 'z' &&
928 FirstChar != 'i' && FirstChar != 'e') {
929 CurLexer->ParsingPreprocessorDirective = false;
930 continue;
931 }
932
Chris Lattnere60165f2006-06-22 06:36:29 +0000933 // Get the identifier name without trigraphs or embedded newlines. Note
934 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
935 // when skipping.
936 // TODO: could do this with zero copies in the no-clean case by using
937 // strncmp below.
938 char Directive[20];
939 unsigned IdLen;
940 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
941 IdLen = Tok.getLength();
942 memcpy(Directive, RawCharData, IdLen);
943 Directive[IdLen] = 0;
944 } else {
945 std::string DirectiveStr = getSpelling(Tok);
946 IdLen = DirectiveStr.size();
947 if (IdLen >= 20) {
948 CurLexer->ParsingPreprocessorDirective = false;
949 continue;
950 }
951 memcpy(Directive, &DirectiveStr[0], IdLen);
952 Directive[IdLen] = 0;
953 }
954
Chris Lattner22eb9722006-06-18 05:43:12 +0000955 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000956 if ((IdLen == 2) || // "if"
957 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
958 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000959 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
960 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000961 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000962 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000963 /*foundnonskip*/false,
964 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000965 }
966 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000967 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000968 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000969 PPConditionalInfo CondInfo;
970 CondInfo.WasSkipping = true; // Silence bogus warning.
971 bool InCond = CurLexer->popConditionalLevel(CondInfo);
972 assert(!InCond && "Can't be skipping if not in a conditional!");
973
974 // If we popped the outermost skipping block, we're done skipping!
975 if (!CondInfo.WasSkipping)
976 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000977 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000978 // #else directive in a skipping conditional. If not in some other
979 // skipping conditional, and if #else hasn't already been seen, enter it
980 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000981 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
983
984 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000985 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000986
987 // Note that we've seen a #else in this conditional.
988 CondInfo.FoundElse = true;
989
990 // If the conditional is at the top level, and the #if block wasn't
991 // entered, enter the #else block now.
992 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
993 CondInfo.FoundNonSkip = true;
994 break;
995 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000996 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +0000997 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
998
999 bool ShouldEnter;
1000 // If this is in a skipping block or if we're already handled this #if
1001 // block, don't bother parsing the condition.
1002 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001003 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001004 ShouldEnter = false;
1005 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001006 // Restore the value of SkippingContents so that identifiers are
1007 // looked up, etc, inside the #elif expression.
1008 assert(SkippingContents && "We have to be skipping here!");
1009 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001010 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001011 SkippingContents = true;
1012 }
1013
1014 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001015 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001016
1017 // If this condition is true, enter it!
1018 if (ShouldEnter) {
1019 CondInfo.FoundNonSkip = true;
1020 break;
1021 }
1022 }
1023 }
1024
1025 CurLexer->ParsingPreprocessorDirective = false;
1026 }
1027
1028 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1029 // of the file, just stop skipping and return to lexing whatever came after
1030 // the #if block.
1031 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001032}
1033
1034//===----------------------------------------------------------------------===//
1035// Preprocessor Directive Handling.
1036//===----------------------------------------------------------------------===//
1037
1038/// HandleDirective - This callback is invoked when the lexer sees a # token
1039/// at the start of a line. This consumes the directive, modifies the
1040/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1041/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001042void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001043 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001044
1045 // We just parsed a # character at the start of a line, so we're in directive
1046 // mode. Tell the lexer this so any newlines we see will be converted into an
1047 // EOM token (this terminates the macro).
1048 CurLexer->ParsingPreprocessorDirective = true;
1049
1050 ++NumDirectives;
1051
1052 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001053 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001054
1055 switch (Result.getKind()) {
1056 default: break;
1057 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001058 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001059
1060#if 0
1061 case tok::numeric_constant:
1062 // FIXME: implement # 7 line numbers!
1063 break;
1064#endif
1065 case tok::kw_else:
1066 return HandleElseDirective(Result);
1067 case tok::kw_if:
1068 return HandleIfDirective(Result);
1069 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001070 // Get the identifier name without trigraphs or embedded newlines.
1071 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001072 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001073 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001075 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnerb8761832006-06-24 21:31:03 +00001076 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001077 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001079 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1080 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001081 break;
1082 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001083 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001084 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001085 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001087 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001089 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001090 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001091 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001092 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001093 break;
1094 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001095 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001096 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001097 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +00001099 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001101 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001102 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001103 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1104 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001105 break;
1106 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001107 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1108 return HandleIncludeDirective(Result); // Handle #include.
1109 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001110 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001111 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001112 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001113 break;
1114 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001115 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001116 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001117 }
1118 break;
1119 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001120 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1121 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001122 break;
1123 }
1124 break;
1125 }
1126
1127 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001128 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001129
1130 // Read the rest of the PP line.
1131 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001132 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001133 } while (Result.getKind() != tok::eom);
1134
1135 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001136}
1137
Chris Lattner01d66cc2006-07-03 22:16:27 +00001138void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001139 bool isWarning) {
1140 // Read the rest of the line raw. We do this because we don't want macros
1141 // to be expanded and we don't require that the tokens be valid preprocessing
1142 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1143 // collapse multiple consequtive white space between tokens, but this isn't
1144 // specified by the standard.
1145 std::string Message = CurLexer->ReadToEndOfLine();
1146
1147 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001148 return Diag(Tok, DiagID, Message);
1149}
1150
1151/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1152///
1153void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
1154 Diag(Tok, diag::ext_pp_ident_directive);
1155
1156 LexerToken StrTok;
1157 Lex(StrTok);
1158
1159 // If the token kind isn't a string, it's a malformed directive.
1160 if (StrTok.getKind() != tok::string_literal)
1161 return Diag(StrTok, diag::err_pp_malformed_ident);
1162
1163 // Verify that there is nothing after the string, other than EOM.
1164 CheckEndOfDirective("#ident");
1165
1166 if (IdentHandler)
1167 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001168}
1169
Chris Lattnerb8761832006-06-24 21:31:03 +00001170//===----------------------------------------------------------------------===//
1171// Preprocessor Include Directive Handling.
1172//===----------------------------------------------------------------------===//
1173
Chris Lattner22eb9722006-06-18 05:43:12 +00001174/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1175/// file to be included from the lexer, then include it! This is a common
1176/// routine with functionality shared between #include, #include_next and
1177/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001178void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001179 const DirectoryLookup *LookupFrom,
1180 bool isImport) {
1181 ++NumIncluded;
1182 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001183 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001184
1185 // If the token kind is EOM, the error has already been diagnosed.
1186 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001187 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001188
1189 // Verify that there is nothing after the filename, other than EOM. Use the
1190 // preprocessor to lex this in case lexing the filename entered a macro.
1191 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001192
1193 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001194 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001195 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1196
Chris Lattner269c2322006-06-25 06:23:00 +00001197 // Find out whether the filename is <x> or "x".
1198 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001199
1200 // Remove the quotes.
1201 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1202
Chris Lattner22eb9722006-06-18 05:43:12 +00001203 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001204 const DirectoryLookup *CurDir;
1205 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001206 if (File == 0)
1207 return Diag(FilenameTok, diag::err_pp_file_not_found);
1208
1209 // Get information about this file.
1210 PerFileInfo &FileInfo = getFileInfo(File);
1211
1212 // If this is a #import directive, check that we have not already imported
1213 // this header.
1214 if (isImport) {
1215 // If this has already been imported, don't import it again.
1216 FileInfo.isImport = true;
1217
1218 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001219 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 } else {
1221 // Otherwise, if this is a #include of a file that was previously #import'd
1222 // or if this is the second #include of a #pragma once file, ignore it.
1223 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001224 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001225 }
1226
1227 // Look up the file, create a File ID for it.
1228 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +00001229 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 if (FileID == 0)
1231 return Diag(FilenameTok, diag::err_pp_file_not_found);
1232
1233 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001234 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001235
1236 // Increment the number of times this file has been included.
1237 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001238}
1239
1240/// HandleIncludeNextDirective - Implements #include_next.
1241///
Chris Lattnercb283342006-06-18 06:48:37 +00001242void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1243 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001244
1245 // #include_next is like #include, except that we start searching after
1246 // the current found directory. If we can't do this, issue a
1247 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001248 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001249 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001250 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001251 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001252 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001253 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001254 } else {
1255 // Start looking up in the next directory.
1256 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001257 }
1258
1259 return HandleIncludeDirective(IncludeNextTok, Lookup);
1260}
1261
1262/// HandleImportDirective - Implements #import.
1263///
Chris Lattnercb283342006-06-18 06:48:37 +00001264void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1265 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001266
1267 return HandleIncludeDirective(ImportTok, 0, true);
1268}
1269
Chris Lattnerb8761832006-06-24 21:31:03 +00001270//===----------------------------------------------------------------------===//
1271// Preprocessor Macro Directive Handling.
1272//===----------------------------------------------------------------------===//
1273
Chris Lattner22eb9722006-06-18 05:43:12 +00001274/// HandleDefineDirective - Implements #define. This consumes the entire macro
1275/// line then lets the caller lex the next real token.
1276///
Chris Lattnercb283342006-06-18 06:48:37 +00001277void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 ++NumDefined;
1279 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001280 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001281
1282 // Error reading macro name? If so, diagnostic already issued.
1283 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001284 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001285
Chris Lattner50b497e2006-06-18 16:32:35 +00001286 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001287
1288 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001289 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001290
1291 if (Tok.getKind() == tok::eom) {
1292 // If there is no body to this macro, we have no special handling here.
1293 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1294 // This is a function-like macro definition.
1295 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 return DiscardUntilEndOfDirective();
1297
1298 } else if (!Tok.hasLeadingSpace()) {
1299 // C99 requires whitespace between the macro definition and the body. Emit
1300 // a diagnostic for something like "#define X+".
1301 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001302 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001303 } else {
1304 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1305 // one in some cases!
1306 }
1307 } else {
1308 // This is a normal token with leading space. Clear the leading space
1309 // marker on the first token to get proper expansion.
1310 Tok.ClearFlag(LexerToken::LeadingSpace);
1311 }
1312
1313 // Read the rest of the macro body.
1314 while (Tok.getKind() != tok::eom) {
1315 MI->AddTokenToBody(Tok);
1316
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001317 // FIXME: Read macro body. See create_iso_definition.
Chris Lattner22eb9722006-06-18 05:43:12 +00001318
1319 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001320 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001321 }
1322
Chris Lattner13044d92006-07-03 05:16:44 +00001323 // If this is the primary source file, remember that this macro hasn't been
1324 // used yet.
1325 if (isInPrimaryFile())
1326 MI->setIsUsed(false);
1327
Chris Lattner22eb9722006-06-18 05:43:12 +00001328 // Finally, if this identifier already had a macro defined for it, verify that
1329 // the macro bodies are identical and free the old definition.
1330 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001331 if (!OtherMI->isUsed())
1332 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1333
Chris Lattner22eb9722006-06-18 05:43:12 +00001334 // FIXME: Verify the definition is the same.
1335 // Macros must be identical. This means all tokes and whitespace separation
1336 // must be the same.
1337 delete OtherMI;
1338 }
1339
1340 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001341}
1342
1343
1344/// HandleUndefDirective - Implements #undef.
1345///
Chris Lattnercb283342006-06-18 06:48:37 +00001346void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 ++NumUndefined;
1348 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001349 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350
1351 // Error reading macro name? If so, diagnostic already issued.
1352 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001353 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001354
1355 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001356 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001357
1358 // Okay, we finally have a valid identifier to undef.
1359 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1360
1361 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001362 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001363
Chris Lattner13044d92006-07-03 05:16:44 +00001364 if (!MI->isUsed())
1365 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001366
1367 // Free macro definition.
1368 delete MI;
1369 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001370}
1371
1372
Chris Lattnerb8761832006-06-24 21:31:03 +00001373//===----------------------------------------------------------------------===//
1374// Preprocessor Conditional Directive Handling.
1375//===----------------------------------------------------------------------===//
1376
Chris Lattner22eb9722006-06-18 05:43:12 +00001377/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1378/// true when this is a #ifndef directive.
1379///
Chris Lattnercb283342006-06-18 06:48:37 +00001380void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001381 ++NumIf;
1382 LexerToken DirectiveTok = Result;
1383
1384 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001385 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001386
1387 // Error reading macro name? If so, diagnostic already issued.
1388 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001389 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001390
1391 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001392 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001393
Chris Lattnera78a97e2006-07-03 05:42:18 +00001394 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1395
1396 // If there is a macro, mark it used.
1397 if (MI) MI->setIsUsed(true);
1398
Chris Lattner22eb9722006-06-18 05:43:12 +00001399 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001400 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001401 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001402 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001403 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001404 } else {
1405 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001406 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001407 /*Foundnonskip*/false,
1408 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 }
1410}
1411
1412/// HandleIfDirective - Implements the #if directive.
1413///
Chris Lattnercb283342006-06-18 06:48:37 +00001414void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 ++NumIf;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001416 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001417
1418 // Should we include the stuff contained by this directive?
1419 if (ConditionalTrue) {
1420 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001421 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001422 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 } else {
1424 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001425 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001426 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001427 }
1428}
1429
1430/// HandleEndifDirective - Implements the #endif directive.
1431///
Chris Lattnercb283342006-06-18 06:48:37 +00001432void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001433 ++NumEndif;
1434 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001435 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001436
1437 PPConditionalInfo CondInfo;
1438 if (CurLexer->popConditionalLevel(CondInfo)) {
1439 // No conditionals on the stack: this is an #endif without an #if.
1440 return Diag(EndifToken, diag::err_pp_endif_without_if);
1441 }
1442
1443 assert(!CondInfo.WasSkipping && !isSkipping() &&
1444 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001445}
1446
1447
Chris Lattnercb283342006-06-18 06:48:37 +00001448void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001449 ++NumElse;
1450 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001451 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001452
1453 PPConditionalInfo CI;
1454 if (CurLexer->popConditionalLevel(CI))
1455 return Diag(Result, diag::pp_err_else_without_if);
1456
1457 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001458 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001459
1460 // Finally, skip the rest of the contents of this block and return the first
1461 // token after it.
1462 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1463 /*FoundElse*/true);
1464}
1465
Chris Lattnercb283342006-06-18 06:48:37 +00001466void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001467 ++NumElse;
1468 // #elif directive in a non-skipping conditional... start skipping.
1469 // We don't care what the condition is, because we will always skip it (since
1470 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001471 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001472
1473 PPConditionalInfo CI;
1474 if (CurLexer->popConditionalLevel(CI))
1475 return Diag(ElifToken, diag::pp_err_elif_without_if);
1476
1477 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001478 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479
1480 // Finally, skip the rest of the contents of this block and return the first
1481 // token after it.
1482 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1483 /*FoundElse*/CI.FoundElse);
1484}
Chris Lattnerb8761832006-06-24 21:31:03 +00001485