blob: 097b4542b6e46f47a8a7750f31cee806ac5301d5 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Preprocessor interface.
11//
12//===----------------------------------------------------------------------===//
13//
14// Options to support:
15// -H - Print the name of each header file used.
Chris Lattner133db8a2009-02-06 06:45:26 +000016// -d[DNI] - Dump various things.
Chris Lattner4b009652007-07-25 00:24:17 +000017// -fworking-directory - #line's with preprocessor's working dir.
18// -fpreprocessed
19// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
20// -W*
21// -w
22//
23// Messages to emit:
24// "Multiple include guards may be useful for:\n"
25//
26//===----------------------------------------------------------------------===//
27
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Lex/HeaderSearch.h"
30#include "clang/Lex/MacroInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000031#include "clang/Lex/Pragma.h"
32#include "clang/Lex/ScratchBuffer.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000033#include "clang/Lex/LexDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000034#include "clang/Basic/SourceManager.h"
Ted Kremenek40003a72009-02-12 03:26:59 +000035#include "clang/Basic/FileManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000036#include "clang/Basic/TargetInfo.h"
Chris Lattnercbed2992008-10-05 20:40:30 +000037#include "llvm/ADT/APFloat.h"
Chris Lattner4b009652007-07-25 00:24:17 +000038#include "llvm/ADT/SmallVector.h"
39#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekce4c64e2008-01-14 16:44:48 +000040#include "llvm/Support/Streams.h"
Chris Lattner7d6220c2009-03-02 22:20:04 +000041#include <cstdio>
Chris Lattner4b009652007-07-25 00:24:17 +000042using namespace clang;
43
44//===----------------------------------------------------------------------===//
45
Ted Kremenek5ab36b02008-04-17 21:23:07 +000046PreprocessorFactory::~PreprocessorFactory() {}
47
Chris Lattner4b009652007-07-25 00:24:17 +000048Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
49 TargetInfo &target, SourceManager &SM,
Ted Kremenekd976c3d2009-01-15 18:47:46 +000050 HeaderSearch &Headers,
51 IdentifierInfoLookup* IILookup)
Chris Lattnerf1e7b792009-03-13 21:17:43 +000052 : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
Ted Kremenekd976c3d2009-01-15 18:47:46 +000053 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup),
Ted Kremenek94dcef72008-11-19 00:44:06 +000054 CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
Chris Lattner4b009652007-07-25 00:24:17 +000055 ScratchBuf = new ScratchBuffer(SourceMgr);
56
57 // Clear stats.
58 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
59 NumIf = NumElse = NumEndif = 0;
60 NumEnteredSourceFiles = 0;
61 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
62 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
63 MaxIncludeStackDepth = 0;
64 NumSkipped = 0;
65
66 // Default to discarding comments.
67 KeepComments = false;
68 KeepMacroComments = false;
69
70 // Macro expansion is enabled.
71 DisableMacroExpansion = false;
72 InMacroArgs = false;
Chris Lattner5b54ed92008-03-09 02:26:03 +000073 NumCachedTokenLexers = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000074
Argiris Kirtzidisf55b1102008-08-10 13:15:22 +000075 CachedLexPos = 0;
76
Chris Lattner4b009652007-07-25 00:24:17 +000077 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
78 // This gets unpoisoned where it is allowed.
79 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
80
81 // Initialize the pragma handlers.
82 PragmaHandlers = new PragmaNamespace(0);
83 RegisterBuiltinPragmas();
84
85 // Initialize builtin macros like __LINE__ and friends.
86 RegisterBuiltinMacros();
87}
88
89Preprocessor::~Preprocessor() {
Argiris Kirtzidis1370cf12008-08-23 12:12:06 +000090 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
91
Chris Lattner4b009652007-07-25 00:24:17 +000092 while (!IncludeMacroStack.empty()) {
93 delete IncludeMacroStack.back().TheLexer;
Chris Lattner5b54ed92008-03-09 02:26:03 +000094 delete IncludeMacroStack.back().TheTokenLexer;
Chris Lattner4b009652007-07-25 00:24:17 +000095 IncludeMacroStack.pop_back();
96 }
Chris Lattner7a1b0882007-10-07 08:44:20 +000097
98 // Free any macro definitions.
99 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
100 Macros.begin(), E = Macros.end(); I != E; ++I) {
Ted Kremenek5f9fb3f2008-12-15 19:56:42 +0000101 // We don't need to free the MacroInfo objects directly. These
102 // will be released when the BumpPtrAllocator 'BP' object gets
Ted Kremenek334035f2009-01-19 07:45:44 +0000103 // destroyed. We still need to run the dstor, however, to free
104 // memory alocated by MacroInfo.
Chris Lattner89188b22009-02-20 22:46:43 +0000105 I->second->Destroy(BP);
Chris Lattner7a1b0882007-10-07 08:44:20 +0000106 I->first->setHasMacroDefinition(false);
107 }
Chris Lattner4b009652007-07-25 00:24:17 +0000108
109 // Free any cached macro expanders.
Chris Lattner5b54ed92008-03-09 02:26:03 +0000110 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
111 delete TokenLexerCache[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000112
113 // Release pragma information.
114 delete PragmaHandlers;
115
116 // Delete the scratch buffer info.
117 delete ScratchBuf;
Chris Lattner65829812008-03-14 06:07:05 +0000118
119 delete Callbacks;
Chris Lattner4b009652007-07-25 00:24:17 +0000120}
121
Ted Kremenek40003a72009-02-12 03:26:59 +0000122void Preprocessor::setPTHManager(PTHManager* pm) {
123 PTH.reset(pm);
124 FileMgr.setStatCache(PTH->createStatCache());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000128 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
129 << getSpelling(Tok) << "'";
Chris Lattner4b009652007-07-25 00:24:17 +0000130
131 if (!DumpFlags) return;
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000132
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000133 llvm::cerr << "\t";
Chris Lattner4b009652007-07-25 00:24:17 +0000134 if (Tok.isAtStartOfLine())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000135 llvm::cerr << " [StartOfLine]";
Chris Lattner4b009652007-07-25 00:24:17 +0000136 if (Tok.hasLeadingSpace())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000137 llvm::cerr << " [LeadingSpace]";
Chris Lattner4b009652007-07-25 00:24:17 +0000138 if (Tok.isExpandDisabled())
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000139 llvm::cerr << " [ExpandDisabled]";
Chris Lattner4b009652007-07-25 00:24:17 +0000140 if (Tok.needsCleaning()) {
141 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000142 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
143 << "']";
Chris Lattner4b009652007-07-25 00:24:17 +0000144 }
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000145
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000146 llvm::cerr << "\tLoc=<";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000147 DumpLocation(Tok.getLocation());
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000148 llvm::cerr << ">";
Chris Lattnerc0f7c512007-12-09 20:31:55 +0000149}
150
151void Preprocessor::DumpLocation(SourceLocation Loc) const {
Chris Lattner836774b2009-01-27 07:57:44 +0000152 Loc.dump(SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +0000153}
154
155void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000156 llvm::cerr << "MACRO: ";
Chris Lattner4b009652007-07-25 00:24:17 +0000157 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
158 DumpToken(MI.getReplacementToken(i));
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000159 llvm::cerr << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000160 }
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000161 llvm::cerr << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000162}
163
164void Preprocessor::PrintStats() {
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000165 llvm::cerr << "\n*** Preprocessor Stats:\n";
166 llvm::cerr << NumDirectives << " directives found:\n";
167 llvm::cerr << " " << NumDefined << " #define.\n";
168 llvm::cerr << " " << NumUndefined << " #undef.\n";
169 llvm::cerr << " #include/#include_next/#import:\n";
170 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
171 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
172 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
173 llvm::cerr << " " << NumElse << " #else/#elif.\n";
174 llvm::cerr << " " << NumEndif << " #endif.\n";
175 llvm::cerr << " " << NumPragma << " #pragma.\n";
176 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000177
Ted Kremenekce4c64e2008-01-14 16:44:48 +0000178 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
179 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
180 << NumFastMacroExpanded << " on the fast path.\n";
181 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
182 << " token paste (##) operations performed, "
183 << NumFastTokenPaste << " on the fast path.\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000184}
185
186//===----------------------------------------------------------------------===//
187// Token Spelling
188//===----------------------------------------------------------------------===//
189
190
191/// getSpelling() - Return the 'spelling' of this token. The spelling of a
192/// token are the characters used to represent the token in the source file
193/// after trigraph expansion and escaped-newline folding. In particular, this
194/// wants to get the true, uncanonicalized, spelling of things like digraphs
195/// UCNs, etc.
196std::string Preprocessor::getSpelling(const Token &Tok) const {
197 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
Ted Kremenek562db7f2009-01-27 00:01:05 +0000198
Chris Lattner4b009652007-07-25 00:24:17 +0000199 // If this token contains nothing interesting, return it directly.
Ted Kremenek562db7f2009-01-27 00:01:05 +0000200 const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000201 if (!Tok.needsCleaning())
202 return std::string(TokStart, TokStart+Tok.getLength());
203
204 std::string Result;
205 Result.reserve(Tok.getLength());
206
207 // Otherwise, hard case, relex the characters into the string.
208 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
209 Ptr != End; ) {
210 unsigned CharSize;
211 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
212 Ptr += CharSize;
213 }
214 assert(Result.size() != unsigned(Tok.getLength()) &&
215 "NeedsCleaning flag set on something that didn't need cleaning!");
216 return Result;
217}
218
219/// getSpelling - This method is used to get the spelling of a token into a
220/// preallocated buffer, instead of as an std::string. The caller is required
221/// to allocate enough space for the token, which is guaranteed to be at least
222/// Tok.getLength() bytes long. The actual length of the token is returned.
223///
224/// Note that this method may do two possible things: it may either fill in
225/// the buffer specified with characters, or it may *change the input pointer*
226/// to point to a constant buffer with the data already in it (avoiding a
227/// copy). The caller is not allowed to modify the returned buffer pointer
228/// if an internal buffer is returned.
229unsigned Preprocessor::getSpelling(const Token &Tok,
230 const char *&Buffer) const {
231 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
232
233 // If this token is an identifier, just return the string from the identifier
234 // table, which is very quick.
235 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
236 Buffer = II->getName();
Chris Lattner9c47fe62009-01-05 19:44:41 +0000237 return II->getLength();
Chris Lattner4b009652007-07-25 00:24:17 +0000238 }
Ted Kremenekd2c849d2009-01-08 02:47:16 +0000239
Chris Lattner4b009652007-07-25 00:24:17 +0000240 // Otherwise, compute the start of the token in the input lexer buffer.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000241 const char *TokStart = 0;
242
243 if (Tok.isLiteral())
244 TokStart = Tok.getLiteralData();
245
246 if (TokStart == 0)
247 TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000248
249 // If this token contains nothing interesting, return it directly.
250 if (!Tok.needsCleaning()) {
251 Buffer = TokStart;
252 return Tok.getLength();
253 }
Chris Lattner6ad1f502009-01-26 19:29:26 +0000254
Chris Lattner4b009652007-07-25 00:24:17 +0000255 // Otherwise, hard case, relex the characters into the string.
256 char *OutBuf = const_cast<char*>(Buffer);
257 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
258 Ptr != End; ) {
259 unsigned CharSize;
260 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
261 Ptr += CharSize;
262 }
263 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
264 "NeedsCleaning flag set on something that didn't need cleaning!");
265
266 return OutBuf-Buffer;
267}
268
Chris Lattner4b009652007-07-25 00:24:17 +0000269/// CreateString - Plop the specified string into a scratch buffer and return a
270/// location for it. If specified, the source location provides a source
271/// location for the token.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000272void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok,
273 SourceLocation InstantiationLoc) {
274 Tok.setLength(Len);
275
276 const char *DestPtr;
277 SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr);
278
279 if (InstantiationLoc.isValid())
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000280 Loc = SourceMgr.createInstantiationLoc(Loc, InstantiationLoc,
281 InstantiationLoc, Len);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000282 Tok.setLocation(Loc);
283
284 // If this is a literal token, set the pointer data.
285 if (Tok.isLiteral())
286 Tok.setLiteralData(DestPtr);
Chris Lattner4b009652007-07-25 00:24:17 +0000287}
288
289
290/// AdvanceToTokenCharacter - Given a location that specifies the start of a
291/// token, return a new location that specifies a character within the token.
292SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
293 unsigned CharNo) {
Chris Lattner29e840e2009-02-18 18:56:29 +0000294 // If they request the first char of the token, we're trivially done.
Chris Lattner81df8462009-02-18 18:52:52 +0000295 if (CharNo == 0) return TokStart;
Chris Lattner4b009652007-07-25 00:24:17 +0000296
Chris Lattner18c8dc02009-01-16 07:36:28 +0000297 // Figure out how many physical characters away the specified instantiation
Chris Lattner4b009652007-07-25 00:24:17 +0000298 // character is. This needs to take into consideration newlines and
299 // trigraphs.
300 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
301 unsigned PhysOffset = 0;
302
303 // The usual case is that tokens don't contain anything interesting. Skip
304 // over the uninteresting characters. If a token only consists of simple
305 // chars, this method is extremely fast.
306 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
307 ++TokPtr, --CharNo, ++PhysOffset;
308
Chris Lattnerbed5ce72009-01-17 07:57:25 +0000309 // If we have a character that may be a trigraph or escaped newline, use a
Chris Lattner4b009652007-07-25 00:24:17 +0000310 // lexer to parse it correctly.
311 if (CharNo != 0) {
Chris Lattner4b009652007-07-25 00:24:17 +0000312 // Skip over characters the remaining characters.
Chris Lattnerbed5ce72009-01-17 07:57:25 +0000313 for (; CharNo; --CharNo) {
314 unsigned Size;
315 Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features);
316 TokPtr += Size;
317 PhysOffset += Size;
318 }
Chris Lattner4b009652007-07-25 00:24:17 +0000319 }
320
321 return TokStart.getFileLocWithOffset(PhysOffset);
322}
323
Douglas Gregor61be3602009-02-27 17:53:17 +0000324/// \brief Computes the source location just past the end of the
325/// token at this source location.
326///
327/// This routine can be used to produce a source location that
328/// points just past the end of the token referenced by \p Loc, and
329/// is generally used when a diagnostic needs to point just after a
330/// token where it expected something different that it received. If
331/// the returned source location would not be meaningful (e.g., if
332/// it points into a macro), this routine returns an invalid
333/// source location.
334SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc) {
335 if (Loc.isInvalid() || !Loc.isFileID())
336 return SourceLocation();
337
338 unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager());
339 return AdvanceToTokenCharacter(Loc, Len);
340}
341
342
Chris Lattner4b009652007-07-25 00:24:17 +0000343
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000344//===----------------------------------------------------------------------===//
345// Preprocessor Initialization Methods
346//===----------------------------------------------------------------------===//
347
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000348
349/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman886bf132008-01-07 04:01:26 +0000350/// which implicitly adds the builtin defines etc.
Ted Kremenek17861c52007-12-19 22:51:13 +0000351void Preprocessor::EnterMainSourceFile() {
Chris Lattner5a0d2272009-02-13 19:33:24 +0000352 // We do not allow the preprocessor to reenter the main file. Doing so will
353 // cause FileID's to accumulate information from both runs (e.g. #line
354 // information) and predefined macros aren't guaranteed to be set properly.
355 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000356 FileID MainFileID = SourceMgr.getMainFileID();
Ted Kremenek17861c52007-12-19 22:51:13 +0000357
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000358 // Enter the main file source buffer.
359 EnterSourceFile(MainFileID, 0);
360
Chris Lattnerb45f05c2007-11-15 19:07:47 +0000361 // Tell the header info that the main file was entered. If the file is later
362 // #imported, it won't be re-entered.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000363 if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID))
Chris Lattnerb45f05c2007-11-15 19:07:47 +0000364 HeaderInfo.IncrementIncludeCount(FE);
365
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000366 std::vector<char> PrologFile;
367 PrologFile.reserve(4080);
368
Chris Lattnera9a0dd52009-04-10 21:58:23 +0000369 // FIXME: Don't make a copy.
Chris Lattner47b6a162008-04-19 23:09:31 +0000370 PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000371
372 // Memory buffer must end with a null byte!
373 PrologFile.push_back(0);
374
375 // Now that we have emitted the predefined macros, #includes, etc into
376 // PrologFile, preprocess it to populate the initial preprocessor state.
377 llvm::MemoryBuffer *SB =
378 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
Chris Lattner0112bdf2009-03-20 20:16:10 +0000379 "<built-in>");
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000380 assert(SB && "Cannot fail to create predefined source buffer");
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000381 FileID FID = SourceMgr.createFileIDForMemBuffer(SB);
382 assert(!FID.isInvalid() && "Could not create FileID for predefines?");
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000383
384 // Start parsing the predefines.
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000385 EnterSourceFile(FID, 0);
Chris Lattnerd1f21e12007-10-09 22:10:18 +0000386}
Chris Lattner4b009652007-07-25 00:24:17 +0000387
Chris Lattner4b009652007-07-25 00:24:17 +0000388
389//===----------------------------------------------------------------------===//
390// Lexer Event Handling.
391//===----------------------------------------------------------------------===//
392
393/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
394/// identifier information for the token and install it into the token.
395IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
396 const char *BufPtr) {
Chris Lattnercb8e41c2007-10-09 18:02:16 +0000397 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Chris Lattner4b009652007-07-25 00:24:17 +0000398 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
399
400 // Look up this token, see if it is a macro, or if it is a language keyword.
401 IdentifierInfo *II;
402 if (BufPtr && !Identifier.needsCleaning()) {
403 // No cleaning needed, just use the characters from the lexed buffer.
404 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
405 } else {
406 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
407 llvm::SmallVector<char, 64> IdentifierBuffer;
408 IdentifierBuffer.resize(Identifier.getLength());
409 const char *TmpBuf = &IdentifierBuffer[0];
410 unsigned Size = getSpelling(Identifier, TmpBuf);
411 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
412 }
413 Identifier.setIdentifierInfo(II);
414 return II;
415}
416
417
418/// HandleIdentifier - This callback is invoked when the lexer reads an
419/// identifier. This callback looks up the identifier in the map and/or
420/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattner5b747d02009-01-21 07:43:11 +0000421///
422/// Note that callers of this method are guarded by checking the
423/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
424/// IdentifierInfo methods that compute these properties will need to change to
425/// match.
Chris Lattner4b009652007-07-25 00:24:17 +0000426void Preprocessor::HandleIdentifier(Token &Identifier) {
427 assert(Identifier.getIdentifierInfo() &&
428 "Can't handle identifiers without identifier info!");
429
430 IdentifierInfo &II = *Identifier.getIdentifierInfo();
431
432 // If this identifier was poisoned, and if it was not produced from a macro
433 // expansion, emit an error.
Ted Kremenek3acf6702008-11-19 22:43:49 +0000434 if (II.isPoisoned() && CurPPLexer) {
Chris Lattner4b009652007-07-25 00:24:17 +0000435 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
436 Diag(Identifier, diag::err_pp_used_poisoned_id);
437 else
438 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
439 }
440
441 // If this is a macro to be expanded, do it.
Chris Lattner7a1b0882007-10-07 08:44:20 +0000442 if (MacroInfo *MI = getMacroInfo(&II)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000443 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
444 if (MI->isEnabled()) {
445 if (!HandleMacroExpandedIdentifier(Identifier, MI))
446 return;
447 } else {
448 // C99 6.10.3.4p2 says that a disabled macro may never again be
449 // expanded, even if it's in a context where it could be expanded in the
450 // future.
451 Identifier.setFlag(Token::DisableExpand);
452 }
453 }
Chris Lattner4b009652007-07-25 00:24:17 +0000454 }
455
456 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
457 // then we act as if it is the actual operator and not the textual
458 // representation of it.
459 if (II.isCPlusPlusOperatorKeyword())
460 Identifier.setIdentifierInfo(0);
461
Chris Lattner4b009652007-07-25 00:24:17 +0000462 // If this is an extension token, diagnose its use.
Steve Naroff892bc0e2008-09-02 18:50:17 +0000463 // We avoid diagnosing tokens that originate from macro definitions.
464 if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion)
Chris Lattner4b009652007-07-25 00:24:17 +0000465 Diag(Identifier, diag::ext_token_used);
466}