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