blob: 3641ae6e38eda0dc74cc0be14a43f354f993fe2a [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 Lattnerb8761832006-06-24 21:31:03 +000068
69 // Initialize the pragma handlers.
70 PragmaHandlers = new PragmaNamespace(0);
71 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000072
73 // Initialize builtin macros like __LINE__ and friends.
74 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000075}
76
77Preprocessor::~Preprocessor() {
78 // Free any active lexers.
79 delete CurLexer;
80
Chris Lattner69772b02006-07-02 20:34:39 +000081 while (!IncludeMacroStack.empty()) {
82 delete IncludeMacroStack.back().TheLexer;
83 delete IncludeMacroStack.back().TheMacroExpander;
84 IncludeMacroStack.pop_back();
Chris Lattner22eb9722006-06-18 05:43:12 +000085 }
Chris Lattnerb8761832006-06-24 21:31:03 +000086
87 // Release pragma information.
88 delete PragmaHandlers;
Chris Lattner0b8cfc22006-06-28 06:49:17 +000089
90 // Delete the scratch buffer info.
91 delete ScratchBuf;
Chris Lattner22eb9722006-06-18 05:43:12 +000092}
93
94/// getFileInfo - Return the PerFileInfo structure for the specified
95/// FileEntry.
96Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
97 if (FE->getUID() >= FileInfo.size())
98 FileInfo.resize(FE->getUID()+1);
99 return FileInfo[FE->getUID()];
100}
101
102
103/// AddKeywords - Add all keywords to the symbol table.
104///
105void Preprocessor::AddKeywords() {
106 enum {
107 C90Shift = 0,
108 EXTC90 = 1 << C90Shift,
109 NOTC90 = 2 << C90Shift,
110 C99Shift = 2,
111 EXTC99 = 1 << C99Shift,
112 NOTC99 = 2 << C99Shift,
113 CPPShift = 4,
114 EXTCPP = 1 << CPPShift,
115 NOTCPP = 2 << CPPShift,
116 Mask = 3
117 };
118
119 // Add keywords and tokens for the current language.
120#define KEYWORD(NAME, FLAGS) \
121 AddKeyword(#NAME+1, tok::kw##NAME, \
122 (FLAGS >> C90Shift) & Mask, \
123 (FLAGS >> C99Shift) & Mask, \
124 (FLAGS >> CPPShift) & Mask);
125#define ALIAS(NAME, TOK) \
126 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
127#include "clang/Basic/TokenKinds.def"
128}
129
130/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
131/// the specified LexerToken's location, translating the token's start
132/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000133void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000134 const std::string &Msg) {
135 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
136 // warnings or extensions.
137 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000138 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000139
Chris Lattnercb283342006-06-18 06:48:37 +0000140 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000141}
Chris Lattnerd01e2912006-06-18 16:22:51 +0000142
143void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
144 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
145 << getSpelling(Tok) << "'";
146
147 if (!DumpFlags) return;
148 std::cerr << "\t";
149 if (Tok.isAtStartOfLine())
150 std::cerr << " [StartOfLine]";
151 if (Tok.hasLeadingSpace())
152 std::cerr << " [LeadingSpace]";
153 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000154 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000155 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
156 << "']";
157 }
158}
159
160void Preprocessor::DumpMacro(const MacroInfo &MI) const {
161 std::cerr << "MACRO: ";
162 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
163 DumpToken(MI.getReplacementToken(i));
164 std::cerr << " ";
165 }
166 std::cerr << "\n";
167}
168
Chris Lattner22eb9722006-06-18 05:43:12 +0000169void Preprocessor::PrintStats() {
170 std::cerr << "\n*** Preprocessor Stats:\n";
171 std::cerr << FileInfo.size() << " files tracked.\n";
172 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
173 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
174 NumOnceOnlyFiles += FileInfo[i].isImport;
175 if (MaxNumIncludes < FileInfo[i].NumIncludes)
176 MaxNumIncludes = FileInfo[i].NumIncludes;
177 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
178 }
179 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
180 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
181 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
182
183 std::cerr << NumDirectives << " directives found:\n";
184 std::cerr << " " << NumDefined << " #define.\n";
185 std::cerr << " " << NumUndefined << " #undef.\n";
186 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
187 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
188 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
189 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
190 std::cerr << " " << NumElse << " #else/#elif.\n";
191 std::cerr << " " << NumEndif << " #endif.\n";
192 std::cerr << " " << NumPragma << " #pragma.\n";
193 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
194
195 std::cerr << NumMacroExpanded << " macros expanded, "
196 << NumFastMacroExpanded << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000197}
198
199//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000200// Token Spelling
201//===----------------------------------------------------------------------===//
202
203
204/// getSpelling() - Return the 'spelling' of this token. The spelling of a
205/// token are the characters used to represent the token in the source file
206/// after trigraph expansion and escaped-newline folding. In particular, this
207/// wants to get the true, uncanonicalized, spelling of things like digraphs
208/// UCNs, etc.
209std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
210 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
211
212 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000213 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000214 assert(TokStart && "Token has invalid location!");
215 if (!Tok.needsCleaning())
216 return std::string(TokStart, TokStart+Tok.getLength());
217
218 // Otherwise, hard case, relex the characters into the string.
219 std::string Result;
220 Result.reserve(Tok.getLength());
221
222 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
223 Ptr != End; ) {
224 unsigned CharSize;
225 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
226 Ptr += CharSize;
227 }
228 assert(Result.size() != unsigned(Tok.getLength()) &&
229 "NeedsCleaning flag set on something that didn't need cleaning!");
230 return Result;
231}
232
233/// getSpelling - This method is used to get the spelling of a token into a
234/// preallocated buffer, instead of as an std::string. The caller is required
235/// to allocate enough space for the token, which is guaranteed to be at least
236/// Tok.getLength() bytes long. The actual length of the token is returned.
237unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
238 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
239
Chris Lattner50b497e2006-06-18 16:32:35 +0000240 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000241 assert(TokStart && "Token has invalid location!");
242
243 // If this token contains nothing interesting, return it directly.
244 if (!Tok.needsCleaning()) {
245 unsigned Size = Tok.getLength();
246 memcpy(Buffer, TokStart, Size);
247 return Size;
248 }
249 // Otherwise, hard case, relex the characters into the string.
250 std::string Result;
251 Result.reserve(Tok.getLength());
252
253 char *OutBuf = Buffer;
254 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
255 Ptr != End; ) {
256 unsigned CharSize;
257 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
258 Ptr += CharSize;
259 }
260 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
261 "NeedsCleaning flag set on something that didn't need cleaning!");
262
263 return OutBuf-Buffer;
264}
265
266//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000267// Source File Location Methods.
268//===----------------------------------------------------------------------===//
269
270
271/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
272/// return null on failure. isAngled indicates whether the file reference is
273/// for system #include's or not (i.e. using <> instead of "").
274const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000275 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000276 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000277 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000278 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000279 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000280
281 // If 'Filename' is absolute, check to see if it exists and no searching.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000282 // FIXME: Portability. This should be a sys::Path interface, this doesn't
283 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner22eb9722006-06-18 05:43:12 +0000284 if (Filename[0] == '/') {
285 // If this was an #include_next "/absolute/file", fail.
286 if (FromDir) return 0;
287
288 // Otherwise, just return the file.
289 return FileMgr.getFile(Filename);
290 }
291
292 // Step #0, unless disabled, check to see if the file is in the #includer's
293 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000294 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000295 unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
296 const FileEntry *CurFE = SourceMgr.getFileEntryForFileID(TheFileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000297 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000298 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000299 // FIXME: Portability. Should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 if (const FileEntry *FE =
301 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000302 if (CurDirLookup)
303 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000304 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000305 CurDir = 0;
306
307 // This file is a system header or C++ unfriendly if the old file is.
308 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000309 return FE;
310 }
311 }
312 }
313
314 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000315 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000316
317 // If this is a #include_next request, start searching after the directory the
318 // file was found in.
319 if (FromDir)
320 i = FromDir-&SearchDirs[0];
321
322 // Check each directory in sequence to see if it contains this file.
323 for (; i != SearchDirs.size(); ++i) {
324 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000325 // FIXME: Portability. Adding file to dir should be in sys::Path.
326 std::string SearchDir = SearchDirs[i].getDir()->getName()+"/"+Filename;
327 if (const FileEntry *FE = FileMgr.getFile(SearchDir)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000328 CurDir = &SearchDirs[i];
329
330 // This file is a system header or C++ unfriendly if the dir is.
331 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000332 return FE;
333 }
334 }
335
336 // Otherwise, didn't find it.
337 return 0;
338}
339
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000340/// isInPrimaryFile - Return true if we're in the top-level file, not in a
341/// #include.
342bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000343 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner13044d92006-07-03 05:16:44 +0000344 return CurLexer->isMainFile();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000345
Chris Lattner13044d92006-07-03 05:16:44 +0000346 // If there are any stacked lexers, we're in a #include.
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000347 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000348 if (IncludeMacroStack[i].TheLexer &&
349 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
350 return IncludeMacroStack[i].TheLexer->isMainFile();
351 return false;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000352}
353
354/// getCurrentLexer - Return the current file lexer being lexed from. Note
355/// that this ignores any potentially active macro expansions and _Pragma
356/// expansions going on at the time.
357Lexer *Preprocessor::getCurrentFileLexer() const {
358 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
359
360 // Look for a stacked lexer.
361 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000362 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000363 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
364 return L;
365 }
366 return 0;
367}
368
369
Chris Lattner22eb9722006-06-18 05:43:12 +0000370/// EnterSourceFile - Add a source file to the top of the include stack and
371/// start lexing tokens from it instead of the current buffer. Return true
372/// on failure.
373void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner13044d92006-07-03 05:16:44 +0000374 const DirectoryLookup *CurDir,
375 bool isMainFile) {
Chris Lattner69772b02006-07-02 20:34:39 +0000376 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000377 ++NumEnteredSourceFiles;
378
Chris Lattner69772b02006-07-02 20:34:39 +0000379 if (MaxIncludeStackDepth < IncludeMacroStack.size())
380 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000381
Chris Lattner22eb9722006-06-18 05:43:12 +0000382 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
Chris Lattner69772b02006-07-02 20:34:39 +0000383 Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
Chris Lattner13044d92006-07-03 05:16:44 +0000384 if (isMainFile) TheLexer->setIsMainFile();
Chris Lattner69772b02006-07-02 20:34:39 +0000385 EnterSourceFileWithLexer(TheLexer, CurDir);
386}
Chris Lattner22eb9722006-06-18 05:43:12 +0000387
Chris Lattner69772b02006-07-02 20:34:39 +0000388/// EnterSourceFile - Add a source file to the top of the include stack and
389/// start lexing tokens from it instead of the current buffer.
390void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
391 const DirectoryLookup *CurDir) {
392
393 // Add the current lexer to the include stack.
394 if (CurLexer || CurMacroExpander)
395 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
396 CurMacroExpander));
397
398 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000399 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000400 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000401
402 // Notify the client, if desired, that we are in a new source file.
Chris Lattner98a53122006-07-02 23:00:20 +0000403 if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000404 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
405
406 // Get the file entry for the current file.
407 if (const FileEntry *FE =
408 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
409 FileType = getFileInfo(FE).DirInfo;
410
Chris Lattner1840e492006-07-02 22:30:01 +0000411 FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
Chris Lattner55a60952006-06-25 04:20:34 +0000412 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000413 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000414}
415
Chris Lattner69772b02006-07-02 20:34:39 +0000416
417
Chris Lattner22eb9722006-06-18 05:43:12 +0000418/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000419/// tokens from it instead of the current buffer.
420void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000421 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
422 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner69772b02006-07-02 20:34:39 +0000423 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
424 CurMacroExpander));
425 CurLexer = 0;
426 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000427
428 // TODO: Figure out arguments.
429
430 // Mark the macro as currently disabled, so that it is not recursively
431 // expanded.
432 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000433 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000434}
435
Chris Lattner22eb9722006-06-18 05:43:12 +0000436//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000437// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000438//===----------------------------------------------------------------------===//
439
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000440/// RegisterBuiltinMacro - Register the specified identifier in the identifier
441/// table and mark it as a builtin macro to be expanded.
442IdentifierTokenInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
443 // Get the identifier.
444 IdentifierTokenInfo *Id = getIdentifierInfo(Name);
445
446 // Mark it as being a macro that is builtin.
447 MacroInfo *MI = new MacroInfo(SourceLocation());
448 MI->setIsBuiltinMacro();
449 Id->setMacroInfo(MI);
450 return Id;
451}
452
453
Chris Lattner677757a2006-06-28 05:26:32 +0000454/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
455/// identifier table.
456void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000457 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000458 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000459 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
460 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000461 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000462
463 // GCC Extensions.
464 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
465 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000466 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000467
Chris Lattner69772b02006-07-02 20:34:39 +0000468 // FIXME: implement them all:
Chris Lattnerc1283b92006-07-01 23:16:30 +0000469//Pseudo #defines.
470 // __STDC__ 1 if !stdc_0_in_system_headers and "std"
471 // __STDC_VERSION__
472 // __STDC_HOSTED__
473 // __OBJC__
Chris Lattner22eb9722006-06-18 05:43:12 +0000474}
475
Chris Lattner677757a2006-06-28 05:26:32 +0000476
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000477/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
478/// expanded as a macro, handle it and return the next token as 'Identifier'.
479void Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
480 MacroInfo *MI) {
481 ++NumMacroExpanded;
Chris Lattner13044d92006-07-03 05:16:44 +0000482
483 // Notice that this macro has been used.
484 MI->setIsUsed(true);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000485
486 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
487 if (MI->isBuiltinMacro())
Chris Lattner69772b02006-07-02 20:34:39 +0000488 return ExpandBuiltinMacro(Identifier);
489
490 // If we started lexing a macro, enter the macro expansion body.
491 // FIXME: Read/Validate the argument list here!
492
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000493
494 // If this macro expands to no tokens, don't bother to push it onto the
495 // expansion stack, only to take it right back off.
496 if (MI->getNumTokens() == 0) {
497 // Ignore this macro use, just return the next token in the current
498 // buffer.
499 bool HadLeadingSpace = Identifier.hasLeadingSpace();
500 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
501
502 Lex(Identifier);
503
504 // If the identifier isn't on some OTHER line, inherit the leading
505 // whitespace/first-on-a-line property of this token. This handles
506 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
507 // empty.
508 if (!Identifier.isAtStartOfLine()) {
509 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
510 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
511 }
512 ++NumFastMacroExpanded;
513 return;
514
515 } else if (MI->getNumTokens() == 1 &&
516 // Don't handle identifiers if they need recursive expansion.
517 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
518 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
519 // FIXME: Function-style macros only if no arguments?
520
521 // Otherwise, if this macro expands into a single trivially-expanded
522 // token: expand it now. This handles common cases like
523 // "#define VAL 42".
524
525 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
526 // identifier to the expanded token.
527 bool isAtStartOfLine = Identifier.isAtStartOfLine();
528 bool hasLeadingSpace = Identifier.hasLeadingSpace();
529
530 // Remember where the token is instantiated.
531 SourceLocation InstantiateLoc = Identifier.getLocation();
532
533 // Replace the result token.
534 Identifier = MI->getReplacementToken(0);
535
536 // Restore the StartOfLine/LeadingSpace markers.
537 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
538 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
539
540 // Update the tokens location to include both its logical and physical
541 // locations.
542 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000543 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000544 Identifier.SetLocation(Loc);
545
546 // Since this is not an identifier token, it can't be macro expanded, so
547 // we're done.
548 ++NumFastMacroExpanded;
549 return;
550 }
551
552 // Start expanding the macro (FIXME, pass arguments).
553 EnterMacro(Identifier);
554
555 // Now that the macro is at the top of the include stack, ask the
556 // preprocessor to read the next token from it.
557 return Lex(Identifier);
558}
559
Chris Lattnerc673f902006-06-30 06:10:41 +0000560/// ComputeDATE_TIME - Compute the current time, enter it into the specified
561/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
562/// the identifier tokens inserted.
563static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
564 ScratchBuffer *ScratchBuf) {
565 time_t TT = time(0);
566 struct tm *TM = localtime(&TT);
567
568 static const char * const Months[] = {
569 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
570 };
571
572 char TmpBuffer[100];
573 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
574 TM->tm_year+1900);
575 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
576
577 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
578 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
579}
580
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000581/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
582/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000583void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000584 // Figure out which token this is.
585 IdentifierTokenInfo *ITI = Tok.getIdentifierInfo();
586 assert(ITI && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000587
588 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
589 // lex the token after it.
590 if (ITI == Ident_Pragma)
591 return Handle_Pragma(Tok);
592
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000593 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000594
595 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000596 Tok.SetIdentifierInfo(0);
597 Tok.ClearFlag(LexerToken::NeedsCleaning);
598
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000599 if (ITI == Ident__LINE__) {
600 // __LINE__ expands to a simple numeric value.
601 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
602 unsigned Length = strlen(TmpBuffer);
603 Tok.SetKind(tok::numeric_constant);
604 Tok.SetLength(Length);
605 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000606 } else if (ITI == Ident__FILE__ || ITI == Ident__BASE_FILE__) {
607 SourceLocation Loc = Tok.getLocation();
608 if (ITI == Ident__BASE_FILE__) {
609 Diag(Tok, diag::ext_pp_base_file);
610 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
611 while (NextLoc.getFileID() != 0) {
612 Loc = NextLoc;
613 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
614 }
615 }
616
Chris Lattner0766e592006-07-03 01:07:01 +0000617 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
618 std::string FN = SourceMgr.getSourceName(Loc);
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000619 FN = Lexer::Stringify(FN);
Chris Lattner630b33c2006-07-01 22:46:53 +0000620 Tok.SetKind(tok::string_literal);
621 Tok.SetLength(FN.size());
622 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000623 } else if (ITI == Ident__DATE__) {
624 if (!DATELoc.isValid())
625 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
626 Tok.SetKind(tok::string_literal);
627 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
628 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc673f902006-06-30 06:10:41 +0000629 } else if (ITI == Ident__TIME__) {
630 if (!TIMELoc.isValid())
631 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
632 Tok.SetKind(tok::string_literal);
633 Tok.SetLength(strlen("\"hh:mm:ss\""));
634 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc1283b92006-07-01 23:16:30 +0000635 } else if (ITI == Ident__INCLUDE_LEVEL__) {
636 Diag(Tok, diag::ext_pp_include_level);
637
638 // Compute the include depth of this token.
639 unsigned Depth = 0;
640 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
641 for (; Loc.getFileID() != 0; ++Depth)
642 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
643
644 // __INCLUDE_LEVEL__ expands to a simple numeric value.
645 sprintf(TmpBuffer, "%u", Depth);
646 unsigned Length = strlen(TmpBuffer);
647 Tok.SetKind(tok::numeric_constant);
648 Tok.SetLength(Length);
649 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattner847e0e42006-07-01 23:49:16 +0000650 } else if (ITI == Ident__TIMESTAMP__) {
651 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
652 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
653 Diag(Tok, diag::ext_pp_timestamp);
654
655 // Get the file that we are lexing out of. If we're currently lexing from
656 // a macro, dig into the include stack.
657 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000658 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000659
660 if (TheLexer)
661 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
662
663 // If this file is older than the file it depends on, emit a diagnostic.
664 const char *Result;
665 if (CurFile) {
666 time_t TT = CurFile->getModificationTime();
667 struct tm *TM = localtime(&TT);
668 Result = asctime(TM);
669 } else {
670 Result = "??? ??? ?? ??:??:?? ????\n";
671 }
672 TmpBuffer[0] = '"';
673 strcpy(TmpBuffer+1, Result);
674 unsigned Len = strlen(TmpBuffer);
675 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
676 Tok.SetKind(tok::string_literal);
677 Tok.SetLength(Len);
678 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000679 } else {
680 assert(0 && "Unknown identifier!");
681 }
682}
Chris Lattner677757a2006-06-28 05:26:32 +0000683
Chris Lattner13044d92006-07-03 05:16:44 +0000684namespace {
685struct UnusedIdentifierReporter : public IdentifierVisitor {
686 Preprocessor &PP;
687 UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
688
689 void VisitIdentifier(IdentifierTokenInfo &ITI) const {
690 if (ITI.getMacroInfo() && !ITI.getMacroInfo()->isUsed())
691 PP.Diag(ITI.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
692 }
693};
694}
695
Chris Lattner677757a2006-06-28 05:26:32 +0000696//===----------------------------------------------------------------------===//
697// Lexer Event Handling.
698//===----------------------------------------------------------------------===//
699
700/// HandleIdentifier - This callback is invoked when the lexer reads an
701/// identifier. This callback looks up the identifier in the map and/or
702/// potentially macro expands it or turns it into a named token (like 'for').
703void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
704 if (Identifier.getIdentifierInfo() == 0) {
705 // If we are skipping tokens (because we are in a #if 0 block), there will
706 // be no identifier info, just return the token.
707 assert(isSkipping() && "Token isn't an identifier?");
708 return;
709 }
710 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
711
712 // If this identifier was poisoned, and if it was not produced from a macro
713 // expansion, emit an error.
714 if (ITI.isPoisoned() && CurLexer)
715 Diag(Identifier, diag::err_pp_used_poisoned_id);
716
717 if (MacroInfo *MI = ITI.getMacroInfo())
718 if (MI->isEnabled() && !DisableMacroExpansion)
719 return HandleMacroExpandedIdentifier(Identifier, MI);
720
721 // Change the kind of this identifier to the appropriate token kind, e.g.
722 // turning "for" into a keyword.
723 Identifier.SetKind(ITI.getTokenID());
724
725 // If this is an extension token, diagnose its use.
726 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
727}
728
Chris Lattner22eb9722006-06-18 05:43:12 +0000729/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
730/// the current file. This either returns the EOF token or pops a level off
731/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000732void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000733 assert(!CurMacroExpander &&
734 "Ending a file when currently in a macro!");
735
736 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
737 // this is an error condition. Just return the EOF token up to
738 // SkipExcludedConditionalBlock. The Lexer will have already have issued
739 // errors for the unterminated #if's on the conditional stack.
740 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000741 Result.StartToken();
742 CurLexer->BufferPtr = CurLexer->BufferEnd;
743 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000744 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000745 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000746 }
747
748 // If this is a #include'd file, pop it off the include stack and continue
749 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000750 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000751 // We're done with the #included file.
752 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000753 CurLexer = IncludeMacroStack.back().TheLexer;
754 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
755 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
756 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000757
758 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000759 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000760 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
761
762 // Get the file entry for the current file.
763 if (const FileEntry *FE =
764 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
765 FileType = getFileInfo(FE).DirInfo;
766
Chris Lattner0c885f52006-06-21 06:50:18 +0000767 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000768 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000769 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000770
Chris Lattner22eb9722006-06-18 05:43:12 +0000771 return Lex(Result);
772 }
773
Chris Lattnerd01e2912006-06-18 16:22:51 +0000774 Result.StartToken();
775 CurLexer->BufferPtr = CurLexer->BufferEnd;
776 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000777 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000778
779 // We're done with the #included file.
780 delete CurLexer;
781 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000782
783 // This is the end of the top-level file.
784 IdentifierInfo.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000785}
786
787/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000788/// the current macro line.
789void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000790 assert(CurMacroExpander && !CurLexer &&
791 "Ending a macro when currently in a #include file!");
792
793 // Mark macro not ignored now that it is no longer being expanded.
794 CurMacroExpander->getMacro().EnableMacro();
795 delete CurMacroExpander;
796
Chris Lattner69772b02006-07-02 20:34:39 +0000797 // Handle this like a #include file being popped off the stack.
798 CurMacroExpander = 0;
799 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000800}
801
802
803//===----------------------------------------------------------------------===//
804// Utility Methods for Preprocessor Directive Handling.
805//===----------------------------------------------------------------------===//
806
807/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
808/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000809void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000810 LexerToken Tmp;
811 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000812 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000814}
815
816/// ReadMacroName - Lex and validate a macro name, which occurs after a
817/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattner44f8a662006-07-03 01:27:27 +0000818/// of the macro line if the macro name is invalid. isDefineUndef is true if
819/// this is due to a a #define or #undef directive, false if it is something
820/// else (e.g. #ifdef).
821void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, bool isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000822 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000823 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000824
825 // Missing macro name?
826 if (MacroNameTok.getKind() == tok::eom)
827 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
828
Chris Lattneraaf09112006-07-03 01:17:59 +0000829 IdentifierTokenInfo *ITI = MacroNameTok.getIdentifierInfo();
830 if (ITI == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000831 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000832 // Fall through on error.
833 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000834 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +0000835
Chris Lattner44f8a662006-07-03 01:27:27 +0000836 } else if (isDefineUndef && ITI->getName()[0] == 'd' && // defined
Chris Lattneraaf09112006-07-03 01:17:59 +0000837 !strcmp(ITI->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +0000838 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +0000839 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattner44f8a662006-07-03 01:27:27 +0000840 } else if (isDefineUndef && ITI->getMacroInfo() &&
841 ITI->getMacroInfo()->isBuiltinMacro()) {
842 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
843 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +0000844 } else {
845 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000846 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000847 }
848
Chris Lattner22eb9722006-06-18 05:43:12 +0000849 // Invalid macro name, read and discard the rest of the line. Then set the
850 // token kind to tok::eom.
851 MacroNameTok.SetKind(tok::eom);
852 return DiscardUntilEndOfDirective();
853}
854
855/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
856/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000857void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000858 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000859 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 // There should be no tokens after the directive, but we allow them as an
861 // extension.
862 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000863 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
864 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000865 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000866}
867
868
869
870/// SkipExcludedConditionalBlock - We just read a #if or related directive and
871/// decided that the subsequent tokens are in the #if'd out portion of the
872/// file. Lex the rest of the file, until we see an #endif. If
873/// FoundNonSkipPortion is true, then we have already emitted code for part of
874/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
875/// is true, then #else directives are ok, if not, then we have already seen one
876/// so a #else directive is a duplicate. When this returns, the caller can lex
877/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000878void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 bool FoundNonSkipPortion,
880 bool FoundElse) {
881 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +0000882 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +0000883 "Lexing a macro, not a file?");
884
885 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
886 FoundNonSkipPortion, FoundElse);
887
888 // Know that we are going to be skipping tokens. Set this flag to indicate
889 // this, which has a couple of effects:
890 // 1. If EOF of the current lexer is found, the include stack isn't popped.
891 // 2. Identifier information is not looked up for identifier tokens. As an
892 // effect of this, implicit macro expansion is naturally disabled.
893 // 3. "#" tokens at the start of a line are treated as normal tokens, not
894 // implicitly transformed by the lexer.
895 // 4. All notes, warnings, and extension messages are disabled.
896 //
897 SkippingContents = true;
898 LexerToken Tok;
899 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000900 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000901
902 // If this is the end of the buffer, we have an error. The lexer will have
903 // already handled this error condition, so just return and let the caller
904 // lex after this #include.
905 if (Tok.getKind() == tok::eof) break;
906
907 // If this token is not a preprocessor directive, just skip it.
908 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
909 continue;
910
911 // We just parsed a # character at the start of a line, so we're in
912 // directive mode. Tell the lexer this so any newlines we see will be
913 // converted into an EOM token (this terminates the macro).
914 CurLexer->ParsingPreprocessorDirective = true;
915
916 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000917 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000918
919 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
920 // something bogus), skip it.
921 if (Tok.getKind() != tok::identifier) {
922 CurLexer->ParsingPreprocessorDirective = false;
923 continue;
924 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000925
Chris Lattner22eb9722006-06-18 05:43:12 +0000926 // If the first letter isn't i or e, it isn't intesting to us. We know that
927 // this is safe in the face of spelling differences, because there is no way
928 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000929 // allows us to avoid looking up the identifier info for #define/#undef and
930 // other common directives.
931 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
932 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 if (FirstChar >= 'a' && FirstChar <= 'z' &&
934 FirstChar != 'i' && FirstChar != 'e') {
935 CurLexer->ParsingPreprocessorDirective = false;
936 continue;
937 }
938
Chris Lattnere60165f2006-06-22 06:36:29 +0000939 // Get the identifier name without trigraphs or embedded newlines. Note
940 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
941 // when skipping.
942 // TODO: could do this with zero copies in the no-clean case by using
943 // strncmp below.
944 char Directive[20];
945 unsigned IdLen;
946 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
947 IdLen = Tok.getLength();
948 memcpy(Directive, RawCharData, IdLen);
949 Directive[IdLen] = 0;
950 } else {
951 std::string DirectiveStr = getSpelling(Tok);
952 IdLen = DirectiveStr.size();
953 if (IdLen >= 20) {
954 CurLexer->ParsingPreprocessorDirective = false;
955 continue;
956 }
957 memcpy(Directive, &DirectiveStr[0], IdLen);
958 Directive[IdLen] = 0;
959 }
960
Chris Lattner22eb9722006-06-18 05:43:12 +0000961 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000962 if ((IdLen == 2) || // "if"
963 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
964 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000965 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
966 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000967 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000968 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000969 /*foundnonskip*/false,
970 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000971 }
972 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000973 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000974 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000975 PPConditionalInfo CondInfo;
976 CondInfo.WasSkipping = true; // Silence bogus warning.
977 bool InCond = CurLexer->popConditionalLevel(CondInfo);
978 assert(!InCond && "Can't be skipping if not in a conditional!");
979
980 // If we popped the outermost skipping block, we're done skipping!
981 if (!CondInfo.WasSkipping)
982 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000983 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000984 // #else directive in a skipping conditional. If not in some other
985 // skipping conditional, and if #else hasn't already been seen, enter it
986 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000987 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000988 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
989
990 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000991 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000992
993 // Note that we've seen a #else in this conditional.
994 CondInfo.FoundElse = true;
995
996 // If the conditional is at the top level, and the #if block wasn't
997 // entered, enter the #else block now.
998 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
999 CondInfo.FoundNonSkip = true;
1000 break;
1001 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001002 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001003 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1004
1005 bool ShouldEnter;
1006 // If this is in a skipping block or if we're already handled this #if
1007 // block, don't bother parsing the condition.
1008 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001009 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001010 ShouldEnter = false;
1011 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001012 // Restore the value of SkippingContents so that identifiers are
1013 // looked up, etc, inside the #elif expression.
1014 assert(SkippingContents && "We have to be skipping here!");
1015 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001016 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 SkippingContents = true;
1018 }
1019
1020 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001021 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001022
1023 // If this condition is true, enter it!
1024 if (ShouldEnter) {
1025 CondInfo.FoundNonSkip = true;
1026 break;
1027 }
1028 }
1029 }
1030
1031 CurLexer->ParsingPreprocessorDirective = false;
1032 }
1033
1034 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1035 // of the file, just stop skipping and return to lexing whatever came after
1036 // the #if block.
1037 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001038}
1039
1040//===----------------------------------------------------------------------===//
1041// Preprocessor Directive Handling.
1042//===----------------------------------------------------------------------===//
1043
1044/// HandleDirective - This callback is invoked when the lexer sees a # token
1045/// at the start of a line. This consumes the directive, modifies the
1046/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1047/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001048void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001049 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001050
1051 // We just parsed a # character at the start of a line, so we're in directive
1052 // mode. Tell the lexer this so any newlines we see will be converted into an
1053 // EOM token (this terminates the macro).
1054 CurLexer->ParsingPreprocessorDirective = true;
1055
1056 ++NumDirectives;
1057
1058 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001059 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001060
1061 switch (Result.getKind()) {
1062 default: break;
1063 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001064 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001065
1066#if 0
1067 case tok::numeric_constant:
1068 // FIXME: implement # 7 line numbers!
1069 break;
1070#endif
1071 case tok::kw_else:
1072 return HandleElseDirective(Result);
1073 case tok::kw_if:
1074 return HandleIfDirective(Result);
1075 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001076 // Get the identifier name without trigraphs or embedded newlines.
1077 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001079 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001080 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001081 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnerb8761832006-06-24 21:31:03 +00001082 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001083 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001084 return HandleElifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001085 if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001086 isExtension = true; // FIXME: implement #sccs
Chris Lattner22eb9722006-06-18 05:43:12 +00001087 // SCCS is the same as #ident.
1088 }
1089 break;
1090 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001091 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001093 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001094 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001095 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001096 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001097 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001099 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattnerb8761832006-06-24 21:31:03 +00001100 isExtension = true; // FIXME: implement #ident
Chris Lattner22eb9722006-06-18 05:43:12 +00001101 break;
1102 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001103 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001105 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001106 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +00001107 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001108 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001109 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001110 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001111 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1112 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001113 break;
1114 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001115 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1116 return HandleIncludeDirective(Result); // Handle #include.
1117 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001118 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001119 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001120 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001121 break;
1122 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001123 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001124 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001125 }
1126 break;
1127 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001128 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1129 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001130 break;
1131 }
1132 break;
1133 }
1134
1135 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001136 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001137
1138 // Read the rest of the PP line.
1139 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001140 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001141 } while (Result.getKind() != tok::eom);
1142
1143 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001144}
1145
Chris Lattnercb283342006-06-18 06:48:37 +00001146void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +00001147 bool isWarning) {
1148 // Read the rest of the line raw. We do this because we don't want macros
1149 // to be expanded and we don't require that the tokens be valid preprocessing
1150 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1151 // collapse multiple consequtive white space between tokens, but this isn't
1152 // specified by the standard.
1153 std::string Message = CurLexer->ReadToEndOfLine();
1154
1155 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
1156 return Diag(Result, DiagID, Message);
1157}
1158
Chris Lattnerb8761832006-06-24 21:31:03 +00001159//===----------------------------------------------------------------------===//
1160// Preprocessor Include Directive Handling.
1161//===----------------------------------------------------------------------===//
1162
Chris Lattner22eb9722006-06-18 05:43:12 +00001163/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1164/// file to be included from the lexer, then include it! This is a common
1165/// routine with functionality shared between #include, #include_next and
1166/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001167void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001168 const DirectoryLookup *LookupFrom,
1169 bool isImport) {
1170 ++NumIncluded;
1171 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001172 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001173
1174 // If the token kind is EOM, the error has already been diagnosed.
1175 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001176 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001177
1178 // Verify that there is nothing after the filename, other than EOM. Use the
1179 // preprocessor to lex this in case lexing the filename entered a macro.
1180 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001181
1182 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001183 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001184 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1185
Chris Lattner269c2322006-06-25 06:23:00 +00001186 // Find out whether the filename is <x> or "x".
1187 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001188
1189 // Remove the quotes.
1190 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1191
Chris Lattner22eb9722006-06-18 05:43:12 +00001192 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001193 const DirectoryLookup *CurDir;
1194 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001195 if (File == 0)
1196 return Diag(FilenameTok, diag::err_pp_file_not_found);
1197
1198 // Get information about this file.
1199 PerFileInfo &FileInfo = getFileInfo(File);
1200
1201 // If this is a #import directive, check that we have not already imported
1202 // this header.
1203 if (isImport) {
1204 // If this has already been imported, don't import it again.
1205 FileInfo.isImport = true;
1206
1207 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001208 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 } else {
1210 // Otherwise, if this is a #include of a file that was previously #import'd
1211 // or if this is the second #include of a #pragma once file, ignore it.
1212 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001213 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001214 }
1215
1216 // Look up the file, create a File ID for it.
1217 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +00001218 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001219 if (FileID == 0)
1220 return Diag(FilenameTok, diag::err_pp_file_not_found);
1221
1222 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001223 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001224
1225 // Increment the number of times this file has been included.
1226 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001227}
1228
1229/// HandleIncludeNextDirective - Implements #include_next.
1230///
Chris Lattnercb283342006-06-18 06:48:37 +00001231void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1232 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001233
1234 // #include_next is like #include, except that we start searching after
1235 // the current found directory. If we can't do this, issue a
1236 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001237 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001238 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001239 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001240 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001242 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001243 } else {
1244 // Start looking up in the next directory.
1245 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 }
1247
1248 return HandleIncludeDirective(IncludeNextTok, Lookup);
1249}
1250
1251/// HandleImportDirective - Implements #import.
1252///
Chris Lattnercb283342006-06-18 06:48:37 +00001253void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1254 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001255
1256 return HandleIncludeDirective(ImportTok, 0, true);
1257}
1258
Chris Lattnerb8761832006-06-24 21:31:03 +00001259//===----------------------------------------------------------------------===//
1260// Preprocessor Macro Directive Handling.
1261//===----------------------------------------------------------------------===//
1262
Chris Lattner22eb9722006-06-18 05:43:12 +00001263/// HandleDefineDirective - Implements #define. This consumes the entire macro
1264/// line then lets the caller lex the next real token.
1265///
Chris Lattnercb283342006-06-18 06:48:37 +00001266void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001267 ++NumDefined;
1268 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001269 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001270
1271 // Error reading macro name? If so, diagnostic already issued.
1272 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001273 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001274
Chris Lattner50b497e2006-06-18 16:32:35 +00001275 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001276
1277 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001278 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001279
1280 if (Tok.getKind() == tok::eom) {
1281 // If there is no body to this macro, we have no special handling here.
1282 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1283 // This is a function-like macro definition.
1284 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 return DiscardUntilEndOfDirective();
1286
1287 } else if (!Tok.hasLeadingSpace()) {
1288 // C99 requires whitespace between the macro definition and the body. Emit
1289 // a diagnostic for something like "#define X+".
1290 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001291 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001292 } else {
1293 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1294 // one in some cases!
1295 }
1296 } else {
1297 // This is a normal token with leading space. Clear the leading space
1298 // marker on the first token to get proper expansion.
1299 Tok.ClearFlag(LexerToken::LeadingSpace);
1300 }
1301
1302 // Read the rest of the macro body.
1303 while (Tok.getKind() != tok::eom) {
1304 MI->AddTokenToBody(Tok);
1305
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001306 // FIXME: Read macro body. See create_iso_definition.
Chris Lattner22eb9722006-06-18 05:43:12 +00001307
1308 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001309 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 }
1311
Chris Lattner13044d92006-07-03 05:16:44 +00001312 // If this is the primary source file, remember that this macro hasn't been
1313 // used yet.
1314 if (isInPrimaryFile())
1315 MI->setIsUsed(false);
1316
Chris Lattner22eb9722006-06-18 05:43:12 +00001317 // Finally, if this identifier already had a macro defined for it, verify that
1318 // the macro bodies are identical and free the old definition.
1319 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001320 if (!OtherMI->isUsed())
1321 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1322
Chris Lattner22eb9722006-06-18 05:43:12 +00001323 // FIXME: Verify the definition is the same.
1324 // Macros must be identical. This means all tokes and whitespace separation
1325 // must be the same.
1326 delete OtherMI;
1327 }
1328
1329 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001330}
1331
1332
1333/// HandleUndefDirective - Implements #undef.
1334///
Chris Lattnercb283342006-06-18 06:48:37 +00001335void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001336 ++NumUndefined;
1337 LexerToken MacroNameTok;
Chris Lattner44f8a662006-07-03 01:27:27 +00001338 ReadMacroName(MacroNameTok, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001339
1340 // Error reading macro name? If so, diagnostic already issued.
1341 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001342 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001343
1344 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001345 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001346
1347 // Okay, we finally have a valid identifier to undef.
1348 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1349
1350 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001351 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001352
Chris Lattner13044d92006-07-03 05:16:44 +00001353 if (!MI->isUsed())
1354 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001355
1356 // Free macro definition.
1357 delete MI;
1358 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001359}
1360
1361
Chris Lattnerb8761832006-06-24 21:31:03 +00001362//===----------------------------------------------------------------------===//
1363// Preprocessor Conditional Directive Handling.
1364//===----------------------------------------------------------------------===//
1365
Chris Lattner22eb9722006-06-18 05:43:12 +00001366/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1367/// true when this is a #ifndef directive.
1368///
Chris Lattnercb283342006-06-18 06:48:37 +00001369void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001370 ++NumIf;
1371 LexerToken DirectiveTok = Result;
1372
1373 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001374 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001375
1376 // Error reading macro name? If so, diagnostic already issued.
1377 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001378 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001379
1380 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001381 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001382
Chris Lattnera78a97e2006-07-03 05:42:18 +00001383 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1384
1385 // If there is a macro, mark it used.
1386 if (MI) MI->setIsUsed(true);
1387
Chris Lattner22eb9722006-06-18 05:43:12 +00001388 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001389 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001390 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001391 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001392 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001393 } else {
1394 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001395 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001396 /*Foundnonskip*/false,
1397 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001398 }
1399}
1400
1401/// HandleIfDirective - Implements the #if directive.
1402///
Chris Lattnercb283342006-06-18 06:48:37 +00001403void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001404 ++NumIf;
Chris Lattner7966aaf2006-06-18 06:50:36 +00001405 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001406
1407 // Should we include the stuff contained by this directive?
1408 if (ConditionalTrue) {
1409 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001410 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001411 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 } else {
1413 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001414 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001415 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001416 }
1417}
1418
1419/// HandleEndifDirective - Implements the #endif directive.
1420///
Chris Lattnercb283342006-06-18 06:48:37 +00001421void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001422 ++NumEndif;
1423 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001424 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001425
1426 PPConditionalInfo CondInfo;
1427 if (CurLexer->popConditionalLevel(CondInfo)) {
1428 // No conditionals on the stack: this is an #endif without an #if.
1429 return Diag(EndifToken, diag::err_pp_endif_without_if);
1430 }
1431
1432 assert(!CondInfo.WasSkipping && !isSkipping() &&
1433 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001434}
1435
1436
Chris Lattnercb283342006-06-18 06:48:37 +00001437void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001438 ++NumElse;
1439 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001440 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001441
1442 PPConditionalInfo CI;
1443 if (CurLexer->popConditionalLevel(CI))
1444 return Diag(Result, diag::pp_err_else_without_if);
1445
1446 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001447 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001448
1449 // Finally, skip the rest of the contents of this block and return the first
1450 // token after it.
1451 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1452 /*FoundElse*/true);
1453}
1454
Chris Lattnercb283342006-06-18 06:48:37 +00001455void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001456 ++NumElse;
1457 // #elif directive in a non-skipping conditional... start skipping.
1458 // We don't care what the condition is, because we will always skip it (since
1459 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001460 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001461
1462 PPConditionalInfo CI;
1463 if (CurLexer->popConditionalLevel(CI))
1464 return Diag(ElifToken, diag::pp_err_elif_without_if);
1465
1466 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001467 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001468
1469 // Finally, skip the rest of the contents of this block and return the first
1470 // token after it.
1471 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1472 /*FoundElse*/CI.FoundElse);
1473}
Chris Lattnerb8761832006-06-24 21:31:03 +00001474