blob: 8b370be924629da7081f51eee763bc4241e03ac2 [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 Lattner677757a2006-06-28 05:26:32 +0000481
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000482/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
483/// expanded as a macro, handle it and return the next token as 'Identifier'.
Chris Lattner78186052006-07-09 00:45:31 +0000484bool Preprocessor::HandleMacroExpandedIdentifier(LexerToken &Identifier,
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000485 MacroInfo *MI) {
Chris Lattner78186052006-07-09 00:45:31 +0000486
487 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
488 if (MI->isBuiltinMacro()) {
489 ExpandBuiltinMacro(Identifier);
490 return false;
491 }
492
493 /// FormalArgs - If this is a function-like macro expansion, this contains,
494 /// for each macro argument, the list of tokens that were provided to the
495 /// invocation.
496 MacroFormalArgs *FormalArgs = 0;
497
498 // If this is a function-like macro, read the arguments.
499 if (MI->isFunctionLike()) {
500 // FIXME: We need to query to see if the ( exists without reading it.
501
502 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
503 // name isn't a '(', this macro should not be expanded.
504 bool isFunctionInvocation = true;
505 if (!isFunctionInvocation)
506 return true;
507
508 LexerToken Tok;
509 LexUnexpandedToken(Tok);
510 assert(Tok.getKind() == tok::l_paren &&
511 "not a function-like macro invocation!");
512
513 // Remember that we are now parsing the arguments to a macro invocation.
514 // Preprocessor directives used inside macro arguments are not portable, and
515 // this enables the warning.
516 InMacroFormalArgs = true;
517 FormalArgs = ReadFunctionLikeMacroFormalArgs(Identifier, MI);
518
519 // Finished parsing args.
520 InMacroFormalArgs = false;
521
522 // If there was an error parsing the arguments, bail out.
523 if (FormalArgs == 0) return false;
524
525 ++NumFnMacroExpanded;
526 } else {
527 ++NumMacroExpanded;
528 }
Chris Lattner13044d92006-07-03 05:16:44 +0000529
530 // Notice that this macro has been used.
531 MI->setIsUsed(true);
Chris Lattner69772b02006-07-02 20:34:39 +0000532
533 // If we started lexing a macro, enter the macro expansion body.
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000534
535 // If this macro expands to no tokens, don't bother to push it onto the
536 // expansion stack, only to take it right back off.
537 if (MI->getNumTokens() == 0) {
Chris Lattner78186052006-07-09 00:45:31 +0000538 // No need for formal arg info.
539 delete FormalArgs;
540
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000541 // Ignore this macro use, just return the next token in the current
542 // buffer.
543 bool HadLeadingSpace = Identifier.hasLeadingSpace();
544 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
545
546 Lex(Identifier);
547
548 // If the identifier isn't on some OTHER line, inherit the leading
549 // whitespace/first-on-a-line property of this token. This handles
550 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
551 // empty.
552 if (!Identifier.isAtStartOfLine()) {
553 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
554 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
555 }
556 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000557 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000558
559 } else if (MI->getNumTokens() == 1 &&
Chris Lattner78186052006-07-09 00:45:31 +0000560 // FIXME: Fn-Like Macros: Fast if arg not used.
561 FormalArgs == 0 &&
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000562 // Don't handle identifiers if they need recursive expansion.
563 (MI->getReplacementToken(0).getIdentifierInfo() == 0 ||
564 !MI->getReplacementToken(0).getIdentifierInfo()->getMacroInfo())){
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000565
566 // Otherwise, if this macro expands into a single trivially-expanded
567 // token: expand it now. This handles common cases like
568 // "#define VAL 42".
569
570 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
571 // identifier to the expanded token.
572 bool isAtStartOfLine = Identifier.isAtStartOfLine();
573 bool hasLeadingSpace = Identifier.hasLeadingSpace();
574
575 // Remember where the token is instantiated.
576 SourceLocation InstantiateLoc = Identifier.getLocation();
577
578 // Replace the result token.
579 Identifier = MI->getReplacementToken(0);
580
581 // Restore the StartOfLine/LeadingSpace markers.
582 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
583 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
584
585 // Update the tokens location to include both its logical and physical
586 // locations.
587 SourceLocation Loc =
Chris Lattnerc673f902006-06-30 06:10:41 +0000588 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000589 Identifier.SetLocation(Loc);
590
591 // Since this is not an identifier token, it can't be macro expanded, so
592 // we're done.
593 ++NumFastMacroExpanded;
Chris Lattner78186052006-07-09 00:45:31 +0000594 return false;
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000595 }
596
Chris Lattner78186052006-07-09 00:45:31 +0000597 // Start expanding the macro.
598 EnterMacro(Identifier, FormalArgs);
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000599
600 // Now that the macro is at the top of the include stack, ask the
601 // preprocessor to read the next token from it.
Chris Lattner78186052006-07-09 00:45:31 +0000602 Lex(Identifier);
603 return false;
604}
605
606/// ReadFunctionLikeMacroFormalArgs - After reading "MACRO(", this method is
607/// invoked to read all of the formal arguments specified for the macro
608/// invocation. This returns null on error.
609MacroFormalArgs *Preprocessor::
610ReadFunctionLikeMacroFormalArgs(LexerToken &MacroName, MacroInfo *MI) {
611 // Use an auto_ptr here so that the MacroFormalArgs object is deleted on
612 // all error paths.
613 std::auto_ptr<MacroFormalArgs> Args(new MacroFormalArgs(MI));
614
615 // The number of fixed arguments to parse.
616 unsigned NumFixedArgsLeft = MI->getNumArgs();
617 bool isVariadic = MI->isVariadic();
618
619 // If this is a C99-style varargs macro invocation, add an extra expected
620 // argument, which will catch all of the varargs formals in one argument.
621 if (MI->isC99Varargs())
622 ++NumFixedArgsLeft;
623
624 // Outer loop, while there are more arguments, keep reading them.
625 LexerToken Tok;
626 Tok.SetKind(tok::comma);
627 --NumFixedArgsLeft; // Start reading the first arg.
628
629 while (Tok.getKind() == tok::comma) {
630 // ArgTokens - Build up a list of tokens that make up this argument.
631 std::vector<LexerToken> ArgTokens;
632 // C99 6.10.3p11: Keep track of the number of l_parens we have seen.
633 unsigned NumParens = 0;
634
635 while (1) {
636 LexUnexpandedToken(Tok);
637
638 if (Tok.getKind() == tok::eof) {
639 Diag(MacroName, diag::err_unterm_macro_invoc);
640 // Do not lose the EOF. Return it to the client.
641 MacroName = Tok;
642 return 0;
643 } else if (Tok.getKind() == tok::r_paren) {
644 // If we found the ) token, the macro arg list is done.
645 if (NumParens-- == 0)
646 break;
647 } else if (Tok.getKind() == tok::l_paren) {
648 ++NumParens;
649 } else if (Tok.getKind() == tok::comma && NumParens == 0) {
650 // Comma ends this argument if there are more fixed arguments expected.
651 if (NumFixedArgsLeft)
652 break;
653
654 // If this is not a variadic macro, too many formals were specified.
655 if (!isVariadic) {
656 // Emit the diagnostic at the macro name in case there is a missing ).
657 // Emitting it at the , could be far away from the macro name.
658 Diag(MacroName, diag::err_too_many_formals_in_macro_invoc);
659 return 0;
660 }
661 // Otherwise, continue to add the tokens to this variable argument.
662 }
663
664 ArgTokens.push_back(Tok);
665 }
666
667 // Remember the tokens that make up this argument. This destroys ArgTokens.
668 Args->addArgument(ArgTokens);
669 --NumFixedArgsLeft;
670 };
671
672 // Okay, we either found the r_paren. Check to see if we parsed too few
673 // arguments.
674 unsigned NumFormals = Args->getNumArguments();
675 unsigned MinArgsExpected = MI->getNumArgs();
676
677 // C99 expects us to pass at least one vararg arg (but as an extension, we
678 // don't require this).
679 if (MI->isC99Varargs())
680 ++MinArgsExpected;
681
682 if (NumFormals < MinArgsExpected) {
683 // There are several cases where too few arguments is ok, handle them now.
684 if (NumFormals+1 == MinArgsExpected && MI->isVariadic()) {
685 // Varargs where the named vararg parameter is missing: ok as extension.
686 // #define A(x, ...)
687 // A("blah")
688 Diag(Tok, diag::ext_missing_varargs_arg);
689 } else if (MI->getNumArgs() == 1) {
690 // #define A(x)
691 // A()
692 // is ok. Add an empty argument.
693 std::vector<LexerToken> ArgTokens;
694 Args->addArgument(ArgTokens);
695 } else {
696 // Otherwise, emit the error.
697 Diag(Tok, diag::err_too_few_formals_in_macro_invoc);
698 return 0;
699 }
700 }
701
702 return Args.release();
Chris Lattnerf373a4a2006-06-26 06:16:29 +0000703}
704
Chris Lattnerc673f902006-06-30 06:10:41 +0000705/// ComputeDATE_TIME - Compute the current time, enter it into the specified
706/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
707/// the identifier tokens inserted.
708static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
709 ScratchBuffer *ScratchBuf) {
710 time_t TT = time(0);
711 struct tm *TM = localtime(&TT);
712
713 static const char * const Months[] = {
714 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
715 };
716
717 char TmpBuffer[100];
718 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
719 TM->tm_year+1900);
720 DATELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
721
722 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
723 TIMELoc = ScratchBuf->getToken(TmpBuffer, strlen(TmpBuffer));
724}
725
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000726/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
727/// as a builtin macro, handle it and return the next token as 'Tok'.
Chris Lattner69772b02006-07-02 20:34:39 +0000728void Preprocessor::ExpandBuiltinMacro(LexerToken &Tok) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000729 // Figure out which token this is.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000730 IdentifierInfo *II = Tok.getIdentifierInfo();
731 assert(II && "Can't be a macro without id info!");
Chris Lattner69772b02006-07-02 20:34:39 +0000732
733 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
734 // lex the token after it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000735 if (II == Ident_Pragma)
Chris Lattner69772b02006-07-02 20:34:39 +0000736 return Handle_Pragma(Tok);
737
Chris Lattner78186052006-07-09 00:45:31 +0000738 ++NumBuiltinMacroExpanded;
739
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000740 char TmpBuffer[100];
Chris Lattner69772b02006-07-02 20:34:39 +0000741
742 // Set up the return result.
Chris Lattner630b33c2006-07-01 22:46:53 +0000743 Tok.SetIdentifierInfo(0);
744 Tok.ClearFlag(LexerToken::NeedsCleaning);
745
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000746 if (II == Ident__LINE__) {
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000747 // __LINE__ expands to a simple numeric value.
748 sprintf(TmpBuffer, "%u", SourceMgr.getLineNumber(Tok.getLocation()));
749 unsigned Length = strlen(TmpBuffer);
750 Tok.SetKind(tok::numeric_constant);
751 Tok.SetLength(Length);
752 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000753 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000754 SourceLocation Loc = Tok.getLocation();
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000755 if (II == Ident__BASE_FILE__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000756 Diag(Tok, diag::ext_pp_base_file);
757 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
758 while (NextLoc.getFileID() != 0) {
759 Loc = NextLoc;
760 NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
761 }
762 }
763
Chris Lattner0766e592006-07-03 01:07:01 +0000764 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
765 std::string FN = SourceMgr.getSourceName(Loc);
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000766 FN = Lexer::Stringify(FN);
Chris Lattner630b33c2006-07-01 22:46:53 +0000767 Tok.SetKind(tok::string_literal);
768 Tok.SetLength(FN.size());
769 Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000770 } else if (II == Ident__DATE__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000771 if (!DATELoc.isValid())
772 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
773 Tok.SetKind(tok::string_literal);
774 Tok.SetLength(strlen("\"Mmm dd yyyy\""));
775 Tok.SetLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000776 } else if (II == Ident__TIME__) {
Chris Lattnerc673f902006-06-30 06:10:41 +0000777 if (!TIMELoc.isValid())
778 ComputeDATE_TIME(DATELoc, TIMELoc, ScratchBuf);
779 Tok.SetKind(tok::string_literal);
780 Tok.SetLength(strlen("\"hh:mm:ss\""));
781 Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000782 } else if (II == Ident__INCLUDE_LEVEL__) {
Chris Lattnerc1283b92006-07-01 23:16:30 +0000783 Diag(Tok, diag::ext_pp_include_level);
784
785 // Compute the include depth of this token.
786 unsigned Depth = 0;
787 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
788 for (; Loc.getFileID() != 0; ++Depth)
789 Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
790
791 // __INCLUDE_LEVEL__ expands to a simple numeric value.
792 sprintf(TmpBuffer, "%u", Depth);
793 unsigned Length = strlen(TmpBuffer);
794 Tok.SetKind(tok::numeric_constant);
795 Tok.SetLength(Length);
796 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000797 } else if (II == Ident__TIMESTAMP__) {
Chris Lattner847e0e42006-07-01 23:49:16 +0000798 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
799 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
800 Diag(Tok, diag::ext_pp_timestamp);
801
802 // Get the file that we are lexing out of. If we're currently lexing from
803 // a macro, dig into the include stack.
804 const FileEntry *CurFile = 0;
Chris Lattnerecfeafe2006-07-02 21:26:45 +0000805 Lexer *TheLexer = getCurrentFileLexer();
Chris Lattner847e0e42006-07-01 23:49:16 +0000806
807 if (TheLexer)
808 CurFile = SourceMgr.getFileEntryForFileID(TheLexer->getCurFileID());
809
810 // If this file is older than the file it depends on, emit a diagnostic.
811 const char *Result;
812 if (CurFile) {
813 time_t TT = CurFile->getModificationTime();
814 struct tm *TM = localtime(&TT);
815 Result = asctime(TM);
816 } else {
817 Result = "??? ??? ?? ??:??:?? ????\n";
818 }
819 TmpBuffer[0] = '"';
820 strcpy(TmpBuffer+1, Result);
821 unsigned Len = strlen(TmpBuffer);
822 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
823 Tok.SetKind(tok::string_literal);
824 Tok.SetLength(Len);
825 Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Len, Tok.getLocation()));
Chris Lattner0b8cfc22006-06-28 06:49:17 +0000826 } else {
827 assert(0 && "Unknown identifier!");
828 }
829}
Chris Lattner677757a2006-06-28 05:26:32 +0000830
Chris Lattner13044d92006-07-03 05:16:44 +0000831namespace {
832struct UnusedIdentifierReporter : public IdentifierVisitor {
833 Preprocessor &PP;
834 UnusedIdentifierReporter(Preprocessor &pp) : PP(pp) {}
835
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000836 void VisitIdentifier(IdentifierInfo &II) const {
837 if (II.getMacroInfo() && !II.getMacroInfo()->isUsed())
838 PP.Diag(II.getMacroInfo()->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner13044d92006-07-03 05:16:44 +0000839 }
840};
841}
842
Chris Lattner677757a2006-06-28 05:26:32 +0000843//===----------------------------------------------------------------------===//
844// Lexer Event Handling.
845//===----------------------------------------------------------------------===//
846
Chris Lattnercefc7682006-07-08 08:28:12 +0000847/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
848/// identifier information for the token and install it into the token.
849IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier,
850 const char *BufPtr) {
851 assert(Identifier.getKind() == tok::identifier && "Not an identifier!");
852 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
853
854 // Look up this token, see if it is a macro, or if it is a language keyword.
855 IdentifierInfo *II;
856 if (BufPtr && !Identifier.needsCleaning()) {
857 // No cleaning needed, just use the characters from the lexed buffer.
858 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
859 } else {
860 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
861 const char *TmpBuf = (char*)alloca(Identifier.getLength());
862 unsigned Size = getSpelling(Identifier, TmpBuf);
863 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
864 }
865 Identifier.SetIdentifierInfo(II);
866 return II;
867}
868
869
Chris Lattner677757a2006-06-28 05:26:32 +0000870/// HandleIdentifier - This callback is invoked when the lexer reads an
871/// identifier. This callback looks up the identifier in the map and/or
872/// potentially macro expands it or turns it into a named token (like 'for').
873void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
874 if (Identifier.getIdentifierInfo() == 0) {
875 // If we are skipping tokens (because we are in a #if 0 block), there will
876 // be no identifier info, just return the token.
877 assert(isSkipping() && "Token isn't an identifier?");
878 return;
879 }
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000880 IdentifierInfo &II = *Identifier.getIdentifierInfo();
Chris Lattner677757a2006-06-28 05:26:32 +0000881
882 // If this identifier was poisoned, and if it was not produced from a macro
883 // expansion, emit an error.
Chris Lattner8ff71992006-07-06 05:17:39 +0000884 if (II.isPoisoned() && CurLexer) {
885 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
886 Diag(Identifier, diag::err_pp_used_poisoned_id);
887 else
888 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
889 }
Chris Lattner677757a2006-06-28 05:26:32 +0000890
Chris Lattner78186052006-07-09 00:45:31 +0000891 // If this is a macro to be expanded, do it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000892 if (MacroInfo *MI = II.getMacroInfo())
Chris Lattner677757a2006-06-28 05:26:32 +0000893 if (MI->isEnabled() && !DisableMacroExpansion)
Chris Lattner78186052006-07-09 00:45:31 +0000894 if (!HandleMacroExpandedIdentifier(Identifier, MI))
895 return;
Chris Lattner677757a2006-06-28 05:26:32 +0000896
897 // Change the kind of this identifier to the appropriate token kind, e.g.
898 // turning "for" into a keyword.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000899 Identifier.SetKind(II.getTokenID());
Chris Lattner677757a2006-06-28 05:26:32 +0000900
901 // If this is an extension token, diagnose its use.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000902 if (II.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner677757a2006-06-28 05:26:32 +0000903}
904
Chris Lattner22eb9722006-06-18 05:43:12 +0000905/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
906/// the current file. This either returns the EOF token or pops a level off
907/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000908void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000909 assert(!CurMacroExpander &&
910 "Ending a file when currently in a macro!");
911
912 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
913 // this is an error condition. Just return the EOF token up to
914 // SkipExcludedConditionalBlock. The Lexer will have already have issued
915 // errors for the unterminated #if's on the conditional stack.
916 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000917 Result.StartToken();
918 CurLexer->BufferPtr = CurLexer->BufferEnd;
919 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000920 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000921 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000922 }
923
Chris Lattner371ac8a2006-07-04 07:11:10 +0000924 // See if this file had a controlling macro.
Chris Lattner3665f162006-07-04 07:26:10 +0000925 if (CurLexer) { // Not ending a macro, ignore it.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000926 if (const IdentifierInfo *ControllingMacro =
Chris Lattner371ac8a2006-07-04 07:11:10 +0000927 CurLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Chris Lattner3665f162006-07-04 07:26:10 +0000928 // Okay, this has a controlling macro, remember in PerFileInfo.
929 if (const FileEntry *FE =
930 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
931 getFileInfo(FE).ControllingMacro = ControllingMacro;
Chris Lattner371ac8a2006-07-04 07:11:10 +0000932 }
933 }
934
Chris Lattner22eb9722006-06-18 05:43:12 +0000935 // If this is a #include'd file, pop it off the include stack and continue
936 // lexing the #includer file.
Chris Lattner69772b02006-07-02 20:34:39 +0000937 if (!IncludeMacroStack.empty()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000938 // We're done with the #included file.
939 delete CurLexer;
Chris Lattner69772b02006-07-02 20:34:39 +0000940 CurLexer = IncludeMacroStack.back().TheLexer;
941 CurDirLookup = IncludeMacroStack.back().TheDirLookup;
942 CurMacroExpander = IncludeMacroStack.back().TheMacroExpander;
943 IncludeMacroStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000944
945 // Notify the client, if desired, that we are in a new source file.
Chris Lattner69772b02006-07-02 20:34:39 +0000946 if (FileChangeHandler && !isEndOfMacro && CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000947 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
948
949 // Get the file entry for the current file.
950 if (const FileEntry *FE =
951 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
952 FileType = getFileInfo(FE).DirInfo;
953
Chris Lattner0c885f52006-06-21 06:50:18 +0000954 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattner55a60952006-06-25 04:20:34 +0000955 ExitFile, FileType);
Chris Lattnerc8997182006-06-22 05:52:16 +0000956 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000957
Chris Lattner22eb9722006-06-18 05:43:12 +0000958 return Lex(Result);
959 }
960
Chris Lattnerd01e2912006-06-18 16:22:51 +0000961 Result.StartToken();
962 CurLexer->BufferPtr = CurLexer->BufferEnd;
963 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000964 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000965
966 // We're done with the #included file.
967 delete CurLexer;
968 CurLexer = 0;
Chris Lattner13044d92006-07-03 05:16:44 +0000969
970 // This is the end of the top-level file.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000971 Identifiers.VisitIdentifiers(UnusedIdentifierReporter(*this));
Chris Lattner22eb9722006-06-18 05:43:12 +0000972}
973
974/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000975/// the current macro line.
976void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000977 assert(CurMacroExpander && !CurLexer &&
978 "Ending a macro when currently in a #include file!");
979
980 // Mark macro not ignored now that it is no longer being expanded.
981 CurMacroExpander->getMacro().EnableMacro();
982 delete CurMacroExpander;
983
Chris Lattner69772b02006-07-02 20:34:39 +0000984 // Handle this like a #include file being popped off the stack.
985 CurMacroExpander = 0;
986 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000987}
988
989
990//===----------------------------------------------------------------------===//
991// Utility Methods for Preprocessor Directive Handling.
992//===----------------------------------------------------------------------===//
993
994/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
995/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000996void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000997 LexerToken Tmp;
998 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000999 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001000 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001001}
1002
1003/// ReadMacroName - Lex and validate a macro name, which occurs after a
1004/// #define or #undef. This sets the token kind to eom and discards the rest
Chris Lattnere8eef322006-07-08 07:01:00 +00001005/// of the macro line if the macro name is invalid. isDefineUndef is 1 if
1006/// this is due to a a #define, 2 if #undef directive, 0 if it is something
Chris Lattner44f8a662006-07-03 01:27:27 +00001007/// else (e.g. #ifdef).
Chris Lattnere8eef322006-07-08 07:01:00 +00001008void Preprocessor::ReadMacroName(LexerToken &MacroNameTok, char isDefineUndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001009 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +00001010 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001011
1012 // Missing macro name?
1013 if (MacroNameTok.getKind() == tok::eom)
1014 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
1015
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001016 IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1017 if (II == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001018 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001019 // Fall through on error.
1020 } else if (0) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001021 // FIXME: C++. Error if defining a C++ named operator.
Chris Lattner22eb9722006-06-18 05:43:12 +00001022
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001023 } else if (isDefineUndef && II->getName()[0] == 'd' && // defined
1024 !strcmp(II->getName()+1, "efined")) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001025 // Error if defining "defined": C99 6.10.8.4.
Chris Lattneraaf09112006-07-03 01:17:59 +00001026 Diag(MacroNameTok, diag::err_defined_macro_name);
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001027 } else if (isDefineUndef && II->getMacroInfo() &&
1028 II->getMacroInfo()->isBuiltinMacro()) {
Chris Lattner44f8a662006-07-03 01:27:27 +00001029 // Error if defining "__LINE__" and other builtins: C99 6.10.8.4.
Chris Lattnere8eef322006-07-08 07:01:00 +00001030 if (isDefineUndef == 1)
1031 Diag(MacroNameTok, diag::pp_redef_builtin_macro);
1032 else
1033 Diag(MacroNameTok, diag::pp_undef_builtin_macro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001034 } else {
1035 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +00001036 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001037 }
1038
Chris Lattner22eb9722006-06-18 05:43:12 +00001039 // Invalid macro name, read and discard the rest of the line. Then set the
1040 // token kind to tok::eom.
1041 MacroNameTok.SetKind(tok::eom);
1042 return DiscardUntilEndOfDirective();
1043}
1044
1045/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
1046/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +00001047void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001048 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +00001049 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001050 // There should be no tokens after the directive, but we allow them as an
1051 // extension.
1052 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +00001053 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
1054 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001055 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001056}
1057
1058
1059
1060/// SkipExcludedConditionalBlock - We just read a #if or related directive and
1061/// decided that the subsequent tokens are in the #if'd out portion of the
1062/// file. Lex the rest of the file, until we see an #endif. If
1063/// FoundNonSkipPortion is true, then we have already emitted code for part of
1064/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
1065/// is true, then #else directives are ok, if not, then we have already seen one
1066/// so a #else directive is a duplicate. When this returns, the caller can lex
1067/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +00001068void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +00001069 bool FoundNonSkipPortion,
1070 bool FoundElse) {
1071 ++NumSkipped;
Chris Lattner69772b02006-07-02 20:34:39 +00001072 assert(CurMacroExpander == 0 && CurLexer &&
Chris Lattner22eb9722006-06-18 05:43:12 +00001073 "Lexing a macro, not a file?");
1074
1075 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
1076 FoundNonSkipPortion, FoundElse);
1077
1078 // Know that we are going to be skipping tokens. Set this flag to indicate
1079 // this, which has a couple of effects:
1080 // 1. If EOF of the current lexer is found, the include stack isn't popped.
1081 // 2. Identifier information is not looked up for identifier tokens. As an
1082 // effect of this, implicit macro expansion is naturally disabled.
1083 // 3. "#" tokens at the start of a line are treated as normal tokens, not
1084 // implicitly transformed by the lexer.
1085 // 4. All notes, warnings, and extension messages are disabled.
1086 //
1087 SkippingContents = true;
1088 LexerToken Tok;
1089 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +00001090 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001091
1092 // If this is the end of the buffer, we have an error. The lexer will have
1093 // already handled this error condition, so just return and let the caller
1094 // lex after this #include.
1095 if (Tok.getKind() == tok::eof) break;
1096
1097 // If this token is not a preprocessor directive, just skip it.
1098 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
1099 continue;
1100
1101 // We just parsed a # character at the start of a line, so we're in
1102 // directive mode. Tell the lexer this so any newlines we see will be
1103 // converted into an EOM token (this terminates the macro).
1104 CurLexer->ParsingPreprocessorDirective = true;
1105
1106 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +00001107 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001108
1109 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
1110 // something bogus), skip it.
1111 if (Tok.getKind() != tok::identifier) {
1112 CurLexer->ParsingPreprocessorDirective = false;
1113 continue;
1114 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001115
Chris Lattner22eb9722006-06-18 05:43:12 +00001116 // If the first letter isn't i or e, it isn't intesting to us. We know that
1117 // this is safe in the face of spelling differences, because there is no way
1118 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +00001119 // allows us to avoid looking up the identifier info for #define/#undef and
1120 // other common directives.
1121 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
1122 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +00001123 if (FirstChar >= 'a' && FirstChar <= 'z' &&
1124 FirstChar != 'i' && FirstChar != 'e') {
1125 CurLexer->ParsingPreprocessorDirective = false;
1126 continue;
1127 }
1128
Chris Lattnere60165f2006-06-22 06:36:29 +00001129 // Get the identifier name without trigraphs or embedded newlines. Note
1130 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
1131 // when skipping.
1132 // TODO: could do this with zero copies in the no-clean case by using
1133 // strncmp below.
1134 char Directive[20];
1135 unsigned IdLen;
1136 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
1137 IdLen = Tok.getLength();
1138 memcpy(Directive, RawCharData, IdLen);
1139 Directive[IdLen] = 0;
1140 } else {
1141 std::string DirectiveStr = getSpelling(Tok);
1142 IdLen = DirectiveStr.size();
1143 if (IdLen >= 20) {
1144 CurLexer->ParsingPreprocessorDirective = false;
1145 continue;
1146 }
1147 memcpy(Directive, &DirectiveStr[0], IdLen);
1148 Directive[IdLen] = 0;
1149 }
1150
Chris Lattner22eb9722006-06-18 05:43:12 +00001151 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001152 if ((IdLen == 2) || // "if"
1153 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
1154 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +00001155 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
1156 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +00001157 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +00001158 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +00001159 /*foundnonskip*/false,
1160 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001161 }
1162 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +00001163 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +00001164 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001165 PPConditionalInfo CondInfo;
1166 CondInfo.WasSkipping = true; // Silence bogus warning.
1167 bool InCond = CurLexer->popConditionalLevel(CondInfo);
1168 assert(!InCond && "Can't be skipping if not in a conditional!");
1169
1170 // If we popped the outermost skipping block, we're done skipping!
1171 if (!CondInfo.WasSkipping)
1172 break;
Chris Lattnere60165f2006-06-22 06:36:29 +00001173 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +00001174 // #else directive in a skipping conditional. If not in some other
1175 // skipping conditional, and if #else hasn't already been seen, enter it
1176 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +00001177 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001178 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1179
1180 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001181 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001182
1183 // Note that we've seen a #else in this conditional.
1184 CondInfo.FoundElse = true;
1185
1186 // If the conditional is at the top level, and the #if block wasn't
1187 // entered, enter the #else block now.
1188 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
1189 CondInfo.FoundNonSkip = true;
1190 break;
1191 }
Chris Lattnere60165f2006-06-22 06:36:29 +00001192 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +00001193 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
1194
1195 bool ShouldEnter;
1196 // If this is in a skipping block or if we're already handled this #if
1197 // block, don't bother parsing the condition.
1198 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +00001199 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001200 ShouldEnter = false;
1201 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00001202 // Restore the value of SkippingContents so that identifiers are
1203 // looked up, etc, inside the #elif expression.
1204 assert(SkippingContents && "We have to be skipping here!");
1205 SkippingContents = false;
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001206 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001207 ShouldEnter = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001208 SkippingContents = true;
1209 }
1210
1211 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001212 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001213
1214 // If this condition is true, enter it!
1215 if (ShouldEnter) {
1216 CondInfo.FoundNonSkip = true;
1217 break;
1218 }
1219 }
1220 }
1221
1222 CurLexer->ParsingPreprocessorDirective = false;
1223 }
1224
1225 // Finally, if we are out of the conditional (saw an #endif or ran off the end
1226 // of the file, just stop skipping and return to lexing whatever came after
1227 // the #if block.
1228 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001229}
1230
1231//===----------------------------------------------------------------------===//
1232// Preprocessor Directive Handling.
1233//===----------------------------------------------------------------------===//
1234
1235/// HandleDirective - This callback is invoked when the lexer sees a # token
1236/// at the start of a line. This consumes the directive, modifies the
1237/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1238/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +00001239void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner4d5e1a72006-07-03 01:01:29 +00001240 // FIXME: Traditional: # with whitespace before it not recognized by K&R?
Chris Lattner22eb9722006-06-18 05:43:12 +00001241
1242 // We just parsed a # character at the start of a line, so we're in directive
1243 // mode. Tell the lexer this so any newlines we see will be converted into an
Chris Lattner78186052006-07-09 00:45:31 +00001244 // EOM token (which terminates the directive).
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 CurLexer->ParsingPreprocessorDirective = true;
1246
1247 ++NumDirectives;
1248
Chris Lattner371ac8a2006-07-04 07:11:10 +00001249 // We are about to read a token. For the multiple-include optimization FA to
1250 // work, we have to remember if we had read any tokens *before* this
1251 // pp-directive.
1252 bool ReadAnyTokensBeforeDirective = CurLexer->MIOpt.getHasReadAnyTokensVal();
1253
Chris Lattner78186052006-07-09 00:45:31 +00001254 // Read the next token, the directive flavor. This isn't expanded due to
1255 // C99 6.10.3p8.
Chris Lattnercb283342006-06-18 06:48:37 +00001256 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001257
Chris Lattner78186052006-07-09 00:45:31 +00001258 // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
1259 // #define A(x) #x
1260 // A(abc
1261 // #warning blah
1262 // def)
1263 // If so, the user is relying on non-portable behavior, emit a diagnostic.
1264 if (InMacroFormalArgs)
1265 Diag(Result, diag::ext_embedded_directive);
1266
Chris Lattner22eb9722006-06-18 05:43:12 +00001267 switch (Result.getKind()) {
1268 default: break;
1269 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +00001270 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001271
1272#if 0
1273 case tok::numeric_constant:
1274 // FIXME: implement # 7 line numbers!
1275 break;
1276#endif
1277 case tok::kw_else:
1278 return HandleElseDirective(Result);
1279 case tok::kw_if:
Chris Lattnera8654ca2006-07-04 17:42:08 +00001280 return HandleIfDirective(Result, ReadAnyTokensBeforeDirective);
Chris Lattner22eb9722006-06-18 05:43:12 +00001281 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +00001282 // Get the identifier name without trigraphs or embedded newlines.
1283 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +00001284 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +00001285 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001286 case 4:
Chris Lattner40931922006-06-22 06:14:04 +00001287 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattnera8654ca2006-07-04 17:42:08 +00001288 ; // FIXME: implement #line
Chris Lattner40931922006-06-22 06:14:04 +00001289 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 return HandleElifDirective(Result);
Chris Lattner01d66cc2006-07-03 22:16:27 +00001291 if (Directive[0] == 's' && !strcmp(Directive, "sccs"))
1292 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001293 break;
1294 case 5:
Chris Lattner40931922006-06-22 06:14:04 +00001295 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001297 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001298 return HandleIfdefDirective(Result, false, true/*not valid for miopt*/);
Chris Lattner40931922006-06-22 06:14:04 +00001299 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001300 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001301 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001302 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +00001303 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner01d66cc2006-07-03 22:16:27 +00001304 return HandleIdentSCCSDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 break;
1306 case 6:
Chris Lattner40931922006-06-22 06:14:04 +00001307 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001308 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +00001309 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner371ac8a2006-07-04 07:11:10 +00001310 return HandleIfdefDirective(Result, true, ReadAnyTokensBeforeDirective);
Chris Lattner40931922006-06-22 06:14:04 +00001311 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 return HandleImportDirective(Result);
Chris Lattnerb8761832006-06-24 21:31:03 +00001313 if (Directive[0] == 'p' && !strcmp(Directive, "pragma"))
Chris Lattner69772b02006-07-02 20:34:39 +00001314 return HandlePragmaDirective();
Chris Lattnerb8761832006-06-24 21:31:03 +00001315 if (Directive[0] == 'a' && !strcmp(Directive, "assert"))
1316 isExtension = true; // FIXME: implement #assert
Chris Lattner22eb9722006-06-18 05:43:12 +00001317 break;
1318 case 7:
Chris Lattner40931922006-06-22 06:14:04 +00001319 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
1320 return HandleIncludeDirective(Result); // Handle #include.
1321 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +00001322 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +00001323 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +00001324 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001325 break;
1326 case 8:
Chris Lattner40931922006-06-22 06:14:04 +00001327 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattnerb8761832006-06-24 21:31:03 +00001328 isExtension = true; // FIXME: implement #unassert
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 }
1330 break;
1331 case 12:
Chris Lattner40931922006-06-22 06:14:04 +00001332 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
1333 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +00001334 break;
1335 }
1336 break;
1337 }
1338
1339 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +00001340 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001341
1342 // Read the rest of the PP line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001343 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001344
1345 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +00001346}
1347
Chris Lattner01d66cc2006-07-03 22:16:27 +00001348void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Tok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 bool isWarning) {
1350 // Read the rest of the line raw. We do this because we don't want macros
1351 // to be expanded and we don't require that the tokens be valid preprocessing
1352 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
1353 // collapse multiple consequtive white space between tokens, but this isn't
1354 // specified by the standard.
1355 std::string Message = CurLexer->ReadToEndOfLine();
1356
1357 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
Chris Lattner01d66cc2006-07-03 22:16:27 +00001358 return Diag(Tok, DiagID, Message);
1359}
1360
1361/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1362///
1363void Preprocessor::HandleIdentSCCSDirective(LexerToken &Tok) {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001364 // Yes, this directive is an extension.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001365 Diag(Tok, diag::ext_pp_ident_directive);
1366
Chris Lattner371ac8a2006-07-04 07:11:10 +00001367 // Read the string argument.
Chris Lattner01d66cc2006-07-03 22:16:27 +00001368 LexerToken StrTok;
1369 Lex(StrTok);
1370
1371 // If the token kind isn't a string, it's a malformed directive.
1372 if (StrTok.getKind() != tok::string_literal)
1373 return Diag(StrTok, diag::err_pp_malformed_ident);
1374
1375 // Verify that there is nothing after the string, other than EOM.
1376 CheckEndOfDirective("#ident");
1377
1378 if (IdentHandler)
1379 IdentHandler(Tok.getLocation(), getSpelling(StrTok));
Chris Lattner22eb9722006-06-18 05:43:12 +00001380}
1381
Chris Lattnerb8761832006-06-24 21:31:03 +00001382//===----------------------------------------------------------------------===//
1383// Preprocessor Include Directive Handling.
1384//===----------------------------------------------------------------------===//
1385
Chris Lattner22eb9722006-06-18 05:43:12 +00001386/// HandleIncludeDirective - The "#include" tokens have just been read, read the
1387/// file to be included from the lexer, then include it! This is a common
1388/// routine with functionality shared between #include, #include_next and
1389/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +00001390void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 const DirectoryLookup *LookupFrom,
1392 bool isImport) {
1393 ++NumIncluded;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001394
Chris Lattner22eb9722006-06-18 05:43:12 +00001395 LexerToken FilenameTok;
Chris Lattner269c2322006-06-25 06:23:00 +00001396 std::string Filename = CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397
1398 // If the token kind is EOM, the error has already been diagnosed.
1399 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001400 return;
Chris Lattner269c2322006-06-25 06:23:00 +00001401
1402 // Verify that there is nothing after the filename, other than EOM. Use the
1403 // preprocessor to lex this in case lexing the filename entered a macro.
1404 CheckEndOfDirective("#include");
Chris Lattner22eb9722006-06-18 05:43:12 +00001405
1406 // Check that we don't have infinite #include recursion.
Chris Lattner69772b02006-07-02 20:34:39 +00001407 if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1)
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 return Diag(FilenameTok, diag::err_pp_include_too_deep);
1409
Chris Lattner269c2322006-06-25 06:23:00 +00001410 // Find out whether the filename is <x> or "x".
1411 bool isAngled = Filename[0] == '<';
Chris Lattner22eb9722006-06-18 05:43:12 +00001412
1413 // Remove the quotes.
1414 Filename = std::string(Filename.begin()+1, Filename.end()-1);
1415
Chris Lattner22eb9722006-06-18 05:43:12 +00001416 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +00001417 const DirectoryLookup *CurDir;
1418 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 if (File == 0)
1420 return Diag(FilenameTok, diag::err_pp_file_not_found);
1421
1422 // Get information about this file.
1423 PerFileInfo &FileInfo = getFileInfo(File);
1424
1425 // If this is a #import directive, check that we have not already imported
1426 // this header.
1427 if (isImport) {
1428 // If this has already been imported, don't import it again.
1429 FileInfo.isImport = true;
1430
1431 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +00001432 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001433 } else {
1434 // Otherwise, if this is a #include of a file that was previously #import'd
1435 // or if this is the second #include of a #pragma once file, ignore it.
1436 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +00001437 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001438 }
Chris Lattner3665f162006-07-04 07:26:10 +00001439
1440 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1441 // if the macro that guards it is defined, we know the #include has no effect.
1442 if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) {
1443 ++NumMultiIncludeFileOptzn;
1444 return;
1445 }
1446
Chris Lattner22eb9722006-06-18 05:43:12 +00001447
1448 // Look up the file, create a File ID for it.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001449 unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001450 if (FileID == 0)
1451 return Diag(FilenameTok, diag::err_pp_file_not_found);
1452
1453 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001454 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001455
1456 // Increment the number of times this file has been included.
1457 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001458}
1459
1460/// HandleIncludeNextDirective - Implements #include_next.
1461///
Chris Lattnercb283342006-06-18 06:48:37 +00001462void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1463 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001464
1465 // #include_next is like #include, except that we start searching after
1466 // the current found directory. If we can't do this, issue a
1467 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001468 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner69772b02006-07-02 20:34:39 +00001469 if (isInPrimaryFile()) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001471 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001472 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001473 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001474 } else {
1475 // Start looking up in the next directory.
1476 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001477 }
1478
1479 return HandleIncludeDirective(IncludeNextTok, Lookup);
1480}
1481
1482/// HandleImportDirective - Implements #import.
1483///
Chris Lattnercb283342006-06-18 06:48:37 +00001484void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1485 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001486
1487 return HandleIncludeDirective(ImportTok, 0, true);
1488}
1489
Chris Lattnerb8761832006-06-24 21:31:03 +00001490//===----------------------------------------------------------------------===//
1491// Preprocessor Macro Directive Handling.
1492//===----------------------------------------------------------------------===//
1493
Chris Lattnercefc7682006-07-08 08:28:12 +00001494/// ReadMacroDefinitionArgList - The ( starting an argument list of a macro
1495/// definition has just been read. Lex the rest of the arguments and the
1496/// closing ), updating MI with what we learn. Return true if an error occurs
1497/// parsing the arg list.
1498bool Preprocessor::ReadMacroDefinitionArgList(MacroInfo *MI) {
1499 LexerToken Tok;
Chris Lattnercefc7682006-07-08 08:28:12 +00001500 while (1) {
1501 LexUnexpandedToken(Tok);
1502 switch (Tok.getKind()) {
1503 case tok::r_paren:
1504 // Found the end of the argument list.
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001505 if (MI->arg_begin() == MI->arg_end()) return false; // #define FOO()
Chris Lattnercefc7682006-07-08 08:28:12 +00001506 // Otherwise we have #define FOO(A,)
1507 Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
1508 return true;
1509 case tok::ellipsis: // #define X(... -> C99 varargs
1510 // Warn if use of C99 feature in non-C99 mode.
1511 if (!Features.C99) Diag(Tok, diag::ext_variadic_macro);
1512
1513 // Lex the token after the identifier.
1514 LexUnexpandedToken(Tok);
1515 if (Tok.getKind() != tok::r_paren) {
1516 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1517 return true;
1518 }
1519 MI->setIsC99Varargs();
1520 return false;
1521 case tok::eom: // #define X(
1522 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1523 return true;
1524 default: // #define X(1
1525 Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
1526 return true;
1527 case tok::identifier:
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001528 IdentifierInfo *II = Tok.getIdentifierInfo();
1529
1530 // If this is already used as an argument, it is used multiple times (e.g.
1531 // #define X(A,A.
1532 if (II->isMacroArg()) { // C99 6.10.3p6
1533 Diag(Tok, diag::err_pp_duplicate_name_in_arg_list, II->getName());
1534 return true;
1535 }
1536
1537 // Add the argument to the macro info.
1538 MI->addArgument(II);
1539 // Remember it is an argument now.
1540 II->setIsMacroArg(true);
Chris Lattnercefc7682006-07-08 08:28:12 +00001541
1542 // Lex the token after the identifier.
1543 LexUnexpandedToken(Tok);
1544
1545 switch (Tok.getKind()) {
1546 default: // #define X(A B
1547 Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
1548 return true;
1549 case tok::r_paren: // #define X(A)
1550 return false;
1551 case tok::comma: // #define X(A,
1552 break;
1553 case tok::ellipsis: // #define X(A... -> GCC extension
1554 // Diagnose extension.
1555 Diag(Tok, diag::ext_named_variadic_macro);
1556
1557 // Lex the token after the identifier.
1558 LexUnexpandedToken(Tok);
1559 if (Tok.getKind() != tok::r_paren) {
1560 Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
1561 return true;
1562 }
1563
1564 MI->setIsGNUVarargs();
1565 return false;
1566 }
1567 }
1568 }
1569}
1570
Chris Lattner22eb9722006-06-18 05:43:12 +00001571/// HandleDefineDirective - Implements #define. This consumes the entire macro
1572/// line then lets the caller lex the next real token.
1573///
Chris Lattnercb283342006-06-18 06:48:37 +00001574void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001575 ++NumDefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001576
Chris Lattner22eb9722006-06-18 05:43:12 +00001577 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001578 ReadMacroName(MacroNameTok, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +00001579
1580 // Error reading macro name? If so, diagnostic already issued.
1581 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001582 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001583
Chris Lattner50b497e2006-06-18 16:32:35 +00001584 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001585
1586 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001587 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001588
Chris Lattner78186052006-07-09 00:45:31 +00001589 // FIXME: Enable __VA_ARGS__.
1590
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001591 // If this is a function-like macro definition, parse the argument list,
1592 // marking each of the identifiers as being used as macro arguments. Also,
1593 // check other constraints on the first token of the macro body.
Chris Lattner22eb9722006-06-18 05:43:12 +00001594 if (Tok.getKind() == tok::eom) {
1595 // If there is no body to this macro, we have no special handling here.
1596 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
Chris Lattnercefc7682006-07-08 08:28:12 +00001597 // This is a function-like macro definition. Read the argument list.
1598 MI->setIsFunctionLike();
1599 if (ReadMacroDefinitionArgList(MI)) {
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001600 // Clear the "isMacroArg" flags from all the macro arguments parsed.
1601 MI->SetIdentifierIsMacroArgFlags(false);
1602 // Forget about MI.
Chris Lattnercefc7682006-07-08 08:28:12 +00001603 delete MI;
Chris Lattner6e0d42c2006-07-08 20:32:52 +00001604 // Throw away the rest of the line.
Chris Lattnercefc7682006-07-08 08:28:12 +00001605 if (CurLexer->ParsingPreprocessorDirective)
1606 DiscardUntilEndOfDirective();
1607 return;
1608 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001609
Chris Lattner815a1f92006-07-08 20:48:04 +00001610 // Read the first token after the arg list for down below.
1611 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001612 } else if (!Tok.hasLeadingSpace()) {
1613 // C99 requires whitespace between the macro definition and the body. Emit
1614 // a diagnostic for something like "#define X+".
1615 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001616 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001617 } else {
1618 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1619 // one in some cases!
1620 }
1621 } else {
1622 // This is a normal token with leading space. Clear the leading space
1623 // marker on the first token to get proper expansion.
1624 Tok.ClearFlag(LexerToken::LeadingSpace);
1625 }
1626
1627 // Read the rest of the macro body.
1628 while (Tok.getKind() != tok::eom) {
1629 MI->AddTokenToBody(Tok);
Chris Lattner815a1f92006-07-08 20:48:04 +00001630
1631 // Check C99 6.10.3.2p1: ensure that # operators are followed by macro
1632 // parameters.
1633 if (Tok.getKind() != tok::hash) {
1634 // Get the next token of the macro.
1635 LexUnexpandedToken(Tok);
1636 continue;
1637 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001638
Chris Lattner815a1f92006-07-08 20:48:04 +00001639 // Get the next token of the macro.
1640 LexUnexpandedToken(Tok);
1641
1642 // Not a macro arg identifier?
1643 if (!Tok.getIdentifierInfo() || !Tok.getIdentifierInfo()->isMacroArg()) {
1644 Diag(Tok, diag::err_pp_stringize_not_parameter);
1645 // Clear the "isMacroArg" flags from all the macro arguments.
1646 MI->SetIdentifierIsMacroArgFlags(false);
1647 delete MI;
1648 return;
1649 }
1650
1651 // Things look ok, add the param name token to the macro.
1652 MI->AddTokenToBody(Tok);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001653
Chris Lattner22eb9722006-06-18 05:43:12 +00001654 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001655 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001656 }
Chris Lattnerbff18d52006-07-06 04:49:18 +00001657
Chris Lattner78186052006-07-09 00:45:31 +00001658 // Clear the "isMacroArg" flags from all the macro arguments.
1659 MI->SetIdentifierIsMacroArgFlags(false);
1660
Chris Lattnerbff18d52006-07-06 04:49:18 +00001661 // Check that there is no paste (##) operator at the begining or end of the
1662 // replacement list.
Chris Lattner78186052006-07-09 00:45:31 +00001663 unsigned NumTokens = MI->getNumTokens();
Chris Lattnerbff18d52006-07-06 04:49:18 +00001664 if (NumTokens != 0) {
1665 if (MI->getReplacementToken(0).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001666 Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001667 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001668 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001669 }
1670 if (MI->getReplacementToken(NumTokens-1).getKind() == tok::hashhash) {
Chris Lattner815a1f92006-07-08 20:48:04 +00001671 Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
Chris Lattnerbff18d52006-07-06 04:49:18 +00001672 delete MI;
Chris Lattner815a1f92006-07-08 20:48:04 +00001673 return;
Chris Lattnerbff18d52006-07-06 04:49:18 +00001674 }
1675 }
1676
Chris Lattner13044d92006-07-03 05:16:44 +00001677 // If this is the primary source file, remember that this macro hasn't been
1678 // used yet.
1679 if (isInPrimaryFile())
1680 MI->setIsUsed(false);
1681
Chris Lattner22eb9722006-06-18 05:43:12 +00001682 // Finally, if this identifier already had a macro defined for it, verify that
1683 // the macro bodies are identical and free the old definition.
1684 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
Chris Lattner13044d92006-07-03 05:16:44 +00001685 if (!OtherMI->isUsed())
1686 Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
1687
Chris Lattner22eb9722006-06-18 05:43:12 +00001688 // Macros must be identical. This means all tokes and whitespace separation
Chris Lattner21284df2006-07-08 07:16:08 +00001689 // must be the same. C99 6.10.3.2.
1690 if (!MI->isIdenticalTo(*OtherMI, *this)) {
Chris Lattnere8eef322006-07-08 07:01:00 +00001691 Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef,
1692 MacroNameTok.getIdentifierInfo()->getName());
1693 Diag(OtherMI->getDefinitionLoc(), diag::ext_pp_macro_redef2);
1694 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001695 delete OtherMI;
1696 }
1697
1698 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001699}
1700
1701
1702/// HandleUndefDirective - Implements #undef.
1703///
Chris Lattnercb283342006-06-18 06:48:37 +00001704void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001705 ++NumUndefined;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001706
Chris Lattner22eb9722006-06-18 05:43:12 +00001707 LexerToken MacroNameTok;
Chris Lattnere8eef322006-07-08 07:01:00 +00001708 ReadMacroName(MacroNameTok, 2);
Chris Lattner22eb9722006-06-18 05:43:12 +00001709
1710 // Error reading macro name? If so, diagnostic already issued.
1711 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001712 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001713
1714 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001715 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001716
1717 // Okay, we finally have a valid identifier to undef.
1718 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1719
1720 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001721 if (MI == 0) return;
Chris Lattner677757a2006-06-28 05:26:32 +00001722
Chris Lattner13044d92006-07-03 05:16:44 +00001723 if (!MI->isUsed())
1724 Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
Chris Lattner22eb9722006-06-18 05:43:12 +00001725
1726 // Free macro definition.
1727 delete MI;
1728 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001729}
1730
1731
Chris Lattnerb8761832006-06-24 21:31:03 +00001732//===----------------------------------------------------------------------===//
1733// Preprocessor Conditional Directive Handling.
1734//===----------------------------------------------------------------------===//
1735
Chris Lattner22eb9722006-06-18 05:43:12 +00001736/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
Chris Lattner371ac8a2006-07-04 07:11:10 +00001737/// true when this is a #ifndef directive. ReadAnyTokensBeforeDirective is true
1738/// if any tokens have been returned or pp-directives activated before this
1739/// #ifndef has been lexed.
Chris Lattner22eb9722006-06-18 05:43:12 +00001740///
Chris Lattner371ac8a2006-07-04 07:11:10 +00001741void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef,
1742 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001743 ++NumIf;
1744 LexerToken DirectiveTok = Result;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001745
Chris Lattner22eb9722006-06-18 05:43:12 +00001746 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001747 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001748
1749 // Error reading macro name? If so, diagnostic already issued.
1750 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001751 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001752
1753 // Check to see if this is the last token on the #if[n]def line.
Chris Lattner371ac8a2006-07-04 07:11:10 +00001754 CheckEndOfDirective(isIfndef ? "#ifndef" : "#ifdef");
1755
1756 // If the start of a top-level #ifdef, inform MIOpt.
1757 if (!ReadAnyTokensBeforeDirective &&
1758 CurLexer->getConditionalStackDepth() == 0) {
1759 assert(isIfndef && "#ifdef shouldn't reach here");
1760 CurLexer->MIOpt.EnterTopLevelIFNDEF(MacroNameTok.getIdentifierInfo());
1761 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001762
Chris Lattnera78a97e2006-07-03 05:42:18 +00001763 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1764
1765 // If there is a macro, mark it used.
1766 if (MI) MI->setIsUsed(true);
1767
Chris Lattner22eb9722006-06-18 05:43:12 +00001768 // Should we include the stuff contained by this directive?
Chris Lattnera78a97e2006-07-03 05:42:18 +00001769 if (!MI == isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001770 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001771 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001772 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001773 } else {
1774 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001775 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001776 /*Foundnonskip*/false,
1777 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001778 }
1779}
1780
1781/// HandleIfDirective - Implements the #if directive.
1782///
Chris Lattnera8654ca2006-07-04 17:42:08 +00001783void Preprocessor::HandleIfDirective(LexerToken &IfToken,
1784 bool ReadAnyTokensBeforeDirective) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001785 ++NumIf;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001786
Chris Lattner371ac8a2006-07-04 07:11:10 +00001787 // Parse and evaluation the conditional expression.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +00001788 IdentifierInfo *IfNDefMacro = 0;
Chris Lattnera8654ca2006-07-04 17:42:08 +00001789 bool ConditionalTrue = EvaluateDirectiveExpression(IfNDefMacro);
Chris Lattner22eb9722006-06-18 05:43:12 +00001790
1791 // Should we include the stuff contained by this directive?
1792 if (ConditionalTrue) {
Chris Lattnera8654ca2006-07-04 17:42:08 +00001793 // If this condition is equivalent to #ifndef X, and if this is the first
1794 // directive seen, handle it for the multiple-include optimization.
1795 if (!ReadAnyTokensBeforeDirective &&
1796 CurLexer->getConditionalStackDepth() == 0 && IfNDefMacro)
1797 CurLexer->MIOpt.EnterTopLevelIFNDEF(IfNDefMacro);
1798
Chris Lattner22eb9722006-06-18 05:43:12 +00001799 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001800 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001801 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001802 } else {
1803 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001804 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001805 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001806 }
1807}
1808
1809/// HandleEndifDirective - Implements the #endif directive.
1810///
Chris Lattnercb283342006-06-18 06:48:37 +00001811void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001812 ++NumEndif;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001813
Chris Lattner22eb9722006-06-18 05:43:12 +00001814 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001815 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001816
1817 PPConditionalInfo CondInfo;
1818 if (CurLexer->popConditionalLevel(CondInfo)) {
1819 // No conditionals on the stack: this is an #endif without an #if.
1820 return Diag(EndifToken, diag::err_pp_endif_without_if);
1821 }
1822
Chris Lattner371ac8a2006-07-04 07:11:10 +00001823 // If this the end of a top-level #endif, inform MIOpt.
1824 if (CurLexer->getConditionalStackDepth() == 0)
1825 CurLexer->MIOpt.ExitTopLevelConditional();
1826
Chris Lattner22eb9722006-06-18 05:43:12 +00001827 assert(!CondInfo.WasSkipping && !isSkipping() &&
1828 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001829}
1830
1831
Chris Lattnercb283342006-06-18 06:48:37 +00001832void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001833 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001834
Chris Lattner22eb9722006-06-18 05:43:12 +00001835 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001836 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001837
1838 PPConditionalInfo CI;
1839 if (CurLexer->popConditionalLevel(CI))
1840 return Diag(Result, diag::pp_err_else_without_if);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001841
1842 // If this is a top-level #else, inform the MIOpt.
1843 if (CurLexer->getConditionalStackDepth() == 0)
1844 CurLexer->MIOpt.FoundTopLevelElse();
Chris Lattner22eb9722006-06-18 05:43:12 +00001845
1846 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001847 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001848
1849 // Finally, skip the rest of the contents of this block and return the first
1850 // token after it.
1851 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1852 /*FoundElse*/true);
1853}
1854
Chris Lattnercb283342006-06-18 06:48:37 +00001855void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001856 ++NumElse;
Chris Lattner371ac8a2006-07-04 07:11:10 +00001857
Chris Lattner22eb9722006-06-18 05:43:12 +00001858 // #elif directive in a non-skipping conditional... start skipping.
1859 // We don't care what the condition is, because we will always skip it (since
1860 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001861 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001862
1863 PPConditionalInfo CI;
1864 if (CurLexer->popConditionalLevel(CI))
1865 return Diag(ElifToken, diag::pp_err_elif_without_if);
1866
Chris Lattner371ac8a2006-07-04 07:11:10 +00001867 // If this is a top-level #elif, inform the MIOpt.
1868 if (CurLexer->getConditionalStackDepth() == 0)
1869 CurLexer->MIOpt.FoundTopLevelElse();
1870
Chris Lattner22eb9722006-06-18 05:43:12 +00001871 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001872 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001873
1874 // Finally, skip the rest of the contents of this block and return the first
1875 // token after it.
1876 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1877 /*FoundElse*/CI.FoundElse);
1878}
Chris Lattnerb8761832006-06-24 21:31:03 +00001879