blob: 420a2b807bd87af3dd59e7e54557c25c01bcb35d [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
Chris Lattner03f83482006-07-10 06:16:26 +0000994 // This is the end of the top-level file. If the diag::pp_macro_not_used
995 // diagnostic is enabled, walk all of the identifiers, looking for macros that
996 // have not been used.
997 if (Diags.getDiagnosticLevel(diag::pp_macro_not_used) != Diagnostic::Ignored)
998 Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000999}
1000
1001/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +00001002/// the current macro line.
1003void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001004 assert(CurMacroExpander && !CurLexer &&
1005 "Ending a macro when currently in a #include file!");
1006
1007 // Mark macro not ignored now that it is no longer being expanded.
1008 CurMacroExpander->getMacro().EnableMacro();
1009 delete CurMacroExpander;
1010
Chris Lattner69772b02006-07-02 20:34:39 +00001011 // Handle this like a #include file being popped off the stack.
1012 CurMacroExpander = 0;
1013 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001014}
1015
1016
1017//===----------------------------------------------------------------------===//
1018// Utility Methods for Preprocessor Directive Handling.
1019//===----------------------------------------------------------------------===//
1020
1021/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
1022/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +00001023void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +00001024 LexerToken Tmp;
1025 do {
Chris Lattnercb283342006-06-18 06:48:37 +00001026 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001027 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001028}
1029
1030/// ReadMacroName - Lex and validate a macro name, which occurs after a
1031/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001032/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1033/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001034/// else (e.g. #ifdef).
Chris Lattnere8eef322006-07-08 07:01:00 +00001035void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001036 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001037 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001038
1039 // Missing macro name?
1040 if (MacroNameTok.getKind() == tok::eom)
1041 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1042
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001043 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1044 if (II == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001045 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001046 // Fall through on error.
1047 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001048 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +00001049
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001050 } else if (isDefineUndef && II->getName()[0] == 'd' && // defined
1051 !strcmp(II->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001052 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001053 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001054 } else if (isDefineUndef && II->getMacroInfo() &&
1055 II->getMacroInfo()->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001056 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001057 if (isDefineUndef == 1)
1058 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1059 else
1060 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001061 } else {
1062 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001063 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001064 }
1065
Chris Lattner22eb9722006-06-18 05:43:12 +00001066 // Invalid macro name, read and discard the rest of the line. Then set the
1067 // token kind to tok::eom.
1068 MacroNameTok.SetKind(tok::eom);
1069 return DiscardUntilEndOfDirective();
1070}
1071
1072/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1073/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001074void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001075 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001076 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001077 // There should be no tokens after the directive, but we allow them as an
1078 // extension.
1079 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +00001080 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1081 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001082 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001083}
1084
1085
1086
1087/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1088/// decided that the subsequent tokens are in the #if'd out portion of the
1089/// file. Lex the rest of the file, until we see an #endif. If
1090/// FoundNonSkipPortion is true, then we have already emitted code for part of
1091/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1092/// is true, then #else directives are ok, if not, then we have already seen one
1093/// so a #else directive is a duplicate. When this returns, the caller can lex
1094/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001095void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001096 bool FoundNonSkipPortion,
1097 bool FoundElse) {
1098 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001099 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 "Lexing a macro, not a file?");
1101
1102 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1103 FoundNonSkipPortion, FoundElse);
1104
1105 // Know that we are going to be skipping tokens. Set this flag to indicate
1106 // this, which has a couple of effects:
1107 // 1. If EOF of the current lexer is found, the include stack isn't popped.
1108 // 2. Identifier information is not looked up for identifier tokens. As an
1109 // effect of this, implicit macro expansion is naturally disabled.
1110 // 3. "#" tokens at the start of a line are treated as normal tokens, not
1111 // implicitly transformed by the lexer.
1112 // 4. All notes, warnings, and extension messages are disabled.
1113 //
1114 SkippingContents = true;
1115 LexerToken Tok;
1116 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001117 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001118
1119 // If this is the end of the buffer, we have an error. The lexer will have
1120 // already handled this error condition, so just return and let the caller
1121 // lex after this #include.
1122 if (Tok.getKind() == tok::eof) break;
1123
1124 // If this token is not a preprocessor directive, just skip it.
1125 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
1126 continue;
1127
1128 // We just parsed a # character at the start of a line, so we're in
1129 // directive mode. Tell the lexer this so any newlines we see will be
1130 // converted into an EOM token (this terminates the macro).
1131 CurLexer->ParsingPreprocessorDirective = true;
1132
1133 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001134 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001135
1136 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1137 // something bogus), skip it.
1138 if (Tok.getKind() != tok::identifier) {
1139 CurLexer->ParsingPreprocessorDirective = false;
1140 continue;
1141 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001142
Chris Lattner22eb9722006-06-18 05:43:12 +00001143 // If the first letter isn't i or e, it isn't intesting to us. We know that
1144 // this is safe in the face of spelling differences, because there is no way
1145 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001146 // allows us to avoid looking up the identifier info for #define/#undef and
1147 // other common directives.
1148 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1149 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001150 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1151 FirstChar != 'i' && FirstChar != 'e') {
1152 CurLexer->ParsingPreprocessorDirective = false;
1153 continue;
1154 }
1155
Chris Lattnere60165f2006-06-22 06:36:29 +00001156 // Get the identifier name without trigraphs or embedded newlines. Note
1157 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1158 // when skipping.
1159 // TODO: could do this with zero copies in the no-clean case by using
1160 // strncmp below.
1161 char Directive[20];
1162 unsigned IdLen;
1163 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1164 IdLen = Tok.getLength();
1165 memcpy(Directive, RawCharData, IdLen);
1166 Directive[IdLen] = 0;
1167 } else {
1168 std::string DirectiveStr = getSpelling(Tok);
1169 IdLen = DirectiveStr.size();
1170 if (IdLen >= 20) {
1171 CurLexer->ParsingPreprocessorDirective = false;
1172 continue;
1173 }
1174 memcpy(Directive, &DirectiveStr[0], IdLen);
1175 Directive[IdLen] = 0;
1176 }
1177
Chris Lattner22eb9722006-06-18 05:43:12 +00001178 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001179 if ((IdLen == 2) || // "if"
1180 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1181 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001182 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1183 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001184 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001185 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001186 /*foundnonskip*/false,
1187 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001188 }
1189 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001190 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001191 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001192 PPConditionalInfo CondInfo;
1193 CondInfo.WasSkipping = true; // Silence bogus warning.
1194 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1195 assert(!InCond && "Can't be skipping if not in a conditional!");
1196
1197 // If we popped the outermost skipping block, we're done skipping!
1198 if (!CondInfo.WasSkipping)
1199 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001200 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 // #else directive in a skipping conditional. If not in some other
1202 // skipping conditional, and if #else hasn't already been seen, enter it
1203 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001204 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1206
1207 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001208 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001209
1210 // Note that we've seen a #else in this conditional.
1211 CondInfo.FoundElse = true;
1212
1213 // If the conditional is at the top level, and the #if block wasn't
1214 // entered, enter the #else block now.
1215 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1216 CondInfo.FoundNonSkip = true;
1217 break;
1218 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001219 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1221
1222 bool ShouldEnter;
1223 // If this is in a skipping block or if we're already handled this #if
1224 // block, don't bother parsing the condition.
1225 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001226 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001227 ShouldEnter = false;
1228 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001229 // Restore the value of SkippingContents so that identifiers are
1230 // looked up, etc, inside the #elif expression.
1231 assert(SkippingContents && "We have to be skipping here!");
1232 SkippingContents = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001233 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001234 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001235 SkippingContents = true;
1236 }
1237
1238 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001239 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001240
1241 // If this condition is true, enter it!
1242 if (ShouldEnter) {
1243 CondInfo.FoundNonSkip = true;
1244 break;
1245 }
1246 }
1247 }
1248
1249 CurLexer->ParsingPreprocessorDirective = false;
1250 }
1251
1252 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1253 // of the file, just stop skipping and return to lexing whatever came after
1254 // the #if block.
1255 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001256}
1257
1258//===----------------------------------------------------------------------===//
1259// Preprocessor Directive Handling.
1260//===----------------------------------------------------------------------===//
1261
1262/// HandleDirective - This callback is invoked when the lexer sees a # token
1263/// at the start of a line. This consumes the directive, modifies the
1264/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1265/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001266void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001267 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001268
1269 // We just parsed a # character at the start of a line, so we're in directive
1270 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001271 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 CurLexer->ParsingPreprocessorDirective = true;
1273
1274 ++NumDirectives;
1275
Chris Lattner371ac8a2006-07-04 07:11:10 +00001276 // We are about to read a token. For the multiple-include optimization FA to
1277 // work, we have to remember if we had read any tokens *before* this
1278 // pp-directive.
1279 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1280
Chris Lattner78186052006-07-09 00:45:31 +00001281 // Read the next token, the directive flavor. This isn't expanded due to
1282 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001283 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001284
Chris Lattner78186052006-07-09 00:45:31 +00001285 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1286 // #define A(x) #x
1287 // A(abc
1288 // #warning blah
1289 // def)
1290 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1291 if (InMacroFormalArgs)
1292 Diag(Result, diag::ext_embedded_directive);
1293
Chris Lattner22eb9722006-06-18 05:43:12 +00001294 switch (Result.getKind()) {
1295 default: break;
1296 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001297 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001298
1299#if 0
1300 case tok::numeric_constant:
1301 // FIXME: implement # 7 line numbers!
1302 break;
1303#endif
1304 case tok::kw_else:
1305 return HandleElseDirective(Result);
1306 case tok::kw_if:
Chris Lattnera8654ca2006-07-04 17:42:08 +00001307 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
Chris Lattner22eb9722006-06-18 05:43:12 +00001308 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001309 // Get the identifier name without trigraphs or embedded newlines.
1310 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001311 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001312 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001313 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001314 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnera8654ca2006-07-04 17:42:08 +00001315 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001316 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001317 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001318 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1319 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001320 break;
1321 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001322 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001323 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001324 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001325 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
Chris Lattner40931922006-06-22 06:14:04 +00001326 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001327 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001328 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001330 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001331 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001332 break;
1333 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001334 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001335 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001336 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001337 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
Chris Lattner40931922006-06-22 06:14:04 +00001338 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001339 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001340 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001341 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001342 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1343 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001344 break;
1345 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001346 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1347 return HandleIncludeDirective(Result); // Handle #include.
1348 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001349 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001350 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001351 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 break;
1353 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001354 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001355 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001356 }
1357 break;
1358 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001359 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1360 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001361 break;
1362 }
1363 break;
1364 }
1365
1366 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001367 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001368
1369 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001370 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001371
1372 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001373}
1374
Chris Lattner01d66cc2006-07-03 22:16:27 +00001375void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001376 bool isWarning) {
1377 // Read the rest of the line raw. We do this because we don't want macros
1378 // to be expanded and we don't require that the tokens be valid preprocessing
1379 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1380 // collapse multiple consequtive white space between tokens, but this isn't
1381 // specified by the standard.
1382 std::string Message = CurLexer->ReadToEndOfLine();
1383
1384 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001385 return Diag(Tok, DiagID, Message);
1386}
1387
1388/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1389///
1390void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001391 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001392 Diag(Tok, diag::ext_pp_ident_directive);
1393
Chris Lattner371ac8a2006-07-04 07:11:10 +00001394 // Read the string argument.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001395 LexerToken StrTok;
1396 Lex(StrTok);
1397
1398 // If the token kind isn't a string, it's a malformed directive.
1399 if (StrTok.getKind() != tok::string_literal)
1400 return Diag(StrTok, diag::err_pp_malformed_ident);
1401
1402 // Verify that there is nothing after the string, other than EOM.
1403 CheckEndOfDirective("#ident");
1404
1405 if (IdentHandler)
1406 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001407}
1408
Chris Lattnerb8761832006-06-24 21:31:03 +00001409//===----------------------------------------------------------------------===//
1410// Preprocessor Include Directive Handling.
1411//===----------------------------------------------------------------------===//
1412
Chris Lattner22eb9722006-06-18 05:43:12 +00001413/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1414/// file to be included from the lexer, then include it! This is a common
1415/// routine with functionality shared between #include, #include_next and
1416/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001417void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001418 const DirectoryLookup *LookupFrom,
1419 bool isImport) {
1420 ++NumIncluded;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001421
Chris Lattner22eb9722006-06-18 05:43:12 +00001422 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001423 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001424
1425 // If the token kind is EOM, the error has already been diagnosed.
1426 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001427 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001428
1429 // Verify that there is nothing after the filename, other than EOM. Use the
1430 // preprocessor to lex this in case lexing the filename entered a macro.
1431 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001432
1433 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001434 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001435 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1436
Chris Lattner269c2322006-06-25 06:23:00 +00001437 // Find out whether the filename is <x> or "x".
1438 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001439
1440 // Remove the quotes.
1441 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1442
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001444 const DirectoryLookup *CurDir;
1445 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001446 if (File == 0)
1447 return Diag(FilenameTok, diag::err_pp_file_not_found);
1448
1449 // Get information about this file.
1450 PerFileInfo &FileInfo = getFileInfo(File);
1451
1452 // If this is a #import directive, check that we have not already imported
1453 // this header.
1454 if (isImport) {
1455 // If this has already been imported, don't import it again.
1456 FileInfo.isImport = true;
1457
1458 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001459 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001460 } else {
1461 // Otherwise, if this is a #include of a file that was previously #import'd
1462 // or if this is the second #include of a #pragma once file, ignore it.
1463 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001464 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001465 }
Chris Lattner3665f162006-07-04 07:26:10 +00001466
1467 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1468 // if the macro that guards it is defined, we know the #include has no effect.
1469 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
1470 ++NumMultiIncludeFileOptzn;
1471 return;
1472 }
1473
Chris Lattner22eb9722006-06-18 05:43:12 +00001474
1475 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001476 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001477 if (FileID == 0)
1478 return Diag(FilenameTok, diag::err_pp_file_not_found);
1479
1480 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001481 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001482
1483 // Increment the number of times this file has been included.
1484 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001485}
1486
1487/// HandleIncludeNextDirective - Implements #include_next.
1488///
Chris Lattnercb283342006-06-18 06:48:37 +00001489void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1490 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001491
1492 // #include_next is like #include, except that we start searching after
1493 // the current found directory. If we can't do this, issue a
1494 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001495 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001496 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001498 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001499 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001500 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001501 } else {
1502 // Start looking up in the next directory.
1503 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001504 }
1505
1506 return HandleIncludeDirective(IncludeNextTok, Lookup);
1507}
1508
1509/// HandleImportDirective - Implements #import.
1510///
Chris Lattnercb283342006-06-18 06:48:37 +00001511void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1512 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001513
1514 return HandleIncludeDirective(ImportTok, 0, true);
1515}
1516
Chris Lattnerb8761832006-06-24 21:31:03 +00001517//===----------------------------------------------------------------------===//
1518// Preprocessor Macro Directive Handling.
1519//===----------------------------------------------------------------------===//
1520
Chris Lattnercefc7682006-07-08 08:28:12 +00001521/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1522/// definition has just been read. Lex the rest of the arguments and the
1523/// closing ), updating MI with what we learn. Return true if an error occurs
1524/// parsing the arg list.
1525bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1526 LexerToken Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00001527 while (1) {
1528 LexUnexpandedToken(Tok);
1529 switch (Tok.getKind()) {
1530 case tok::r_paren:
1531 // Found the end of the argument list.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001532 if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
Chris Lattnercefc7682006-07-08 08:28:12 +00001533 // Otherwise we have #define FOO(A,)
1534 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1535 return true;
1536 case tok::ellipsis: // #define X(... -> C99 varargs
1537 // Warn if use of C99 feature in non-C99 mode.
1538 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1539
1540 // Lex the token after the identifier.
1541 LexUnexpandedToken(Tok);
1542 if (Tok.getKind() != tok::r_paren) {
1543 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1544 return true;
1545 }
1546 MI->setIsC99Varargs();
1547 return false;
1548 case tok::eom: // #define X(
1549 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1550 return true;
1551 default: // #define X(1
1552 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1553 return true;
1554 case tok::identifier:
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001555 IdentifierInfo *II = Tok.getIdentifierInfo();
1556
1557 // If this is already used as an argument, it is used multiple times (e.g.
1558 // #define X(A,A.
1559 if (II->isMacroArg()) { // C99 6.10.3p6
1560 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
1561 return true;
1562 }
1563
1564 // Add the argument to the macro info.
1565 MI->addArgument(II);
1566 // Remember it is an argument now.
1567 II->setIsMacroArg(true);
Chris Lattnercefc7682006-07-08 08:28:12 +00001568
1569 // Lex the token after the identifier.
1570 LexUnexpandedToken(Tok);
1571
1572 switch (Tok.getKind()) {
1573 default: // #define X(A B
1574 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1575 return true;
1576 case tok::r_paren: // #define X(A)
1577 return false;
1578 case tok::comma: // #define X(A,
1579 break;
1580 case tok::ellipsis: // #define X(A... -> GCC extension
1581 // Diagnose extension.
1582 Diag(Tok, diag::ext_named_variadic_macro);
1583
1584 // Lex the token after the identifier.
1585 LexUnexpandedToken(Tok);
1586 if (Tok.getKind() != tok::r_paren) {
1587 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1588 return true;
1589 }
1590
1591 MI->setIsGNUVarargs();
1592 return false;
1593 }
1594 }
1595 }
1596}
1597
Chris Lattner22eb9722006-06-18 05:43:12 +00001598/// HandleDefineDirective - Implements #define. This consumes the entire macro
1599/// line then lets the caller lex the next real token.
1600///
Chris Lattnercb283342006-06-18 06:48:37 +00001601void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001602 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001603
Chris Lattner22eb9722006-06-18 05:43:12 +00001604 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001605 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00001606
1607 // Error reading macro name? If so, diagnostic already issued.
1608 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001609 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001610
Chris Lattner50b497e2006-06-18 16:32:35 +00001611 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001612
1613 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001614 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001615
Chris Lattner78186052006-07-09 00:45:31 +00001616 // FIXME: Enable __VA_ARGS__.
1617
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001618 // If this is a function-like macro definition, parse the argument list,
1619 // marking each of the identifiers as being used as macro arguments. Also,
1620 // check other constraints on the first token of the macro body.
Chris Lattner22eb9722006-06-18 05:43:12 +00001621 if (Tok.getKind() == tok::eom) {
1622 // If there is no body to this macro, we have no special handling here.
1623 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00001624 // This is a function-like macro definition. Read the argument list.
1625 MI->setIsFunctionLike();
1626 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001627 // Clear the "isMacroArg" flags from all the macro arguments parsed.
1628 MI->SetIdentifierIsMacroArgFlags(false);
1629 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00001630 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001631 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00001632 if (CurLexer->ParsingPreprocessorDirective)
1633 DiscardUntilEndOfDirective();
1634 return;
1635 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001636
Chris Lattner815a1f92006-07-08 20:48:04 +00001637 // Read the first token after the arg list for down below.
1638 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001639 } else if (!Tok.hasLeadingSpace()) {
1640 // C99 requires whitespace between the macro definition and the body. Emit
1641 // a diagnostic for something like "#define X+".
1642 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001643 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001644 } else {
1645 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1646 // one in some cases!
1647 }
1648 } else {
1649 // This is a normal token with leading space. Clear the leading space
1650 // marker on the first token to get proper expansion.
1651 Tok.ClearFlag(LexerToken::LeadingSpace);
1652 }
1653
1654 // Read the rest of the macro body.
1655 while (Tok.getKind() != tok::eom) {
1656 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00001657
1658 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
1659 // parameters.
1660 if (Tok.getKind() != tok::hash) {
1661 // Get the next token of the macro.
1662 LexUnexpandedToken(Tok);
1663 continue;
1664 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001665
Chris Lattner815a1f92006-07-08 20:48:04 +00001666 // Get the next token of the macro.
1667 LexUnexpandedToken(Tok);
1668
1669 // Not a macro arg identifier?
1670 if (!Tok.getIdentifierInfo() || !Tok.getIdentifierInfo()->isMacroArg()) {
1671 Diag(Tok, diag::err_pp_stringize_not_parameter);
1672 // Clear the "isMacroArg" flags from all the macro arguments.
1673 MI->SetIdentifierIsMacroArgFlags(false);
1674 delete MI;
1675 return;
1676 }
1677
1678 // Things look ok, add the param name token to the macro.
1679 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001680
Chris Lattner22eb9722006-06-18 05:43:12 +00001681 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001682 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001683 }
Chris Lattnerbff18d52006-07-06 04:49:18 +00001684
Chris Lattner78186052006-07-09 00:45:31 +00001685 // Clear the "isMacroArg" flags from all the macro arguments.
1686 MI->SetIdentifierIsMacroArgFlags(false);
1687
Chris Lattnerbff18d52006-07-06 04:49:18 +00001688 // Check that there is no paste (##) operator at the begining or end of the
1689 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00001690 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00001691 if (NumTokens != 0) {
1692 if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001693 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001694 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001695 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001696 }
1697 if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001698 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001699 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001700 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001701 }
1702 }
1703
Chris Lattner13044d92006-07-03 05:16:44 +00001704 // If this is the primary source file, remember that this macro hasn't been
1705 // used yet.
1706 if (isInPrimaryFile())
1707 MI->setIsUsed(false);
1708
Chris Lattner22eb9722006-06-18 05:43:12 +00001709 // Finally, if this identifier already had a macro defined for it, verify that
1710 // the macro bodies are identical and free the old definition.
1711 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001712 if (!OtherMI->isUsed())
1713 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1714
Chris Lattner22eb9722006-06-18 05:43:12 +00001715 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00001716 // must be the same. C99 6.10.3.2.
1717 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00001718 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
1719 MacroNameTok.getIdentifierInfo()->getName());
1720 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
1721 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001722 delete OtherMI;
1723 }
1724
1725 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001726}
1727
1728
1729/// HandleUndefDirective - Implements #undef.
1730///
Chris Lattnercb283342006-06-18 06:48:37 +00001731void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001732 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001733
Chris Lattner22eb9722006-06-18 05:43:12 +00001734 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001735 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00001736
1737 // Error reading macro name? If so, diagnostic already issued.
1738 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001739 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001740
1741 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001742 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001743
1744 // Okay, we finally have a valid identifier to undef.
1745 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1746
1747 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001748 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001749
Chris Lattner13044d92006-07-03 05:16:44 +00001750 if (!MI->isUsed())
1751 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001752
1753 // Free macro definition.
1754 delete MI;
1755 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001756}
1757
1758
Chris Lattnerb8761832006-06-24 21:31:03 +00001759//===----------------------------------------------------------------------===//
1760// Preprocessor Conditional Directive Handling.
1761//===----------------------------------------------------------------------===//
1762
Chris Lattner22eb9722006-06-18 05:43:12 +00001763/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00001764/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1765/// if any tokens have been returned or pp-directives activated before this
1766/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00001767///
Chris Lattner371ac8a2006-07-04 07:11:10 +00001768void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1769 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001770 ++NumIf;
1771 LexerToken DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001772
Chris Lattner22eb9722006-06-18 05:43:12 +00001773 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001774 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001775
1776 // Error reading macro name? If so, diagnostic already issued.
1777 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001778 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001779
1780 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001781 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1782
1783 // If the start of a top-level #ifdef, inform MIOpt.
1784 if (!ReadAnyTokensBeforeDirective &&
1785 CurLexer->getConditionalStackDepth() == 0) {
1786 assert(isIfndef && "#ifdef shouldn't reach here");
1787 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1788 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001789
Chris Lattnera78a97e2006-07-03 05:42:18 +00001790 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1791
1792 // If there is a macro, mark it used.
1793 if (MI) MI->setIsUsed(true);
1794
Chris Lattner22eb9722006-06-18 05:43:12 +00001795 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001796 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001797 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001798 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001799 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001800 } else {
1801 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001802 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001803 /*Foundnonskip*/false,
1804 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001805 }
1806}
1807
1808/// HandleIfDirective - Implements the #if directive.
1809///
Chris Lattnera8654ca2006-07-04 17:42:08 +00001810void Preprocessor::HandleIfDirective(LexerToken &IfToken,
1811 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001812 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001813
Chris Lattner371ac8a2006-07-04 07:11:10 +00001814 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001815 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001816 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001817
1818 // Should we include the stuff contained by this directive?
1819 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00001820 // If this condition is equivalent to #ifndef X, and if this is the first
1821 // directive seen, handle it for the multiple-include optimization.
1822 if (!ReadAnyTokensBeforeDirective &&
1823 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
1824 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1825
Chris Lattner22eb9722006-06-18 05:43:12 +00001826 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001827 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001828 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001829 } else {
1830 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001831 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001832 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001833 }
1834}
1835
1836/// HandleEndifDirective - Implements the #endif directive.
1837///
Chris Lattnercb283342006-06-18 06:48:37 +00001838void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001839 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001840
Chris Lattner22eb9722006-06-18 05:43:12 +00001841 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001842 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001843
1844 PPConditionalInfo CondInfo;
1845 if (CurLexer->popConditionalLevel(CondInfo)) {
1846 // No conditionals on the stack: this is an #endif without an #if.
1847 return Diag(EndifToken, diag::err_pp_endif_without_if);
1848 }
1849
Chris Lattner371ac8a2006-07-04 07:11:10 +00001850 // If this the end of a top-level #endif, inform MIOpt.
1851 if (CurLexer->getConditionalStackDepth() == 0)
1852 CurLexer->MIOpt.ExitTopLevelConditional();
1853
Chris Lattner22eb9722006-06-18 05:43:12 +00001854 assert(!CondInfo.WasSkipping && !isSkipping() &&
1855 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001856}
1857
1858
Chris Lattnercb283342006-06-18 06:48:37 +00001859void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001860 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001861
Chris Lattner22eb9722006-06-18 05:43:12 +00001862 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001863 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001864
1865 PPConditionalInfo CI;
1866 if (CurLexer->popConditionalLevel(CI))
1867 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001868
1869 // If this is a top-level #else, inform the MIOpt.
1870 if (CurLexer->getConditionalStackDepth() == 0)
1871 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00001872
1873 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001874 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001875
1876 // Finally, skip the rest of the contents of this block and return the first
1877 // token after it.
1878 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1879 /*FoundElse*/true);
1880}
1881
Chris Lattnercb283342006-06-18 06:48:37 +00001882void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001883 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001884
Chris Lattner22eb9722006-06-18 05:43:12 +00001885 // #elif directive in a non-skipping conditional... start skipping.
1886 // We don't care what the condition is, because we will always skip it (since
1887 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001888 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001889
1890 PPConditionalInfo CI;
1891 if (CurLexer->popConditionalLevel(CI))
1892 return Diag(ElifToken, diag::pp_err_elif_without_if);
1893
Chris Lattner371ac8a2006-07-04 07:11:10 +00001894 // If this is a top-level #elif, inform the MIOpt.
1895 if (CurLexer->getConditionalStackDepth() == 0)
1896 CurLexer->MIOpt.FoundTopLevelElse();
1897
Chris Lattner22eb9722006-06-18 05:43:12 +00001898 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001899 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001900
1901 // Finally, skip the rest of the contents of this block and return the first
1902 // token after it.
1903 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1904 /*FoundElse*/CI.FoundElse);
1905}
Chris Lattnerb8761832006-06-24 21:31:03 +00001906