blob: c3fc8b94081b59b91837b7c6fb127c29120259ac [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//
Chris Lattner22eb9722006-06-18 05:43:12 +000014// Options to support:
15// -H - Print the name of each header file used.
16// -C -CC - Do not discard comments for cpp.
Chris Lattner22eb9722006-06-18 05:43:12 +000017// -d[MDNI] - Dump various things.
18// -fworking-directory - #line's with preprocessor's working dir.
19// -fpreprocessed
20// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
21// -W*
22// -w
23//
24// Messages to emit:
25// "Multiple include guards may be useful for:\n"
26//
Chris Lattner22eb9722006-06-18 05:43:12 +000027//===----------------------------------------------------------------------===//
28
29#include "clang/Lex/Preprocessor.h"
30#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8761832006-06-24 21:31:03 +000031#include "clang/Lex/Pragma.h"
Chris Lattner0b8cfc22006-06-28 06:49:17 +000032#include "clang/Lex/ScratchBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include "clang/Basic/Diagnostic.h"
34#include "clang/Basic/FileManager.h"
35#include "clang/Basic/SourceManager.h"
36#include <iostream>
37using namespace llvm;
38using namespace clang;
39
40//===----------------------------------------------------------------------===//
41
42Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
43 FileManager &FM, SourceManager &SM)
44 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
45 SystemDirIdx(0), NoCurDirSearch(false),
Chris Lattnerc8997182006-06-22 05:52:16 +000046 CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +000047 ScratchBuf = new ScratchBuffer(SourceMgr);
48
Chris Lattner22eb9722006-06-18 05:43:12 +000049 // Clear stats.
50 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
51 NumIf = NumElse = NumEndif = 0;
Chris Lattner78186052006-07-09 00:45:31 +000052 NumEnteredSourceFiles = 0;
53 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
54 NumFastMacroExpanded = 0;
Chris Lattner3665f162006-07-04 07:26:10 +000055 MaxIncludeStackDepth = 0; NumMultiIncludeFileOptzn = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000056 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000057
Chris Lattner22eb9722006-06-18 05:43:12 +000058 // Macro expansion is enabled.
59 DisableMacroExpansion = false;
60 SkippingContents = false;
Chris Lattner78186052006-07-09 00:45:31 +000061 InMacroFormalArgs = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000062
63 // There is no file-change handler yet.
64 FileChangeHandler = 0;
Chris Lattner01d66cc2006-07-03 22:16:27 +000065 IdentHandler = 0;
Chris Lattnerb8761832006-06-24 21:31:03 +000066
Chris Lattner8ff71992006-07-06 05:17:39 +000067 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
68 // This gets unpoisoned where it is allowed.
69 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
70
Chris Lattnerb8761832006-06-24 21:31:03 +000071 // Initialize the pragma handlers.
72 PragmaHandlers = new PragmaNamespace(0);
73 RegisterBuiltinPragmas();
Chris Lattner677757a2006-06-28 05:26:32 +000074
75 // Initialize builtin macros like __LINE__ and friends.
76 RegisterBuiltinMacros();
Chris Lattner22eb9722006-06-18 05:43:12 +000077}
78
79Preprocessor::~Preprocessor() {
80 // Free any active lexers.
81 delete CurLexer;
82
Chris Lattner69772b02006-07-02 20:34:39 +000083 while (!IncludeMacroStack.empty()) {
84 delete IncludeMacroStack.back().TheLexer;
85 delete IncludeMacroStack.back().TheMacroExpander;
86 IncludeMacroStack.pop_back();
Chris Lattner22eb9722006-06-18 05:43:12 +000087 }
Chris Lattnerb8761832006-06-24 21:31:03 +000088
89 // Release pragma information.
90 delete PragmaHandlers;
Chris Lattner0b8cfc22006-06-28 06:49:17 +000091
92 // Delete the scratch buffer info.
93 delete ScratchBuf;
Chris Lattner22eb9722006-06-18 05:43:12 +000094}
95
96/// getFileInfo - Return the PerFileInfo structure for the specified
97/// FileEntry.
98Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
99 if (FE->getUID() >= FileInfo.size())
100 FileInfo.resize(FE->getUID()+1);
101 return FileInfo[FE->getUID()];
102}
103
104
105/// AddKeywords - Add all keywords to the symbol table.
106///
107void Preprocessor::AddKeywords() {
108 enum {
109 C90Shift = 0,
110 EXTC90 = 1 << C90Shift,
111 NOTC90 = 2 << C90Shift,
112 C99Shift = 2,
113 EXTC99 = 1 << C99Shift,
114 NOTC99 = 2 << C99Shift,
115 CPPShift = 4,
116 EXTCPP = 1 << CPPShift,
117 NOTCPP = 2 << CPPShift,
118 Mask = 3
119 };
120
121 // Add keywords and tokens for the current language.
122#define KEYWORD(NAME, FLAGS) \
123 AddKeyword(#NAME+1, tok::kw##NAME, \
124 (FLAGS >> C90Shift) & Mask, \
125 (FLAGS >> C99Shift) & Mask, \
126 (FLAGS >> CPPShift) & Mask);
127#define ALIAS(NAME, TOK) \
128 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
129#include "clang/Basic/TokenKinds.def"
130}
131
132/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
133/// the specified LexerToken's location, translating the token's start
134/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000135void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000136 const std::string &Msg) {
137 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
138 // warnings or extensions.
139 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000140 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000141
Chris Lattnercb283342006-06-18 06:48:37 +0000142 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000143}
Chris Lattnerd01e2912006-06-18 16:22:51 +0000144
145void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
146 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
147 << getSpelling(Tok) << "'";
148
149 if (!DumpFlags) return;
150 std::cerr << "\t";
151 if (Tok.isAtStartOfLine())
152 std::cerr << " [StartOfLine]";
153 if (Tok.hasLeadingSpace())
154 std::cerr << " [LeadingSpace]";
155 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000156 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000157 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
158 << "']";
159 }
160}
161
162void Preprocessor::DumpMacro(const MacroInfo &MI) const {
163 std::cerr << "MACRO: ";
164 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
165 DumpToken(MI.getReplacementToken(i));
166 std::cerr << " ";
167 }
168 std::cerr << "\n";
169}
170
Chris Lattner22eb9722006-06-18 05:43:12 +0000171void Preprocessor::PrintStats() {
172 std::cerr << "\n*** Preprocessor Stats:\n";
173 std::cerr << FileInfo.size() << " files tracked.\n";
174 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
175 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
176 NumOnceOnlyFiles += FileInfo[i].isImport;
177 if (MaxNumIncludes < FileInfo[i].NumIncludes)
178 MaxNumIncludes = FileInfo[i].NumIncludes;
179 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
180 }
181 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
182 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
183 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
184
185 std::cerr << NumDirectives << " directives found:\n";
186 std::cerr << " " << NumDefined << " #define.\n";
187 std::cerr << " " << NumUndefined << " #undef.\n";
188 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
Chris Lattner3665f162006-07-04 07:26:10 +0000189 std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to"
190 << " the multi-include optimization.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000191 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
192 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
193 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
194 std::cerr << " " << NumElse << " #else/#elif.\n";
195 std::cerr << " " << NumEndif << " #endif.\n";
196 std::cerr << " " << NumPragma << " #pragma.\n";
197 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
198
Chris Lattner78186052006-07-09 00:45:31 +0000199 std::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
200 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
Chris Lattner22eb9722006-06-18 05:43:12 +0000201 << NumFastMacroExpanded << " on the fast path.\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000202}
203
204//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000205// Token Spelling
206//===----------------------------------------------------------------------===//
207
208
209/// getSpelling() - Return the 'spelling' of this token. The spelling of a
210/// token are the characters used to represent the token in the source file
211/// after trigraph expansion and escaped-newline folding. In particular, this
212/// wants to get the true, uncanonicalized, spelling of things like digraphs
213/// UCNs, etc.
214std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
215 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
216
217 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000218 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000219 if (!Tok.needsCleaning())
220 return std::string(TokStart, TokStart+Tok.getLength());
221
Chris Lattnerd01e2912006-06-18 16:22:51 +0000222 std::string Result;
223 Result.reserve(Tok.getLength());
224
Chris Lattneref9eae12006-07-04 22:33:12 +0000225 // Otherwise, hard case, relex the characters into the string.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000226 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
227 Ptr != End; ) {
228 unsigned CharSize;
229 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
230 Ptr += CharSize;
231 }
232 assert(Result.size() != unsigned(Tok.getLength()) &&
233 "NeedsCleaning flag set on something that didn't need cleaning!");
234 return Result;
235}
236
237/// getSpelling - This method is used to get the spelling of a token into a
238/// preallocated buffer, instead of as an std::string. The caller is required
239/// to allocate enough space for the token, which is guaranteed to be at least
240/// Tok.getLength() bytes long. The actual length of the token is returned.
Chris Lattneref9eae12006-07-04 22:33:12 +0000241///
242/// Note that this method may do two possible things: it may either fill in
243/// the buffer specified with characters, or it may *change the input pointer*
244/// to point to a constant buffer with the data already in it (avoiding a
245/// copy). The caller is not allowed to modify the returned buffer pointer
246/// if an internal buffer is returned.
247unsigned Preprocessor::getSpelling(const LexerToken &Tok,
248 const char *&Buffer) const {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000249 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
250
Chris Lattnerd3a15f72006-07-04 23:01:03 +0000251 // If this token is an identifier, just return the string from the identifier
252 // table, which is very quick.
253 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
254 Buffer = II->getName();
255 return Tok.getLength();
256 }
257
258 // Otherwise, compute the start of the token in the input lexer buffer.
Chris Lattner50b497e2006-06-18 16:32:35 +0000259 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000260
261 // If this token contains nothing interesting, return it directly.
262 if (!Tok.needsCleaning()) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000263 Buffer = TokStart;
264 return Tok.getLength();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000265 }
266 // Otherwise, hard case, relex the characters into the string.
Chris Lattneref9eae12006-07-04 22:33:12 +0000267 char *OutBuf = const_cast<char*>(Buffer);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000268 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
269 Ptr != End; ) {
270 unsigned CharSize;
271 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
272 Ptr += CharSize;
273 }
274 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
275 "NeedsCleaning flag set on something that didn't need cleaning!");
276
277 return OutBuf-Buffer;
278}
279
280//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000281// Source File Location Methods.
282//===----------------------------------------------------------------------===//
283
284
285/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
286/// return null on failure. isAngled indicates whether the file reference is
287/// for system #include's or not (i.e. using <> instead of "").
288const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000289 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000290 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000291 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000292 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000293 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000294
295 // If 'Filename' is absolute, check to see if it exists and no searching.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000296 // FIXME: Portability. This should be a sys::Path interface, this doesn't
297 // handle things like C:\foo.txt right, nor win32 \\network\device\blah.
Chris Lattner22eb9722006-06-18 05:43:12 +0000298 if (Filename[0] == '/') {
299 // If this was an #include_next "/absolute/file", fail.
300 if (FromDir) return 0;
301
302 // Otherwise, just return the file.
303 return FileMgr.getFile(Filename);
304 }
305
306 // Step #0, unless disabled, check to see if the file is in the #includer's
307 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000308 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000309 unsigned TheFileID = getCurrentFileLexer()->getCurFileID();
310 const FileEntry *CurFE = SourceMgr.getFileEntryForFileID(TheFileID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000311 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000312 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000313 // FIXME: Portability. Should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000314 if (const FileEntry *FE =
315 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000316 if (CurDirLookup)
317 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000318 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000319 CurDir = 0;
320
321 // This file is a system header or C++ unfriendly if the old file is.
322 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000323 return FE;
324 }
325 }
326 }
327
328 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000329 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000330
331 // If this is a #include_next request, start searching after the directory the
332 // file was found in.
333 if (FromDir)
334 i = FromDir-&SearchDirs[0];
335
336 // Check each directory in sequence to see if it contains this file.
337 for (; i != SearchDirs.size(); ++i) {
338 // Concatenate the requested file onto the directory.
Chris Lattner4d5e1a72006-07-03 01:01:29 +0000339 // FIXME: Portability. Adding file to dir should be in sys::Path.
340 std::string SearchDir = SearchDirs[i].getDir()->getName()+"/"+Filename;
341 if (const FileEntry *FE = FileMgr.getFile(SearchDir)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000342 CurDir = &SearchDirs[i];
343
344 // This file is a system header or C++ unfriendly if the dir is.
345 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000346 return FE;
347 }
348 }
349
350 // Otherwise, didn't find it.
351 return 0;
352}
353
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000354/// isInPrimaryFile - Return true if we're in the top-level file, not in a
355/// #include.
356bool Preprocessor::isInPrimaryFile() const {
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000357 if (CurLexer && !CurLexer->Is_PragmaLexer)
Chris Lattner13044d92006-07-03 05:16:44 +0000358 return CurLexer->isMainFile();
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000359
Chris Lattner13044d92006-07-03 05:16:44 +0000360 // If there are any stacked lexers, we're in a #include.
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000361 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i)
Chris Lattner13044d92006-07-03 05:16:44 +0000362 if (IncludeMacroStack[i].TheLexer &&
363 !IncludeMacroStack[i].TheLexer->Is_PragmaLexer)
364 return IncludeMacroStack[i].TheLexer->isMainFile();
365 return false;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000366}
367
368/// getCurrentLexer - Return the current file lexer being lexed from. Note
369/// that this ignores any potentially active macro expansions and _Pragma
370/// expansions going on at the time.
371Lexer *Preprocessor::getCurrentFileLexer() const {
372 if (CurLexer && !CurLexer->Is_PragmaLexer) return CurLexer;
373
374 // Look for a stacked lexer.
375 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Chris Lattnerf88c53a2006-07-03 05:26:05 +0000376 Lexer *L = IncludeMacroStack[i-1].TheLexer;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000377 if (L && !L->Is_PragmaLexer) // Ignore macro & _Pragma expansions.
378 return L;
379 }
380 return 0;
381}
382
383
Chris Lattner22eb9722006-06-18 05:43:12 +0000384/// EnterSourceFile - Add a source file to the top of the include stack and
385/// start lexing tokens from it instead of the current buffer. Return true
386/// on failure.
387void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattner13044d92006-07-03 05:16:44 +0000388 const DirectoryLookup *CurDir,
389 bool isMainFile) {
Chris Lattner69772b02006-07-02 20:34:39 +0000390 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000391 ++NumEnteredSourceFiles;
392
Chris Lattner69772b02006-07-02 20:34:39 +0000393 if (MaxIncludeStackDepth < IncludeMacroStack.size())
394 MaxIncludeStackDepth = IncludeMacroStack.size();
Chris Lattner22eb9722006-06-18 05:43:12 +0000395
Chris Lattner22eb9722006-06-18 05:43:12 +0000396 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
Chris Lattner69772b02006-07-02 20:34:39 +0000397 Lexer *TheLexer = new Lexer(Buffer, FileID, *this);
Chris Lattner13044d92006-07-03 05:16:44 +0000398 if (isMainFile) TheLexer->setIsMainFile();
Chris Lattner69772b02006-07-02 20:34:39 +0000399 EnterSourceFileWithLexer(TheLexer, CurDir);
400}
Chris Lattner22eb9722006-06-18 05:43:12 +0000401
Chris Lattner69772b02006-07-02 20:34:39 +0000402/// EnterSourceFile - Add a source file to the top of the include stack and
403/// start lexing tokens from it instead of the current buffer.
404void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
405 const DirectoryLookup *CurDir) {
406
407 // Add the current lexer to the include stack.
408 if (CurLexer || CurMacroExpander)
409 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
410 CurMacroExpander));
411
412 CurLexer = TheLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000413 CurDirLookup = CurDir;
Chris Lattner69772b02006-07-02 20:34:39 +0000414 CurMacroExpander = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +0000415
416 // Notify the client, if desired, that we are in a new source file.
Chris Lattner98a53122006-07-02 23:00:20 +0000417 if (FileChangeHandler && !CurLexer->Is_PragmaLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000418 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
419
420 // Get the file entry for the current file.
421 if (const FileEntry *FE =
422 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
423 FileType = getFileInfo(FE).DirInfo;
424
Chris Lattner1840e492006-07-02 22:30:01 +0000425 FileChangeHandler(SourceLocation(CurLexer->getCurFileID(), 0),
Chris Lattner55a60952006-06-25 04:20:34 +0000426 EnterFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000427 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000428}
429
Chris Lattner69772b02006-07-02 20:34:39 +0000430
431
Chris Lattner22eb9722006-06-18 05:43:12 +0000432/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000433/// tokens from it instead of the current buffer.
Chris Lattner78186052006-07-09 00:45:31 +0000434void Preprocessor::EnterMacro(LexerToken &Tok, MacroFormalArgs *Formals) {
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000435 IdentifierInfo *Identifier = Tok.getIdentifierInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000436 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner69772b02006-07-02 20:34:39 +0000437 IncludeMacroStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup,
438 CurMacroExpander));
439 CurLexer = 0;
440 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000441
Chris Lattner22eb9722006-06-18 05:43:12 +0000442 // Mark the macro as currently disabled, so that it is not recursively
443 // expanded.
444 MI.DisableMacro();
Chris Lattner78186052006-07-09 00:45:31 +0000445 CurMacroExpander = new MacroExpander(Tok, Formals, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000446}
447
Chris Lattner22eb9722006-06-18 05:43:12 +0000448//===----------------------------------------------------------------------===//
Chris Lattner677757a2006-06-28 05:26:32 +0000449// Macro Expansion Handling.
Chris Lattner22eb9722006-06-18 05:43:12 +0000450//===----------------------------------------------------------------------===//
451
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000452/// RegisterBuiltinMacro - Register the specified identifier in the identifier
453/// table and mark it as a builtin macro to be expanded.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000454IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000455 // Get the identifier.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000456 IdentifierInfo *Id = getIdentifierInfo(Name);
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000457
458 // Mark it as being a macro that is builtin.
459 MacroInfo *MI = new MacroInfo(SourceLocation());
460 MI->setIsBuiltinMacro();
461 Id->setMacroInfo(MI);
462 return Id;
463}
464
465
Chris Lattner677757a2006-06-28 05:26:32 +0000466/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
467/// identifier table.
468void Preprocessor::RegisterBuiltinMacros() {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000469 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
Chris Lattner630b33c2006-07-01 22:46:53 +0000470 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Chris Lattnerc673f902006-06-30 06:10:41 +0000471 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
472 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
Chris Lattner69772b02006-07-02 20:34:39 +0000473 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
Chris Lattnerc1283b92006-07-01 23:16:30 +0000474
475 // GCC Extensions.
476 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
477 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
Chris Lattner847e0e42006-07-01 23:49:16 +0000478 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
Chris Lattner22eb9722006-06-18 05:43:12 +0000479}
480
Chris Lattnerc2395832006-07-09 00:57:04 +0000481/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
482/// in its expansion, currently expands to that token literally.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000483static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
484 const IdentifierInfo *MacroIdent) {
Chris Lattnerc2395832006-07-09 00:57:04 +0000485 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
486
487 // If the token isn't an identifier, it's always literally expanded.
488 if (II == 0) return true;
489
490 // If the identifier is a macro, and if that macro is enabled, it may be
491 // expanded so it's not a trivial expansion.
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000492 if (II->getMacroInfo() && II->getMacroInfo()->isEnabled() &&
493 // Fast expanding "#define X X" is ok, because X would be disabled.
494 II != MacroIdent)
Chris Lattnerc2395832006-07-09 00:57:04 +0000495 return false;
496
497 // If this is an object-like macro invocation, it is safe to trivially expand
498 // it.
499 if (MI->isObjectLike()) return true;
500
501 // If this is a function-like macro invocation, it's safe to trivially expand
502 // as long as the identifier is not a macro argument.
503 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
504 I != E; ++I)
505 if (*I == II)
506 return false; // Identifier is a macro argument.
507 return true;
508}
509
Chris Lattner677757a2006-06-28 05:26:32 +0000510
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000511/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
512/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner78186052006-07-09 00:45:31 +0000513bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000514 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000515
516 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
517 if (MI->isBuiltinMacro()) {
518 ExpandBuiltinMacro(Identifier);
519 return false;
520 }
521
522 /// FormalArgs - If this is a function-like macro expansion, this contains,
523 /// for each macro argument, the list of tokens that were provided to the
524 /// invocation.
525 MacroFormalArgs *FormalArgs = 0;
526
527 // If this is a function-like macro, read the arguments.
528 if (MI->isFunctionLike()) {
529 // FIXME: We need to query to see if the ( exists without reading it.
530
531 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
532 // name isn't a '(', this macro should not be expanded.
533 bool isFunctionInvocation = true;
534 if (!isFunctionInvocation)
535 return true;
536
537 LexerToken Tok;
538 LexUnexpandedToken(Tok);
539 assert(Tok.getKind() == tok::l_paren &&
540 "not a function-like macro invocation!");
541
542 // Remember that we are now parsing the arguments to a macro invocation.
543 // Preprocessor directives used inside macro arguments are not portable, and
544 // this enables the warning.
545 InMacroFormalArgs = true;
546 FormalArgs = ReadFunctionLikeMacroFormalArgs(Identifier, MI);
547
548 // Finished parsing args.
549 InMacroFormalArgs = false;
550
551 // If there was an error parsing the arguments, bail out.
552 if (FormalArgs == 0) return false;
553
554 ++NumFnMacroExpanded;
555 } else {
556 ++NumMacroExpanded;
557 }
Chris Lattner13044d92006-07-03 05:16:44 +0000558
559 // Notice that this macro has been used.
560 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000561
562 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000563
564 // If this macro expands to no tokens, don't bother to push it onto the
565 // expansion stack, only to take it right back off.
566 if (MI->getNumTokens() == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000567 // No need for formal arg info.
568 delete FormalArgs;
569
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000570 // Ignore this macro use, just return the next token in the current
571 // buffer.
572 bool HadLeadingSpace = Identifier.hasLeadingSpace();
573 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
574
575 Lex(Identifier);
576
577 // If the identifier isn't on some OTHER line, inherit the leading
578 // whitespace/first-on-a-line property of this token. This handles
579 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
580 // empty.
581 if (!Identifier.isAtStartOfLine()) {
582 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
583 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
584 }
585 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000586 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000587
Chris Lattner3ce1d1a2006-07-09 01:00:18 +0000588 } else if (MI->getNumTokens() == 1 &&
589 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo())){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000590 // Otherwise, if this macro expands into a single trivially-expanded
591 // token: expand it now. This handles common cases like
592 // "#define VAL 42".
593
594 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
595 // identifier to the expanded token.
596 bool isAtStartOfLine = Identifier.isAtStartOfLine();
597 bool hasLeadingSpace = Identifier.hasLeadingSpace();
598
599 // Remember where the token is instantiated.
600 SourceLocation InstantiateLoc = Identifier.getLocation();
601
602 // Replace the result token.
603 Identifier = MI->getReplacementToken(0);
604
605 // Restore the StartOfLine/LeadingSpace markers.
606 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
607 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
608
609 // Update the tokens location to include both its logical and physical
610 // locations.
611 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000612 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000613 Identifier.SetLocation(Loc);
614
615 // Since this is not an identifier token, it can't be macro expanded, so
616 // we're done.
617 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000618 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000619 }
620
Chris Lattner78186052006-07-09 00:45:31 +0000621 // Start expanding the macro.
622 EnterMacro(Identifier, FormalArgs);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000623
624 // Now that the macro is at the top of the include stack, ask the
625 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000626 Lex(Identifier);
627 return false;
628}
629
630/// ReadFunctionLikeMacroFormalArgs - After reading "MACRO(", this method is
631/// invoked to read all of the formal arguments specified for the macro
632/// invocation. This returns null on error.
633MacroFormalArgs *Preprocessor::
634ReadFunctionLikeMacroFormalArgs(LexerToken &MacroName, MacroInfo *MI) {
635 // Use an auto_ptr here so that the MacroFormalArgs object is deleted on
636 // all error paths.
637 std::auto_ptr<MacroFormalArgs> Args(new MacroFormalArgs(MI));
638
639 // The number of fixed arguments to parse.
640 unsigned NumFixedArgsLeft = MI->getNumArgs();
641 bool isVariadic = MI->isVariadic();
642
643 // If this is a C99-style varargs macro invocation, add an extra expected
644 // argument, which will catch all of the varargs formals in one argument.
645 if (MI->isC99Varargs())
646 ++NumFixedArgsLeft;
647
648 // Outer loop, while there are more arguments, keep reading them.
649 LexerToken Tok;
650 Tok.SetKind(tok::comma);
651 --NumFixedArgsLeft; // Start reading the first arg.
652
653 while (Tok.getKind() == tok::comma) {
654 // ArgTokens - Build up a list of tokens that make up this argument.
655 std::vector<LexerToken> ArgTokens;
656 // C99 6.10.3p11: Keep track of the number of l_parens we have seen.
657 unsigned NumParens = 0;
658
659 while (1) {
660 LexUnexpandedToken(Tok);
661
662 if (Tok.getKind() == tok::eof) {
663 Diag(MacroName, diag::err_unterm_macro_invoc);
664 // Do not lose the EOF. Return it to the client.
665 MacroName = Tok;
666 return 0;
667 } else if (Tok.getKind() == tok::r_paren) {
668 // If we found the ) token, the macro arg list is done.
669 if (NumParens-- == 0)
670 break;
671 } else if (Tok.getKind() == tok::l_paren) {
672 ++NumParens;
673 } else if (Tok.getKind() == tok::comma && NumParens == 0) {
674 // Comma ends this argument if there are more fixed arguments expected.
675 if (NumFixedArgsLeft)
676 break;
677
678 // If this is not a variadic macro, too many formals were specified.
679 if (!isVariadic) {
680 // Emit the diagnostic at the macro name in case there is a missing ).
681 // Emitting it at the , could be far away from the macro name.
682 Diag(MacroName, diag::err_too_many_formals_in_macro_invoc);
683 return 0;
684 }
685 // Otherwise, continue to add the tokens to this variable argument.
686 }
687
688 ArgTokens.push_back(Tok);
689 }
690
691 // Remember the tokens that make up this argument. This destroys ArgTokens.
692 Args->addArgument(ArgTokens);
693 --NumFixedArgsLeft;
694 };
695
696 // Okay, we either found the r_paren. Check to see if we parsed too few
697 // arguments.
698 unsigned NumFormals = Args->getNumArguments();
699 unsigned MinArgsExpected = MI->getNumArgs();
700
701 // C99 expects us to pass at least one vararg arg (but as an extension, we
Chris Lattnerc2395832006-07-09 00:57:04 +0000702 // don't require this). GNU-style varargs already include the 'rest' name in
703 // the count.
704 MinArgsExpected += MI->isC99Varargs();
Chris Lattner78186052006-07-09 00:45:31 +0000705
706 if (NumFormals < MinArgsExpected) {
707 // There are several cases where too few arguments is ok, handle them now.
708 if (NumFormals+1 == MinArgsExpected && MI->isVariadic()) {
709 // Varargs where the named vararg parameter is missing: ok as extension.
710 // #define A(x, ...)
711 // A("blah")
712 Diag(Tok, diag::ext_missing_varargs_arg);
713 } else if (MI->getNumArgs() == 1) {
714 // #define A(x)
715 // A()
716 // is ok. Add an empty argument.
717 std::vector<LexerToken> ArgTokens;
718 Args->addArgument(ArgTokens);
719 } else {
720 // Otherwise, emit the error.
721 Diag(Tok, diag::err_too_few_formals_in_macro_invoc);
722 return 0;
723 }
724 }
725
726 return Args.release();
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000727}
728
Chris Lattnerc673f902006-06-30 06:10:41 +0000729/// ComputeDATE_TIME - Compute the current time, enter it into the specified
730/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
731/// the identifier tokens inserted.
732static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
733 ScratchBuffer *ScratchBuf) {
734 time_t TT = time(0);
735 struct tm *TM = localtime(&TT);
736
737 static const char * const Months[] = {
738 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
739 };
740
741 char TmpBuffer[100];
742 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
743 TM->tm_year+1900);
744 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
745
746 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
747 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
748}
749
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000750/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
751/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000752void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000753 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000754 IdentifierInfo *II = Tok.getIdentifierInfo();
755 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000756
757 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
758 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000759 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +0000760 return Handle_Pragma(Tok);
761
Chris Lattner78186052006-07-09 00:45:31 +0000762 ++NumBuiltinMacroExpanded;
763
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000764 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000765
766 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000767 Tok.SetIdentifierInfo(0);
768 Tok.ClearFlag(LexerToken::NeedsCleaning);
769
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000770 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000771 // __LINE__ expands to a simple numeric value.
772 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
773 unsigned Length = strlen(TmpBuffer);
774 Tok.SetKind(tok::numeric_constant);
775 Tok.SetLength(Length);
776 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000777 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000778 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000779 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000780 Diag(Tok, diag::ext_pp_base_file);
781 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
782 while (NextLoc.getFileID() != 0) {
783 Loc = NextLoc;
784 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
785 }
786 }
787
Chris Lattner0766e592006-07-03 01:07:01 +0000788 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
789 std::string FN = SourceMgr.getSourceName(Loc);
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000790 FN = Lexer::Stringify(FN);
Chris Lattner630b33c2006-07-01 22:46:53 +0000791 Tok.SetKind(tok::string_literal);
792 Tok.SetLength(FN.size());
793 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000794 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000795 if (!DATELoc.isValid())
796 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
797 Tok.SetKind(tok::string_literal);
798 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
799 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000800 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000801 if (!TIMELoc.isValid())
802 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
803 Tok.SetKind(tok::string_literal);
804 Tok.SetLength(strlen("\"hh:mm:ss\""));
805 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000806 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000807 Diag(Tok, diag::ext_pp_include_level);
808
809 // Compute the include depth of this token.
810 unsigned Depth = 0;
811 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
812 for (; Loc.getFileID() != 0; ++Depth)
813 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
814
815 // __INCLUDE_LEVEL__ expands to a simple numeric value.
816 sprintf(TmpBuffer, "%u", Depth);
817 unsigned Length = strlen(TmpBuffer);
818 Tok.SetKind(tok::numeric_constant);
819 Tok.SetLength(Length);
820 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000821 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +0000822 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
823 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
824 Diag(Tok, diag::ext_pp_timestamp);
825
826 // Get the file that we are lexing out of. If we're currently lexing from
827 // a macro, dig into the include stack.
828 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000829 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000830
831 if (TheLexer)
832 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
833
834 // If this file is older than the file it depends on, emit a diagnostic.
835 const char *Result;
836 if (CurFile) {
837 time_t TT = CurFile->getModificationTime();
838 struct tm *TM = localtime(&TT);
839 Result = asctime(TM);
840 } else {
841 Result = "??? ??? ?? ??:??:?? ????\n";
842 }
843 TmpBuffer[0] = '"';
844 strcpy(TmpBuffer+1, Result);
845 unsigned Len = strlen(TmpBuffer);
846 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
847 Tok.SetKind(tok::string_literal);
848 Tok.SetLength(Len);
849 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000850 } else {
851 assert(0 && "Unknown identifier!");
852 }
853}
Chris Lattner677757a2006-06-28 05:26:32 +0000854
Chris Lattner13044d92006-07-03 05:16:44 +0000855namespace {
856struct UnusedIdentifierReporter : public IdentifierVisitor {
857 Preprocessor &PP;
858 UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
859
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000860 void VisitIdentifier(IdentifierInfo &II) const {
861 if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
862 PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner13044d92006-07-03 05:16:44 +0000863 }
864};
865}
866
Chris Lattner677757a2006-06-28 05:26:32 +0000867//===----------------------------------------------------------------------===//
868// Lexer Event Handling.
869//===----------------------------------------------------------------------===//
870
Chris Lattnercefc7682006-07-08 08:28:12 +0000871/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
872/// identifier information for the token and install it into the token.
873IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier,
874 const char *BufPtr) {
875 assert(Identifier.getKind() == tok::identifier && "Not an identifier!");
876 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
877
878 // Look up this token, see if it is a macro, or if it is a language keyword.
879 IdentifierInfo *II;
880 if (BufPtr && !Identifier.needsCleaning()) {
881 // No cleaning needed, just use the characters from the lexed buffer.
882 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
883 } else {
884 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
885 const char *TmpBuf = (char*)alloca(Identifier.getLength());
886 unsigned Size = getSpelling(Identifier, TmpBuf);
887 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
888 }
889 Identifier.SetIdentifierInfo(II);
890 return II;
891}
892
893
Chris Lattner677757a2006-06-28 05:26:32 +0000894/// HandleIdentifier - This callback is invoked when the lexer reads an
895/// identifier. This callback looks up the identifier in the map and/or
896/// potentially macro expands it or turns it into a named token (like 'for').
897void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
898 if (Identifier.getIdentifierInfo() == 0) {
899 // If we are skipping tokens (because we are in a #if 0 block), there will
900 // be no identifier info, just return the token.
901 assert(isSkipping() && "Token isn't an identifier?");
902 return;
903 }
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000904 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +0000905
906 // If this identifier was poisoned, and if it was not produced from a macro
907 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +0000908 if (II.isPoisoned() && CurLexer) {
909 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
910 Diag(Identifier, diag::err_pp_used_poisoned_id);
911 else
912 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
913 }
Chris Lattner677757a2006-06-28 05:26:32 +0000914
Chris Lattner78186052006-07-09 00:45:31 +0000915 // If this is a macro to be expanded, do it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000916 if (MacroInfo *MI = II.getMacroInfo())
Chris Lattner677757a2006-06-28 05:26:32 +0000917 if (MI->isEnabled() && !DisableMacroExpansion)
Chris Lattner78186052006-07-09 00:45:31 +0000918 if (!HandleMacroExpandedIdentifier(Identifier, MI))
919 return;
Chris Lattner677757a2006-06-28 05:26:32 +0000920
921 // Change the kind of this identifier to the appropriate token kind, e.g.
922 // turning "for" into a keyword.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000923 Identifier.SetKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +0000924
925 // If this is an extension token, diagnose its use.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000926 if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +0000927}
928
Chris Lattner22eb9722006-06-18 05:43:12 +0000929/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
930/// the current file. This either returns the EOF token or pops a level off
931/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000932void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 assert(!CurMacroExpander &&
934 "Ending a file when currently in a macro!");
935
936 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
937 // this is an error condition. Just return the EOF token up to
938 // SkipExcludedConditionalBlock. The Lexer will have already have issued
939 // errors for the unterminated #if's on the conditional stack.
940 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000941 Result.StartToken();
942 CurLexer->BufferPtr = CurLexer->BufferEnd;
943 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000944 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000945 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000946 }
947
Chris Lattner371ac8a2006-07-04 07:11:10 +0000948 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +0000949 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000950 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +0000951 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +0000952 // Okay, this has a controlling macro, remember in PerFileInfo.
953 if (const FileEntry *FE =
954 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
955 getFileInfo(FE).ControllingMacro = ControllingMacro;
Chris Lattner371ac8a2006-07-04 07:11:10 +0000956 }
957 }
958
Chris Lattner22eb9722006-06-18 05:43:12 +0000959 // If this is a #include'd file, pop it off the include stack and continue
960 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000961 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000962 // We're done with the #included file.
963 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000964 CurLexer = IncludeMacroStack.back().TheLexer;
965 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
966 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
967 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000968
969 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000970 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000971 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
972
973 // Get the file entry for the current file.
974 if (const FileEntry *FE =
975 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
976 FileType = getFileInfo(FE).DirInfo;
977
Chris Lattner0c885f52006-06-21 06:50:18 +0000978 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000979 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000980 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000981
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 return Lex(Result);
983 }
984
Chris Lattnerd01e2912006-06-18 16:22:51 +0000985 Result.StartToken();
986 CurLexer->BufferPtr = CurLexer->BufferEnd;
987 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000988 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000989
990 // We're done with the #included file.
991 delete CurLexer;
992 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000993
994 // This is the end of the top-level file.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000995 Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000996}
997
998/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000999/// the current macro line.
1000void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001001 assert(CurMacroExpander && !CurLexer &&
1002 "Ending a macro when currently in a #include file!");
1003
1004 // Mark macro not ignored now that it is no longer being expanded.
1005 CurMacroExpander->getMacro().EnableMacro();
1006 delete CurMacroExpander;
1007
Chris Lattner69772b02006-07-02 20:34:39 +00001008 // Handle this like a #include file being popped off the stack.
1009 CurMacroExpander = 0;
1010 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001011}
1012
1013
1014//===----------------------------------------------------------------------===//
1015// Utility Methods for Preprocessor Directive Handling.
1016//===----------------------------------------------------------------------===//
1017
1018/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1019/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001020void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +00001021 LexerToken Tmp;
1022 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001023 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001024 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001025}
1026
1027/// ReadMacroName - Lex and validate a macro name, which occurs after a
1028/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001029/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1030/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001031/// else (e.g. #ifdef).
Chris Lattnere8eef322006-07-08 07:01:00 +00001032void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001033 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001034 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001035
1036 // Missing macro name?
1037 if (MacroNameTok.getKind() == tok::eom)
1038 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1039
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001040 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1041 if (II == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001042 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001043 // Fall through on error.
1044 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001045 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +00001046
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001047 } else if (isDefineUndef && II->getName()[0] == 'd' && // defined
1048 !strcmp(II->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001049 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001050 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001051 } else if (isDefineUndef && II->getMacroInfo() &&
1052 II->getMacroInfo()->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001053 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001054 if (isDefineUndef == 1)
1055 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1056 else
1057 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001058 } else {
1059 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001060 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001061 }
1062
Chris Lattner22eb9722006-06-18 05:43:12 +00001063 // Invalid macro name, read and discard the rest of the line. Then set the
1064 // token kind to tok::eom.
1065 MacroNameTok.SetKind(tok::eom);
1066 return DiscardUntilEndOfDirective();
1067}
1068
1069/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1070/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001071void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001072 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001073 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 // There should be no tokens after the directive, but we allow them as an
1075 // extension.
1076 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +00001077 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1078 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001079 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001080}
1081
1082
1083
1084/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1085/// decided that the subsequent tokens are in the #if'd out portion of the
1086/// file. Lex the rest of the file, until we see an #endif. If
1087/// FoundNonSkipPortion is true, then we have already emitted code for part of
1088/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1089/// is true, then #else directives are ok, if not, then we have already seen one
1090/// so a #else directive is a duplicate. When this returns, the caller can lex
1091/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001092void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001093 bool FoundNonSkipPortion,
1094 bool FoundElse) {
1095 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001096 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001097 "Lexing a macro, not a file?");
1098
1099 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1100 FoundNonSkipPortion, FoundElse);
1101
1102 // Know that we are going to be skipping tokens. Set this flag to indicate
1103 // this, which has a couple of effects:
1104 // 1. If EOF of the current lexer is found, the include stack isn't popped.
1105 // 2. Identifier information is not looked up for identifier tokens. As an
1106 // effect of this, implicit macro expansion is naturally disabled.
1107 // 3. "#" tokens at the start of a line are treated as normal tokens, not
1108 // implicitly transformed by the lexer.
1109 // 4. All notes, warnings, and extension messages are disabled.
1110 //
1111 SkippingContents = true;
1112 LexerToken Tok;
1113 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001114 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001115
1116 // If this is the end of the buffer, we have an error. The lexer will have
1117 // already handled this error condition, so just return and let the caller
1118 // lex after this #include.
1119 if (Tok.getKind() == tok::eof) break;
1120
1121 // If this token is not a preprocessor directive, just skip it.
1122 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
1123 continue;
1124
1125 // We just parsed a # character at the start of a line, so we're in
1126 // directive mode. Tell the lexer this so any newlines we see will be
1127 // converted into an EOM token (this terminates the macro).
1128 CurLexer->ParsingPreprocessorDirective = true;
1129
1130 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001131 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001132
1133 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1134 // something bogus), skip it.
1135 if (Tok.getKind() != tok::identifier) {
1136 CurLexer->ParsingPreprocessorDirective = false;
1137 continue;
1138 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001139
Chris Lattner22eb9722006-06-18 05:43:12 +00001140 // If the first letter isn't i or e, it isn't intesting to us. We know that
1141 // this is safe in the face of spelling differences, because there is no way
1142 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001143 // allows us to avoid looking up the identifier info for #define/#undef and
1144 // other common directives.
1145 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1146 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001147 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1148 FirstChar != 'i' && FirstChar != 'e') {
1149 CurLexer->ParsingPreprocessorDirective = false;
1150 continue;
1151 }
1152
Chris Lattnere60165f2006-06-22 06:36:29 +00001153 // Get the identifier name without trigraphs or embedded newlines. Note
1154 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1155 // when skipping.
1156 // TODO: could do this with zero copies in the no-clean case by using
1157 // strncmp below.
1158 char Directive[20];
1159 unsigned IdLen;
1160 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1161 IdLen = Tok.getLength();
1162 memcpy(Directive, RawCharData, IdLen);
1163 Directive[IdLen] = 0;
1164 } else {
1165 std::string DirectiveStr = getSpelling(Tok);
1166 IdLen = DirectiveStr.size();
1167 if (IdLen >= 20) {
1168 CurLexer->ParsingPreprocessorDirective = false;
1169 continue;
1170 }
1171 memcpy(Directive, &DirectiveStr[0], IdLen);
1172 Directive[IdLen] = 0;
1173 }
1174
Chris Lattner22eb9722006-06-18 05:43:12 +00001175 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001176 if ((IdLen == 2) || // "if"
1177 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1178 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001179 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1180 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001181 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001182 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001183 /*foundnonskip*/false,
1184 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001185 }
1186 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001187 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001188 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001189 PPConditionalInfo CondInfo;
1190 CondInfo.WasSkipping = true; // Silence bogus warning.
1191 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1192 assert(!InCond && "Can't be skipping if not in a conditional!");
1193
1194 // If we popped the outermost skipping block, we're done skipping!
1195 if (!CondInfo.WasSkipping)
1196 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001197 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 // #else directive in a skipping conditional. If not in some other
1199 // skipping conditional, and if #else hasn't already been seen, enter it
1200 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001201 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001202 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1203
1204 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001205 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001206
1207 // Note that we've seen a #else in this conditional.
1208 CondInfo.FoundElse = true;
1209
1210 // If the conditional is at the top level, and the #if block wasn't
1211 // entered, enter the #else block now.
1212 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1213 CondInfo.FoundNonSkip = true;
1214 break;
1215 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001216 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001217 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1218
1219 bool ShouldEnter;
1220 // If this is in a skipping block or if we're already handled this #if
1221 // block, don't bother parsing the condition.
1222 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001223 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001224 ShouldEnter = false;
1225 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 // Restore the value of SkippingContents so that identifiers are
1227 // looked up, etc, inside the #elif expression.
1228 assert(SkippingContents && "We have to be skipping here!");
1229 SkippingContents = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001230 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001231 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001232 SkippingContents = true;
1233 }
1234
1235 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001236 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001237
1238 // If this condition is true, enter it!
1239 if (ShouldEnter) {
1240 CondInfo.FoundNonSkip = true;
1241 break;
1242 }
1243 }
1244 }
1245
1246 CurLexer->ParsingPreprocessorDirective = false;
1247 }
1248
1249 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1250 // of the file, just stop skipping and return to lexing whatever came after
1251 // the #if block.
1252 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001253}
1254
1255//===----------------------------------------------------------------------===//
1256// Preprocessor Directive Handling.
1257//===----------------------------------------------------------------------===//
1258
1259/// HandleDirective - This callback is invoked when the lexer sees a # token
1260/// at the start of a line. This consumes the directive, modifies the
1261/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1262/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001263void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001264 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001265
1266 // We just parsed a # character at the start of a line, so we're in directive
1267 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001268 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001269 CurLexer->ParsingPreprocessorDirective = true;
1270
1271 ++NumDirectives;
1272
Chris Lattner371ac8a2006-07-04 07:11:10 +00001273 // We are about to read a token. For the multiple-include optimization FA to
1274 // work, we have to remember if we had read any tokens *before* this
1275 // pp-directive.
1276 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1277
Chris Lattner78186052006-07-09 00:45:31 +00001278 // Read the next token, the directive flavor. This isn't expanded due to
1279 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001280 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001281
Chris Lattner78186052006-07-09 00:45:31 +00001282 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1283 // #define A(x) #x
1284 // A(abc
1285 // #warning blah
1286 // def)
1287 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1288 if (InMacroFormalArgs)
1289 Diag(Result, diag::ext_embedded_directive);
1290
Chris Lattner22eb9722006-06-18 05:43:12 +00001291 switch (Result.getKind()) {
1292 default: break;
1293 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001294 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001295
1296#if 0
1297 case tok::numeric_constant:
1298 // FIXME: implement # 7 line numbers!
1299 break;
1300#endif
1301 case tok::kw_else:
1302 return HandleElseDirective(Result);
1303 case tok::kw_if:
Chris Lattnera8654ca2006-07-04 17:42:08 +00001304 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001306 // Get the identifier name without trigraphs or embedded newlines.
1307 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001308 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001309 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001311 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnera8654ca2006-07-04 17:42:08 +00001312 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001313 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001314 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001315 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1316 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001317 break;
1318 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001319 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001320 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001321 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001322 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
Chris Lattner40931922006-06-22 06:14:04 +00001323 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001324 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001325 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001326 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001327 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001328 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 break;
1330 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001331 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001332 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001333 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001334 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
Chris Lattner40931922006-06-22 06:14:04 +00001335 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001336 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001337 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001338 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001339 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1340 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001341 break;
1342 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001343 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1344 return HandleIncludeDirective(Result); // Handle #include.
1345 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001346 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001347 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001348 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 break;
1350 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001351 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001352 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001353 }
1354 break;
1355 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001356 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1357 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001358 break;
1359 }
1360 break;
1361 }
1362
1363 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001364 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365
1366 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001367 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001368
1369 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001370}
1371
Chris Lattner01d66cc2006-07-03 22:16:27 +00001372void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001373 bool isWarning) {
1374 // Read the rest of the line raw. We do this because we don't want macros
1375 // to be expanded and we don't require that the tokens be valid preprocessing
1376 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1377 // collapse multiple consequtive white space between tokens, but this isn't
1378 // specified by the standard.
1379 std::string Message = CurLexer->ReadToEndOfLine();
1380
1381 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001382 return Diag(Tok, DiagID, Message);
1383}
1384
1385/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1386///
1387void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001388 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001389 Diag(Tok, diag::ext_pp_ident_directive);
1390
Chris Lattner371ac8a2006-07-04 07:11:10 +00001391 // Read the string argument.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001392 LexerToken StrTok;
1393 Lex(StrTok);
1394
1395 // If the token kind isn't a string, it's a malformed directive.
1396 if (StrTok.getKind() != tok::string_literal)
1397 return Diag(StrTok, diag::err_pp_malformed_ident);
1398
1399 // Verify that there is nothing after the string, other than EOM.
1400 CheckEndOfDirective("#ident");
1401
1402 if (IdentHandler)
1403 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001404}
1405
Chris Lattnerb8761832006-06-24 21:31:03 +00001406//===----------------------------------------------------------------------===//
1407// Preprocessor Include Directive Handling.
1408//===----------------------------------------------------------------------===//
1409
Chris Lattner22eb9722006-06-18 05:43:12 +00001410/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1411/// file to be included from the lexer, then include it! This is a common
1412/// routine with functionality shared between #include, #include_next and
1413/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001414void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 const DirectoryLookup *LookupFrom,
1416 bool isImport) {
1417 ++NumIncluded;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001418
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001420 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001421
1422 // If the token kind is EOM, the error has already been diagnosed.
1423 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001424 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001425
1426 // Verify that there is nothing after the filename, other than EOM. Use the
1427 // preprocessor to lex this in case lexing the filename entered a macro.
1428 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001429
1430 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001431 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1433
Chris Lattner269c2322006-06-25 06:23:00 +00001434 // Find out whether the filename is <x> or "x".
1435 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001436
1437 // Remove the quotes.
1438 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1439
Chris Lattner22eb9722006-06-18 05:43:12 +00001440 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001441 const DirectoryLookup *CurDir;
1442 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 if (File == 0)
1444 return Diag(FilenameTok, diag::err_pp_file_not_found);
1445
1446 // Get information about this file.
1447 PerFileInfo &FileInfo = getFileInfo(File);
1448
1449 // If this is a #import directive, check that we have not already imported
1450 // this header.
1451 if (isImport) {
1452 // If this has already been imported, don't import it again.
1453 FileInfo.isImport = true;
1454
1455 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001456 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001457 } else {
1458 // Otherwise, if this is a #include of a file that was previously #import'd
1459 // or if this is the second #include of a #pragma once file, ignore it.
1460 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001461 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001462 }
Chris Lattner3665f162006-07-04 07:26:10 +00001463
1464 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1465 // if the macro that guards it is defined, we know the #include has no effect.
1466 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
1467 ++NumMultiIncludeFileOptzn;
1468 return;
1469 }
1470
Chris Lattner22eb9722006-06-18 05:43:12 +00001471
1472 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001473 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001474 if (FileID == 0)
1475 return Diag(FilenameTok, diag::err_pp_file_not_found);
1476
1477 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001478 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479
1480 // Increment the number of times this file has been included.
1481 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001482}
1483
1484/// HandleIncludeNextDirective - Implements #include_next.
1485///
Chris Lattnercb283342006-06-18 06:48:37 +00001486void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1487 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001488
1489 // #include_next is like #include, except that we start searching after
1490 // the current found directory. If we can't do this, issue a
1491 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001492 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001493 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001494 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001495 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001496 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001497 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001498 } else {
1499 // Start looking up in the next directory.
1500 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001501 }
1502
1503 return HandleIncludeDirective(IncludeNextTok, Lookup);
1504}
1505
1506/// HandleImportDirective - Implements #import.
1507///
Chris Lattnercb283342006-06-18 06:48:37 +00001508void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1509 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001510
1511 return HandleIncludeDirective(ImportTok, 0, true);
1512}
1513
Chris Lattnerb8761832006-06-24 21:31:03 +00001514//===----------------------------------------------------------------------===//
1515// Preprocessor Macro Directive Handling.
1516//===----------------------------------------------------------------------===//
1517
Chris Lattnercefc7682006-07-08 08:28:12 +00001518/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1519/// definition has just been read. Lex the rest of the arguments and the
1520/// closing ), updating MI with what we learn. Return true if an error occurs
1521/// parsing the arg list.
1522bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1523 LexerToken Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00001524 while (1) {
1525 LexUnexpandedToken(Tok);
1526 switch (Tok.getKind()) {
1527 case tok::r_paren:
1528 // Found the end of the argument list.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001529 if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
Chris Lattnercefc7682006-07-08 08:28:12 +00001530 // Otherwise we have #define FOO(A,)
1531 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1532 return true;
1533 case tok::ellipsis: // #define X(... -> C99 varargs
1534 // Warn if use of C99 feature in non-C99 mode.
1535 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1536
1537 // Lex the token after the identifier.
1538 LexUnexpandedToken(Tok);
1539 if (Tok.getKind() != tok::r_paren) {
1540 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1541 return true;
1542 }
1543 MI->setIsC99Varargs();
1544 return false;
1545 case tok::eom: // #define X(
1546 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1547 return true;
1548 default: // #define X(1
1549 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1550 return true;
1551 case tok::identifier:
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001552 IdentifierInfo *II = Tok.getIdentifierInfo();
1553
1554 // If this is already used as an argument, it is used multiple times (e.g.
1555 // #define X(A,A.
1556 if (II->isMacroArg()) { // C99 6.10.3p6
1557 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
1558 return true;
1559 }
1560
1561 // Add the argument to the macro info.
1562 MI->addArgument(II);
1563 // Remember it is an argument now.
1564 II->setIsMacroArg(true);
Chris Lattnercefc7682006-07-08 08:28:12 +00001565
1566 // Lex the token after the identifier.
1567 LexUnexpandedToken(Tok);
1568
1569 switch (Tok.getKind()) {
1570 default: // #define X(A B
1571 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1572 return true;
1573 case tok::r_paren: // #define X(A)
1574 return false;
1575 case tok::comma: // #define X(A,
1576 break;
1577 case tok::ellipsis: // #define X(A... -> GCC extension
1578 // Diagnose extension.
1579 Diag(Tok, diag::ext_named_variadic_macro);
1580
1581 // Lex the token after the identifier.
1582 LexUnexpandedToken(Tok);
1583 if (Tok.getKind() != tok::r_paren) {
1584 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1585 return true;
1586 }
1587
1588 MI->setIsGNUVarargs();
1589 return false;
1590 }
1591 }
1592 }
1593}
1594
Chris Lattner22eb9722006-06-18 05:43:12 +00001595/// HandleDefineDirective - Implements #define. This consumes the entire macro
1596/// line then lets the caller lex the next real token.
1597///
Chris Lattnercb283342006-06-18 06:48:37 +00001598void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001599 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001600
Chris Lattner22eb9722006-06-18 05:43:12 +00001601 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001602 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00001603
1604 // Error reading macro name? If so, diagnostic already issued.
1605 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001606 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001607
Chris Lattner50b497e2006-06-18 16:32:35 +00001608 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001609
1610 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001611 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001612
Chris Lattner78186052006-07-09 00:45:31 +00001613 // FIXME: Enable __VA_ARGS__.
1614
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001615 // If this is a function-like macro definition, parse the argument list,
1616 // marking each of the identifiers as being used as macro arguments. Also,
1617 // check other constraints on the first token of the macro body.
Chris Lattner22eb9722006-06-18 05:43:12 +00001618 if (Tok.getKind() == tok::eom) {
1619 // If there is no body to this macro, we have no special handling here.
1620 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00001621 // This is a function-like macro definition. Read the argument list.
1622 MI->setIsFunctionLike();
1623 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001624 // Clear the "isMacroArg" flags from all the macro arguments parsed.
1625 MI->SetIdentifierIsMacroArgFlags(false);
1626 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00001627 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001628 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00001629 if (CurLexer->ParsingPreprocessorDirective)
1630 DiscardUntilEndOfDirective();
1631 return;
1632 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001633
Chris Lattner815a1f92006-07-08 20:48:04 +00001634 // Read the first token after the arg list for down below.
1635 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001636 } else if (!Tok.hasLeadingSpace()) {
1637 // C99 requires whitespace between the macro definition and the body. Emit
1638 // a diagnostic for something like "#define X+".
1639 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001640 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001641 } else {
1642 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1643 // one in some cases!
1644 }
1645 } else {
1646 // This is a normal token with leading space. Clear the leading space
1647 // marker on the first token to get proper expansion.
1648 Tok.ClearFlag(LexerToken::LeadingSpace);
1649 }
1650
1651 // Read the rest of the macro body.
1652 while (Tok.getKind() != tok::eom) {
1653 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00001654
1655 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
1656 // parameters.
1657 if (Tok.getKind() != tok::hash) {
1658 // Get the next token of the macro.
1659 LexUnexpandedToken(Tok);
1660 continue;
1661 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001662
Chris Lattner815a1f92006-07-08 20:48:04 +00001663 // Get the next token of the macro.
1664 LexUnexpandedToken(Tok);
1665
1666 // Not a macro arg identifier?
1667 if (!Tok.getIdentifierInfo() || !Tok.getIdentifierInfo()->isMacroArg()) {
1668 Diag(Tok, diag::err_pp_stringize_not_parameter);
1669 // Clear the "isMacroArg" flags from all the macro arguments.
1670 MI->SetIdentifierIsMacroArgFlags(false);
1671 delete MI;
1672 return;
1673 }
1674
1675 // Things look ok, add the param name token to the macro.
1676 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001677
Chris Lattner22eb9722006-06-18 05:43:12 +00001678 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001679 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001680 }
Chris Lattnerbff18d52006-07-06 04:49:18 +00001681
Chris Lattner78186052006-07-09 00:45:31 +00001682 // Clear the "isMacroArg" flags from all the macro arguments.
1683 MI->SetIdentifierIsMacroArgFlags(false);
1684
Chris Lattnerbff18d52006-07-06 04:49:18 +00001685 // Check that there is no paste (##) operator at the begining or end of the
1686 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00001687 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00001688 if (NumTokens != 0) {
1689 if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001690 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001691 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001692 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001693 }
1694 if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001695 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001696 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001697 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001698 }
1699 }
1700
Chris Lattner13044d92006-07-03 05:16:44 +00001701 // If this is the primary source file, remember that this macro hasn't been
1702 // used yet.
1703 if (isInPrimaryFile())
1704 MI->setIsUsed(false);
1705
Chris Lattner22eb9722006-06-18 05:43:12 +00001706 // Finally, if this identifier already had a macro defined for it, verify that
1707 // the macro bodies are identical and free the old definition.
1708 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001709 if (!OtherMI->isUsed())
1710 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1711
Chris Lattner22eb9722006-06-18 05:43:12 +00001712 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00001713 // must be the same. C99 6.10.3.2.
1714 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00001715 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
1716 MacroNameTok.getIdentifierInfo()->getName());
1717 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
1718 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001719 delete OtherMI;
1720 }
1721
1722 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001723}
1724
1725
1726/// HandleUndefDirective - Implements #undef.
1727///
Chris Lattnercb283342006-06-18 06:48:37 +00001728void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001729 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001730
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001732 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00001733
1734 // Error reading macro name? If so, diagnostic already issued.
1735 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001736 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001737
1738 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001739 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001740
1741 // Okay, we finally have a valid identifier to undef.
1742 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1743
1744 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001745 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001746
Chris Lattner13044d92006-07-03 05:16:44 +00001747 if (!MI->isUsed())
1748 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001749
1750 // Free macro definition.
1751 delete MI;
1752 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001753}
1754
1755
Chris Lattnerb8761832006-06-24 21:31:03 +00001756//===----------------------------------------------------------------------===//
1757// Preprocessor Conditional Directive Handling.
1758//===----------------------------------------------------------------------===//
1759
Chris Lattner22eb9722006-06-18 05:43:12 +00001760/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00001761/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1762/// if any tokens have been returned or pp-directives activated before this
1763/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00001764///
Chris Lattner371ac8a2006-07-04 07:11:10 +00001765void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1766 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001767 ++NumIf;
1768 LexerToken DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001769
Chris Lattner22eb9722006-06-18 05:43:12 +00001770 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001771 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001772
1773 // Error reading macro name? If so, diagnostic already issued.
1774 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001775 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001776
1777 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001778 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1779
1780 // If the start of a top-level #ifdef, inform MIOpt.
1781 if (!ReadAnyTokensBeforeDirective &&
1782 CurLexer->getConditionalStackDepth() == 0) {
1783 assert(isIfndef && "#ifdef shouldn't reach here");
1784 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1785 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001786
Chris Lattnera78a97e2006-07-03 05:42:18 +00001787 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1788
1789 // If there is a macro, mark it used.
1790 if (MI) MI->setIsUsed(true);
1791
Chris Lattner22eb9722006-06-18 05:43:12 +00001792 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001793 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001794 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001795 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001796 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001797 } else {
1798 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001799 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001800 /*Foundnonskip*/false,
1801 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001802 }
1803}
1804
1805/// HandleIfDirective - Implements the #if directive.
1806///
Chris Lattnera8654ca2006-07-04 17:42:08 +00001807void Preprocessor::HandleIfDirective(LexerToken &IfToken,
1808 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001809 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001810
Chris Lattner371ac8a2006-07-04 07:11:10 +00001811 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001812 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001813 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001814
1815 // Should we include the stuff contained by this directive?
1816 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00001817 // If this condition is equivalent to #ifndef X, and if this is the first
1818 // directive seen, handle it for the multiple-include optimization.
1819 if (!ReadAnyTokensBeforeDirective &&
1820 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
1821 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1822
Chris Lattner22eb9722006-06-18 05:43:12 +00001823 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001824 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001825 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001826 } else {
1827 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001828 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001829 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001830 }
1831}
1832
1833/// HandleEndifDirective - Implements the #endif directive.
1834///
Chris Lattnercb283342006-06-18 06:48:37 +00001835void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001836 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001837
Chris Lattner22eb9722006-06-18 05:43:12 +00001838 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001839 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001840
1841 PPConditionalInfo CondInfo;
1842 if (CurLexer->popConditionalLevel(CondInfo)) {
1843 // No conditionals on the stack: this is an #endif without an #if.
1844 return Diag(EndifToken, diag::err_pp_endif_without_if);
1845 }
1846
Chris Lattner371ac8a2006-07-04 07:11:10 +00001847 // If this the end of a top-level #endif, inform MIOpt.
1848 if (CurLexer->getConditionalStackDepth() == 0)
1849 CurLexer->MIOpt.ExitTopLevelConditional();
1850
Chris Lattner22eb9722006-06-18 05:43:12 +00001851 assert(!CondInfo.WasSkipping && !isSkipping() &&
1852 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001853}
1854
1855
Chris Lattnercb283342006-06-18 06:48:37 +00001856void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001857 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001858
Chris Lattner22eb9722006-06-18 05:43:12 +00001859 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001860 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001861
1862 PPConditionalInfo CI;
1863 if (CurLexer->popConditionalLevel(CI))
1864 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001865
1866 // If this is a top-level #else, inform the MIOpt.
1867 if (CurLexer->getConditionalStackDepth() == 0)
1868 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00001869
1870 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001871 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001872
1873 // Finally, skip the rest of the contents of this block and return the first
1874 // token after it.
1875 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1876 /*FoundElse*/true);
1877}
1878
Chris Lattnercb283342006-06-18 06:48:37 +00001879void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001880 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001881
Chris Lattner22eb9722006-06-18 05:43:12 +00001882 // #elif directive in a non-skipping conditional... start skipping.
1883 // We don't care what the condition is, because we will always skip it (since
1884 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001885 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001886
1887 PPConditionalInfo CI;
1888 if (CurLexer->popConditionalLevel(CI))
1889 return Diag(ElifToken, diag::pp_err_elif_without_if);
1890
Chris Lattner371ac8a2006-07-04 07:11:10 +00001891 // If this is a top-level #elif, inform the MIOpt.
1892 if (CurLexer->getConditionalStackDepth() == 0)
1893 CurLexer->MIOpt.FoundTopLevelElse();
1894
Chris Lattner22eb9722006-06-18 05:43:12 +00001895 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001896 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001897
1898 // Finally, skip the rest of the contents of this block and return the first
1899 // token after it.
1900 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1901 /*FoundElse*/CI.FoundElse);
1902}
Chris Lattnerb8761832006-06-24 21:31:03 +00001903