blob: 53e1f8dc710ff5e6cb99795ffafed58b3cd7fa2f [file] [log] [blame]
Chris Lattnerc7a39682008-03-09 03:13:06 +00001//===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the top level handling of macro expasion for the
11// preprocessor.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/Preprocessor.h"
16#include "MacroArgs.h"
17#include "clang/Lex/MacroInfo.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Basic/FileManager.h"
20#include "clang/Basic/Diagnostic.h"
Chris Lattner54fd1812008-03-18 05:59:11 +000021#include <ctime>
Chris Lattnerc7a39682008-03-09 03:13:06 +000022using namespace clang;
23
24/// setMacroInfo - Specify a macro for this identifier.
25///
26void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) {
27 if (MI == 0) {
28 if (II->hasMacroDefinition()) {
29 Macros.erase(II);
30 II->setHasMacroDefinition(false);
31 }
32 } else {
33 Macros[II] = MI;
34 II->setHasMacroDefinition(true);
35 }
36}
37
38/// RegisterBuiltinMacro - Register the specified identifier in the identifier
39/// table and mark it as a builtin macro to be expanded.
40IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) {
41 // Get the identifier.
42 IdentifierInfo *Id = getIdentifierInfo(Name);
43
44 // Mark it as being a macro that is builtin.
45 MacroInfo *MI = new MacroInfo(SourceLocation());
46 MI->setIsBuiltinMacro();
47 setMacroInfo(Id, MI);
48 return Id;
49}
50
51
52/// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the
53/// identifier table.
54void Preprocessor::RegisterBuiltinMacros() {
55 Ident__LINE__ = RegisterBuiltinMacro("__LINE__");
56 Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
57 Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
58 Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
59 Ident_Pragma = RegisterBuiltinMacro("_Pragma");
60
61 // GCC Extensions.
62 Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
63 Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
64 Ident__TIMESTAMP__ = RegisterBuiltinMacro("__TIMESTAMP__");
65}
66
67/// isTrivialSingleTokenExpansion - Return true if MI, which has a single token
68/// in its expansion, currently expands to that token literally.
69static bool isTrivialSingleTokenExpansion(const MacroInfo *MI,
70 const IdentifierInfo *MacroIdent,
71 Preprocessor &PP) {
72 IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo();
73
74 // If the token isn't an identifier, it's always literally expanded.
75 if (II == 0) return true;
76
77 // If the identifier is a macro, and if that macro is enabled, it may be
78 // expanded so it's not a trivial expansion.
79 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() &&
80 // Fast expanding "#define X X" is ok, because X would be disabled.
81 II != MacroIdent)
82 return false;
83
84 // If this is an object-like macro invocation, it is safe to trivially expand
85 // it.
86 if (MI->isObjectLike()) return true;
87
88 // If this is a function-like macro invocation, it's safe to trivially expand
89 // as long as the identifier is not a macro argument.
90 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
91 I != E; ++I)
92 if (*I == II)
93 return false; // Identifier is a macro argument.
94
95 return true;
96}
97
98
99/// isNextPPTokenLParen - Determine whether the next preprocessor token to be
100/// lexed is a '('. If so, consume the token and return true, if not, this
101/// method should have no observable side-effect on the lexed tokens.
102bool Preprocessor::isNextPPTokenLParen() {
103 // Do some quick tests for rejection cases.
104 unsigned Val;
105 if (CurLexer)
106 Val = CurLexer->isNextPPTokenLParen();
Ted Kremenek3acf6702008-11-19 22:43:49 +0000107 else if (CurPTHLexer)
108 Val = CurPTHLexer->isNextPPTokenLParen();
Chris Lattnerc7a39682008-03-09 03:13:06 +0000109 else
110 Val = CurTokenLexer->isNextTokenLParen();
111
112 if (Val == 2) {
113 // We have run off the end. If it's a source file we don't
114 // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the
115 // macro stack.
Ted Kremenekb53b1f42008-11-19 22:21:33 +0000116 if (CurPPLexer)
Chris Lattnerc7a39682008-03-09 03:13:06 +0000117 return false;
118 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
119 IncludeStackInfo &Entry = IncludeMacroStack[i-1];
120 if (Entry.TheLexer)
121 Val = Entry.TheLexer->isNextPPTokenLParen();
122 else
123 Val = Entry.TheTokenLexer->isNextTokenLParen();
124
125 if (Val != 2)
126 break;
127
128 // Ran off the end of a source file?
129 if (Entry.TheLexer)
130 return false;
131 }
132 }
133
134 // Okay, if we know that the token is a '(', lex it and return. Otherwise we
135 // have found something that isn't a '(' or we found the end of the
136 // translation unit. In either case, return false.
137 if (Val != 1)
138 return false;
139
140 Token Tok;
141 LexUnexpandedToken(Tok);
142 assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?");
143 return true;
144}
145
146/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
147/// expanded as a macro, handle it and return the next token as 'Identifier'.
148bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
149 MacroInfo *MI) {
150 // If this is a macro exapnsion in the "#if !defined(x)" line for the file,
151 // then the macro could expand to different things in other contexts, we need
152 // to disable the optimization in this case.
Ted Kremenek31dd0262008-11-18 01:12:54 +0000153 if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro();
Chris Lattnerc7a39682008-03-09 03:13:06 +0000154
155 // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
156 if (MI->isBuiltinMacro()) {
157 ExpandBuiltinMacro(Identifier);
158 return false;
159 }
160
161 /// Args - If this is a function-like macro expansion, this contains,
162 /// for each macro argument, the list of tokens that were provided to the
163 /// invocation.
164 MacroArgs *Args = 0;
165
166 // If this is a function-like macro, read the arguments.
167 if (MI->isFunctionLike()) {
168 // C99 6.10.3p10: If the preprocessing token immediately after the the macro
169 // name isn't a '(', this macro should not be expanded. Otherwise, consume
170 // it.
171 if (!isNextPPTokenLParen())
172 return true;
173
174 // Remember that we are now parsing the arguments to a macro invocation.
175 // Preprocessor directives used inside macro arguments are not portable, and
176 // this enables the warning.
177 InMacroArgs = true;
178 Args = ReadFunctionLikeMacroArgs(Identifier, MI);
179
180 // Finished parsing args.
181 InMacroArgs = false;
182
183 // If there was an error parsing the arguments, bail out.
184 if (Args == 0) return false;
185
186 ++NumFnMacroExpanded;
187 } else {
188 ++NumMacroExpanded;
189 }
190
191 // Notice that this macro has been used.
192 MI->setIsUsed(true);
193
194 // If we started lexing a macro, enter the macro expansion body.
195
196 // If this macro expands to no tokens, don't bother to push it onto the
197 // expansion stack, only to take it right back off.
198 if (MI->getNumTokens() == 0) {
199 // No need for arg info.
200 if (Args) Args->destroy();
201
202 // Ignore this macro use, just return the next token in the current
203 // buffer.
204 bool HadLeadingSpace = Identifier.hasLeadingSpace();
205 bool IsAtStartOfLine = Identifier.isAtStartOfLine();
206
207 Lex(Identifier);
208
209 // If the identifier isn't on some OTHER line, inherit the leading
210 // whitespace/first-on-a-line property of this token. This handles
211 // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is
212 // empty.
213 if (!Identifier.isAtStartOfLine()) {
214 if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine);
215 if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace);
216 }
217 ++NumFastMacroExpanded;
218 return false;
219
220 } else if (MI->getNumTokens() == 1 &&
221 isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(),
222 *this)){
223 // Otherwise, if this macro expands into a single trivially-expanded
224 // token: expand it now. This handles common cases like
225 // "#define VAL 42".
Sam Bishopa7fa72f2008-03-21 07:13:02 +0000226
227 // No need for arg info.
228 if (Args) Args->destroy();
229
Chris Lattnerc7a39682008-03-09 03:13:06 +0000230 // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro
231 // identifier to the expanded token.
232 bool isAtStartOfLine = Identifier.isAtStartOfLine();
233 bool hasLeadingSpace = Identifier.hasLeadingSpace();
234
235 // Remember where the token is instantiated.
236 SourceLocation InstantiateLoc = Identifier.getLocation();
237
238 // Replace the result token.
239 Identifier = MI->getReplacementToken(0);
240
241 // Restore the StartOfLine/LeadingSpace markers.
242 Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine);
243 Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace);
244
245 // Update the tokens location to include both its logical and physical
246 // locations.
247 SourceLocation Loc =
248 SourceMgr.getInstantiationLoc(Identifier.getLocation(), InstantiateLoc);
249 Identifier.setLocation(Loc);
250
251 // If this is #define X X, we must mark the result as unexpandible.
252 if (IdentifierInfo *NewII = Identifier.getIdentifierInfo())
253 if (getMacroInfo(NewII) == MI)
254 Identifier.setFlag(Token::DisableExpand);
255
256 // Since this is not an identifier token, it can't be macro expanded, so
257 // we're done.
258 ++NumFastMacroExpanded;
259 return false;
260 }
261
262 // Start expanding the macro.
263 EnterMacro(Identifier, Args);
264
265 // Now that the macro is at the top of the include stack, ask the
266 // preprocessor to read the next token from it.
267 Lex(Identifier);
268 return false;
269}
270
271/// ReadFunctionLikeMacroArgs - After reading "MACRO(", this method is
272/// invoked to read all of the actual arguments specified for the macro
273/// invocation. This returns null on error.
274MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName,
275 MacroInfo *MI) {
276 // The number of fixed arguments to parse.
277 unsigned NumFixedArgsLeft = MI->getNumArgs();
278 bool isVariadic = MI->isVariadic();
279
280 // Outer loop, while there are more arguments, keep reading them.
281 Token Tok;
282 Tok.setKind(tok::comma);
283 --NumFixedArgsLeft; // Start reading the first arg.
284
285 // ArgTokens - Build up a list of tokens that make up each argument. Each
286 // argument is separated by an EOF token. Use a SmallVector so we can avoid
287 // heap allocations in the common case.
288 llvm::SmallVector<Token, 64> ArgTokens;
289
290 unsigned NumActuals = 0;
291 while (Tok.is(tok::comma)) {
292 // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note
293 // that we already consumed the first one.
294 unsigned NumParens = 0;
295
296 while (1) {
297 // Read arguments as unexpanded tokens. This avoids issues, e.g., where
298 // an argument value in a macro could expand to ',' or '(' or ')'.
299 LexUnexpandedToken(Tok);
300
301 if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n"
302 Diag(MacroName, diag::err_unterm_macro_invoc);
303 // Do not lose the EOF/EOM. Return it to the client.
304 MacroName = Tok;
305 return 0;
306 } else if (Tok.is(tok::r_paren)) {
307 // If we found the ) token, the macro arg list is done.
308 if (NumParens-- == 0)
309 break;
310 } else if (Tok.is(tok::l_paren)) {
311 ++NumParens;
312 } else if (Tok.is(tok::comma) && NumParens == 0) {
313 // Comma ends this argument if there are more fixed arguments expected.
314 if (NumFixedArgsLeft)
315 break;
316
317 // If this is not a variadic macro, too many args were specified.
318 if (!isVariadic) {
319 // Emit the diagnostic at the macro name in case there is a missing ).
320 // Emitting it at the , could be far away from the macro name.
321 Diag(MacroName, diag::err_too_many_args_in_macro_invoc);
322 return 0;
323 }
324 // Otherwise, continue to add the tokens to this variable argument.
325 } else if (Tok.is(tok::comment) && !KeepMacroComments) {
326 // If this is a comment token in the argument list and we're just in
327 // -C mode (not -CC mode), discard the comment.
328 continue;
329 } else if (Tok.is(tok::identifier)) {
330 // Reading macro arguments can cause macros that we are currently
331 // expanding from to be popped off the expansion stack. Doing so causes
332 // them to be reenabled for expansion. Here we record whether any
333 // identifiers we lex as macro arguments correspond to disabled macros.
334 // If so, we mark the token as noexpand. This is a subtle aspect of
335 // C99 6.10.3.4p2.
336 if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo()))
337 if (!MI->isEnabled())
338 Tok.setFlag(Token::DisableExpand);
339 }
340
341 ArgTokens.push_back(Tok);
342 }
343
344 // Empty arguments are standard in C99 and supported as an extension in
345 // other modes.
346 if (ArgTokens.empty() && !Features.C99)
347 Diag(Tok, diag::ext_empty_fnmacro_arg);
348
349 // Add a marker EOF token to the end of the token list for this argument.
350 Token EOFTok;
351 EOFTok.startToken();
352 EOFTok.setKind(tok::eof);
353 EOFTok.setLocation(Tok.getLocation());
354 EOFTok.setLength(0);
355 ArgTokens.push_back(EOFTok);
356 ++NumActuals;
357 --NumFixedArgsLeft;
358 };
359
360 // Okay, we either found the r_paren. Check to see if we parsed too few
361 // arguments.
362 unsigned MinArgsExpected = MI->getNumArgs();
363
364 // See MacroArgs instance var for description of this.
365 bool isVarargsElided = false;
366
367 if (NumActuals < MinArgsExpected) {
368 // There are several cases where too few arguments is ok, handle them now.
369 if (NumActuals+1 == MinArgsExpected && MI->isVariadic()) {
370 // Varargs where the named vararg parameter is missing: ok as extension.
371 // #define A(x, ...)
372 // A("blah")
373 Diag(Tok, diag::ext_missing_varargs_arg);
374
Chris Lattnerdd2e5312008-05-08 05:10:33 +0000375 // Remember this occurred if this is a macro invocation with at least
376 // one actual argument. This allows us to elide the comma when used for
377 // cases like:
378 // #define A(x, foo...) blah(a, ## foo)
379 // #define A(x, ...) blah(a, ## __VA_ARGS__)
380 isVarargsElided = MI->getNumArgs() > 1;
Chris Lattnerc7a39682008-03-09 03:13:06 +0000381 } else if (MI->getNumArgs() == 1) {
382 // #define A(x)
383 // A()
384 // is ok because it is an empty argument.
385
386 // Empty arguments are standard in C99 and supported as an extension in
387 // other modes.
388 if (ArgTokens.empty() && !Features.C99)
389 Diag(Tok, diag::ext_empty_fnmacro_arg);
390 } else {
391 // Otherwise, emit the error.
392 Diag(Tok, diag::err_too_few_args_in_macro_invoc);
393 return 0;
394 }
395
396 // Add a marker EOF token to the end of the token list for this argument.
397 SourceLocation EndLoc = Tok.getLocation();
398 Tok.startToken();
399 Tok.setKind(tok::eof);
400 Tok.setLocation(EndLoc);
401 Tok.setLength(0);
402 ArgTokens.push_back(Tok);
403 }
404
405 return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided);
406}
407
408/// ComputeDATE_TIME - Compute the current time, enter it into the specified
409/// scratch buffer, then return DATELoc/TIMELoc locations with the position of
410/// the identifier tokens inserted.
411static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc,
412 Preprocessor &PP) {
413 time_t TT = time(0);
414 struct tm *TM = localtime(&TT);
415
416 static const char * const Months[] = {
417 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
418 };
419
420 char TmpBuffer[100];
421 sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday,
422 TM->tm_year+1900);
423 DATELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
424
425 sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec);
426 TIMELoc = PP.CreateString(TmpBuffer, strlen(TmpBuffer));
427}
428
429/// ExpandBuiltinMacro - If an identifier token is read that is to be expanded
430/// as a builtin macro, handle it and return the next token as 'Tok'.
431void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
432 // Figure out which token this is.
433 IdentifierInfo *II = Tok.getIdentifierInfo();
434 assert(II && "Can't be a macro without id info!");
435
436 // If this is an _Pragma directive, expand it, invoke the pragma handler, then
437 // lex the token after it.
438 if (II == Ident_Pragma)
439 return Handle_Pragma(Tok);
440
441 ++NumBuiltinMacroExpanded;
442
443 char TmpBuffer[100];
444
445 // Set up the return result.
446 Tok.setIdentifierInfo(0);
447 Tok.clearFlag(Token::NeedsCleaning);
448
449 if (II == Ident__LINE__) {
Chris Lattner9501a482008-09-29 23:12:31 +0000450 // __LINE__ expands to a simple numeric value. Add a space after it so that
451 // it will tokenize as a number (and not run into stuff after it in the temp
452 // buffer).
453 sprintf(TmpBuffer, "%u ",
454 SourceMgr.getLogicalLineNumber(Tok.getLocation()));
455 unsigned Length = strlen(TmpBuffer)-1;
Chris Lattnerc7a39682008-03-09 03:13:06 +0000456 Tok.setKind(tok::numeric_constant);
457 Tok.setLength(Length);
Chris Lattner9501a482008-09-29 23:12:31 +0000458 Tok.setLocation(CreateString(TmpBuffer, Length+1, Tok.getLocation()));
Chris Lattnerc7a39682008-03-09 03:13:06 +0000459 } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) {
460 SourceLocation Loc = Tok.getLocation();
461 if (II == Ident__BASE_FILE__) {
462 Diag(Tok, diag::ext_pp_base_file);
463 SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc);
464 while (NextLoc.isValid()) {
465 Loc = NextLoc;
466 NextLoc = SourceMgr.getIncludeLoc(Loc);
467 }
468 }
469
470 // Escape this filename. Turn '\' -> '\\' '"' -> '\"'
471 std::string FN = SourceMgr.getSourceName(SourceMgr.getLogicalLoc(Loc));
472 FN = '"' + Lexer::Stringify(FN) + '"';
473 Tok.setKind(tok::string_literal);
474 Tok.setLength(FN.size());
475 Tok.setLocation(CreateString(&FN[0], FN.size(), Tok.getLocation()));
476 } else if (II == Ident__DATE__) {
477 if (!DATELoc.isValid())
478 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
479 Tok.setKind(tok::string_literal);
480 Tok.setLength(strlen("\"Mmm dd yyyy\""));
481 Tok.setLocation(SourceMgr.getInstantiationLoc(DATELoc, Tok.getLocation()));
482 } else if (II == Ident__TIME__) {
483 if (!TIMELoc.isValid())
484 ComputeDATE_TIME(DATELoc, TIMELoc, *this);
485 Tok.setKind(tok::string_literal);
486 Tok.setLength(strlen("\"hh:mm:ss\""));
487 Tok.setLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
488 } else if (II == Ident__INCLUDE_LEVEL__) {
489 Diag(Tok, diag::ext_pp_include_level);
490
491 // Compute the include depth of this token.
492 unsigned Depth = 0;
493 SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation());
494 for (; Loc.isValid(); ++Depth)
495 Loc = SourceMgr.getIncludeLoc(Loc);
496
Chris Lattner9501a482008-09-29 23:12:31 +0000497 // __INCLUDE_LEVEL__ expands to a simple numeric value. Add a space after
498 // it so that it will tokenize as a number (and not run into stuff after it
499 // in the temp buffer).
500 sprintf(TmpBuffer, "%u ", Depth);
501 unsigned Length = strlen(TmpBuffer)-1;
Chris Lattnerc7a39682008-03-09 03:13:06 +0000502 Tok.setKind(tok::numeric_constant);
503 Tok.setLength(Length);
504 Tok.setLocation(CreateString(TmpBuffer, Length, Tok.getLocation()));
505 } else if (II == Ident__TIMESTAMP__) {
506 // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
507 // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
508 Diag(Tok, diag::ext_pp_timestamp);
509
510 // Get the file that we are lexing out of. If we're currently lexing from
511 // a macro, dig into the include stack.
512 const FileEntry *CurFile = 0;
513 Lexer *TheLexer = getCurrentFileLexer();
514
515 if (TheLexer)
516 CurFile = SourceMgr.getFileEntryForLoc(TheLexer->getFileLoc());
517
518 // If this file is older than the file it depends on, emit a diagnostic.
519 const char *Result;
520 if (CurFile) {
521 time_t TT = CurFile->getModificationTime();
522 struct tm *TM = localtime(&TT);
523 Result = asctime(TM);
524 } else {
525 Result = "??? ??? ?? ??:??:?? ????\n";
526 }
527 TmpBuffer[0] = '"';
528 strcpy(TmpBuffer+1, Result);
529 unsigned Len = strlen(TmpBuffer);
530 TmpBuffer[Len-1] = '"'; // Replace the newline with a quote.
531 Tok.setKind(tok::string_literal);
532 Tok.setLength(Len);
Chris Lattner9501a482008-09-29 23:12:31 +0000533 Tok.setLocation(CreateString(TmpBuffer, Len+1, Tok.getLocation()));
Chris Lattnerc7a39682008-03-09 03:13:06 +0000534 } else {
535 assert(0 && "Unknown identifier!");
536 }
537}