blob: de6c94fa7e3263fa52137904bcfc219806e1e9b8 [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
Chris Lattner371ac8a2006-07-04 07:11:10 +0000742 // See if this file had a controlling macro.
743 if (CurLexer) { // Not ending a macro...
744 if (const IdentifierTokenInfo *ControllingMacro =
745 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
746 ;
747 }
748 }
749
Chris Lattner22eb9722006-06-18 05:43:12 +0000750 // If this is a #include'd file, pop it off the include stack and continue
751 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000752 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000753 // We're done with the #included file.
754 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000755 CurLexer = IncludeMacroStack.back().TheLexer;
756 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
757 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
758 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000759
760 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000761 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000762 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
763
764 // Get the file entry for the current file.
765 if (const FileEntry *FE =
766 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
767 FileType = getFileInfo(FE).DirInfo;
768
Chris Lattner0c885f52006-06-21 06:50:18 +0000769 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000770 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000771 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000772
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 return Lex(Result);
774 }
775
Chris Lattnerd01e2912006-06-18 16:22:51 +0000776 Result.StartToken();
777 CurLexer->BufferPtr = CurLexer->BufferEnd;
778 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000779 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000780
781 // We're done with the #included file.
782 delete CurLexer;
783 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000784
785 // This is the end of the top-level file.
786 IdentifierInfo.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000787}
788
789/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000790/// the current macro line.
791void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000792 assert(CurMacroExpander && !CurLexer &&
793 "Ending a macro when currently in a #include file!");
794
795 // Mark macro not ignored now that it is no longer being expanded.
796 CurMacroExpander->getMacro().EnableMacro();
797 delete CurMacroExpander;
798
Chris Lattner69772b02006-07-02 20:34:39 +0000799 // Handle this like a #include file being popped off the stack.
800 CurMacroExpander = 0;
801 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000802}
803
804
805//===----------------------------------------------------------------------===//
806// Utility Methods for Preprocessor Directive Handling.
807//===----------------------------------------------------------------------===//
808
809/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
810/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000811void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000812 LexerToken Tmp;
813 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000814 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000816}
817
818/// ReadMacroName - Lex and validate a macro name, which occurs after a
819/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattner44f8a662006-07-03 01:27:27 +0000820/// of the macro line if the macro name is invalid. isDefineUndef is true if
821/// this is due to a a #define or #undef directive, false if it is something
822/// else (e.g. #ifdef).
823void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, bool isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000825 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826
827 // Missing macro name?
828 if (MacroNameTok.getKind() == tok::eom)
829 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
830
Chris Lattneraaf09112006-07-03 01:17:59 +0000831 IdentifierTokenInfo *ITI = MacroNameTok.getIdentifierInfo();
832 if (ITI == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000833 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000834 // Fall through on error.
835 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000836 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +0000837
Chris Lattner44f8a662006-07-03 01:27:27 +0000838 } else if (isDefineUndef && ITI->getName()[0] == 'd' && // defined
Chris Lattneraaf09112006-07-03 01:17:59 +0000839 !strcmp(ITI->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +0000840 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +0000841 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner44f8a662006-07-03 01:27:27 +0000842 } else if (isDefineUndef && ITI->getMacroInfo() &&
843 ITI->getMacroInfo()->isBuiltinMacro()) {
844 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
845 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +0000846 } else {
847 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000848 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000849 }
850
Chris Lattner22eb9722006-06-18 05:43:12 +0000851 // Invalid macro name, read and discard the rest of the line. Then set the
852 // token kind to tok::eom.
853 MacroNameTok.SetKind(tok::eom);
854 return DiscardUntilEndOfDirective();
855}
856
857/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
858/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000859void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000861 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000862 // There should be no tokens after the directive, but we allow them as an
863 // extension.
864 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000865 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
866 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000868}
869
870
871
872/// SkipExcludedConditionalBlock - We just read a #if or related directive and
873/// decided that the subsequent tokens are in the #if'd out portion of the
874/// file. Lex the rest of the file, until we see an #endif. If
875/// FoundNonSkipPortion is true, then we have already emitted code for part of
876/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
877/// is true, then #else directives are ok, if not, then we have already seen one
878/// so a #else directive is a duplicate. When this returns, the caller can lex
879/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000880void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000881 bool FoundNonSkipPortion,
882 bool FoundElse) {
883 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +0000884 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +0000885 "Lexing a macro, not a file?");
886
887 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
888 FoundNonSkipPortion, FoundElse);
889
890 // Know that we are going to be skipping tokens. Set this flag to indicate
891 // this, which has a couple of effects:
892 // 1. If EOF of the current lexer is found, the include stack isn't popped.
893 // 2. Identifier information is not looked up for identifier tokens. As an
894 // effect of this, implicit macro expansion is naturally disabled.
895 // 3. "#" tokens at the start of a line are treated as normal tokens, not
896 // implicitly transformed by the lexer.
897 // 4. All notes, warnings, and extension messages are disabled.
898 //
899 SkippingContents = true;
900 LexerToken Tok;
901 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000902 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000903
904 // If this is the end of the buffer, we have an error. The lexer will have
905 // already handled this error condition, so just return and let the caller
906 // lex after this #include.
907 if (Tok.getKind() == tok::eof) break;
908
909 // If this token is not a preprocessor directive, just skip it.
910 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
911 continue;
912
913 // We just parsed a # character at the start of a line, so we're in
914 // directive mode. Tell the lexer this so any newlines we see will be
915 // converted into an EOM token (this terminates the macro).
916 CurLexer->ParsingPreprocessorDirective = true;
917
918 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000919 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000920
921 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
922 // something bogus), skip it.
923 if (Tok.getKind() != tok::identifier) {
924 CurLexer->ParsingPreprocessorDirective = false;
925 continue;
926 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000927
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 // If the first letter isn't i or e, it isn't intesting to us. We know that
929 // this is safe in the face of spelling differences, because there is no way
930 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000931 // allows us to avoid looking up the identifier info for #define/#undef and
932 // other common directives.
933 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
934 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000935 if (FirstChar >= 'a' && FirstChar <= 'z' &&
936 FirstChar != 'i' && FirstChar != 'e') {
937 CurLexer->ParsingPreprocessorDirective = false;
938 continue;
939 }
940
Chris Lattnere60165f2006-06-22 06:36:29 +0000941 // Get the identifier name without trigraphs or embedded newlines. Note
942 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
943 // when skipping.
944 // TODO: could do this with zero copies in the no-clean case by using
945 // strncmp below.
946 char Directive[20];
947 unsigned IdLen;
948 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
949 IdLen = Tok.getLength();
950 memcpy(Directive, RawCharData, IdLen);
951 Directive[IdLen] = 0;
952 } else {
953 std::string DirectiveStr = getSpelling(Tok);
954 IdLen = DirectiveStr.size();
955 if (IdLen >= 20) {
956 CurLexer->ParsingPreprocessorDirective = false;
957 continue;
958 }
959 memcpy(Directive, &DirectiveStr[0], IdLen);
960 Directive[IdLen] = 0;
961 }
962
Chris Lattner22eb9722006-06-18 05:43:12 +0000963 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000964 if ((IdLen == 2) || // "if"
965 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
966 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000967 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
968 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000969 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000970 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000971 /*foundnonskip*/false,
972 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000973 }
974 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000975 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000976 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000977 PPConditionalInfo CondInfo;
978 CondInfo.WasSkipping = true; // Silence bogus warning.
979 bool InCond = CurLexer->popConditionalLevel(CondInfo);
980 assert(!InCond && "Can't be skipping if not in a conditional!");
981
982 // If we popped the outermost skipping block, we're done skipping!
983 if (!CondInfo.WasSkipping)
984 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000985 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000986 // #else directive in a skipping conditional. If not in some other
987 // skipping conditional, and if #else hasn't already been seen, enter it
988 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000989 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
991
992 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000993 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000994
995 // Note that we've seen a #else in this conditional.
996 CondInfo.FoundElse = true;
997
998 // If the conditional is at the top level, and the #if block wasn't
999 // entered, enter the #else block now.
1000 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1001 CondInfo.FoundNonSkip = true;
1002 break;
1003 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001004 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001005 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1006
1007 bool ShouldEnter;
1008 // If this is in a skipping block or if we're already handled this #if
1009 // block, don't bother parsing the condition.
1010 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001011 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001012 ShouldEnter = false;
1013 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001014 // Restore the value of SkippingContents so that identifiers are
1015 // looked up, etc, inside the #elif expression.
1016 assert(SkippingContents && "We have to be skipping here!");
1017 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001018 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001019 SkippingContents = true;
1020 }
1021
1022 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001023 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001024
1025 // If this condition is true, enter it!
1026 if (ShouldEnter) {
1027 CondInfo.FoundNonSkip = true;
1028 break;
1029 }
1030 }
1031 }
1032
1033 CurLexer->ParsingPreprocessorDirective = false;
1034 }
1035
1036 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1037 // of the file, just stop skipping and return to lexing whatever came after
1038 // the #if block.
1039 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001040}
1041
1042//===----------------------------------------------------------------------===//
1043// Preprocessor Directive Handling.
1044//===----------------------------------------------------------------------===//
1045
1046/// HandleDirective - This callback is invoked when the lexer sees a # token
1047/// at the start of a line. This consumes the directive, modifies the
1048/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1049/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001050void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001051 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001052
1053 // We just parsed a # character at the start of a line, so we're in directive
1054 // mode. Tell the lexer this so any newlines we see will be converted into an
1055 // EOM token (this terminates the macro).
1056 CurLexer->ParsingPreprocessorDirective = true;
1057
1058 ++NumDirectives;
1059
Chris Lattner371ac8a2006-07-04 07:11:10 +00001060 // We are about to read a token. For the multiple-include optimization FA to
1061 // work, we have to remember if we had read any tokens *before* this
1062 // pp-directive.
1063 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1064
Chris Lattner22eb9722006-06-18 05:43:12 +00001065 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001066 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001067
1068 switch (Result.getKind()) {
1069 default: break;
1070 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001071 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001072
1073#if 0
1074 case tok::numeric_constant:
Chris Lattner371ac8a2006-07-04 07:11:10 +00001075 MIOpt.ReadDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001076 // FIXME: implement # 7 line numbers!
1077 break;
1078#endif
1079 case tok::kw_else:
1080 return HandleElseDirective(Result);
1081 case tok::kw_if:
1082 return HandleIfDirective(Result);
1083 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001084 // Get the identifier name without trigraphs or embedded newlines.
1085 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001087 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001089 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001090 CurLexer->MIOpt.ReadDirective(); // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001091 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001093 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1094 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001095 break;
1096 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001097 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001099 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001100 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
Chris Lattner40931922006-06-22 06:14:04 +00001101 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001102 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001103 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001105 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001106 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001107 break;
1108 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001109 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001110 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001111 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001112 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
Chris Lattner40931922006-06-22 06:14:04 +00001113 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001114 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001115 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001116 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001117 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1118 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001119 break;
1120 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001121 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1122 return HandleIncludeDirective(Result); // Handle #include.
1123 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001124 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001125 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001126 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001127 break;
1128 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001129 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001130 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001131 }
1132 break;
1133 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001134 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1135 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001136 break;
1137 }
1138 break;
1139 }
1140
1141 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001142 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001143
1144 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001145 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001146
1147 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001148}
1149
Chris Lattner01d66cc2006-07-03 22:16:27 +00001150void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001151 bool isWarning) {
1152 // Read the rest of the line raw. We do this because we don't want macros
1153 // to be expanded and we don't require that the tokens be valid preprocessing
1154 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1155 // collapse multiple consequtive white space between tokens, but this isn't
1156 // specified by the standard.
1157 std::string Message = CurLexer->ReadToEndOfLine();
1158
1159 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001160 return Diag(Tok, DiagID, Message);
1161}
1162
1163/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1164///
1165void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001166 // Inform MIOpt that we found a side-effect of parsing this file.
1167 CurLexer->MIOpt.ReadDirective();
1168
1169 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001170 Diag(Tok, diag::ext_pp_ident_directive);
1171
Chris Lattner371ac8a2006-07-04 07:11:10 +00001172 // Read the string argument.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001173 LexerToken StrTok;
1174 Lex(StrTok);
1175
1176 // If the token kind isn't a string, it's a malformed directive.
1177 if (StrTok.getKind() != tok::string_literal)
1178 return Diag(StrTok, diag::err_pp_malformed_ident);
1179
1180 // Verify that there is nothing after the string, other than EOM.
1181 CheckEndOfDirective("#ident");
1182
1183 if (IdentHandler)
1184 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001185}
1186
Chris Lattnerb8761832006-06-24 21:31:03 +00001187//===----------------------------------------------------------------------===//
1188// Preprocessor Include Directive Handling.
1189//===----------------------------------------------------------------------===//
1190
Chris Lattner22eb9722006-06-18 05:43:12 +00001191/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1192/// file to be included from the lexer, then include it! This is a common
1193/// routine with functionality shared between #include, #include_next and
1194/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001195void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001196 const DirectoryLookup *LookupFrom,
1197 bool isImport) {
1198 ++NumIncluded;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001199
1200 // Inform MIOpt that we found a side-effect of parsing this file.
1201 CurLexer->MIOpt.ReadDirective();
1202
Chris Lattner22eb9722006-06-18 05:43:12 +00001203 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001204 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001205
1206 // If the token kind is EOM, the error has already been diagnosed.
1207 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001208 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001209
1210 // Verify that there is nothing after the filename, other than EOM. Use the
1211 // preprocessor to lex this in case lexing the filename entered a macro.
1212 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001213
1214 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001215 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1217
Chris Lattner269c2322006-06-25 06:23:00 +00001218 // Find out whether the filename is <x> or "x".
1219 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001220
1221 // Remove the quotes.
1222 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1223
Chris Lattner22eb9722006-06-18 05:43:12 +00001224 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001225 const DirectoryLookup *CurDir;
1226 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001227 if (File == 0)
1228 return Diag(FilenameTok, diag::err_pp_file_not_found);
1229
1230 // Get information about this file.
1231 PerFileInfo &FileInfo = getFileInfo(File);
1232
1233 // If this is a #import directive, check that we have not already imported
1234 // this header.
1235 if (isImport) {
1236 // If this has already been imported, don't import it again.
1237 FileInfo.isImport = true;
1238
1239 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001240 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 } else {
1242 // Otherwise, if this is a #include of a file that was previously #import'd
1243 // or if this is the second #include of a #pragma once file, ignore it.
1244 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001245 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 }
1247
1248 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001249 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001250 if (FileID == 0)
1251 return Diag(FilenameTok, diag::err_pp_file_not_found);
1252
1253 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001254 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001255
1256 // Increment the number of times this file has been included.
1257 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001258}
1259
1260/// HandleIncludeNextDirective - Implements #include_next.
1261///
Chris Lattnercb283342006-06-18 06:48:37 +00001262void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1263 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001264
1265 // #include_next is like #include, except that we start searching after
1266 // the current found directory. If we can't do this, issue a
1267 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001268 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001269 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001270 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001271 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001273 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001274 } else {
1275 // Start looking up in the next directory.
1276 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001277 }
1278
1279 return HandleIncludeDirective(IncludeNextTok, Lookup);
1280}
1281
1282/// HandleImportDirective - Implements #import.
1283///
Chris Lattnercb283342006-06-18 06:48:37 +00001284void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1285 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001286
1287 return HandleIncludeDirective(ImportTok, 0, true);
1288}
1289
Chris Lattnerb8761832006-06-24 21:31:03 +00001290//===----------------------------------------------------------------------===//
1291// Preprocessor Macro Directive Handling.
1292//===----------------------------------------------------------------------===//
1293
Chris Lattner22eb9722006-06-18 05:43:12 +00001294/// HandleDefineDirective - Implements #define. This consumes the entire macro
1295/// line then lets the caller lex the next real token.
1296///
Chris Lattnercb283342006-06-18 06:48:37 +00001297void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001298 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001299
1300 // Inform MIOpt that we found a side-effect of parsing this file.
1301 CurLexer->MIOpt.ReadDirective();
1302
Chris Lattner22eb9722006-06-18 05:43:12 +00001303 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001304 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001305
1306 // Error reading macro name? If so, diagnostic already issued.
1307 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001308 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001309
Chris Lattner50b497e2006-06-18 16:32:35 +00001310 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001311
1312 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001313 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001314
1315 if (Tok.getKind() == tok::eom) {
1316 // If there is no body to this macro, we have no special handling here.
1317 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1318 // This is a function-like macro definition.
1319 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001320 return DiscardUntilEndOfDirective();
1321
1322 } else if (!Tok.hasLeadingSpace()) {
1323 // C99 requires whitespace between the macro definition and the body. Emit
1324 // a diagnostic for something like "#define X+".
1325 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001326 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001327 } else {
1328 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1329 // one in some cases!
1330 }
1331 } else {
1332 // This is a normal token with leading space. Clear the leading space
1333 // marker on the first token to get proper expansion.
1334 Tok.ClearFlag(LexerToken::LeadingSpace);
1335 }
1336
1337 // Read the rest of the macro body.
1338 while (Tok.getKind() != tok::eom) {
1339 MI->AddTokenToBody(Tok);
1340
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001341 // FIXME: Read macro body. See create_iso_definition.
Chris Lattner22eb9722006-06-18 05:43:12 +00001342
1343 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001344 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001345 }
1346
Chris Lattner13044d92006-07-03 05:16:44 +00001347 // If this is the primary source file, remember that this macro hasn't been
1348 // used yet.
1349 if (isInPrimaryFile())
1350 MI->setIsUsed(false);
1351
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 // Finally, if this identifier already had a macro defined for it, verify that
1353 // the macro bodies are identical and free the old definition.
1354 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001355 if (!OtherMI->isUsed())
1356 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1357
Chris Lattner22eb9722006-06-18 05:43:12 +00001358 // FIXME: Verify the definition is the same.
1359 // Macros must be identical. This means all tokes and whitespace separation
1360 // must be the same.
1361 delete OtherMI;
1362 }
1363
1364 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365}
1366
1367
1368/// HandleUndefDirective - Implements #undef.
1369///
Chris Lattnercb283342006-06-18 06:48:37 +00001370void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001371 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001372
1373 // Inform MIOpt that we found a side-effect of parsing this file.
1374 CurLexer->MIOpt.ReadDirective();
1375
Chris Lattner22eb9722006-06-18 05:43:12 +00001376 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001377 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001378
1379 // Error reading macro name? If so, diagnostic already issued.
1380 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001381 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001382
1383 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001384 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001385
1386 // Okay, we finally have a valid identifier to undef.
1387 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1388
1389 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001390 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001391
Chris Lattner13044d92006-07-03 05:16:44 +00001392 if (!MI->isUsed())
1393 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001394
1395 // Free macro definition.
1396 delete MI;
1397 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001398}
1399
1400
Chris Lattnerb8761832006-06-24 21:31:03 +00001401//===----------------------------------------------------------------------===//
1402// Preprocessor Conditional Directive Handling.
1403//===----------------------------------------------------------------------===//
1404
Chris Lattner22eb9722006-06-18 05:43:12 +00001405/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00001406/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1407/// if any tokens have been returned or pp-directives activated before this
1408/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00001409///
Chris Lattner371ac8a2006-07-04 07:11:10 +00001410void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1411 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 ++NumIf;
1413 LexerToken DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001414
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001416 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001417
1418 // Error reading macro name? If so, diagnostic already issued.
1419 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001420 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001421
1422 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001423 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1424
1425 // If the start of a top-level #ifdef, inform MIOpt.
1426 if (!ReadAnyTokensBeforeDirective &&
1427 CurLexer->getConditionalStackDepth() == 0) {
1428 assert(isIfndef && "#ifdef shouldn't reach here");
1429 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1430 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001431
Chris Lattnera78a97e2006-07-03 05:42:18 +00001432 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1433
1434 // If there is a macro, mark it used.
1435 if (MI) MI->setIsUsed(true);
1436
Chris Lattner22eb9722006-06-18 05:43:12 +00001437 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001438 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001439 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001440 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001442 } else {
1443 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001444 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001445 /*Foundnonskip*/false,
1446 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001447 }
1448}
1449
1450/// HandleIfDirective - Implements the #if directive.
1451///
Chris Lattnercb283342006-06-18 06:48:37 +00001452void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001454
1455 // FIXME: Detect "#if !defined(X)" for the MIOpt.
1456 CurLexer->MIOpt.ReadDirective();
1457
1458 // Parse and evaluation the conditional expression.
Chris Lattner7966aaf2006-06-18 06:50:36 +00001459 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001460
1461 // Should we include the stuff contained by this directive?
1462 if (ConditionalTrue) {
1463 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001464 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001465 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001466 } else {
1467 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001468 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001469 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 }
1471}
1472
1473/// HandleEndifDirective - Implements the #endif directive.
1474///
Chris Lattnercb283342006-06-18 06:48:37 +00001475void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001476 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001477
Chris Lattner22eb9722006-06-18 05:43:12 +00001478 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001479 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001480
1481 PPConditionalInfo CondInfo;
1482 if (CurLexer->popConditionalLevel(CondInfo)) {
1483 // No conditionals on the stack: this is an #endif without an #if.
1484 return Diag(EndifToken, diag::err_pp_endif_without_if);
1485 }
1486
Chris Lattner371ac8a2006-07-04 07:11:10 +00001487 // If this the end of a top-level #endif, inform MIOpt.
1488 if (CurLexer->getConditionalStackDepth() == 0)
1489 CurLexer->MIOpt.ExitTopLevelConditional();
1490
Chris Lattner22eb9722006-06-18 05:43:12 +00001491 assert(!CondInfo.WasSkipping && !isSkipping() &&
1492 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001493}
1494
1495
Chris Lattnercb283342006-06-18 06:48:37 +00001496void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001498
Chris Lattner22eb9722006-06-18 05:43:12 +00001499 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001500 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001501
1502 PPConditionalInfo CI;
1503 if (CurLexer->popConditionalLevel(CI))
1504 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001505
1506 // If this is a top-level #else, inform the MIOpt.
1507 if (CurLexer->getConditionalStackDepth() == 0)
1508 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00001509
1510 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001511 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001512
1513 // Finally, skip the rest of the contents of this block and return the first
1514 // token after it.
1515 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1516 /*FoundElse*/true);
1517}
1518
Chris Lattnercb283342006-06-18 06:48:37 +00001519void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001520 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001521
Chris Lattner22eb9722006-06-18 05:43:12 +00001522 // #elif directive in a non-skipping conditional... start skipping.
1523 // We don't care what the condition is, because we will always skip it (since
1524 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001525 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001526
1527 PPConditionalInfo CI;
1528 if (CurLexer->popConditionalLevel(CI))
1529 return Diag(ElifToken, diag::pp_err_elif_without_if);
1530
Chris Lattner371ac8a2006-07-04 07:11:10 +00001531 // If this is a top-level #elif, inform the MIOpt.
1532 if (CurLexer->getConditionalStackDepth() == 0)
1533 CurLexer->MIOpt.FoundTopLevelElse();
1534
Chris Lattner22eb9722006-06-18 05:43:12 +00001535 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001536 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001537
1538 // Finally, skip the rest of the contents of this block and return the first
1539 // token after it.
1540 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1541 /*FoundElse*/CI.FoundElse);
1542}
Chris Lattnerb8761832006-06-24 21:31:03 +00001543