blob: cac78fe6e008e5ae726eec0dd300ab1d0377459f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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.
16// -d[MDNI] - Dump various things.
17// -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"
Reid Spencer5f016e22007-07-11 17:01:13 +000031#include "clang/Lex/Pragma.h"
32#include "clang/Lex/ScratchBuffer.h"
33#include "clang/Basic/Diagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000034#include "clang/Basic/SourceManager.h"
35#include "clang/Basic/TargetInfo.h"
Chris Lattner2db78dd2008-10-05 20:40:30 +000036#include "llvm/ADT/APFloat.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000037#include "llvm/ADT/SmallVector.h"
Chris Lattner97ba77c2007-07-16 06:48:38 +000038#include "llvm/Support/MemoryBuffer.h"
Ted Kremenekbdd30c22008-01-14 16:44:48 +000039#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000040using namespace clang;
41
42//===----------------------------------------------------------------------===//
43
Ted Kremenekec6c5742008-04-17 21:23:07 +000044PreprocessorFactory::~PreprocessorFactory() {}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046Preprocessor::Preprocessor(Diagnostic &diags, const LangOptions &opts,
47 TargetInfo &target, SourceManager &SM,
Ted Kremenek72b1b152009-01-15 18:47:46 +000048 HeaderSearch &Headers,
49 IdentifierInfoLookup* IILookup)
Reid Spencer5f016e22007-07-11 17:01:13 +000050 : Diags(diags), Features(opts), Target(target), FileMgr(Headers.getFileMgr()),
Ted Kremenek72b1b152009-01-15 18:47:46 +000051 SourceMgr(SM), HeaderInfo(Headers), Identifiers(opts, IILookup),
Ted Kremenek4b71e3e2008-11-19 00:44:06 +000052 CurPPLexer(0), CurDirLookup(0), Callbacks(0) {
Reid Spencer5f016e22007-07-11 17:01:13 +000053 ScratchBuf = new ScratchBuffer(SourceMgr);
Chris Lattner9594acf2007-07-15 00:25:26 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // Clear stats.
56 NumDirectives = NumDefined = NumUndefined = NumPragma = 0;
57 NumIf = NumElse = NumEndif = 0;
58 NumEnteredSourceFiles = 0;
59 NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0;
60 NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0;
61 MaxIncludeStackDepth = 0;
62 NumSkipped = 0;
63
64 // Default to discarding comments.
65 KeepComments = false;
66 KeepMacroComments = false;
67
68 // Macro expansion is enabled.
69 DisableMacroExpansion = false;
70 InMacroArgs = false;
Chris Lattner6cfe7592008-03-09 02:26:03 +000071 NumCachedTokenLexers = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000072
Argyrios Kyrtzidis03db1b32008-08-10 13:15:22 +000073 CachedLexPos = 0;
74
Reid Spencer5f016e22007-07-11 17:01:13 +000075 // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro.
76 // This gets unpoisoned where it is allowed.
77 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
78
79 // Initialize the pragma handlers.
80 PragmaHandlers = new PragmaNamespace(0);
81 RegisterBuiltinPragmas();
82
83 // Initialize builtin macros like __LINE__ and friends.
84 RegisterBuiltinMacros();
85}
86
87Preprocessor::~Preprocessor() {
Argyrios Kyrtzidis2174a4f2008-08-23 12:12:06 +000088 assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
89
Reid Spencer5f016e22007-07-11 17:01:13 +000090 while (!IncludeMacroStack.empty()) {
91 delete IncludeMacroStack.back().TheLexer;
Chris Lattner6cfe7592008-03-09 02:26:03 +000092 delete IncludeMacroStack.back().TheTokenLexer;
Reid Spencer5f016e22007-07-11 17:01:13 +000093 IncludeMacroStack.pop_back();
94 }
Chris Lattnercc1a8752007-10-07 08:44:20 +000095
96 // Free any macro definitions.
97 for (llvm::DenseMap<IdentifierInfo*, MacroInfo*>::iterator I =
98 Macros.begin(), E = Macros.end(); I != E; ++I) {
Ted Kremenek0ea76722008-12-15 19:56:42 +000099 // We don't need to free the MacroInfo objects directly. These
100 // will be released when the BumpPtrAllocator 'BP' object gets
101 // destroyed.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000102 I->first->setHasMacroDefinition(false);
103 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104
Chris Lattner9594acf2007-07-15 00:25:26 +0000105 // Free any cached macro expanders.
Chris Lattner6cfe7592008-03-09 02:26:03 +0000106 for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i)
107 delete TokenLexerCache[i];
Chris Lattner9594acf2007-07-15 00:25:26 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 // Release pragma information.
110 delete PragmaHandlers;
111
112 // Delete the scratch buffer info.
113 delete ScratchBuf;
Chris Lattnereb50ed82008-03-14 06:07:05 +0000114
115 delete Callbacks;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116}
117
Chris Lattnerd2177732007-07-20 16:59:19 +0000118void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000119 llvm::cerr << tok::getTokenName(Tok.getKind()) << " '"
120 << getSpelling(Tok) << "'";
Reid Spencer5f016e22007-07-11 17:01:13 +0000121
122 if (!DumpFlags) return;
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000123
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000124 llvm::cerr << "\t";
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 if (Tok.isAtStartOfLine())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000126 llvm::cerr << " [StartOfLine]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 if (Tok.hasLeadingSpace())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000128 llvm::cerr << " [LeadingSpace]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 if (Tok.isExpandDisabled())
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000130 llvm::cerr << " [ExpandDisabled]";
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 if (Tok.needsCleaning()) {
132 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000133 llvm::cerr << " [UnClean='" << std::string(Start, Start+Tok.getLength())
134 << "']";
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 }
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000136
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000137 llvm::cerr << "\tLoc=<";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000138 DumpLocation(Tok.getLocation());
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000139 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000140}
141
142void Preprocessor::DumpLocation(SourceLocation Loc) const {
143 SourceLocation LogLoc = SourceMgr.getLogicalLoc(Loc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000144 llvm::cerr << SourceMgr.getSourceName(LogLoc) << ':'
145 << SourceMgr.getLineNumber(LogLoc) << ':'
Ted Kremenek109949a2008-07-19 19:10:04 +0000146 << SourceMgr.getColumnNumber(LogLoc);
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000147
148 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Loc);
149 if (PhysLoc != LogLoc) {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000150 llvm::cerr << " <PhysLoc=";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000151 DumpLocation(PhysLoc);
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000152 llvm::cerr << ">";
Chris Lattnerc3d8d572007-12-09 20:31:55 +0000153 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000154}
155
156void Preprocessor::DumpMacro(const MacroInfo &MI) const {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000157 llvm::cerr << "MACRO: ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
159 DumpToken(MI.getReplacementToken(i));
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000160 llvm::cerr << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000162 llvm::cerr << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
165void Preprocessor::PrintStats() {
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000166 llvm::cerr << "\n*** Preprocessor Stats:\n";
167 llvm::cerr << NumDirectives << " directives found:\n";
168 llvm::cerr << " " << NumDefined << " #define.\n";
169 llvm::cerr << " " << NumUndefined << " #undef.\n";
170 llvm::cerr << " #include/#include_next/#import:\n";
171 llvm::cerr << " " << NumEnteredSourceFiles << " source files entered.\n";
172 llvm::cerr << " " << MaxIncludeStackDepth << " max include stack depth\n";
173 llvm::cerr << " " << NumIf << " #if/#ifndef/#ifdef.\n";
174 llvm::cerr << " " << NumElse << " #else/#elif.\n";
175 llvm::cerr << " " << NumEndif << " #endif.\n";
176 llvm::cerr << " " << NumPragma << " #pragma.\n";
177 llvm::cerr << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000178
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000179 llvm::cerr << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
180 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
181 << NumFastMacroExpanded << " on the fast path.\n";
182 llvm::cerr << (NumFastTokenPaste+NumTokenPaste)
183 << " token paste (##) operations performed, "
184 << NumFastTokenPaste << " on the fast path.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000185}
186
187//===----------------------------------------------------------------------===//
188// Token Spelling
189//===----------------------------------------------------------------------===//
190
191
192/// getSpelling() - Return the 'spelling' of this token. The spelling of a
193/// token are the characters used to represent the token in the source file
194/// after trigraph expansion and escaped-newline folding. In particular, this
195/// wants to get the true, uncanonicalized, spelling of things like digraphs
196/// UCNs, etc.
Chris Lattnerd2177732007-07-20 16:59:19 +0000197std::string Preprocessor::getSpelling(const Token &Tok) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000199 const char* TokStart;
200
201 if (PTH) {
202 SourceLocation sloc = SourceMgr.getPhysicalLoc(Tok.getLocation());
Ted Kremenek28396602009-01-13 23:19:12 +0000203 unsigned fid = SourceMgr.getCanonicalFileID(sloc);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000204 unsigned fpos = SourceMgr.getFullFilePos(sloc);
205 if (unsigned len = PTH->getSpelling(fid, fpos, TokStart)) {
206 assert(!Tok.needsCleaning());
207 return std::string(TokStart, TokStart+len);
208 }
209 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
211 // If this token contains nothing interesting, return it directly.
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000212 TokStart = SourceMgr.getCharacterData(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 if (!Tok.needsCleaning())
214 return std::string(TokStart, TokStart+Tok.getLength());
215
216 std::string Result;
217 Result.reserve(Tok.getLength());
218
219 // Otherwise, hard case, relex the characters into the string.
220 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
221 Ptr != End; ) {
222 unsigned CharSize;
223 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features));
224 Ptr += CharSize;
225 }
226 assert(Result.size() != unsigned(Tok.getLength()) &&
227 "NeedsCleaning flag set on something that didn't need cleaning!");
228 return Result;
229}
230
231/// getSpelling - This method is used to get the spelling of a token into a
232/// preallocated buffer, instead of as an std::string. The caller is required
233/// to allocate enough space for the token, which is guaranteed to be at least
234/// Tok.getLength() bytes long. The actual length of the token is returned.
235///
236/// Note that this method may do two possible things: it may either fill in
237/// the buffer specified with characters, or it may *change the input pointer*
238/// to point to a constant buffer with the data already in it (avoiding a
239/// copy). The caller is not allowed to modify the returned buffer pointer
240/// if an internal buffer is returned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000241unsigned Preprocessor::getSpelling(const Token &Tok,
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 const char *&Buffer) const {
243 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!");
244
245 // If this token is an identifier, just return the string from the identifier
246 // table, which is very quick.
247 if (const IdentifierInfo *II = Tok.getIdentifierInfo()) {
248 Buffer = II->getName();
Chris Lattnere1dccae2009-01-05 19:44:41 +0000249 return II->getLength();
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 }
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000251
252 // If using PTH, try and get the spelling from the PTH file.
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000253 if (PTH) {
254 unsigned len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000255
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000256 if (CurPTHLexer) {
257 // We perform the const_cast<> here because we will only have a PTHLexer
258 // when grabbing a stream of tokens from the PTH file (and thus the
259 // Preprocessor state is allowed to change). The PTHLexer can assume we are
260 // getting token spellings in the order of tokens, and thus can update
261 // its internal state so that it can quickly fetch spellings from the PTH
262 // file.
263 len =
264 const_cast<PTHLexer*>(CurPTHLexer.get())->getSpelling(Tok.getLocation(),
265 Buffer);
266 }
267 else {
268 SourceLocation sloc = SourceMgr.getPhysicalLoc(Tok.getLocation());
Ted Kremenek28396602009-01-13 23:19:12 +0000269 unsigned fid = SourceMgr.getCanonicalFileID(sloc);
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000270 unsigned fpos = SourceMgr.getFullFilePos(sloc);
271 len = PTH->getSpelling(fid, fpos, Buffer);
272 }
273
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000274 // Did we find a spelling? If so return its length. Otherwise fall
275 // back to the default behavior for getting the spelling by looking at
Ted Kremenekf02f6f02009-01-13 22:05:50 +0000276 // at the source code.
277 if (len)
278 return len;
Ted Kremenekb70e3da2009-01-08 02:47:16 +0000279 }
280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // Otherwise, compute the start of the token in the input lexer buffer.
282 const char *TokStart = SourceMgr.getCharacterData(Tok.getLocation());
283
284 // If this token contains nothing interesting, return it directly.
285 if (!Tok.needsCleaning()) {
286 Buffer = TokStart;
287 return Tok.getLength();
288 }
289 // Otherwise, hard case, relex the characters into the string.
290 char *OutBuf = const_cast<char*>(Buffer);
291 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength();
292 Ptr != End; ) {
293 unsigned CharSize;
294 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features);
295 Ptr += CharSize;
296 }
297 assert(unsigned(OutBuf-Buffer) != Tok.getLength() &&
298 "NeedsCleaning flag set on something that didn't need cleaning!");
299
300 return OutBuf-Buffer;
301}
302
303
304/// CreateString - Plop the specified string into a scratch buffer and return a
305/// location for it. If specified, the source location provides a source
306/// location for the token.
307SourceLocation Preprocessor::
308CreateString(const char *Buf, unsigned Len, SourceLocation SLoc) {
309 if (SLoc.isValid())
310 return ScratchBuf->getToken(Buf, Len, SLoc);
311 return ScratchBuf->getToken(Buf, Len);
312}
313
314
Chris Lattner97ba77c2007-07-16 06:48:38 +0000315/// AdvanceToTokenCharacter - Given a location that specifies the start of a
316/// token, return a new location that specifies a character within the token.
317SourceLocation Preprocessor::AdvanceToTokenCharacter(SourceLocation TokStart,
318 unsigned CharNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000319 // If they request the first char of the token, we're trivially done. If this
320 // is a macro expansion, it doesn't make sense to point to a character within
321 // the instantiation point (the name). We could point to the source
322 // character, but without also pointing to instantiation info, this is
323 // confusing.
324 if (CharNo == 0 || TokStart.isMacroID()) return TokStart;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000325
326 // Figure out how many physical characters away the specified logical
327 // character is. This needs to take into consideration newlines and
328 // trigraphs.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000329 const char *TokPtr = SourceMgr.getCharacterData(TokStart);
330 unsigned PhysOffset = 0;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000331
332 // The usual case is that tokens don't contain anything interesting. Skip
333 // over the uninteresting characters. If a token only consists of simple
334 // chars, this method is extremely fast.
335 while (CharNo && Lexer::isObviouslySimpleCharacter(*TokPtr))
Chris Lattner9dc1f532007-07-20 16:37:10 +0000336 ++TokPtr, --CharNo, ++PhysOffset;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000337
338 // If we have a character that may be a trigraph or escaped newline, create a
339 // lexer to parse it correctly.
Chris Lattner97ba77c2007-07-16 06:48:38 +0000340 if (CharNo != 0) {
341 // Create a lexer starting at this token position.
Chris Lattner25bdb512007-07-20 16:52:03 +0000342 Lexer TheLexer(TokStart, *this, TokPtr);
Chris Lattnerd2177732007-07-20 16:59:19 +0000343 Token Tok;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000344 // Skip over characters the remaining characters.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000345 const char *TokStartPtr = TokPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000346 for (; CharNo; --CharNo)
347 TheLexer.getAndAdvanceChar(TokPtr, Tok);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000348
349 PhysOffset += TokPtr-TokStartPtr;
Chris Lattner97ba77c2007-07-16 06:48:38 +0000350 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000351
352 return TokStart.getFileLocWithOffset(PhysOffset);
Chris Lattner97ba77c2007-07-16 06:48:38 +0000353}
354
355
Chris Lattner53b0dab2007-10-09 22:10:18 +0000356//===----------------------------------------------------------------------===//
357// Preprocessor Initialization Methods
358//===----------------------------------------------------------------------===//
359
360// Append a #define line to Buf for Macro. Macro should be of the form XXX,
361// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
362// "#define XXX Y z W". To get a #define with no value, use "XXX=".
363static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
364 const char *Command = "#define ") {
365 Buf.insert(Buf.end(), Command, Command+strlen(Command));
366 if (const char *Equal = strchr(Macro, '=')) {
367 // Turn the = into ' '.
368 Buf.insert(Buf.end(), Macro, Equal);
369 Buf.push_back(' ');
370 Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
371 } else {
372 // Push "macroname 1".
373 Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
374 Buf.push_back(' ');
375 Buf.push_back('1');
376 }
377 Buf.push_back('\n');
378}
379
Chris Lattner2db78dd2008-10-05 20:40:30 +0000380/// PickFP - This is used to pick a value based on the FP semantics of the
381/// specified FP model.
382template <typename T>
383static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal,
384 T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) {
385 if (Sem == &llvm::APFloat::IEEEsingle)
386 return IEEESingleVal;
387 if (Sem == &llvm::APFloat::IEEEdouble)
388 return IEEEDoubleVal;
389 if (Sem == &llvm::APFloat::x87DoubleExtended)
390 return X87DoubleExtendedVal;
391 assert(Sem == &llvm::APFloat::PPCDoubleDouble);
392 return PPCDoubleDoubleVal;
393}
394
395static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix,
396 const llvm::fltSemantics *Sem) {
Chris Lattnere9863ca2008-10-05 21:40:58 +0000397 const char *DenormMin, *Epsilon, *Max, *Min;
398 DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324",
399 "3.64519953188247460253e-4951L",
400 "4.94065645841246544176568792868221e-324L");
401 int Digits = PickFP(Sem, 6, 15, 18, 31);
402 Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16",
403 "1.08420217248550443401e-19L",
404 "4.94065645841246544176568792868221e-324L");
405 int HasInifinity = 1, HasQuietNaN = 1;
406 int MantissaDigits = PickFP(Sem, 24, 53, 64, 106);
407 int Min10Exp = PickFP(Sem, -37, -307, -4931, -291);
408 int Max10Exp = PickFP(Sem, 38, 308, 4932, 308);
409 int MinExp = PickFP(Sem, -125, -1021, -16381, -968);
410 int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024);
411 Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308",
412 "3.36210314311209350626e-4932L",
413 "2.00416836000897277799610805135016e-292L");
414 Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308",
415 "1.18973149535723176502e+4932L",
416 "1.79769313486231580793728971405301e+308L");
Chris Lattner2db78dd2008-10-05 20:40:30 +0000417
418 char MacroBuf[60];
419 sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin);
420 DefineBuiltinMacro(Buf, MacroBuf);
Chris Lattnere9863ca2008-10-05 21:40:58 +0000421 sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits);
422 DefineBuiltinMacro(Buf, MacroBuf);
423 sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon);
424 DefineBuiltinMacro(Buf, MacroBuf);
425 sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity);
426 DefineBuiltinMacro(Buf, MacroBuf);
427 sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN);
428 DefineBuiltinMacro(Buf, MacroBuf);
429 sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits);
430 DefineBuiltinMacro(Buf, MacroBuf);
431 sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp);
432 DefineBuiltinMacro(Buf, MacroBuf);
433 sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp);
434 DefineBuiltinMacro(Buf, MacroBuf);
435 sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max);
436 DefineBuiltinMacro(Buf, MacroBuf);
437 sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp);
438 DefineBuiltinMacro(Buf, MacroBuf);
439 sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp);
440 DefineBuiltinMacro(Buf, MacroBuf);
441 sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min);
442 DefineBuiltinMacro(Buf, MacroBuf);
Chris Lattner2db78dd2008-10-05 20:40:30 +0000443}
444
Chris Lattner53b0dab2007-10-09 22:10:18 +0000445
446static void InitializePredefinedMacros(Preprocessor &PP,
447 std::vector<char> &Buf) {
Chris Lattner62213d92008-10-05 19:32:22 +0000448 // Compiler version introspection macros.
449 DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend
450 DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend
451
452 // Currently claim to be compatible with GCC 4.2.1-5621.
453 DefineBuiltinMacro(Buf, "__APPLE_CC__=5621");
454 DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2");
455 DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
456 DefineBuiltinMacro(Buf, "__GNUC__=4");
457 DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
458 DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 (Apple Computer, Inc. "
459 "build 5621) (dot 3)\"");
460
461
462 // Initialize language-specific preprocessor defines.
463
Chris Lattner53b0dab2007-10-09 22:10:18 +0000464 // FIXME: Implement magic like cpp_init_builtins for things like __STDC__
465 // and __DATE__ etc.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000466 // These should all be defined in the preprocessor according to the
467 // current language configuration.
Steve Naroff7e0fbb22008-12-18 22:37:25 +0000468 if (!PP.getLangOptions().Microsoft)
469 DefineBuiltinMacro(Buf, "__STDC__=1");
Daniel Dunbarc1571452008-12-01 18:55:22 +0000470 if (PP.getLangOptions().AsmPreprocessor)
471 DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
Chris Lattner53b0dab2007-10-09 22:10:18 +0000472 if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus)
473 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
474 else if (0) // STDC94 ?
475 DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
476
477 DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
Daniel Dunbarfba5cb12008-08-12 00:21:46 +0000478 if (PP.getLangOptions().ObjC1) {
Chris Lattner53b0dab2007-10-09 22:10:18 +0000479 DefineBuiltinMacro(Buf, "__OBJC__=1");
Daniel Dunbarfba5cb12008-08-12 00:21:46 +0000480
481 if (PP.getLangOptions().getGCMode() == LangOptions::NonGC) {
482 DefineBuiltinMacro(Buf, "__weak=");
483 DefineBuiltinMacro(Buf, "__strong=");
484 } else {
485 DefineBuiltinMacro(Buf, "__weak=__attribute__((objc_gc(weak)))");
486 DefineBuiltinMacro(Buf, "__strong=__attribute__((objc_gc(strong)))");
487 DefineBuiltinMacro(Buf, "__OBJC_GC__=1");
488 }
489
490 if (PP.getLangOptions().NeXTRuntime)
491 DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1");
Daniel Dunbarfba5cb12008-08-12 00:21:46 +0000492 }
Chris Lattner9b533162008-10-05 19:44:25 +0000493
Chris Lattnereb52b442008-10-06 07:43:09 +0000494 // darwin_constant_cfstrings controls this. This is also dependent
495 // on other things like the runtime I believe. This is set even for C code.
496 DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1");
497
Steve Naroff73b17cd2008-05-15 21:12:10 +0000498 if (PP.getLangOptions().ObjC2)
499 DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES");
Steve Naroff8ee529b2007-10-31 18:42:27 +0000500
Chris Lattner048dd942008-09-30 00:48:48 +0000501 if (PP.getLangOptions().PascalStrings)
502 DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__");
503
Chris Lattner62213d92008-10-05 19:32:22 +0000504 if (PP.getLangOptions().Blocks) {
505 DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))");
506 DefineBuiltinMacro(Buf, "__BLOCKS__=1");
Chris Lattner2b43ad92008-10-05 19:32:52 +0000507 }
Chris Lattner62213d92008-10-05 19:32:22 +0000508
509 if (PP.getLangOptions().CPlusPlus) {
510 DefineBuiltinMacro(Buf, "__DEPRECATED=1");
511 DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
512 DefineBuiltinMacro(Buf, "__GNUG__=4");
513 DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
514 DefineBuiltinMacro(Buf, "__cplusplus=1");
515 DefineBuiltinMacro(Buf, "__private_extern__=extern");
516 }
517
518 // Filter out some microsoft extensions when trying to parse in ms-compat
519 // mode.
520 if (PP.getLangOptions().Microsoft) {
Steve Naroff239f0732008-12-25 14:16:32 +0000521 DefineBuiltinMacro(Buf, "_cdecl=__cdecl");
Chris Lattner62213d92008-10-05 19:32:22 +0000522 DefineBuiltinMacro(Buf, "__int8=char");
523 DefineBuiltinMacro(Buf, "__int16=short");
524 DefineBuiltinMacro(Buf, "__int32=int");
525 DefineBuiltinMacro(Buf, "__int64=long long");
Chris Lattner62213d92008-10-05 19:32:22 +0000526 }
527
528
529 // Initialize target-specific preprocessor defines.
Chris Lattner9b533162008-10-05 19:44:25 +0000530 const TargetInfo &TI = PP.getTargetInfo();
531
532 // Define type sizing macros based on the target properties.
533 assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far");
534 DefineBuiltinMacro(Buf, "__CHAR_BIT__=8");
535 DefineBuiltinMacro(Buf, "__SCHAR_MAX__=127");
Chris Lattner2db78dd2008-10-05 20:40:30 +0000536
537 assert(TI.getWCharWidth() == 32 && "Only support 32-bit wchar so far");
538 DefineBuiltinMacro(Buf, "__WCHAR_MAX__=2147483647");
539 DefineBuiltinMacro(Buf, "__WCHAR_TYPE__=int");
540 DefineBuiltinMacro(Buf, "__WINT_TYPE__=int");
Chris Lattner9b533162008-10-05 19:44:25 +0000541
542 assert(TI.getShortWidth() == 16 && "Only support 16-bit short so far");
Chris Lattner9b533162008-10-05 19:44:25 +0000543 DefineBuiltinMacro(Buf, "__SHRT_MAX__=32767");
544
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000545 if (TI.getIntWidth() == 32)
546 DefineBuiltinMacro(Buf, "__INT_MAX__=2147483647");
547 else if (TI.getIntWidth() == 16)
548 DefineBuiltinMacro(Buf, "__INT_MAX__=32767");
549 else
550 assert(0 && "Unknown integer size");
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000551
552 if (TI.getLongLongWidth() == 64)
553 DefineBuiltinMacro(Buf, "__LONG_LONG_MAX__=9223372036854775807LL");
554 else if (TI.getLongLongWidth() == 32)
555 DefineBuiltinMacro(Buf, "__LONG_LONG_MAX__=2147483647L");
Chris Lattner9b533162008-10-05 19:44:25 +0000556
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000557 if (TI.getLongWidth() == 32)
558 DefineBuiltinMacro(Buf, "__LONG_MAX__=2147483647L");
559 else if (TI.getLongWidth() == 64)
560 DefineBuiltinMacro(Buf, "__LONG_MAX__=9223372036854775807L");
561 else if (TI.getLongWidth() == 16)
562 DefineBuiltinMacro(Buf, "__LONG_MAX__=32767L");
563 else
564 assert(0 && "Unknown long size");
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000565 char MacroBuf[60];
566 sprintf(MacroBuf, "__INTMAX_MAX__=%lld",
567 (TI.getIntMaxType() == TargetInfo::UnsignedLongLong?
Sanjiv Gupta73608a82008-10-31 10:24:31 +0000568 (1LL << (TI.getLongLongWidth() - 1)) :
569 ((1LL << (TI.getLongLongWidth() - 2)) - 1)));
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000570 DefineBuiltinMacro(Buf, MacroBuf);
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000571
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000572 if (TI.getIntMaxType() == TargetInfo::UnsignedLongLong)
573 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned long long int");
574 else if (TI.getIntMaxType() == TargetInfo::SignedLongLong)
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000575 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=long long int");
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000576 else if (TI.getIntMaxType() == TargetInfo::UnsignedLong)
577 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned long int");
578 else if (TI.getIntMaxType() == TargetInfo::SignedLong)
579 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=long int");
580 else if (TI.getIntMaxType() == TargetInfo::UnsignedInt)
581 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=unsigned int");
582 else
583 DefineBuiltinMacro(Buf, "__INTMAX_TYPE__=int");
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000584
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +0000585 if (TI.getUIntMaxType() == TargetInfo::UnsignedLongLong)
586 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned long long int");
587 else if (TI.getUIntMaxType() == TargetInfo::SignedLongLong)
588 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=long long int");
589 else if (TI.getUIntMaxType() == TargetInfo::UnsignedLong)
590 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned long int");
591 else if (TI.getUIntMaxType() == TargetInfo::SignedLong)
592 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=long int");
593 else if (TI.getUIntMaxType() == TargetInfo::UnsignedInt)
594 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=unsigned int");
595 else
596 DefineBuiltinMacro(Buf, "__UINTMAX_TYPE__=int");
597
598 if (TI.getPtrDiffType(0) == TargetInfo::UnsignedLongLong)
599 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned long long int");
600 else if (TI.getPtrDiffType(0) == TargetInfo::SignedLongLong)
601 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=long long int");
602 else if (TI.getPtrDiffType(0) == TargetInfo::UnsignedLong)
603 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned long int");
604 else if (TI.getPtrDiffType(0) == TargetInfo::SignedLong)
605 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=long int");
606 else if (TI.getPtrDiffType(0) == TargetInfo::UnsignedInt)
607 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=unsigned int");
608 else
609 DefineBuiltinMacro(Buf, "__PTRDIFF_TYPE__=int");
610
611 if (TI.getSizeType() == TargetInfo::UnsignedLongLong)
612 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned long long int");
613 else if (TI.getSizeType() == TargetInfo::SignedLongLong)
614 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=long long int");
615 else if (TI.getSizeType() == TargetInfo::UnsignedLong)
616 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned long int");
617 else if (TI.getSizeType() == TargetInfo::SignedLong)
618 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=long int");
619 else if (TI.getSizeType() == TargetInfo::UnsignedInt)
620 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned int");
621 else if (TI.getSizeType() == TargetInfo::SignedInt)
622 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=int");
623 else
624 DefineBuiltinMacro(Buf, "__SIZE_TYPE__=unsigned short");
625
Chris Lattner2db78dd2008-10-05 20:40:30 +0000626 DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat());
627 DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat());
628 DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat());
Chris Lattner0e5d4ef2008-10-05 20:06:37 +0000629
Chris Lattner048dd942008-09-30 00:48:48 +0000630
Chris Lattnerd19144b2007-10-10 17:48:53 +0000631 // Add __builtin_va_list typedef.
632 {
Chris Lattner9b533162008-10-05 19:44:25 +0000633 const char *VAList = TI.getVAListDeclaration();
Chris Lattnerd19144b2007-10-10 17:48:53 +0000634 Buf.insert(Buf.end(), VAList, VAList+strlen(VAList));
635 Buf.push_back('\n');
636 }
Chris Lattner53b0dab2007-10-09 22:10:18 +0000637
Chris Lattner9b533162008-10-05 19:44:25 +0000638 if (const char *Prefix = TI.getUserLabelPrefix()) {
Chris Lattner12f09262008-10-05 21:49:27 +0000639 sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix);
640 DefineBuiltinMacro(Buf, MacroBuf);
Chris Lattner3fdf4672008-10-05 19:22:37 +0000641 }
642
Chris Lattner9b533162008-10-05 19:44:25 +0000643 // Build configuration options. FIXME: these should be controlled by
644 // command line options or something.
Chris Lattner53b0dab2007-10-09 22:10:18 +0000645 DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
646 DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
647 DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
648 DefineBuiltinMacro(Buf, "__PIC__=1");
Chris Lattner9b533162008-10-05 19:44:25 +0000649
Chris Lattner12f09262008-10-05 21:49:27 +0000650 // Macros to control C99 numerics and <float.h>
651 DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0");
652 DefineBuiltinMacro(Buf, "__FLT_RADIX__=2");
653 sprintf(MacroBuf, "__DECIMAL_DIG__=%d",
654 PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33));
655 DefineBuiltinMacro(Buf, MacroBuf);
656
Chris Lattner9b533162008-10-05 19:44:25 +0000657 // Get other target #defines.
658 TI.getTargetDefines(Buf);
Chris Lattner53b0dab2007-10-09 22:10:18 +0000659
Chris Lattner53b0dab2007-10-09 22:10:18 +0000660 // FIXME: Should emit a #line directive here.
661}
662
663
664/// EnterMainSourceFile - Enter the specified FileID as the main source file,
Nate Begeman6b616022008-01-07 04:01:26 +0000665/// which implicitly adds the builtin defines etc.
Ted Kremenek95041a22007-12-19 22:51:13 +0000666void Preprocessor::EnterMainSourceFile() {
667
668 unsigned MainFileID = SourceMgr.getMainFileID();
669
Chris Lattner53b0dab2007-10-09 22:10:18 +0000670 // Enter the main file source buffer.
671 EnterSourceFile(MainFileID, 0);
672
Chris Lattnerb2832982007-11-15 19:07:47 +0000673 // Tell the header info that the main file was entered. If the file is later
674 // #imported, it won't be re-entered.
675 if (const FileEntry *FE =
676 SourceMgr.getFileEntryForLoc(SourceLocation::getFileLoc(MainFileID, 0)))
677 HeaderInfo.IncrementIncludeCount(FE);
678
Chris Lattner53b0dab2007-10-09 22:10:18 +0000679 std::vector<char> PrologFile;
680 PrologFile.reserve(4080);
681
682 // Install things like __POWERPC__, __GNUC__, etc into the macro table.
683 InitializePredefinedMacros(*this, PrologFile);
684
685 // Add on the predefines from the driver.
Chris Lattneraa391972008-04-19 23:09:31 +0000686 PrologFile.insert(PrologFile.end(), Predefines.begin(), Predefines.end());
Chris Lattner53b0dab2007-10-09 22:10:18 +0000687
688 // Memory buffer must end with a null byte!
689 PrologFile.push_back(0);
690
691 // Now that we have emitted the predefined macros, #includes, etc into
692 // PrologFile, preprocess it to populate the initial preprocessor state.
693 llvm::MemoryBuffer *SB =
694 llvm::MemoryBuffer::getMemBufferCopy(&PrologFile.front(),&PrologFile.back(),
695 "<predefines>");
696 assert(SB && "Cannot fail to create predefined source buffer");
697 unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
698 assert(FileID && "Could not create FileID for predefines?");
699
700 // Start parsing the predefines.
701 EnterSourceFile(FileID, 0);
702}
Chris Lattner97ba77c2007-07-16 06:48:38 +0000703
Reid Spencer5f016e22007-07-11 17:01:13 +0000704
705//===----------------------------------------------------------------------===//
706// Lexer Event Handling.
707//===----------------------------------------------------------------------===//
708
709/// LookUpIdentifierInfo - Given a tok::identifier token, look up the
710/// identifier information for the token and install it into the token.
Chris Lattnerd2177732007-07-20 16:59:19 +0000711IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier,
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 const char *BufPtr) {
Chris Lattner22f6bbc2007-10-09 18:02:16 +0000713 assert(Identifier.is(tok::identifier) && "Not an identifier!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 assert(Identifier.getIdentifierInfo() == 0 && "Identinfo already exists!");
715
716 // Look up this token, see if it is a macro, or if it is a language keyword.
717 IdentifierInfo *II;
718 if (BufPtr && !Identifier.needsCleaning()) {
719 // No cleaning needed, just use the characters from the lexed buffer.
720 II = getIdentifierInfo(BufPtr, BufPtr+Identifier.getLength());
721 } else {
722 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattnerc35717a2007-07-13 17:10:38 +0000723 llvm::SmallVector<char, 64> IdentifierBuffer;
724 IdentifierBuffer.resize(Identifier.getLength());
725 const char *TmpBuf = &IdentifierBuffer[0];
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 unsigned Size = getSpelling(Identifier, TmpBuf);
727 II = getIdentifierInfo(TmpBuf, TmpBuf+Size);
728 }
729 Identifier.setIdentifierInfo(II);
730 return II;
731}
732
733
734/// HandleIdentifier - This callback is invoked when the lexer reads an
735/// identifier. This callback looks up the identifier in the map and/or
736/// potentially macro expands it or turns it into a named token (like 'for').
Chris Lattnerd2177732007-07-20 16:59:19 +0000737void Preprocessor::HandleIdentifier(Token &Identifier) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 assert(Identifier.getIdentifierInfo() &&
739 "Can't handle identifiers without identifier info!");
740
741 IdentifierInfo &II = *Identifier.getIdentifierInfo();
742
743 // If this identifier was poisoned, and if it was not produced from a macro
744 // expansion, emit an error.
Ted Kremenek1a531572008-11-19 22:43:49 +0000745 if (II.isPoisoned() && CurPPLexer) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 if (&II != Ident__VA_ARGS__) // We warn about __VA_ARGS__ with poisoning.
747 Diag(Identifier, diag::err_pp_used_poisoned_id);
748 else
749 Diag(Identifier, diag::ext_pp_bad_vaargs_use);
750 }
751
752 // If this is a macro to be expanded, do it.
Chris Lattnercc1a8752007-10-07 08:44:20 +0000753 if (MacroInfo *MI = getMacroInfo(&II)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) {
755 if (MI->isEnabled()) {
756 if (!HandleMacroExpandedIdentifier(Identifier, MI))
757 return;
758 } else {
759 // C99 6.10.3.4p2 says that a disabled macro may never again be
760 // expanded, even if it's in a context where it could be expanded in the
761 // future.
Chris Lattnerd2177732007-07-20 16:59:19 +0000762 Identifier.setFlag(Token::DisableExpand);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763 }
764 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 }
766
767 // C++ 2.11p2: If this is an alternative representation of a C++ operator,
768 // then we act as if it is the actual operator and not the textual
769 // representation of it.
770 if (II.isCPlusPlusOperatorKeyword())
771 Identifier.setIdentifierInfo(0);
772
773 // Change the kind of this identifier to the appropriate token kind, e.g.
774 // turning "for" into a keyword.
775 Identifier.setKind(II.getTokenID());
776
777 // If this is an extension token, diagnose its use.
Steve Naroffb4eaf9c2008-09-02 18:50:17 +0000778 // We avoid diagnosing tokens that originate from macro definitions.
779 if (II.isExtensionToken() && Features.C99 && !DisableMacroExpansion)
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 Diag(Identifier, diag::ext_token_used);
781}