blob: 017166b42dcaec1debf689d80b334289ec5df262 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15//
16// ERROR : __VA_ARGS__ can only appear in the expansion of a C99 variadic macro
17//
18// Options to support:
19// -H - Print the name of each header file used.
20// -C -CC - Do not discard comments for cpp.
21// -P - Do not emit #line directives.
22// -d[MDNI] - Dump various things.
23// -fworking-directory - #line's with preprocessor's working dir.
24// -fpreprocessed
25// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
26// -W*
27// -w
28//
29// Messages to emit:
30// "Multiple include guards may be useful for:\n"
31//
32// TODO: Implement the include guard optimization.
33//
34//===----------------------------------------------------------------------===//
35
36#include "clang/Lex/Preprocessor.h"
37#include "clang/Lex/MacroInfo.h"
38#include "clang/Basic/Diagnostic.h"
39#include "clang/Basic/FileManager.h"
40#include "clang/Basic/SourceManager.h"
41#include <iostream>
42using namespace llvm;
43using namespace clang;
44
45//===----------------------------------------------------------------------===//
46
47Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
48 FileManager &FM, SourceManager &SM)
49 : Diags(diags), Features(opts), FileMgr(FM), SourceMgr(SM),
50 SystemDirIdx(0), NoCurDirSearch(false),
Chris Lattnerc8997182006-06-22 05:52:16 +000051 CurLexer(0), CurDirLookup(0), CurMacroExpander(0) {
Chris Lattner22eb9722006-06-18 05:43:12 +000052 // Clear stats.
53 NumDirectives = NumIncluded = NumDefined = NumUndefined = NumPragma = 0;
54 NumIf = NumElse = NumEndif = 0;
55 NumEnteredSourceFiles = NumMacroExpanded = NumFastMacroExpanded = 0;
56 MaxIncludeStackDepth = MaxMacroStackDepth = 0;
57 NumSkipped = 0;
Chris Lattner0c885f52006-06-21 06:50:18 +000058
Chris Lattner22eb9722006-06-18 05:43:12 +000059 // Macro expansion is enabled.
60 DisableMacroExpansion = false;
61 SkippingContents = false;
Chris Lattner0c885f52006-06-21 06:50:18 +000062
63 // There is no file-change handler yet.
64 FileChangeHandler = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +000065}
66
67Preprocessor::~Preprocessor() {
68 // Free any active lexers.
69 delete CurLexer;
70
71 while (!IncludeStack.empty()) {
72 delete IncludeStack.back().TheLexer;
73 IncludeStack.pop_back();
74 }
75}
76
77/// getFileInfo - Return the PerFileInfo structure for the specified
78/// FileEntry.
79Preprocessor::PerFileInfo &Preprocessor::getFileInfo(const FileEntry *FE) {
80 if (FE->getUID() >= FileInfo.size())
81 FileInfo.resize(FE->getUID()+1);
82 return FileInfo[FE->getUID()];
83}
84
85
86/// AddKeywords - Add all keywords to the symbol table.
87///
88void Preprocessor::AddKeywords() {
89 enum {
90 C90Shift = 0,
91 EXTC90 = 1 << C90Shift,
92 NOTC90 = 2 << C90Shift,
93 C99Shift = 2,
94 EXTC99 = 1 << C99Shift,
95 NOTC99 = 2 << C99Shift,
96 CPPShift = 4,
97 EXTCPP = 1 << CPPShift,
98 NOTCPP = 2 << CPPShift,
99 Mask = 3
100 };
101
102 // Add keywords and tokens for the current language.
103#define KEYWORD(NAME, FLAGS) \
104 AddKeyword(#NAME+1, tok::kw##NAME, \
105 (FLAGS >> C90Shift) & Mask, \
106 (FLAGS >> C99Shift) & Mask, \
107 (FLAGS >> CPPShift) & Mask);
108#define ALIAS(NAME, TOK) \
109 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0);
110#include "clang/Basic/TokenKinds.def"
111}
112
113/// Diag - Forwarding function for diagnostics. This emits a diagnostic at
114/// the specified LexerToken's location, translating the token's start
115/// position in the current buffer into a SourcePosition object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000116void Preprocessor::Diag(SourceLocation Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000117 const std::string &Msg) {
118 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
119 // warnings or extensions.
120 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000121 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000122
Chris Lattnercb283342006-06-18 06:48:37 +0000123 Diags.Report(Loc, DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000124}
Chris Lattnercb283342006-06-18 06:48:37 +0000125void Preprocessor::Diag(const LexerToken &Tok, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000126 const std::string &Msg) {
127 // If we are in a '#if 0' block, don't emit any diagnostics for notes,
128 // warnings or extensions.
129 if (isSkipping() && Diagnostic::isNoteWarningOrExtension(DiagID))
Chris Lattnercb283342006-06-18 06:48:37 +0000130 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000131
Chris Lattner50b497e2006-06-18 16:32:35 +0000132 Diag(Tok.getLocation(), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000133}
134
Chris Lattnerd01e2912006-06-18 16:22:51 +0000135
136void Preprocessor::DumpToken(const LexerToken &Tok, bool DumpFlags) const {
137 std::cerr << tok::getTokenName(Tok.getKind()) << " '"
138 << getSpelling(Tok) << "'";
139
140 if (!DumpFlags) return;
141 std::cerr << "\t";
142 if (Tok.isAtStartOfLine())
143 std::cerr << " [StartOfLine]";
144 if (Tok.hasLeadingSpace())
145 std::cerr << " [LeadingSpace]";
146 if (Tok.needsCleaning()) {
Chris Lattner50b497e2006-06-18 16:32:35 +0000147 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148 std::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
149 << "']";
150 }
151}
152
153void Preprocessor::DumpMacro(const MacroInfo &MI) const {
154 std::cerr << "MACRO: ";
155 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
156 DumpToken(MI.getReplacementToken(i));
157 std::cerr << " ";
158 }
159 std::cerr << "\n";
160}
161
Chris Lattner22eb9722006-06-18 05:43:12 +0000162void Preprocessor::PrintStats() {
163 std::cerr << "\n*** Preprocessor Stats:\n";
164 std::cerr << FileInfo.size() << " files tracked.\n";
165 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
166 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
167 NumOnceOnlyFiles += FileInfo[i].isImport;
168 if (MaxNumIncludes < FileInfo[i].NumIncludes)
169 MaxNumIncludes = FileInfo[i].NumIncludes;
170 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
171 }
172 std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
173 std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n";
174 std::cerr << " " << MaxNumIncludes << " max times a file is included.\n";
175
176 std::cerr << NumDirectives << " directives found:\n";
177 std::cerr << " " << NumDefined << " #define.\n";
178 std::cerr << " " << NumUndefined << " #undef.\n";
179 std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n";
180 std::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
181 std::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
182 std::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
183 std::cerr << " " << NumElse << " #else/#elif.\n";
184 std::cerr << " " << NumEndif << " #endif.\n";
185 std::cerr << " " << NumPragma << " #pragma.\n";
186 std::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
187
188 std::cerr << NumMacroExpanded << " macros expanded, "
189 << NumFastMacroExpanded << " on the fast path.\n";
190 if (MaxMacroStackDepth > 1)
191 std::cerr << " " << MaxMacroStackDepth << " max macroexpand stack depth\n";
192}
193
194//===----------------------------------------------------------------------===//
Chris Lattnerd01e2912006-06-18 16:22:51 +0000195// Token Spelling
196//===----------------------------------------------------------------------===//
197
198
199/// getSpelling() - Return the 'spelling' of this token. The spelling of a
200/// token are the characters used to represent the token in the source file
201/// after trigraph expansion and escaped-newline folding. In particular, this
202/// wants to get the true, uncanonicalized, spelling of things like digraphs
203/// UCNs, etc.
204std::string Preprocessor::getSpelling(const LexerToken &Tok) const {
205 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
206
207 // If this token contains nothing interesting, return it directly.
Chris Lattner50b497e2006-06-18 16:32:35 +0000208 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000209 assert(TokStart && "Token has invalid location!");
210 if (!Tok.needsCleaning())
211 return std::string(TokStart, TokStart+Tok.getLength());
212
213 // Otherwise, hard case, relex the characters into the string.
214 std::string Result;
215 Result.reserve(Tok.getLength());
216
217 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
218 Ptr != End; ) {
219 unsigned CharSize;
220 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
221 Ptr += CharSize;
222 }
223 assert(Result.size() != unsigned(Tok.getLength()) &&
224 "NeedsCleaning flag set on something that didn't need cleaning!");
225 return Result;
226}
227
228/// getSpelling - This method is used to get the spelling of a token into a
229/// preallocated buffer, instead of as an std::string. The caller is required
230/// to allocate enough space for the token, which is guaranteed to be at least
231/// Tok.getLength() bytes long. The actual length of the token is returned.
232unsigned Preprocessor::getSpelling(const LexerToken &Tok, char *Buffer) const {
233 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
234
Chris Lattner50b497e2006-06-18 16:32:35 +0000235 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000236 assert(TokStart && "Token has invalid location!");
237
238 // If this token contains nothing interesting, return it directly.
239 if (!Tok.needsCleaning()) {
240 unsigned Size = Tok.getLength();
241 memcpy(Buffer, TokStart, Size);
242 return Size;
243 }
244 // Otherwise, hard case, relex the characters into the string.
245 std::string Result;
246 Result.reserve(Tok.getLength());
247
248 char *OutBuf = Buffer;
249 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
250 Ptr != End; ) {
251 unsigned CharSize;
252 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
253 Ptr += CharSize;
254 }
255 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
256 "NeedsCleaning flag set on something that didn't need cleaning!");
257
258 return OutBuf-Buffer;
259}
260
261//===----------------------------------------------------------------------===//
Chris Lattner22eb9722006-06-18 05:43:12 +0000262// Source File Location Methods.
263//===----------------------------------------------------------------------===//
264
265
266/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
267/// return null on failure. isAngled indicates whether the file reference is
268/// for system #include's or not (i.e. using <> instead of "").
269const FileEntry *Preprocessor::LookupFile(const std::string &Filename,
Chris Lattnerc8997182006-06-22 05:52:16 +0000270 bool isAngled,
Chris Lattner22eb9722006-06-18 05:43:12 +0000271 const DirectoryLookup *FromDir,
Chris Lattnerc8997182006-06-22 05:52:16 +0000272 const DirectoryLookup *&CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000273 assert(CurLexer && "Cannot enter a #include inside a macro expansion!");
Chris Lattnerc8997182006-06-22 05:52:16 +0000274 CurDir = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000275
276 // If 'Filename' is absolute, check to see if it exists and no searching.
277 // FIXME: this should be a sys::Path interface, this doesn't handle things
278 // like C:\foo.txt right, nor win32 \\network\device\blah.
279 if (Filename[0] == '/') {
280 // If this was an #include_next "/absolute/file", fail.
281 if (FromDir) return 0;
282
283 // Otherwise, just return the file.
284 return FileMgr.getFile(Filename);
285 }
286
287 // Step #0, unless disabled, check to see if the file is in the #includer's
288 // directory. This search is not done for <> headers.
Chris Lattnerc8997182006-06-22 05:52:16 +0000289 if (!isAngled && !FromDir && !NoCurDirSearch) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000290 const FileEntry *CurFE =
291 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID());
292 if (CurFE) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000293 // Concatenate the requested file onto the directory.
294 // FIXME: should be in sys::Path.
Chris Lattner22eb9722006-06-18 05:43:12 +0000295 if (const FileEntry *FE =
296 FileMgr.getFile(CurFE->getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000297 if (CurDirLookup)
298 CurDir = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000299 else
Chris Lattnerc8997182006-06-22 05:52:16 +0000300 CurDir = 0;
301
302 // This file is a system header or C++ unfriendly if the old file is.
303 getFileInfo(FE).DirInfo = getFileInfo(CurFE).DirInfo;
Chris Lattner22eb9722006-06-18 05:43:12 +0000304 return FE;
305 }
306 }
307 }
308
309 // If this is a system #include, ignore the user #include locs.
Chris Lattnerc8997182006-06-22 05:52:16 +0000310 unsigned i = isAngled ? SystemDirIdx : 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000311
312 // If this is a #include_next request, start searching after the directory the
313 // file was found in.
314 if (FromDir)
315 i = FromDir-&SearchDirs[0];
316
317 // Check each directory in sequence to see if it contains this file.
318 for (; i != SearchDirs.size(); ++i) {
319 // Concatenate the requested file onto the directory.
320 // FIXME: should be in sys::Path.
321 if (const FileEntry *FE =
322 FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename)) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000323 CurDir = &SearchDirs[i];
324
325 // This file is a system header or C++ unfriendly if the dir is.
326 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Chris Lattner22eb9722006-06-18 05:43:12 +0000327 return FE;
328 }
329 }
330
331 // Otherwise, didn't find it.
332 return 0;
333}
334
335/// EnterSourceFile - Add a source file to the top of the include stack and
336/// start lexing tokens from it instead of the current buffer. Return true
337/// on failure.
338void Preprocessor::EnterSourceFile(unsigned FileID,
Chris Lattnerc8997182006-06-22 05:52:16 +0000339 const DirectoryLookup *CurDir) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000340 ++NumEnteredSourceFiles;
341
342 // Add the current lexer to the include stack.
343 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000344 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
Chris Lattner22eb9722006-06-18 05:43:12 +0000345 } else {
346 assert(CurMacroExpander == 0 && "Cannot #include a file inside a macro!");
347 }
348
349 if (MaxIncludeStackDepth < IncludeStack.size())
350 MaxIncludeStackDepth = IncludeStack.size();
351
352 const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
353
Chris Lattnerc8997182006-06-22 05:52:16 +0000354 CurLexer = new Lexer(Buffer, FileID, *this);
355 CurDirLookup = CurDir;
Chris Lattner0c885f52006-06-21 06:50:18 +0000356
357 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000358 if (FileChangeHandler) {
359 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
360
361 // Get the file entry for the current file.
362 if (const FileEntry *FE =
363 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
364 FileType = getFileInfo(FE).DirInfo;
365
366 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferStart), true,
367 FileType);
368 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000369}
370
371/// EnterMacro - Add a Macro to the top of the include stack and start lexing
Chris Lattnercb283342006-06-18 06:48:37 +0000372/// tokens from it instead of the current buffer.
373void Preprocessor::EnterMacro(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000374 IdentifierTokenInfo *Identifier = Tok.getIdentifierInfo();
375 MacroInfo &MI = *Identifier->getMacroInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +0000376 if (CurLexer) {
Chris Lattnerc8997182006-06-22 05:52:16 +0000377 IncludeStack.push_back(IncludeStackInfo(CurLexer, CurDirLookup));
378 CurLexer = 0;
379 CurDirLookup = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 } else if (CurMacroExpander) {
381 MacroStack.push_back(CurMacroExpander);
382 }
383
384 if (MaxMacroStackDepth < MacroStack.size())
385 MaxMacroStackDepth = MacroStack.size();
386
387 // TODO: Figure out arguments.
388
389 // Mark the macro as currently disabled, so that it is not recursively
390 // expanded.
391 MI.DisableMacro();
Chris Lattnerd01e2912006-06-18 16:22:51 +0000392 CurMacroExpander = new MacroExpander(Tok, *this);
Chris Lattner22eb9722006-06-18 05:43:12 +0000393}
394
395
396//===----------------------------------------------------------------------===//
397// Lexer Event Handling.
398//===----------------------------------------------------------------------===//
399
400/// HandleIdentifier - This callback is invoked when the lexer reads an
401/// identifier. This callback looks up the identifier in the map and/or
402/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnercb283342006-06-18 06:48:37 +0000403void Preprocessor::HandleIdentifier(LexerToken &Identifier) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000404 if (Identifier.getIdentifierInfo() == 0) {
405 // If we are skipping tokens (because we are in a #if 0 block), there will
406 // be no identifier info, just return the token.
407 assert(isSkipping() && "Token isn't an identifier?");
Chris Lattnercb283342006-06-18 06:48:37 +0000408 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000409 }
410 IdentifierTokenInfo &ITI = *Identifier.getIdentifierInfo();
411
412 // FIXME: Check for poisoning in ITI?
413
414 if (MacroInfo *MI = ITI.getMacroInfo()) {
415 if (MI->isEnabled() && !DisableMacroExpansion) {
416 ++NumMacroExpanded;
417 // If we started lexing a macro, enter the macro expansion body.
418 // FIXME: Read/Validate the argument list here!
419
420 // If this macro expands to no tokens, don't bother to push it onto the
421 // expansion stack, only to take it right back off.
422 if (MI->getNumTokens() == 0) {
423 // Ignore this macro use, just return the next token in the current
424 // buffer.
425 bool HadLeadingSpace = Identifier.hasLeadingSpace();
426 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
427
Chris Lattnercb283342006-06-18 06:48:37 +0000428 Lex(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000429
430 // If the identifier isn't on some OTHER line, inherit the leading
431 // whitespace/first-on-a-line property of this token. This handles
432 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
433 // empty.
434 if (!Identifier.isAtStartOfLine()) {
435 if (IsAtStartOfLine) Identifier.SetFlag(LexerToken::StartOfLine);
436 if (HadLeadingSpace) Identifier.SetFlag(LexerToken::LeadingSpace);
437 }
438 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000439 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000440
441 } else if (MI->getNumTokens() == 1 &&
442 // Don't handle identifiers, which might need recursive
443 // expansion.
444 MI->getReplacementToken(0).getIdentifierInfo() == 0) {
445 // FIXME: Function-style macros only if no arguments?
446
447 // Otherwise, if this macro expands into a single trivially-expanded
448 // token: expand it now. This handles common cases like
449 // "#define VAL 42".
450
451 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
452 // identifier to the expanded token.
453 bool isAtStartOfLine = Identifier.isAtStartOfLine();
454 bool hasLeadingSpace = Identifier.hasLeadingSpace();
455
456 // Replace the result token.
457 Identifier = MI->getReplacementToken(0);
458
459 // Restore the StartOfLine/LeadingSpace markers.
460 Identifier.SetFlagValue(LexerToken::StartOfLine , isAtStartOfLine);
461 Identifier.SetFlagValue(LexerToken::LeadingSpace, hasLeadingSpace);
462
463 // FIXME: Get correct macro expansion stack location info!
464
465 // Since this is not an identifier token, it can't be macro expanded, so
466 // we're done.
467 ++NumFastMacroExpanded;
Chris Lattnercb283342006-06-18 06:48:37 +0000468 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000469 }
470
471 // Start expanding the macro (FIXME, pass arguments).
Chris Lattnercb283342006-06-18 06:48:37 +0000472 EnterMacro(Identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000473
474 // Now that the macro is at the top of the include stack, ask the
475 // preprocessor to read the next token from it.
476 return Lex(Identifier);
477 }
478 }
479
480 // Change the kind of this identifier to the appropriate token kind, e.g.
481 // turning "for" into a keyword.
482 Identifier.SetKind(ITI.getTokenID());
483
484 // If this is an extension token, diagnose its use.
Chris Lattnercb283342006-06-18 06:48:37 +0000485 if (ITI.isExtensionToken()) Diag(Identifier, diag::ext_token_used);
Chris Lattner22eb9722006-06-18 05:43:12 +0000486}
487
488/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
489/// the current file. This either returns the EOF token or pops a level off
490/// the include stack and keeps going.
Chris Lattner0c885f52006-06-21 06:50:18 +0000491void Preprocessor::HandleEndOfFile(LexerToken &Result, bool isEndOfMacro) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000492 assert(!CurMacroExpander &&
493 "Ending a file when currently in a macro!");
494
495 // If we are in a #if 0 block skipping tokens, and we see the end of the file,
496 // this is an error condition. Just return the EOF token up to
497 // SkipExcludedConditionalBlock. The Lexer will have already have issued
498 // errors for the unterminated #if's on the conditional stack.
499 if (isSkipping()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000500 Result.StartToken();
501 CurLexer->BufferPtr = CurLexer->BufferEnd;
502 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000503 Result.SetKind(tok::eof);
Chris Lattnercb283342006-06-18 06:48:37 +0000504 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000505 }
506
507 // If this is a #include'd file, pop it off the include stack and continue
508 // lexing the #includer file.
509 if (!IncludeStack.empty()) {
510 // We're done with the #included file.
511 delete CurLexer;
Chris Lattnerc8997182006-06-22 05:52:16 +0000512 CurLexer = IncludeStack.back().TheLexer;
513 CurDirLookup = IncludeStack.back().TheDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +0000514 IncludeStack.pop_back();
Chris Lattner0c885f52006-06-21 06:50:18 +0000515
516 // Notify the client, if desired, that we are in a new source file.
Chris Lattnerc8997182006-06-22 05:52:16 +0000517 if (FileChangeHandler && !isEndOfMacro) {
518 DirectoryLookup::DirType FileType = DirectoryLookup::NormalHeaderDir;
519
520 // Get the file entry for the current file.
521 if (const FileEntry *FE =
522 SourceMgr.getFileEntryForFileID(CurLexer->getCurFileID()))
523 FileType = getFileInfo(FE).DirInfo;
524
Chris Lattner0c885f52006-06-21 06:50:18 +0000525 FileChangeHandler(CurLexer->getSourceLocation(CurLexer->BufferPtr),
Chris Lattnerc8997182006-06-22 05:52:16 +0000526 false, FileType);
527 }
Chris Lattner0c885f52006-06-21 06:50:18 +0000528
Chris Lattner22eb9722006-06-18 05:43:12 +0000529 return Lex(Result);
530 }
531
Chris Lattnerd01e2912006-06-18 16:22:51 +0000532 Result.StartToken();
533 CurLexer->BufferPtr = CurLexer->BufferEnd;
534 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000535 Result.SetKind(tok::eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000536
537 // We're done with the #included file.
538 delete CurLexer;
539 CurLexer = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000540}
541
542/// HandleEndOfMacro - This callback is invoked when the lexer hits the end of
Chris Lattnercb283342006-06-18 06:48:37 +0000543/// the current macro line.
544void Preprocessor::HandleEndOfMacro(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000545 assert(CurMacroExpander && !CurLexer &&
546 "Ending a macro when currently in a #include file!");
547
548 // Mark macro not ignored now that it is no longer being expanded.
549 CurMacroExpander->getMacro().EnableMacro();
550 delete CurMacroExpander;
551
552 if (!MacroStack.empty()) {
553 // In a nested macro invocation, continue lexing from the macro.
554 CurMacroExpander = MacroStack.back();
555 MacroStack.pop_back();
556 return Lex(Result);
557 } else {
558 CurMacroExpander = 0;
559 // Handle this like a #include file being popped off the stack.
Chris Lattner0c885f52006-06-21 06:50:18 +0000560 return HandleEndOfFile(Result, true);
Chris Lattner22eb9722006-06-18 05:43:12 +0000561 }
562}
563
564
565//===----------------------------------------------------------------------===//
566// Utility Methods for Preprocessor Directive Handling.
567//===----------------------------------------------------------------------===//
568
569/// DiscardUntilEndOfDirective - Read and discard all tokens remaining on the
570/// current line until the tok::eom token is found.
Chris Lattnercb283342006-06-18 06:48:37 +0000571void Preprocessor::DiscardUntilEndOfDirective() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000572 LexerToken Tmp;
573 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000574 LexUnexpandedToken(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000575 } while (Tmp.getKind() != tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +0000576}
577
578/// ReadMacroName - Lex and validate a macro name, which occurs after a
579/// #define or #undef. This sets the token kind to eom and discards the rest
580/// of the macro line if the macro name is invalid.
Chris Lattnercb283342006-06-18 06:48:37 +0000581void Preprocessor::ReadMacroName(LexerToken &MacroNameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000582 // Read the token, don't allow macro expansion on it.
Chris Lattnercb283342006-06-18 06:48:37 +0000583 LexUnexpandedToken(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000584
585 // Missing macro name?
586 if (MacroNameTok.getKind() == tok::eom)
587 return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
588
589 if (MacroNameTok.getIdentifierInfo() == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +0000590 Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000591 // Fall through on error.
592 } else if (0) {
593 // FIXME: Error if defining a C++ named operator.
594
595 } else if (0) {
596 // FIXME: Error if defining "defined", "__DATE__", and other predef macros
597 // in C99 6.10.8.4.
598 } else {
599 // Okay, we got a good identifier node. Return it.
Chris Lattnercb283342006-06-18 06:48:37 +0000600 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000601 }
602
603
604 // Invalid macro name, read and discard the rest of the line. Then set the
605 // token kind to tok::eom.
606 MacroNameTok.SetKind(tok::eom);
607 return DiscardUntilEndOfDirective();
608}
609
610/// CheckEndOfDirective - Ensure that the next token is a tok::eom token. If
611/// not, emit a diagnostic and consume up until the eom.
Chris Lattnercb283342006-06-18 06:48:37 +0000612void Preprocessor::CheckEndOfDirective(const char *DirType) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000613 LexerToken Tmp;
Chris Lattnercb283342006-06-18 06:48:37 +0000614 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000615 // There should be no tokens after the directive, but we allow them as an
616 // extension.
617 if (Tmp.getKind() != tok::eom) {
Chris Lattnercb283342006-06-18 06:48:37 +0000618 Diag(Tmp, diag::ext_pp_extra_tokens_at_eol, DirType);
619 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000620 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000621}
622
623
624
625/// SkipExcludedConditionalBlock - We just read a #if or related directive and
626/// decided that the subsequent tokens are in the #if'd out portion of the
627/// file. Lex the rest of the file, until we see an #endif. If
628/// FoundNonSkipPortion is true, then we have already emitted code for part of
629/// this #if directive, so #else/#elif blocks should never be entered. If ElseOk
630/// is true, then #else directives are ok, if not, then we have already seen one
631/// so a #else directive is a duplicate. When this returns, the caller can lex
632/// the first valid token.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000633void Preprocessor::SkipExcludedConditionalBlock(SourceLocation IfTokenLoc,
Chris Lattner22eb9722006-06-18 05:43:12 +0000634 bool FoundNonSkipPortion,
635 bool FoundElse) {
636 ++NumSkipped;
637 assert(MacroStack.empty() && CurMacroExpander == 0 && CurLexer &&
638 "Lexing a macro, not a file?");
639
640 CurLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/false,
641 FoundNonSkipPortion, FoundElse);
642
643 // Know that we are going to be skipping tokens. Set this flag to indicate
644 // this, which has a couple of effects:
645 // 1. If EOF of the current lexer is found, the include stack isn't popped.
646 // 2. Identifier information is not looked up for identifier tokens. As an
647 // effect of this, implicit macro expansion is naturally disabled.
648 // 3. "#" tokens at the start of a line are treated as normal tokens, not
649 // implicitly transformed by the lexer.
650 // 4. All notes, warnings, and extension messages are disabled.
651 //
652 SkippingContents = true;
653 LexerToken Tok;
654 while (1) {
Chris Lattnercb283342006-06-18 06:48:37 +0000655 CurLexer->Lex(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000656
657 // If this is the end of the buffer, we have an error. The lexer will have
658 // already handled this error condition, so just return and let the caller
659 // lex after this #include.
660 if (Tok.getKind() == tok::eof) break;
661
662 // If this token is not a preprocessor directive, just skip it.
663 if (Tok.getKind() != tok::hash || !Tok.isAtStartOfLine())
664 continue;
665
666 // We just parsed a # character at the start of a line, so we're in
667 // directive mode. Tell the lexer this so any newlines we see will be
668 // converted into an EOM token (this terminates the macro).
669 CurLexer->ParsingPreprocessorDirective = true;
670
671 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000672 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000673
674 // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
675 // something bogus), skip it.
676 if (Tok.getKind() != tok::identifier) {
677 CurLexer->ParsingPreprocessorDirective = false;
678 continue;
679 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000680
Chris Lattner22eb9722006-06-18 05:43:12 +0000681 // If the first letter isn't i or e, it isn't intesting to us. We know that
682 // this is safe in the face of spelling differences, because there is no way
683 // to spell an i/e in a strange way that is another letter. Skipping this
Chris Lattnere60165f2006-06-22 06:36:29 +0000684 // allows us to avoid looking up the identifier info for #define/#undef and
685 // other common directives.
686 const char *RawCharData = SourceMgr.getCharacterData(Tok.getLocation());
687 char FirstChar = RawCharData[0];
Chris Lattner22eb9722006-06-18 05:43:12 +0000688 if (FirstChar >= 'a' && FirstChar <= 'z' &&
689 FirstChar != 'i' && FirstChar != 'e') {
690 CurLexer->ParsingPreprocessorDirective = false;
691 continue;
692 }
693
Chris Lattnere60165f2006-06-22 06:36:29 +0000694 // Get the identifier name without trigraphs or embedded newlines. Note
695 // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
696 // when skipping.
697 // TODO: could do this with zero copies in the no-clean case by using
698 // strncmp below.
699 char Directive[20];
700 unsigned IdLen;
701 if (!Tok.needsCleaning() && Tok.getLength() < 20) {
702 IdLen = Tok.getLength();
703 memcpy(Directive, RawCharData, IdLen);
704 Directive[IdLen] = 0;
705 } else {
706 std::string DirectiveStr = getSpelling(Tok);
707 IdLen = DirectiveStr.size();
708 if (IdLen >= 20) {
709 CurLexer->ParsingPreprocessorDirective = false;
710 continue;
711 }
712 memcpy(Directive, &DirectiveStr[0], IdLen);
713 Directive[IdLen] = 0;
714 }
715
Chris Lattner22eb9722006-06-18 05:43:12 +0000716 if (FirstChar == 'i' && Directive[1] == 'f') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000717 if ((IdLen == 2) || // "if"
718 (IdLen == 5 && !strcmp(Directive+2, "def")) || // "ifdef"
719 (IdLen == 6 && !strcmp(Directive+2, "ndef"))) { // "ifndef"
Chris Lattner22eb9722006-06-18 05:43:12 +0000720 // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
721 // bother parsing the condition.
Chris Lattnercb283342006-06-18 06:48:37 +0000722 DiscardUntilEndOfDirective();
Chris Lattner50b497e2006-06-18 16:32:35 +0000723 CurLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
Chris Lattnerd01e2912006-06-18 16:22:51 +0000724 /*foundnonskip*/false,
725 /*fnddelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +0000726 }
727 } else if (FirstChar == 'e') {
Chris Lattnere60165f2006-06-22 06:36:29 +0000728 if (IdLen == 5 && !strcmp(Directive+1, "ndif")) { // "endif"
Chris Lattnercb283342006-06-18 06:48:37 +0000729 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +0000730 PPConditionalInfo CondInfo;
731 CondInfo.WasSkipping = true; // Silence bogus warning.
732 bool InCond = CurLexer->popConditionalLevel(CondInfo);
733 assert(!InCond && "Can't be skipping if not in a conditional!");
734
735 // If we popped the outermost skipping block, we're done skipping!
736 if (!CondInfo.WasSkipping)
737 break;
Chris Lattnere60165f2006-06-22 06:36:29 +0000738 } else if (IdLen == 4 && !strcmp(Directive+1, "lse")) { // "else".
Chris Lattner22eb9722006-06-18 05:43:12 +0000739 // #else directive in a skipping conditional. If not in some other
740 // skipping conditional, and if #else hasn't already been seen, enter it
741 // as a non-skipping conditional.
Chris Lattnercb283342006-06-18 06:48:37 +0000742 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +0000743 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
744
745 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000746 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000747
748 // Note that we've seen a #else in this conditional.
749 CondInfo.FoundElse = true;
750
751 // If the conditional is at the top level, and the #if block wasn't
752 // entered, enter the #else block now.
753 if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
754 CondInfo.FoundNonSkip = true;
755 break;
756 }
Chris Lattnere60165f2006-06-22 06:36:29 +0000757 } else if (IdLen == 4 && !strcmp(Directive+1, "lif")) { // "elif".
Chris Lattner22eb9722006-06-18 05:43:12 +0000758 PPConditionalInfo &CondInfo = CurLexer->peekConditionalLevel();
759
760 bool ShouldEnter;
761 // If this is in a skipping block or if we're already handled this #if
762 // block, don't bother parsing the condition.
763 if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
Chris Lattnercb283342006-06-18 06:48:37 +0000764 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +0000765 ShouldEnter = false;
766 } else {
767 // Evaluate the #elif condition!
768 const char *Start = CurLexer->BufferPtr;
769
770 // Restore the value of SkippingContents so that identifiers are
771 // looked up, etc, inside the #elif expression.
772 assert(SkippingContents && "We have to be skipping here!");
773 SkippingContents = false;
Chris Lattner7966aaf2006-06-18 06:50:36 +0000774 ShouldEnter = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +0000775 SkippingContents = true;
776 }
777
778 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +0000779 if (CondInfo.FoundElse) Diag(Tok, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +0000780
781 // If this condition is true, enter it!
782 if (ShouldEnter) {
783 CondInfo.FoundNonSkip = true;
784 break;
785 }
786 }
787 }
788
789 CurLexer->ParsingPreprocessorDirective = false;
790 }
791
792 // Finally, if we are out of the conditional (saw an #endif or ran off the end
793 // of the file, just stop skipping and return to lexing whatever came after
794 // the #if block.
795 SkippingContents = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000796}
797
798//===----------------------------------------------------------------------===//
799// Preprocessor Directive Handling.
800//===----------------------------------------------------------------------===//
801
802/// HandleDirective - This callback is invoked when the lexer sees a # token
803/// at the start of a line. This consumes the directive, modifies the
804/// lexer/preprocessor state, and advances the lexer(s) so that the next token
805/// read is the correct one.
Chris Lattnercb283342006-06-18 06:48:37 +0000806void Preprocessor::HandleDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000807 // FIXME: TRADITIONAL: # with whitespace before it not recognized by K&R?
808
809 // We just parsed a # character at the start of a line, so we're in directive
810 // mode. Tell the lexer this so any newlines we see will be converted into an
811 // EOM token (this terminates the macro).
812 CurLexer->ParsingPreprocessorDirective = true;
813
814 ++NumDirectives;
815
816 // Read the next token, the directive flavor.
Chris Lattnercb283342006-06-18 06:48:37 +0000817 LexUnexpandedToken(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000818
819 switch (Result.getKind()) {
820 default: break;
821 case tok::eom:
Chris Lattnercb283342006-06-18 06:48:37 +0000822 return; // null directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000823
824#if 0
825 case tok::numeric_constant:
826 // FIXME: implement # 7 line numbers!
827 break;
828#endif
829 case tok::kw_else:
830 return HandleElseDirective(Result);
831 case tok::kw_if:
832 return HandleIfDirective(Result);
833 case tok::identifier:
Chris Lattner40931922006-06-22 06:14:04 +0000834 // Get the identifier name without trigraphs or embedded newlines.
835 const char *Directive = Result.getIdentifierInfo()->getName();
Chris Lattner22eb9722006-06-18 05:43:12 +0000836 bool isExtension = false;
Chris Lattner40931922006-06-22 06:14:04 +0000837 switch (Result.getIdentifierInfo()->getNameLength()) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000838 case 4:
Chris Lattner40931922006-06-22 06:14:04 +0000839 if (Directive[0] == 'l' && !strcmp(Directive, "line"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000840 ;
Chris Lattner40931922006-06-22 06:14:04 +0000841 if (Directive[0] == 'e' && !strcmp(Directive, "elif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000842 return HandleElifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000843 if (Directive[0] == 's' && !strcmp(Directive, "sccs")) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000844 isExtension = true;
845 // SCCS is the same as #ident.
846 }
847 break;
848 case 5:
Chris Lattner40931922006-06-22 06:14:04 +0000849 if (Directive[0] == 'e' && !strcmp(Directive, "endif"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000850 return HandleEndifDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000851 if (Directive[0] == 'i' && !strcmp(Directive, "ifdef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000852 return HandleIfdefDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000853 if (Directive[0] == 'u' && !strcmp(Directive, "undef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 return HandleUndefDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000855 if (Directive[0] == 'e' && !strcmp(Directive, "error"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000856 return HandleUserDiagnosticDirective(Result, false);
Chris Lattner40931922006-06-22 06:14:04 +0000857 if (Directive[0] == 'i' && !strcmp(Directive, "ident"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000858 isExtension = true;
859 break;
860 case 6:
Chris Lattner40931922006-06-22 06:14:04 +0000861 if (Directive[0] == 'd' && !strcmp(Directive, "define"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000862 return HandleDefineDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000863 if (Directive[0] == 'i' && !strcmp(Directive, "ifndef"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000864 return HandleIfdefDirective(Result, true);
Chris Lattner40931922006-06-22 06:14:04 +0000865 if (Directive[0] == 'i' && !strcmp(Directive, "import"))
Chris Lattner22eb9722006-06-18 05:43:12 +0000866 return HandleImportDirective(Result);
Chris Lattner40931922006-06-22 06:14:04 +0000867 if (Directive[0] == 'p' && !strcmp(Directive, "pragma")) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000868 // FIXME: implement #pragma
869 ++NumPragma;
870#if 1
871 // Read the rest of the PP line.
872 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000873 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000874 } while (Result.getKind() != tok::eom);
875
Chris Lattnercb283342006-06-18 06:48:37 +0000876 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000877#endif
Chris Lattner40931922006-06-22 06:14:04 +0000878 } else if (Directive[0] == 'a' && !strcmp(Directive, "assert")) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 isExtension = true;
880 }
881 break;
882 case 7:
Chris Lattner40931922006-06-22 06:14:04 +0000883 if (Directive[0] == 'i' && !strcmp(Directive, "include"))
884 return HandleIncludeDirective(Result); // Handle #include.
885 if (Directive[0] == 'w' && !strcmp(Directive, "warning")) {
Chris Lattnercb283342006-06-18 06:48:37 +0000886 Diag(Result, diag::ext_pp_warning_directive);
Chris Lattner504f2eb2006-06-18 07:19:54 +0000887 return HandleUserDiagnosticDirective(Result, true);
Chris Lattnercb283342006-06-18 06:48:37 +0000888 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000889 break;
890 case 8:
Chris Lattner40931922006-06-22 06:14:04 +0000891 if (Directive[0] == 'u' && !strcmp(Directive, "unassert")) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000892 isExtension = true;
893 }
894 break;
895 case 12:
Chris Lattner40931922006-06-22 06:14:04 +0000896 if (Directive[0] == 'i' && !strcmp(Directive, "include_next"))
897 return HandleIncludeNextDirective(Result); // Handle #include_next.
Chris Lattner22eb9722006-06-18 05:43:12 +0000898 break;
899 }
900 break;
901 }
902
903 // If we reached here, the preprocessing token is not valid!
Chris Lattnercb283342006-06-18 06:48:37 +0000904 Diag(Result, diag::err_pp_invalid_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +0000905
906 // Read the rest of the PP line.
907 do {
Chris Lattnercb283342006-06-18 06:48:37 +0000908 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000909 } while (Result.getKind() != tok::eom);
910
911 // Okay, we're done parsing the directive.
Chris Lattner22eb9722006-06-18 05:43:12 +0000912}
913
Chris Lattnercb283342006-06-18 06:48:37 +0000914void Preprocessor::HandleUserDiagnosticDirective(LexerToken &Result,
Chris Lattner22eb9722006-06-18 05:43:12 +0000915 bool isWarning) {
916 // Read the rest of the line raw. We do this because we don't want macros
917 // to be expanded and we don't require that the tokens be valid preprocessing
918 // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
919 // collapse multiple consequtive white space between tokens, but this isn't
920 // specified by the standard.
921 std::string Message = CurLexer->ReadToEndOfLine();
922
923 unsigned DiagID = isWarning ? diag::pp_hash_warning : diag::err_pp_hash_error;
924 return Diag(Result, DiagID, Message);
925}
926
927/// HandleIncludeDirective - The "#include" tokens have just been read, read the
928/// file to be included from the lexer, then include it! This is a common
929/// routine with functionality shared between #include, #include_next and
930/// #import.
Chris Lattnercb283342006-06-18 06:48:37 +0000931void Preprocessor::HandleIncludeDirective(LexerToken &IncludeTok,
Chris Lattner22eb9722006-06-18 05:43:12 +0000932 const DirectoryLookup *LookupFrom,
933 bool isImport) {
934 ++NumIncluded;
935 LexerToken FilenameTok;
Chris Lattnercb283342006-06-18 06:48:37 +0000936 CurLexer->LexIncludeFilename(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000937
938 // If the token kind is EOM, the error has already been diagnosed.
939 if (FilenameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +0000940 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000941
942 // Check that we don't have infinite #include recursion.
943 if (IncludeStack.size() == MaxAllowedIncludeStackDepth-1)
944 return Diag(FilenameTok, diag::err_pp_include_too_deep);
945
946 // Get the text form of the filename.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000947 std::string Filename = getSpelling(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000948 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
949
950 // Make sure the filename is <x> or "x".
951 bool isAngled;
952 if (Filename[0] == '<') {
953 isAngled = true;
954 if (Filename[Filename.size()-1] != '>')
955 return Diag(FilenameTok, diag::err_pp_expects_filename);
956 } else if (Filename[0] == '"') {
957 isAngled = false;
958 if (Filename[Filename.size()-1] != '"')
959 return Diag(FilenameTok, diag::err_pp_expects_filename);
960 } else {
961 return Diag(FilenameTok, diag::err_pp_expects_filename);
962 }
963
964 // Remove the quotes.
965 Filename = std::string(Filename.begin()+1, Filename.end()-1);
966
967 // Diagnose #include "" as invalid.
968 if (Filename.empty())
969 return Diag(FilenameTok, diag::err_pp_empty_filename);
970
971 // Search include directories.
Chris Lattnerc8997182006-06-22 05:52:16 +0000972 const DirectoryLookup *CurDir;
973 const FileEntry *File = LookupFile(Filename, isAngled, LookupFrom, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +0000974 if (File == 0)
975 return Diag(FilenameTok, diag::err_pp_file_not_found);
976
977 // Get information about this file.
978 PerFileInfo &FileInfo = getFileInfo(File);
979
980 // If this is a #import directive, check that we have not already imported
981 // this header.
982 if (isImport) {
983 // If this has already been imported, don't import it again.
984 FileInfo.isImport = true;
985
986 // Has this already been #import'ed or #include'd?
Chris Lattnercb283342006-06-18 06:48:37 +0000987 if (FileInfo.NumIncludes) return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000988 } else {
989 // Otherwise, if this is a #include of a file that was previously #import'd
990 // or if this is the second #include of a #pragma once file, ignore it.
991 if (FileInfo.isImport)
Chris Lattnercb283342006-06-18 06:48:37 +0000992 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000993 }
994
995 // Look up the file, create a File ID for it.
996 unsigned FileID =
Chris Lattner50b497e2006-06-18 16:32:35 +0000997 SourceMgr.createFileID(File, FilenameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +0000998 if (FileID == 0)
999 return Diag(FilenameTok, diag::err_pp_file_not_found);
1000
1001 // Finally, if all is good, enter the new file!
Chris Lattnerc8997182006-06-22 05:52:16 +00001002 EnterSourceFile(FileID, CurDir);
Chris Lattner22eb9722006-06-18 05:43:12 +00001003
1004 // Increment the number of times this file has been included.
1005 ++FileInfo.NumIncludes;
Chris Lattner22eb9722006-06-18 05:43:12 +00001006}
1007
1008/// HandleIncludeNextDirective - Implements #include_next.
1009///
Chris Lattnercb283342006-06-18 06:48:37 +00001010void Preprocessor::HandleIncludeNextDirective(LexerToken &IncludeNextTok) {
1011 Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001012
1013 // #include_next is like #include, except that we start searching after
1014 // the current found directory. If we can't do this, issue a
1015 // diagnostic.
Chris Lattnerc8997182006-06-22 05:52:16 +00001016 const DirectoryLookup *Lookup = CurDirLookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 if (IncludeStack.empty()) {
1018 Lookup = 0;
Chris Lattnercb283342006-06-18 06:48:37 +00001019 Diag(IncludeNextTok, diag::pp_include_next_in_primary);
Chris Lattner22eb9722006-06-18 05:43:12 +00001020 } else if (Lookup == 0) {
Chris Lattnercb283342006-06-18 06:48:37 +00001021 Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
Chris Lattnerc8997182006-06-22 05:52:16 +00001022 } else {
1023 // Start looking up in the next directory.
1024 ++Lookup;
Chris Lattner22eb9722006-06-18 05:43:12 +00001025 }
1026
1027 return HandleIncludeDirective(IncludeNextTok, Lookup);
1028}
1029
1030/// HandleImportDirective - Implements #import.
1031///
Chris Lattnercb283342006-06-18 06:48:37 +00001032void Preprocessor::HandleImportDirective(LexerToken &ImportTok) {
1033 Diag(ImportTok, diag::ext_pp_import_directive);
Chris Lattner22eb9722006-06-18 05:43:12 +00001034
1035 return HandleIncludeDirective(ImportTok, 0, true);
1036}
1037
1038/// HandleDefineDirective - Implements #define. This consumes the entire macro
1039/// line then lets the caller lex the next real token.
1040///
Chris Lattnercb283342006-06-18 06:48:37 +00001041void Preprocessor::HandleDefineDirective(LexerToken &DefineTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001042 ++NumDefined;
1043 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001044 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001045
1046 // Error reading macro name? If so, diagnostic already issued.
1047 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001048 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001049
Chris Lattner50b497e2006-06-18 16:32:35 +00001050 MacroInfo *MI = new MacroInfo(MacroNameTok.getLocation());
Chris Lattner22eb9722006-06-18 05:43:12 +00001051
1052 LexerToken Tok;
Chris Lattnercb283342006-06-18 06:48:37 +00001053 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001054
1055 if (Tok.getKind() == tok::eom) {
1056 // If there is no body to this macro, we have no special handling here.
1057 } else if (Tok.getKind() == tok::l_paren && !Tok.hasLeadingSpace()) {
1058 // This is a function-like macro definition.
1059 //assert(0 && "Function-like macros not implemented!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001060 return DiscardUntilEndOfDirective();
1061
1062 } else if (!Tok.hasLeadingSpace()) {
1063 // C99 requires whitespace between the macro definition and the body. Emit
1064 // a diagnostic for something like "#define X+".
1065 if (Features.C99) {
Chris Lattnercb283342006-06-18 06:48:37 +00001066 Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
Chris Lattner22eb9722006-06-18 05:43:12 +00001067 } else {
1068 // FIXME: C90/C++ do not get this diagnostic, but it does get a similar
1069 // one in some cases!
1070 }
1071 } else {
1072 // This is a normal token with leading space. Clear the leading space
1073 // marker on the first token to get proper expansion.
1074 Tok.ClearFlag(LexerToken::LeadingSpace);
1075 }
1076
1077 // Read the rest of the macro body.
1078 while (Tok.getKind() != tok::eom) {
1079 MI->AddTokenToBody(Tok);
1080
1081 // FIXME: See create_iso_definition.
1082
1083 // Get the next token of the macro.
Chris Lattnercb283342006-06-18 06:48:37 +00001084 LexUnexpandedToken(Tok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001085 }
1086
1087 // Finally, if this identifier already had a macro defined for it, verify that
1088 // the macro bodies are identical and free the old definition.
1089 if (MacroInfo *OtherMI = MacroNameTok.getIdentifierInfo()->getMacroInfo()) {
1090 // FIXME: Verify the definition is the same.
1091 // Macros must be identical. This means all tokes and whitespace separation
1092 // must be the same.
1093 delete OtherMI;
1094 }
1095
1096 MacroNameTok.getIdentifierInfo()->setMacroInfo(MI);
Chris Lattner22eb9722006-06-18 05:43:12 +00001097}
1098
1099
1100/// HandleUndefDirective - Implements #undef.
1101///
Chris Lattnercb283342006-06-18 06:48:37 +00001102void Preprocessor::HandleUndefDirective(LexerToken &UndefTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001103 ++NumUndefined;
1104 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001105 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001106
1107 // Error reading macro name? If so, diagnostic already issued.
1108 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001109 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001110
1111 // Check to see if this is the last token on the #undef line.
Chris Lattnercb283342006-06-18 06:48:37 +00001112 CheckEndOfDirective("#undef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001113
1114 // Okay, we finally have a valid identifier to undef.
1115 MacroInfo *MI = MacroNameTok.getIdentifierInfo()->getMacroInfo();
1116
1117 // If the macro is not defined, this is a noop undef, just return.
Chris Lattnercb283342006-06-18 06:48:37 +00001118 if (MI == 0) return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001119
1120#if 0 // FIXME: implement warn_unused_macros.
1121 if (CPP_OPTION (pfile, warn_unused_macros))
1122 _cpp_warn_if_unused_macro (pfile, node, NULL);
1123#endif
1124
1125 // Free macro definition.
1126 delete MI;
1127 MacroNameTok.getIdentifierInfo()->setMacroInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001128}
1129
1130
1131/// HandleIfdefDirective - Implements the #ifdef/#ifndef directive. isIfndef is
1132/// true when this is a #ifndef directive.
1133///
Chris Lattnercb283342006-06-18 06:48:37 +00001134void Preprocessor::HandleIfdefDirective(LexerToken &Result, bool isIfndef) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 ++NumIf;
1136 LexerToken DirectiveTok = Result;
1137
1138 LexerToken MacroNameTok;
Chris Lattnercb283342006-06-18 06:48:37 +00001139 ReadMacroName(MacroNameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001140
1141 // Error reading macro name? If so, diagnostic already issued.
1142 if (MacroNameTok.getKind() == tok::eom)
Chris Lattnercb283342006-06-18 06:48:37 +00001143 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001144
1145 // Check to see if this is the last token on the #if[n]def line.
Chris Lattnercb283342006-06-18 06:48:37 +00001146 CheckEndOfDirective("#ifdef");
Chris Lattner22eb9722006-06-18 05:43:12 +00001147
1148 // Should we include the stuff contained by this directive?
1149 if (!MacroNameTok.getIdentifierInfo()->getMacroInfo() == isIfndef) {
1150 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001151 CurLexer->pushConditionalLevel(DirectiveTok.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001152 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001153 } else {
1154 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001155 SkipExcludedConditionalBlock(DirectiveTok.getLocation(),
Chris Lattnercb283342006-06-18 06:48:37 +00001156 /*Foundnonskip*/false,
1157 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001158 }
1159}
1160
1161/// HandleIfDirective - Implements the #if directive.
1162///
Chris Lattnercb283342006-06-18 06:48:37 +00001163void Preprocessor::HandleIfDirective(LexerToken &IfToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001164 ++NumIf;
1165 const char *Start = CurLexer->BufferPtr;
1166
Chris Lattner7966aaf2006-06-18 06:50:36 +00001167 bool ConditionalTrue = EvaluateDirectiveExpression();
Chris Lattner22eb9722006-06-18 05:43:12 +00001168
1169 // Should we include the stuff contained by this directive?
1170 if (ConditionalTrue) {
1171 // Yes, remember that we are inside a conditional, then lex the next token.
Chris Lattner50b497e2006-06-18 16:32:35 +00001172 CurLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
Chris Lattner22eb9722006-06-18 05:43:12 +00001173 /*foundnonskip*/true, /*foundelse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001174 } else {
1175 // No, skip the contents of this block and return the first token after it.
Chris Lattner50b497e2006-06-18 16:32:35 +00001176 SkipExcludedConditionalBlock(IfToken.getLocation(), /*Foundnonskip*/false,
Chris Lattnercb283342006-06-18 06:48:37 +00001177 /*FoundElse*/false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001178 }
1179}
1180
1181/// HandleEndifDirective - Implements the #endif directive.
1182///
Chris Lattnercb283342006-06-18 06:48:37 +00001183void Preprocessor::HandleEndifDirective(LexerToken &EndifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001184 ++NumEndif;
1185 // Check that this is the whole directive.
Chris Lattnercb283342006-06-18 06:48:37 +00001186 CheckEndOfDirective("#endif");
Chris Lattner22eb9722006-06-18 05:43:12 +00001187
1188 PPConditionalInfo CondInfo;
1189 if (CurLexer->popConditionalLevel(CondInfo)) {
1190 // No conditionals on the stack: this is an #endif without an #if.
1191 return Diag(EndifToken, diag::err_pp_endif_without_if);
1192 }
1193
1194 assert(!CondInfo.WasSkipping && !isSkipping() &&
1195 "This code should only be reachable in the non-skipping case!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001196}
1197
1198
Chris Lattnercb283342006-06-18 06:48:37 +00001199void Preprocessor::HandleElseDirective(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001200 ++NumElse;
1201 // #else directive in a non-skipping conditional... start skipping.
Chris Lattnercb283342006-06-18 06:48:37 +00001202 CheckEndOfDirective("#else");
Chris Lattner22eb9722006-06-18 05:43:12 +00001203
1204 PPConditionalInfo CI;
1205 if (CurLexer->popConditionalLevel(CI))
1206 return Diag(Result, diag::pp_err_else_without_if);
1207
1208 // If this is a #else with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001209 if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001210
1211 // Finally, skip the rest of the contents of this block and return the first
1212 // token after it.
1213 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1214 /*FoundElse*/true);
1215}
1216
Chris Lattnercb283342006-06-18 06:48:37 +00001217void Preprocessor::HandleElifDirective(LexerToken &ElifToken) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 ++NumElse;
1219 // #elif directive in a non-skipping conditional... start skipping.
1220 // We don't care what the condition is, because we will always skip it (since
1221 // the block immediately before it was included).
Chris Lattnercb283342006-06-18 06:48:37 +00001222 DiscardUntilEndOfDirective();
Chris Lattner22eb9722006-06-18 05:43:12 +00001223
1224 PPConditionalInfo CI;
1225 if (CurLexer->popConditionalLevel(CI))
1226 return Diag(ElifToken, diag::pp_err_elif_without_if);
1227
1228 // If this is a #elif with a #else before it, report the error.
Chris Lattnercb283342006-06-18 06:48:37 +00001229 if (CI.FoundElse) Diag(ElifToken, diag::pp_err_elif_after_else);
Chris Lattner22eb9722006-06-18 05:43:12 +00001230
1231 // Finally, skip the rest of the contents of this block and return the first
1232 // token after it.
1233 return SkipExcludedConditionalBlock(CI.IfLoc, /*Foundnonskip*/true,
1234 /*FoundElse*/CI.FoundElse);
1235}