Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 1 | //===--- 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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 20 | #include "clang/Lex/LexDiagnostic.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Chris Lattner | f90a248 | 2008-03-18 05:59:11 +0000 | [diff] [blame] | 22 | #include <ctime> |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | /// setMacroInfo - Specify a macro for this identifier. |
| 26 | /// |
| 27 | void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { |
Chris Lattner | 555589d | 2009-04-10 21:17:07 +0000 | [diff] [blame] | 28 | if (MI) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 29 | Macros[II] = MI; |
| 30 | II->setHasMacroDefinition(true); |
Chris Lattner | 555589d | 2009-04-10 21:17:07 +0000 | [diff] [blame] | 31 | } else if (II->hasMacroDefinition()) { |
| 32 | Macros.erase(II); |
| 33 | II->setHasMacroDefinition(false); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
| 37 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 38 | /// table and mark it as a builtin macro to be expanded. |
| 39 | IdentifierInfo *Preprocessor::RegisterBuiltinMacro(const char *Name) { |
| 40 | // Get the identifier. |
| 41 | IdentifierInfo *Id = getIdentifierInfo(Name); |
| 42 | |
| 43 | // Mark it as being a macro that is builtin. |
Ted Kremenek | 0ea7672 | 2008-12-15 19:56:42 +0000 | [diff] [blame] | 44 | MacroInfo *MI = AllocateMacroInfo(SourceLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 45 | MI->setIsBuiltinMacro(); |
| 46 | setMacroInfo(Id, MI); |
| 47 | return Id; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 52 | /// identifier table. |
| 53 | void Preprocessor::RegisterBuiltinMacros() { |
| 54 | Ident__LINE__ = RegisterBuiltinMacro("__LINE__"); |
| 55 | Ident__FILE__ = RegisterBuiltinMacro("__FILE__"); |
| 56 | Ident__DATE__ = RegisterBuiltinMacro("__DATE__"); |
| 57 | Ident__TIME__ = RegisterBuiltinMacro("__TIME__"); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 58 | Ident__COUNTER__ = RegisterBuiltinMacro("__COUNTER__"); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 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. |
| 69 | static 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. |
| 102 | bool Preprocessor::isNextPPTokenLParen() { |
| 103 | // Do some quick tests for rejection cases. |
| 104 | unsigned Val; |
| 105 | if (CurLexer) |
| 106 | Val = CurLexer->isNextPPTokenLParen(); |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 107 | else if (CurPTHLexer) |
| 108 | Val = CurPTHLexer->isNextPPTokenLParen(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 109 | 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 Kremenek | 17ff58a | 2008-11-19 22:21:33 +0000 | [diff] [blame] | 116 | if (CurPPLexer) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 117 | 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(); |
Ted Kremenek | dd95d6c | 2008-11-20 16:46:54 +0000 | [diff] [blame] | 122 | else if (Entry.ThePTHLexer) |
| 123 | Val = Entry.ThePTHLexer->isNextPPTokenLParen(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 124 | else |
| 125 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
| 126 | |
| 127 | if (Val != 2) |
| 128 | break; |
| 129 | |
| 130 | // Ran off the end of a source file? |
Ted Kremenek | dd95d6c | 2008-11-20 16:46:54 +0000 | [diff] [blame] | 131 | if (Entry.ThePPLexer) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 137 | // have found something that isn't a '(' or we found the end of the |
| 138 | // translation unit. In either case, return false. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 139 | return Val == 1; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 143 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 144 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
| 145 | MacroInfo *MI) { |
Chris Lattner | ba9eee3 | 2009-03-12 17:31:43 +0000 | [diff] [blame] | 146 | if (Callbacks) Callbacks->MacroExpands(Identifier, MI); |
| 147 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 148 | // If this is a macro exapnsion in the "#if !defined(x)" line for the file, |
| 149 | // then the macro could expand to different things in other contexts, we need |
| 150 | // to disable the optimization in this case. |
Ted Kremenek | 68a91d5 | 2008-11-18 01:12:54 +0000 | [diff] [blame] | 151 | if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 152 | |
| 153 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 154 | if (MI->isBuiltinMacro()) { |
| 155 | ExpandBuiltinMacro(Identifier); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | /// Args - If this is a function-like macro expansion, this contains, |
| 160 | /// for each macro argument, the list of tokens that were provided to the |
| 161 | /// invocation. |
| 162 | MacroArgs *Args = 0; |
| 163 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 164 | // Remember where the end of the instantiation occurred. For an object-like |
| 165 | // macro, this is the identifier. For a function-like macro, this is the ')'. |
| 166 | SourceLocation InstantiationEnd = Identifier.getLocation(); |
| 167 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 168 | // If this is a function-like macro, read the arguments. |
| 169 | if (MI->isFunctionLike()) { |
| 170 | // C99 6.10.3p10: If the preprocessing token immediately after the the macro |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 171 | // name isn't a '(', this macro should not be expanded. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 172 | if (!isNextPPTokenLParen()) |
| 173 | return true; |
| 174 | |
| 175 | // Remember that we are now parsing the arguments to a macro invocation. |
| 176 | // Preprocessor directives used inside macro arguments are not portable, and |
| 177 | // this enables the warning. |
| 178 | InMacroArgs = true; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 179 | Args = ReadFunctionLikeMacroArgs(Identifier, MI, InstantiationEnd); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 180 | |
| 181 | // Finished parsing args. |
| 182 | InMacroArgs = false; |
| 183 | |
| 184 | // If there was an error parsing the arguments, bail out. |
| 185 | if (Args == 0) return false; |
| 186 | |
| 187 | ++NumFnMacroExpanded; |
| 188 | } else { |
| 189 | ++NumMacroExpanded; |
| 190 | } |
| 191 | |
| 192 | // Notice that this macro has been used. |
| 193 | MI->setIsUsed(true); |
| 194 | |
| 195 | // If we started lexing a macro, enter the macro expansion body. |
| 196 | |
| 197 | // If this macro expands to no tokens, don't bother to push it onto the |
| 198 | // expansion stack, only to take it right back off. |
| 199 | if (MI->getNumTokens() == 0) { |
| 200 | // No need for arg info. |
| 201 | if (Args) Args->destroy(); |
| 202 | |
| 203 | // Ignore this macro use, just return the next token in the current |
| 204 | // buffer. |
| 205 | bool HadLeadingSpace = Identifier.hasLeadingSpace(); |
| 206 | bool IsAtStartOfLine = Identifier.isAtStartOfLine(); |
| 207 | |
| 208 | Lex(Identifier); |
| 209 | |
| 210 | // If the identifier isn't on some OTHER line, inherit the leading |
| 211 | // whitespace/first-on-a-line property of this token. This handles |
| 212 | // stuff like "! XX," -> "! ," and " XX," -> " ,", when XX is |
| 213 | // empty. |
| 214 | if (!Identifier.isAtStartOfLine()) { |
| 215 | if (IsAtStartOfLine) Identifier.setFlag(Token::StartOfLine); |
| 216 | if (HadLeadingSpace) Identifier.setFlag(Token::LeadingSpace); |
| 217 | } |
| 218 | ++NumFastMacroExpanded; |
| 219 | return false; |
| 220 | |
| 221 | } else if (MI->getNumTokens() == 1 && |
| 222 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 223 | *this)) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 224 | // Otherwise, if this macro expands into a single trivially-expanded |
| 225 | // token: expand it now. This handles common cases like |
| 226 | // "#define VAL 42". |
Sam Bishop | 9a4939f | 2008-03-21 07:13:02 +0000 | [diff] [blame] | 227 | |
| 228 | // No need for arg info. |
| 229 | if (Args) Args->destroy(); |
| 230 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 231 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 232 | // identifier to the expanded token. |
| 233 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 234 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 235 | |
| 236 | // Remember where the token is instantiated. |
| 237 | SourceLocation InstantiateLoc = Identifier.getLocation(); |
| 238 | |
| 239 | // Replace the result token. |
| 240 | Identifier = MI->getReplacementToken(0); |
| 241 | |
| 242 | // Restore the StartOfLine/LeadingSpace markers. |
| 243 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 244 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
| 245 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 246 | // Update the tokens location to include both its instantiation and physical |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 247 | // locations. |
| 248 | SourceLocation Loc = |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 249 | SourceMgr.createInstantiationLoc(Identifier.getLocation(), InstantiateLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 250 | InstantiationEnd,Identifier.getLength()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 251 | Identifier.setLocation(Loc); |
| 252 | |
| 253 | // If this is #define X X, we must mark the result as unexpandible. |
| 254 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) |
| 255 | if (getMacroInfo(NewII) == MI) |
| 256 | Identifier.setFlag(Token::DisableExpand); |
| 257 | |
| 258 | // Since this is not an identifier token, it can't be macro expanded, so |
| 259 | // we're done. |
| 260 | ++NumFastMacroExpanded; |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // Start expanding the macro. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 265 | EnterMacro(Identifier, InstantiationEnd, Args); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 266 | |
| 267 | // Now that the macro is at the top of the include stack, ask the |
| 268 | // preprocessor to read the next token from it. |
| 269 | Lex(Identifier); |
| 270 | return false; |
| 271 | } |
| 272 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 273 | /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next |
| 274 | /// token is the '(' of the macro, this method is invoked to read all of the |
| 275 | /// actual arguments specified for the macro invocation. This returns null on |
| 276 | /// error. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 277 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 278 | MacroInfo *MI, |
| 279 | SourceLocation &MacroEnd) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 280 | // The number of fixed arguments to parse. |
| 281 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 282 | bool isVariadic = MI->isVariadic(); |
| 283 | |
| 284 | // Outer loop, while there are more arguments, keep reading them. |
| 285 | Token Tok; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 287 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 288 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 289 | LexUnexpandedToken(Tok); |
| 290 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
| 291 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 292 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 293 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 294 | // heap allocations in the common case. |
| 295 | llvm::SmallVector<Token, 64> ArgTokens; |
| 296 | |
| 297 | unsigned NumActuals = 0; |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 298 | while (Tok.isNot(tok::r_paren)) { |
| 299 | assert((Tok.is(tok::l_paren) || Tok.is(tok::comma)) && |
| 300 | "only expect argument separators here"); |
| 301 | |
| 302 | unsigned ArgTokenStart = ArgTokens.size(); |
| 303 | SourceLocation ArgStartLoc = Tok.getLocation(); |
| 304 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 305 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 306 | // that we already consumed the first one. |
| 307 | unsigned NumParens = 0; |
| 308 | |
| 309 | while (1) { |
| 310 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 311 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 312 | LexUnexpandedToken(Tok); |
| 313 | |
| 314 | if (Tok.is(tok::eof) || Tok.is(tok::eom)) { // "#if f(<eof>" & "#if f(\n" |
| 315 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 316 | // Do not lose the EOF/EOM. Return it to the client. |
| 317 | MacroName = Tok; |
| 318 | return 0; |
| 319 | } else if (Tok.is(tok::r_paren)) { |
| 320 | // If we found the ) token, the macro arg list is done. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 321 | if (NumParens-- == 0) { |
| 322 | MacroEnd = Tok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 323 | break; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 324 | } |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 325 | } else if (Tok.is(tok::l_paren)) { |
| 326 | ++NumParens; |
| 327 | } else if (Tok.is(tok::comma) && NumParens == 0) { |
| 328 | // Comma ends this argument if there are more fixed arguments expected. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 329 | // However, if this is a variadic macro, and this is part of the |
| 330 | // variadic part, then the comma is just an argument token. |
| 331 | if (!isVariadic) break; |
| 332 | if (NumFixedArgsLeft > 1) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 333 | break; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 334 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
| 335 | // If this is a comment token in the argument list and we're just in |
| 336 | // -C mode (not -CC mode), discard the comment. |
| 337 | continue; |
Chris Lattner | 5c497a8 | 2009-04-18 06:44:18 +0000 | [diff] [blame] | 338 | } else if (Tok.getIdentifierInfo() != 0) { |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 339 | // Reading macro arguments can cause macros that we are currently |
| 340 | // expanding from to be popped off the expansion stack. Doing so causes |
| 341 | // them to be reenabled for expansion. Here we record whether any |
| 342 | // identifiers we lex as macro arguments correspond to disabled macros. |
| 343 | // If so, we mark the token as noexpand. This is a subtle aspect of |
| 344 | // C99 6.10.3.4p2. |
| 345 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 346 | if (!MI->isEnabled()) |
| 347 | Tok.setFlag(Token::DisableExpand); |
| 348 | } |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 349 | ArgTokens.push_back(Tok); |
| 350 | } |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 351 | |
| 352 | // If this was an empty argument list foo(), don't add this as an empty |
| 353 | // argument. |
| 354 | if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) |
| 355 | break; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 356 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 357 | // If this is not a variadic macro, and too many args were specified, emit |
| 358 | // an error. |
| 359 | if (!isVariadic && NumFixedArgsLeft == 0) { |
| 360 | if (ArgTokens.size() != ArgTokenStart) |
| 361 | ArgStartLoc = ArgTokens[ArgTokenStart].getLocation(); |
| 362 | |
| 363 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 364 | // Emitting it at the , could be far away from the macro name. |
| 365 | Diag(ArgStartLoc, diag::err_too_many_args_in_macro_invoc); |
| 366 | return 0; |
| 367 | } |
| 368 | |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 369 | // Empty arguments are standard in C99 and supported as an extension in |
| 370 | // other modes. |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 371 | if (ArgTokens.size() == ArgTokenStart && !Features.C99) |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 372 | Diag(Tok, diag::ext_empty_fnmacro_arg); |
| 373 | |
| 374 | // Add a marker EOF token to the end of the token list for this argument. |
| 375 | Token EOFTok; |
| 376 | EOFTok.startToken(); |
| 377 | EOFTok.setKind(tok::eof); |
Chris Lattner | e768988 | 2009-01-26 20:24:53 +0000 | [diff] [blame] | 378 | EOFTok.setLocation(Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 379 | EOFTok.setLength(0); |
| 380 | ArgTokens.push_back(EOFTok); |
| 381 | ++NumActuals; |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 382 | assert(NumFixedArgsLeft != 0 && "Too many arguments parsed"); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 383 | --NumFixedArgsLeft; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 384 | } |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 385 | |
| 386 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 387 | // arguments. |
| 388 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 389 | |
| 390 | // See MacroArgs instance var for description of this. |
| 391 | bool isVarargsElided = false; |
| 392 | |
| 393 | if (NumActuals < MinArgsExpected) { |
| 394 | // There are several cases where too few arguments is ok, handle them now. |
Chris Lattner | 97e2de1 | 2009-04-20 21:08:10 +0000 | [diff] [blame] | 395 | if (NumActuals == 0 && MinArgsExpected == 1) { |
| 396 | // #define A(X) or #define A(...) ---> A() |
| 397 | |
| 398 | // If there is exactly one argument, and that argument is missing, |
| 399 | // then we have an empty "()" argument empty list. This is fine, even if |
| 400 | // the macro expects one argument (the argument is just empty). |
| 401 | isVarargsElided = MI->isVariadic(); |
| 402 | } else if (MI->isVariadic() && |
| 403 | (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) |
| 404 | (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 405 | // Varargs where the named vararg parameter is missing: ok as extension. |
| 406 | // #define A(x, ...) |
| 407 | // A("blah") |
| 408 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 409 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 410 | // Remember this occurred, allowing us to elide the comma when used for |
Chris Lattner | 63bc035 | 2008-05-08 05:10:33 +0000 | [diff] [blame] | 411 | // cases like: |
| 412 | // #define A(x, foo...) blah(a, ## foo) |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 413 | // #define B(x, ...) blah(a, ## __VA_ARGS__) |
| 414 | // #define C(...) blah(a, ## __VA_ARGS__) |
| 415 | // A(x) B(x) C() |
Chris Lattner | 97e2de1 | 2009-04-20 21:08:10 +0000 | [diff] [blame] | 416 | isVarargsElided = true; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 417 | } else { |
| 418 | // Otherwise, emit the error. |
| 419 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | // Add a marker EOF token to the end of the token list for this argument. |
| 424 | SourceLocation EndLoc = Tok.getLocation(); |
| 425 | Tok.startToken(); |
| 426 | Tok.setKind(tok::eof); |
| 427 | Tok.setLocation(EndLoc); |
| 428 | Tok.setLength(0); |
| 429 | ArgTokens.push_back(Tok); |
Chris Lattner | 9fc9e77 | 2009-05-13 00:55:26 +0000 | [diff] [blame] | 430 | |
| 431 | // If we expect two arguments, add both as empty. |
| 432 | if (NumActuals == 0 && MinArgsExpected == 2) |
| 433 | ArgTokens.push_back(Tok); |
| 434 | |
Chris Lattner | 0a4f1b9 | 2009-04-18 01:13:56 +0000 | [diff] [blame] | 435 | } else if (NumActuals > MinArgsExpected && !MI->isVariadic()) { |
| 436 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 437 | // Emitting it at the , could be far away from the macro name. |
| 438 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
| 439 | return 0; |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | return MacroArgs::create(MI, &ArgTokens[0], ArgTokens.size(),isVarargsElided); |
| 443 | } |
| 444 | |
| 445 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 446 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 447 | /// the identifier tokens inserted. |
| 448 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 449 | Preprocessor &PP) { |
| 450 | time_t TT = time(0); |
| 451 | struct tm *TM = localtime(&TT); |
| 452 | |
| 453 | static const char * const Months[] = { |
| 454 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 455 | }; |
| 456 | |
| 457 | char TmpBuffer[100]; |
| 458 | sprintf(TmpBuffer, "\"%s %2d %4d\"", Months[TM->tm_mon], TM->tm_mday, |
| 459 | TM->tm_year+1900); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 460 | |
| 461 | Token TmpTok; |
| 462 | TmpTok.startToken(); |
| 463 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 464 | DATELoc = TmpTok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 465 | |
| 466 | sprintf(TmpBuffer, "\"%02d:%02d:%02d\"", TM->tm_hour, TM->tm_min, TM->tm_sec); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 467 | PP.CreateString(TmpBuffer, strlen(TmpBuffer), TmpTok); |
| 468 | TIMELoc = TmpTok.getLocation(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 472 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 473 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 474 | // Figure out which token this is. |
| 475 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 476 | assert(II && "Can't be a macro without id info!"); |
| 477 | |
| 478 | // If this is an _Pragma directive, expand it, invoke the pragma handler, then |
| 479 | // lex the token after it. |
| 480 | if (II == Ident_Pragma) |
| 481 | return Handle_Pragma(Tok); |
| 482 | |
| 483 | ++NumBuiltinMacroExpanded; |
| 484 | |
| 485 | char TmpBuffer[100]; |
| 486 | |
| 487 | // Set up the return result. |
| 488 | Tok.setIdentifierInfo(0); |
| 489 | Tok.clearFlag(Token::NeedsCleaning); |
| 490 | |
| 491 | if (II == Ident__LINE__) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 492 | // C99 6.10.8: "__LINE__: The presumed line number (within the current |
| 493 | // source file) of the current source line (an integer constant)". This can |
| 494 | // be affected by #line. |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 495 | SourceLocation Loc = Tok.getLocation(); |
| 496 | |
Chris Lattner | dff070f | 2009-04-18 22:29:33 +0000 | [diff] [blame] | 497 | // Advance to the location of the first _, this might not be the first byte |
| 498 | // of the token if it starts with an escaped newline. |
| 499 | Loc = AdvanceToTokenCharacter(Loc, 0); |
| 500 | |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 501 | // One wrinkle here is that GCC expands __LINE__ to location of the *end* of |
| 502 | // a macro instantiation. This doesn't matter for object-like macros, but |
| 503 | // can matter for a function-like macro that expands to contain __LINE__. |
| 504 | // Skip down through instantiation points until we find a file loc for the |
| 505 | // end of the instantiation history. |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 506 | Loc = SourceMgr.getInstantiationRange(Loc).second; |
Chris Lattner | 081927b | 2009-02-15 21:06:39 +0000 | [diff] [blame] | 507 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 508 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 509 | // __LINE__ expands to a simple numeric value. |
| 510 | sprintf(TmpBuffer, "%u", PLoc.getLine()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 511 | Tok.setKind(tok::numeric_constant); |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 512 | CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 513 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 514 | // C99 6.10.8: "__FILE__: The presumed name of the current source file (a |
| 515 | // character string literal)". This can be affected by #line. |
| 516 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 517 | |
| 518 | // __BASE_FILE__ is a GNU extension that returns the top of the presumed |
| 519 | // #include stack instead of the current file. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 520 | if (II == Ident__BASE_FILE__) { |
| 521 | Diag(Tok, diag::ext_pp_base_file); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 522 | SourceLocation NextLoc = PLoc.getIncludeLoc(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 523 | while (NextLoc.isValid()) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 524 | PLoc = SourceMgr.getPresumedLoc(NextLoc); |
| 525 | NextLoc = PLoc.getIncludeLoc(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 530 | std::string FN = PLoc.getFilename(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 531 | FN = '"' + Lexer::Stringify(FN) + '"'; |
| 532 | Tok.setKind(tok::string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 533 | CreateString(&FN[0], FN.size(), Tok, Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 534 | } else if (II == Ident__DATE__) { |
| 535 | if (!DATELoc.isValid()) |
| 536 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 537 | Tok.setKind(tok::string_literal); |
| 538 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 539 | Tok.setLocation(SourceMgr.createInstantiationLoc(DATELoc, Tok.getLocation(), |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 540 | Tok.getLocation(), |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 541 | Tok.getLength())); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 542 | } else if (II == Ident__TIME__) { |
| 543 | if (!TIMELoc.isValid()) |
| 544 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 545 | Tok.setKind(tok::string_literal); |
| 546 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 547 | Tok.setLocation(SourceMgr.createInstantiationLoc(TIMELoc, Tok.getLocation(), |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 548 | Tok.getLocation(), |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 549 | Tok.getLength())); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 550 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 551 | Diag(Tok, diag::ext_pp_include_level); |
| 552 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 553 | // Compute the presumed include depth of this token. This can be affected |
| 554 | // by GNU line markers. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 555 | unsigned Depth = 0; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 556 | |
| 557 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 558 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 559 | for (; PLoc.isValid(); ++Depth) |
| 560 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 561 | |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 562 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 563 | sprintf(TmpBuffer, "%u", Depth); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 564 | Tok.setKind(tok::numeric_constant); |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 565 | CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 566 | } else if (II == Ident__TIMESTAMP__) { |
| 567 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 568 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 569 | Diag(Tok, diag::ext_pp_timestamp); |
| 570 | |
| 571 | // Get the file that we are lexing out of. If we're currently lexing from |
| 572 | // a macro, dig into the include stack. |
| 573 | const FileEntry *CurFile = 0; |
Ted Kremenek | a275a19 | 2008-11-20 01:35:24 +0000 | [diff] [blame] | 574 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 575 | |
| 576 | if (TheLexer) |
Ted Kremenek | ac80c6e | 2008-11-19 22:55:25 +0000 | [diff] [blame] | 577 | CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 578 | |
| 579 | // If this file is older than the file it depends on, emit a diagnostic. |
| 580 | const char *Result; |
| 581 | if (CurFile) { |
| 582 | time_t TT = CurFile->getModificationTime(); |
| 583 | struct tm *TM = localtime(&TT); |
| 584 | Result = asctime(TM); |
| 585 | } else { |
| 586 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 587 | } |
| 588 | TmpBuffer[0] = '"'; |
| 589 | strcpy(TmpBuffer+1, Result); |
| 590 | unsigned Len = strlen(TmpBuffer); |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 591 | TmpBuffer[Len] = '"'; // Replace the newline with a quote. |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 592 | Tok.setKind(tok::string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 593 | CreateString(TmpBuffer, Len+1, Tok, Tok.getLocation()); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 594 | } else if (II == Ident__COUNTER__) { |
| 595 | Diag(Tok, diag::ext_pp_counter); |
| 596 | |
| 597 | // __COUNTER__ expands to a simple numeric value. |
| 598 | sprintf(TmpBuffer, "%u", CounterValue++); |
| 599 | Tok.setKind(tok::numeric_constant); |
| 600 | CreateString(TmpBuffer, strlen(TmpBuffer), Tok, Tok.getLocation()); |
Chris Lattner | a3b605e | 2008-03-09 03:13:06 +0000 | [diff] [blame] | 601 | } else { |
| 602 | assert(0 && "Unknown identifier!"); |
| 603 | } |
| 604 | } |